Skip to content

Commit

Permalink
Simplify specification and use of the various memory configurations
Browse files Browse the repository at this point in the history
Several changes to make memory allocation cleaner:

1) enable build-time configuration of the memory configuration for the
eservice enclave (the pservice enclave does not have dynamic memory
requirements so they remain fixed).

2) a single variable now controls the allocation for enclave and
interpreter memory; PDO_MEMORY_CONFIG may be set to SMALL, MEDIUM, or
LARGE. this replaces the old WASM_MEM_CONFIG which was used for
interpreter memory configuration. The SMALL configuration sets up the
enclave and the interpreter for 4MB, MEDIUM is for 8MB and LARGE is
for 16MB.

3) remove the stack and heap configuration from the contract build
scripts; this appears to be unnecessary though further testing is
warranted.

Signed-off-by: Mic Bowman <mic.bowman@intel.com>
  • Loading branch information
cmickeyb authored and bvavala committed Jun 17, 2024
1 parent 214e656 commit 70bf620
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 92 deletions.
14 changes: 14 additions & 0 deletions build/cmake/ProjectVariables.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 29 additions & 3 deletions build/cmake/SGX.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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()
Expand Down
16 changes: 8 additions & 8 deletions build/common-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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]="
Expand Down
10 changes: 4 additions & 6 deletions build/tests/wawaka/memory-test.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down Expand Up @@ -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}",
Expand Down
46 changes: 25 additions & 21 deletions common/interpreter/wawaka_wasm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 "")
Expand Down Expand Up @@ -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)
Expand Down
33 changes: 0 additions & 33 deletions contracts/wawaka/contract-build.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
# ---------------------------------------------
Expand All @@ -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")

Expand Down
18 changes: 0 additions & 18 deletions contracts/wawaka/memory-test/test-small.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
7 changes: 4 additions & 3 deletions eservice/lib/libpdo_enclave/pdo_enclave.config.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ limitations under the License.
<EnclaveConfiguration>
<ProdID>0x90E7</ProdID>
<ISVSVN>1</ISVSVN>
<StackMaxSize>0x80000</StackMaxSize>
<HeapMaxSize>0x2000000</HeapMaxSize>
<ReservedMemMaxSize>0x100000</ReservedMemMaxSize>
<StackMaxSize>${ENCLAVE_STACK_SIZE}</StackMaxSize>
<HeapMaxSize>${ENCLAVE_HEAP_SIZE}</HeapMaxSize>
<ReservedMemMaxSize>${ENCLAVE_RESERVED_SIZE}</ReservedMemMaxSize>
<ReservedMemInitSize>${ENCLAVE_RESERVED_SIZE}</ReservedMemInitSize>
<ReservedMemExecutable>1</ReservedMemExecutable>
<TCSNum>2</TCSNum>
<TCSPolicy>1</TCSPolicy>
Expand Down

0 comments on commit 70bf620

Please sign in to comment.