[clang] [WIP] For Asan instrumented global, emit two symbols, one with actual size and other with instrumented size. (PR #70166)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 24 22:26:12 PDT 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Chaitanya (skc7)
<details>
<summary>Changes</summary>
This PR has dependency on #<!-- -->68865
---
Full diff: https://github.com/llvm/llvm-project/pull/70166.diff
3 Files Affected:
- (added) clang/test/CodeGen/asan_globals_symbols.cpp (+15)
- (modified) llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp (+27-1)
- (modified) llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp (+3)
``````````diff
diff --git a/clang/test/CodeGen/asan_globals_symbols.cpp b/clang/test/CodeGen/asan_globals_symbols.cpp
new file mode 100644
index 000000000000000..09e35506bd8e186
--- /dev/null
+++ b/clang/test/CodeGen/asan_globals_symbols.cpp
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -S -x c++ -std=c++11 -triple x86_64-linux \
+// RUN: -fsanitize=address -o %t.out %s
+// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-A
+
+// CHECK-A: myGlobal:
+// CHECK-A: .size myGlobal, 4
+// CHECK-A: myGlobal__asan_instrumented:
+// CHECK-A .size myGlobal__asan_instrumented, 32
+
+int myGlobal;
+
+int main() {
+ myGlobal = 0;
+ return 0;
+}
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 072c55f79caa9dc..d71ee82ce6ca628 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -758,6 +758,19 @@ void AsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
// sections and expected to be contiguous (e.g. ObjC metadata).
const Align Alignment = getGVAlignment(GV, DL);
+ // Identify globals with "asan_instrumented" attribute and extract
+ // the actual global variable size.
+ uint64_t ActualSize = 0;
+ if (GV->hasAttribute(Attribute::SanitizeAddress)) {
+ StructType *ST = dyn_cast<StructType>(GV->getValueType());
+ if (ST && ST->getNumElements() == 2) {
+ auto *ET0 = ST->getElementType(0);
+ if (ET0 && isa<ArrayType>(ST->getElementType(1))) {
+ ActualSize = DL.getTypeAllocSize(ET0);
+ }
+ }
+ }
+
for (const HandlerInfo &HI : Handlers) {
NamedRegionTimer T(HI.TimerName, HI.TimerDescription,
HI.TimerGroupName, HI.TimerGroupDescription,
@@ -868,6 +881,18 @@ void AsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
MCSymbol *EmittedInitSym = GVSym;
+ if (GV->hasAttribute(Attribute::SanitizeAddress)) {
+ OutStreamer->switchSection(TheSection);
+ emitLinkage(GV, EmittedInitSym);
+ OutStreamer->emitLabel(EmittedInitSym);
+ if (MAI->hasDotTypeDotSizeDirective())
+ OutStreamer->emitELFSize(EmittedInitSym,
+ MCConstantExpr::create(ActualSize, OutContext));
+ EmittedInitSym = OutContext.getOrCreateSymbol(GVSym->getName() +
+ Twine("__asan_instrumented"));
+ emitVisibility(EmittedInitSym, GV->getVisibility(), !GV->isDeclaration());
+ }
+
OutStreamer->switchSection(TheSection);
emitLinkage(GV, EmittedInitSym);
@@ -875,7 +900,8 @@ void AsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
OutStreamer->emitLabel(EmittedInitSym);
MCSymbol *LocalAlias = getSymbolPreferLocal(*GV);
- if (LocalAlias != EmittedInitSym)
+ if ((LocalAlias != EmittedInitSym) &&
+ !GV->hasAttribute(Attribute::SanitizeAddress))
OutStreamer->emitLabel(LocalAlias);
emitGlobalConstant(GV->getParent()->getDataLayout(), GV->getInitializer());
diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
index e80ee1953de6b21..c5ef705d8ca9e30 100644
--- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
@@ -2441,6 +2441,9 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB, Module &M,
// zero so we can copy the metadata over as is.
NewGlobal->copyMetadata(G, 0);
+ // Attach "asan_instrumented" attribute to the new global.
+ NewGlobal->addAttribute(Attribute::SanitizeAddress);
+
Value *Indices2[2];
Indices2[0] = IRB.getInt32(0);
Indices2[1] = IRB.getInt32(0);
``````````
</details>
https://github.com/llvm/llvm-project/pull/70166
More information about the cfe-commits
mailing list