diff options
author | Dan Gohman <dev@sunfishcode.online> | 2024-12-05 16:02:40 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-12-06 00:02:40 +0000 |
commit | 4e7d7efe6e9a786370848e669041bdc237730a8b (patch) | |
tree | 59915d25d4b8644610c170819b647b35750166ad | |
parent | c6a4f637bf1dd9b1c4e2f75249a82af9d5b7e71e (diff) | |
download | wabt-4e7d7efe6e9a786370848e669041bdc237730a8b.tar.gz wabt-4e7d7efe6e9a786370848e669041bdc237730a8b.tar.bz2 wabt-4e7d7efe6e9a786370848e669041bdc237730a8b.zip |
Issue a nicer error message on wasm components. (#2515)
Decode just enough of the component binary format to recognize when the
input is a component, and issue a dedicated error message for it.
Before:
0000008: error: bad wasm file version: 0x1000d (expected 0x1)
After:
0000008: error: wasm components are not yet supported in this tool
-rw-r--r-- | include/wabt/binary.h | 2 | ||||
-rw-r--r-- | src/binary-reader.cc | 45 | ||||
-rw-r--r-- | test/binary/unrecognized-layer.txt | 7 | ||||
-rw-r--r-- | test/binary/unsupported-component.txt | 7 | ||||
-rw-r--r-- | test/spec/binary.txt | 10 | ||||
-rw-r--r-- | test/spec/exception-handling/binary.txt | 10 | ||||
-rw-r--r-- | test/spec/memory64/binary.txt | 10 | ||||
-rw-r--r-- | test/spec/multi-memory/binary.txt | 10 |
8 files changed, 70 insertions, 31 deletions
diff --git a/include/wabt/binary.h b/include/wabt/binary.h index 98575f96..a8c768f3 100644 --- a/include/wabt/binary.h +++ b/include/wabt/binary.h @@ -21,6 +21,8 @@ #define WABT_BINARY_MAGIC 0x6d736100 #define WABT_BINARY_VERSION 1 +#define WABT_BINARY_LAYER_MODULE 0 +#define WABT_BINARY_LAYER_COMPONENT 1 #define WABT_BINARY_LIMITS_HAS_MAX_FLAG 0x1 #define WABT_BINARY_LIMITS_IS_SHARED_FLAG 0x2 #define WABT_BINARY_LIMITS_IS_64_FLAG 0x4 diff --git a/src/binary-reader.cc b/src/binary-reader.cc index 7b0b4187..e1859a1c 100644 --- a/src/binary-reader.cc +++ b/src/binary-reader.cc @@ -37,12 +37,17 @@ #include <alloca.h> #endif -#define ERROR_IF(expr, ...) \ - do { \ - if (expr) { \ - PrintError(__VA_ARGS__); \ - return Result::Error; \ - } \ +#define ERROR(...) \ + do { \ + PrintError(__VA_ARGS__); \ + return Result::Error; \ + } while (0) + +#define ERROR_IF(expr, ...) \ + do { \ + if (expr) { \ + ERROR(__VA_ARGS__); \ + } \ } while (0) #define ERROR_UNLESS(expr, ...) ERROR_IF(!(expr), __VA_ARGS__) @@ -100,6 +105,7 @@ class BinaryReader { const char* type_name, const char* desc); [[nodiscard]] Result ReadU8(uint8_t* out_value, const char* desc); + [[nodiscard]] Result ReadU16(uint16_t* out_value, const char* desc); [[nodiscard]] Result ReadU32(uint32_t* out_value, const char* desc); [[nodiscard]] Result ReadF32(uint32_t* out_value, const char* desc); [[nodiscard]] Result ReadF64(uint64_t* out_value, const char* desc); @@ -300,6 +306,10 @@ Result BinaryReader::ReadU8(uint8_t* out_value, const char* desc) { return ReadT(out_value, "uint8_t", desc); } +Result BinaryReader::ReadU16(uint16_t* out_value, const char* desc) { + return ReadT(out_value, "uint16_t", desc); +} + Result BinaryReader::ReadU32(uint32_t* out_value, const char* desc) { return ReadT(out_value, "uint32_t", desc); } @@ -3086,11 +3096,24 @@ Result BinaryReader::ReadModule(const ReadModuleOptions& options) { uint32_t magic = 0; CHECK_RESULT(ReadU32(&magic, "magic")); ERROR_UNLESS(magic == WABT_BINARY_MAGIC, "bad magic value"); - uint32_t version = 0; - CHECK_RESULT(ReadU32(&version, "version")); - ERROR_UNLESS(version == WABT_BINARY_VERSION, - "bad wasm file version: %#x (expected %#x)", version, - WABT_BINARY_VERSION); + + uint16_t version = 0, layer = 0; + CHECK_RESULT(ReadU16(&version, "version")); + CHECK_RESULT(ReadU16(&layer, "layer")); + + switch (layer) { + case WABT_BINARY_LAYER_MODULE: + ERROR_UNLESS(version == WABT_BINARY_VERSION, + "bad wasm file version: %#x (expected %#x)", version, + WABT_BINARY_VERSION); + break; + case WABT_BINARY_LAYER_COMPONENT: + ERROR("wasm components are not yet supported in this tool"); + break; + default: + ERROR("unsupported wasm layer: %#x", layer); + break; + } CALLBACK(BeginModule, version); CHECK_RESULT(ReadSections(ReadSectionsOptions{options.stop_on_first_error})); diff --git a/test/binary/unrecognized-layer.txt b/test/binary/unrecognized-layer.txt new file mode 100644 index 00000000..3ba231dc --- /dev/null +++ b/test/binary/unrecognized-layer.txt @@ -0,0 +1,7 @@ +;;; TOOL: run-gen-wasm-bad +magic +0xe 0 0x2 0 +(;; STDERR ;;; +0000008: error: unsupported wasm layer: 0x2 +0000008: error: unsupported wasm layer: 0x2 +;;; STDERR ;;) diff --git a/test/binary/unsupported-component.txt b/test/binary/unsupported-component.txt new file mode 100644 index 00000000..c1167f0d --- /dev/null +++ b/test/binary/unsupported-component.txt @@ -0,0 +1,7 @@ +;;; TOOL: run-gen-wasm-bad +magic +0xd 0 0x1 0 +(;; STDERR ;;; +0000008: error: wasm components are not yet supported in this tool +0000008: error: wasm components are not yet supported in this tool +;;; STDERR ;;) diff --git a/test/spec/binary.txt b/test/spec/binary.txt index fd20a651..a4cfc29e 100644 --- a/test/spec/binary.txt +++ b/test/spec/binary.txt @@ -40,11 +40,11 @@ out/test/spec/binary.wast:31: assert_malformed passed: out/test/spec/binary.wast:34: assert_malformed passed: 0000004: error: bad magic value out/test/spec/binary.wast:37: assert_malformed passed: - 0000004: error: unable to read uint32_t: version + 0000004: error: unable to read uint16_t: version out/test/spec/binary.wast:38: assert_malformed passed: - 0000004: error: unable to read uint32_t: version + 0000004: error: unable to read uint16_t: version out/test/spec/binary.wast:39: assert_malformed passed: - 0000004: error: unable to read uint32_t: version + 0000006: error: unable to read uint16_t: layer out/test/spec/binary.wast:40: assert_malformed passed: 0000008: error: bad wasm file version: 0 (expected 0x1) out/test/spec/binary.wast:41: assert_malformed passed: @@ -54,9 +54,9 @@ out/test/spec/binary.wast:42: assert_malformed passed: out/test/spec/binary.wast:43: assert_malformed passed: 0000008: error: bad wasm file version: 0x100 (expected 0x1) out/test/spec/binary.wast:44: assert_malformed passed: - 0000008: error: bad wasm file version: 0x10000 (expected 0x1) + 0000008: error: wasm components are not yet supported in this tool out/test/spec/binary.wast:45: assert_malformed passed: - 0000008: error: bad wasm file version: 0x1000000 (expected 0x1) + 0000008: error: unsupported wasm layer: 0x100 out/test/spec/binary.wast:48: assert_malformed passed: 000000a: error: invalid section code: 14 out/test/spec/binary.wast:49: assert_malformed passed: diff --git a/test/spec/exception-handling/binary.txt b/test/spec/exception-handling/binary.txt index 8d253cf7..a289d3be 100644 --- a/test/spec/exception-handling/binary.txt +++ b/test/spec/exception-handling/binary.txt @@ -41,11 +41,11 @@ out/test/spec/exception-handling/binary.wast:31: assert_malformed passed: out/test/spec/exception-handling/binary.wast:34: assert_malformed passed: 0000004: error: bad magic value out/test/spec/exception-handling/binary.wast:37: assert_malformed passed: - 0000004: error: unable to read uint32_t: version + 0000004: error: unable to read uint16_t: version out/test/spec/exception-handling/binary.wast:38: assert_malformed passed: - 0000004: error: unable to read uint32_t: version + 0000004: error: unable to read uint16_t: version out/test/spec/exception-handling/binary.wast:39: assert_malformed passed: - 0000004: error: unable to read uint32_t: version + 0000006: error: unable to read uint16_t: layer out/test/spec/exception-handling/binary.wast:40: assert_malformed passed: 0000008: error: bad wasm file version: 0 (expected 0x1) out/test/spec/exception-handling/binary.wast:41: assert_malformed passed: @@ -55,9 +55,9 @@ out/test/spec/exception-handling/binary.wast:42: assert_malformed passed: out/test/spec/exception-handling/binary.wast:43: assert_malformed passed: 0000008: error: bad wasm file version: 0x100 (expected 0x1) out/test/spec/exception-handling/binary.wast:44: assert_malformed passed: - 0000008: error: bad wasm file version: 0x10000 (expected 0x1) + 0000008: error: wasm components are not yet supported in this tool out/test/spec/exception-handling/binary.wast:45: assert_malformed passed: - 0000008: error: bad wasm file version: 0x1000000 (expected 0x1) + 0000008: error: unsupported wasm layer: 0x100 out/test/spec/exception-handling/binary.wast:48: assert_malformed passed: 000000a: error: invalid section code: 14 out/test/spec/exception-handling/binary.wast:49: assert_malformed passed: diff --git a/test/spec/memory64/binary.txt b/test/spec/memory64/binary.txt index e34a5f79..6b3bfef5 100644 --- a/test/spec/memory64/binary.txt +++ b/test/spec/memory64/binary.txt @@ -41,11 +41,11 @@ out/test/spec/memory64/binary.wast:31: assert_malformed passed: out/test/spec/memory64/binary.wast:34: assert_malformed passed: 0000004: error: bad magic value out/test/spec/memory64/binary.wast:37: assert_malformed passed: - 0000004: error: unable to read uint32_t: version + 0000004: error: unable to read uint16_t: version out/test/spec/memory64/binary.wast:38: assert_malformed passed: - 0000004: error: unable to read uint32_t: version + 0000004: error: unable to read uint16_t: version out/test/spec/memory64/binary.wast:39: assert_malformed passed: - 0000004: error: unable to read uint32_t: version + 0000006: error: unable to read uint16_t: layer out/test/spec/memory64/binary.wast:40: assert_malformed passed: 0000008: error: bad wasm file version: 0 (expected 0x1) out/test/spec/memory64/binary.wast:41: assert_malformed passed: @@ -55,9 +55,9 @@ out/test/spec/memory64/binary.wast:42: assert_malformed passed: out/test/spec/memory64/binary.wast:43: assert_malformed passed: 0000008: error: bad wasm file version: 0x100 (expected 0x1) out/test/spec/memory64/binary.wast:44: assert_malformed passed: - 0000008: error: bad wasm file version: 0x10000 (expected 0x1) + 0000008: error: wasm components are not yet supported in this tool out/test/spec/memory64/binary.wast:45: assert_malformed passed: - 0000008: error: bad wasm file version: 0x1000000 (expected 0x1) + 0000008: error: unsupported wasm layer: 0x100 out/test/spec/memory64/binary.wast:48: assert_malformed passed: 000000a: error: invalid section code: 14 out/test/spec/memory64/binary.wast:49: assert_malformed passed: diff --git a/test/spec/multi-memory/binary.txt b/test/spec/multi-memory/binary.txt index d88e22f5..ed0842c9 100644 --- a/test/spec/multi-memory/binary.txt +++ b/test/spec/multi-memory/binary.txt @@ -41,11 +41,11 @@ out/test/spec/multi-memory/binary.wast:31: assert_malformed passed: out/test/spec/multi-memory/binary.wast:34: assert_malformed passed: 0000004: error: bad magic value out/test/spec/multi-memory/binary.wast:37: assert_malformed passed: - 0000004: error: unable to read uint32_t: version + 0000004: error: unable to read uint16_t: version out/test/spec/multi-memory/binary.wast:38: assert_malformed passed: - 0000004: error: unable to read uint32_t: version + 0000004: error: unable to read uint16_t: version out/test/spec/multi-memory/binary.wast:39: assert_malformed passed: - 0000004: error: unable to read uint32_t: version + 0000006: error: unable to read uint16_t: layer out/test/spec/multi-memory/binary.wast:40: assert_malformed passed: 0000008: error: bad wasm file version: 0 (expected 0x1) out/test/spec/multi-memory/binary.wast:41: assert_malformed passed: @@ -55,9 +55,9 @@ out/test/spec/multi-memory/binary.wast:42: assert_malformed passed: out/test/spec/multi-memory/binary.wast:43: assert_malformed passed: 0000008: error: bad wasm file version: 0x100 (expected 0x1) out/test/spec/multi-memory/binary.wast:44: assert_malformed passed: - 0000008: error: bad wasm file version: 0x10000 (expected 0x1) + 0000008: error: wasm components are not yet supported in this tool out/test/spec/multi-memory/binary.wast:45: assert_malformed passed: - 0000008: error: bad wasm file version: 0x1000000 (expected 0x1) + 0000008: error: unsupported wasm layer: 0x100 out/test/spec/multi-memory/binary.wast:48: assert_malformed passed: 000000a: error: invalid section code: 14 out/test/spec/multi-memory/binary.wast:49: assert_malformed passed: |