Skip to content

Commit d6b9778

Browse files
committed
wasmtime: branch execution path after wasm header interpretation
Signed-off-by: Maximilian Hüter <[email protected]>
1 parent 877ca9a commit d6b9778

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

src/libcrun/handlers/handler-utils.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,20 @@ wasm_encoding_t
7575
wasm_interpete_header (const char *header)
7676
{
7777
// Check for the WebAssembly magic bytes
78+
// See: https://webassembly.github.io/spec/core/binary/modules.html#binary-module
7879
if (strncmp (header, "\0asm", 4))
7980
return WASM_ENC_INVALID;
8081

81-
// We don't care for the specific WebAssembly version
82-
// so we only read the value of the `layer` field.
82+
/* The next four bytes are the WebAssembly version.
83+
We don't care for the specific WebAssembly version
84+
so we only read the value of the `layer` field which
85+
was defined by the component spec.
86+
See: https://github.com/WebAssembly/component-model/blob/main/design/mvp/Binary.md#component-definitions
87+
*/
8388
if (header[6] == '\0' && header[7] == '\0')
8489
return WASM_ENC_MODULE;
8590

86-
// `layer` does not equal 0x00 0x00 so we are working
91+
// `layer` does not equal `0x00 0x00` so we are working
8792
// with a component.
8893
return WASM_ENC_COMPONENT;
8994
}

src/libcrun/handlers/wasmtime.c

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -117,26 +117,16 @@ libwasmtime_exec (void *cookie, libcrun_container_t *container arg_unused,
117117
wasm = wasm_bytes;
118118
}
119119

120-
// Check if it is a valid webassembly module or
121-
// a component.
122-
bool is_wasm_module = true;
123-
wasmtime_error_t *err = wasmtime_module_validate (engine, (uint8_t *) wasm.data, wasm.size);
124-
if (err != NULL)
125-
{
126-
wasmtime_error_message (err, &error_message);
127-
wasmtime_error_delete (err);
128-
129-
if (strcmp ((char *) error_message.data, "component passed to module validation") != 0)
130-
error (EXIT_FAILURE, 0, "failed to validate module: %.*s", (int) error_message.size, error_message.data);
120+
wasm_encoding_t wasm_enc = wasm_interpete_header (wasm.data);
121+
if (wasm_enc == WASM_ENC_INVALID)
122+
error (EXIT_FAILURE, 0, "invalid wasm binary header");
131123

132-
err = NULL;
133-
is_wasm_module = false;
134-
}
135-
136-
if (is_wasm_module)
124+
if (wasm_enc == WASM_ENC_MODULE)
137125
libwasmtime_run_module (cookie, argv, engine, &wasm);
138-
else
126+
else if (wasm_enc == WASM_ENC_COMPONENT)
139127
libwasmtime_run_component (cookie, argv, engine, &wasm);
128+
else
129+
error (EXIT_FAILURE, 0, "unsupport wasm encoding detected");
140130

141131
exit (EXIT_SUCCESS);
142132
}

0 commit comments

Comments
 (0)