mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-02-09 00:15:07 +00:00
46b93b9d22
* Implement memory profiler, optimize memory usage, modify code indent * Implement memory.grow and limit heap space base offset to 1G; modify iwasm build type to Release and 64 bit by default * Add a new extension library: connection * Fix bug of reading magic number and version in big endian platform * Re-org platform APIs: move most platform APIs from iwasm to shared-lib * Enhance wasm loader to fix some security issues * Fix issue about illegal load of EXC_RETURN into PC on stm32 board * Updates that let a restricted version of the interpreter run in SGX * Enable native/app address validation and conversion for wasm app * Remove wasm_application_exectue_* APIs from wasm_export.h which makes confused * Refine binary size and fix several minor issues Optimize interpreter LOAD/STORE opcodes to decrease the binary size Fix issues when using iwasm library: _bh_log undefined, bh_memory.h not found Remove unused _stdin/_stdout/_stderr global variables resolve in libc wrapper Add macros of global heap size, stack size, heap size for Zephyr main.c Clear compile warning of wasm_application.c * Add more strict security checks for libc wrapper API's * Use one libc wrapper copy for sgx and other platforms; remove bh_printf macro for other platform header files * Enhance security of libc strcpy/sprintf wrapper function * Fix issue of call native for x86_64/arm/mips, add module inst parameter for native wrapper functions * Remove get_module_inst() and fix issue of call native * Refine wgl lib: remove module_inst parameter from widget functions; move function index check to runtime instantiate * Refine interpreter call native process, refine memory boudary check * Fix issues of invokeNative function of arm/mips/general version * Add a switch to build simple sample without gui support * Add BUILD_TARGET setting in makefile to replace cpu compiler flags in source code * Re-org shared lib header files, remove unused info; fix compile issues of vxworks * Add build target general * Remove unused files * Update license header * test push * Restore file * Sync up with internal/feature * Sync up with internal/feature * Rename build_wamr_app to build_wasm_app * Fix small issues of README * Enhance malformed wasm file checking Fix issue of print hex int and implement utf8 string check Fix wasi file read/write right issue Fix minor issue of build wasm app doc * Sync up with internal/feature * Sync up with internal/feature: fix interpreter arm issue, fix read leb issue * Sync up with internal/feature * Fix bug of config.h and rename wasi config.h to ssp_config.h * Sync up with internal/feature * Import wamr aot * update document * update document * Update document, disable WASI in 32bit * update document * remove files * update document * Update document * update document * update document * update samples * Sync up with internal repo
131 lines
3.5 KiB
C++
131 lines
3.5 KiB
C++
/*
|
|
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
|
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <iostream>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <unistd.h>
|
|
#include <pwd.h>
|
|
|
|
#include "Enclave_u.h"
|
|
#include "sgx_urts.h"
|
|
|
|
#ifndef TRUE
|
|
#define TRUE 1
|
|
#endif
|
|
|
|
#ifndef FALSE
|
|
#define FALSE 0
|
|
#endif
|
|
|
|
#define TOKEN_FILENAME "enclave.token"
|
|
#define ENCLAVE_FILENAME "enclave.signed.so"
|
|
#define MAX_PATH FILENAME_MAX
|
|
|
|
sgx_enclave_id_t g_eid = 0;
|
|
|
|
void
|
|
ocall_print(const char* str)
|
|
{
|
|
printf("%s", str);
|
|
}
|
|
|
|
static void
|
|
print_error_message(sgx_status_t ret)
|
|
{
|
|
printf("SGX error code: %d\n", ret);
|
|
}
|
|
|
|
/* Initialize the enclave:
|
|
* Step 1: try to retrieve the launch token saved by last transaction
|
|
* Step 2: call sgx_create_enclave to initialize an enclave instance
|
|
* Step 3: save the launch token if it is updated
|
|
*/
|
|
static int
|
|
enclave_init(sgx_enclave_id_t *p_eid)
|
|
|
|
{
|
|
char token_path[MAX_PATH] = {'\0'};
|
|
sgx_launch_token_t token = {0};
|
|
sgx_status_t ret = SGX_ERROR_UNEXPECTED;
|
|
int updated = 0;
|
|
|
|
/* Step 1: try to retrieve the launch token saved by last transaction
|
|
* if there is no token, then create a new one.
|
|
*/
|
|
/* try to get the token saved in $HOME */
|
|
const char *home_dir = getpwuid(getuid())->pw_dir;
|
|
|
|
if (home_dir != NULL &&
|
|
(strlen(home_dir) + strlen("/") + sizeof(TOKEN_FILENAME) + 1) <= MAX_PATH) {
|
|
/* compose the token path */
|
|
strncpy(token_path, home_dir, strlen(home_dir));
|
|
strncat(token_path, "/", strlen("/"));
|
|
strncat(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME) + 1);
|
|
}
|
|
else {
|
|
/* if token path is too long or $HOME is NULL */
|
|
strncpy(token_path, TOKEN_FILENAME, sizeof(TOKEN_FILENAME));
|
|
}
|
|
|
|
FILE *fp = fopen(token_path, "rb");
|
|
if (fp == NULL && (fp = fopen(token_path, "wb")) == NULL) {
|
|
printf("Warning: Failed to create/open the launch token file \"%s\".\n", token_path);
|
|
}
|
|
|
|
if (fp != NULL) {
|
|
/* read the token from saved file */
|
|
size_t read_num = fread(token, 1, sizeof(sgx_launch_token_t), fp);
|
|
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);
|
|
}
|
|
}
|
|
|
|
/* 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);
|
|
if (ret != SGX_SUCCESS) {
|
|
print_error_message(ret);
|
|
if (fp != NULL)
|
|
fclose(fp);
|
|
return -1;
|
|
}
|
|
|
|
/* 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);
|
|
return 0;
|
|
}
|
|
|
|
/* reopen the file with write capablity */
|
|
fp = freopen(token_path, "wb", fp);
|
|
if (fp == NULL)
|
|
return 0;
|
|
|
|
size_t write_num = fwrite(token, 1, sizeof(sgx_launch_token_t), fp);
|
|
if (write_num != sizeof(sgx_launch_token_t))
|
|
printf("Warning: Failed to save launch token to \"%s\".\n", token_path);
|
|
|
|
fclose(fp);
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
main(int argc, char const *argv[])
|
|
{
|
|
if (enclave_init(&g_eid) < 0) {
|
|
std::cout << "Fail to initialize enclave." << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
ecall_iwasm_main(g_eid);
|
|
return 0;
|
|
}
|
|
|