diff --git a/build/cmake/ProjectVariables.cmake b/build/cmake/ProjectVariables.cmake index 5b393839..b12f5ef1 100644 --- a/build/cmake/ProjectVariables.cmake +++ b/build/cmake/ProjectVariables.cmake @@ -66,6 +66,20 @@ IF (NOT DEFINED ENV{PDO_SOURCE_ROOT}) ENDIF() SET(PDO_SOURCE_ROOT $ENV{PDO_SOURCE_ROOT}) +# The memory size option configures enclave and interpreter memory +# size values. The variable may have the value of "SMALL", "MEDIUM" or +# "LARGE". This is a project variable because the configurations +# depend on one another (the interpreter heap size must fit into the +# enclave heap, for example). +SET(PDO_MEMORY_CONFIG "MEDIUM" CACHE STRING "Set memory size parameters for enclave and interpreter") +IF (DEFINED ENV{PDO_MEMORY_CONFIG}) + SET(PDO_MEMORY_CONFIG $ENV{PDO_MEMORY_CONFIG}) +ENDIF() +SET(MEMORY_SIZE_OPTIONS "SMALL" "MEDIUM" "LARGE") +IF (NOT ${PDO_MEMORY_CONFIG} IN_LIST MEMORY_SIZE_OPTIONS) + MESSAGE(FATAL_ERROR "Invalid memory size; ${PDO_MEMORY_CONFIG}") +ENDIF() + # Get the current version using the get_version # utility; note that this will provide 0.0.0 as # the version if something goes wrong (like running diff --git a/build/cmake/SGX.cmake b/build/cmake/SGX.cmake index 74bc3967..73447aae 100644 --- a/build/cmake/SGX.cmake +++ b/build/cmake/SGX.cmake @@ -21,16 +21,41 @@ IF (NOT DEFINED ENV{PDO_SGX_KEY_ROOT}) ENDIF() SET(PDO_SGX_KEY_ROOT "$ENV{PDO_SGX_KEY_ROOT}") -IF (NOT DEFINED ENV{SGX_MODE}) - MESSAGE(FATAL_ERROR "SGX_MODE not defined") +# Memory size should be set in ProjectVariables.cmake which must be included +# before this file. There are three values for memory size: SMALL, MEDIUM +# and LARGE. Each provides defaults for memory allocation. See the note +# about memory size in ProjectVariables.cmake regarding dependencies between +# memory settings here and in other parts of PDO. +IF (NOT DEFINED PDO_MEMORY_CONFIG) + MESSAGE(FATAL_ERROR "PDO_MEMORY_CONFIG not defined") +ENDIF() + +IF (${PDO_MEMORY_CONFIG} STREQUAL "SMALL") + MATH(EXPR ENCLAVE_STACK_SIZE "2 * 1024 * 1024") + MATH(EXPR ENCLAVE_HEAP_SIZE "32 * 1024 * 1024") + MATH(EXPR ENCLAVE_RESERVED_SIZE "1 * 1024 * 1024") +ELSEIF (${PDO_MEMORY_CONFIG} STREQUAL "MEDIUM") + MATH(EXPR ENCLAVE_STACK_SIZE "2 * 1024 * 1024") + MATH(EXPR ENCLAVE_HEAP_SIZE "64 * 1024 * 1024") + MATH(EXPR ENCLAVE_RESERVED_SIZE "2 * 1024 * 1024") +ELSEIF (${PDO_MEMORY_CONFIG} STREQUAL "LARGE") + MATH(EXPR ENCLAVE_STACK_SIZE "2 * 1024 * 1024") + MATH(EXPR ENCLAVE_HEAP_SIZE "128 * 1024 * 1024") + MATH(EXPR ENCLAVE_RESERVED_SIZE "4 * 1024 * 1024") +ELSE() + MESSAGE(FATAL_ERROR "Invalid memory size; ${PDO_MEMORY_CONFIG}") ENDIF() -SET(SGX_MODE $ENV{SGX_MODE}) # There are effectively three build modes for SGX: # 1) SIM mode with PDO_DEBUG_BUILD enabled # 2) HW mode with PDO_DEBUG_BUILD enabled # 3) HW mode with PDO_DEBUG_BUILD disabled (release mode) # For now we just check the consistency of the variables (SGX_MODE, PDO_DEBUG_BUILD and CMAKE_BUIDL_TYPE) +IF (NOT DEFINED ENV{SGX_MODE}) + MESSAGE(FATAL_ERROR "SGX_MODE not defined") +ENDIF() +SET(SGX_MODE $ENV{SGX_MODE}) + IF (${SGX_MODE} STREQUAL "SIM") IF (NOT ${PDO_DEBUG_BUILD}) MESSAGE(FATAL_ERROR "SGX_MODE=SIM does not accept PDO_DEBUG_BUILD=0") @@ -47,6 +72,7 @@ ELSE() SET(SGX_USE_SIMULATOR FALSE) ENDIF() +# These environment variables are generally set through the SGX SDK IF (NOT DEFINED ENV{SGX_SDK}) MESSAGE(FATAL_ERROR "SGX_SDK not defined") ENDIF() diff --git a/build/common-config.sh b/build/common-config.sh index eeddb520..5c8873cd 100755 --- a/build/common-config.sh +++ b/build/common-config.sh @@ -34,19 +34,19 @@ var_set() { " env_key_sort[$i]="WASM_SRC"; i=$i+1; export WASM_SRC=${env_val[WASM_SRC]}; - env_val[WASM_MEM_CONFIG]="${WASM_MEM_CONFIG:-MEDIUM}" - env_desc[WASM_MEM_CONFIG]=" - WASM_MEM_CONFIG indicates the memory configuration for the - WASM runtime: the runtime's global memory pool size, + env_val[PDO_MEMORY_CONFIG]="${PDO_MEMORY_CONFIG:-MEDIUM}" + env_desc[PDO_MEMORY_CONFIG]=" + PDO_MEMORY_CONFIG indicates the memory configuration for the + enclave and WASM runtime: the runtime's global memory pool size, and a module's operand stack and heap size. When the variable is set to 'SMALL', the runtime's memory pool - size is set to 1MB. If the variable is set to 'MEDIUM', the - runtime's memory pool size is set to 2MB. + size is set to 4MB. If the variable is set to 'MEDIUM', the + runtime's memory pool size is set to 8MB. When the variable is set to 'LARGE', the runtime's - memory pool size is set to 4MB. See + memory pool size is set to 16MB. See common/interpreter/wawaka_wasm/README.md for further details. " - env_key_sort[$i]="WASM_MEM_CONFIG"; i=$i+1; export WASM_MEM_CONFIG=${env_val[WASM_MEM_CONFIG]}; + env_key_sort[$i]="PDO_MEMORY_CONFIG"; i=$i+1; export PDO_MEMORY_CONFIG=${env_val[PDO_MEMORY_CONFIG]}; env_val[PDO_INTERPRETER]="${PDO_INTERPRETER:-wawaka}" env_desc[PDO_INTERPRETER]=" diff --git a/build/tests/wawaka/memory-test.json b/build/tests/wawaka/memory-test.json index 4c885a6f..86ac26db 100644 --- a/build/tests/wawaka/memory-test.json +++ b/build/tests/wawaka/memory-test.json @@ -140,13 +140,12 @@ "expected": "1500000" }, { - "description" : "big value test 2 MB should {invert} with WAMR \"out of memory\" exception", + "description" : "big value test 2 MB", "MethodName": "big_value_test", "KeywordParameters": { "num_chars" : 2000000 }, - "invert": "fail", - "expected": "internal pdo error" + "expected": "2000000" }, { "description" : "deep recursion test {KeywordParameters}", @@ -197,13 +196,12 @@ "expected": "64000" }, { - "description" : "big key test 1 MB should {invert} with WAMR \"out of memory\" exception", + "description" : "big key test 1 MB", "MethodName": "big_key_test", "KeywordParameters": { "num_chars" : 1000000 }, - "invert": "fail", - "expected":"internal pdo error" + "expected":"1000000" }, { "description" : "deep recursion test {KeywordParameters}", diff --git a/common/interpreter/wawaka_wasm/CMakeLists.txt b/common/interpreter/wawaka_wasm/CMakeLists.txt index b4fc3e4d..1b847638 100644 --- a/common/interpreter/wawaka_wasm/CMakeLists.txt +++ b/common/interpreter/wawaka_wasm/CMakeLists.txt @@ -31,11 +31,6 @@ IF (SUBMOD_CONTENTS EQUAL 0) MESSAGE(FATAL_ERROR "WAMR git submodule has not been cloned. Please run `git submodule update --init` first.") ENDIF() -IF (NOT DEFINED ENV{WASM_MEM_CONFIG}) - MESSAGE(FATAL_ERROR "WASM_MEM_CONFIG environment variable not defined!") -ENDIF() -SET(WASM_MEM_CONFIG "$ENV{WASM_MEM_CONFIG}") - # Reset default linker flags SET (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") SET (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "") @@ -164,22 +159,31 @@ SGX_PREPARE_TRUSTED(${WAWAKA_STATIC_NAME}) # HEAP_SIZE: Size of the runtime's heap for dynamic allocations by a WASM module. # STACK_SIZE: Size of the runtime's stack for executing a WASM module # Layout: RUNTIME_MEM_POOL_SIZE > HEAP_SIZE + STACK_SIZE + padding -IF (WASM_MEM_CONFIG STREQUAL "SMALL") - MESSAGE(STATUS "Using SMALL memory configuration") - TARGET_COMPILE_DEFINITIONS(${WAWAKA_STATIC_NAME} PRIVATE RUNTIME_MEM_POOL_SIZE=1*1024*1024) - TARGET_COMPILE_DEFINITIONS(${WAWAKA_STATIC_NAME} PRIVATE HEAP_SIZE=512*1024) - TARGET_COMPILE_DEFINITIONS(${WAWAKA_STATIC_NAME} PRIVATE STACK_SIZE=64*1024) -ELSEIF (WASM_MEM_CONFIG STREQUAL "LARGE") - MESSAGE(STATUS "Using LARGE memory configuration") - TARGET_COMPILE_DEFINITIONS(${WAWAKA_STATIC_NAME} PRIVATE RUNTIME_MEM_POOL_SIZE=4*1024*1024) - TARGET_COMPILE_DEFINITIONS(${WAWAKA_STATIC_NAME} PRIVATE HEAP_SIZE=3*1024*1024) - TARGET_COMPILE_DEFINITIONS(${WAWAKA_STATIC_NAME} PRIVATE STACK_SIZE=256*1024) -ELSE () - MESSAGE(STATUS "Using MEDIUM memory configuration") - TARGET_COMPILE_DEFINITIONS(${WAWAKA_STATIC_NAME} PRIVATE RUNTIME_MEM_POOL_SIZE=2*1024*1024) - TARGET_COMPILE_DEFINITIONS(${WAWAKA_STATIC_NAME} PRIVATE HEAP_SIZE=1536*1024) - TARGET_COMPILE_DEFINITIONS(${WAWAKA_STATIC_NAME} PRIVATE STACK_SIZE=128*1024) -ENDIF () +# The numbers below were chosen to set RUNTIME_MEM_POOL_SIZE to be about +# 1/8 of the size of the enclave heap size defined in the SGX.cmake file. +IF (NOT DEFINED PDO_MEMORY_CONFIG) + MESSAGE(FATAL_ERROR "PDO_MEMORY_CONFIG not defined") +ENDIF() + +IF (${PDO_MEMORY_CONFIG} STREQUAL "SMALL") + MATH(EXPR WW_RUNTIME_MEM_POOL_SIZE "4 * 1024 * 1024") + MATH(EXPR WW_STACK_SIZE "512 * 1024") + MATH(EXPR WW_HEAP_SIZE "3 * 1024 * 1024") +ELSEIF (${PDO_MEMORY_CONFIG} STREQUAL "MEDIUM") + MATH(EXPR WW_RUNTIME_MEM_POOL_SIZE "8 * 1024 * 1024") + MATH(EXPR WW_STACK_SIZE "512 * 1024") + MATH(EXPR WW_HEAP_SIZE "7 * 1024 * 1024") +ELSEIF (${PDO_MEMORY_CONFIG} STREQUAL "LARGE") + MATH(EXPR WW_RUNTIME_MEM_POOL_SIZE "16 * 1024 * 1024") + MATH(EXPR WW_STACK_SIZE "512 * 1024") + MATH(EXPR WW_HEAP_SIZE "15 * 1024 * 1024") +ELSE() + MESSAGE(FATAL_ERROR "Invalid memory size; ${PDO_MEMORY_CONFIG}") +ENDIF() + +TARGET_COMPILE_DEFINITIONS(${WAWAKA_STATIC_NAME} PRIVATE RUNTIME_MEM_POOL_SIZE=${WW_RUNTIME_MEM_POOL_SIZE}) +TARGET_COMPILE_DEFINITIONS(${WAWAKA_STATIC_NAME} PRIVATE HEAP_SIZE=${WW_HEAP_SIZE}) +TARGET_COMPILE_DEFINITIONS(${WAWAKA_STATIC_NAME} PRIVATE STACK_SIZE=${WW_STACK_SIZE}) TARGET_INCLUDE_DIRECTORIES(${WAWAKA_STATIC_NAME} PRIVATE ${INTERPRETER_INCLUDE_DIRS}) TARGET_INCLUDE_DIRECTORIES(${WAWAKA_STATIC_NAME} PRIVATE ${IWASM_DIR}/include) diff --git a/contracts/wawaka/contract-build.cmake b/contracts/wawaka/contract-build.cmake index 2f96d13f..843bd48d 100644 --- a/contracts/wawaka/contract-build.cmake +++ b/contracts/wawaka/contract-build.cmake @@ -28,40 +28,11 @@ IF (NOT DEFINED ENV{WASM_SRC}) ENDIF() SET(WASM_SRC "$ENV{WASM_SRC}") -IF (NOT DEFINED ENV{WASM_MEM_CONFIG}) - MESSAGE(FATAL_ERROR "WASM_MEM_CONFIG environment variable not defined!") -ENDIF() -SET(WASM_MEM_CONFIG "$ENV{WASM_MEM_CONFIG}") - # this should be set by the WAMR toolchain file IF (NOT DEFINED WASI_SDK_DIR) MESSAGE(FATAL_ERROR "WASM_SDK_DIR was not defined, check toolchain defines") ENDIF() -# --------------------------------------------- -# Set up the memory configuration -# --------------------------------------------- - -# LINEAR_MEMORY: Maximum size for a WASM module's linear memory (module's -# internal stack + static globals + padding); needs to be multiple of 64KB - -# INTERNAL_STACK_SIZE: Size of a WASM module's internal data stack -# (part of LINEAR_MEMORY) - -IF (WASM_MEM_CONFIG STREQUAL "SMALL") - SET(INTERNAL_STACK_SIZE 24576) - SET(LINEAR_MEMORY 65536) - message(STATUS "Building contracts for SMALL memory configuration") -ELSEIF (WASM_MEM_CONFIG STREQUAL "LARGE") - SET(INTERNAL_STACK_SIZE 98304) - SET(LINEAR_MEMORY 262144) - message(STATUS "Building contracts for LARGE memory configuration") -ELSE() - SET(INTERNAL_STACK_SIZE 49152) - SET(LINEAR_MEMORY 131072) - message(STATUS "Building contracts for MEDIUM memory configuration") -ENDIF () - # --------------------------------------------- # Set up the compiler configuration # --------------------------------------------- @@ -80,11 +51,7 @@ LIST(APPEND WASM_BUILD_OPTIONS "-std=c++11") LIST(APPEND WASM_BUILD_OPTIONS "-DUSE_WASI_SDK=1") SET(WASM_LINK_OPTIONS) -LIST(APPEND WASM_LINK_OPTIONS "-Wl,--initial-memory=${LINEAR_MEMORY}") -LIST(APPEND WASM_LINK_OPTIONS "-Wl,--max-memory=${LINEAR_MEMORY}") -LIST(APPEND WASM_LINK_OPTIONS "-z stack-size=${INTERNAL_STACK_SIZE}") LIST(APPEND WASM_LINK_OPTIONS "-Wl,--allow-undefined") - LIST(APPEND WASM_LINK_OPTIONS "-Wl,--export=ww_dispatch") LIST(APPEND WASM_LINK_OPTIONS "-Wl,--export=ww_initialize") diff --git a/contracts/wawaka/memory-test/test-small.json b/contracts/wawaka/memory-test/test-small.json index 460d4fb8..57844008 100644 --- a/contracts/wawaka/memory-test/test-small.json +++ b/contracts/wawaka/memory-test/test-small.json @@ -31,15 +31,6 @@ }, "expected": "512000" }, - { - "description" : "big value test 521 KB should {invert} with WAMR \"out of memory\" exception", - "MethodName": "big_value_test", - "KeywordParameters": { - "num_chars" : 521000 - }, - "invert": "fail", - "expected": "internal pdo error" - }, { "description" : "deep recursion test {KeywordParameters}", "MethodName": "deep_recursion_test", @@ -64,15 +55,6 @@ }, "expected": "256000" }, - { - "description" : "big key test 300 KB should {invert} with WAMR \"out of memory\" exception", - "MethodName": "big_key_test", - "KeywordParameters": { - "num_chars" : 300000 - }, - "invert": "fail", - "expected": "internal pdo error" - }, { "description" : "deep recursion test {KeywordParameters}", "MethodName": "deep_recursion_test", diff --git a/eservice/lib/libpdo_enclave/pdo_enclave.config.xml.in b/eservice/lib/libpdo_enclave/pdo_enclave.config.xml.in index 335927b8..2bdac3f1 100644 --- a/eservice/lib/libpdo_enclave/pdo_enclave.config.xml.in +++ b/eservice/lib/libpdo_enclave/pdo_enclave.config.xml.in @@ -17,9 +17,10 @@ limitations under the License. 0x90E7 1 - 0x80000 - 0x2000000 - 0x100000 + ${ENCLAVE_STACK_SIZE} + ${ENCLAVE_HEAP_SIZE} + ${ENCLAVE_RESERVED_SIZE} + ${ENCLAVE_RESERVED_SIZE} 1 2 1