Skip to content

Commit ba31ed4

Browse files
authored
Disable memtag sanitization for global fnptrs going into .ctors (#70186)
Looks like there's code out there that, instead of using '__attribute__((constructor(x)))' to add constructor functions, they just declare a global function pointer and use '__attribute__((section('.ctors')))' instead. Problem is, with memtag-globals, we pad the global function pointer to be 16 bytes large. This of course means we have an 8-byte real function pointer, then 8 bytes of zero padding, and this trips up the loader when it processes this section. Fixes #69939
1 parent 0b5e0fb commit ba31ed4

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

clang/test/CodeGen/memtag-globals-asm.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,23 @@ int f(int x) {
259259
// CHECK-Q-DAG: ldr {{.*}}, [[[REG_O2]]]
260260
function_int;
261261
}
262+
263+
typedef void (*func_t)(void);
264+
#define CONSTRUCTOR(section_name) \
265+
__attribute__((used)) __attribute__((section(section_name)))
266+
267+
__attribute__((constructor(0))) void func_constructor() {}
268+
CONSTRUCTOR(".init") func_t func_init = func_constructor;
269+
CONSTRUCTOR(".fini") func_t func_fini = func_constructor;
270+
CONSTRUCTOR(".ctors") func_t func_ctors = func_constructor;
271+
CONSTRUCTOR(".dtors") func_t func_dtors = func_constructor;
272+
CONSTRUCTOR(".init_array") func_t func_init_array = func_constructor;
273+
CONSTRUCTOR(".fini_array") func_t func_fini_array = func_constructor;
274+
275+
// CHECK-NOT: .memtag func_constructor
276+
// CHECK-NOT: .memtag func_init
277+
// CHECK-NOT: .memtag func_fini
278+
// CHECK-NOT: .memtag func_ctors
279+
// CHECK-NOT: .memtag func_dtors
280+
// CHECK-NOT: .memtag func_init_array
281+
// CHECK-NOT: .memtag func_fini_array

llvm/lib/Target/AArch64/AArch64GlobalsTagging.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ static bool shouldTagGlobal(GlobalVariable &G) {
4343
return false;
4444
}
4545

46+
// Don't instrument function pointers that are going into various init arrays
47+
// via `__attribute__((section(<foo>)))`:
48+
// https://github.com/llvm/llvm-project/issues/69939
49+
if (G.hasSection() &&
50+
(G.getSection() == ".init" || G.getSection() == ".fini" ||
51+
G.getSection() == ".init_array" || G.getSection() == ".fini_array" ||
52+
G.getSection() == ".ctors" || G.getSection() == ".dtors")) {
53+
Meta.Memtag = false;
54+
G.setSanitizerMetadata(Meta);
55+
return false;
56+
}
57+
4658
return true;
4759
}
4860

0 commit comments

Comments
 (0)