Skip to content

Commit b86db3c

Browse files
committed
wasmtime: use symbol loading helper everywhere
Signed-off-by: Maximilian Hüter <[email protected]>
1 parent fbc799c commit b86db3c

File tree

1 file changed

+74
-106
lines changed

1 file changed

+74
-106
lines changed

src/libcrun/handlers/wasmtime.c

Lines changed: 74 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
#endif
4545

4646
#if HAVE_DLOPEN && HAVE_WASMTIME
47+
static void *
48+
libwasmtime_load_symbol (void *cookie, char *const symbol);
49+
4750
static void
4851
libwasmtime_run_module (void *cookie, char *const argv[], wasm_engine_t *engine, wasm_byte_vec_t *wasm);
4952

@@ -56,30 +59,19 @@ libwasmtime_exec (void *cookie, libcrun_container_t *container arg_unused,
5659
{
5760
wasm_byte_vec_t error_message;
5861
wasm_byte_vec_t wasm_bytes;
59-
wasm_engine_t *(*wasm_engine_new) ();
60-
wasmtime_error_t *(*wasmtime_wat2wasm) (const char *wat, size_t wat_len, wasm_byte_vec_t *out);
61-
void (*wasm_byte_vec_new_uninitialized) (wasm_byte_vec_t *, size_t);
62-
void (*wasm_byte_vec_delete) (wasm_byte_vec_t *);
63-
wasmtime_error_t *(*wasmtime_module_validate) (wasm_engine_t *engine, const uint8_t *wasm, size_t wasm_len);
64-
void (*wasmtime_error_message) (const wasmtime_error_t *error, wasm_name_t *message);
65-
void (*wasmtime_error_delete) (wasmtime_error_t *error);
66-
67-
wasmtime_wat2wasm = dlsym (cookie, "wasmtime_wat2wasm");
68-
wasm_engine_new = dlsym (cookie, "wasm_engine_new");
69-
wasm_byte_vec_new_uninitialized = dlsym (cookie, "wasm_byte_vec_new_uninitialized");
70-
wasm_byte_vec_delete = dlsym (cookie, "wasm_byte_vec_delete");
71-
wasmtime_module_validate = dlsym (cookie, "wasmtime_module_validate");
72-
wasmtime_error_delete = dlsym (cookie, "wasmtime_error_delete");
73-
wasmtime_error_message = dlsym (cookie, "wasmtime_error_message");
74-
75-
if (wasmtime_wat2wasm == NULL
76-
|| wasm_engine_new == NULL
77-
|| wasm_byte_vec_new_uninitialized == NULL
78-
|| wasm_byte_vec_delete == NULL
79-
|| wasmtime_module_validate == NULL
80-
|| wasmtime_error_delete == NULL
81-
|| wasmtime_error_message == NULL)
82-
error (EXIT_FAILURE, 0, "could not find symbol in `libwasmtime.so`");
62+
63+
wasm_engine_t *(*wasm_engine_new) ()
64+
= libwasmtime_load_symbol (cookie, "wasm_engine_new");
65+
wasmtime_error_t *(*wasmtime_wat2wasm) (const char *wat, size_t wat_len, wasm_byte_vec_t *out)
66+
= libwasmtime_load_symbol (cookie, "wasmtime_wat2wasm");
67+
void (*wasm_byte_vec_new_uninitialized) (wasm_byte_vec_t *, size_t)
68+
= libwasmtime_load_symbol (cookie, "wasm_byte_vec_new_uninitialized");
69+
void (*wasm_byte_vec_delete) (wasm_byte_vec_t *)
70+
= libwasmtime_load_symbol (cookie, "wasm_byte_vec_delete");
71+
void (*wasmtime_error_message) (const wasmtime_error_t *error, wasm_name_t *message)
72+
= libwasmtime_load_symbol (cookie, "wasmtime_error_message");
73+
void (*wasmtime_error_delete) (wasmtime_error_t *error)
74+
= libwasmtime_load_symbol (cookie, "wasmtime_error_delete");
8375

8476
// Set up wasmtime context
8577
wasm_engine_t *engine = wasm_engine_new ();
@@ -175,65 +167,53 @@ libwasmtime_run_module (void *cookie, char *const argv[], wasm_engine_t *engine,
175167

176168
// Load needed functions
177169
WASMTIME_COMMON_SYMBOLS (cookie)
178-
wasi_config_t *(*wasi_config_new) (const char *);
179-
wasmtime_linker_t *(*wasmtime_linker_new) (wasm_engine_t *engine);
180-
wasmtime_error_t *(*wasmtime_linker_define_wasi) (wasmtime_linker_t *linker);
170+
wasi_config_t *(*wasi_config_new) (const char *)
171+
= libwasmtime_load_symbol (cookie, "wasi_config_new");
172+
wasmtime_linker_t *(*wasmtime_linker_new) (wasm_engine_t *engine)
173+
= libwasmtime_load_symbol (cookie, "wasmtime_linker_new");
174+
wasmtime_error_t *(*wasmtime_linker_define_wasi) (wasmtime_linker_t *linker)
175+
= libwasmtime_load_symbol (cookie, "wasmtime_linker_define_wasi");
181176
wasmtime_error_t *(*wasmtime_module_new) (
182177
wasm_engine_t *engine,
183178
const uint8_t *wasm,
184179
size_t wasm_len,
185-
wasmtime_module_t **ret);
186-
void (*wasi_config_set_argv) (wasi_config_t *config, int argc, const char *argv[]);
187-
void (*wasi_config_inherit_stdin) (wasi_config_t *config);
188-
void (*wasi_config_inherit_stdout) (wasi_config_t *config);
189-
void (*wasi_config_inherit_stderr) (wasi_config_t *config);
190-
wasmtime_error_t *(*wasmtime_context_set_wasi) (wasmtime_context_t *context, wasi_config_t *wasi);
180+
wasmtime_module_t **ret)
181+
= libwasmtime_load_symbol (cookie, "wasmtime_module_new");
182+
void (*wasi_config_set_argv) (wasi_config_t *config, int argc, const char *argv[])
183+
= libwasmtime_load_symbol (cookie, "wasi_config_set_argv");
184+
void (*wasi_config_inherit_stdin) (wasi_config_t *config)
185+
= libwasmtime_load_symbol (cookie, "wasi_config_inherit_stdin");
186+
void (*wasi_config_inherit_stdout) (wasi_config_t *config)
187+
= libwasmtime_load_symbol (cookie, "wasi_config_inherit_stdout");
188+
void (*wasi_config_inherit_stderr) (wasi_config_t *config)
189+
= libwasmtime_load_symbol (cookie, "wasi_config_inherit_stderr");
190+
wasmtime_error_t *(*wasmtime_context_set_wasi) (wasmtime_context_t *context, wasi_config_t *wasi)
191+
= libwasmtime_load_symbol (cookie, "wasmtime_context_set_wasi");
191192
wasmtime_error_t *(*wasmtime_linker_module) (
192193
wasmtime_linker_t *linker,
193194
wasmtime_context_t *store,
194195
const char *name,
195196
size_t name_len,
196-
const wasmtime_module_t *module);
197+
const wasmtime_module_t *module)
198+
= libwasmtime_load_symbol (cookie, "wasmtime_linker_module");
197199
wasmtime_error_t *(*wasmtime_linker_get_default) (
198200
const wasmtime_linker_t *linker,
199201
wasmtime_context_t *store,
200202
const char *name,
201203
size_t name_len,
202-
wasmtime_func_t *func);
204+
wasmtime_func_t *func)
205+
= libwasmtime_load_symbol (cookie, "wasmtime_linker_get_default");
203206
wasmtime_error_t *(*wasmtime_func_call) (
204207
wasmtime_context_t *store,
205208
const wasmtime_func_t *func,
206209
const wasmtime_val_t *args,
207210
size_t nargs,
208211
wasmtime_val_t *results,
209212
size_t nresults,
210-
wasm_trap_t **trap);
211-
void (*wasmtime_module_delete) (wasmtime_module_t *m);
212-
213-
wasi_config_new = dlsym (cookie, "wasi_config_new");
214-
wasi_config_set_argv = dlsym (cookie, "wasi_config_set_argv");
215-
wasmtime_linker_new = dlsym (cookie, "wasmtime_linker_new");
216-
wasmtime_linker_define_wasi = dlsym (cookie, "wasmtime_linker_define_wasi");
217-
wasmtime_module_new = dlsym (cookie, "wasmtime_module_new");
218-
wasi_config_inherit_stdout = dlsym (cookie, "wasi_config_inherit_stdout");
219-
wasi_config_inherit_stdin = dlsym (cookie, "wasi_config_inherit_stdin");
220-
wasi_config_inherit_stderr = dlsym (cookie, "wasi_config_inherit_stderr");
221-
wasmtime_context_set_wasi = dlsym (cookie, "wasmtime_context_set_wasi");
222-
wasmtime_linker_module = dlsym (cookie, "wasmtime_linker_module");
223-
wasmtime_linker_get_default = dlsym (cookie, "wasmtime_linker_get_default");
224-
wasmtime_func_call = dlsym (cookie, "wasmtime_func_call");
225-
wasmtime_module_delete = dlsym (cookie, "wasmtime_module_delete");
226-
227-
if (wasm_engine_delete == NULL || wasm_byte_vec_delete == NULL
228-
|| wasi_config_new == NULL || wasmtime_store_new == NULL
229-
|| wasmtime_store_context == NULL || wasmtime_linker_new == NULL || wasmtime_linker_define_wasi == NULL
230-
|| wasmtime_module_new == NULL || wasi_config_inherit_stdout == NULL
231-
|| wasi_config_inherit_stdin == NULL || wasi_config_inherit_stderr == NULL
232-
|| wasi_config_inherit_env == NULL || wasmtime_context_set_wasi == NULL
233-
|| wasmtime_linker_module == NULL || wasmtime_linker_get_default == NULL || wasmtime_func_call == NULL
234-
|| wasmtime_module_delete == NULL || wasmtime_store_delete == NULL || wasi_config_set_argv == NULL
235-
|| wasmtime_error_delete == NULL || wasmtime_error_message == NULL || wasi_config_preopen_dir == NULL)
236-
error (EXIT_FAILURE, 0, "could not find symbol in `libwasmtime.so`");
213+
wasm_trap_t **trap)
214+
= libwasmtime_load_symbol (cookie, "wasmtime_func_call");
215+
void (*wasmtime_module_delete) (wasmtime_module_t *m)
216+
= libwasmtime_load_symbol (cookie, "wasmtime_module_delete");
237217

238218
// Set up wasmtime context
239219
wasmtime_store_t *store = wasmtime_store_new (engine, NULL, NULL);
@@ -340,69 +320,57 @@ libwasmtime_run_component (void *cookie, char *const argv[], wasm_engine_t *engi
340320
const wasm_engine_t *engine,
341321
const uint8_t *buf,
342322
size_t len,
343-
wasmtime_component_t **component_out);
344-
wasmtime_wasip2_config_t *(*wasmtime_wasip2_config_new) (void);
345-
void (*wasmtime_wasip2_config_inherit_stdin) (wasmtime_wasip2_config_t *config);
346-
void (*wasmtime_wasip2_config_inherit_stdout) (wasmtime_wasip2_config_t *config);
347-
void (*wasmtime_wasip2_config_inherit_stderr) (wasmtime_wasip2_config_t *config);
348-
void (*wasmtime_wasip2_config_arg) (wasmtime_wasip2_config_t *config, const char *arg, size_t arg_len);
349-
void (*wasmtime_context_set_wasip2) (wasmtime_context_t *context, wasmtime_wasip2_config_t *config);
350-
wasmtime_component_linker_t *(*wasmtime_component_linker_new) (wasm_engine_t *engine);
351-
wasmtime_error_t *(*wasmtime_component_linker_add_wasip2) (wasmtime_component_linker_t *linker);
323+
wasmtime_component_t **component_out)
324+
= libwasmtime_load_symbol (cookie, "wasmtime_component_new");
325+
wasmtime_wasip2_config_t *(*wasmtime_wasip2_config_new) (void)
326+
= libwasmtime_load_symbol (cookie, "wasmtime_wasip2_config_new");
327+
void (*wasmtime_wasip2_config_inherit_stdin) (wasmtime_wasip2_config_t *config)
328+
= libwasmtime_load_symbol (cookie, "wasmtime_wasip2_config_inherit_stdin");
329+
void (*wasmtime_wasip2_config_inherit_stdout) (wasmtime_wasip2_config_t *config)
330+
= libwasmtime_load_symbol (cookie, "wasmtime_wasip2_config_inherit_stdout");
331+
void (*wasmtime_wasip2_config_inherit_stderr) (wasmtime_wasip2_config_t *config)
332+
= libwasmtime_load_symbol (cookie, "wasmtime_wasip2_config_inherit_stderr");
333+
void (*wasmtime_wasip2_config_arg) (wasmtime_wasip2_config_t *config, const char *arg, size_t arg_len)
334+
= libwasmtime_load_symbol (cookie, "wasmtime_wasip2_config_arg");
335+
void (*wasmtime_context_set_wasip2) (wasmtime_context_t *context, wasmtime_wasip2_config_t *config)
336+
= libwasmtime_load_symbol (cookie, "wasmtime_context_set_wasip2");
337+
wasmtime_component_linker_t *(*wasmtime_component_linker_new) (wasm_engine_t *engine)
338+
= libwasmtime_load_symbol (cookie, "wasmtime_component_linker_new");
339+
wasmtime_error_t *(*wasmtime_component_linker_add_wasip2) (wasmtime_component_linker_t *linker)
340+
= libwasmtime_load_symbol (cookie, "wasmtime_component_linker_add_wasip2");
352341
wasmtime_error_t *(*wasmtime_component_linker_instantiate) (
353342
const wasmtime_component_linker_t *linker,
354343
wasmtime_context_t *context,
355344
const wasmtime_component_t *component,
356-
wasmtime_component_instance_t *instance_out);
345+
wasmtime_component_instance_t *instance_out)
346+
= libwasmtime_load_symbol (cookie, "wasmtime_component_linker_instantiate");
357347
wasmtime_component_export_index_t *(*wasmtime_component_instance_get_export_index) (
358348
const wasmtime_component_instance_t *instance,
359349
wasmtime_context_t *context,
360350
const wasmtime_component_export_index_t *instance_export_index,
361351
const char *name,
362-
size_t name_len);
352+
size_t name_len)
353+
= libwasmtime_load_symbol (cookie, "wasmtime_component_instance_get_export_index");
363354
bool (*wasmtime_component_instance_get_func) (
364355
const wasmtime_component_instance_t *instance,
365356
wasmtime_context_t *context,
366357
const wasmtime_component_export_index_t *export_index,
367-
wasmtime_component_func_t *func_out);
358+
wasmtime_component_func_t *func_out)
359+
= libwasmtime_load_symbol (cookie, "wasmtime_component_instance_get_func");
368360
wasmtime_error_t *(*wasmtime_component_func_call) (
369361
const wasmtime_component_func_t *func,
370362
wasmtime_context_t *context,
371363
const wasmtime_component_val_t *args,
372364
size_t args_size,
373365
wasmtime_component_val_t *results,
374-
size_t results_size);
375-
void (*wasmtime_component_export_index_delete) (wasmtime_component_export_index_t *export_index);
376-
void (*wasmtime_component_delete) (wasmtime_component_t *c);
377-
void (*wasmtime_component_linker_delete) (wasmtime_component_linker_t *linker);
378-
379-
wasmtime_component_new = dlsym (cookie, "wasmtime_component_new");
380-
wasmtime_wasip2_config_new = dlsym (cookie, "wasmtime_wasip2_config_new");
381-
wasmtime_wasip2_config_inherit_stdin = dlsym (cookie, "wasmtime_wasip2_config_inherit_stdin");
382-
wasmtime_wasip2_config_inherit_stdout = dlsym (cookie, "wasmtime_wasip2_config_inherit_stdout");
383-
wasmtime_wasip2_config_inherit_stderr = dlsym (cookie, "wasmtime_wasip2_config_inherit_stderr");
384-
wasmtime_wasip2_config_arg = dlsym (cookie, "wasmtime_wasip2_config_arg");
385-
wasmtime_context_set_wasip2 = dlsym (cookie, "wasmtime_context_set_wasip2");
386-
wasmtime_component_linker_new = dlsym (cookie, "wasmtime_component_linker_new");
387-
wasmtime_component_linker_add_wasip2 = dlsym (cookie, "wasmtime_component_linker_add_wasip2");
388-
wasmtime_component_linker_instantiate = dlsym (cookie, "wasmtime_component_linker_instantiate");
389-
wasmtime_component_instance_get_export_index = dlsym (cookie, "wasmtime_component_instance_get_export_index");
390-
wasmtime_component_instance_get_func = dlsym (cookie, "wasmtime_component_instance_get_func");
391-
wasmtime_component_func_call = dlsym (cookie, "wasmtime_component_func_call");
392-
wasmtime_component_export_index_delete = dlsym (cookie, "wasmtime_component_export_index_delete");
393-
wasmtime_component_delete = dlsym (cookie, "wasmtime_component_delete");
394-
wasmtime_component_linker_delete = dlsym (cookie, "wasmtime_component_linker_delete");
395-
396-
if (wasm_engine_delete == NULL || wasm_byte_vec_delete == NULL || wasmtime_store_new == NULL
397-
|| wasmtime_store_delete == NULL || wasmtime_store_context == NULL || wasmtime_component_new == NULL
398-
|| wasmtime_wasip2_config_new == NULL || wasi_config_inherit_env == NULL || wasmtime_wasip2_config_inherit_stdin == NULL
399-
|| wasmtime_wasip2_config_inherit_stdout == NULL || wasmtime_wasip2_config_inherit_stderr == NULL || wasmtime_wasip2_config_arg == NULL
400-
|| wasmtime_context_set_wasip2 == NULL || wasmtime_component_linker_new == NULL || wasmtime_component_linker_add_wasip2 == NULL
401-
|| wasmtime_component_linker_instantiate == NULL || wasmtime_component_instance_get_export_index == NULL
402-
|| wasmtime_component_instance_get_func == NULL || wasmtime_component_func_call == NULL || wasmtime_component_export_index_delete == NULL
403-
|| wasmtime_component_delete == NULL || wasmtime_component_linker_delete == NULL || wasmtime_error_message == NULL
404-
|| wasmtime_error_delete == NULL || wasi_config_preopen_dir == NULL)
405-
error (EXIT_FAILURE, 0, "could not find symbol in `libwasmtime.so`");
366+
size_t results_size)
367+
= libwasmtime_load_symbol (cookie, "wasmtime_component_func_call");
368+
void (*wasmtime_component_export_index_delete) (wasmtime_component_export_index_t *export_index)
369+
= libwasmtime_load_symbol (cookie, "wasmtime_component_export_index_delete");
370+
void (*wasmtime_component_delete) (wasmtime_component_t *c)
371+
= libwasmtime_load_symbol (cookie, "wasmtime_component_delete");
372+
void (*wasmtime_component_linker_delete) (wasmtime_component_linker_t *linker)
373+
= libwasmtime_load_symbol (cookie, "wasmtime_component_linker_delete");
406374

407375
// Set up wasmtime context
408376
wasmtime_store_t *store = wasmtime_store_new (engine, NULL, NULL);

0 commit comments

Comments
 (0)