Advertisement
Guest User

Untitled

a guest
Feb 9th, 2024
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.62 KB | None | 0 0
  1. diff --git a/clang/test/CodeGen/memtag-globals-asm.cpp b/clang/test/CodeGen/memtag-globals-asm.cpp
  2. index 186045f8f2fb..754f846d33f7 100644
  3. --- a/clang/test/CodeGen/memtag-globals-asm.cpp
  4. +++ b/clang/test/CodeGen/memtag-globals-asm.cpp
  5. @@ -50,8 +50,9 @@
  6.  
  7. // CHECK-A: .memtag global_int
  8. // CHECK-A: .globl global_int
  9. +// CHECK-A: .size global_int, 4
  10. // CHECK-A: .p2align 4, 0x0
  11. -// CHECK-A: .size global_int, 16
  12. +// CHECK-A: .size global_int.sanitized, 16
  13. int global_int;
  14. // CHECK-B: .memtag _ZL9local_int
  15. // CHECK-B: .local _ZL9local_int
  16. @@ -63,35 +64,40 @@ static int local_int;
  17. // CHECK-C: .comm _ZL12local_buffer,16,16
  18. static char local_buffer[16];
  19. // CHECK-D: .memtag _ZL22local_buffer_local_end
  20. +// CHECK-D: .size _ZL22local_buffer_local_end, 8
  21. // CHECK-D: .p2align 4, 0x0
  22. -// CHECK-D: _ZL22local_buffer_local_end:
  23. +// CHECK-D: _ZL22local_buffer_local_end.sanitized:
  24. // CHECK-D: .xword _ZL12local_buffer+16
  25. -// CHECK-D: .size _ZL22local_buffer_local_end, 16
  26. +// CHECK-D: .size _ZL22local_buffer_local_end.sanitized, 16
  27. static char* local_buffer_local_end = &local_buffer[16];
  28. // CHECK-E: .memtag local_buffer_global_end
  29. // CHECK-E: .globl local_buffer_global_end
  30. -// CHECK-E .p2align 4, 0x0
  31. -// CHECK-E: local_buffer_global_end:
  32. +// CHECK-E: .size local_buffer_global_end, 8
  33. +// CHECK-E: .p2align 4, 0x0
  34. +// CHECK-E: local_buffer_global_end.sanitized:
  35. // CHECK-E: .xword _ZL12local_buffer+16
  36. -// CHECK-E: .size local_buffer_global_end, 16
  37. +// CHECK-E: .size local_buffer_global_end.sanitized, 16
  38. char* local_buffer_global_end = &local_buffer[16];
  39.  
  40. // CHECK-F: .memtag global_buffer
  41. +// CHECK-F: .size global_buffer, 16
  42. // CHECK-F: .globl global_buffer
  43. // CHECK-F: .p2align 4, 0x0
  44. -// CHECK-F: .size global_buffer, 16
  45. +// CHECK-F: .size global_buffer.sanitized, 16
  46. char global_buffer[16];
  47. // CHECK-G: .memtag _ZL23global_buffer_local_end
  48. +// CHECK-G: .size _ZL23global_buffer_local_end, 8
  49. // CHECK-G: .p2align 4, 0x0
  50. -// CHECK-G: _ZL23global_buffer_local_end:
  51. +// CHECK-G: _ZL23global_buffer_local_end.sanitized:
  52. // CHECK-G: .xword global_buffer+16
  53. -// CHECK-G: .size _ZL23global_buffer_local_end, 16
  54. +// CHECK-G: .size _ZL23global_buffer_local_end.sanitized, 16
  55. static char* global_buffer_local_end = &global_buffer[16];
  56. // CHECK-H: .memtag global_buffer_global_end
  57. +// CHECK-H: .size global_buffer_global_end, 8
  58. // CHECK-H: .p2align 4, 0x0
  59. -// CHECK-H: global_buffer_global_end:
  60. +// CHECK-H: global_buffer_global_end.sanitized:
  61. // CHECK-H: .xword global_buffer+16
  62. -// CHECK-H: .size global_buffer_global_end, 16
  63. +// CHECK-H: .size global_buffer_global_end.sanitized, 16
  64. char* global_buffer_global_end = &global_buffer[16];
  65.  
  66. class MyClass {
  67. @@ -103,37 +109,43 @@ class MyClass {
  68. };
  69. // CHECK-I: .memtag _ZN7MyClass12my_class_intE
  70. // CHECK-I: .globl _ZN7MyClass12my_class_intE
  71. +// CHECK-I: .size _ZN7MyClass12my_class_intE, 4
  72. // CHECK-I: .p2align 4, 0x0
  73. -// CHECK-I: .size _ZN7MyClass12my_class_intE, 16
  74. +// CHECK-I: .size _ZN7MyClass12my_class_intE.sanitized, 16
  75. int MyClass::my_class_int;
  76. // CHECK-NOT: .memtag _ZN7MyClass18my_class_const_intE
  77. const int MyClass::my_class_const_int = 1;
  78.  
  79. // CHECK-J: .memtag global_my_class
  80. +// CHECK-J: .size global_my_class, 8
  81. // CHECK-J: .globl global_my_class
  82. // CHECK-J: .p2align 4, 0x0
  83. -// CHECK-J: .size global_my_class, 16
  84. +// CHECK-J: .size global_my_class.sanitized, 16
  85. MyClass global_my_class;
  86. // CHECK-K: .memtag _ZL14local_my_class
  87. +// CHECK-K: .size _ZL14local_my_class, 8
  88. // CHECK-K: .p2align 4, 0x0
  89. -// CHECK-K: .size _ZL14local_my_class, 16
  90. +// CHECK-K: .size _ZL14local_my_class.sanitized, 16
  91. static MyClass local_my_class;
  92.  
  93. // CHECK-NOT: .memtag _ZL18local_const_string
  94. static const char local_const_string[] = "this is a local string";
  95. // CHECK-L: .memtag _ZL12local_string
  96. +// CHECK-L: .size _ZL12local_string, 23
  97. // CHECK-L: .p2align 4, 0x0
  98. -// CHECK-L: .size _ZL12local_string, 32
  99. +// CHECK-L: .size _ZL12local_string.sanitized, 32
  100. static char local_string[] = "this is a local string";
  101.  
  102. // CHECK-M: .memtag global_atomic_int
  103. // CHECK-M: .globl global_atomic_int
  104. +// CHECK-M: .size global_atomic_int, 4
  105. // CHECK-M: .p2align 4, 0x0
  106. -// CHECK-M: .size global_atomic_int, 16
  107. +// CHECK-M: .size global_atomic_int.sanitized, 16
  108. _Atomic(int) global_atomic_int;
  109. // CHECK-N: .memtag _ZL16local_atomic_int
  110. +// CHECK-N: .size _ZL16local_atomic_int, 4
  111. // CHECK-N: .local _ZL16local_atomic_int
  112. -// CHECK-N: .comm _ZL16local_atomic_int,16,16
  113. +// CHECK-N: .comm _ZL16local_atomic_int.sanitized,16,16
  114. static _Atomic(int) local_atomic_int;
  115.  
  116. union MyUnion {
  117. diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst
  118. index fd2e3aacd016..342fa84d662e 100644
  119. --- a/llvm/docs/LangRef.rst
  120. +++ b/llvm/docs/LangRef.rst
  121. @@ -319,7 +319,7 @@ added in the future:
  122. not be used lightly but only for specific situations such as an
  123. alternative to the *register pinning* performance technique often
  124. used when implementing functional programming languages. At the
  125. - moment only X86, AArch64, and RISCV support this convention. The
  126. + moment only X86, AArch64, and RISCV support this convention. The
  127. following limitations exist:
  128.  
  129. - On *X86-32* only up to 4 bit type parameters are supported. No
  130. @@ -650,10 +650,10 @@ implementation defined, the optimizer can't do the latter. The former is
  131. challenging as many commonly expected properties, such as
  132. ``ptrtoint(v)-ptrtoint(v) == 0``, don't hold for non-integral types.
  133. Similar restrictions apply to intrinsics that might examine the pointer bits,
  134. -such as :ref:`llvm.ptrmask<int_ptrmask>`.
  135. +such as :ref:`llvm.ptrmask<int_ptrmask>`.
  136.  
  137. The alignment information provided by the frontend for a non-integral pointer
  138. -(typically using attributes or metadata) must be valid for every possible
  139. +(typically using attributes or metadata) must be valid for every possible
  140. representation of the pointer.
  141.  
  142. .. _globalvars:
  143. @@ -789,6 +789,7 @@ Syntax::
  144. [, code_model "model"]
  145. [, no_sanitize_address] [, no_sanitize_hwaddress]
  146. [, sanitize_address_dyninit] [, sanitize_memtag]
  147. + [, sanitize_needs_alias ]
  148. (, !name !N)*
  149.  
  150. For example, the following defines a global in a numbered address space
  151. @@ -2538,6 +2539,11 @@ are grouped into a single :ref:`attribute group <attrgrp>`.
  152. AddressSanitizer, should be checked for ODR violations. This attribute is
  153. applied to global variables that are dynamically initialized according to
  154. C++ rules.
  155. +``sanitize_needs_alias``
  156. + This attribute indicates that the global variable is instrumented with a
  157. + sanitizer that has changed the size of the global variable, and two symtab
  158. + entries should be created for the GV. One with the original size and
  159. + original name, and one with a ".sanitized" suffix and the new, padded size.
  160.  
  161. .. _opbundles:
  162.  
  163. diff --git a/llvm/include/llvm/AsmParser/LLToken.h b/llvm/include/llvm/AsmParser/LLToken.h
  164. index 3c34706ee03e..a0097e4c6d54 100644
  165. --- a/llvm/include/llvm/AsmParser/LLToken.h
  166. +++ b/llvm/include/llvm/AsmParser/LLToken.h
  167. @@ -452,6 +452,9 @@ enum Kind {
  168. // GV's where the clang++ frontend (when ASan is used) notes that this is
  169. // dynamically initialized, and thus needs ODR detection.
  170. kw_sanitize_address_dyninit,
  171. + // GV's that need an alias in the symbol table. See GlobalValue.h for more
  172. + // info.
  173. + kw_sanitize_needs_alias,
  174.  
  175. // Unsigned Valued tokens (UIntVal).
  176. LabelID, // 42:
  177. diff --git a/llvm/include/llvm/IR/GlobalValue.h b/llvm/include/llvm/IR/GlobalValue.h
  178. index aa8188cd99fe..5dd7d2409337 100644
  179. --- a/llvm/include/llvm/IR/GlobalValue.h
  180. +++ b/llvm/include/llvm/IR/GlobalValue.h
  181. @@ -316,8 +316,8 @@ public:
  182. // specifically to global variables.
  183. struct SanitizerMetadata {
  184. SanitizerMetadata()
  185. - : NoAddress(false), NoHWAddress(false),
  186. - Memtag(false), IsDynInit(false) {}
  187. + : NoAddress(false), NoHWAddress(false), Memtag(false), IsDynInit(false),
  188. + NeedsAlias(false) {}
  189. // For ASan and HWASan, this instrumentation is implicitly applied to all
  190. // global variables when built with -fsanitize=*. What we need is a way to
  191. // persist the information that a certain global variable should *not* have
  192. @@ -350,6 +350,19 @@ public:
  193. // (from a C++ language perspective), and should therefore be checked for
  194. // ODR violations.
  195. unsigned IsDynInit : 1;
  196. +
  197. + // Some runtimes, like the AMD language runtime, query the size of the
  198. + // symbol in the symtab in order to provide certain services, like copying
  199. + // data to/from the GPU. Certain transformations to global variables that
  200. + // are often present in sanitizers, such as increasing the size to provide a
  201. + // redzone (ASan) or taking up a whole 16-byte granue (MemtagGlobals), are
  202. + // broken with these runtimes. This attribute ensures that the AsmPrinter
  203. + // produces two aliases to the sanitized global variable:
  204. + // 1. A symtab entry with the original global variable name, which has the
  205. + // size which is defined by the language.
  206. + // 2. A symtab with the original name, plus ".sanitized", which has the
  207. + // actual symbol size that's present in the file.
  208. + unsigned NeedsAlias : 1;
  209. };
  210.  
  211. bool hasSanitizerMetadata() const { return HasSanitizerMetadata; }
  212. @@ -361,6 +374,18 @@ public:
  213. void setSanitizerMetadata(SanitizerMetadata Meta);
  214. void removeSanitizerMetadata();
  215.  
  216. + bool needsSanitizerAlias() const {
  217. + return hasSanitizerMetadata() && getSanitizerMetadata().NeedsAlias;
  218. + }
  219. +
  220. + void setNeedsSanitizerAlias(bool Needs) {
  221. + SanitizerMetadata Meta;
  222. + if (hasSanitizerMetadata())
  223. + Meta = getSanitizerMetadata();
  224. + Meta.NeedsAlias = Needs;
  225. + setSanitizerMetadata(Meta);
  226. + }
  227. +
  228. bool isTagged() const {
  229. return hasSanitizerMetadata() && getSanitizerMetadata().Memtag;
  230. }
  231. diff --git a/llvm/lib/AsmParser/LLLexer.cpp b/llvm/lib/AsmParser/LLLexer.cpp
  232. index 5d8a50eee130..66f0c04ae870 100644
  233. --- a/llvm/lib/AsmParser/LLLexer.cpp
  234. +++ b/llvm/lib/AsmParser/LLLexer.cpp
  235. @@ -585,6 +585,7 @@ lltok::Kind LLLexer::LexIdentifier() {
  236. KEYWORD(no_sanitize_address);
  237. KEYWORD(no_sanitize_hwaddress);
  238. KEYWORD(sanitize_address_dyninit);
  239. + KEYWORD(sanitize_needs_alias);
  240.  
  241. KEYWORD(ccc);
  242. KEYWORD(fastcc);
  243. diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
  244. index a91e2f690999..2f6b91bb9a48 100644
  245. --- a/llvm/lib/AsmParser/LLParser.cpp
  246. +++ b/llvm/lib/AsmParser/LLParser.cpp
  247. @@ -1252,6 +1252,7 @@ static bool isSanitizer(lltok::Kind Kind) {
  248. case lltok::kw_no_sanitize_hwaddress:
  249. case lltok::kw_sanitize_memtag:
  250. case lltok::kw_sanitize_address_dyninit:
  251. + case lltok::kw_sanitize_needs_alias:
  252. return true;
  253. default:
  254. return false;
  255. @@ -1277,6 +1278,9 @@ bool LLParser::parseSanitizer(GlobalVariable *GV) {
  256. case lltok::kw_sanitize_address_dyninit:
  257. Meta.IsDynInit = true;
  258. break;
  259. + case lltok::kw_sanitize_needs_alias:
  260. + Meta.NeedsAlias = true;
  261. + break;
  262. default:
  263. return tokError("non-sanitizer token passed to LLParser::parseSanitizer()");
  264. }
  265. diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
  266. index 515a1d0caa04..361080f175f5 100644
  267. --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
  268. +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
  269. @@ -3834,6 +3834,8 @@ GlobalValue::SanitizerMetadata deserializeSanitizerMetadata(unsigned V) {
  270. Meta.Memtag = true;
  271. if (V & (1 << 3))
  272. Meta.IsDynInit = true;
  273. + if (V & (1 << 4))
  274. + Meta.NeedsAlias = true;
  275. return Meta;
  276. }
  277.  
  278. diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
  279. index 13be0b0c3307..9e6a5efa75e1 100644
  280. --- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
  281. +++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
  282. @@ -1312,7 +1312,8 @@ static_assert(sizeof(GlobalValue::SanitizerMetadata) <= sizeof(unsigned),
  283. static unsigned
  284. serializeSanitizerMetadata(const GlobalValue::SanitizerMetadata &Meta) {
  285. return Meta.NoAddress | (Meta.NoHWAddress << 1) |
  286. - (Meta.Memtag << 2) | (Meta.IsDynInit << 3);
  287. + (Meta.Memtag << 2) | (Meta.IsDynInit << 3) |
  288. + (Meta.NeedsAlias << 4);
  289. }
  290.  
  291. /// Emit top-level description of module, including target triple, inline asm,
  292. diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
  293. index b961fc2c338a..4a0b50b85e53 100644
  294. --- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
  295. +++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
  296. @@ -785,6 +785,19 @@ void AsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
  297. // sections and expected to be contiguous (e.g. ObjC metadata).
  298. const Align Alignment = getGVAlignment(GV, DL);
  299.  
  300. + // Identify globals with "SanitizedPaddedGlobal" attribute and extract
  301. + // the actual global variable size.
  302. + uint64_t ActualSize = 0;
  303. + if (GV->needsSanitizerAlias()) {
  304. + StructType *ST = dyn_cast<StructType>(GV->getValueType());
  305. + if (ST && ST->getNumElements() == 2) {
  306. + auto *ET0 = ST->getElementType(0);
  307. + if (ET0 && isa<ArrayType>(ST->getElementType(1))) {
  308. + ActualSize = DL.getTypeAllocSize(ET0);
  309. + }
  310. + }
  311. + }
  312. +
  313. for (const HandlerInfo &HI : Handlers) {
  314. NamedRegionTimer T(HI.TimerName, HI.TimerDescription,
  315. HI.TimerGroupName, HI.TimerGroupDescription,
  316. @@ -897,12 +910,24 @@ void AsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
  317.  
  318. OutStreamer->switchSection(TheSection);
  319.  
  320. + if (GV->needsSanitizerAlias()) {
  321. + emitLinkage(GV, EmittedInitSym);
  322. + OutStreamer->emitLabel(EmittedInitSym);
  323. + if (MAI->hasDotTypeDotSizeDirective())
  324. + OutStreamer->emitELFSize(EmittedInitSym,
  325. + MCConstantExpr::create(ActualSize, OutContext));
  326. + EmittedInitSym = OutContext.getOrCreateSymbol(
  327. + GVSym->getName() + Twine(".sanitized"));
  328. + emitVisibility(EmittedInitSym, GV->getVisibility(), !GV->isDeclaration());
  329. + }
  330. +
  331. emitLinkage(GV, EmittedInitSym);
  332. emitAlignment(Alignment, GV);
  333.  
  334. OutStreamer->emitLabel(EmittedInitSym);
  335. MCSymbol *LocalAlias = getSymbolPreferLocal(*GV);
  336. - if (LocalAlias != EmittedInitSym)
  337. + if ((LocalAlias != EmittedInitSym) &&
  338. + !GV->needsSanitizerAlias())
  339. OutStreamer->emitLabel(LocalAlias);
  340.  
  341. emitGlobalConstant(GV->getParent()->getDataLayout(), GV->getInitializer());
  342. diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp
  343. index c6ef332403fd..f6848a9fad12 100644
  344. --- a/llvm/lib/IR/AsmWriter.cpp
  345. +++ b/llvm/lib/IR/AsmWriter.cpp
  346. @@ -3724,6 +3724,8 @@ void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
  347. Out << ", sanitize_memtag";
  348. if (MD.IsDynInit)
  349. Out << ", sanitize_address_dyninit";
  350. + if (MD.NeedsAlias)
  351. + Out << ", sanitize_needs_alias";
  352. }
  353.  
  354. maybePrintComdat(Out, *GV);
  355. diff --git a/llvm/lib/Target/AArch64/AArch64GlobalsTagging.cpp b/llvm/lib/Target/AArch64/AArch64GlobalsTagging.cpp
  356. index 27959489e7df..4607112ba107 100644
  357. --- a/llvm/lib/Target/AArch64/AArch64GlobalsTagging.cpp
  358. +++ b/llvm/lib/Target/AArch64/AArch64GlobalsTagging.cpp
  359. @@ -118,6 +118,8 @@ static void tagGlobalDefinition(Module &M, GlobalVariable *G) {
  360. // Ensure that tagged globals don't get merged by ICF - as they should have
  361. // different tags at runtime.
  362. G->setUnnamedAddr(GlobalValue::UnnamedAddr::None);
  363. +
  364. + G->setNeedsSanitizerAlias(true);
  365. }
  366.  
  367. namespace {
  368. diff --git a/llvm/test/Assembler/globalvariable-attributes.ll b/llvm/test/Assembler/globalvariable-attributes.ll
  369. index 4882b447973c..5d323cdf571c 100644
  370. --- a/llvm/test/Assembler/globalvariable-attributes.ll
  371. +++ b/llvm/test/Assembler/globalvariable-attributes.ll
  372. @@ -14,6 +14,7 @@
  373. @g12 = global i32 2, code_model "kernel"
  374. @g13 = global i32 2, code_model "medium"
  375. @g14 = global i32 2, code_model "large"
  376. +@g15 = global i32 2, sanitize_needs_alias, align 4
  377.  
  378. attributes #0 = { "string" = "value" nobuiltin norecurse }
  379.  
  380. @@ -31,6 +32,7 @@ attributes #0 = { "string" = "value" nobuiltin norecurse }
  381. ; CHECK: @g12 = global i32 2, code_model "kernel"
  382. ; CHECK: @g13 = global i32 2, code_model "medium"
  383. ; CHECK: @g14 = global i32 2, code_model "large"
  384. +; CHECK: @g15 = global i32 2, sanitize_needs_alias, align 4
  385.  
  386. ; CHECK: attributes #0 = { "key"="value" "key2"="value2" }
  387. ; CHECK: attributes #1 = { "key3"="value3" }
  388. diff --git a/llvm/test/Bitcode/compatibility.ll b/llvm/test/Bitcode/compatibility.ll
  389. index ce6a6571ec14..4ce269666d98 100644
  390. --- a/llvm/test/Bitcode/compatibility.ll
  391. +++ b/llvm/test/Bitcode/compatibility.ll
  392. @@ -207,12 +207,14 @@ declare void @g.f1()
  393. @g.no_sanitize_address = global i32 0, no_sanitize_address
  394. @g.no_sanitize_hwaddress = global i32 0, no_sanitize_hwaddress
  395. @g.sanitize_memtag = global i32 0, sanitize_memtag
  396. +@g.sanitize_needs_alias = global i32 0, sanitize_needs_alias
  397. @g.no_sanitize_multiple = global i32 0, no_sanitize_address, no_sanitize_hwaddress
  398. @g.sanitize_address_dyninit = global i32 0, sanitize_address_dyninit
  399. @g.sanitize_multiple = global i32 0, sanitize_memtag, sanitize_address_dyninit
  400. ; CHECK: @g.no_sanitize_address = global i32 0, no_sanitize_address
  401. ; CHECK: @g.no_sanitize_hwaddress = global i32 0, no_sanitize_hwaddress
  402. ; CHECK: @g.sanitize_memtag = global i32 0, sanitize_memtag
  403. +; CHECK: @g.sanitize_needs_alias = global i32 0, sanitize_needs_alias
  404. ; CHECK: @g.no_sanitize_multiple = global i32 0, no_sanitize_address, no_sanitize_hwaddress
  405. ; CHECK: @g.sanitize_address_dyninit = global i32 0, sanitize_address_dyninit
  406. ; CHECK: @g.sanitize_multiple = global i32 0, sanitize_memtag, sanitize_address_dyninit
  407.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement