Skip to content

Commit

Permalink
Issue a nicer error message on wasm components. (#2515)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sunfishcode authored Dec 6, 2024
1 parent c6a4f63 commit 4e7d7ef
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 31 deletions.
2 changes: 2 additions & 0 deletions include/wabt/binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 34 additions & 11 deletions src/binary-reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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}));
Expand Down
7 changes: 7 additions & 0 deletions test/binary/unrecognized-layer.txt
Original file line number Diff line number Diff line change
@@ -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 ;;)
7 changes: 7 additions & 0 deletions test/binary/unsupported-component.txt
Original file line number Diff line number Diff line change
@@ -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 ;;)
10 changes: 5 additions & 5 deletions test/spec/binary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions test/spec/exception-handling/binary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions test/spec/memory64/binary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions test/spec/multi-memory/binary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit 4e7d7ef

Please sign in to comment.