Skip to content

Commit 78414b6

Browse files
authored
ESP IDF fixes (#927)
Various fixes and beautifications coordinated with @1c3t3a, fixes 2 of the 3 all remaining issues from #892: - enable to os_mmap executable memory - fix os_malloc/os_realloc/os_free issues - implement os_thread_get_stack_boundary - add build scripts to include with esp-idf to use wamr as an ESP-IDF component - update sample and document
1 parent 2c3f284 commit 78414b6

File tree

17 files changed

+345
-196
lines changed

17 files changed

+345
-196
lines changed

build-scripts/esp-idf/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# wasm-micro-runtime as ESP-IDF component
2+
3+
You can build an ESP-IDF project with wasm-micro-runtime as a component:
4+
5+
- Make sure you have the ESP-IDF properly installed and setup
6+
- In particular have the following paths set:
7+
- `WAMR_PATH` to point to your wasm-micro-runtime repository
8+
- `IDF_PATH` to point to your ESP-IDF
9+
- `source $IDF_PATH/export.sh`
10+
- Create a new project, e.g.: `idf.py create-project wamr-hello`
11+
- In the newly created project folder edit the `CMakeList.txt`:
12+
13+
```
14+
cmake_minimum_required(VERSION 3.5)
15+
16+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
17+
18+
set (COMPONENTS ${IDF_TARGET} main freertos esptool_py wamr)
19+
20+
list(APPEND EXTRA_COMPONENT_DIRS "$ENV{WAMR_PATH}/build-scripts/esp-idf")
21+
22+
project(wamr-hello)
23+
```
24+
- Develop your project in it's `main` component folder.
25+
26+
You can find an example [here](../../product-mini/platforms/esp-idf).
27+
28+
- Set target platform: `idf.py set-target esp32c3`
29+
- Build: `idf.py build`
30+
- Flash: `idf.py flash`
31+
- Check the output: `idf.py monitor`
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright (C) 2021 Intel Corporation and others. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
# Set WAMR's build options
5+
if("${IDF_TARGET}" STREQUAL "esp32c3")
6+
set(WAMR_BUILD_TARGET "RISCV32")
7+
else()
8+
set(WAMR_BUILD_TARGET "XTENSA")
9+
endif()
10+
11+
set(WAMR_BUILD_PLATFORM "esp-idf")
12+
13+
if (NOT CMAKE_BUILD_TYPE)
14+
set(CMAKE_BUILD_TYPE Release)
15+
endif ()
16+
17+
if (NOT DEFINED WAMR_BUILD_INTERP)
18+
set (WAMR_BUILD_INTERP 1)
19+
endif ()
20+
21+
if (NOT DEFINED WAMR_BUILD_FAST_INTERP)
22+
set (WAMR_BUILD_FAST_INTERP 1)
23+
endif ()
24+
25+
if (NOT DEFINED WAMR_BUILD_AOT)
26+
set (WAMR_BUILD_AOT 1)
27+
endif ()
28+
29+
if (NOT DEFINED WAMR_BUILD_LIBC_BUILTIN)
30+
set (WAMR_BUILD_LIBC_BUILTIN 1)
31+
endif ()
32+
33+
if (NOT DEFINED WAMR_BUILD_APP_FRAMEWORK)
34+
set (WAMR_BUILD_APP_FRAMEWORK 0)
35+
endif ()
36+
37+
if (NOT CMAKE_BUILD_EARLY_EXPANSION)
38+
if (WAMR_BUILD_TARGET STREQUAL "XTENSA")
39+
idf_build_set_property(COMPILE_DEFINITIONS "-DBUILD_TARGET_XTENSA=1" APPEND)
40+
endif ()
41+
if (WAMR_BUILD_INTERP)
42+
idf_build_set_property(COMPILE_DEFINITIONS "-DWASM_ENABLE_INTERP=1" APPEND)
43+
endif ()
44+
if (WAMR_BUILD_AOT)
45+
idf_build_set_property(COMPILE_DEFINITIONS "-DWASM_ENABLE_AOT=1" APPEND)
46+
endif ()
47+
48+
set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../../..)
49+
include(${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
50+
endif()
51+
52+
idf_component_register(SRCS ${WAMR_RUNTIME_LIB_SOURCE} ${PLATFORM_SHARED_SOURCE}
53+
INCLUDE_DIRS ${IWASM_DIR}/include ${UTILS_SHARED_DIR} ${PLATFORM_SHARED_DIR} ${PLATFORM_SHARED_DIR}/../include
54+
REQUIRES pthread
55+
)
56+
57+

core/iwasm/aot/arch/aot_reloc_xtensa.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ put_imm16_to_addr(int16 imm16, int16 *addr)
112112
if ((intptr_t)addr % 4 != 3) {
113113
*(int32 *)bytes = *addr_aligned1;
114114
*(int16 *)(bytes + ((intptr_t)addr % 4)) = imm16;
115-
memcpy(addr_aligned1, bytes, 4);
115+
*addr_aligned1 = *(int32 *)bytes;
116116
}
117117
else {
118118
addr_aligned2 = (int32 *)(((intptr_t)addr + 3) & ~3);

core/shared/platform/esp-idf/espidf_malloc.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ os_malloc(unsigned size)
1414
uintptr_t *addr_field;
1515

1616
buf_origin = malloc(size + 8 + sizeof(uintptr_t));
17+
if (!buf_origin) {
18+
return NULL;
19+
}
1720
buf_fixed = buf_origin + sizeof(void *);
1821
if ((uintptr_t)buf_fixed & (uintptr_t)0x7) {
1922
buf_fixed = (void *)((uintptr_t)(buf_fixed + 8) & (~(uintptr_t)7));
@@ -34,12 +37,15 @@ os_realloc(void *ptr, unsigned size)
3437
uintptr_t *addr_field;
3538

3639
if (!ptr) {
37-
return NULL;
40+
return os_malloc(size);
3841
}
3942

4043
addr_field = ptr - sizeof(uintptr_t);
4144
mem_origin = (void *)(*addr_field);
4245
mem_new = realloc(mem_origin, size + 8 + sizeof(uintptr_t));
46+
if (!mem_new) {
47+
return NULL;
48+
}
4349

4450
if (mem_origin != mem_new) {
4551
mem_new_fixed = mem_new + sizeof(uintptr_t);
@@ -61,7 +67,7 @@ void
6167
os_free(void *ptr)
6268
{
6369
void *mem_origin;
64-
uintptr *addr_field;
70+
uintptr_t *addr_field;
6571

6672
if (ptr) {
6773
addr_field = ptr - sizeof(uintptr_t);

core/shared/platform/esp-idf/espidf_memmap.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,34 @@
99
void *
1010
os_mmap(void *hint, size_t size, int prot, int flags)
1111
{
12-
return os_malloc((int)size);
12+
if (prot & MMAP_PROT_EXEC) {
13+
// Memory allocation with MALLOC_CAP_EXEC will return 4-byte aligned
14+
// Reserve extra 4 byte to fixup alignment and size for the pointer to
15+
// the originally allocated address
16+
void *buf_origin =
17+
heap_caps_malloc(size + 4 + sizeof(uintptr_t), MALLOC_CAP_EXEC);
18+
if (!buf_origin) {
19+
return NULL;
20+
}
21+
void *buf_fixed = buf_origin + sizeof(void *);
22+
if ((uintptr_t)buf_fixed & (uintptr_t)0x7) {
23+
buf_fixed = (void *)((uintptr_t)(buf_fixed + 4) & (~(uintptr_t)7));
24+
}
25+
26+
uintptr_t *addr_field = buf_fixed - sizeof(uintptr_t);
27+
*addr_field = (uintptr_t)buf_origin;
28+
return buf_fixed;
29+
}
30+
else {
31+
return os_malloc(size);
32+
}
1333
}
1434

1535
void
1636
os_munmap(void *addr, size_t size)
1737
{
38+
// We don't need special handling of the executable allocations
39+
// here, free() of esp-idf handles it properly
1840
return os_free(addr);
1941
}
2042

core/shared/platform/esp-idf/espidf_platform.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ os_time_get_boot_microsecond(void)
4444
uint8 *
4545
os_thread_get_stack_boundary(void)
4646
{
47+
#if defined(CONFIG_FREERTOS_USE_TRACE_FACILITY)
48+
TaskStatus_t pxTaskStatus;
49+
vTaskGetInfo(xTaskGetCurrentTaskHandle(), &pxTaskStatus, pdTRUE, eInvalid);
50+
return pxTaskStatus.pxStackBase;
51+
#else // !defined(CONFIG_FREERTOS_USE_TRACE_FACILITY)
4752
return NULL;
53+
#endif
4854
}
4955

5056
int
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
sdkconfig
2+
sdkconfig.old
Lines changed: 5 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,12 @@
1-
# Copyright (C) 2019 Intel Corporation. All rights reserved.
1+
# Copyright (C) 2019-21 Intel Corporation and others. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
33

44
# from ESP-IDF 4.0 examples/build_system/cmake/idf_as_lib
55
cmake_minimum_required(VERSION 3.5)
6-
project(wamr_on_esp32c3)
76

8-
enable_language(ASM)
7+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
98

10-
if (NOT CMAKE_BUILD_TYPE)
11-
set(CMAKE_BUILD_TYPE Release)
12-
endif ()
9+
set (COMPONENTS ${IDF_TARGET} main freertos esptool_py wamr)
10+
list(APPEND EXTRA_COMPONENT_DIRS "$ENV{WAMR_PATH}/build-scripts/esp-idf")
1311

14-
if("${IDF_TARGET}" STREQUAL "")
15-
message(FATAL_ERROR "You need to set IDF_TARGET to your target string")
16-
endif()
17-
18-
# Include for ESP-IDF build system functions
19-
include($ENV{IDF_PATH}/tools/cmake/idf.cmake)
20-
# Create idf::esp32c3 and idf::freertos static libraries
21-
idf_build_process(${IDF_TARGET}
22-
# try and trim the build; additional components
23-
# will be included as needed based on dependency tree
24-
#
25-
# although esptool_py does not generate static library,
26-
# processing the component is needed for flashing related
27-
# targets and file generation
28-
COMPONENTS ${IDF_TARGET} freertos esptool_py
29-
SDKCONFIG ${CMAKE_BINARY_DIR}/sdkconfig
30-
BUILD_DIR ${CMAKE_BINARY_DIR})
31-
32-
# Set WAMR's build options
33-
if("${IDF_TARGET}" STREQUAL "esp32c3")
34-
set(WAMR_BUILD_TARGET "RISCV32")
35-
else()
36-
set(WAMR_BUILD_TARGET "XTENSA")
37-
add_compile_options(-DWAMR_BUILD_TARGET_XTENSA=1)
38-
endif()
39-
40-
set(WAMR_BUILD_PLATFORM "esp-idf")
41-
42-
if (NOT DEFINED WAMR_BUILD_INTERP)
43-
set (WAMR_BUILD_INTERP 0)
44-
endif ()
45-
46-
if (NOT DEFINED WAMR_BUILD_FAST_INTERP)
47-
set (WAMR_BUILD_FAST_INTERP 0)
48-
endif ()
49-
50-
if (NOT DEFINED WAMR_BUILD_AOT)
51-
set (WAMR_BUILD_AOT 1)
52-
endif ()
53-
54-
if (NOT DEFINED WAMR_BUILD_LIBC_BUILTIN)
55-
set (WAMR_BUILD_LIBC_BUILTIN 1)
56-
endif ()
57-
58-
if (NOT DEFINED WAMR_BUILD_APP_FRAMEWORK)
59-
set (WAMR_BUILD_APP_FRAMEWORK 0)
60-
endif ()
61-
62-
63-
# Set the compile time variable so that the right binary is selected
64-
add_compile_options(-DWAMR_BUILD_INTERP=${WAMR_BUILD_INTERP})
65-
66-
set(WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
67-
include(${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
68-
69-
# define WAMR as library and provide it the esp-idf srcs
70-
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
71-
target_link_libraries(vmlib PUBLIC idf::pthread idf::${IDF_TARGET} idf::freertos)
72-
73-
# Define the final executable
74-
set(elf_file ${CMAKE_PROJECT_NAME}.elf)
75-
add_executable(${elf_file} main.c test_wasm.h)
76-
target_link_libraries(${elf_file} idf::${IDF_TARGET} idf::freertos idf::spi_flash vmlib)
77-
idf_build_executable(${elf_file})
12+
project(wamr-simple)

product-mini/platforms/esp-idf/build.sh

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash -e
2+
3+
# Copyright (C) 2019-21 Intel Corporation and others. All rights reserved.
4+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
6+
ESP32_TARGET="esp32"
7+
ESP32C3_TARGET="esp32c3"
8+
9+
usage ()
10+
{
11+
echo "USAGE:"
12+
echo "$0 $ESP32_TARGET|$ESP32C3_TARGET"
13+
echo "Example:"
14+
echo " $0 $ESP32_TARGET"
15+
echo " $0 $ESP32C3_TARGET"
16+
exit 1
17+
}
18+
19+
if [ $# != 1 ] ; then
20+
usage
21+
fi
22+
23+
TARGET=$1
24+
25+
if [[ -z "${WAMR_PATH}" ]]; then
26+
export WAMR_PATH=$PWD/../../..
27+
fi
28+
29+
rm -rf build
30+
idf.py set-target $TARGET
31+
idf.py build
32+
idf.py flash
33+

0 commit comments

Comments
 (0)