Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lldb] Disallow left shifts of negative values in the interpreter #119620

Merged
merged 1 commit into from
Dec 11, 2024

Conversation

adrian-prantl
Copy link
Collaborator

This trips UBSAN and probably isn't partiuclarly useful either.

This trips UBSAN and probably isn't partiuclarly useful either.
@llvmbot
Copy link
Member

llvmbot commented Dec 11, 2024

@llvm/pr-subscribers-lldb

Author: Adrian Prantl (adrian-prantl)

Changes

This trips UBSAN and probably isn't partiuclarly useful either.


Full diff: https://github.com/llvm/llvm-project/pull/119620.diff

2 Files Affected:

  • (modified) lldb/source/DataFormatters/FormatterBytecode.cpp (+5-3)
  • (modified) lldb/unittests/DataFormatter/FormatterBytecodeTest.cpp (+5-2)
diff --git a/lldb/source/DataFormatters/FormatterBytecode.cpp b/lldb/source/DataFormatters/FormatterBytecode.cpp
index f344fbaff6f02a..e49c7506781875 100644
--- a/lldb/source/DataFormatters/FormatterBytecode.cpp
+++ b/lldb/source/DataFormatters/FormatterBytecode.cpp
@@ -379,7 +379,7 @@ llvm::Error Interpret(std::vector<ControlStackElement> &control,
       BINOP_CHECKZERO(%);
       continue;
     case op_shl:
-#define SHIFTOP(OP)                                                            \
+#define SHIFTOP(OP, LEFT)                                                      \
   {                                                                            \
     TYPE_CHECK(Any, UInt);                                                     \
     uint64_t y = data.Pop<uint64_t>();                                         \
@@ -390,16 +390,18 @@ llvm::Error Interpret(std::vector<ControlStackElement> &control,
       data.Push(x OP y);                                                       \
     } else if (std::holds_alternative<int64_t>(data.back())) {                 \
       int64_t x = data.Pop<int64_t>();                                         \
+      if (x < 0 && LEFT)                                                       \
+        return error("left shift of negative value");                          \
       if (y > 64)                                                              \
         return error("shift out of bounds");                                   \
       data.Push(x OP y);                                                       \
     } else                                                                     \
       return error("unsupported data types");                                  \
   }
-      SHIFTOP(<<);
+      SHIFTOP(<<, true);
       continue;
     case op_shr:
-      SHIFTOP(<<);
+      SHIFTOP(>>, false);
       continue;
     case op_and:
       BINOP(&);
diff --git a/lldb/unittests/DataFormatter/FormatterBytecodeTest.cpp b/lldb/unittests/DataFormatter/FormatterBytecodeTest.cpp
index 15d9229de00332..7307db650c1629 100644
--- a/lldb/unittests/DataFormatter/FormatterBytecodeTest.cpp
+++ b/lldb/unittests/DataFormatter/FormatterBytecodeTest.cpp
@@ -147,9 +147,12 @@ TEST_F(FormatterBytecodeTest, ArithOps) {
   {
     DataStack data;
     unsigned char minus_one = 127;
-    ASSERT_TRUE(
+    ASSERT_FALSE(
         Interpret({op_lit_int, minus_one, op_lit_uint, 2, op_shl}, data));
-    ASSERT_EQ(data.Pop<int64_t>(), -4);
+    unsigned char minus_two = 126;
+    ASSERT_TRUE(
+        Interpret({op_lit_int, minus_two, op_lit_uint, 1, op_shr}, data));
+    ASSERT_EQ(data.Pop<int64_t>(), -1);
   }
   {
     DataStack data;

@adrian-prantl adrian-prantl merged commit 2470cfa into llvm:main Dec 11, 2024
7 of 8 checks passed
adrian-prantl added a commit to adrian-prantl/llvm-project that referenced this pull request Dec 13, 2024
…vm#119620)

This trips UBSAN and probably isn't partiuclarly useful either.

(cherry picked from commit 2470cfa)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants