Skip to content

Commit 53a3cea

Browse files
committed
[BOLT] Only run pac-ret passes if AllowPacret is set.
Tests: - negate-ra-state-incorrect.s: added the --allow-experimental-pacret flag. - negate-ra-state-flag.cpp: fails if the flag is not set.
1 parent 2a93a0d commit 53a3cea

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

bolt/lib/Rewrite/BinaryPassManager.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ Error BinaryFunctionPassManager::runPasses() {
352352
Error BinaryFunctionPassManager::runAllPasses(BinaryContext &BC) {
353353
BinaryFunctionPassManager Manager(BC);
354354

355-
if (BC.isAArch64())
355+
if (BC.isAArch64() && AllowPacret)
356356
Manager.registerPass(std::make_unique<MarkRAStates>());
357357

358358
Manager.registerPass(
@@ -515,7 +515,8 @@ Error BinaryFunctionPassManager::runAllPasses(BinaryContext &BC) {
515515
// relocations out of range and crash during linking.
516516
Manager.registerPass(std::make_unique<LongJmpPass>(PrintLongJmp));
517517

518-
Manager.registerPass(std::make_unique<InsertNegateRAState>());
518+
if (AllowPacret)
519+
Manager.registerPass(std::make_unique<InsertNegateRAState>());
519520
}
520521

521522
// This pass should always run last.*
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// This test checks that BOLT refuses to optimize binaries
2+
// compiled with -mbranch-protection=pac-ret, unless the
3+
// --allow-experimental-pacret flag is set.
4+
5+
// REQUIRES: system-linux,bolt-runtime
6+
// RUN: %clangxx --target=aarch64-unknown-linux-gnu \
7+
// RUN: -mbranch-protection=pac-ret -Wl,-q %s -o %t.exe
8+
// RUN: not --crash llvm-bolt %t.exe -o %t.bolt.exe 2>&1 | FileCheck %s
9+
10+
// CHECK: BOLT-ERROR: set --allow-experimental-pacret to allow processing
11+
12+
extern "C" int printf(const char *, ...);
13+
14+
void bar() { throw 10; }
15+
16+
void foo() {
17+
try {
18+
bar();
19+
} catch (int e) {
20+
printf("Exception caught: %d\n", e);
21+
}
22+
}
23+
24+
int main() {
25+
foo();
26+
return 0;
27+
}

bolt/test/AArch64/negate-ra-state-incorrect.s

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown %s -o %t.o
22
# RUN: %clang %cflags %t.o -o %t.exe -Wl,-q
3-
# RUN: llvm-bolt %t.exe -o %t.exe.bolt | FileCheck %s
3+
# RUN: llvm-bolt %t.exe -o %t.exe.bolt --allow-experimental-pacret \
4+
# RUN: | FileCheck %s
45

56
# check that the output is listing foo as incorrect
67
# CHECK: BOLT-INFO: inconsistent RAStates in function foo

0 commit comments

Comments
 (0)