[clang] [clang-repl] Fix unboxing of va_args Value on 32-bit ARM (#94994) (PR #96900)

Stefan Gränitz via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 27 05:33:38 PDT 2024


https://github.com/weliveindetail created https://github.com/llvm/llvm-project/pull/96900

todo

>From 2e1eac5919a56fd15a7c79281d6544c4958d6874 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stefan=20Gr=C3=A4nitz?= <stefan.graenitz at gmail.com>
Date: Thu, 27 Jun 2024 14:28:03 +0200
Subject: [PATCH] [clang-repl] Fix unboxing of va_args Value on 32-bit ARM

---
 clang/include/clang/Interpreter/Value.h       |  2 ++
 clang/lib/Interpreter/Interpreter.cpp         | 31 ++++++++++++++++++-
 .../unittests/Interpreter/InterpreterTest.cpp |  2 --
 3 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Interpreter/Value.h b/clang/include/clang/Interpreter/Value.h
index d70e8f8719026..f1dcae2d672c0 100644
--- a/clang/include/clang/Interpreter/Value.h
+++ b/clang/include/clang/Interpreter/Value.h
@@ -98,6 +98,8 @@ class REPL_EXTERNAL_VISIBILITY Value {
     void *m_Ptr;
   };
 
+  static_assert(sizeof(Storage) <= 2 * sizeof(void *), "va_args");
+
 public:
   enum Kind {
 #define X(type, name) K_##name,
diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp
index 7a95278914276..11f3273dff45f 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -877,7 +877,36 @@ extern "C" void REPL_EXTERNAL_VISIBILITY __clang_Interpreter_SetValueNoAlloc(
   } else {
     if (const auto *ET = QT->getAs<EnumType>())
       QT = ET->getDecl()->getIntegerType();
-    switch (QT->castAs<BuiltinType>()->getKind()) {
+
+    BuiltinType::Kind K = QT->castAs<BuiltinType>()->getKind();
+  #ifdef __arm__
+    switch (K) {
+    default:
+      llvm_unreachable("unknown type kind!");
+    case BuiltinType::Bool:
+    case BuiltinType::SChar:
+    case BuiltinType::Char_S:
+    case BuiltinType::Char_U:
+    case BuiltinType::UChar:
+    case BuiltinType::Short:
+    case BuiltinType::UShort:
+    case BuiltinType::Int:
+    case BuiltinType::UInt:
+    case BuiltinType::Long:
+    case BuiltinType::ULong:
+    case BuiltinType::LongLong:
+    case BuiltinType::ULongLong:
+    case BuiltinType::Float:
+    case BuiltinType::Double:
+      // Consume unused leading 32-bit of storage
+      va_arg(args, int);
+
+    case BuiltinType::LongDouble:
+      break;
+    }
+  #endif
+
+    switch (K) {
     default:
       llvm_unreachable("unknown type kind!");
       break;
diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp b/clang/unittests/Interpreter/InterpreterTest.cpp
index bbd854149d5f5..37978b09adad2 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -284,7 +284,6 @@ TEST_F(InterpreterTest, InstantiateTemplate) {
 
 // This test exposes an ARM specific problem in the interpreter, see
 // https://github.com/llvm/llvm-project/issues/94994.
-#ifndef __arm__
 TEST_F(InterpreterTest, Value) {
   std::vector<const char *> Args = {"-fno-sized-deallocation"};
   std::unique_ptr<Interpreter> Interp = createInterpreter(Args);
@@ -383,6 +382,5 @@ TEST_F(InterpreterTest, Value) {
   EXPECT_EQ(V9.getKind(), Value::K_PtrOrObj);
   EXPECT_TRUE(V9.isManuallyAlloc());
 }
-#endif /* ifndef __arm__ */
 
 } // end anonymous namespace



More information about the cfe-commits mailing list