[WebAssembly] Validate exports when parsing object files

Subscribers: jfb, dschuff, jgravelle-google, aheejin

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

llvm-svn: 312286
This commit is contained in:
Sam Clegg
2017-08-31 21:43:45 +00:00
parent 67419e3947
commit a3b9fe6acd
6 changed files with 75 additions and 0 deletions

View File

@@ -472,6 +472,7 @@ Error WasmObjectFile::parseImportSection(const uint8_t *Ptr, const uint8_t *End)
Im.Kind = readUint8(Ptr);
switch (Im.Kind) {
case wasm::WASM_EXTERNAL_FUNCTION:
NumImportedFunctions++;
Im.SigIndex = readVaruint32(Ptr);
SymbolMap.try_emplace(Im.Field, Symbols.size());
Symbols.emplace_back(Im.Field, WasmSymbol::SymbolType::FUNCTION_IMPORT,
@@ -480,6 +481,7 @@ Error WasmObjectFile::parseImportSection(const uint8_t *Ptr, const uint8_t *End)
<< " sym index:" << Symbols.size() << "\n");
break;
case wasm::WASM_EXTERNAL_GLOBAL:
NumImportedGlobals++;
Im.Global.Type = readVarint7(Ptr);
Im.Global.Mutable = readVaruint1(Ptr);
SymbolMap.try_emplace(Im.Field, Symbols.size());
@@ -580,10 +582,16 @@ Error WasmObjectFile::parseExportSection(const uint8_t *Ptr, const uint8_t *End)
switch (Ex.Kind) {
case wasm::WASM_EXTERNAL_FUNCTION:
ExportType = WasmSymbol::SymbolType::FUNCTION_EXPORT;
if (Ex.Index >= FunctionTypes.size() + NumImportedFunctions)
return make_error<GenericBinaryError>("Invalid function export",
object_error::parse_failed);
MakeSymbol = true;
break;
case wasm::WASM_EXTERNAL_GLOBAL:
ExportType = WasmSymbol::SymbolType::GLOBAL_EXPORT;
if (Ex.Index >= Globals.size() + NumImportedGlobals)
return make_error<GenericBinaryError>("Invalid global export",
object_error::parse_failed);
MakeSymbol = true;
break;
case wasm::WASM_EXTERNAL_MEMORY: