Skip to content

Commit 0b17248

Browse files
authored
fix: protect unsafe for return operand (#243)
1 parent 11f88d4 commit 0b17248

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/compiler/evm_frontend/evm_mir_compiler.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ EVMMirBuilder::U256Inst EVMMirBuilder::handleCompareEQZ(const U256Inst &LHS,
907907
false, Predicate, ResultType, OrResult, Zero);
908908

909909
// Convert to u256: result[0] = CmpResult extended to i64, others = 0
910-
Result[0] = CmpResult;
910+
Result[0] = protectUnsafeValue(CmpResult, MirI64Type);
911911
for (size_t I = 1; I < EVM_ELEMENTS_COUNT; ++I) {
912912
Result[I] = Zero;
913913
}
@@ -937,7 +937,7 @@ EVMMirBuilder::U256Inst EVMMirBuilder::handleCompareEQ(const U256Inst &LHS,
937937

938938
MType *MirI64Type =
939939
EVMFrontendContext::getMIRTypeFromEVMType(EVMType::UINT64);
940-
Result[0] = AndResult;
940+
Result[0] = protectUnsafeValue(AndResult, MirI64Type);
941941
MInstruction *Zero = createIntConstInstruction(MirI64Type, 0);
942942
for (size_t I = 1; I < EVM_ELEMENTS_COUNT; ++I) {
943943
Result[I] = Zero;
@@ -998,7 +998,7 @@ EVMMirBuilder::handleCompareGT_LT(const U256Inst &LHS, const U256Inst &RHS,
998998
}
999999

10001000
ZEN_ASSERT(FinalResult);
1001-
Result[0] = FinalResult;
1001+
Result[0] = protectUnsafeValue(FinalResult, MirI64Type);
10021002
for (size_t I = 1; I < EVM_ELEMENTS_COUNT; ++I) {
10031003
Result[I] = Zero;
10041004
}
@@ -1132,10 +1132,11 @@ EVMMirBuilder::handleLeftShift(const U256Inst &Value, MInstruction *ShiftAmount,
11321132

11331133
// Final result selection based on bounds checking and large shift flag
11341134
// result[I] = IsLargeShift ? 0 : (IsInBounds ? CombinedValue : 0)
1135-
Result[I] = createInstruction<SelectInstruction>(
1135+
MInstruction *FinalValue = createInstruction<SelectInstruction>(
11361136
false, MirI64Type, IsLargeShift, Zero,
11371137
createInstruction<SelectInstruction>(false, MirI64Type, IsInBounds,
11381138
CombinedValue, Zero));
1139+
Result[I] = protectUnsafeValue(FinalValue, MirI64Type);
11391140
}
11401141

11411142
return Result;
@@ -1252,10 +1253,11 @@ EVMMirBuilder::handleLogicalRightShift(const U256Inst &Value,
12521253

12531254
// Final result selection based on bounds checking and large shift flag
12541255
// result[I] = IsLargeShift ? 0 : (IsInBounds ? CombinedValue : 0)
1255-
Result[I] = createInstruction<SelectInstruction>(
1256+
MInstruction *FinalValue = createInstruction<SelectInstruction>(
12561257
false, MirI64Type, IsLargeShift, Zero,
12571258
createInstruction<SelectInstruction>(false, MirI64Type, IsInBounds,
12581259
CombinedValue, Zero));
1260+
Result[I] = protectUnsafeValue(FinalValue, MirI64Type);
12591261
}
12601262

12611263
return Result;
@@ -1370,10 +1372,11 @@ EVMMirBuilder::handleArithmeticRightShift(const U256Inst &Value,
13701372
MInstruction *CombinedValue = createInstruction<BinaryInstruction>(
13711373
false, OP_or, MirI64Type, ShiftedValue, CarryValue);
13721374

1373-
Result[I] = createInstruction<SelectInstruction>(
1375+
MInstruction *FinalValue = createInstruction<SelectInstruction>(
13741376
false, MirI64Type, IsLargeShift, LargeShiftResult,
13751377
createInstruction<SelectInstruction>(false, MirI64Type, IsInBounds,
13761378
CombinedValue, LargeShiftResult));
1379+
Result[I] = protectUnsafeValue(FinalValue, MirI64Type);
13771380
}
13781381

13791382
return Result;
@@ -1545,8 +1548,11 @@ EVMMirBuilder::handleSignextend(Operand IndexOp, Operand ValueOp) {
15451548
false, MirI64Type, IsEqual, ExtendedSignComp, ValueComponents[I]));
15461549

15471550
// If index >= 31, use original value; otherwise use sign-extended value
1548-
ResultComponents[I] = createInstruction<SelectInstruction>(
1549-
false, MirI64Type, NoExtension, ValueComponents[I], ComponentResult);
1551+
ResultComponents[I] =
1552+
protectUnsafeValue(createInstruction<SelectInstruction>(
1553+
false, MirI64Type, NoExtension,
1554+
ValueComponents[I], ComponentResult),
1555+
MirI64Type);
15501556
}
15511557

15521558
return Operand(ResultComponents, EVMType::UINT256);

src/compiler/evm_frontend/evm_mir_compiler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ class EVMMirBuilder final {
248248
MInstruction *Diff2 = createInstruction<BinaryInstruction>(
249249
false, OP_sub, MirI64Type, Diff1, Borrow);
250250

251-
Result[I] = Diff2;
251+
Result[I] = protectUnsafeValue(Diff2, MirI64Type);
252252

253253
// (LHS[I] < RHS[I]) || (Diff1 < Borrow)
254254
if (I < EVM_ELEMENTS_COUNT - 1) {

0 commit comments

Comments
 (0)