Skip to content

Commit b6c6ff3

Browse files
authoredOct 19, 2021
Convert calls to Rust's panic functions to a call to a marker (#763)
This removes the calls to panic functions called by Rust programs. Removing the calls enables dead code elimination to remove the arguments flowing into the panic function call.
1 parent b1c47e2 commit b6c6ff3

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed
 

‎include/smack/Naming.h

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class Naming {
9898
static const std::string RUST_LANG_START_INTERNAL;
9999
static const std::vector<std::string> RUST_PANICS;
100100
static const std::string RUST_PANIC_ANNOTATION;
101+
static const std::string RUST_PANIC_MARKER;
101102

102103
static const std::string INT_WRAP_SIGNED_FUNCTION;
103104
static const std::string INT_WRAP_UNSIGNED_FUNCTION;

‎lib/smack/Naming.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ const std::vector<std::string> Naming::RUST_PANICS = {
7272

7373
const std::string Naming::RUST_PANIC_ANNOTATION = "rust_panic";
7474

75+
const std::string Naming::RUST_PANIC_MARKER = "__SMACK_RUST_PANIC_MARKER";
76+
7577
const std::string Naming::BLOCK_LBL = "$bb";
7678
const std::string Naming::RET_VAR = "$r";
7779
const std::string Naming::EXN_VAR = "$exn";

‎lib/smack/RustFixes.cpp

+22-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ bool isRustNameMatch(StringRef search, StringRef name) {
2727
return hashed_match || exact_match;
2828
}
2929

30-
bool replaceRustMemoryFunctions(Function &f) {
30+
bool replaceSpecialRustFunctions(Function &f) {
31+
std::vector<Instruction *> to_remove;
32+
3133
bool changed = false;
3234
static const std::map<StringRef, StringRef> alloc_fns = {
3335
{"_ZN5alloc5alloc5alloc17h", "__smack_rust_std_alloc"},
@@ -53,9 +55,27 @@ bool replaceRustMemoryFunctions(Function &f) {
5355
ci->setCalledFunction(replacement);
5456
}
5557
}
58+
59+
if (Naming::isRustPanic(name)) {
60+
// Remove the calls rust panic functions
61+
changed = true;
62+
// Keep track of the panic call
63+
Module *m = f->getParent();
64+
FunctionCallee marker = m->getOrInsertFunction(
65+
Naming::RUST_PANIC_MARKER, Type::getVoidTy(m->getContext()));
66+
CallInst *panic_marker = CallInst::Create(marker);
67+
panic_marker->setDebugLoc(ci->getDebugLoc());
68+
panic_marker->insertBefore(ci);
69+
to_remove.push_back(ci);
70+
}
5671
}
5772
}
5873
}
74+
75+
for (auto ci : to_remove) {
76+
ci->eraseFromParent();
77+
}
78+
5979
return changed;
6080
}
6181

@@ -127,7 +147,7 @@ bool RustFixes::runOnFunction(Function &F) {
127147
if (name == "main") {
128148
result |= fixEntry(F);
129149
}
130-
result |= replaceRustMemoryFunctions(F);
150+
result |= replaceSpecialRustFunctions(F);
131151
}
132152

133153
return result;

‎lib/smack/SmackInstGenerator.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ void SmackInstGenerator::visitCallInst(llvm::CallInst &ci) {
648648

649649
StringRef name = f->hasName() ? f->getName() : "";
650650

651-
if (SmackOptions::RustPanics && Naming::isRustPanic(name) &&
651+
if (SmackOptions::RustPanics && name == Naming::RUST_PANIC_MARKER &&
652652
SmackOptions::shouldCheckFunction(
653653
ci.getParent()->getParent()->getName())) {
654654
// Convert Rust's panic functions into assertion violations

0 commit comments

Comments
 (0)
Please sign in to comment.