Skip to content

Disable memtag sanitization for global fnptrs going into .ctors #70186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions clang/test/CodeGen/memtag-globals-asm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,23 @@ int f(int x) {
// CHECK-Q-DAG: ldr {{.*}}, [[[REG_O2]]]
function_int;
}

typedef void (*func_t)(void);
#define CONSTRUCTOR(section_name) \
__attribute__((used)) __attribute__((section(section_name)))

__attribute__((constructor(0))) void func_constructor() {}
CONSTRUCTOR(".init") func_t func_init = func_constructor;
CONSTRUCTOR(".fini") func_t func_fini = func_constructor;
CONSTRUCTOR(".ctors") func_t func_ctors = func_constructor;
CONSTRUCTOR(".dtors") func_t func_dtors = func_constructor;
CONSTRUCTOR(".init_array") func_t func_init_array = func_constructor;
CONSTRUCTOR(".fini_array") func_t func_fini_array = func_constructor;

// CHECK-NOT: .memtag func_constructor
// CHECK-NOT: .memtag func_init
// CHECK-NOT: .memtag func_fini
// CHECK-NOT: .memtag func_ctors
// CHECK-NOT: .memtag func_dtors
// CHECK-NOT: .memtag func_init_array
// CHECK-NOT: .memtag func_fini_array
12 changes: 12 additions & 0 deletions llvm/lib/Target/AArch64/AArch64GlobalsTagging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ static bool shouldTagGlobal(GlobalVariable &G) {
return false;
}

// Don't instrument function pointers that are going into various init arrays
// via `__attribute__((section(<foo>)))`:
// https://github.com/llvm/llvm-project/issues/69939
if (G.hasSection() &&
(G.getSection() == ".init" || G.getSection() == ".fini" ||
G.getSection() == ".init_array" || G.getSection() == ".fini_array" ||
G.getSection() == ".ctors" || G.getSection() == ".dtors")) {
Meta.Memtag = false;
G.setSanitizerMetadata(Meta);
return false;
}

return true;
}

Expand Down