Skip to content

Commit

Permalink
[WebAssembly] Disallow weak undefined globals in the object format
Browse files Browse the repository at this point in the history
This implements WebAssembly/tool-conventions#47

Differential Revision: https://reviews.llvm.org/D44201

llvm-svn=327146
  • Loading branch information
NWilson committed Mar 9, 2018
1 parent 7411810 commit 73eaa48
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
3 changes: 3 additions & 0 deletions llvm/lib/MC/WasmObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,9 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
Imports.push_back(Import);
WasmIndices[&WS] = NumFunctionImports++;
} else if (WS.isGlobal()) {
if (WS.isWeak())
report_fatal_error("undefined global symbol cannot be weak");

wasm::WasmImport Import;
Import.Module = WS.getModuleName();
Import.Field = WS.getName();
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/Object/WasmObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,11 @@ Error WasmObjectFile::parseLinkingSectionSymtab(const uint8_t *&Ptr,
IsDefined != isDefinedGlobalIndex(Info.ElementIndex))
return make_error<GenericBinaryError>("invalid global symbol index",
object_error::parse_failed);
if (!IsDefined &&
(Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) ==
wasm::WASM_SYMBOL_BINDING_WEAK)
return make_error<GenericBinaryError>("undefined weak global symbol",
object_error::parse_failed);
if (IsDefined) {
Info.Name = readString(Ptr);
unsigned GlobalIndex = Info.ElementIndex - NumImportedGlobals;
Expand Down
24 changes: 24 additions & 0 deletions llvm/test/ObjectYAML/wasm/invalid_global_weak.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# RUN: yaml2obj < %s | not obj2yaml 2>&1 | FileCheck %s

--- !WASM
FileHeader:
Version: 0x00000001
Sections:
- Type: IMPORT
Imports:
- Module: fiz
Field: imported_global
Kind: GLOBAL
GlobalType: I32
GlobalMutable: false
- Type: CUSTOM
Name: linking
SymbolTable:
- Index: 0
Kind: GLOBAL
Name: imported_global
Flags: [ BINDING_WEAK, UNDEFINED ]
Global: 0
...

# CHECK: Error reading file: <stdin>: undefined weak global symbol
17 changes: 3 additions & 14 deletions llvm/test/tools/llvm-nm/wasm/weak-symbols.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@ Sections:
Field: weak_import_func
Kind: FUNCTION
SigIndex: 0
- Module: env
Field: weak_import_global
Kind: GLOBAL
GlobalType: I32
GlobalMutable: false
- Type: FUNCTION
FunctionTypes: [ 0 ]
- Type: GLOBAL
Globals:
- Index: 1
- Index: 0
Type: I32
Mutable: false
InitExpr:
Expand Down Expand Up @@ -64,7 +59,7 @@ Sections:
Kind: GLOBAL
Name: weak_defined_global
Flags: [ BINDING_WEAK ]
Global: 1
Global: 0
- Index: 3
Kind: DATA
Name: weak_import_data
Expand All @@ -74,11 +69,6 @@ Sections:
Name: weak_import_func
Flags: [ BINDING_WEAK, UNDEFINED ]
Function: 0
- Index: 5
Kind: GLOBAL
Name: weak_import_global
Flags: [ BINDING_WEAK, UNDEFINED ]
Global: 0
SegmentInfo:
- Index: 0
Name: .rodata.constantData
Expand All @@ -88,7 +78,6 @@ Sections:

# CHECK: 00000000 W weak_defined_data
# CHECK-NEXT: 00000001 W weak_defined_func
# CHECK-NEXT: 00000001 W weak_defined_global
# CHECK-NEXT: 00000000 W weak_defined_global
# CHECK-NEXT: w weak_import_data
# CHECK-NEXT: w weak_import_func
# CHECK-NEXT: w weak_import_global

0 comments on commit 73eaa48

Please sign in to comment.