diff --git a/core/shared/platform/linux-sgx/platform_internal.h b/core/shared/platform/linux-sgx/platform_internal.h index 33e23c8c..5670052c 100644 --- a/core/shared/platform/linux-sgx/platform_internal.h +++ b/core/shared/platform/linux-sgx/platform_internal.h @@ -52,7 +52,7 @@ typedef pthread_mutex_t korp_mutex; typedef pthread_cond_t korp_cond; typedef unsigned int korp_sem; -typedef void (*os_print_function_t)(const char *message); +typedef int (*os_print_function_t)(const char *message); void os_set_print_function(os_print_function_t pf); diff --git a/core/shared/platform/linux-sgx/sgx_platform.c b/core/shared/platform/linux-sgx/sgx_platform.c index b1b741b0..b14ce67d 100644 --- a/core/shared/platform/linux-sgx/sgx_platform.c +++ b/core/shared/platform/linux-sgx/sgx_platform.c @@ -7,8 +7,6 @@ #include "platform_api_extension.h" #include "sgx_rsrv_mem_mngr.h" -#define FIXED_BUFFER_SIZE (1 << 9) - static os_print_function_t print_function = NULL; int @@ -57,31 +55,37 @@ os_set_print_function(os_print_function_t pf) print_function = pf; } +#define FIXED_BUFFER_SIZE 4096 + int os_printf(const char *message, ...) { + int bytes_written = 0; + if (print_function != NULL) { char msg[FIXED_BUFFER_SIZE] = { '\0' }; va_list ap; va_start(ap, message); vsnprintf(msg, FIXED_BUFFER_SIZE, message, ap); va_end(ap); - print_function(msg); + bytes_written += print_function(msg); } - return 0; + return bytes_written; } int os_vprintf(const char *format, va_list arg) { + int bytes_written = 0; + if (print_function != NULL) { char msg[FIXED_BUFFER_SIZE] = { '\0' }; vsnprintf(msg, FIXED_BUFFER_SIZE, format, arg); - print_function(msg); + bytes_written += print_function(msg); } - return 0; + return bytes_written; } char * diff --git a/product-mini/platforms/linux-sgx/enclave-sample/App/App.cpp b/product-mini/platforms/linux-sgx/enclave-sample/App/App.cpp index 099a96dc..2a697285 100644 --- a/product-mini/platforms/linux-sgx/enclave-sample/App/App.cpp +++ b/product-mini/platforms/linux-sgx/enclave-sample/App/App.cpp @@ -40,10 +40,10 @@ pal_get_enclave_id(void) return g_eid; } -void +int ocall_print(const char *str) { - printf("%s", str); + return printf("%s", str); } static char * diff --git a/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp b/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp index 35367e08..e8d266c3 100644 --- a/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp +++ b/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp @@ -13,14 +13,19 @@ #include "bh_platform.h" extern "C" { -typedef void (*os_print_function_t)(const char *message); +typedef int (*os_print_function_t)(const char *message); extern void os_set_print_function(os_print_function_t pf); -void +int enclave_print(const char *message) { - ocall_print(message); + int bytes_written = 0; + + if (SGX_SUCCESS != ocall_print(&bytes_written, message)) + return 0; + + return bytes_written; } } @@ -589,16 +594,16 @@ ecall_iwasm_main(uint8_t *wasm_file_buf, uint32_t wasm_file_size) /* initialize runtime environment */ if (!wasm_runtime_full_init(&init_args)) { - ocall_print("Init runtime environment failed."); - ocall_print("\n"); + enclave_print("Init runtime environment failed."); + enclave_print("\n"); return; } /* load WASM module */ if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf, sizeof(error_buf)))) { - ocall_print(error_buf); - ocall_print("\n"); + enclave_print(error_buf); + enclave_print("\n"); goto fail1; } @@ -606,16 +611,16 @@ ecall_iwasm_main(uint8_t *wasm_file_buf, uint32_t wasm_file_size) 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"); + enclave_print(error_buf); + enclave_print("\n"); goto fail2; } /* execute the main function of wasm app */ wasm_application_execute_main(wasm_module_inst, 0, NULL); if ((exception = wasm_runtime_get_exception(wasm_module_inst))) { - ocall_print(exception); - ocall_print("\n"); + enclave_print(exception); + enclave_print("\n"); } /* destroy the module instance */ diff --git a/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.edl b/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.edl index 77d0d3ea..3978465e 100644 --- a/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.edl +++ b/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.edl @@ -19,6 +19,6 @@ enclave { untrusted { /* define OCALLs here. */ - void ocall_print([in, string]const char* str); + int ocall_print([in, string]const char* str); }; }; diff --git a/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave_minimal.edl b/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave_minimal.edl index 5b3bde34..f7c1ebee 100644 --- a/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave_minimal.edl +++ b/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave_minimal.edl @@ -17,6 +17,6 @@ enclave { untrusted { /* define OCALLs here. */ - void ocall_print([in, string]const char* str); + int ocall_print([in, string]const char* str); }; };