[llvm] r312286 - [WebAssembly] Validate exports when parsing object files

Sam Clegg via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 31 14:43:45 PDT 2017


Author: sbc
Date: Thu Aug 31 14:43:45 2017
New Revision: 312286

URL: http://llvm.org/viewvc/llvm-project?rev=312286&view=rev
Log:
[WebAssembly] Validate exports when parsing object files

Subscribers: jfb, dschuff, jgravelle-google, aheejin

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

Added:
    llvm/trunk/test/ObjectYAML/wasm/invalid_export.yaml
Modified:
    llvm/trunk/include/llvm/Object/Wasm.h
    llvm/trunk/lib/Object/WasmObjectFile.cpp
    llvm/trunk/test/ObjectYAML/wasm/export_section.yaml
    llvm/trunk/test/tools/llvm-nm/wasm/exports.yaml
    llvm/trunk/test/tools/llvm-nm/wasm/weak-symbols.yaml

Modified: llvm/trunk/include/llvm/Object/Wasm.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/Wasm.h?rev=312286&r1=312285&r2=312286&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/Wasm.h (original)
+++ llvm/trunk/include/llvm/Object/Wasm.h Thu Aug 31 14:43:45 2017
@@ -221,6 +221,8 @@ private:
   uint32_t StartFunction = -1;
   bool HasLinkingSection = false;
   wasm::WasmLinkingData LinkingData;
+  uint32_t NumImportedGlobals = 0;
+  uint32_t NumImportedFunctions = 0;
 
   StringMap<uint32_t> SymbolMap;
 };

Modified: llvm/trunk/lib/Object/WasmObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/WasmObjectFile.cpp?rev=312286&r1=312285&r2=312286&view=diff
==============================================================================
--- llvm/trunk/lib/Object/WasmObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/WasmObjectFile.cpp Thu Aug 31 14:43:45 2017
@@ -472,6 +472,7 @@ Error WasmObjectFile::parseImportSection
     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
                    << " 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
     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:

Modified: llvm/trunk/test/ObjectYAML/wasm/export_section.yaml
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/ObjectYAML/wasm/export_section.yaml?rev=312286&r1=312285&r2=312286&view=diff
==============================================================================
--- llvm/trunk/test/ObjectYAML/wasm/export_section.yaml (original)
+++ llvm/trunk/test/ObjectYAML/wasm/export_section.yaml Thu Aug 31 14:43:45 2017
@@ -3,6 +3,20 @@
 FileHeader:
   Version:         0x00000001
 Sections:
+  - Type:            FUNCTION
+    FunctionTypes: [ 0, 0 ]
+  - Type:            GLOBAL
+    Globals:
+      - Type:        I32
+        Mutable:     false
+        InitExpr:
+          Opcode:          I64_CONST
+          Value:           32
+      - Type:        I32
+        Mutable:     false
+        InitExpr:
+          Opcode:          I64_CONST
+          Value:           64
   - Type:            EXPORT
     Exports:         
       - Name:            function_export

Added: llvm/trunk/test/ObjectYAML/wasm/invalid_export.yaml
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/ObjectYAML/wasm/invalid_export.yaml?rev=312286&view=auto
==============================================================================
--- llvm/trunk/test/ObjectYAML/wasm/invalid_export.yaml (added)
+++ llvm/trunk/test/ObjectYAML/wasm/invalid_export.yaml Thu Aug 31 14:43:45 2017
@@ -0,0 +1,13 @@
+# RUN: yaml2obj < %s | not obj2yaml 2>&1 | FileCheck %s
+
+--- !WASM
+FileHeader:
+  Version:         0x00000001
+Sections:
+  - Type:            EXPORT
+    Exports:
+      - Name:            invalid_function_index
+        Kind:            FUNCTION
+        Index:           0x00000001
+
+# CHECK: Error reading file: <stdin>: Invalid function export

Modified: llvm/trunk/test/tools/llvm-nm/wasm/exports.yaml
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-nm/wasm/exports.yaml?rev=312286&r1=312285&r2=312286&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-nm/wasm/exports.yaml (original)
+++ llvm/trunk/test/tools/llvm-nm/wasm/exports.yaml Thu Aug 31 14:43:45 2017
@@ -12,6 +12,25 @@ Sections:
       - ReturnType:      I32
         ParamTypes:
           - I32
+  - Type:            FUNCTION
+    FunctionTypes: [ 0, 0, 0, 0, 0 ]
+  - Type:            GLOBAL
+    Globals:
+      - Type:        I32
+        Mutable:     false
+        InitExpr:
+          Opcode:          I64_CONST
+          Value:           32
+      - Type:        I32
+        Mutable:     false
+        InitExpr:
+          Opcode:          I64_CONST
+          Value:           64
+      - Type:        I32
+        Mutable:     false
+        InitExpr:
+          Opcode:          I64_CONST
+          Value:           1024
   - Type:            EXPORT
     Exports:
       - Name:            foo

Modified: llvm/trunk/test/tools/llvm-nm/wasm/weak-symbols.yaml
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-nm/wasm/weak-symbols.yaml?rev=312286&r1=312285&r2=312286&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-nm/wasm/weak-symbols.yaml (original)
+++ llvm/trunk/test/tools/llvm-nm/wasm/weak-symbols.yaml Thu Aug 31 14:43:45 2017
@@ -12,6 +12,8 @@ Sections:
       - ReturnType:      I32
         ParamTypes:
           - I32
+  - Type:            FUNCTION
+    FunctionTypes: [ 0, 0, 0, 0 ]
   - Type:            IMPORT
     Imports:
       - Module:          env
@@ -23,6 +25,23 @@ Sections:
         Kind:            GLOBAL
         GlobalType:      I32
         GlobalMutable:   false
+  - Type:            GLOBAL
+    Globals:
+      - Type:        I32
+        Mutable:     false
+        InitExpr:
+          Opcode:          I64_CONST
+          Value:           32
+      - Type:        I32
+        Mutable:     false
+        InitExpr:
+          Opcode:          I64_CONST
+          Value:           64
+      - Type:        I32
+        Mutable:     false
+        InitExpr:
+          Opcode:          I64_CONST
+          Value:           1024
   - Type:            EXPORT
     Exports:
       - Name:            weak_global_func




More information about the llvm-commits mailing list