Skip to content

Commit 4e7d7ef

Browse files
authored
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
1 parent c6a4f63 commit 4e7d7ef

File tree

8 files changed

+70
-31
lines changed

8 files changed

+70
-31
lines changed

include/wabt/binary.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
#define WABT_BINARY_MAGIC 0x6d736100
2323
#define WABT_BINARY_VERSION 1
24+
#define WABT_BINARY_LAYER_MODULE 0
25+
#define WABT_BINARY_LAYER_COMPONENT 1
2426
#define WABT_BINARY_LIMITS_HAS_MAX_FLAG 0x1
2527
#define WABT_BINARY_LIMITS_IS_SHARED_FLAG 0x2
2628
#define WABT_BINARY_LIMITS_IS_64_FLAG 0x4

src/binary-reader.cc

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,17 @@
3737
#include <alloca.h>
3838
#endif
3939

40-
#define ERROR_IF(expr, ...) \
41-
do { \
42-
if (expr) { \
43-
PrintError(__VA_ARGS__); \
44-
return Result::Error; \
45-
} \
40+
#define ERROR(...) \
41+
do { \
42+
PrintError(__VA_ARGS__); \
43+
return Result::Error; \
44+
} while (0)
45+
46+
#define ERROR_IF(expr, ...) \
47+
do { \
48+
if (expr) { \
49+
ERROR(__VA_ARGS__); \
50+
} \
4651
} while (0)
4752

4853
#define ERROR_UNLESS(expr, ...) ERROR_IF(!(expr), __VA_ARGS__)
@@ -100,6 +105,7 @@ class BinaryReader {
100105
const char* type_name,
101106
const char* desc);
102107
[[nodiscard]] Result ReadU8(uint8_t* out_value, const char* desc);
108+
[[nodiscard]] Result ReadU16(uint16_t* out_value, const char* desc);
103109
[[nodiscard]] Result ReadU32(uint32_t* out_value, const char* desc);
104110
[[nodiscard]] Result ReadF32(uint32_t* out_value, const char* desc);
105111
[[nodiscard]] Result ReadF64(uint64_t* out_value, const char* desc);
@@ -300,6 +306,10 @@ Result BinaryReader::ReadU8(uint8_t* out_value, const char* desc) {
300306
return ReadT(out_value, "uint8_t", desc);
301307
}
302308

309+
Result BinaryReader::ReadU16(uint16_t* out_value, const char* desc) {
310+
return ReadT(out_value, "uint16_t", desc);
311+
}
312+
303313
Result BinaryReader::ReadU32(uint32_t* out_value, const char* desc) {
304314
return ReadT(out_value, "uint32_t", desc);
305315
}
@@ -3086,11 +3096,24 @@ Result BinaryReader::ReadModule(const ReadModuleOptions& options) {
30863096
uint32_t magic = 0;
30873097
CHECK_RESULT(ReadU32(&magic, "magic"));
30883098
ERROR_UNLESS(magic == WABT_BINARY_MAGIC, "bad magic value");
3089-
uint32_t version = 0;
3090-
CHECK_RESULT(ReadU32(&version, "version"));
3091-
ERROR_UNLESS(version == WABT_BINARY_VERSION,
3092-
"bad wasm file version: %#x (expected %#x)", version,
3093-
WABT_BINARY_VERSION);
3099+
3100+
uint16_t version = 0, layer = 0;
3101+
CHECK_RESULT(ReadU16(&version, "version"));
3102+
CHECK_RESULT(ReadU16(&layer, "layer"));
3103+
3104+
switch (layer) {
3105+
case WABT_BINARY_LAYER_MODULE:
3106+
ERROR_UNLESS(version == WABT_BINARY_VERSION,
3107+
"bad wasm file version: %#x (expected %#x)", version,
3108+
WABT_BINARY_VERSION);
3109+
break;
3110+
case WABT_BINARY_LAYER_COMPONENT:
3111+
ERROR("wasm components are not yet supported in this tool");
3112+
break;
3113+
default:
3114+
ERROR("unsupported wasm layer: %#x", layer);
3115+
break;
3116+
}
30943117

30953118
CALLBACK(BeginModule, version);
30963119
CHECK_RESULT(ReadSections(ReadSectionsOptions{options.stop_on_first_error}));

test/binary/unrecognized-layer.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
;;; TOOL: run-gen-wasm-bad
2+
magic
3+
0xe 0 0x2 0
4+
(;; STDERR ;;;
5+
0000008: error: unsupported wasm layer: 0x2
6+
0000008: error: unsupported wasm layer: 0x2
7+
;;; STDERR ;;)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
;;; TOOL: run-gen-wasm-bad
2+
magic
3+
0xd 0 0x1 0
4+
(;; STDERR ;;;
5+
0000008: error: wasm components are not yet supported in this tool
6+
0000008: error: wasm components are not yet supported in this tool
7+
;;; STDERR ;;)

test/spec/binary.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ out/test/spec/binary.wast:31: assert_malformed passed:
4040
out/test/spec/binary.wast:34: assert_malformed passed:
4141
0000004: error: bad magic value
4242
out/test/spec/binary.wast:37: assert_malformed passed:
43-
0000004: error: unable to read uint32_t: version
43+
0000004: error: unable to read uint16_t: version
4444
out/test/spec/binary.wast:38: assert_malformed passed:
45-
0000004: error: unable to read uint32_t: version
45+
0000004: error: unable to read uint16_t: version
4646
out/test/spec/binary.wast:39: assert_malformed passed:
47-
0000004: error: unable to read uint32_t: version
47+
0000006: error: unable to read uint16_t: layer
4848
out/test/spec/binary.wast:40: assert_malformed passed:
4949
0000008: error: bad wasm file version: 0 (expected 0x1)
5050
out/test/spec/binary.wast:41: assert_malformed passed:
@@ -54,9 +54,9 @@ out/test/spec/binary.wast:42: assert_malformed passed:
5454
out/test/spec/binary.wast:43: assert_malformed passed:
5555
0000008: error: bad wasm file version: 0x100 (expected 0x1)
5656
out/test/spec/binary.wast:44: assert_malformed passed:
57-
0000008: error: bad wasm file version: 0x10000 (expected 0x1)
57+
0000008: error: wasm components are not yet supported in this tool
5858
out/test/spec/binary.wast:45: assert_malformed passed:
59-
0000008: error: bad wasm file version: 0x1000000 (expected 0x1)
59+
0000008: error: unsupported wasm layer: 0x100
6060
out/test/spec/binary.wast:48: assert_malformed passed:
6161
000000a: error: invalid section code: 14
6262
out/test/spec/binary.wast:49: assert_malformed passed:

test/spec/exception-handling/binary.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ out/test/spec/exception-handling/binary.wast:31: assert_malformed passed:
4141
out/test/spec/exception-handling/binary.wast:34: assert_malformed passed:
4242
0000004: error: bad magic value
4343
out/test/spec/exception-handling/binary.wast:37: assert_malformed passed:
44-
0000004: error: unable to read uint32_t: version
44+
0000004: error: unable to read uint16_t: version
4545
out/test/spec/exception-handling/binary.wast:38: assert_malformed passed:
46-
0000004: error: unable to read uint32_t: version
46+
0000004: error: unable to read uint16_t: version
4747
out/test/spec/exception-handling/binary.wast:39: assert_malformed passed:
48-
0000004: error: unable to read uint32_t: version
48+
0000006: error: unable to read uint16_t: layer
4949
out/test/spec/exception-handling/binary.wast:40: assert_malformed passed:
5050
0000008: error: bad wasm file version: 0 (expected 0x1)
5151
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:
5555
out/test/spec/exception-handling/binary.wast:43: assert_malformed passed:
5656
0000008: error: bad wasm file version: 0x100 (expected 0x1)
5757
out/test/spec/exception-handling/binary.wast:44: assert_malformed passed:
58-
0000008: error: bad wasm file version: 0x10000 (expected 0x1)
58+
0000008: error: wasm components are not yet supported in this tool
5959
out/test/spec/exception-handling/binary.wast:45: assert_malformed passed:
60-
0000008: error: bad wasm file version: 0x1000000 (expected 0x1)
60+
0000008: error: unsupported wasm layer: 0x100
6161
out/test/spec/exception-handling/binary.wast:48: assert_malformed passed:
6262
000000a: error: invalid section code: 14
6363
out/test/spec/exception-handling/binary.wast:49: assert_malformed passed:

test/spec/memory64/binary.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ out/test/spec/memory64/binary.wast:31: assert_malformed passed:
4141
out/test/spec/memory64/binary.wast:34: assert_malformed passed:
4242
0000004: error: bad magic value
4343
out/test/spec/memory64/binary.wast:37: assert_malformed passed:
44-
0000004: error: unable to read uint32_t: version
44+
0000004: error: unable to read uint16_t: version
4545
out/test/spec/memory64/binary.wast:38: assert_malformed passed:
46-
0000004: error: unable to read uint32_t: version
46+
0000004: error: unable to read uint16_t: version
4747
out/test/spec/memory64/binary.wast:39: assert_malformed passed:
48-
0000004: error: unable to read uint32_t: version
48+
0000006: error: unable to read uint16_t: layer
4949
out/test/spec/memory64/binary.wast:40: assert_malformed passed:
5050
0000008: error: bad wasm file version: 0 (expected 0x1)
5151
out/test/spec/memory64/binary.wast:41: assert_malformed passed:
@@ -55,9 +55,9 @@ out/test/spec/memory64/binary.wast:42: assert_malformed passed:
5555
out/test/spec/memory64/binary.wast:43: assert_malformed passed:
5656
0000008: error: bad wasm file version: 0x100 (expected 0x1)
5757
out/test/spec/memory64/binary.wast:44: assert_malformed passed:
58-
0000008: error: bad wasm file version: 0x10000 (expected 0x1)
58+
0000008: error: wasm components are not yet supported in this tool
5959
out/test/spec/memory64/binary.wast:45: assert_malformed passed:
60-
0000008: error: bad wasm file version: 0x1000000 (expected 0x1)
60+
0000008: error: unsupported wasm layer: 0x100
6161
out/test/spec/memory64/binary.wast:48: assert_malformed passed:
6262
000000a: error: invalid section code: 14
6363
out/test/spec/memory64/binary.wast:49: assert_malformed passed:

test/spec/multi-memory/binary.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ out/test/spec/multi-memory/binary.wast:31: assert_malformed passed:
4141
out/test/spec/multi-memory/binary.wast:34: assert_malformed passed:
4242
0000004: error: bad magic value
4343
out/test/spec/multi-memory/binary.wast:37: assert_malformed passed:
44-
0000004: error: unable to read uint32_t: version
44+
0000004: error: unable to read uint16_t: version
4545
out/test/spec/multi-memory/binary.wast:38: assert_malformed passed:
46-
0000004: error: unable to read uint32_t: version
46+
0000004: error: unable to read uint16_t: version
4747
out/test/spec/multi-memory/binary.wast:39: assert_malformed passed:
48-
0000004: error: unable to read uint32_t: version
48+
0000006: error: unable to read uint16_t: layer
4949
out/test/spec/multi-memory/binary.wast:40: assert_malformed passed:
5050
0000008: error: bad wasm file version: 0 (expected 0x1)
5151
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:
5555
out/test/spec/multi-memory/binary.wast:43: assert_malformed passed:
5656
0000008: error: bad wasm file version: 0x100 (expected 0x1)
5757
out/test/spec/multi-memory/binary.wast:44: assert_malformed passed:
58-
0000008: error: bad wasm file version: 0x10000 (expected 0x1)
58+
0000008: error: wasm components are not yet supported in this tool
5959
out/test/spec/multi-memory/binary.wast:45: assert_malformed passed:
60-
0000008: error: bad wasm file version: 0x1000000 (expected 0x1)
60+
0000008: error: unsupported wasm layer: 0x100
6161
out/test/spec/multi-memory/binary.wast:48: assert_malformed passed:
6262
000000a: error: invalid section code: 14
6363
out/test/spec/multi-memory/binary.wast:49: assert_malformed passed:

0 commit comments

Comments
 (0)