Skip to content

Commit 042475a

Browse files
authored
Protect memory layout (#113)
* libfaasm: add dummy function to preserve memory layout * gh: bump code version to have a new libfaasm installed in the sysroot * libfaasm: make include more reliable by being included in core.h * mpi: debug funcs * faasmtools: remove unnecessary initial memory, wasm-ld should pick up the optimal value * mpi: revert debugging changes in mpi functions * faasmtools: export malloc and free symbols for wamr's memory management * nit: remove unnecessary whitespace * libfaasm: remove __wasm__ conditional inclusion * libfaasm: reintroduce __wasm__ * wasi-libc: disable memory growth using mmap
1 parent b556c29 commit 042475a

File tree

8 files changed

+27
-6
lines changed

8 files changed

+27
-6
lines changed

.env

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
SYSROOT_VERSION=0.2.4
2-
SYSROOT_CLI_IMAGE=faasm.azurecr.io/cpp-sysroot:0.2.4
1+
SYSROOT_VERSION=0.2.5
2+
SYSROOT_CLI_IMAGE=faasm.azurecr.io/cpp-sysroot:0.2.5
33
COMPOSE_PROJECT_NAME=cpp-dev

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
if: github.event.pull_request.draft == false
2020
runs-on: ubuntu-latest
2121
container:
22-
image: faasm.azurecr.io/cpp-sysroot:0.2.4
22+
image: faasm.azurecr.io/cpp-sysroot:0.2.5
2323
credentials:
2424
username: ${{ secrets.ACR_SERVICE_PRINCIPAL_ID }}
2525
password: ${{ secrets.ACR_SERVICE_PRINCIPAL_PASSWORD }}

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.2.4
1+
0.2.5

faasmtools/build.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,13 @@
137137
"-Xlinker --stack-first",
138138
"-Xlinker --export=__heap_base",
139139
"-Xlinker --export=__data_end",
140+
"-Xlinker --export=free",
141+
"-Xlinker --export=malloc",
140142
"-Xlinker --export={}".format(FAASM_WASM_CTORS_FUNC_NAME),
141143
"-Xlinker --export=__stack_pointer",
142144
"-Xlinker --max-memory={}".format(FAASM_WASM_MAX_MEMORY),
143145
"-Xlinker --features=mutable-globals,simd128",
144146
"-Wl,-z,stack-size={} -Wl".format(FAASM_WASM_STACK_SIZE),
145-
"-Wl,--initial-memory={}".format(FAASM_WASM_INITIAL_MEMORY_SIZE),
146147
]
147148

148149
WASM_EXE_LDFLAGS += WASM_WASI_LIBC_LDFLAGS

libfaasm/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ set(PUBLIC_HEADERS
1313
faasm/faasm.h
1414
faasm/files.h
1515
faasm/input.h
16+
faasm/memory.h
1617
faasm/migrate.h
1718
faasm/print.h
1819
faasm/random.h

libfaasm/faasm/core.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <stddef.h>
66
#include <stdint.h>
77

8+
#include "faasm/memory.h"
9+
810
#ifdef __cplusplus
911
extern "C"
1012
{

libfaasm/faasm/memory.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#ifdef __wasm__
4+
// WAMR performs some optimisations on the WASM modules at load time, whereby
5+
// if no memory.grow op-code is found, memory is shrunk to the minimum
6+
// (as specified by the default heap and stack size). This optimisation breaks
7+
// Faasm's memory management, as we may sometimes want to enlarge the WASM's
8+
// memory from outside WASM code. To prevent this optimisation, every time we
9+
// build a WASM function for its usage in faasm (and include the faasm/faasm.h
10+
// header) we link with this dummy function that uses the memory.grow op-code.
11+
// Note that this function is actually not called. See:
12+
// https://github.com/bytecodealliance/wasm-micro-runtime/issues/1706
13+
inline void __attribute__((used)) __faasm_memory_layout_protection()
14+
{
15+
__builtin_wasm_memory_grow(0, 0);
16+
}
17+
#endif

third-party/wasi-libc

0 commit comments

Comments
 (0)