Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 48 additions & 7 deletions src/compiler/evm_frontend/evm_mir_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -984,13 +984,35 @@ void EVMMirBuilder::handleJump(Operand Dest) {
MBasicBlock *InvalidJumpBB =
getOrCreateExceptionSetBB(ErrorCode::EVMBadJumpDestination);
if (Dest.isConstant()) {
uint64_t ConstDest = Dest.getConstValue()[0];
const auto &ConstValue = Dest.getConstValue();
if ((ConstValue[3] | ConstValue[2] | ConstValue[1]) != 0) {
createInstruction<BrInstruction>(true, Ctx, InvalidJumpBB);
addSuccessor(InvalidJumpBB);
return;
}
uint64_t ConstDest = ConstValue[0];
implementConstantJump(ConstDest, InvalidJumpBB);
} else {
U256Inst DestComponents = extractU256Operand(Dest);
MInstruction *JumpTarget = DestComponents[0];
implementIndirectJump(JumpTarget, InvalidJumpBB);
return;
}

U256Inst DestComponents = extractU256Operand(Dest);
MInstruction *JumpTarget = DestComponents[0];
MType *MirI64Type =
EVMFrontendContext::getMIRTypeFromEVMType(EVMType::UINT64);
MInstruction *Zero = createIntConstInstruction(MirI64Type, 0);
MInstruction *HighOr = createInstruction<BinaryInstruction>(
false, OP_or, MirI64Type, DestComponents[1], DestComponents[2]);
HighOr = createInstruction<BinaryInstruction>(false, OP_or, MirI64Type,
HighOr, DestComponents[3]);
MInstruction *HighNonZero = createInstruction<CmpInstruction>(
false, CmpInstruction::Predicate::ICMP_NE, &Ctx.I64Type, HighOr, Zero);
MBasicBlock *ValidJumpBB = createBasicBlock();
createInstruction<BrIfInstruction>(true, Ctx, HighNonZero, InvalidJumpBB,
ValidJumpBB);
addSuccessor(InvalidJumpBB);
addSuccessor(ValidJumpBB);
setInsertBlock(ValidJumpBB);
implementIndirectJump(JumpTarget, InvalidJumpBB);
}

void EVMMirBuilder::handleJumpI(Operand Dest, Operand Cond) {
Expand Down Expand Up @@ -1034,9 +1056,28 @@ void EVMMirBuilder::handleJumpI(Operand Dest, Operand Cond) {
addSuccessor(FallThroughBB);
setInsertBlock(JumpTableBB);
if (Dest.isConstant()) {
uint64_t ConstDest = Dest.getConstValue()[0];
implementConstantJump(ConstDest, InvalidJumpBB);
const auto &ConstValue = Dest.getConstValue();
if ((ConstValue[3] | ConstValue[2] | ConstValue[1]) != 0) {
createInstruction<BrInstruction>(true, Ctx, InvalidJumpBB);
addSuccessor(InvalidJumpBB);
} else {
uint64_t ConstDest = ConstValue[0];
implementConstantJump(ConstDest, InvalidJumpBB);
}
} else {
MInstruction *HighOr = createInstruction<BinaryInstruction>(
false, OP_or, MirI64Type, DestComponents[1], DestComponents[2]);
HighOr = createInstruction<BinaryInstruction>(false, OP_or, MirI64Type,
HighOr, DestComponents[3]);
MInstruction *HighNonZero = createInstruction<CmpInstruction>(
false, CmpInstruction::Predicate::ICMP_NE, &Ctx.I64Type, HighOr,
Zero);
MBasicBlock *ValidJumpBB = createBasicBlock();
createInstruction<BrIfInstruction>(true, Ctx, HighNonZero, InvalidJumpBB,
ValidJumpBB);
addSuccessor(InvalidJumpBB);
addSuccessor(ValidJumpBB);
setInsertBlock(ValidJumpBB);
implementIndirectJump(JumpTarget, InvalidJumpBB);
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/evm/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <cstddef>
#include <cstring>
#include <limits>

using namespace zen;
using namespace zen::evm;
Expand Down Expand Up @@ -335,7 +336,10 @@ void BaseInterpreter::interpret() {
}

auto Uint256ToUint64 = [](const intx::uint256 &Value) -> uint64_t {
return static_cast<uint64_t>(Value & 0xFFFFFFFFFFFFFFFFULL);
if ((Value[3] | Value[2] | Value[1]) != 0) {
return std::numeric_limits<uint64_t>::max();
}
return Value[0];
};

while (Frame->Pc < CodeSize) {
Expand Down
Loading