From 3198018214b67a1d2cc6a15bd8c6de544e73613b Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Thu, 11 Jan 2024 14:26:39 +0800 Subject: [PATCH] Fix linux-sgx build error when libc-wasi is disabled (#2997) Compilation error was reported when `cmake -DWAMR_BUILD_LIBC_WASI=0` on linux-sgx platform: ``` core/shared/platform/linux-sgx/sgx_socket.c:8:10: fatal error: libc_errno.h: No such file or directory 8 | #include "libc_errno.h" | ^~~~~~~~~~~~~~ ``` After fixing, both `cmake -DWAMR_BUILD_LIBC_WASI=1` and `WAMR_BUILD_LIBC_WASI=0` work good. --- core/shared/platform/linux-sgx/sgx_platform.c | 12 ++++++++++++ core/shared/platform/linux-sgx/sgx_socket.c | 3 ++- product-mini/platforms/linux-sgx/CMakeLists.txt | 12 ++++++++++++ .../linux-sgx/enclave-sample/Enclave/Enclave.cpp | 4 ++-- .../platforms/linux-sgx/enclave-sample/Makefile | 13 +++++++++++-- .../linux-sgx/enclave-sample/Makefile_minimal | 2 +- 6 files changed, 40 insertions(+), 6 deletions(-) diff --git a/core/shared/platform/linux-sgx/sgx_platform.c b/core/shared/platform/linux-sgx/sgx_platform.c index 32b956826..3a4a19245 100644 --- a/core/shared/platform/linux-sgx/sgx_platform.c +++ b/core/shared/platform/linux-sgx/sgx_platform.c @@ -119,6 +119,18 @@ strcpy(char *dest, const char *src) return dest; } +#if WASM_ENABLE_LIBC_WASI == 0 +bool +os_is_handle_valid(os_file_handle *handle) +{ + assert(handle != NULL); + + return *handle > -1; +} +#else +/* implemented in posix_file.c */ +#endif + void * os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file) { diff --git a/core/shared/platform/linux-sgx/sgx_socket.c b/core/shared/platform/linux-sgx/sgx_socket.c index cd1a6cead..458bb1e24 100644 --- a/core/shared/platform/linux-sgx/sgx_socket.c +++ b/core/shared/platform/linux-sgx/sgx_socket.c @@ -5,10 +5,11 @@ #include "platform_api_vmcore.h" #include "platform_api_extension.h" -#include "libc_errno.h" #ifndef SGX_DISABLE_WASI +#include "libc_errno.h" + #define TRACE_OCALL_FAIL() os_printf("ocall %s failed!\n", __FUNCTION__) /** OCALLs prototypes **/ diff --git a/product-mini/platforms/linux-sgx/CMakeLists.txt b/product-mini/platforms/linux-sgx/CMakeLists.txt index a9aef355c..0b2b4afec 100644 --- a/product-mini/platforms/linux-sgx/CMakeLists.txt +++ b/product-mini/platforms/linux-sgx/CMakeLists.txt @@ -164,3 +164,15 @@ else() OUTPUT_VARIABLE cmdOutput ) endif() + +if (WAMR_BUILD_LIBC_WASI EQUAL 1) + execute_process( + COMMAND bash -c "sed -i -E 's/^WAMR_BUILD_LIBC_WASI = 0/WAMR_BUILD_LIBC_WASI = 1/g' ${CMAKE_CURRENT_SOURCE_DIR}/enclave-sample/Makefile" + OUTPUT_VARIABLE cmdOutput + ) +else() + execute_process( + COMMAND bash -c "sed -i -E 's/^WAMR_BUILD_LIBC_WASI = 1/WAMR_BUILD_LIBC_WASI = 0/g' ${CMAKE_CURRENT_SOURCE_DIR}/enclave-sample/Makefile" + OUTPUT_VARIABLE cmdOutput + ) +endif() 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 b854c70b4..27e12d378 100644 --- a/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp +++ b/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp @@ -510,7 +510,7 @@ handle_cmd_set_log_level(uint64 *args, uint32 argc) #endif } -#ifndef SGX_DISABLE_WASI +#if WASM_ENABLE_LIBC_WASI != 0 static void handle_cmd_set_wasi_args(uint64 *args, int32 argc) { @@ -637,7 +637,7 @@ handle_cmd_set_wasi_args(uint64 *args, int32 argc) { *args = true; } -#endif /* end of SGX_DISABLE_WASI */ +#endif /* end of WASM_ENABLE_LIBC_WASI != 0 */ static void handle_cmd_get_version(uint64 *args, uint32 argc) diff --git a/product-mini/platforms/linux-sgx/enclave-sample/Makefile b/product-mini/platforms/linux-sgx/enclave-sample/Makefile index dae97a069..8fd053a5f 100644 --- a/product-mini/platforms/linux-sgx/enclave-sample/Makefile +++ b/product-mini/platforms/linux-sgx/enclave-sample/Makefile @@ -16,6 +16,7 @@ WAMR_BUILD_LIB_RATS = 0 WAMR_BUILD_GLOBAL_HEAP_POOL = 0 WAMR_BUILD_GLOBAL_HEAP_SIZE = 10485760 WAMR_BUILD_STATIC_PGO = 0 +WAMR_BUILD_LIBC_WASI = 1 VMLIB_BUILD_DIR ?= $(CURDIR)/../build LIB_RATS_SRC ?= $(VMLIB_BUILD_DIR)/_deps/librats-build @@ -66,7 +67,9 @@ ifeq ($(WAMR_BUILD_LIB_RATS), 1) App_Include_Paths += -I$(LIB_RATS_INCLUDE_DIR) endif -App_C_Flags := $(SGX_COMMON_CFLAGS) -fPIC -Wno-attributes $(App_Include_Paths) -DWASM_ENABLE_STATIC_PGO=$(WAMR_BUILD_STATIC_PGO) +App_C_Flags := $(SGX_COMMON_CFLAGS) -fPIC -Wno-attributes $(App_Include_Paths) \ + -DWASM_ENABLE_STATIC_PGO=$(WAMR_BUILD_STATIC_PGO) \ + -DWASM_ENABLE_LIBC_WASI=$(WAMR_BUILD_LIBC_WASI) # Three configuration modes - Debug, prerelease, release # Debug - Macro DEBUG enabled. @@ -135,7 +138,13 @@ ifeq ($(WAMR_BUILD_LIB_RATS), 1) Enclave_Include_Paths += -I$(LIB_RATS_INCLUDE_DIR) -I$(SGX_SSL)/include endif -Enclave_C_Flags := $(SGX_COMMON_CFLAGS) -nostdinc -fvisibility=hidden -fpie -fstack-protector $(Enclave_Include_Paths) -DWASM_GLOBAL_HEAP_SIZE=$(WAMR_BUILD_GLOBAL_HEAP_SIZE) -DWASM_ENABLE_GLOBAL_HEAP_POOL=$(WAMR_BUILD_GLOBAL_HEAP_POOL) -DWASM_ENABLE_LIB_RATS=$(WAMR_BUILD_LIB_RATS) -DWASM_ENABLE_STATIC_PGO=$(WAMR_BUILD_STATIC_PGO) +Enclave_C_Flags := $(SGX_COMMON_CFLAGS) -nostdinc -fvisibility=hidden \ + -fpie -fstack-protector $(Enclave_Include_Paths) \ + -DWASM_GLOBAL_HEAP_SIZE=$(WAMR_BUILD_GLOBAL_HEAP_SIZE) \ + -DWASM_ENABLE_GLOBAL_HEAP_POOL=$(WAMR_BUILD_GLOBAL_HEAP_POOL) \ + -DWASM_ENABLE_LIB_RATS=$(WAMR_BUILD_LIB_RATS) \ + -DWASM_ENABLE_STATIC_PGO=$(WAMR_BUILD_STATIC_PGO) \ + -DWASM_ENABLE_LIBC_WASI=$(WAMR_BUILD_LIBC_WASI) ifeq ($(SPEC_TEST), 1) Enclave_C_Flags += -DWASM_ENABLE_SPEC_TEST=1 else diff --git a/product-mini/platforms/linux-sgx/enclave-sample/Makefile_minimal b/product-mini/platforms/linux-sgx/enclave-sample/Makefile_minimal index a64d57744..07b640da2 100644 --- a/product-mini/platforms/linux-sgx/enclave-sample/Makefile_minimal +++ b/product-mini/platforms/linux-sgx/enclave-sample/Makefile_minimal @@ -102,7 +102,7 @@ Enclave_Include_Paths := -IEnclave -I$(WAMR_ROOT)/core/iwasm/include \ Enclave_C_Flags := $(SGX_COMMON_CFLAGS) -nostdinc -fvisibility=hidden -fpie -fstack-protector $(Enclave_Include_Paths) # disable wasi -Enclave_C_Flags += -DSGX_DISABLE_WASI +Enclave_C_Flags += -DWASM_ENABLE_LIBC_WASI=0 ifeq ($(SPEC_TEST), 1) Enclave_C_Flags += -DWASM_ENABLE_SPEC_TEST=1