Skip to content

Commit a9d0331

Browse files
committed
Update Wawaka interpreter for WAMR 2.4.1
- Refactored `CMakeLists.txt` to support WAMR integration and updated memory configuration. - Added `wamr` directory with patching scripts and custom build configurations. Copied the linux-sgx platform cmake build script from wamr, modified to apply a set of patches which include zero-ing SGX extended memory on deallocation. - Introduced `contract_abort_wrapper` and improved exception handling in `WasmExtensions.cpp`. Removed operations that are now supported directly by WAMR libc. - Replaced `RegisterNativeFunctions` with `InitializeNativeSymbols` for runtime initialization. This incorporates native function initialization into the single runtime initialization. - Update `WawakaInterpreter` for wamr 2.4.1. Class variables now conform to standard usage. Improved initialization and cleanup (e.g. to release static heap memory). In general, improved checks on memory usage. - Removed redundant `__wasm_call_ctors` calls in `Dispatch.cpp`. - Updated `contract-build.cmake` to export additional symbols for WAMR compatibility. Specifically, exporting malloc and free symbols enables wamr to adapt memory usage more easily. Signed-off-by: Mic Bowman <[email protected]>
1 parent 0b06893 commit a9d0331

File tree

12 files changed

+516
-289
lines changed

12 files changed

+516
-289
lines changed

common/interpreter/wawaka_wasm/CMakeLists.txt

Lines changed: 65 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -22,132 +22,89 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.10 FATAL_ERROR)
2222
IF (NOT DEFINED ENV{WASM_SRC})
2323
MESSAGE(FATAL_ERROR "WASM_SRC environment variable not defined!")
2424
ENDIF()
25-
SET(WASM_SRC "$ENV{WASM_SRC}")
25+
SET (WAMR_ROOT_DIR "$ENV{WASM_SRC}")
2626

2727
# Make sure wasm-micro-runtime submodule has been cloned
28-
FILE(GLOB WAMR_SUBMOD ${WASM_SRC})
28+
FILE(GLOB WAMR_SUBMOD ${WAMR_ROOT_DIR})
2929
LIST(LENGTH WAMR_SUBMOD SUBMOD_CONTENTS)
3030
IF (SUBMOD_CONTENTS EQUAL 0)
3131
MESSAGE(FATAL_ERROR "WAMR git submodule has not been cloned. Please run `git submodule update --init` first.")
3232
ENDIF()
3333

34-
# Reset default linker flags
35-
SET (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
36-
SET (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
37-
38-
# Set WAMR_BUILD_TARGET
39-
IF (NOT DEFINED WAMR_BUILD_TARGET)
40-
IF (CMAKE_SIZEOF_VOID_P EQUAL 8)
41-
# Build as X86_64 by default in 64-bit platform
42-
SET (WAMR_BUILD_TARGET "X86_64")
43-
ELSE ()
44-
# Build as X86_32 by default in 32-bit platform
45-
SET (WAMR_BUILD_TARGET "X86_32")
46-
ENDIF ()
47-
ENDIF ()
48-
49-
IF (NOT CMAKE_BUILD_TYPE)
50-
SET(CMAKE_BUILD_TYPE Release)
34+
# Memory size should be set in Memory.cmake which must be included
35+
# before this file. There are three values for memory size: SMALL, MEDIUM
36+
# and LARGE. Each provides defaults for memory allocation. See the note
37+
# about memory size in Memory.cmake regarding dependencies between
38+
# memory settings here and in other parts of PDO.
39+
IF (NOT DEFINED PDO_MEMORY_CONFIG)
40+
MESSAGE(FATAL_ERROR "PDO_MEMORY_CONFIG not defined")
5141
ENDIF()
5242

53-
# Disable AoT by default
43+
#################################################################
44+
# WAMR Library
45+
#
46+
# The WAMR library is built using a slightly modified version
47+
# of the build script found in product-mini/platforms/linux-sgx/
48+
#################################################################
49+
50+
# Set up the WAMR build configuration for use with Wawaka
5451
SET (WAMR_BUILD_AOT 0)
52+
SET (WAMR_BUILD_JIT 0)
5553
SET (WAMR_BUILD_INTERP 1)
54+
55+
# Set the interpreter type
5656
IF (PDO_INTERPRETER STREQUAL "wawaka-opt")
5757
SET (WAMR_BUILD_FAST_INTERP 1)
5858
ADD_DEFINITIONS (-DUSE_WAWAKA_OPT=1)
5959
MESSAGE(STATUS "Building wawaka in optimized INTERP mode")
6060
ELSE()
61-
# Disable optimized interpreter by default
6261
SET (WAMR_BUILD_FAST_INTERP 0)
63-
MESSAGE(STATUS "Building wawaka in INTERP mode")
62+
ADD_DEFINITIONS (-DUSE_WAWAKA_OPT=0)
63+
MESSAGE(STATUS "Building wawaka in regular INTERP mode")
6464
ENDIF()
6565

66-
# Disable JIT by default for all runtime modes.
67-
SET (WAMR_BUILD_JIT 0)
68-
69-
IF (NOT DEFINED WAMR_BUILD_LIBC_BUILTIN)
70-
# Enable libc builtin support by default
71-
SET (WAMR_BUILD_LIBC_BUILTIN 1)
72-
ENDIF ()
66+
# Build LIBC support but we do not want any of the
67+
# WASI system calls or pthread support
68+
SET (WAMR_BUILD_LIBC_BUILTIN 1)
69+
SET (WAMR_BUILD_LIBC_WASI 0)
70+
SET (WAMR_BUILD_LIB_PTHREAD 0)
7371

74-
IF (NOT DEFINED WAMR_BUILD_LIBC_WASI)
75-
# Disable libc wasi support by default
76-
SET (WAMR_BUILD_LIBC_WASI 0)
77-
ENDIF ()
78-
79-
################################################################################
72+
# No need for multi-module support
73+
SET (WAMR_BUILD_MULTI_MODULE 0)
8074

81-
SET(IWASM_STATIC_NAME iwasm)
82-
SET(WAWAKA_STATIC_NAME wwasm)
83-
PROJECT(wawaka_wasm C CXX)
75+
# WASI/clang compiles references so we need
76+
# to ensure WAMR supports them
77+
SET (WAMR_BUILD_REF_TYPES 1)
8478

85-
SET (WAMR_BUILD_PLATFORM "linux-sgx")
79+
# We do not want heap sharing support between modules
80+
SET (WASM_ENABLE_SHARED_HEAP 0)
8681

87-
SET (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
88-
SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -ffunction-sections -fdata-sections \
89-
-Wall -Wno-unused-parameter -Wno-pedantic \
90-
-nostdinc -fvisibility=hidden -fpie" )
82+
# --------------- Other flags that are not required ---------------
9183

92-
SET (WAMR_ROOT_DIR ${WASM_SRC})
93-
SET (SHARED_DIR ${WAMR_ROOT_DIR}/core/shared)
94-
SET (IWASM_DIR ${WAMR_ROOT_DIR}/core/iwasm)
95-
SET (APP_FRAMEWORK_DIR ${WAMR_ROOT_DIR}/core/app-framework)
84+
# In theory this should remove the mmap calls, in practice it doesn't
85+
# appear to work that way.
86+
# SET(WAMR_DISABLE_STACK_HW_BOUND_CHECK 1)
87+
# SET(WAMR_DISABLE_HW_BOUND_CHECK 1)
9688

97-
ENABLE_LANGUAGE (ASM)
89+
# This is used to distinguish between pre-allocated buffers and WAMR
90+
# managed dynamic allocators in the application. It is not needed.
91+
# SET (WASM_ENABLE_GLOBAL_HEAP_POOL 0)
9892

99-
#################################################################
100-
# WAMR Library
101-
#################################################################
102-
# include the build config template file
103-
INCLUDE (${WAMR_ROOT_DIR}/build-scripts/config_common.cmake)
104-
105-
INCLUDE (${SHARED_DIR}/platform/${WAMR_BUILD_PLATFORM}/shared_platform.cmake)
106-
INCLUDE (${SHARED_DIR}/mem-alloc/mem_alloc.cmake)
107-
INCLUDE (${SHARED_DIR}/utils/shared_utils.cmake)
108-
109-
# this picks up the libc_erro.h, this appears to be an error in WAMR in that the
110-
# test for WASI is AFTER the attempt to include a file that is only available when
111-
# WASI is turned on.
112-
INCLUDE (${SHARED_DIR}/platform/common/libc-util/platform_common_libc_util.cmake)
113-
114-
IF (WAMR_BUILD_LIBC_BUILTIN EQUAL 1)
115-
INCLUDE (${IWASM_DIR}/libraries/libc-builtin/libc_builtin.cmake)
116-
ENDIF ()
117-
IF (WAMR_BUILD_LIBC_WASI EQUAL 1)
118-
INCLUDE (${IWASM_DIR}/libraries/libc-wasi/libc_wasi.cmake)
119-
ENDIF ()
120-
121-
INCLUDE (${IWASM_DIR}/common/iwasm_common.cmake)
122-
123-
IF (WAMR_BUILD_INTERP EQUAL 1 OR WAMR_BUILD_JIT EQUAL 1)
124-
INCLUDE (${IWASM_DIR}/interpreter/iwasm_interp.cmake)
125-
ENDIF ()
126-
127-
ADD_LIBRARY (${IWASM_STATIC_NAME}
128-
${PLATFORM_SHARED_SOURCE}
129-
${PLATFORM_COMMON_LIBC_UTIL_SOURCE}
130-
${MEM_ALLOC_SHARED_SOURCE}
131-
${UTILS_SHARED_SOURCE}
132-
${LIBC_BUILTIN_SOURCE}
133-
${LIBC_WASI_SOURCE}
134-
${WAMR_POSIX_SOURCES}
135-
${IWASM_COMMON_SOURCE}
136-
${IWASM_INTERP_SOURCE}
137-
${IWASM_COMPL_SOURCE}
138-
# this is necessary because WAMR currently does not have a definition
139-
# for os_is_handle_valid in the sgx_platform.c file
140-
${CMAKE_CURRENT_SOURCE_DIR}/wamr_fixes.c
141-
)
142-
143-
TARGET_INCLUDE_DIRECTORIES(${IWASM_STATIC_NAME} PUBLIC ${SHARED_DIR}/include)
144-
TARGET_INCLUDE_DIRECTORIES(${IWASM_STATIC_NAME} PUBLIC ${IWASM_DIR}/include)
93+
SET(IWASM_STATIC_NAME iwasm)
94+
INCLUDE (${CMAKE_CURRENT_SOURCE_DIR}/wamr/wamr.cmake)
14595

14696
#################################################################
14797
# Wawaka Interpreter
14898
#################################################################
99+
PROJECT(wawaka_wasm C CXX)
100+
101+
SET(WAWAKA_STATIC_NAME wwasm)
102+
103+
SET (SHARED_DIR ${WAMR_ROOT_DIR}/core/shared)
104+
SET (IWASM_DIR ${WAMR_ROOT_DIR}/core/iwasm)
105+
149106
FILE(GLOB WWASM_PROJECT_HEADERS *.h)
150-
FILE(GLOB WWASM_PROJECT_SOURCES *.cpp)
107+
FILE(GLOB WWASM_PROJECT_SOURCES *.cpp wamr/*.c)
151108

152109
ADD_LIBRARY(${WAWAKA_STATIC_NAME}
153110
${WWASM_PROJECT_HEADERS}
@@ -158,32 +115,22 @@ SGX_PREPARE_TRUSTED(${WAWAKA_STATIC_NAME})
158115
# RUNTIME_MEM_POOL_SIZE: The WASM runtime's global memory pool size
159116
# HEAP_SIZE: Size of the runtime's heap for dynamic allocations by a WASM module.
160117
# STACK_SIZE: Size of the runtime's stack for executing a WASM module
161-
# Layout: RUNTIME_MEM_POOL_SIZE > HEAP_SIZE + STACK_SIZE + padding
162-
# The numbers below were chosen to set RUNTIME_MEM_POOL_SIZE to be about
163-
# 1/8 of the size of the enclave heap size defined in the SGX.cmake file.
164-
IF (NOT DEFINED PDO_MEMORY_CONFIG)
165-
MESSAGE(FATAL_ERROR "PDO_MEMORY_CONFIG not defined")
166-
ENDIF()
118+
SET (WW_STACK_SIZE ${ENCLAVE_WORKER_STACK})
119+
SET (WW_HEAP_SIZE ${ENCLAVE_WORKER_HEAP})
167120

168-
IF (${PDO_MEMORY_CONFIG} STREQUAL "SMALL")
169-
MATH(EXPR WW_RUNTIME_MEM_POOL_SIZE "4 * 1024 * 1024")
170-
MATH(EXPR WW_STACK_SIZE "512 * 1024")
171-
MATH(EXPR WW_HEAP_SIZE "3 * 1024 * 1024")
172-
ELSEIF (${PDO_MEMORY_CONFIG} STREQUAL "MEDIUM")
173-
MATH(EXPR WW_RUNTIME_MEM_POOL_SIZE "8 * 1024 * 1024")
174-
MATH(EXPR WW_STACK_SIZE "512 * 1024")
175-
MATH(EXPR WW_HEAP_SIZE "7 * 1024 * 1024")
176-
ELSEIF (${PDO_MEMORY_CONFIG} STREQUAL "LARGE")
177-
MATH(EXPR WW_RUNTIME_MEM_POOL_SIZE "16 * 1024 * 1024")
178-
MATH(EXPR WW_STACK_SIZE "512 * 1024")
179-
MATH(EXPR WW_HEAP_SIZE "15 * 1024 * 1024")
180-
ELSE()
181-
MESSAGE(FATAL_ERROR "Invalid memory size; ${PDO_MEMORY_CONFIG}")
121+
# MEM_POOL_SIZE determined from the stack and heap size plus padding, padding
122+
# factor is just a multiplier that can be adjusted to increase the padding
123+
MATH(EXPR WW_RUNTIME_MEM_POOL_SIZE "${WW_STACK_SIZE} + ${WW_HEAP_SIZE}")
124+
125+
# Check that the runtime memory pool size is not larger than the enclave heap size
126+
IF (WW_RUNTIME_MEM_POOL_SIZE GREATER ENCLAVE_HEAP_SIZE)
127+
MESSAGE(FATAL_ERROR "Invalid memory size; RUNTIME_MEM_POOL_SIZE too big for the enclave heap size")
182128
ENDIF()
183129

184-
TARGET_COMPILE_DEFINITIONS(${WAWAKA_STATIC_NAME} PRIVATE RUNTIME_MEM_POOL_SIZE=${WW_RUNTIME_MEM_POOL_SIZE})
185-
TARGET_COMPILE_DEFINITIONS(${WAWAKA_STATIC_NAME} PRIVATE HEAP_SIZE=${WW_HEAP_SIZE})
186-
TARGET_COMPILE_DEFINITIONS(${WAWAKA_STATIC_NAME} PRIVATE STACK_SIZE=${WW_STACK_SIZE})
130+
TARGET_COMPILE_DEFINITIONS(${WAWAKA_STATIC_NAME} PRIVATE MAXIMUM_SIZE=${CONTRACT_MAXIMUM_SIZE})
131+
TARGET_COMPILE_DEFINITIONS(${WAWAKA_STATIC_NAME} PRIVATE GLOBAL_HEAP_SIZE=${CONTRACT_GLOBAL_HEAP_SIZE})
132+
TARGET_COMPILE_DEFINITIONS(${WAWAKA_STATIC_NAME} PRIVATE HEAP_SIZE=${CONTRACT_HEAP_SIZE})
133+
TARGET_COMPILE_DEFINITIONS(${WAWAKA_STATIC_NAME} PRIVATE STACK_SIZE=${CONTRACT_STACK_SIZE})
187134

188135
TARGET_INCLUDE_DIRECTORIES(${WAWAKA_STATIC_NAME} PRIVATE ${INTERPRETER_INCLUDE_DIRS})
189136
TARGET_INCLUDE_DIRECTORIES(${WAWAKA_STATIC_NAME} PRIVATE ${IWASM_DIR}/include)

common/interpreter/wawaka_wasm/WasmExtensions.cpp

Lines changed: 20 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -37,36 +37,6 @@
3737

3838
namespace pe = pdo::error;
3939

40-
/* ----------------------------------------------------------------- *
41-
* NAME: memchr
42-
* ----------------------------------------------------------------- */
43-
extern "C" int32 memchr_wrapper(
44-
wasm_exec_env_t exec_env,
45-
int32 src_offset,
46-
int32 ch,
47-
uint32 src_size)
48-
{
49-
wasm_module_inst_t module_inst = wasm_runtime_get_module_inst(exec_env);
50-
try {
51-
if (src_size == 0)
52-
return 0;
53-
54-
void *src = get_buffer(module_inst, src_offset, src_size);
55-
if (src == NULL)
56-
return 0;
57-
58-
void *ptr = memchr(src, ch, src_size);
59-
if (ptr == NULL)
60-
return 0;
61-
62-
return wasm_runtime_addr_native_to_app(module_inst, ptr);
63-
}
64-
catch (...) {
65-
SAFE_LOG(PDO_LOG_ERROR, "unexpected failure in %s", __FUNCTION__);
66-
return false;
67-
}
68-
}
69-
7040
/* ----------------------------------------------------------------- *
7141
* NAME: _contract_log_wrapper
7242
* ----------------------------------------------------------------- */
@@ -86,6 +56,17 @@ extern "C" bool contract_log_wrapper(
8656
}
8757
}
8858

59+
/* ----------------------------------------------------------------- *
60+
* NAME: _contract_abort_wrapper
61+
* ----------------------------------------------------------------- */
62+
extern "C" void contract_abort_wrapper(
63+
wasm_exec_env_t exec_env,
64+
const char* buffer)
65+
{
66+
wasm_module_inst_t module_inst = get_module_inst(exec_env);
67+
wasm_runtime_set_exception(module_inst, buffer);
68+
}
69+
8970
/* ----------------------------------------------------------------- *
9071
* NAME: simple_hash
9172
* ----------------------------------------------------------------- */
@@ -137,7 +118,8 @@ extern "C" double strtod_wrapper(
137118

138119
extern "C" void abort_wrapper(wasm_exec_env_t exec_env)
139120
{
140-
abort();
121+
wasm_module_inst_t module_inst = get_module_inst(exec_env);
122+
wasm_runtime_set_exception(module_inst, "env.abort()");
141123
}
142124

143125
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
@@ -154,20 +136,13 @@ WASM_PASSTHRU_FUNCTION(islower)
154136
WASM_PASSTHRU_FUNCTION(ispunct)
155137
WASM_PASSTHRU_FUNCTION(isblank)
156138

157-
#if 0
158-
WASM_PASSTHRU_FUNCTION(isascii)
159-
#endif
160-
161139
static NativeSymbol native_symbols[] =
162140
{
163141
/* Missing libc functions */
164142
EXPORT_WASM_API_WITH_SIG2(iscntrl,"(i)i"),
165143
EXPORT_WASM_API_WITH_SIG2(islower,"(i)i"),
166144
EXPORT_WASM_API_WITH_SIG2(ispunct,"(i)i"),
167145
EXPORT_WASM_API_WITH_SIG2(isblank,"(i)i"),
168-
#if 0
169-
EXPORT_WASM_API_WITH_SIG2(isascii,"(i)i"),
170-
#endif
171146
EXPORT_WASM_API_WITH_SIG2(abort,"()"),
172147

173148
/* Crypto operations from WasmCryptoExtensions.h */
@@ -204,32 +179,24 @@ static NativeSymbol native_symbols[] =
204179
EXPORT_WASM_API_WITH_SIG2(key_value_open,"(*~*~)i"),
205180
EXPORT_WASM_API_WITH_SIG2(key_value_finalize,"(iii)i"),
206181

207-
208182
/* Utility functions */
209183
EXPORT_WASM_API_WITH_SIG2(contract_log, "(i$)i"),
184+
EXPORT_WASM_API_WITH_SIG2(contract_abort, "($)"),
210185
EXPORT_WASM_API_WITH_SIG2(simple_hash, "(*~)i"),
211-
EXPORT_WASM_API_WITH_SIG2(memchr, "(iii)i"),
212186
EXPORT_WASM_API_WITH_SIG2(strtod, "($*)F"),
213187
};
214188

215189
#ifdef __cplusplus
216190
}
217191
#endif
218192

219-
bool RegisterNativeFunctions(void)
193+
bool InitializeNativeSymbols(RuntimeInitArgs& init_args)
220194
{
221-
try {
222-
size_t native_symbols_count = sizeof(native_symbols)/sizeof(NativeSymbol);
223-
if (! wasm_runtime_register_natives("env", native_symbols, native_symbols_count))
224-
{
225-
SAFE_LOG(PDO_LOG_ERROR, "failed to register native functions");
226-
return false;
227-
}
228-
}
229-
catch (...) {
230-
SAFE_LOG(PDO_LOG_ERROR, "exception throw while registering native functions");
231-
return false;
232-
}
195+
size_t native_symbols_count = sizeof(native_symbols)/sizeof(NativeSymbol);
196+
197+
init_args.native_module_name = "env";
198+
init_args.n_native_symbols = native_symbols_count;
199+
init_args.native_symbols = native_symbols;
233200

234201
return true;
235202
}

common/interpreter/wawaka_wasm/WasmExtensions.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,22 @@ bool contract_log(
232232
const uint32_t loglevel,
233233
const char *buffer);
234234

235+
void contract_abort(
236+
const char* buffer);
237+
235238
int simple_hash(uint8_t *buffer, const size_t buflen);
236239

237240
#ifdef __cplusplus
238241
}
239242
#endif
240243

244+
#define CONTRACT_SAFE_ABORT(FMT, ...) \
245+
{ \
246+
char buf[512]; \
247+
snprintf(buf, 512, FMT, ##__VA_ARGS__); \
248+
contract_abort(buf); \
249+
}
250+
241251
#define CONTRACT_SAFE_LOG(LEVEL, FMT, ...) \
242252
{ \
243253
char buf[512]; \

0 commit comments

Comments
 (0)