llvm.org GIT mirror llvm / 209d3c2
[WebAssembly] Validate exports when parsing object files Subscribers: jfb, dschuff, jgravelle-google, aheejin Differential Revision: https://reviews.llvm.org/D37358 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@312286 91177308-0d34-0410-b5e6-96231b3b80d8 Sam Clegg 2 years ago
6 changed file(s) with 75 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
220220 uint32_t StartFunction = -1;
221221 bool HasLinkingSection = false;
222222 wasm::WasmLinkingData LinkingData;
223 uint32_t NumImportedGlobals = 0;
224 uint32_t NumImportedFunctions = 0;
223225
224226 StringMap SymbolMap;
225227 };
471471 Im.Kind = readUint8(Ptr);
472472 switch (Im.Kind) {
473473 case wasm::WASM_EXTERNAL_FUNCTION:
474 NumImportedFunctions++;
474475 Im.SigIndex = readVaruint32(Ptr);
475476 SymbolMap.try_emplace(Im.Field, Symbols.size());
476477 Symbols.emplace_back(Im.Field, WasmSymbol::SymbolType::FUNCTION_IMPORT,
479480 << " sym index:" << Symbols.size() << "\n");
480481 break;
481482 case wasm::WASM_EXTERNAL_GLOBAL:
483 NumImportedGlobals++;
482484 Im.Global.Type = readVarint7(Ptr);
483485 Im.Global.Mutable = readVaruint1(Ptr);
484486 SymbolMap.try_emplace(Im.Field, Symbols.size());
579581 switch (Ex.Kind) {
580582 case wasm::WASM_EXTERNAL_FUNCTION:
581583 ExportType = WasmSymbol::SymbolType::FUNCTION_EXPORT;
584 if (Ex.Index >= FunctionTypes.size() + NumImportedFunctions)
585 return make_error("Invalid function export",
586 object_error::parse_failed);
582587 MakeSymbol = true;
583588 break;
584589 case wasm::WASM_EXTERNAL_GLOBAL:
585590 ExportType = WasmSymbol::SymbolType::GLOBAL_EXPORT;
591 if (Ex.Index >= Globals.size() + NumImportedGlobals)
592 return make_error("Invalid global export",
593 object_error::parse_failed);
586594 MakeSymbol = true;
587595 break;
588596 case wasm::WASM_EXTERNAL_MEMORY:
22 FileHeader:
33 Version: 0x00000001
44 Sections:
5 - Type: FUNCTION
6 FunctionTypes: [ 0, 0 ]
7 - Type: GLOBAL
8 Globals:
9 - Type: I32
10 Mutable: false
11 InitExpr:
12 Opcode: I64_CONST
13 Value: 32
14 - Type: I32
15 Mutable: false
16 InitExpr:
17 Opcode: I64_CONST
18 Value: 64
519 - Type: EXPORT
620 Exports:
721 - Name: function_export
0 # RUN: yaml2obj < %s | not obj2yaml 2>&1 | FileCheck %s
1
2 --- !WASM
3 FileHeader:
4 Version: 0x00000001
5 Sections:
6 - Type: EXPORT
7 Exports:
8 - Name: invalid_function_index
9 Kind: FUNCTION
10 Index: 0x00000001
11
12 # CHECK: Error reading file: : Invalid function export
1111 - ReturnType: I32
1212 ParamTypes:
1313 - I32
14 - Type: FUNCTION
15 FunctionTypes: [ 0, 0, 0, 0, 0 ]
16 - Type: GLOBAL
17 Globals:
18 - Type: I32
19 Mutable: false
20 InitExpr:
21 Opcode: I64_CONST
22 Value: 32
23 - Type: I32
24 Mutable: false
25 InitExpr:
26 Opcode: I64_CONST
27 Value: 64
28 - Type: I32
29 Mutable: false
30 InitExpr:
31 Opcode: I64_CONST
32 Value: 1024
1433 - Type: EXPORT
1534 Exports:
1635 - Name: foo
1111 - ReturnType: I32
1212 ParamTypes:
1313 - I32
14 - Type: FUNCTION
15 FunctionTypes: [ 0, 0, 0, 0 ]
1416 - Type: IMPORT
1517 Imports:
1618 - Module: env
2224 Kind: GLOBAL
2325 GlobalType: I32
2426 GlobalMutable: false
27 - Type: GLOBAL
28 Globals:
29 - Type: I32
30 Mutable: false
31 InitExpr:
32 Opcode: I64_CONST
33 Value: 32
34 - Type: I32
35 Mutable: false
36 InitExpr:
37 Opcode: I64_CONST
38 Value: 64
39 - Type: I32
40 Mutable: false
41 InitExpr:
42 Opcode: I64_CONST
43 Value: 1024
2544 - Type: EXPORT
2645 Exports:
2746 - Name: weak_global_func