[WebAssembly] Disallow weak undefined globals in the object format

This implements https://github.com/WebAssembly/tool-conventions/pull/47

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

llvm-svn: 327146
This commit is contained in:
Nicholas Wilson 2018-03-09 16:30:44 +00:00
parent 69df838b52
commit 15f349f76f
4 changed files with 35 additions and 14 deletions

View File

@ -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();

View File

@ -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;

View File

@ -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

View File

@ -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:
@ -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
@ -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
@ -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