[clang] [clang] inherit GD to let the codegen add kcfi type for ifunc (PR #96400)
MaskRay wrote: > Oh, hmm, I see. > > Maybe the right strategy here is to delay attaching the resolver to the ifunc > until the end of the translation unit, when we know the definition is already > emitted. That way, it should already have the right attributes. (We already > do some delayed checking on aliases/ifuncs anyway, in checkAliasedGlobal().) @efriedma-quic I've tried your suggestion: move `setResolver` to `checkAliasedGlobal`. However, I've run into some difficulty. When `EmitGlobal(foo)` is called, how to mark `resolver` as eagerly emitted? (Otherwise, `resolver` would not be emitted at all.) ```c int foo(int) __attribute__ ((ifunc("resolver"))); static void *resolver(void) { return 0; } // MustBeEmitted(Global) return false ``` https://github.com/llvm/llvm-project/pull/96400 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] inherit GD to let the codegen add kcfi type for ifunc (PR #96400)
efriedma-quic wrote: Oh, hmm, I see. Maybe the right strategy here is to delay attaching the resolver to the ifunc until the end of the translation unit, when we know the definition is already emitted. That way, it should already have the right attributes. (We already do some delayed checking on aliases/ifuncs anyway, in checkAliasedGlobal().) https://github.com/llvm/llvm-project/pull/96400 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] inherit GD to let the codegen add kcfi type for ifunc (PR #96400)
aokblast wrote: The problem happens because ifunc can refer resolver that havn't been defined before ifunc attribute. But LLVM Function only add attribute when it constructed first time. So there are two possible path: In the under case, no_sanitize cannot be add because it happens at ifunc("resolver") so the unit test failed ```c func_t resolver() { } int foo(int) __attribute__ ((ifunc("resolver"))); ``` In the under case, kcfi attribute failed to add because it happens at function definition: ```c int foo(int) __attribute__ ((ifunc("resolver"))); func_t resolver() { } ``` I try to fix it but other error happens, here is the [patch file](https://pastebin.com/8DtXHpF1). https://github.com/llvm/llvm-project/pull/96400 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] inherit GD to let the codegen add kcfi type for ifunc (PR #96400)
efriedma-quic wrote: Probably you want a dedicated codepath for computing the right kcfi type for resolvers. https://github.com/llvm/llvm-project/pull/96400 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] inherit GD to let the codegen add kcfi type for ifunc (PR #96400)
aokblast wrote: I think it is not the correct way to fix this issue. GD is type for ifunc not for resolver. So the CI brokes if we do things like this. https://github.com/llvm/llvm-project/pull/96400 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] inherit GD to let the codegen add kcfi type for ifunc (PR #96400)
efriedma-quic wrote: Please fix buildbot failure (it looks like clang/test/CodeGen/ifunc.c is failing). https://github.com/llvm/llvm-project/pull/96400 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] inherit GD to let the codegen add kcfi type for ifunc (PR #96400)
llvmbot wrote: @llvm/pr-subscribers-clang Author: None (aokblast) Changes In FreeBSD, we use ifunc to select best performance function in load time. However, the resolver is also a function itself but not been tagged kcfi by clang. The problems is caused by ``` if (D) CodeGenModule::SetFunctionAttribute() ``` called by CodeGenModule::GetOrCreateLLVMFunction where D is empty originally. --- Full diff: https://github.com/llvm/llvm-project/pull/96400.diff 1 Files Affected: - (modified) clang/lib/CodeGen/CodeGenModule.cpp (+1-1) ``diff diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index c8898ce196c1e..3e1f650884a7a 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -5995,7 +5995,7 @@ void CodeGenModule::emitIFuncDefinition(GlobalDecl GD) { llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); llvm::Type *ResolverTy = llvm::GlobalIFunc::getResolverFunctionType(DeclTy); llvm::Constant *Resolver = - GetOrCreateLLVMFunction(IFA->getResolver(), ResolverTy, {}, + GetOrCreateLLVMFunction(IFA->getResolver(), ResolverTy, GD, /*ForVTable=*/false); llvm::GlobalIFunc *GIF = llvm::GlobalIFunc::create(DeclTy, 0, llvm::Function::ExternalLinkage, `` https://github.com/llvm/llvm-project/pull/96400 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] inherit GD to let the codegen add kcfi type for ifunc (PR #96400)
https://github.com/aokblast created https://github.com/llvm/llvm-project/pull/96400 In FreeBSD, we use ifunc to select best performance function in load time. However, the resolver is also a function itself but not been tagged kcfi by clang. The problems is caused by ``` if (D) CodeGenModule::SetFunctionAttribute() ``` called by CodeGenModule::GetOrCreateLLVMFunction where D is empty originally. >From 0aa3505e0775c0cd7ef98698f930327e4af5ff7a Mon Sep 17 00:00:00 2001 From: aokblast Date: Sat, 22 Jun 2024 23:41:07 +0800 Subject: [PATCH] inherit GD to let the codegen add kcfi handler for ifunc --- clang/lib/CodeGen/CodeGenModule.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index c8898ce196c1e..3e1f650884a7a 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -5995,7 +5995,7 @@ void CodeGenModule::emitIFuncDefinition(GlobalDecl GD) { llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); llvm::Type *ResolverTy = llvm::GlobalIFunc::getResolverFunctionType(DeclTy); llvm::Constant *Resolver = - GetOrCreateLLVMFunction(IFA->getResolver(), ResolverTy, {}, + GetOrCreateLLVMFunction(IFA->getResolver(), ResolverTy, GD, /*ForVTable=*/false); llvm::GlobalIFunc *GIF = llvm::GlobalIFunc::create(DeclTy, 0, llvm::Function::ExternalLinkage, ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits