Jelajahi Sumber

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.
Wenyong Huang 3 tahun lalu
induk
melakukan
6caa6b1d73

+ 1 - 1
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);
 

+ 10 - 6
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 *

+ 2 - 2
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 *

+ 16 - 11
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 */

+ 1 - 1
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);
     };
 };

+ 1 - 1
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);
     };
 };