Skip to content

Commit 23b18fa

Browse files
authored
[MTE] Do not allow local aliases to MTE globals (#106280)
With this change and appropriate linker changes (https://r.android.com/3236256) AOSP boots with memtag-global throughout the platform. Without this change, we would sometimes generate PC-relative references to tagged globals, which then do not have the proper tag.
1 parent dac0f7e commit 23b18fa

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

llvm/lib/IR/Globals.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ bool GlobalValue::isInterposable() const {
110110
}
111111

112112
bool GlobalValue::canBenefitFromLocalAlias() const {
113+
if (isTagged()) {
114+
// Cannot create local aliases to MTE tagged globals. The address of a
115+
// tagged global includes a tag that is assigned by the loader in the
116+
// GOT.
117+
return false;
118+
}
113119
// See AsmPrinter::getSymbolPreferLocal(). For a deduplicate comdat kind,
114120
// references to a discarded local symbol from outside the group are not
115121
// allowed, so avoid the local alias.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2+
; RUN: llc -relocation-model=pic < %s | FileCheck %s
3+
4+
;; Test that we use do not the local alias for dso_local globals with MTE.
5+
6+
target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32"
7+
target triple = "aarch64-unknown-linux-android10000"
8+
9+
@x = dso_local global i32 1, sanitize_memtag, align 4
10+
@y = dso_local global i32 1, align 4
11+
12+
; Function Attrs: noinline optnone
13+
define dso_local i32 @main() #0 {
14+
; CHECK-LABEL: main:
15+
; CHECK: .Lmain$local:
16+
; CHECK-NEXT: .type .Lmain$local,@function
17+
; CHECK-NEXT: .cfi_startproc
18+
; CHECK-NEXT: // %bb.0: // %entry
19+
; CHECK-NEXT: adrp x8, :got:x
20+
; CHECK-NEXT: ldr x8, [x8, :got_lo12:x]
21+
; CHECK-NEXT: ldr w0, [x8]
22+
; CHECK-NEXT: ret
23+
entry:
24+
%0 = load i32, ptr @x, align 4
25+
ret i32 %0
26+
}
27+
28+
; Function Attrs: noinline optnone
29+
define dso_local i32 @main2() #0 {
30+
; CHECK-LABEL: main2:
31+
; CHECK: .Lmain2$local:
32+
; CHECK-NEXT: .type .Lmain2$local,@function
33+
; CHECK-NEXT: .cfi_startproc
34+
; CHECK-NEXT: // %bb.0: // %entry
35+
; CHECK-NEXT: adrp x8, .Ly$local
36+
; CHECK-NEXT: add x8, x8, :lo12:.Ly$local
37+
; CHECK-NEXT: ldr w0, [x8]
38+
; CHECK-NEXT: ret
39+
entry:
40+
%0 = load i32, ptr @y, align 4
41+
ret i32 %0
42+
}
43+
44+
45+
attributes #0 = { noinline optnone "target-cpu"="generic" "target-features"="+mte,+v8a" }
46+
47+
!llvm.module.flags = !{!2, !3}
48+
49+
!2 = !{i32 8, !"PIC Level", i32 2}
50+
!3 = !{i32 7, !"PIE Level", i32 0}

0 commit comments

Comments
 (0)