Skip to content

Commit

Permalink
clang-format
Browse files Browse the repository at this point in the history
Signed-off-by: Maciej <[email protected]>
  • Loading branch information
Maciej committed Jul 23, 2024
1 parent 212ddff commit d0cdb02
Showing 1 changed file with 62 additions and 57 deletions.
119 changes: 62 additions & 57 deletions src/libcrun/handlers/wamr.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* crun - OCI runtime written in C
*
* Copyright (C) 2017, 2018, 2019, 2020, 2021 Giuseppe Scrivano <[email protected]>
* Copyright (C) 2024 Maciej Kozub <[email protected]>
* crun is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
Expand Down Expand Up @@ -71,47 +71,52 @@ libwamr_unload (void *cookie, libcrun_error_t *err)
}

// Function to read a WebAssembly binary file into a buffer
char *read_wasm_binary_to_buffer(const char *pathname, uint32_t *size) {
char *
read_wasm_binary_to_buffer (const char *pathname, uint32_t *size)
{

FILE *file;
char *buffer;
size_t file_size;
FILE *file;
char *buffer;
size_t file_size;

// Open the file in binary mode
file = fopen(pathname, "rb");
if (!file) {
error (EXIT_FAILURE, 0, "Failed to open file");
return NULL;
// Open the file in binary mode
file = fopen (pathname, "rb");
if (! file)
{
error (EXIT_FAILURE, 0, "Failed to open file");
return NULL;
}

// Seek to the end of the file to determine the size
fseek(file, 0, SEEK_END);
file_size = ftell(file);
fseek(file, 0, SEEK_SET);

// Allocate memory for the buffer
buffer = (char *)malloc(file_size);
if (!buffer) {
error (EXIT_FAILURE, 0, "Failed to allocate memory");
fclose(file);
return NULL;
// Seek to the end of the file to determine the size
fseek (file, 0, SEEK_END);
file_size = ftell (file);
fseek (file, 0, SEEK_SET);

// Allocate memory for the buffer
buffer = (char *) malloc (file_size);
if (! buffer)
{
error (EXIT_FAILURE, 0, "Failed to allocate memory");
fclose (file);
return NULL;
}

// Read the file into the buffer
if (fread(buffer, 1, file_size, file) != file_size) {
error (EXIT_FAILURE, 0, "Failed to read file");
free(buffer);
fclose(file);
return NULL;
// Read the file into the buffer
if (fread (buffer, 1, file_size, file) != file_size)
{
error (EXIT_FAILURE, 0, "Failed to read file");
free (buffer);
fclose (file);
return NULL;
}
// Close the file
fclose(file);
// Close the file
fclose (file);

// Set the size output parameter
*size = file_size;
// Set the size output parameter
*size = file_size;

// Return the buffer
return buffer;
// Return the buffer
return buffer;
}

static int
Expand All @@ -120,23 +125,23 @@ libwamr_exec (void *cookie, __attribute__ ((unused)) libcrun_container_t *contai
// load symbols from the shared library libiwasm.so
bool (*wasm_runtime_init) ();
RuntimeInitArgs init_args;
bool (*wasm_runtime_full_init)(RuntimeInitArgs *init_args);
bool (*wasm_runtime_full_init) (RuntimeInitArgs * init_args);
wasm_module_t module;
wasm_module_t (*wasm_runtime_load) (uint8_t *buf, uint32_t size, char *error_buf, uint32_t error_buf_size);
wasm_module_t (*wasm_runtime_load) (uint8_t * buf, uint32_t size, char *error_buf, uint32_t error_buf_size);
wasm_module_inst_t module_inst;
wasm_module_inst_t (*wasm_runtime_instantiate) (const wasm_module_t module, uint32_t default_stack_size, uint32_t host_managed_heap_size, char *error_buf, uint32_t error_buf_size);
wasm_function_inst_t func;
wasm_function_inst_t (*wasm_runtime_lookup_function) (wasm_module_inst_t const module_inst, const char *name);
wasm_exec_env_t exec_env;
wasm_exec_env_t (*wasm_runtime_create_exec_env) (wasm_module_inst_t module_inst, uint32_t stack_size);
bool (*wasm_runtime_call_wasm) (wasm_exec_env_t exec_env, wasm_function_inst_t function, uint32_t argc, uint32_t argv[]);
const char *(*wasm_runtime_get_exception)(wasm_module_inst_t module_inst);
const char *(*wasm_runtime_get_exception) (wasm_module_inst_t module_inst);
void (*wasm_runtime_destroy_exec_env) (wasm_exec_env_t exec_env);
void (*wasm_runtime_deinstantiate) (wasm_module_inst_t module_inst);
void (*wasm_runtime_unload) (wasm_module_t module);
void (*wasm_runtime_destroy) ();
uint32_t (*wasm_runtime_get_wasi_exit_code)(wasm_module_inst_t module_inst);
bool (*wasm_application_execute_main)(wasm_module_inst_t module_inst, int32_t argc, char *argv[]);
uint32_t (*wasm_runtime_get_wasi_exit_code) (wasm_module_inst_t module_inst);
bool (*wasm_application_execute_main) (wasm_module_inst_t module_inst, int32_t argc, char *argv[]);
void (*wasm_runtime_set_wasi_args) (wasm_module_t module, const char *dir_list[], uint32_t dir_count, const char *map_dir_list[], uint32_t map_dir_count, const char *env[], uint32_t env_count, char *argv[], int argc);

wasm_runtime_init = dlsym (cookie, "wasm_runtime_init");
Expand All @@ -157,11 +162,11 @@ libwamr_exec (void *cookie, __attribute__ ((unused)) libcrun_container_t *contai

if (wasm_runtime_init == NULL)
error (EXIT_FAILURE, 0, "could not find wasm_runtime_init symbol in `libiwasm.so`");
if(wasm_runtime_full_init == NULL)
if (wasm_runtime_full_init == NULL)
error (EXIT_FAILURE, 0, "could not find wasm_runtime_full_init symbol in `libiwasm.so`");
if (wasm_runtime_load == NULL)
error (EXIT_FAILURE, 0, "could not find wasm_runtime_load symbol in `libiwasm.so`");
if (wasm_runtime_instantiate == NULL)
if (wasm_runtime_instantiate == NULL)
error (EXIT_FAILURE, 0, "could not find wasm_runtime_instantiate symbol in `libiwasm.so`");
if (wasm_runtime_lookup_function == NULL)
error (EXIT_FAILURE, 0, "could not find wasm_runtime_lookup_function symbol in `libiwasm.so`");
Expand Down Expand Up @@ -199,45 +204,45 @@ libwamr_exec (void *cookie, __attribute__ ((unused)) libcrun_container_t *contai
arg_count++;

/* initialize the wasm runtime by default configurations */
if (!wasm_runtime_init())
if (! wasm_runtime_init ())
error (EXIT_FAILURE, 0, "Failed to initialize the wasm runtime");

/* read WASM file into a memory buffer */
buffer = read_wasm_binary_to_buffer(pathname, &buffer_size);
if (!buffer || buffer_size == 0)
buffer = read_wasm_binary_to_buffer (pathname, &buffer_size);
if (! buffer || buffer_size == 0)
error (EXIT_FAILURE, 0, "Failed to read file");

/* parse the WASM file from buffer and create a WASM module */
module = wasm_runtime_load(buffer, buffer_size, error_buf, sizeof(error_buf));
if (!module)
module = wasm_runtime_load (buffer, buffer_size, error_buf, sizeof (error_buf));
if (! module)
error (EXIT_FAILURE, 0, "Failed to load WASM file");

/* instantiate the WASI environment */
wasm_runtime_set_wasi_args(module, dirs, 1, NULL, 0, container_env, env_count, (char **) argv, arg_count);
wasm_runtime_set_wasi_args (module, dirs, 1, NULL, 0, container_env, env_count, (char **) argv, arg_count);

/* create an instance of the WASM module (WASM linear memory is ready) */
module_inst = wasm_runtime_instantiate(module, stack_size, heap_size, error_buf, sizeof(error_buf));
if (!module_inst)
module_inst = wasm_runtime_instantiate (module, stack_size, heap_size, error_buf, sizeof (error_buf));
if (! module_inst)
error (EXIT_FAILURE, 0, "Failed to instantiate the WASM module");

/* lookup a WASM function by its name The function signature can NULL here */
func = wasm_runtime_lookup_function(module_inst, "_start");
if (!func || func == NULL)
func = wasm_runtime_lookup_function (module_inst, "_start");
if (! func || func == NULL)
error (EXIT_FAILURE, 0, "Failed to lookup the WASM function");

/* creat an execution environment to execute the WASM functions */
exec_env = wasm_runtime_create_exec_env(module_inst, stack_size);
if (!exec_env)
exec_env = wasm_runtime_create_exec_env (module_inst, stack_size);
if (! exec_env)
error (EXIT_FAILURE, 0, "Failed to create the execution environment");

/* call the WASM function */
if (!wasm_runtime_call_wasm(exec_env, func, 0, NULL))
if (! wasm_runtime_call_wasm (exec_env, func, 0, NULL))
error (EXIT_FAILURE, 0, "Failed to call the WASM function");

wasm_runtime_destroy_exec_env(exec_env);
wasm_runtime_deinstantiate(module_inst);
wasm_runtime_unload(module);
wasm_runtime_destroy();
wasm_runtime_destroy_exec_env (exec_env);
wasm_runtime_deinstantiate (module_inst);
wasm_runtime_unload (module);
wasm_runtime_destroy ();

exit (EXIT_SUCCESS);
}
Expand Down

0 comments on commit d0cdb02

Please sign in to comment.