Skip to content

Commit c88c048

Browse files
committed
wasmtime: misc improvements
- rm `malloc` from libwasmtime_setup_vm - check entrypoint extension case insensitive - keep `ftell` return value as `long` Signed-off-by: Maximilian Hüter <[email protected]>
1 parent 13377c7 commit c88c048

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

src/libcrun/handlers/handler-utils.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,22 @@
2020

2121
#include <config.h>
2222
#include <string.h>
23+
#include <strings.h>
2324
#include "../container.h"
2425
#include "../utils.h"
2526
#include "handler-utils.h"
2627

28+
int
29+
has_case_suffix (const char *s, const char *suffix)
30+
{
31+
const unsigned long s_len = strlen (s);
32+
const unsigned long suffix_len = strlen (suffix);
33+
if (s_len < suffix_len)
34+
return 0;
35+
36+
return strcasecmp (s + s_len - suffix_len, suffix) == 0 ? 1 : 0;
37+
}
38+
2739
int
2840
wasm_can_handle_container (libcrun_container_t *container, libcrun_error_t *err arg_unused)
2941
{
@@ -46,7 +58,7 @@ wasm_can_handle_container (libcrun_container_t *container, libcrun_error_t *err
4658
*/
4759
if (strcmp (annotation, "wasm-smart") == 0)
4860
{
49-
return ((has_suffix (entrypoint_executable, ".wat") > 0) || (has_suffix (entrypoint_executable, ".wasm") > 0)) ? 1 : 0;
61+
return ((has_case_suffix (entrypoint_executable, ".wat") > 0) || (has_case_suffix (entrypoint_executable, ".wasm") > 0)) ? 1 : 0;
5062
}
5163
return strcmp (annotation, "wasm") == 0 ? 1 : 0;
5264
}
@@ -62,7 +74,7 @@ wasm_can_handle_container (libcrun_container_t *container, libcrun_error_t *err
6274
*/
6375
if (strcmp (annotation, "compat-smart") == 0)
6476
{
65-
return ((has_suffix (entrypoint_executable, ".wat") > 0) || (has_suffix (entrypoint_executable, ".wasm") > 0)) ? 1 : 0;
77+
return ((has_case_suffix (entrypoint_executable, ".wat") > 0) || (has_case_suffix (entrypoint_executable, ".wasm") > 0)) ? 1 : 0;
6678
}
6779

6880
return strcmp (annotation, "compat") == 0 ? 1 : 0;

src/libcrun/handlers/handler-utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ typedef enum
2828
WASM_ENC_COMPONENT
2929
} wasm_encoding_t;
3030

31+
int has_case_suffix (const char *s, const char *suffix);
32+
3133
int wasm_can_handle_container (libcrun_container_t *container, libcrun_error_t *err);
3234

3335
wasm_encoding_t wasm_interpret_header (const char *header, const size_t len);

src/libcrun/handlers/wasmtime.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ libwasmtime_setup_vm (void *cookie, char *const argv[], struct libwasmtime_vm *v
7575
wasm_byte_vec_t error_message;
7676

7777
if (vm == NULL)
78-
vm = malloc (sizeof (struct libwasmtime_vm));
78+
error (EXIT_FAILURE, 0, "internal error: cannot setup a NULL vm");
7979

8080
// Load needed functions
8181
WASMTIME_COMMON_SYMBOLS (cookie)
@@ -162,7 +162,7 @@ libwasmtime_setup_vm (void *cookie, char *const argv[], struct libwasmtime_vm *v
162162
}
163163

164164
static void
165-
libwasmtime_free_vm (void *cookie, struct libwasmtime_vm *vm)
165+
libwasmtime_delete_vm (void *cookie, struct libwasmtime_vm *vm)
166166
{
167167
void (*wasmtime_store_delete) (wasmtime_store_t *store)
168168
= libwasmtime_load_symbol (cookie, "wasmtime_store_delete");
@@ -200,22 +200,22 @@ libwasmtime_exec (void *cookie, libcrun_container_t *container arg_unused,
200200
error (EXIT_FAILURE, 0, "error loading entrypoint");
201201
if (fseek (file, 0L, SEEK_END))
202202
error (EXIT_FAILURE, 0, "error fully loading entrypoint");
203-
size_t file_size = ftell (file);
204-
if (file_size == (size_t) -1L)
203+
long file_size = ftell (file);
204+
if (file_size == -1L)
205205
error (EXIT_FAILURE, 0, "error getting entrypoint size");
206-
wasm_byte_vec_new_uninitialized (&wasm, file_size);
206+
wasm_byte_vec_new_uninitialized (&wasm, (size_t) file_size);
207207
if (fseek (file, 0L, SEEK_SET))
208208
error (EXIT_FAILURE, 0, "error resetting entrypoint");
209-
if (fread (wasm.data, file_size, 1, file) != 1)
209+
if (fread (wasm.data, (size_t) file_size, 1, file) != 1)
210210
error (EXIT_FAILURE, 0, "error reading entrypoint");
211211
fclose (file);
212212

213213
// If entrypoint contains a webassembly text format
214214
// compile it on the fly and convert to equivalent
215215
// binary format.
216-
if (has_suffix (pathname, ".wat") > 0)
216+
if (has_case_suffix (pathname, ".wat") > 0)
217217
{
218-
wasmtime_error_t *err = wasmtime_wat2wasm ((char *) wasm.data, file_size, &wasm_bytes);
218+
wasmtime_error_t *err = wasmtime_wat2wasm ((char *) wasm.data, (size_t) file_size, &wasm_bytes);
219219
if (err != NULL)
220220
{
221221
wasmtime_error_message (err, &error_message);
@@ -240,7 +240,7 @@ libwasmtime_exec (void *cookie, libcrun_container_t *container arg_unused,
240240
else
241241
error (EXIT_FAILURE, 0, "unsupported wasm encoding detected");
242242

243-
libwasmtime_free_vm (cookie, &vm);
243+
libwasmtime_delete_vm (cookie, &vm);
244244
exit (status);
245245
}
246246

0 commit comments

Comments
 (0)