[llvm] 02ea369 - Utils: Fix appending to global_ctors with program address spaces
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 29 07:54:15 PST 2022
Author: Matt Arsenault
Date: 2022-11-29T10:54:11-05:00
New Revision: 02ea3694a0a541f6328d120f02eb04977c1fc840
URL: https://github.com/llvm/llvm-project/commit/02ea3694a0a541f6328d120f02eb04977c1fc840
DIFF: https://github.com/llvm/llvm-project/commit/02ea3694a0a541f6328d120f02eb04977c1fc840.diff
LOG: Utils: Fix appending to global_ctors with program address spaces
Also fix constructing sanitizer constructors in address space 0 so
it's testable (this was also failing the verifier on the type of
global_ctors).
Added:
llvm/test/Instrumentation/AddressSanitizer/program-addrspace.ll
Modified:
llvm/lib/Transforms/Utils/ModuleUtils.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Utils/ModuleUtils.cpp b/llvm/lib/Transforms/Utils/ModuleUtils.cpp
index da588b3445e77..b0f784b29fa19 100644
--- a/llvm/lib/Transforms/Utils/ModuleUtils.cpp
+++ b/llvm/lib/Transforms/Utils/ModuleUtils.cpp
@@ -30,7 +30,9 @@ static void appendToGlobalArray(StringRef ArrayName, Module &M, Function *F,
// to the list.
SmallVector<Constant *, 16> CurrentCtors;
StructType *EltTy = StructType::get(
- IRB.getInt32Ty(), PointerType::getUnqual(FnTy), IRB.getInt8PtrTy());
+ IRB.getInt32Ty(), PointerType::get(FnTy, F->getAddressSpace()),
+ IRB.getInt8PtrTy());
+
if (GlobalVariable *GVCtor = M.getNamedGlobal(ArrayName)) {
if (Constant *Init = GVCtor->getInitializer()) {
unsigned n = Init->getNumOperands();
@@ -123,7 +125,8 @@ llvm::declareSanitizerInitFunction(Module &M, StringRef InitName,
Function *llvm::createSanitizerCtor(Module &M, StringRef CtorName) {
Function *Ctor = Function::createWithDefaultAttr(
FunctionType::get(Type::getVoidTy(M.getContext()), false),
- GlobalValue::InternalLinkage, 0, CtorName, &M);
+ GlobalValue::InternalLinkage, M.getDataLayout().getProgramAddressSpace(),
+ CtorName, &M);
Ctor->addFnAttr(Attribute::NoUnwind);
BasicBlock *CtorBB = BasicBlock::Create(M.getContext(), "", Ctor);
ReturnInst::Create(M.getContext(), CtorBB);
diff --git a/llvm/test/Instrumentation/AddressSanitizer/program-addrspace.ll b/llvm/test/Instrumentation/AddressSanitizer/program-addrspace.ll
new file mode 100644
index 0000000000000..adfe21135e7ad
--- /dev/null
+++ b/llvm/test/Instrumentation/AddressSanitizer/program-addrspace.ll
@@ -0,0 +1,24 @@
+; RUN: opt -S -passes=asan < %s | FileCheck %s
+
+; Check behavior with a non-0 default program address space. The
+; constructor should be in addrspace(1) and the global_ctors should
+; pass the verifier.
+
+; CHECK: @a = internal addrspace(42) global [1 x i32] zeroinitializer, align 4
+; CHECK: @llvm.used = appending global [1 x ptr] [ptr addrspacecast (ptr addrspace(1) @asan.module_ctor to ptr)], section "llvm.metadata"
+; CHECK: @llvm.global_ctors = appending global [1 x { i32, ptr addrspace(1), ptr }] [{ i32, ptr addrspace(1), ptr } { i32 1, ptr addrspace(1) @asan.module_ctor, ptr addrspacecast (ptr addrspace(1) @asan.module_ctor to ptr) }]
+
+; CHECK: define internal void @asan.module_ctor() addrspace(1) #0 comdat {
+
+target datalayout = "P1"
+
+ at a = internal addrspace(42) global [1 x i32] zeroinitializer, align 4
+
+define i1 @b(i64 %c) addrspace(1) {
+ %cast = inttoptr i64 %c to ptr addrspace(42)
+ %cmp = icmp ugt ptr addrspace(42) %cast, getelementptr inbounds ([1 x i32], ptr addrspace(42) @a, i64 0, i64 0)
+ ret i1 %cmp
+}
+
+!llvm.asan.globals = !{!0}
+!0 = !{ptr addrspace(42) @a, null, !"a", i1 false, i1 false}
More information about the llvm-commits
mailing list