Skip to content

Commit d0f8ac0

Browse files
Add custom section sample and enable CI test (#4891)
1 parent 92f4091 commit d0f8ac0

File tree

14 files changed

+531
-0
lines changed

14 files changed

+531
-0
lines changed

.github/workflows/compilation_on_android_ubuntu.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,14 @@ jobs:
628628
cmake --build . --config Release --parallel 4
629629
./import-func-callback
630630
631+
- name: Build Sample [custom_section]
632+
run: |
633+
cd samples/custom-section
634+
./build.sh
635+
./run.sh
636+
./build.sh --aot
637+
./run.sh --aot
638+
631639
test:
632640
needs: [build_iwasm, build_llvm_libraries_on_ubuntu_2204, build_wamrc]
633641
runs-on: ${{ matrix.os }}

.github/workflows/compilation_on_macos.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,14 @@ jobs:
441441
cmake --build . --config Release --parallel 4
442442
./import-func-callback
443443
444+
- name: Build Sample [custom_section]
445+
run: |
446+
cd samples/custom-section
447+
./build.sh
448+
./run.sh
449+
./build.sh --aot
450+
./run.sh --aot
451+
444452
- name: Test x18 register reservation (macOS ARM64 only)
445453
if: matrix.os == 'macos-15'
446454
run: |

.github/workflows/nightly_run.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,14 @@ jobs:
598598
cmake --build . --config Release --parallel 4
599599
./import-func-callback
600600
601+
- name: Build Sample [custom_section]
602+
run: |
603+
cd samples/custom-section
604+
./build.sh
605+
./run.sh
606+
./build.sh --aot
607+
./run.sh --aot
608+
601609
test:
602610
needs: [build_iwasm, build_llvm_libraries_on_ubuntu, build_wamrc]
603611
runs-on: ${{ matrix.os }}

samples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Samples
22

33
- [**basic**](./basic): Demonstrating how to use runtime exposed API's to call WASM functions, how to register native functions and call them, and how to call WASM function from native function.
4+
- [**custom-section**](./custom-section): Demonstrating how to embed a binary payload into a Wasm custom section, resolve it from native code with `wasm_runtime_get_custom_section`, and print it later through a handle-based native API.
45
- **[file](./file/README.md)**: Demonstrating the supported file interaction API of WASI. This sample can also demonstrate the SGX IPFS (Intel Protected File System), enabling an enclave to seal and unseal data at rest.
56
- **[multi-thread](./multi-thread/)**: Demonstrating how to run wasm application which creates multiple threads to execute wasm functions concurrently, and uses mutex/cond by calling pthread related API's.
67
- **[spawn-thread](./spawn-thread)**: Demonstrating how to execute wasm functions of the same wasm application concurrently, in threads created by host embedder or runtime, but not the wasm application itself.

samples/custom-section/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/out/
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Copyright (C) 2019 Intel Corporation. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
cmake_minimum_required(VERSION 3.14)
5+
6+
include(CheckPIESupported)
7+
8+
if(NOT WAMR_BUILD_PLATFORM STREQUAL "windows")
9+
project(custom_section)
10+
else()
11+
project(custom_section C ASM)
12+
endif()
13+
14+
# ############### runtime settings ################
15+
string(TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
16+
17+
if(APPLE)
18+
add_definitions(-DBH_PLATFORM_DARWIN)
19+
endif()
20+
21+
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
22+
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
23+
24+
if(NOT DEFINED WAMR_BUILD_TARGET)
25+
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
26+
set(WAMR_BUILD_TARGET "AARCH64")
27+
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
28+
set(WAMR_BUILD_TARGET "RISCV64")
29+
elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
30+
set(WAMR_BUILD_TARGET "X86_64")
31+
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
32+
set(WAMR_BUILD_TARGET "X86_32")
33+
else()
34+
message(SEND_ERROR "Unsupported build target platform!")
35+
endif()
36+
endif()
37+
38+
if(NOT CMAKE_BUILD_TYPE)
39+
set(CMAKE_BUILD_TYPE Debug)
40+
endif()
41+
42+
set(WAMR_BUILD_INTERP 1)
43+
set(WAMR_BUILD_AOT 1)
44+
set(WAMR_BUILD_JIT 0)
45+
set(WAMR_BUILD_LIBC_BUILTIN 1)
46+
set(WAMR_BUILD_LOAD_CUSTOM_SECTION 1)
47+
48+
if(NOT MSVC)
49+
set(WAMR_BUILD_LIBC_WASI 1)
50+
endif()
51+
52+
if(NOT MSVC)
53+
if(NOT(CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
54+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
55+
endif()
56+
endif()
57+
58+
set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
59+
include(${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
60+
61+
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
62+
63+
# ############### application related ################
64+
include_directories(${CMAKE_CURRENT_LIST_DIR}/src)
65+
include(${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
66+
67+
add_executable(custom_section
68+
src/main.c
69+
src/native_impl.c
70+
${UNCOMMON_SHARED_SOURCE}
71+
)
72+
73+
check_pie_supported()
74+
set_target_properties(custom_section PROPERTIES POSITION_INDEPENDENT_CODE ON)
75+
76+
if(APPLE)
77+
target_link_libraries(custom_section vmlib -lm -ldl -lpthread)
78+
else()
79+
target_link_libraries(custom_section vmlib -lm -ldl -lpthread -lrt)
80+
endif()

samples/custom-section/README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
description: "The related code/working directory of this example resides in directory {WAMR_DIR}/samples/custom-section"
3+
---
4+
5+
# The "custom-section" sample project
6+
7+
This sample demonstrates how to:
8+
9+
- embed a separate binary payload into a Wasm custom section through a `.s` file
10+
- load that Wasm module with `WAMR_BUILD_LOAD_CUSTOM_SECTION=1`
11+
- export native APIs that resolve a custom section name to a host-side handle
12+
- print the custom section bytes later through a second native API
13+
- optionally compile the Wasm module to AoT while preserving the `demo` custom section
14+
15+
The Wasm application is built from:
16+
17+
- `wasm-apps/custom_section.c`
18+
- `wasm-apps/custom_section_payload.s`
19+
- `wasm-apps/custom_section_payload.bin`
20+
21+
The assembler file emits a section named `.custom_section.demo`, which becomes a Wasm custom section named `demo` in the final `.wasm` file.
22+
23+
## Why use a custom section for this payload
24+
25+
The payload in this sample is treated as read-only metadata. Putting it in a custom section lets the embedder access the bytes directly from the loaded module through `wasm_runtime_get_custom_section`, instead of copying the data into Wasm linear memory per instance.
26+
27+
That matters when the data is large or rarely changed:
28+
29+
- the bytes stay in the module image as immutable data
30+
- the host can look them up by section name and use them in place
31+
- the Wasm app only needs to pass a small section name and receive a small handle
32+
- no extra application-level serialization or buffer duplication is needed for the read-only payload
33+
34+
This pattern is useful for embedded assets, lookup tables, model metadata, certificates, and other static blobs that the host wants to consume without treating them as mutable Wasm heap data.
35+
36+
## Build this sample
37+
38+
Execute the `build.sh` script. The host executable and the Wasm app are generated in `out`.
39+
40+
```sh
41+
./build.sh
42+
```
43+
44+
Build the AoT variant only when needed by passing `--aot`. This preserves the `demo` custom section in the generated AoT file by calling `wamrc --emit-custom-sections=demo`.
45+
46+
```sh
47+
./build.sh --aot
48+
```
49+
50+
## Run the sample
51+
52+
Enter the output directory and run the Wasm sample directly:
53+
54+
```sh
55+
cd ./out/
56+
./custom_section -f wasm-apps/custom_section.wasm
57+
```
58+
59+
Or run the helper script from `samples/custom_section`:
60+
61+
```sh
62+
./run.sh
63+
```
64+
65+
To run the AoT artifact instead, pass `--aot` to the helper script:
66+
67+
```sh
68+
./run.sh --aot
69+
```

samples/custom-section/build.sh

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
CURR_DIR=$PWD
6+
OUT_DIR=${PWD}/out
7+
WASM_APPS=${PWD}/wasm-apps
8+
WAMR_ROOT_DIR=${PWD}/../..
9+
WAMRC_CMD=${WAMR_ROOT_DIR}/wamr-compiler/build/wamrc
10+
BUILD_AOT=0
11+
12+
if [ $# -gt 1 ]; then
13+
echo "Usage: $0 [--aot]"
14+
exit 1
15+
fi
16+
17+
if [ $# -eq 1 ]; then
18+
if [ "$1" = "--aot" ]; then
19+
BUILD_AOT=1
20+
else
21+
echo "Usage: $0 [--aot]"
22+
exit 1
23+
fi
24+
fi
25+
26+
rm -rf ${OUT_DIR}
27+
mkdir -p ${OUT_DIR}/wasm-apps
28+
29+
printf '##################### build custom-section project\n'
30+
mkdir -p build
31+
cd build
32+
cmake .. -DCMAKE_BUILD_TYPE=Debug
33+
make -j 4
34+
cp -a custom_section ${OUT_DIR}
35+
36+
printf '\n##################### build wasm app\n'
37+
cd ${WASM_APPS}
38+
/opt/wasi-sdk/bin/clang \
39+
--target=wasm32 \
40+
-O0 \
41+
-nostdlib \
42+
-Wl,--strip-all,--no-entry \
43+
-Wl,--allow-undefined \
44+
-Wl,--export=run_demo \
45+
-o ${OUT_DIR}/wasm-apps/custom_section.wasm \
46+
custom_section.c \
47+
custom_section_payload.s
48+
49+
printf '\nbuild custom_section.wasm success\n'
50+
51+
if [ ${BUILD_AOT} -eq 1 ]; then
52+
if [ ! -x ${WAMRC_CMD} ]; then
53+
echo "Error: wamrc not found at ${WAMRC_CMD}"
54+
echo "Please build wamrc first under ${WAMR_ROOT_DIR}/wamr-compiler"
55+
exit 1
56+
fi
57+
58+
printf '\n##################### build aot app\n'
59+
${WAMRC_CMD} --emit-custom-sections=demo -o ${OUT_DIR}/wasm-apps/custom_section.aot ${OUT_DIR}/wasm-apps/custom_section.wasm
60+
printf '\nbuild custom_section.aot success\n'
61+
fi
62+
63+
cd ${CURR_DIR}

samples/custom-section/run.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
if [ $# -gt 1 ]; then
6+
echo "Usage: $0 [--aot]"
7+
exit 1
8+
fi
9+
10+
APP=out/wasm-apps/custom_section.wasm
11+
12+
if [ $# -eq 1 ]; then
13+
if [ "$1" = "--aot" ]; then
14+
APP=out/wasm-apps/custom_section.aot
15+
else
16+
echo "Usage: $0 [--aot]"
17+
exit 1
18+
fi
19+
fi
20+
21+
if [ ! -f ${APP} ]; then
22+
echo "Error: ${APP} not found"
23+
if [ "$APP" = "out/wasm-apps/custom_section.aot" ]; then
24+
echo "Run ./build.sh --aot first"
25+
else
26+
echo "Run ./build.sh first"
27+
fi
28+
exit 1
29+
fi
30+
31+
out/custom_section -f ${APP}

0 commit comments

Comments
 (0)