Skip to content

Commit 507e594

Browse files
committed
llvm2alive: fix ordering issue with UB-triggering metadata
1 parent b4b59bc commit 507e594

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

llvm_util/llvm2alive.cpp

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,8 +1321,10 @@ class llvm2alive_ : public llvm::InstVisitor<llvm2alive_, unique_ptr<Instr>> {
13211321
break;
13221322
}
13231323

1324+
// these need to go last
13241325
case LLVMContext::MD_noundef:
1325-
BB->addInstr(make_unique<Assume>(*i, Assume::WellDefined));
1326+
case LLVMContext::MD_dereferenceable:
1327+
case LLVMContext::MD_dereferenceable_or_null:
13261328
break;
13271329

13281330
case LLVMContext::MD_callees: {
@@ -1365,16 +1367,6 @@ class llvm2alive_ : public llvm::InstVisitor<llvm2alive_, unique_ptr<Instr>> {
13651367
break;
13661368
}
13671369

1368-
case LLVMContext::MD_dereferenceable:
1369-
case LLVMContext::MD_dereferenceable_or_null: {
1370-
auto kind = ID == LLVMContext::MD_dereferenceable
1371-
? Assume::Dereferenceable : Assume::DereferenceableOrNull;
1372-
auto bytes = get_operand(
1373-
llvm::mdconst::extract<llvm::ConstantInt>(Node->getOperand(0)));
1374-
BB->addInstr(make_unique<Assume>(vector<Value*>{i, bytes}, kind));
1375-
break;
1376-
}
1377-
13781370
// non-relevant for correctness
13791371
case LLVMContext::MD_loop:
13801372
case LLVMContext::MD_nosanitize:
@@ -1395,6 +1387,33 @@ class llvm2alive_ : public llvm::InstVisitor<llvm2alive_, unique_ptr<Instr>> {
13951387
return false;
13961388
}
13971389
}
1390+
1391+
auto get_md = [&](unsigned id) -> llvm::MDNode* {
1392+
for (auto &[node_id, node] : MDs) {
1393+
if (id == node_id)
1394+
return node;
1395+
}
1396+
return nullptr;
1397+
};
1398+
1399+
// these produce UB, so need to go after the value transformers above
1400+
if (get_md(LLVMContext::MD_noundef))
1401+
BB->addInstr(make_unique<Assume>(*i, Assume::WellDefined));
1402+
1403+
if (auto *Node = get_md(LLVMContext::MD_dereferenceable)) {
1404+
auto kind = Assume::Dereferenceable;
1405+
auto bytes = get_operand(
1406+
llvm::mdconst::extract<llvm::ConstantInt>(Node->getOperand(0)));
1407+
BB->addInstr(make_unique<Assume>(vector<Value*>{i, bytes}, kind));
1408+
}
1409+
1410+
if (auto *Node = get_md(LLVMContext::MD_dereferenceable_or_null)) {
1411+
auto kind = Assume::DereferenceableOrNull;
1412+
auto bytes = get_operand(
1413+
llvm::mdconst::extract<llvm::ConstantInt>(Node->getOperand(0)));
1414+
BB->addInstr(make_unique<Assume>(vector<Value*>{i, bytes}, kind));
1415+
}
1416+
13981417
return true;
13991418
}
14001419

0 commit comments

Comments
 (0)