Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/clang/test/CodeGen/memtag-globals-asm.cpp b/clang/test/CodeGen/memtag-globals-asm.cpp
- index 186045f8f2fb..754f846d33f7 100644
- --- a/clang/test/CodeGen/memtag-globals-asm.cpp
- +++ b/clang/test/CodeGen/memtag-globals-asm.cpp
- @@ -50,8 +50,9 @@
- // CHECK-A: .memtag global_int
- // CHECK-A: .globl global_int
- +// CHECK-A: .size global_int, 4
- // CHECK-A: .p2align 4, 0x0
- -// CHECK-A: .size global_int, 16
- +// CHECK-A: .size global_int.sanitized, 16
- int global_int;
- // CHECK-B: .memtag _ZL9local_int
- // CHECK-B: .local _ZL9local_int
- @@ -63,35 +64,40 @@ static int local_int;
- // CHECK-C: .comm _ZL12local_buffer,16,16
- static char local_buffer[16];
- // CHECK-D: .memtag _ZL22local_buffer_local_end
- +// CHECK-D: .size _ZL22local_buffer_local_end, 8
- // CHECK-D: .p2align 4, 0x0
- -// CHECK-D: _ZL22local_buffer_local_end:
- +// CHECK-D: _ZL22local_buffer_local_end.sanitized:
- // CHECK-D: .xword _ZL12local_buffer+16
- -// CHECK-D: .size _ZL22local_buffer_local_end, 16
- +// CHECK-D: .size _ZL22local_buffer_local_end.sanitized, 16
- static char* local_buffer_local_end = &local_buffer[16];
- // CHECK-E: .memtag local_buffer_global_end
- // CHECK-E: .globl local_buffer_global_end
- -// CHECK-E .p2align 4, 0x0
- -// CHECK-E: local_buffer_global_end:
- +// CHECK-E: .size local_buffer_global_end, 8
- +// CHECK-E: .p2align 4, 0x0
- +// CHECK-E: local_buffer_global_end.sanitized:
- // CHECK-E: .xword _ZL12local_buffer+16
- -// CHECK-E: .size local_buffer_global_end, 16
- +// CHECK-E: .size local_buffer_global_end.sanitized, 16
- char* local_buffer_global_end = &local_buffer[16];
- // CHECK-F: .memtag global_buffer
- +// CHECK-F: .size global_buffer, 16
- // CHECK-F: .globl global_buffer
- // CHECK-F: .p2align 4, 0x0
- -// CHECK-F: .size global_buffer, 16
- +// CHECK-F: .size global_buffer.sanitized, 16
- char global_buffer[16];
- // CHECK-G: .memtag _ZL23global_buffer_local_end
- +// CHECK-G: .size _ZL23global_buffer_local_end, 8
- // CHECK-G: .p2align 4, 0x0
- -// CHECK-G: _ZL23global_buffer_local_end:
- +// CHECK-G: _ZL23global_buffer_local_end.sanitized:
- // CHECK-G: .xword global_buffer+16
- -// CHECK-G: .size _ZL23global_buffer_local_end, 16
- +// CHECK-G: .size _ZL23global_buffer_local_end.sanitized, 16
- static char* global_buffer_local_end = &global_buffer[16];
- // CHECK-H: .memtag global_buffer_global_end
- +// CHECK-H: .size global_buffer_global_end, 8
- // CHECK-H: .p2align 4, 0x0
- -// CHECK-H: global_buffer_global_end:
- +// CHECK-H: global_buffer_global_end.sanitized:
- // CHECK-H: .xword global_buffer+16
- -// CHECK-H: .size global_buffer_global_end, 16
- +// CHECK-H: .size global_buffer_global_end.sanitized, 16
- char* global_buffer_global_end = &global_buffer[16];
- class MyClass {
- @@ -103,37 +109,43 @@ class MyClass {
- };
- // CHECK-I: .memtag _ZN7MyClass12my_class_intE
- // CHECK-I: .globl _ZN7MyClass12my_class_intE
- +// CHECK-I: .size _ZN7MyClass12my_class_intE, 4
- // CHECK-I: .p2align 4, 0x0
- -// CHECK-I: .size _ZN7MyClass12my_class_intE, 16
- +// CHECK-I: .size _ZN7MyClass12my_class_intE.sanitized, 16
- int MyClass::my_class_int;
- // CHECK-NOT: .memtag _ZN7MyClass18my_class_const_intE
- const int MyClass::my_class_const_int = 1;
- // CHECK-J: .memtag global_my_class
- +// CHECK-J: .size global_my_class, 8
- // CHECK-J: .globl global_my_class
- // CHECK-J: .p2align 4, 0x0
- -// CHECK-J: .size global_my_class, 16
- +// CHECK-J: .size global_my_class.sanitized, 16
- MyClass global_my_class;
- // CHECK-K: .memtag _ZL14local_my_class
- +// CHECK-K: .size _ZL14local_my_class, 8
- // CHECK-K: .p2align 4, 0x0
- -// CHECK-K: .size _ZL14local_my_class, 16
- +// CHECK-K: .size _ZL14local_my_class.sanitized, 16
- static MyClass local_my_class;
- // CHECK-NOT: .memtag _ZL18local_const_string
- static const char local_const_string[] = "this is a local string";
- // CHECK-L: .memtag _ZL12local_string
- +// CHECK-L: .size _ZL12local_string, 23
- // CHECK-L: .p2align 4, 0x0
- -// CHECK-L: .size _ZL12local_string, 32
- +// CHECK-L: .size _ZL12local_string.sanitized, 32
- static char local_string[] = "this is a local string";
- // CHECK-M: .memtag global_atomic_int
- // CHECK-M: .globl global_atomic_int
- +// CHECK-M: .size global_atomic_int, 4
- // CHECK-M: .p2align 4, 0x0
- -// CHECK-M: .size global_atomic_int, 16
- +// CHECK-M: .size global_atomic_int.sanitized, 16
- _Atomic(int) global_atomic_int;
- // CHECK-N: .memtag _ZL16local_atomic_int
- +// CHECK-N: .size _ZL16local_atomic_int, 4
- // CHECK-N: .local _ZL16local_atomic_int
- -// CHECK-N: .comm _ZL16local_atomic_int,16,16
- +// CHECK-N: .comm _ZL16local_atomic_int.sanitized,16,16
- static _Atomic(int) local_atomic_int;
- union MyUnion {
- diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst
- index fd2e3aacd016..342fa84d662e 100644
- --- a/llvm/docs/LangRef.rst
- +++ b/llvm/docs/LangRef.rst
- @@ -319,7 +319,7 @@ added in the future:
- not be used lightly but only for specific situations such as an
- alternative to the *register pinning* performance technique often
- used when implementing functional programming languages. At the
- - moment only X86, AArch64, and RISCV support this convention. The
- + moment only X86, AArch64, and RISCV support this convention. The
- following limitations exist:
- - On *X86-32* only up to 4 bit type parameters are supported. No
- @@ -650,10 +650,10 @@ implementation defined, the optimizer can't do the latter. The former is
- challenging as many commonly expected properties, such as
- ``ptrtoint(v)-ptrtoint(v) == 0``, don't hold for non-integral types.
- Similar restrictions apply to intrinsics that might examine the pointer bits,
- -such as :ref:`llvm.ptrmask<int_ptrmask>`.
- +such as :ref:`llvm.ptrmask<int_ptrmask>`.
- The alignment information provided by the frontend for a non-integral pointer
- -(typically using attributes or metadata) must be valid for every possible
- +(typically using attributes or metadata) must be valid for every possible
- representation of the pointer.
- .. _globalvars:
- @@ -789,6 +789,7 @@ Syntax::
- [, code_model "model"]
- [, no_sanitize_address] [, no_sanitize_hwaddress]
- [, sanitize_address_dyninit] [, sanitize_memtag]
- + [, sanitize_needs_alias ]
- (, !name !N)*
- For example, the following defines a global in a numbered address space
- @@ -2538,6 +2539,11 @@ are grouped into a single :ref:`attribute group <attrgrp>`.
- AddressSanitizer, should be checked for ODR violations. This attribute is
- applied to global variables that are dynamically initialized according to
- C++ rules.
- +``sanitize_needs_alias``
- + This attribute indicates that the global variable is instrumented with a
- + sanitizer that has changed the size of the global variable, and two symtab
- + entries should be created for the GV. One with the original size and
- + original name, and one with a ".sanitized" suffix and the new, padded size.
- .. _opbundles:
- diff --git a/llvm/include/llvm/AsmParser/LLToken.h b/llvm/include/llvm/AsmParser/LLToken.h
- index 3c34706ee03e..a0097e4c6d54 100644
- --- a/llvm/include/llvm/AsmParser/LLToken.h
- +++ b/llvm/include/llvm/AsmParser/LLToken.h
- @@ -452,6 +452,9 @@ enum Kind {
- // GV's where the clang++ frontend (when ASan is used) notes that this is
- // dynamically initialized, and thus needs ODR detection.
- kw_sanitize_address_dyninit,
- + // GV's that need an alias in the symbol table. See GlobalValue.h for more
- + // info.
- + kw_sanitize_needs_alias,
- // Unsigned Valued tokens (UIntVal).
- LabelID, // 42:
- diff --git a/llvm/include/llvm/IR/GlobalValue.h b/llvm/include/llvm/IR/GlobalValue.h
- index aa8188cd99fe..5dd7d2409337 100644
- --- a/llvm/include/llvm/IR/GlobalValue.h
- +++ b/llvm/include/llvm/IR/GlobalValue.h
- @@ -316,8 +316,8 @@ public:
- // specifically to global variables.
- struct SanitizerMetadata {
- SanitizerMetadata()
- - : NoAddress(false), NoHWAddress(false),
- - Memtag(false), IsDynInit(false) {}
- + : NoAddress(false), NoHWAddress(false), Memtag(false), IsDynInit(false),
- + NeedsAlias(false) {}
- // For ASan and HWASan, this instrumentation is implicitly applied to all
- // global variables when built with -fsanitize=*. What we need is a way to
- // persist the information that a certain global variable should *not* have
- @@ -350,6 +350,19 @@ public:
- // (from a C++ language perspective), and should therefore be checked for
- // ODR violations.
- unsigned IsDynInit : 1;
- +
- + // Some runtimes, like the AMD language runtime, query the size of the
- + // symbol in the symtab in order to provide certain services, like copying
- + // data to/from the GPU. Certain transformations to global variables that
- + // are often present in sanitizers, such as increasing the size to provide a
- + // redzone (ASan) or taking up a whole 16-byte granue (MemtagGlobals), are
- + // broken with these runtimes. This attribute ensures that the AsmPrinter
- + // produces two aliases to the sanitized global variable:
- + // 1. A symtab entry with the original global variable name, which has the
- + // size which is defined by the language.
- + // 2. A symtab with the original name, plus ".sanitized", which has the
- + // actual symbol size that's present in the file.
- + unsigned NeedsAlias : 1;
- };
- bool hasSanitizerMetadata() const { return HasSanitizerMetadata; }
- @@ -361,6 +374,18 @@ public:
- void setSanitizerMetadata(SanitizerMetadata Meta);
- void removeSanitizerMetadata();
- + bool needsSanitizerAlias() const {
- + return hasSanitizerMetadata() && getSanitizerMetadata().NeedsAlias;
- + }
- +
- + void setNeedsSanitizerAlias(bool Needs) {
- + SanitizerMetadata Meta;
- + if (hasSanitizerMetadata())
- + Meta = getSanitizerMetadata();
- + Meta.NeedsAlias = Needs;
- + setSanitizerMetadata(Meta);
- + }
- +
- bool isTagged() const {
- return hasSanitizerMetadata() && getSanitizerMetadata().Memtag;
- }
- diff --git a/llvm/lib/AsmParser/LLLexer.cpp b/llvm/lib/AsmParser/LLLexer.cpp
- index 5d8a50eee130..66f0c04ae870 100644
- --- a/llvm/lib/AsmParser/LLLexer.cpp
- +++ b/llvm/lib/AsmParser/LLLexer.cpp
- @@ -585,6 +585,7 @@ lltok::Kind LLLexer::LexIdentifier() {
- KEYWORD(no_sanitize_address);
- KEYWORD(no_sanitize_hwaddress);
- KEYWORD(sanitize_address_dyninit);
- + KEYWORD(sanitize_needs_alias);
- KEYWORD(ccc);
- KEYWORD(fastcc);
- diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
- index a91e2f690999..2f6b91bb9a48 100644
- --- a/llvm/lib/AsmParser/LLParser.cpp
- +++ b/llvm/lib/AsmParser/LLParser.cpp
- @@ -1252,6 +1252,7 @@ static bool isSanitizer(lltok::Kind Kind) {
- case lltok::kw_no_sanitize_hwaddress:
- case lltok::kw_sanitize_memtag:
- case lltok::kw_sanitize_address_dyninit:
- + case lltok::kw_sanitize_needs_alias:
- return true;
- default:
- return false;
- @@ -1277,6 +1278,9 @@ bool LLParser::parseSanitizer(GlobalVariable *GV) {
- case lltok::kw_sanitize_address_dyninit:
- Meta.IsDynInit = true;
- break;
- + case lltok::kw_sanitize_needs_alias:
- + Meta.NeedsAlias = true;
- + break;
- default:
- return tokError("non-sanitizer token passed to LLParser::parseSanitizer()");
- }
- diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
- index 515a1d0caa04..361080f175f5 100644
- --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
- +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
- @@ -3834,6 +3834,8 @@ GlobalValue::SanitizerMetadata deserializeSanitizerMetadata(unsigned V) {
- Meta.Memtag = true;
- if (V & (1 << 3))
- Meta.IsDynInit = true;
- + if (V & (1 << 4))
- + Meta.NeedsAlias = true;
- return Meta;
- }
- diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
- index 13be0b0c3307..9e6a5efa75e1 100644
- --- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
- +++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
- @@ -1312,7 +1312,8 @@ static_assert(sizeof(GlobalValue::SanitizerMetadata) <= sizeof(unsigned),
- static unsigned
- serializeSanitizerMetadata(const GlobalValue::SanitizerMetadata &Meta) {
- return Meta.NoAddress | (Meta.NoHWAddress << 1) |
- - (Meta.Memtag << 2) | (Meta.IsDynInit << 3);
- + (Meta.Memtag << 2) | (Meta.IsDynInit << 3) |
- + (Meta.NeedsAlias << 4);
- }
- /// Emit top-level description of module, including target triple, inline asm,
- diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
- index b961fc2c338a..4a0b50b85e53 100644
- --- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
- +++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
- @@ -785,6 +785,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 "SanitizedPaddedGlobal" attribute and extract
- + // the actual global variable size.
- + uint64_t ActualSize = 0;
- + if (GV->needsSanitizerAlias()) {
- + 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,
- @@ -897,12 +910,24 @@ void AsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
- OutStreamer->switchSection(TheSection);
- + if (GV->needsSanitizerAlias()) {
- + emitLinkage(GV, EmittedInitSym);
- + OutStreamer->emitLabel(EmittedInitSym);
- + if (MAI->hasDotTypeDotSizeDirective())
- + OutStreamer->emitELFSize(EmittedInitSym,
- + MCConstantExpr::create(ActualSize, OutContext));
- + EmittedInitSym = OutContext.getOrCreateSymbol(
- + GVSym->getName() + Twine(".sanitized"));
- + emitVisibility(EmittedInitSym, GV->getVisibility(), !GV->isDeclaration());
- + }
- +
- emitLinkage(GV, EmittedInitSym);
- emitAlignment(Alignment, GV);
- OutStreamer->emitLabel(EmittedInitSym);
- MCSymbol *LocalAlias = getSymbolPreferLocal(*GV);
- - if (LocalAlias != EmittedInitSym)
- + if ((LocalAlias != EmittedInitSym) &&
- + !GV->needsSanitizerAlias())
- OutStreamer->emitLabel(LocalAlias);
- emitGlobalConstant(GV->getParent()->getDataLayout(), GV->getInitializer());
- diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp
- index c6ef332403fd..f6848a9fad12 100644
- --- a/llvm/lib/IR/AsmWriter.cpp
- +++ b/llvm/lib/IR/AsmWriter.cpp
- @@ -3724,6 +3724,8 @@ void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
- Out << ", sanitize_memtag";
- if (MD.IsDynInit)
- Out << ", sanitize_address_dyninit";
- + if (MD.NeedsAlias)
- + Out << ", sanitize_needs_alias";
- }
- maybePrintComdat(Out, *GV);
- diff --git a/llvm/lib/Target/AArch64/AArch64GlobalsTagging.cpp b/llvm/lib/Target/AArch64/AArch64GlobalsTagging.cpp
- index 27959489e7df..4607112ba107 100644
- --- a/llvm/lib/Target/AArch64/AArch64GlobalsTagging.cpp
- +++ b/llvm/lib/Target/AArch64/AArch64GlobalsTagging.cpp
- @@ -118,6 +118,8 @@ static void tagGlobalDefinition(Module &M, GlobalVariable *G) {
- // Ensure that tagged globals don't get merged by ICF - as they should have
- // different tags at runtime.
- G->setUnnamedAddr(GlobalValue::UnnamedAddr::None);
- +
- + G->setNeedsSanitizerAlias(true);
- }
- namespace {
- diff --git a/llvm/test/Assembler/globalvariable-attributes.ll b/llvm/test/Assembler/globalvariable-attributes.ll
- index 4882b447973c..5d323cdf571c 100644
- --- a/llvm/test/Assembler/globalvariable-attributes.ll
- +++ b/llvm/test/Assembler/globalvariable-attributes.ll
- @@ -14,6 +14,7 @@
- @g12 = global i32 2, code_model "kernel"
- @g13 = global i32 2, code_model "medium"
- @g14 = global i32 2, code_model "large"
- +@g15 = global i32 2, sanitize_needs_alias, align 4
- attributes #0 = { "string" = "value" nobuiltin norecurse }
- @@ -31,6 +32,7 @@ attributes #0 = { "string" = "value" nobuiltin norecurse }
- ; CHECK: @g12 = global i32 2, code_model "kernel"
- ; CHECK: @g13 = global i32 2, code_model "medium"
- ; CHECK: @g14 = global i32 2, code_model "large"
- +; CHECK: @g15 = global i32 2, sanitize_needs_alias, align 4
- ; CHECK: attributes #0 = { "key"="value" "key2"="value2" }
- ; CHECK: attributes #1 = { "key3"="value3" }
- diff --git a/llvm/test/Bitcode/compatibility.ll b/llvm/test/Bitcode/compatibility.ll
- index ce6a6571ec14..4ce269666d98 100644
- --- a/llvm/test/Bitcode/compatibility.ll
- +++ b/llvm/test/Bitcode/compatibility.ll
- @@ -207,12 +207,14 @@ declare void @g.f1()
- @g.no_sanitize_address = global i32 0, no_sanitize_address
- @g.no_sanitize_hwaddress = global i32 0, no_sanitize_hwaddress
- @g.sanitize_memtag = global i32 0, sanitize_memtag
- +@g.sanitize_needs_alias = global i32 0, sanitize_needs_alias
- @g.no_sanitize_multiple = global i32 0, no_sanitize_address, no_sanitize_hwaddress
- @g.sanitize_address_dyninit = global i32 0, sanitize_address_dyninit
- @g.sanitize_multiple = global i32 0, sanitize_memtag, sanitize_address_dyninit
- ; CHECK: @g.no_sanitize_address = global i32 0, no_sanitize_address
- ; CHECK: @g.no_sanitize_hwaddress = global i32 0, no_sanitize_hwaddress
- ; CHECK: @g.sanitize_memtag = global i32 0, sanitize_memtag
- +; CHECK: @g.sanitize_needs_alias = global i32 0, sanitize_needs_alias
- ; CHECK: @g.no_sanitize_multiple = global i32 0, no_sanitize_address, no_sanitize_hwaddress
- ; CHECK: @g.sanitize_address_dyninit = global i32 0, sanitize_address_dyninit
- ; CHECK: @g.sanitize_multiple = global i32 0, sanitize_memtag, sanitize_address_dyninit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement