Apply clang-format for core/shared and product-mini files (#785)
Apply clang-format for core/shared and product-mini files
This commit is contained in:
@ -26,7 +26,7 @@
|
||||
#define FALSE 0
|
||||
#endif
|
||||
|
||||
#define TOKEN_FILENAME "enclave.token"
|
||||
#define TOKEN_FILENAME "enclave.token"
|
||||
#define ENCLAVE_FILENAME "enclave.signed.so"
|
||||
#define MAX_PATH 1024
|
||||
|
||||
@ -41,7 +41,7 @@ pal_get_enclave_id(void)
|
||||
}
|
||||
|
||||
void
|
||||
ocall_print(const char* str)
|
||||
ocall_print(const char *str)
|
||||
{
|
||||
printf("%s", str);
|
||||
}
|
||||
@ -50,8 +50,7 @@ static char *
|
||||
get_exe_path(char *path_buf, unsigned path_buf_size)
|
||||
{
|
||||
ssize_t i;
|
||||
ssize_t size = readlink("/proc/self/exe",
|
||||
path_buf, path_buf_size - 1);
|
||||
ssize_t size = readlink("/proc/self/exe", path_buf, path_buf_size - 1);
|
||||
|
||||
if (size < 0 || (size >= path_buf_size - 1)) {
|
||||
return NULL;
|
||||
@ -99,8 +98,9 @@ enclave_init(sgx_enclave_id_t *p_eid)
|
||||
home_dir = getpwuid(getuid())->pw_dir;
|
||||
size_t home_dir_len = home_dir ? strlen(home_dir) : 0;
|
||||
|
||||
if (home_dir != NULL &&
|
||||
home_dir_len <= MAX_PATH - 1 - sizeof(TOKEN_FILENAME) - strlen("/")) {
|
||||
if (home_dir != NULL
|
||||
&& home_dir_len
|
||||
<= MAX_PATH - 1 - sizeof(TOKEN_FILENAME) - strlen("/")) {
|
||||
/* compose the token path */
|
||||
strncpy(token_path, home_dir, MAX_PATH);
|
||||
strncat(token_path, "/", strlen("/"));
|
||||
@ -123,18 +123,19 @@ enclave_init(sgx_enclave_id_t *p_eid)
|
||||
if (read_num != 0 && read_num != sizeof(sgx_launch_token_t)) {
|
||||
/* if token is invalid, clear the buffer */
|
||||
memset(&token, 0x0, sizeof(sgx_launch_token_t));
|
||||
printf("Warning: Invalid launch token read from \"%s\".\n", token_path);
|
||||
printf("Warning: Invalid launch token read from \"%s\".\n",
|
||||
token_path);
|
||||
}
|
||||
}
|
||||
|
||||
/* Step 2: call sgx_create_enclave to initialize an enclave instance */
|
||||
/* Debug Support: set 2nd parameter to 1 */
|
||||
ret = sgx_create_enclave(ENCLAVE_FILENAME, SGX_DEBUG_FLAG,
|
||||
&token, &updated, p_eid, NULL);
|
||||
ret = sgx_create_enclave(ENCLAVE_FILENAME, SGX_DEBUG_FLAG, &token, &updated,
|
||||
p_eid, NULL);
|
||||
if (ret != SGX_SUCCESS)
|
||||
/* Try to load enclave.sign.so from the path of exe file */
|
||||
ret = sgx_create_enclave(enclave_path, SGX_DEBUG_FLAG,
|
||||
&token, &updated, p_eid, NULL);
|
||||
ret = sgx_create_enclave(enclave_path, SGX_DEBUG_FLAG, &token, &updated,
|
||||
p_eid, NULL);
|
||||
if (ret != SGX_SUCCESS) {
|
||||
printf("Failed to create enclave from %s, error code: %d\n",
|
||||
ENCLAVE_FILENAME, ret);
|
||||
@ -145,8 +146,10 @@ enclave_init(sgx_enclave_id_t *p_eid)
|
||||
|
||||
/* Step 3: save the launch token if it is updated */
|
||||
if (updated == FALSE || fp == NULL) {
|
||||
/* if the token is not updated, or file handler is invalid, do not perform saving */
|
||||
if (fp != NULL) fclose(fp);
|
||||
/* if the token is not updated, or file handler is invalid,
|
||||
do not perform saving */
|
||||
if (fp != NULL)
|
||||
fclose(fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -176,8 +179,7 @@ read_file_to_buffer(const char *filename, uint32_t *ret_size)
|
||||
}
|
||||
|
||||
if (!(file = fopen(filename, "r"))) {
|
||||
printf("Read file to buffer failed: open file %s failed.\n",
|
||||
filename);
|
||||
printf("Read file to buffer failed: open file %s failed.\n", filename);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -185,7 +187,7 @@ read_file_to_buffer(const char *filename, uint32_t *ret_size)
|
||||
file_size = ftell(file);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
|
||||
if (!(buffer = (unsigned char*)malloc(file_size))) {
|
||||
if (!(buffer = (unsigned char *)malloc(file_size))) {
|
||||
printf("Read file to buffer failed: alloc memory failed.\n");
|
||||
fclose(file);
|
||||
return NULL;
|
||||
@ -205,6 +207,7 @@ read_file_to_buffer(const char *filename, uint32_t *ret_size)
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/* clang-format off */
|
||||
static int
|
||||
print_help()
|
||||
{
|
||||
@ -227,6 +230,7 @@ print_help()
|
||||
printf(" --max-threads=n Set maximum thread number per cluster, default is 4\n");
|
||||
return 1;
|
||||
}
|
||||
/* clang-format on */
|
||||
|
||||
/**
|
||||
* Split a space separated strings into an array of strings
|
||||
@ -287,8 +291,8 @@ typedef enum EcallCmd {
|
||||
} EcallCmd;
|
||||
|
||||
static void
|
||||
app_instance_func(void *wasm_module_inst, const char *func_name,
|
||||
int app_argc, char **app_argv);
|
||||
app_instance_func(void *wasm_module_inst, const char *func_name, int app_argc,
|
||||
char **app_argv);
|
||||
|
||||
static void *
|
||||
app_instance_repl(void *module_inst, int app_argc, char **app_argv)
|
||||
@ -315,8 +319,8 @@ app_instance_repl(void *module_inst, int app_argc, char **app_argv)
|
||||
break;
|
||||
}
|
||||
if (app_argc != 0) {
|
||||
app_instance_func(module_inst, app_argv[0],
|
||||
app_argc - 1, app_argv + 1);
|
||||
app_instance_func(module_inst, app_argv[0], app_argc - 1,
|
||||
app_argv + 1);
|
||||
}
|
||||
free(app_argv);
|
||||
}
|
||||
@ -349,9 +353,9 @@ set_log_verbose_level(int log_verbose_level)
|
||||
/* Set log verbose level */
|
||||
if (log_verbose_level != 2) {
|
||||
ecall_args[0] = log_verbose_level;
|
||||
if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_SET_LOG_LEVEL,
|
||||
(uint8_t *)ecall_args,
|
||||
sizeof(uint64_t))) {
|
||||
if (SGX_SUCCESS
|
||||
!= ecall_handle_command(g_eid, CMD_SET_LOG_LEVEL,
|
||||
(uint8_t *)ecall_args, sizeof(uint64_t))) {
|
||||
printf("Call ecall_handle_command() failed.\n");
|
||||
return false;
|
||||
}
|
||||
@ -366,9 +370,9 @@ init_runtime(bool alloc_with_pool, uint32_t max_thread_num)
|
||||
|
||||
ecall_args[0] = alloc_with_pool;
|
||||
ecall_args[1] = max_thread_num;
|
||||
if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_INIT_RUNTIME,
|
||||
(uint8_t *)ecall_args,
|
||||
sizeof(uint64_t) * 2)) {
|
||||
if (SGX_SUCCESS
|
||||
!= ecall_handle_command(g_eid, CMD_INIT_RUNTIME, (uint8_t *)ecall_args,
|
||||
sizeof(uint64_t) * 2)) {
|
||||
printf("Call ecall_handle_command() failed.\n");
|
||||
return false;
|
||||
}
|
||||
@ -382,15 +386,15 @@ init_runtime(bool alloc_with_pool, uint32_t max_thread_num)
|
||||
static void
|
||||
destroy_runtime()
|
||||
{
|
||||
if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_DESTROY_RUNTIME,
|
||||
NULL, 0)) {
|
||||
if (SGX_SUCCESS
|
||||
!= ecall_handle_command(g_eid, CMD_DESTROY_RUNTIME, NULL, 0)) {
|
||||
printf("Call ecall_handle_command() failed.\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void *
|
||||
load_module(uint8_t *wasm_file_buf, uint32_t wasm_file_size,
|
||||
char *error_buf, uint32_t error_buf_size)
|
||||
load_module(uint8_t *wasm_file_buf, uint32_t wasm_file_size, char *error_buf,
|
||||
uint32_t error_buf_size)
|
||||
{
|
||||
uint64_t ecall_args[4];
|
||||
|
||||
@ -398,9 +402,9 @@ load_module(uint8_t *wasm_file_buf, uint32_t wasm_file_size,
|
||||
ecall_args[1] = wasm_file_size;
|
||||
ecall_args[2] = (uint64_t)(uintptr_t)error_buf;
|
||||
ecall_args[3] = error_buf_size;
|
||||
if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_LOAD_MODULE,
|
||||
(uint8_t *)ecall_args,
|
||||
sizeof(uint64_t) * 4)) {
|
||||
if (SGX_SUCCESS
|
||||
!= ecall_handle_command(g_eid, CMD_LOAD_MODULE, (uint8_t *)ecall_args,
|
||||
sizeof(uint64_t) * 4)) {
|
||||
printf("Call ecall_handle_command() failed.\n");
|
||||
return NULL;
|
||||
}
|
||||
@ -414,16 +418,15 @@ unload_module(void *wasm_module)
|
||||
uint64_t ecall_args[1];
|
||||
|
||||
ecall_args[0] = (uint64_t)(uintptr_t)wasm_module;
|
||||
if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_UNLOAD_MODULE,
|
||||
(uint8_t *)ecall_args,
|
||||
sizeof(uint64_t))) {
|
||||
if (SGX_SUCCESS
|
||||
!= ecall_handle_command(g_eid, CMD_UNLOAD_MODULE, (uint8_t *)ecall_args,
|
||||
sizeof(uint64_t))) {
|
||||
printf("Call ecall_handle_command() failed.\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void *
|
||||
instantiate_module(void *wasm_module,
|
||||
uint32_t stack_size, uint32_t heap_size,
|
||||
instantiate_module(void *wasm_module, uint32_t stack_size, uint32_t heap_size,
|
||||
char *error_buf, uint32_t error_buf_size)
|
||||
{
|
||||
uint64_t ecall_args[5];
|
||||
@ -433,9 +436,9 @@ instantiate_module(void *wasm_module,
|
||||
ecall_args[2] = heap_size;
|
||||
ecall_args[3] = (uint64_t)(uintptr_t)error_buf;
|
||||
ecall_args[4] = error_buf_size;
|
||||
if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_INSTANTIATE_MODULE,
|
||||
(uint8_t *)ecall_args,
|
||||
sizeof(uint64_t) * 5)) {
|
||||
if (SGX_SUCCESS
|
||||
!= ecall_handle_command(g_eid, CMD_INSTANTIATE_MODULE,
|
||||
(uint8_t *)ecall_args, sizeof(uint64_t) * 5)) {
|
||||
printf("Call ecall_handle_command() failed.\n");
|
||||
return NULL;
|
||||
}
|
||||
@ -449,25 +452,24 @@ deinstantiate_module(void *wasm_module_inst)
|
||||
uint64_t ecall_args[1];
|
||||
|
||||
ecall_args[0] = (uint64_t)(uintptr_t)wasm_module_inst;
|
||||
if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_DEINSTANTIATE_MODULE,
|
||||
(uint8_t *)ecall_args,
|
||||
sizeof(uint64_t))) {
|
||||
if (SGX_SUCCESS
|
||||
!= ecall_handle_command(g_eid, CMD_DEINSTANTIATE_MODULE,
|
||||
(uint8_t *)ecall_args, sizeof(uint64_t))) {
|
||||
printf("Call ecall_handle_command() failed.\n");
|
||||
}
|
||||
}
|
||||
|
||||
static bool
|
||||
get_exception(void *wasm_module_inst,
|
||||
char *exception, uint32_t exception_size)
|
||||
get_exception(void *wasm_module_inst, char *exception, uint32_t exception_size)
|
||||
{
|
||||
uint64_t ecall_args[3];
|
||||
|
||||
ecall_args[0] = (uint64_t)(uintptr_t)wasm_module_inst;
|
||||
ecall_args[1] = (uint64_t)(uintptr_t)exception;
|
||||
ecall_args[2] = exception_size;
|
||||
if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_GET_EXCEPTION,
|
||||
(uint8_t *)ecall_args,
|
||||
sizeof(uint64_t) * 3)) {
|
||||
if (SGX_SUCCESS
|
||||
!= ecall_handle_command(g_eid, CMD_GET_EXCEPTION, (uint8_t *)ecall_args,
|
||||
sizeof(uint64_t) * 3)) {
|
||||
printf("Call ecall_handle_command() failed.\n");
|
||||
}
|
||||
|
||||
@ -475,16 +477,15 @@ get_exception(void *wasm_module_inst,
|
||||
}
|
||||
|
||||
static void
|
||||
app_instance_main(void *wasm_module_inst,
|
||||
int app_argc, char **app_argv)
|
||||
app_instance_main(void *wasm_module_inst, int app_argc, char **app_argv)
|
||||
{
|
||||
char exception[128];
|
||||
uint64_t ecall_args_buf[16], *ecall_args = ecall_args_buf;
|
||||
int i, size;
|
||||
|
||||
if (app_argc + 2 > sizeof(ecall_args_buf) / sizeof(uint64_t)) {
|
||||
if (!(ecall_args = (uint64_t *)
|
||||
malloc(sizeof(uint64_t) * (app_argc + 2)))) {
|
||||
if (!(ecall_args =
|
||||
(uint64_t *)malloc(sizeof(uint64_t) * (app_argc + 2)))) {
|
||||
printf("Allocate memory failed.\n");
|
||||
return;
|
||||
}
|
||||
@ -497,9 +498,9 @@ app_instance_main(void *wasm_module_inst,
|
||||
}
|
||||
|
||||
size = (uint32_t)sizeof(uint64_t) * (app_argc + 2);
|
||||
if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_EXEC_APP_MAIN,
|
||||
(uint8_t *)ecall_args,
|
||||
size)) {
|
||||
if (SGX_SUCCESS
|
||||
!= ecall_handle_command(g_eid, CMD_EXEC_APP_MAIN, (uint8_t *)ecall_args,
|
||||
size)) {
|
||||
printf("Call ecall_handle_command() failed.\n");
|
||||
}
|
||||
|
||||
@ -513,15 +514,15 @@ app_instance_main(void *wasm_module_inst,
|
||||
}
|
||||
|
||||
static void
|
||||
app_instance_func(void *wasm_module_inst, const char *func_name,
|
||||
int app_argc, char **app_argv)
|
||||
app_instance_func(void *wasm_module_inst, const char *func_name, int app_argc,
|
||||
char **app_argv)
|
||||
{
|
||||
uint64_t ecall_args_buf[16], *ecall_args = ecall_args_buf;
|
||||
int i, size;
|
||||
|
||||
if (app_argc + 3 > sizeof(ecall_args_buf) / sizeof(uint64_t)) {
|
||||
if (!(ecall_args = (uint64_t *)
|
||||
malloc(sizeof(uint64_t) * (app_argc + 3)))) {
|
||||
if (!(ecall_args =
|
||||
(uint64_t *)malloc(sizeof(uint64_t) * (app_argc + 3)))) {
|
||||
printf("Allocate memory failed.\n");
|
||||
return;
|
||||
}
|
||||
@ -535,9 +536,9 @@ app_instance_func(void *wasm_module_inst, const char *func_name,
|
||||
}
|
||||
|
||||
size = (uint32_t)sizeof(uint64_t) * (app_argc + 3);
|
||||
if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_EXEC_APP_FUNC,
|
||||
(uint8_t *)ecall_args,
|
||||
size)) {
|
||||
if (SGX_SUCCESS
|
||||
!= ecall_handle_command(g_eid, CMD_EXEC_APP_FUNC, (uint8_t *)ecall_args,
|
||||
size)) {
|
||||
printf("Call ecall_handle_command() failed.\n");
|
||||
}
|
||||
|
||||
@ -547,11 +548,9 @@ app_instance_func(void *wasm_module_inst, const char *func_name,
|
||||
}
|
||||
|
||||
static bool
|
||||
set_wasi_args(void *wasm_module,
|
||||
const char **dir_list, uint32_t dir_list_size,
|
||||
const char **env_list, uint32_t env_list_size,
|
||||
int stdinfd, int stdoutfd, int stderrfd,
|
||||
char **argv, uint32_t argc)
|
||||
set_wasi_args(void *wasm_module, const char **dir_list, uint32_t dir_list_size,
|
||||
const char **env_list, uint32_t env_list_size, int stdinfd,
|
||||
int stdoutfd, int stderrfd, char **argv, uint32_t argc)
|
||||
{
|
||||
uint64_t ecall_args[10];
|
||||
|
||||
@ -565,9 +564,9 @@ set_wasi_args(void *wasm_module,
|
||||
ecall_args[7] = stderrfd;
|
||||
ecall_args[8] = (uint64_t)(uintptr_t)argv;
|
||||
ecall_args[9] = argc;
|
||||
if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_SET_WASI_ARGS,
|
||||
(uint8_t *)ecall_args,
|
||||
sizeof(uint64_t) * 10)) {
|
||||
if (SGX_SUCCESS
|
||||
!= ecall_handle_command(g_eid, CMD_SET_WASI_ARGS, (uint8_t *)ecall_args,
|
||||
sizeof(uint64_t) * 10)) {
|
||||
printf("Call ecall_handle_command() failed.\n");
|
||||
}
|
||||
|
||||
@ -693,29 +692,28 @@ main(int argc, char *argv[])
|
||||
|
||||
/* Load WASM byte buffer from WASM bin file */
|
||||
if (!(wasm_file_buf =
|
||||
(uint8_t *)read_file_to_buffer(wasm_file, &wasm_file_size))) {
|
||||
(uint8_t *)read_file_to_buffer(wasm_file, &wasm_file_size))) {
|
||||
goto fail1;
|
||||
}
|
||||
|
||||
/* Load module */
|
||||
if (!(wasm_module = load_module(wasm_file_buf, wasm_file_size,
|
||||
error_buf, sizeof(error_buf)))) {
|
||||
if (!(wasm_module = load_module(wasm_file_buf, wasm_file_size, error_buf,
|
||||
sizeof(error_buf)))) {
|
||||
printf("%s\n", error_buf);
|
||||
goto fail2;
|
||||
}
|
||||
|
||||
/* Set wasi arguments */
|
||||
if (!set_wasi_args(wasm_module, dir_list, dir_list_size,
|
||||
env_list, env_list_size, 0, 1, 2, argv, argc)) {
|
||||
if (!set_wasi_args(wasm_module, dir_list, dir_list_size, env_list,
|
||||
env_list_size, 0, 1, 2, argv, argc)) {
|
||||
printf("%s\n", "set wasi arguments failed.\n");
|
||||
goto fail3;
|
||||
}
|
||||
|
||||
/* Instantiate module */
|
||||
if (!(wasm_module_inst = instantiate_module(wasm_module,
|
||||
stack_size, heap_size,
|
||||
error_buf,
|
||||
sizeof(error_buf)))) {
|
||||
if (!(wasm_module_inst =
|
||||
instantiate_module(wasm_module, stack_size, heap_size, error_buf,
|
||||
sizeof(error_buf)))) {
|
||||
printf("%s\n", error_buf);
|
||||
goto fail3;
|
||||
}
|
||||
@ -723,8 +721,7 @@ main(int argc, char *argv[])
|
||||
if (is_repl_mode)
|
||||
app_instance_repl(wasm_module_inst, argc, argv);
|
||||
else if (func_name)
|
||||
app_instance_func(wasm_module_inst, func_name,
|
||||
argc - 1, argv + 1);
|
||||
app_instance_func(wasm_module_inst, func_name, argc - 1, argv + 1);
|
||||
else
|
||||
app_instance_main(wasm_module_inst, argc, argv);
|
||||
|
||||
@ -782,19 +779,19 @@ wamr_pal_create_process(struct wamr_pal_create_process_args *args)
|
||||
int stderrfd = -1;
|
||||
|
||||
int argc = 2;
|
||||
char *argv[argc] = { (char*)"./iwasm", (char *)args->argv[0] };
|
||||
char *argv[argc] = { (char *)"./iwasm", (char *)args->argv[0] };
|
||||
|
||||
uint8_t *wasm_files_buf = NULL;
|
||||
void *wasm_modules = NULL;
|
||||
int len = 0, i;
|
||||
|
||||
char *temp = (char *)args->argv[0];
|
||||
while(temp) {
|
||||
while (temp) {
|
||||
len++;
|
||||
temp=(char *)args->argv[len];
|
||||
temp = (char *)args->argv[len];
|
||||
}
|
||||
|
||||
if (len > sizeof(wasm_files)/sizeof(char *)) {
|
||||
if (len > sizeof(wasm_files) / sizeof(char *)) {
|
||||
printf("Number of input files is out of range\n");
|
||||
return -1;
|
||||
}
|
||||
@ -829,8 +826,8 @@ wamr_pal_create_process(struct wamr_pal_create_process_args *args)
|
||||
char error_buf[128] = { 0 };
|
||||
|
||||
/* Load WASM byte buffer from WASM bin file */
|
||||
if (!(wasm_file_buf = (uint8_t *)read_file_to_buffer
|
||||
(wasm_files[i], &wasm_file_size))) {
|
||||
if (!(wasm_file_buf = (uint8_t *)read_file_to_buffer(
|
||||
wasm_files[i], &wasm_file_size))) {
|
||||
printf("Failed to read file to buffer\n");
|
||||
destroy_runtime();
|
||||
return -1;
|
||||
@ -846,10 +843,9 @@ wamr_pal_create_process(struct wamr_pal_create_process_args *args)
|
||||
}
|
||||
|
||||
/* Set wasi arguments */
|
||||
if (!set_wasi_args(wasm_module, dir_list, dir_list_size,
|
||||
env_list, env_list_size,
|
||||
stdinfd, stdoutfd, stderrfd,
|
||||
argv, argc)) {
|
||||
if (!set_wasi_args(wasm_module, dir_list, dir_list_size, env_list,
|
||||
env_list_size, stdinfd, stdoutfd, stderrfd, argv,
|
||||
argc)) {
|
||||
printf("%s\n", "set wasi arguments failed.\n");
|
||||
unload_module(wasm_module);
|
||||
free(wasm_file_buf);
|
||||
@ -858,10 +854,9 @@ wamr_pal_create_process(struct wamr_pal_create_process_args *args)
|
||||
}
|
||||
|
||||
/* Instantiate module */
|
||||
if (!(wasm_module_inst[i] = instantiate_module(wasm_module,
|
||||
stack_size, heap_size,
|
||||
error_buf,
|
||||
sizeof(error_buf)))) {
|
||||
if (!(wasm_module_inst[i] =
|
||||
instantiate_module(wasm_module, stack_size, heap_size,
|
||||
error_buf, sizeof(error_buf)))) {
|
||||
printf("%s\n", error_buf);
|
||||
unload_module(wasm_module);
|
||||
free(wasm_file_buf);
|
||||
@ -884,37 +879,43 @@ wamr_pal_create_process(struct wamr_pal_create_process_args *args)
|
||||
int
|
||||
wamr_pal_destroy(void)
|
||||
{
|
||||
//sgx_destroy_enclave(g_eid);
|
||||
// sgx_destroy_enclave(g_eid);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
wamr_pal_exec(struct wamr_pal_exec_args *args)
|
||||
{
|
||||
//app_instance_main(wasm_module_inst[i], argc, argv);
|
||||
// app_instance_main(wasm_module_inst[i], argc, argv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
wamr_pal_kill(int pid, int sig)
|
||||
{
|
||||
//deinstantiate_module(wasm_module_inst[i]);
|
||||
//unload_module(wasm_module);
|
||||
//free(wasm_file_buf);
|
||||
// deinstantiate_module(wasm_module_inst[i]);
|
||||
// unload_module(wasm_module);
|
||||
// free(wasm_file_buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pal_get_version(void) __attribute__((weak, alias ("wamr_pal_get_version")));
|
||||
int
|
||||
pal_get_version(void) __attribute__((weak, alias("wamr_pal_get_version")));
|
||||
|
||||
int pal_init(const struct wamr_pal_attr *attr)\
|
||||
__attribute__ ((weak, alias ("wamr_pal_init")));
|
||||
int
|
||||
pal_init(const struct wamr_pal_attr *attr)
|
||||
__attribute__((weak, alias("wamr_pal_init")));
|
||||
|
||||
int pal_create_process(struct wamr_pal_create_process_args *args)\
|
||||
__attribute__ ((weak, alias ("wamr_pal_create_process")));
|
||||
int
|
||||
pal_create_process(struct wamr_pal_create_process_args *args)
|
||||
__attribute__((weak, alias("wamr_pal_create_process")));
|
||||
|
||||
int pal_exec(struct wamr_pal_exec_args *args)\
|
||||
__attribute__ ((weak, alias ("wamr_pal_exec")));
|
||||
int
|
||||
pal_exec(struct wamr_pal_exec_args *args)
|
||||
__attribute__((weak, alias("wamr_pal_exec")));
|
||||
|
||||
int pal_kill(int pid, int sig) __attribute__ ((weak, alias ("wamr_pal_kill")));
|
||||
int
|
||||
pal_kill(int pid, int sig) __attribute__((weak, alias("wamr_pal_kill")));
|
||||
|
||||
int pal_destroy(void) __attribute__ ((weak, alias ("wamr_pal_destroy")));
|
||||
int
|
||||
pal_destroy(void) __attribute__((weak, alias("wamr_pal_destroy")));
|
||||
|
||||
@ -20,7 +20,8 @@ extern "C" {
|
||||
*
|
||||
* @retval If > 0, then success; otherwise, it is an invalid version.
|
||||
*/
|
||||
int wamr_pal_get_version(void);
|
||||
int
|
||||
wamr_pal_get_version(void);
|
||||
|
||||
/*
|
||||
* WAMR PAL attributes
|
||||
@ -29,19 +30,21 @@ typedef struct wamr_pal_attr {
|
||||
// WAMR instance directory.
|
||||
//
|
||||
// The default value is "."; that is, the current working directory
|
||||
const char *instance_dir;
|
||||
const char *instance_dir;
|
||||
// Log level.
|
||||
//
|
||||
// Specifies the log verbose level (0 to 5, default is 2)
|
||||
// large level with more log
|
||||
//
|
||||
const char *log_level;
|
||||
const char *log_level;
|
||||
} wamr_pal_attr_t;
|
||||
|
||||
/* clang-format off */
|
||||
#define WAMR_PAL_ATTR_INITVAL { \
|
||||
.instance_dir = ".", \
|
||||
.log_level = 2 \
|
||||
}
|
||||
/* clang-format on */
|
||||
|
||||
/*
|
||||
* The struct which consists of file descriptors of standard I/O
|
||||
@ -66,21 +69,23 @@ struct wamr_pal_create_process_args {
|
||||
|
||||
// Argments array pass to new process.
|
||||
//
|
||||
// The arguments to the command. By convention, the argv[0] should be the program name.
|
||||
// And the last element of the array must be NULL to indicate the length of array.
|
||||
// The arguments to the command. By convention, the argv[0] should be the
|
||||
// program name. And the last element of the array must be NULL to indicate
|
||||
// the length of array.
|
||||
//
|
||||
// Mandatory field. Must not be NULL.
|
||||
const char **argv;
|
||||
|
||||
// Untrusted environment variable array pass to new process.
|
||||
//
|
||||
// The untrusted env vars to the command. And the last element of the array must be
|
||||
// NULL to indicate the length of array.
|
||||
// The untrusted env vars to the command. And the last element of the array
|
||||
// must be NULL to indicate the length of array.
|
||||
//
|
||||
// Optional field.
|
||||
const char **env;
|
||||
|
||||
// File descriptors of the redirected standard I/O (i.e., stdin, stdout, stderr)
|
||||
// File descriptors of the redirected standard I/O (i.e., stdin, stdout,
|
||||
// stderr)
|
||||
//
|
||||
// If set to NULL, will use the original standard I/O file descriptors.
|
||||
//
|
||||
@ -115,32 +120,41 @@ struct wamr_pal_exec_args {
|
||||
int *exit_value;
|
||||
};
|
||||
|
||||
int wamr_pal_init(const struct wamr_pal_attr *args);
|
||||
int
|
||||
wamr_pal_init(const struct wamr_pal_attr *args);
|
||||
|
||||
int wamr_pal_create_process(struct wamr_pal_create_process_args *args);
|
||||
int
|
||||
wamr_pal_create_process(struct wamr_pal_create_process_args *args);
|
||||
|
||||
int wamr_pal_destroy(void);
|
||||
int
|
||||
wamr_pal_destroy(void);
|
||||
|
||||
int wamr_pal_exec(struct wamr_pal_exec_args *args);
|
||||
int
|
||||
wamr_pal_exec(struct wamr_pal_exec_args *args);
|
||||
|
||||
int wamr_pal_kill(int pid, int sig);
|
||||
int
|
||||
wamr_pal_kill(int pid, int sig);
|
||||
|
||||
int pal_get_version(void);
|
||||
int
|
||||
pal_get_version(void);
|
||||
|
||||
int pal_init(const struct wamr_pal_attr *attr);
|
||||
int
|
||||
pal_init(const struct wamr_pal_attr *attr);
|
||||
|
||||
int pal_create_process(struct wamr_pal_create_process_args *args);
|
||||
int
|
||||
pal_create_process(struct wamr_pal_create_process_args *args);
|
||||
|
||||
int pal_exec(struct wamr_pal_exec_args *args);
|
||||
int
|
||||
pal_exec(struct wamr_pal_exec_args *args);
|
||||
|
||||
int pal_kill(int pid, int sig);
|
||||
|
||||
int pal_destroy(void);
|
||||
int
|
||||
pal_kill(int pid, int sig);
|
||||
|
||||
int
|
||||
pal_destroy(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __WAMR_PAL_API_H__ */
|
||||
|
||||
|
||||
@ -13,14 +13,15 @@
|
||||
#include "bh_platform.h"
|
||||
|
||||
extern "C" {
|
||||
typedef void (*os_print_function_t)(const char* message);
|
||||
extern void os_set_print_function(os_print_function_t pf);
|
||||
typedef void (*os_print_function_t)(const char *message);
|
||||
extern void
|
||||
os_set_print_function(os_print_function_t pf);
|
||||
|
||||
void
|
||||
enclave_print(const char *message)
|
||||
{
|
||||
ocall_print(message);
|
||||
}
|
||||
void
|
||||
enclave_print(const char *message)
|
||||
{
|
||||
ocall_print(message);
|
||||
}
|
||||
}
|
||||
|
||||
typedef enum EcallCmd {
|
||||
@ -130,8 +131,8 @@ handle_cmd_load_module(uint64 *args, uint32 argc)
|
||||
bh_assert(argc == 4);
|
||||
|
||||
if (total_size >= UINT32_MAX
|
||||
|| !(enclave_module = (EnclaveModule *)
|
||||
wasm_runtime_malloc((uint32)total_size))) {
|
||||
|| !(enclave_module =
|
||||
(EnclaveModule *)wasm_runtime_malloc((uint32)total_size))) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"WASM module load failed: "
|
||||
"allocate memory failed.");
|
||||
@ -140,14 +141,13 @@ handle_cmd_load_module(uint64 *args, uint32 argc)
|
||||
}
|
||||
|
||||
memset(enclave_module, 0, (uint32)total_size);
|
||||
enclave_module->wasm_file = (uint8 *)enclave_module
|
||||
+ sizeof(EnclaveModule);
|
||||
bh_memcpy_s(enclave_module->wasm_file, wasm_file_size,
|
||||
wasm_file, wasm_file_size);
|
||||
enclave_module->wasm_file = (uint8 *)enclave_module + sizeof(EnclaveModule);
|
||||
bh_memcpy_s(enclave_module->wasm_file, wasm_file_size, wasm_file,
|
||||
wasm_file_size);
|
||||
|
||||
if (!(enclave_module->module =
|
||||
wasm_runtime_load(enclave_module->wasm_file, wasm_file_size,
|
||||
error_buf, error_buf_size))) {
|
||||
wasm_runtime_load(enclave_module->wasm_file, wasm_file_size,
|
||||
error_buf, error_buf_size))) {
|
||||
wasm_runtime_free(enclave_module);
|
||||
*(void **)args_org = NULL;
|
||||
return;
|
||||
@ -189,9 +189,8 @@ handle_cmd_instantiate_module(uint64 *args, uint32 argc)
|
||||
bh_assert(argc == 5);
|
||||
|
||||
if (!(module_inst =
|
||||
wasm_runtime_instantiate(enclave_module->module,
|
||||
stack_size, heap_size,
|
||||
error_buf, error_buf_size))) {
|
||||
wasm_runtime_instantiate(enclave_module->module, stack_size,
|
||||
heap_size, error_buf, error_buf_size))) {
|
||||
*(void **)args_org = NULL;
|
||||
return;
|
||||
}
|
||||
@ -225,8 +224,7 @@ handle_cmd_get_exception(uint64 *args, uint32 argc)
|
||||
bh_assert(argc == 3);
|
||||
|
||||
if ((exception1 = wasm_runtime_get_exception(module_inst))) {
|
||||
snprintf(exception, exception_size,
|
||||
"%s", exception1);
|
||||
snprintf(exception, exception_size, "%s", exception1);
|
||||
args_org[0] = true;
|
||||
}
|
||||
else {
|
||||
@ -339,15 +337,14 @@ handle_cmd_set_wasi_args(uint64 *args, int32 argc)
|
||||
}
|
||||
|
||||
if (total_size >= UINT32_MAX
|
||||
|| !(enclave_module->wasi_arg_buf = p = (char *)
|
||||
wasm_runtime_malloc((uint32)total_size))) {
|
||||
|| !(enclave_module->wasi_arg_buf = p =
|
||||
(char *)wasm_runtime_malloc((uint32)total_size))) {
|
||||
*args_org = false;
|
||||
return;
|
||||
}
|
||||
|
||||
p1 = p + sizeof(char *) * dir_list_size
|
||||
+ sizeof(char *) * env_list_size
|
||||
+ sizeof(char *) * wasi_argc;
|
||||
p1 = p + sizeof(char *) * dir_list_size + sizeof(char *) * env_list_size
|
||||
+ sizeof(char *) * wasi_argc;
|
||||
|
||||
if (dir_list_size > 0) {
|
||||
enclave_module->wasi_dir_list = (char **)p;
|
||||
@ -385,17 +382,12 @@ handle_cmd_set_wasi_args(uint64 *args, int32 argc)
|
||||
p += sizeof(char *) * wasi_argc;
|
||||
}
|
||||
|
||||
wasm_runtime_set_wasi_args_ex(enclave_module->module,
|
||||
(const char **)enclave_module->wasi_dir_list,
|
||||
dir_list_size,
|
||||
NULL, 0,
|
||||
(const char **)enclave_module->wasi_env_list,
|
||||
env_list_size,
|
||||
enclave_module->wasi_argv,
|
||||
enclave_module->wasi_argc,
|
||||
(stdinfd != -1) ? stdinfd : 0,
|
||||
(stdoutfd != -1) ? stdoutfd : 1,
|
||||
(stderrfd != -1) ? stderrfd : 2);
|
||||
wasm_runtime_set_wasi_args_ex(
|
||||
enclave_module->module, (const char **)enclave_module->wasi_dir_list,
|
||||
dir_list_size, NULL, 0, (const char **)enclave_module->wasi_env_list,
|
||||
env_list_size, enclave_module->wasi_argv, enclave_module->wasi_argc,
|
||||
(stdinfd != -1) ? stdinfd : 0, (stdoutfd != -1) ? stdoutfd : 1,
|
||||
(stderrfd != -1) ? stderrfd : 2);
|
||||
|
||||
*args_org = true;
|
||||
}
|
||||
@ -408,8 +400,7 @@ handle_cmd_set_wasi_args(uint64 *args, int32 argc)
|
||||
#endif /* end of SGX_DISABLE_WASI */
|
||||
|
||||
void
|
||||
ecall_handle_command(unsigned cmd,
|
||||
unsigned char *cmd_buf,
|
||||
ecall_handle_command(unsigned cmd, unsigned char *cmd_buf,
|
||||
unsigned cmd_buf_size)
|
||||
{
|
||||
uint64 *args = (uint64 *)cmd_buf;
|
||||
@ -494,11 +485,9 @@ ecall_iwasm_main(uint8_t *wasm_file_buf, uint32_t wasm_file_size)
|
||||
}
|
||||
|
||||
/* instantiate the module */
|
||||
if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module,
|
||||
16 * 1024,
|
||||
16 * 1024,
|
||||
error_buf,
|
||||
sizeof(error_buf)))) {
|
||||
if (!(wasm_module_inst =
|
||||
wasm_runtime_instantiate(wasm_module, 16 * 1024, 16 * 1024,
|
||||
error_buf, sizeof(error_buf)))) {
|
||||
ocall_print(error_buf);
|
||||
ocall_print("\n");
|
||||
goto fail2;
|
||||
|
||||
@ -8,7 +8,8 @@
|
||||
#include "wasm_export.h"
|
||||
#include "bh_platform.h"
|
||||
|
||||
void ecall_iwasm_test()
|
||||
void
|
||||
ecall_iwasm_test()
|
||||
{
|
||||
ocall_print(" Prepare to invoke sgx_open ... ==>\n\n");
|
||||
|
||||
@ -76,7 +77,7 @@ void ecall_iwasm_test()
|
||||
02 : - O_RDWR */
|
||||
/** 1. open **/
|
||||
fd = open(file, O_RDWR);
|
||||
if (fd !=-1) {
|
||||
if (fd != -1) {
|
||||
ocall_print("\tOperation open test_open.txt success.\n");
|
||||
}
|
||||
|
||||
@ -107,7 +108,7 @@ void ecall_iwasm_test()
|
||||
ocall_print("\tOperation fdatasync success.\n");
|
||||
}
|
||||
|
||||
/** isatty **/
|
||||
/** isatty **/
|
||||
ret = isatty(fd);
|
||||
if (ret == 0) {
|
||||
ocall_print("\tOperation fisatty success.\n");
|
||||
@ -140,7 +141,7 @@ void ecall_iwasm_test()
|
||||
p_dirent = readdir(dirp);
|
||||
if (p_dirent != NULL) {
|
||||
ocall_print("\tOperation readdir success.\t");
|
||||
ocall_print(p_dirent -> d_name);
|
||||
ocall_print(p_dirent->d_name);
|
||||
ocall_print("\n");
|
||||
}
|
||||
|
||||
@ -158,7 +159,7 @@ void ecall_iwasm_test()
|
||||
|
||||
/** closedir **/
|
||||
ret = closedir(dirp);
|
||||
if (ret == 0 ) {
|
||||
if (ret == 0) {
|
||||
ocall_print("\tOperation closedir success. \n");
|
||||
}
|
||||
/* 2. close */
|
||||
@ -192,7 +193,7 @@ void ecall_iwasm_test()
|
||||
ret = renameat(AT_FDCWD, rlt_dir_path, AT_FDCWD, rlt_dir_path_new);
|
||||
if (ret == 0) {
|
||||
ocall_print("\tOperation renameat ./tmp to "
|
||||
"./tmp_new success. \n");
|
||||
"./tmp_new success. \n");
|
||||
}
|
||||
renameat(AT_FDCWD, rlt_dir_path_new, AT_FDCWD, rlt_dir_path);
|
||||
|
||||
@ -220,7 +221,7 @@ void ecall_iwasm_test()
|
||||
ret = symlinkat(file, AT_FDCWD, file_sf_ln);
|
||||
if (ret == 0) {
|
||||
ocall_print("\tOperation symlinkat from test.txt "
|
||||
"to text_sf_ln.txt success. \n");
|
||||
"to text_sf_ln.txt success. \n");
|
||||
}
|
||||
/** readlinkat **/
|
||||
total_size = readlinkat(AT_FDCWD, file_sf_ln, buf, sizeof(buf));
|
||||
@ -336,22 +337,23 @@ void ecall_iwasm_test()
|
||||
close(fd);
|
||||
|
||||
/** getopt **/
|
||||
while((ret = getopt(argc, argv, "f:abc")) != -1){ //get option from the getopt() method
|
||||
switch(ret){
|
||||
//For option i, r, l, print that these are options
|
||||
case 'a':
|
||||
case 'b':
|
||||
case 'c':
|
||||
ocall_print("\tGiven Option operation success. \n");
|
||||
break;
|
||||
case 'f': //here f is used for some file name
|
||||
ocall_print("\tGiven File operation success.\n");
|
||||
break;
|
||||
case '?': //used for some unknown options
|
||||
ocall_print("\tunknown option trigger success.\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
while ((ret = getopt(argc, argv, "f:abc"))
|
||||
!= -1) { // get option from the getopt() method
|
||||
switch (ret) {
|
||||
// For option i, r, l, print that these are options
|
||||
case 'a':
|
||||
case 'b':
|
||||
case 'c':
|
||||
ocall_print("\tGiven Option operation success. \n");
|
||||
break;
|
||||
case 'f': // here f is used for some file name
|
||||
ocall_print("\tGiven File operation success.\n");
|
||||
break;
|
||||
case '?': // used for some unknown options
|
||||
ocall_print("\tunknown option trigger success.\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/** sched_yield **/
|
||||
ret = sched_yield();
|
||||
|
||||
Reference in New Issue
Block a user