Skip to content

Commit 31fd57e

Browse files
committed
wasmtime: misc code cleanup
Signed-off-by: Maximilian Hüter <[email protected]>
1 parent ce8441d commit 31fd57e

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

src/libcrun/handlers/wasmtime.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include <sys/types.h>
3030
#include <fcntl.h>
3131
#include <sched.h>
32-
#include <assert.h>
3332
#include <stdio.h>
3433
#include <stdlib.h>
3534
#include <string.h>
@@ -84,17 +83,20 @@ libwasmtime_exec (void *cookie, libcrun_container_t *container arg_unused,
8483

8584
// Set up wasmtime context
8685
wasm_engine_t *engine = wasm_engine_new ();
87-
assert (engine != NULL);
86+
if (engine == NULL)
87+
error (EXIT_FAILURE, 0, "could not create WebAssembly engine");
8888

8989
wasm_byte_vec_t wasm;
9090
// Load and parse container entrypoint
9191
FILE *file = fopen (pathname, "rbe");
9292
if (! file)
9393
error (EXIT_FAILURE, 0, "error loading entrypoint");
94-
fseek (file, 0L, SEEK_END);
94+
if (fseek (file, 0L, SEEK_END))
95+
error (EXIT_FAILURE, 0, "error fully loading entrypoint");
9596
size_t file_size = ftell (file);
9697
wasm_byte_vec_new_uninitialized (&wasm, file_size);
97-
fseek (file, 0L, SEEK_SET);
98+
if (fseek (file, 0L, SEEK_SET))
99+
error (EXIT_FAILURE, 0, "error resetting entrypoint");
98100
if (fread (wasm.data, file_size, 1, file) != 1)
99101
error (EXIT_FAILURE, 0, "error load");
100102
fclose (file);
@@ -125,22 +127,16 @@ libwasmtime_exec (void *cookie, libcrun_container_t *container arg_unused,
125127
wasmtime_error_delete (err);
126128

127129
if (strcmp ((char *) error_message.data, "component passed to module validation") != 0)
128-
{
129-
error (EXIT_FAILURE, 0, "failed to validate module: %.*s", (int) error_message.size, error_message.data);
130-
}
130+
error (EXIT_FAILURE, 0, "failed to validate module: %.*s", (int) error_message.size, error_message.data);
131131

132132
err = NULL;
133133
is_wasm_module = false;
134134
}
135135

136136
if (is_wasm_module)
137-
{
138-
libwasmtime_run_module (cookie, argv, engine, &wasm);
139-
}
137+
libwasmtime_run_module (cookie, argv, engine, &wasm);
140138
else
141-
{
142-
libwasmtime_run_component (cookie, argv, engine, &wasm);
143-
}
139+
libwasmtime_run_component (cookie, argv, engine, &wasm);
144140

145141
exit (EXIT_SUCCESS);
146142
}
@@ -235,7 +231,8 @@ libwasmtime_run_module (void *cookie, char *const argv[], wasm_engine_t *engine,
235231

236232
// Set up wasmtime context
237233
wasmtime_store_t *store = wasmtime_store_new (engine, NULL, NULL);
238-
assert (store != NULL);
234+
if (store == NULL)
235+
error (EXIT_FAILURE, 0, "could not create WebAssembly store");
239236
wasmtime_context_t *context = wasmtime_store_context (store);
240237

241238
// Link with wasi functions defined
@@ -261,7 +258,8 @@ libwasmtime_run_module (void *cookie, char *const argv[], wasm_engine_t *engine,
261258

262259
// Init WASI program
263260
wasi_config_t *wasi_config = wasi_config_new ("crun_wasi_program");
264-
assert (wasi_config);
261+
if (wasi_config == NULL)
262+
error (EXIT_FAILURE, 0, "could not create WASI configuration");
265263

266264
// Calculate argc for `wasi_config_set_argv`
267265
for (arg = argv; *arg != NULL; ++arg)
@@ -410,7 +408,8 @@ libwasmtime_run_component (void *cookie, char *const argv[], wasm_engine_t *engi
410408

411409
// Set up wasmtime context
412410
wasmtime_store_t *store = wasmtime_store_new (engine, NULL, NULL);
413-
assert (store != NULL);
411+
if (store == NULL)
412+
error (EXIT_FAILURE, 0, "could not create WebAssembly store");
414413
wasmtime_context_t *context = wasmtime_store_context (store);
415414

416415
// Compile wasm component
@@ -426,7 +425,8 @@ libwasmtime_run_component (void *cookie, char *const argv[], wasm_engine_t *engi
426425

427426
// Set up WASIp2 config
428427
wasmtime_wasip2_config_t *wasi_config = wasmtime_wasip2_config_new ();
429-
assert (wasi_config != NULL);
428+
if (wasi_config == NULL)
429+
error (EXIT_FAILURE, 0, "could not create WASIp2 configuration");
430430

431431
wasi_config_inherit_env ((wasi_config_t *) wasi_config);
432432
wasmtime_wasip2_config_inherit_stdin (wasi_config);
@@ -465,22 +465,22 @@ libwasmtime_run_component (void *cookie, char *const argv[], wasm_engine_t *engi
465465
NULL,
466466
wasi_cli_run_interface,
467467
strlen (wasi_cli_run_interface));
468-
assert (run_interface_idx != NULL);
468+
if (run_interface_idx == NULL)
469+
error (EXIT_FAILURE, 0, "failed to fetch export index of %.*s", (int) strlen (wasi_cli_run_interface), wasi_cli_run_interface);
469470
// Get index of the run function
470471
wasmtime_component_export_index_t *run_func_idx = wasmtime_component_instance_get_export_index (
471472
&component_inst,
472473
context,
473474
run_interface_idx,
474475
wasi_cli_run_interface_run,
475476
strlen (wasi_cli_run_interface_run));
476-
assert (run_func_idx != NULL);
477+
if (run_func_idx == NULL)
478+
error (EXIT_FAILURE, 0, "failed to fetch export index of %.*s", (int) strlen (wasi_cli_run_interface_run), wasi_cli_run_interface_run);
477479

478480
// Actually retrieve the func
479481
wasmtime_component_func_t run_func = {};
480482
if (! wasmtime_component_instance_get_func (&component_inst, context, run_func_idx, &run_func))
481-
{
482-
error (EXIT_FAILURE, 0, "failed to retrieve run function");
483-
}
483+
error (EXIT_FAILURE, 0, "failed to retrieve run function");
484484

485485
// Call the func
486486
wasmtime_component_val_t result = {};

0 commit comments

Comments
 (0)