https://github.com/adrian-prantl created 
https://github.com/llvm/llvm-project/pull/119620

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

>From 104f0d227f5642bf7a02311482fafb4edc667c67 Mon Sep 17 00:00:00 2001
From: Adrian Prantl <apra...@apple.com>
Date: Wed, 11 Dec 2024 13:57:43 -0800
Subject: [PATCH] [lldb] Disallow left shifts of negative values in the
 interpreter

This trips UBSAN and probably isn't partiuclarly useful either.
---
 lldb/source/DataFormatters/FormatterBytecode.cpp       | 8 +++++---
 lldb/unittests/DataFormatter/FormatterBytecodeTest.cpp | 7 +++++--
 2 files changed, 10 insertions(+), 5 deletions(-)

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;

_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to