Support get return value for SGX os_printf/os_vprintf (#1387)

Fix the issue reported in #1359, change the implementation of
os_printf/os_vprintf for Intel SGX to get the actual bytes written.
This commit is contained in:
Wenyong Huang 2022-08-16 14:23:34 +08:00 committed by GitHub
parent aa7d447ee5
commit 6caa6b1d73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 31 additions and 22 deletions

View File

@ -52,7 +52,7 @@ typedef pthread_mutex_t korp_mutex;
typedef pthread_cond_t korp_cond; typedef pthread_cond_t korp_cond;
typedef unsigned int korp_sem; typedef unsigned int korp_sem;
typedef void (*os_print_function_t)(const char *message); typedef int (*os_print_function_t)(const char *message);
void void
os_set_print_function(os_print_function_t pf); os_set_print_function(os_print_function_t pf);

View File

@ -7,8 +7,6 @@
#include "platform_api_extension.h" #include "platform_api_extension.h"
#include "sgx_rsrv_mem_mngr.h" #include "sgx_rsrv_mem_mngr.h"
#define FIXED_BUFFER_SIZE (1 << 9)
static os_print_function_t print_function = NULL; static os_print_function_t print_function = NULL;
int int
@ -57,31 +55,37 @@ os_set_print_function(os_print_function_t pf)
print_function = pf; print_function = pf;
} }
#define FIXED_BUFFER_SIZE 4096
int int
os_printf(const char *message, ...) os_printf(const char *message, ...)
{ {
int bytes_written = 0;
if (print_function != NULL) { if (print_function != NULL) {
char msg[FIXED_BUFFER_SIZE] = { '\0' }; char msg[FIXED_BUFFER_SIZE] = { '\0' };
va_list ap; va_list ap;
va_start(ap, message); va_start(ap, message);
vsnprintf(msg, FIXED_BUFFER_SIZE, message, ap); vsnprintf(msg, FIXED_BUFFER_SIZE, message, ap);
va_end(ap); va_end(ap);
print_function(msg); bytes_written += print_function(msg);
} }
return 0; return bytes_written;
} }
int int
os_vprintf(const char *format, va_list arg) os_vprintf(const char *format, va_list arg)
{ {
int bytes_written = 0;
if (print_function != NULL) { if (print_function != NULL) {
char msg[FIXED_BUFFER_SIZE] = { '\0' }; char msg[FIXED_BUFFER_SIZE] = { '\0' };
vsnprintf(msg, FIXED_BUFFER_SIZE, format, arg); vsnprintf(msg, FIXED_BUFFER_SIZE, format, arg);
print_function(msg); bytes_written += print_function(msg);
} }
return 0; return bytes_written;
} }
char * char *

View File

@ -40,10 +40,10 @@ pal_get_enclave_id(void)
return g_eid; return g_eid;
} }
void int
ocall_print(const char *str) ocall_print(const char *str)
{ {
printf("%s", str); return printf("%s", str);
} }
static char * static char *

View File

@ -13,14 +13,19 @@
#include "bh_platform.h" #include "bh_platform.h"
extern "C" { extern "C" {
typedef void (*os_print_function_t)(const char *message); typedef int (*os_print_function_t)(const char *message);
extern void extern void
os_set_print_function(os_print_function_t pf); os_set_print_function(os_print_function_t pf);
void int
enclave_print(const char *message) 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 */ /* initialize runtime environment */
if (!wasm_runtime_full_init(&init_args)) { if (!wasm_runtime_full_init(&init_args)) {
ocall_print("Init runtime environment failed."); enclave_print("Init runtime environment failed.");
ocall_print("\n"); enclave_print("\n");
return; return;
} }
/* load WASM module */ /* load WASM module */
if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
error_buf, sizeof(error_buf)))) { error_buf, sizeof(error_buf)))) {
ocall_print(error_buf); enclave_print(error_buf);
ocall_print("\n"); enclave_print("\n");
goto fail1; goto fail1;
} }
@ -606,16 +611,16 @@ ecall_iwasm_main(uint8_t *wasm_file_buf, uint32_t wasm_file_size)
if (!(wasm_module_inst = if (!(wasm_module_inst =
wasm_runtime_instantiate(wasm_module, 16 * 1024, 16 * 1024, wasm_runtime_instantiate(wasm_module, 16 * 1024, 16 * 1024,
error_buf, sizeof(error_buf)))) { error_buf, sizeof(error_buf)))) {
ocall_print(error_buf); enclave_print(error_buf);
ocall_print("\n"); enclave_print("\n");
goto fail2; goto fail2;
} }
/* execute the main function of wasm app */ /* execute the main function of wasm app */
wasm_application_execute_main(wasm_module_inst, 0, NULL); wasm_application_execute_main(wasm_module_inst, 0, NULL);
if ((exception = wasm_runtime_get_exception(wasm_module_inst))) { if ((exception = wasm_runtime_get_exception(wasm_module_inst))) {
ocall_print(exception); enclave_print(exception);
ocall_print("\n"); enclave_print("\n");
} }
/* destroy the module instance */ /* destroy the module instance */

View File

@ -19,6 +19,6 @@ enclave {
untrusted { untrusted {
/* define OCALLs here. */ /* define OCALLs here. */
void ocall_print([in, string]const char* str); int ocall_print([in, string]const char* str);
}; };
}; };

View File

@ -17,6 +17,6 @@ enclave {
untrusted { untrusted {
/* define OCALLs here. */ /* define OCALLs here. */
void ocall_print([in, string]const char* str); int ocall_print([in, string]const char* str);
}; };
}; };