[llvm-branch-commits] [asan] Limit priority ctor to kMax-1 (PR #101772)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Fri Aug 2 16:13:07 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Vitaly Buka (vitalybuka)
<details>
<summary>Changes</summary>
Reserve maximal availibe priority to
runtime. We need to run code in sanitizer
runtime after all C++ constructors.
https://clang.llvm.org/docs/AttributeReference.html#constructor
---
Full diff: https://github.com/llvm/llvm-project/pull/101772.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp (+24)
- (modified) llvm/test/Instrumentation/AddressSanitizer/instrument_late_initializer.ll (+1-1)
``````````diff
diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
index 9fb1df7ab2b79..734cbd0515ea8 100644
--- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
@@ -133,6 +133,7 @@ const char kAsanModuleDtorName[] = "asan.module_dtor";
static const uint64_t kAsanCtorAndDtorPriority = 1;
// On Emscripten, the system needs more than one priorities for constructors.
static const uint64_t kAsanEmscriptenCtorAndDtorPriority = 50;
+static const uint64_t kMaxCtorAndDtorPriority = 65535;
const char kAsanReportErrorTemplate[] = "__asan_report_";
const char kAsanRegisterGlobalsName[] = "__asan_register_globals";
const char kAsanUnregisterGlobalsName[] = "__asan_unregister_globals";
@@ -1993,6 +1994,29 @@ void ModuleAddressSanitizer::createInitializerPoisonCalls(
poisonOneInitializer(*F, ModuleName);
}
}
+ assert(ClInitializers);
+ updateGlobalCtors(M, [](Constant *C) -> Constant * {
+ ConstantStruct *CS = dyn_cast<ConstantStruct>(C);
+ if (!CS)
+ return C;
+ auto *Priority = cast<ConstantInt>(CS->getOperand(0));
+ if (Priority->getLimitedValue() != kMaxCtorAndDtorPriority)
+ return C;
+ // As optimization, runtime needs to execute callback just after all
+ // constructors. We going to set priority to the max allowed value. However,
+ // the default constructor priorily is already max, so as-is we will not be
+ // able to guaranty desired order. So reduce the priority by one to reserve
+ // max value for the constructor in runtime.
+ StructType *EltTy = cast<StructType>(CS->getType());
+ Constant *CSVals[3] = {
+ ConstantInt::getSigned(Priority->getType(),
+ kMaxCtorAndDtorPriority - 1),
+ CS->getOperand(1),
+ CS->getOperand(2),
+ };
+ return cast<ConstantStruct>(
+ ConstantStruct::get(EltTy, ArrayRef(CSVals, EltTy->getNumElements())));
+ });
}
const GlobalVariable *
diff --git a/llvm/test/Instrumentation/AddressSanitizer/instrument_late_initializer.ll b/llvm/test/Instrumentation/AddressSanitizer/instrument_late_initializer.ll
index 45d526a42c9f7..2225ccb217208 100644
--- a/llvm/test/Instrumentation/AddressSanitizer/instrument_late_initializer.ll
+++ b/llvm/test/Instrumentation/AddressSanitizer/instrument_late_initializer.ll
@@ -9,7 +9,7 @@ target triple = "x86_64-unknown-linux-gnu"
@g = internal global i32 0, align 4, sanitize_address_dyninit ; With dynamic initializer.
;.
-; CHECK: @llvm.global_ctors = appending global [2 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__late_ctor, ptr null }, { i32, ptr, ptr } { i32 1, ptr @asan.module_ctor, ptr @asan.module_ctor }]
+; CHECK: @llvm.global_ctors = appending global [2 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65534, ptr @__late_ctor, ptr null }, { i32, ptr, ptr } { i32 1, ptr @asan.module_ctor, ptr @asan.module_ctor }]
;.
; NOINIT: @llvm.global_ctors = appending global [2 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__late_ctor, ptr null }, { i32, ptr, ptr } { i32 1, ptr @asan.module_ctor, ptr @asan.module_ctor }]
;.
``````````
</details>
https://github.com/llvm/llvm-project/pull/101772
More information about the llvm-branch-commits
mailing list