Skip to content

Commit 36b344c

Browse files
committed
Fix bug with uninitialized MONITOR variable causing UB
1 parent 05e5de1 commit 36b344c

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

lib/Arch/Arch.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,11 @@ void Arch::InitializeEmptyLiftedFunction(llvm::Function *func) const {
758758
llvm::IRBuilder<> ir(block);
759759
ir.CreateAlloca(u8, nullptr, "BRANCH_TAKEN");
760760
ir.CreateAlloca(addr, nullptr, "RETURN_PC");
761-
ir.CreateAlloca(addr, nullptr, "MONITOR");
761+
762+
// NOTE: we need to start with an initialize MONITOR state,
763+
// otherwise STLXR without a preceding LDXR/LDAXR will do UB
764+
auto monitor = ir.CreateAlloca(addr, nullptr, "MONITOR");
765+
ir.CreateStore(llvm::ConstantInt::get(addr, 0), monitor);
762766

763767
// NOTE(pag): `PC` and `NEXT_PC` are handled by
764768
// `FinishLiftedFunctionInitialization`.

0 commit comments

Comments
 (0)