Skip to content

Commit 2470cfa

Browse files
[lldb] Disallow left shifts of negative values in the interpreter (#119620)
This trips UBSAN and probably isn't partiuclarly useful either.
1 parent 6f013db commit 2470cfa

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

lldb/source/DataFormatters/FormatterBytecode.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ llvm::Error Interpret(std::vector<ControlStackElement> &control,
379379
BINOP_CHECKZERO(%);
380380
continue;
381381
case op_shl:
382-
#define SHIFTOP(OP) \
382+
#define SHIFTOP(OP, LEFT) \
383383
{ \
384384
TYPE_CHECK(Any, UInt); \
385385
uint64_t y = data.Pop<uint64_t>(); \
@@ -390,16 +390,18 @@ llvm::Error Interpret(std::vector<ControlStackElement> &control,
390390
data.Push(x OP y); \
391391
} else if (std::holds_alternative<int64_t>(data.back())) { \
392392
int64_t x = data.Pop<int64_t>(); \
393+
if (x < 0 && LEFT) \
394+
return error("left shift of negative value"); \
393395
if (y > 64) \
394396
return error("shift out of bounds"); \
395397
data.Push(x OP y); \
396398
} else \
397399
return error("unsupported data types"); \
398400
}
399-
SHIFTOP(<<);
401+
SHIFTOP(<<, true);
400402
continue;
401403
case op_shr:
402-
SHIFTOP(<<);
404+
SHIFTOP(>>, false);
403405
continue;
404406
case op_and:
405407
BINOP(&);

lldb/unittests/DataFormatter/FormatterBytecodeTest.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,12 @@ TEST_F(FormatterBytecodeTest, ArithOps) {
147147
{
148148
DataStack data;
149149
unsigned char minus_one = 127;
150-
ASSERT_TRUE(
150+
ASSERT_FALSE(
151151
Interpret({op_lit_int, minus_one, op_lit_uint, 2, op_shl}, data));
152-
ASSERT_EQ(data.Pop<int64_t>(), -4);
152+
unsigned char minus_two = 126;
153+
ASSERT_TRUE(
154+
Interpret({op_lit_int, minus_two, op_lit_uint, 1, op_shr}, data));
155+
ASSERT_EQ(data.Pop<int64_t>(), -1);
153156
}
154157
{
155158
DataStack data;

0 commit comments

Comments
 (0)