[PATCH] D138945: [ModuleUtils][KCFI] Set !kcfi_type metadata for sanitizer constructors
Sami Tolvanen via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Dec 2 16:33:39 PST 2022
samitolvanen updated this revision to Diff 479779.
samitolvanen marked 3 inline comments as done.
samitolvanen added a comment.
Addressed feedback.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D138945/new/
https://reviews.llvm.org/D138945
Files:
llvm/lib/Transforms/Utils/ModuleUtils.cpp
llvm/test/Instrumentation/AddressSanitizer/kcfi.ll
Index: llvm/test/Instrumentation/AddressSanitizer/kcfi.ll
===================================================================
--- /dev/null
+++ llvm/test/Instrumentation/AddressSanitizer/kcfi.ll
@@ -0,0 +1,11 @@
+;; Test that we emit kcfi_type metadata for asan.module_ctor with KCFI.
+
+; RUN: opt < %s -passes=asan -S | FileCheck %s
+
+; CHECK: @llvm.global_ctors = {{.*}}{ i32 1, ptr @asan.module_ctor, ptr @asan.module_ctor }
+
+; CHECK: define internal void @asan.module_ctor()
+; CHECK-SAME: !kcfi_type
+
+ !llvm.module.flags = !{!0}
+ !0 = !{i32 4, !"kcfi", i32 1}
Index: llvm/lib/Transforms/Utils/ModuleUtils.cpp
===================================================================
--- llvm/lib/Transforms/Utils/ModuleUtils.cpp
+++ llvm/lib/Transforms/Utils/ModuleUtils.cpp
@@ -15,8 +15,10 @@
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/xxhash.h"
using namespace llvm;
#define DEBUG_TYPE "moduleutils"
@@ -112,6 +114,19 @@
appendToUsedList(M, "llvm.compiler.used", Values);
}
+static void setKCFIType(Module &M, Function &F, StringRef MangledType) {
+ if (!M.getModuleFlag("kcfi"))
+ return;
+ // Matches CodeGenModule::CreateKCFITypeId in Clang.
+ LLVMContext &Ctx = M.getContext();
+ MDBuilder MDB(Ctx);
+ F.setMetadata(
+ LLVMContext::MD_kcfi_type,
+ MDNode::get(Ctx, MDB.createConstant(ConstantInt::get(
+ Type::getInt32Ty(Ctx),
+ static_cast<uint32_t>(xxHash64(MangledType))))));
+}
+
FunctionCallee
llvm::declareSanitizerInitFunction(Module &M, StringRef InitName,
ArrayRef<Type *> InitArgTypes) {
@@ -128,6 +143,7 @@
GlobalValue::InternalLinkage, M.getDataLayout().getProgramAddressSpace(),
CtorName, &M);
Ctor->addFnAttr(Attribute::NoUnwind);
+ setKCFIType(M, *Ctor, "_ZTSFvvE"); // void (*)(void)
BasicBlock *CtorBB = BasicBlock::Create(M.getContext(), "", Ctor);
ReturnInst::Create(M.getContext(), CtorBB);
// Ensure Ctor cannot be discarded, even if in a comdat.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138945.479779.patch
Type: text/x-patch
Size: 2204 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221203/33e68375/attachment.bin>
More information about the llvm-commits
mailing list