Skip to content

[AArch64] Bugfix when using execute-only and memtag sanitizer together #133084

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
Apr 1, 2025
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
16 changes: 11 additions & 5 deletions llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,11 +511,17 @@ void AArch64TargetELFStreamer::finish() {
})) {
auto *Text =
static_cast<MCSectionELF *>(Ctx.getObjectFileInfo()->getTextSection());
for (auto &F : *Text)
if (auto *DF = dyn_cast<MCDataFragment>(&F))
if (!DF->getContents().empty())
return;
Text->setFlags(Text->getFlags() | ELF::SHF_AARCH64_PURECODE);
bool Empty = true;
for (auto &F : *Text) {
if (auto *DF = dyn_cast<MCDataFragment>(&F)) {
if (!DF->getContents().empty()) {
Empty = false;
break;
}
}
}
Comment on lines +514 to +522
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use something like all_of(*Text, [](***) {return DF->getContents().empty();} here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to do that too, but I get the following error:

error: no matching function for call to '__iterator_category'
note: candidate template ignored: substitution failure [with _Iter = llvm::MCSection::iterator]: no type named 'iterator_category' in 'std::iterator_traits<llvm::MCSection::iterator>'

To me it looks like MCSection::iterator is not a proper iterator according to the standard library, so we can't use std::all_of (which llvm::all_of uses).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Thanks for checking.

if (Empty)
Text->setFlags(Text->getFlags() | ELF::SHF_AARCH64_PURECODE);
}

MCSectionELF *MemtagSec = nullptr;
Expand Down
18 changes: 18 additions & 0 deletions llvm/test/MC/AArch64/execute-only-memtag.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
; RUN: llc %s -mtriple=aarch64-linux-android31 -filetype=obj -o %t.o
; RUN: llvm-readelf -r %t.o | FileCheck %s

; CHECK: Relocation section '.rela.memtag.globals.static' at offset {{.*}} contains 1 entries:
; CHECK-NEXT: Type {{.*}} Symbol's Name
; CHECK-NEXT: R_AARCH64_NONE {{.*}} global

@global = global i32 1, sanitize_memtag

define void @foo() {
ret void
}

define void @bar() #0 {
ret void
}

attributes #0 = { "target-features"="+execute-only" }