Skip to content

Commit b5c5977

Browse files
sbc100mahesh-attarde
authored andcommitted
[lld][WebAssembly] Fix check for exporting mutable globals (llvm#160787)
We were not actually checking whether the global in question was actually mutable before reporting the error.
1 parent 427bacd commit b5c5977

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

lld/test/wasm/mutable-global-exports.s

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
.globl _start
1818
.globl foo_global
19+
.globl bar_global
20+
21+
.globaltype bar_global, i32, immutable
22+
bar_global:
1923

2024
.globaltype foo_global, i32
2125
foo_global:
@@ -33,6 +37,7 @@ _start:
3337
.ascii "atomics"
3438

3539
# CHECK-ERR: mutable global exported but 'mutable-globals' feature not present in inputs: `foo_global`. Use --no-check-features to suppress
40+
# CHECK-ERR-NOT: bar_global
3641

3742
# CHECK: - Type: EXPORT
3843
# CHECK-NEXT: Exports:
@@ -74,36 +79,39 @@ _start:
7479
# CHECK-ALL-NEXT: - Name: foo_global
7580
# CHECK-ALL-NEXT: Kind: GLOBAL
7681
# CHECK-ALL-NEXT: Index: 1
77-
# CHECK-ALL-NEXT: - Name: __dso_handle
82+
# CHECK-ALL-NEXT: - Name: bar_global
7883
# CHECK-ALL-NEXT: Kind: GLOBAL
7984
# CHECK-ALL-NEXT: Index: 2
80-
# CHECK-ALL-NEXT: - Name: __data_end
85+
# CHECK-ALL-NEXT: - Name: __dso_handle
8186
# CHECK-ALL-NEXT: Kind: GLOBAL
8287
# CHECK-ALL-NEXT: Index: 3
83-
# CHECK-ALL-NEXT: - Name: __stack_low
88+
# CHECK-ALL-NEXT: - Name: __data_end
8489
# CHECK-ALL-NEXT: Kind: GLOBAL
8590
# CHECK-ALL-NEXT: Index: 4
86-
# CHECK-ALL-NEXT: - Name: __stack_high
91+
# CHECK-ALL-NEXT: - Name: __stack_low
8792
# CHECK-ALL-NEXT: Kind: GLOBAL
8893
# CHECK-ALL-NEXT: Index: 5
89-
# CHECK-ALL-NEXT: - Name: __global_base
94+
# CHECK-ALL-NEXT: - Name: __stack_high
9095
# CHECK-ALL-NEXT: Kind: GLOBAL
9196
# CHECK-ALL-NEXT: Index: 6
92-
# CHECK-ALL-NEXT: - Name: __heap_base
97+
# CHECK-ALL-NEXT: - Name: __global_base
9398
# CHECK-ALL-NEXT: Kind: GLOBAL
9499
# CHECK-ALL-NEXT: Index: 7
95-
# CHECK-ALL-NEXT: - Name: __heap_end
100+
# CHECK-ALL-NEXT: - Name: __heap_base
96101
# CHECK-ALL-NEXT: Kind: GLOBAL
97102
# CHECK-ALL-NEXT: Index: 8
98-
# CHECK-ALL-NEXT: - Name: __memory_base
103+
# CHECK-ALL-NEXT: - Name: __heap_end
99104
# CHECK-ALL-NEXT: Kind: GLOBAL
100105
# CHECK-ALL-NEXT: Index: 9
101-
# CHECK-ALL-NEXT: - Name: __table_base
106+
# CHECK-ALL-NEXT: - Name: __memory_base
102107
# CHECK-ALL-NEXT: Kind: GLOBAL
103108
# CHECK-ALL-NEXT: Index: 10
104-
# CHECK-ALL-NEXT: - Name: __wasm_first_page_end
109+
# CHECK-ALL-NEXT: - Name: __table_base
105110
# CHECK-ALL-NEXT: Kind: GLOBAL
106111
# CHECK-ALL-NEXT: Index: 11
112+
# CHECK-ALL-NEXT: - Name: __wasm_first_page_end
113+
# CHECK-ALL-NEXT: Kind: GLOBAL
114+
# CHECK-ALL-NEXT: Index: 12
107115
# CHECK-ALL-NEXT: - Type: CODE
108116

109117
# CHECK-ALL: Name: target_features

lld/wasm/Writer.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ void Writer::populateTargetFeatures() {
576576

577577
if (ctx.isPic) {
578578
// This should not be necessary because all PIC objects should
579-
// contain the mutable-globals feature.
579+
// contain the `mutable-globals` feature.
580580
// TODO (https://github.com/llvm/llvm-project/issues/51681)
581581
allowed.insert("mutable-globals");
582582
}
@@ -703,10 +703,12 @@ void Writer::checkImportExportTargetFeatures() {
703703
}
704704
}
705705
for (const Symbol *sym : out.exportSec->exportedSymbols) {
706-
if (isa<GlobalSymbol>(sym)) {
707-
error(Twine("mutable global exported but 'mutable-globals' feature "
708-
"not present in inputs: `") +
709-
toString(*sym) + "`. Use --no-check-features to suppress.");
706+
if (auto *global = dyn_cast<GlobalSymbol>(sym)) {
707+
if (global->getGlobalType()->Mutable) {
708+
error(Twine("mutable global exported but 'mutable-globals' feature "
709+
"not present in inputs: `") +
710+
toString(*sym) + "`. Use --no-check-features to suppress.");
711+
}
710712
}
711713
}
712714
}

0 commit comments

Comments
 (0)