Compare commits

...

7 Commits

Author SHA1 Message Date
liang.he
f84d905a97
Merge d3a2cdd8f7 into 0e8b57d8a8 2025-05-08 01:06:57 +08:00
Chris Woods
0e8b57d8a8
More detail to python setup, and fixed small typo (#4247)
Some checks are pending
compilation on SGX / build_iwasm (-DWAMR_DISABLE_HW_BOUND_CHECK=1, $FAST_INTERP_BUILD_OPTIONS, ubuntu-22.04, linux-sgx) (push) Waiting to run
compilation on SGX / build_iwasm (-DWAMR_DISABLE_HW_BOUND_CHECK=1, $FAST_JIT_BUILD_OPTIONS, ubuntu-22.04, linux-sgx) (push) Waiting to run
compilation on SGX / run_samples_file (-DWAMR_BUILD_SGX_IPFS=1, $AOT_BUILD_OPTIONS, ${{ needs.build_llvm_libraries.outputs.cache_key }}, ubuntu-22.04, linux-sgx) (push) Blocked by required conditions
compilation on SGX / run_samples_file (-DWAMR_BUILD_SGX_IPFS=1, $CLASSIC_INTERP_BUILD_OPTIONS, ${{ needs.build_llvm_libraries.outputs.cache_key }}, ubuntu-22.04, linux-sgx) (push) Blocked by required conditions
compilation on SGX / run_samples_file (-DWAMR_BUILD_SGX_IPFS=1, $FAST_INTERP_BUILD_OPTIONS, ${{ needs.build_llvm_libraries.outputs.cache_key }}, ubuntu-22.04, linux-sgx) (push) Blocked by required conditions
compilation on SGX / run_samples_file (-DWAMR_BUILD_SGX_IPFS=1, $FAST_JIT_BUILD_OPTIONS, ${{ needs.build_llvm_libraries.outputs.cache_key }}, ubuntu-22.04, linux-sgx) (push) Blocked by required conditions
compilation on SGX / spec_test_default (${{ needs.build_llvm_libraries.outputs.cache_key }}, ubuntu-22.04, aot, $DEFAULT_TEST_OPTIONS) (push) Blocked by required conditions
compilation on SGX / spec_test_default (${{ needs.build_llvm_libraries.outputs.cache_key }}, ubuntu-22.04, aot, $SIMD_TEST_OPTIONS) (push) Blocked by required conditions
compilation on SGX / spec_test_default (${{ needs.build_llvm_libraries.outputs.cache_key }}, ubuntu-22.04, aot, $XIP_TEST_OPTIONS) (push) Blocked by required conditions
compilation on SGX / spec_test_default (${{ needs.build_llvm_libraries.outputs.cache_key }}, ubuntu-22.04, classic-interp, $DEFAULT_TEST_OPTIONS) (push) Blocked by required conditions
compilation on SGX / spec_test_default (${{ needs.build_llvm_libraries.outputs.cache_key }}, ubuntu-22.04, fast-jit, $DEFAULT_TEST_OPTIONS) (push) Blocked by required conditions
compilation on windows-latest / build (-DWAMR_BUILD_AOT=0) (push) Waiting to run
compilation on windows-latest / build (-DWAMR_BUILD_AOT=1 -DWAMR_BUILD_INTERP=0) (push) Waiting to run
compilation on windows-latest / build (-DWAMR_BUILD_CUSTOM_NAME_SECTION=1) (push) Waiting to run
compilation on windows-latest / build (-DWAMR_BUILD_DEBUG_INTERP=1) (push) Waiting to run
compilation on windows-latest / build (-DWAMR_BUILD_LIBC_UVWASI=0 -DWAMR_BUILD_LIBC_WASI=1) (push) Waiting to run
compilation on windows-latest / build (-DWAMR_BUILD_LIB_PTHREAD=1) (push) Waiting to run
compilation on windows-latest / build (-DWAMR_BUILD_LIB_WASI_THREADS=1) (push) Waiting to run
compilation on windows-latest / build (-DWAMR_BUILD_REF_TYPES=1) (push) Waiting to run
compilation on windows-latest / build (-DWAMR_BUILD_SIMD=1) (push) Waiting to run
compilation on windows-latest / build (-DWAMR_BUILD_TAIL_CALL=1) (push) Waiting to run
compilation on windows-latest / build (-DWAMR_DISABLE_HW_BOUND_CHECK=1) (push) Waiting to run
compilation on windows-latest / test (classic-interp, $DEFAULT_TEST_OPTIONS) (push) Blocked by required conditions
compilation on windows-latest / test (classic-interp, $MULTI_MODULES_TEST_OPTIONS) (push) Blocked by required conditions
compilation on windows-latest / test (classic-interp, $THREADS_TEST_OPTIONS) (push) Blocked by required conditions
compilation on windows-latest / test (classic-interp, $WASI_TEST_OPTIONS) (push) Blocked by required conditions
compilation on windows-latest / test (fast-interp, $DEFAULT_TEST_OPTIONS) (push) Blocked by required conditions
compilation on windows-latest / test (fast-interp, $MULTI_MODULES_TEST_OPTIONS) (push) Blocked by required conditions
compilation on windows-latest / test (fast-interp, $THREADS_TEST_OPTIONS) (push) Blocked by required conditions
compilation on windows-latest / test (fast-interp, $WASI_TEST_OPTIONS) (push) Blocked by required conditions
2025-05-07 11:32:43 +08:00
YAMAMOTO Takashi
88b5f6a535
samples/wasm-c-api: remove unused valgrind detection (#4249)
- it's unused
- valgrind is basically a linux-only software.
  it isn't a good idea to make it a hard requirement.
  if we want to use valgrind, it's better to introduce
  a separate option to control it.
2025-05-07 11:32:29 +08:00
YAMAMOTO Takashi
ac2fe552d5
aot_resolve_object_relocation_group: adapt to LLVM 16 (#4250)
cf. https://reviews.llvm.org/D123264
2025-05-07 11:32:14 +08:00
liang.he
ea417d7619
Add overflow check for preserved local offset in preserve_referenced_local (#4211) 2025-05-07 09:45:49 +08:00
YAMAMOTO Takashi
bb36a43fa4
riscv: avoid llvm.cttz.i32/i64 for xip (#4248)
LLVM 16 and later expands cttz intrinsic to a table lookup,
which involves some relocations. (unless ZBB is available,
in which case the native instructions are preferred over
the table-based lowering.)

cf. https://reviews.llvm.org/D128911
2025-05-07 09:42:51 +08:00
liang.he@intel.com
d3a2cdd8f7 Use execve() to replace system() and rename
- Direct Execution: execve() directly executes a program, bypassing the shell.
  This avoids vulnerabilities like shell injection, which can occur with
  system() if user input is not properly sanitized.
- Controlled Environment: With execve(), you can explicitly specify the
  environment variables for the new process, providing better control over
  the execution context.
- No Shell Overhead: execve() does not invoke a shell, reducing the risk
  of unintended behavior caused by shell features or configurations.
- Predictable Behavior: execve() only executes the specified program, whereas
  system() relies on the shell, which may interpret commands differently
  based on the shell's configuration or environment.
2025-04-29 05:31:44 +00:00
11 changed files with 128 additions and 54 deletions

View File

@ -898,6 +898,17 @@ aot_intrinsic_fill_capability_flags(AOTCompContext *comp_ctx)
if (!strncmp(comp_ctx->target_arch, "riscv32", 7)) { if (!strncmp(comp_ctx->target_arch, "riscv32", 7)) {
add_i64_common_intrinsics(comp_ctx); add_i64_common_intrinsics(comp_ctx);
} }
/*
* LLVM 16 and later expands cttz intrinsic to a table lookup,
* which involves some relocations. (unless ZBB is available,
* in which case the native instructions are preferred over
* the table-based lowering.)
* https://reviews.llvm.org/D128911
*/
#if LLVM_VERSION_MAJOR >= 16
add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_I32_CTZ);
add_intrinsic_capability(comp_ctx, AOT_INTRINSIC_FLAG_I64_CTZ);
#endif
} }
else if (!strncmp(comp_ctx->target_arch, "xtensa", 6)) { else if (!strncmp(comp_ctx->target_arch, "xtensa", 6)) {
/* /*

View File

@ -4195,13 +4195,13 @@ aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name)
bh_print_time("Begin to emit object file"); bh_print_time("Begin to emit object file");
if (comp_ctx->external_llc_compiler || comp_ctx->external_asm_compiler) { if (comp_ctx->external_llc_compiler || comp_ctx->external_asm_compiler) {
char cmd[1024];
int ret; int ret;
if (comp_ctx->external_llc_compiler) { if (comp_ctx->external_llc_compiler) {
const char *stack_usage_flag = ""; char *stack_usage_flag = "";
char bc_file_name[64]; char bc_file_name[64] = { 0 };
char su_file_name[65]; /* See the comment below */ char su_file_name[65] = { 0 };
char *argv[10] = { 0 };
if (comp_ctx->stack_usage_file != NULL) { if (comp_ctx->stack_usage_file != NULL) {
/* /*
@ -4229,14 +4229,16 @@ aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name)
return false; return false;
} }
snprintf(cmd, sizeof(cmd), "%s%s %s -o %s %s", argv[0] = stack_usage_flag;
comp_ctx->external_llc_compiler, stack_usage_flag, argv[1] = comp_ctx->llc_compiler_flags
comp_ctx->llc_compiler_flags ? comp_ctx->llc_compiler_flags ? (char *)comp_ctx->llc_compiler_flags
: "-O3 -c", : "-O3 -c";
file_name, bc_file_name); argv[2] = "-o";
LOG_VERBOSE("invoking external LLC compiler:\n\t%s", cmd); argv[3] = file_name;
argv[4] = bc_file_name;
argv[5] = NULL;
ret = bh_system(cmd); ret = bh_execve(comp_ctx->external_llc_compiler, argv, 6);
/* remove temp bitcode file */ /* remove temp bitcode file */
unlink(bc_file_name); unlink(bc_file_name);
@ -4263,7 +4265,8 @@ aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name)
} }
} }
else if (comp_ctx->external_asm_compiler) { else if (comp_ctx->external_asm_compiler) {
char asm_file_name[64]; char asm_file_name[64] = { 0 };
char *argv[10] = { 0 };
if (!aot_generate_tempfile_name("wamrc-asm", "s", asm_file_name, if (!aot_generate_tempfile_name("wamrc-asm", "s", asm_file_name,
sizeof(asm_file_name))) { sizeof(asm_file_name))) {
@ -4282,14 +4285,15 @@ aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name)
return false; return false;
} }
snprintf(cmd, sizeof(cmd), "%s %s -o %s %s", argv[0] = comp_ctx->asm_compiler_flags
comp_ctx->external_asm_compiler, ? (char *)comp_ctx->asm_compiler_flags
comp_ctx->asm_compiler_flags ? comp_ctx->asm_compiler_flags : "-O3 -c";
: "-O3 -c", argv[1] = "-o";
file_name, asm_file_name); argv[2] = file_name;
LOG_VERBOSE("invoking external ASM compiler:\n\t%s", cmd); argv[3] = asm_file_name;
argv[4] = NULL;
ret = bh_system(cmd); ret = bh_execve(comp_ctx->external_asm_compiler, argv, 5);
/* remove temp assembly file */ /* remove temp assembly file */
unlink(asm_file_name); unlink(asm_file_name);

View File

@ -4007,8 +4007,12 @@ aot_resolve_object_relocation_group(AOTObjectData *obj_data,
&& (str_starts_with(relocation->symbol_name, ".LCPI") && (str_starts_with(relocation->symbol_name, ".LCPI")
|| str_starts_with(relocation->symbol_name, ".LJTI") || str_starts_with(relocation->symbol_name, ".LJTI")
|| str_starts_with(relocation->symbol_name, ".LBB") || str_starts_with(relocation->symbol_name, ".LBB")
|| str_starts_with(relocation->symbol_name, || str_starts_with(relocation->symbol_name, ".Lswitch.table.")
".Lswitch.table."))) { #if LLVM_VERSION_MAJOR >= 16
/* cf. https://reviews.llvm.org/D123264 */
|| str_starts_with(relocation->symbol_name, ".Lpcrel_hi")
#endif
)) {
/* change relocation->relocation_addend and /* change relocation->relocation_addend and
relocation->symbol_name */ relocation->symbol_name */
LLVMSectionIteratorRef contain_section; LLVMSectionIteratorRef contain_section;
@ -4348,18 +4352,23 @@ aot_obj_data_create(AOTCompContext *comp_ctx)
else if (!strncmp(LLVMGetTargetName(target), "arc", 3)) { else if (!strncmp(LLVMGetTargetName(target), "arc", 3)) {
/* Emit to assembly file instead for arc target /* Emit to assembly file instead for arc target
as it cannot emit to object file */ as it cannot emit to object file */
char file_name[] = "wasm-XXXXXX", buf[128]; char file_name[] = "wasm-XXXXXX";
char assembly_file_name[64] = { 0 };
char object_file_name[64] = { 0 };
int ret; int ret;
char *argv[] = { "-mcpu=arcem", "-o", object_file_name, "-c",
assembly_file_name, NULL };
if (!bh_mkstemp(file_name, sizeof(file_name))) { if (!bh_mkstemp(file_name, sizeof(file_name))) {
aot_set_last_error("make temp file failed."); aot_set_last_error("make temp file failed.");
goto fail; goto fail;
} }
snprintf(buf, sizeof(buf), "%s%s", file_name, ".s"); snprintf(assembly_file_name, sizeof(assembly_file_name) - 1, "%s.s",
file_name);
if (LLVMTargetMachineEmitToFile(comp_ctx->target_machine, if (LLVMTargetMachineEmitToFile(comp_ctx->target_machine,
comp_ctx->module, buf, LLVMAssemblyFile, comp_ctx->module, assembly_file_name,
&err) LLVMAssemblyFile, &err)
!= 0) { != 0) {
if (err) { if (err) {
LLVMDisposeMessage(err); LLVMDisposeMessage(err);
@ -4372,14 +4381,14 @@ aot_obj_data_create(AOTCompContext *comp_ctx)
/* call arc gcc to compile assembly file to object file */ /* call arc gcc to compile assembly file to object file */
/* TODO: get arc gcc from environment variable firstly /* TODO: get arc gcc from environment variable firstly
and check whether the toolchain exists actually */ and check whether the toolchain exists actually */
snprintf(buf, sizeof(buf), "%s%s%s%s%s%s", snprintf(object_file_name, sizeof(object_file_name) - 1, "%s.o",
"/opt/zephyr-sdk/arc-zephyr-elf/bin/arc-zephyr-elf-gcc ", file_name);
"-mcpu=arcem -o ", file_name, ".o -c ", file_name, ".s");
/* TODO: use try..catch to handle possible exceptions */ /* TODO: use try..catch to handle possible exceptions */
ret = bh_system(buf); /* TODO: use ZEPHYR_SDK_INSTALL_DIR to construct the path */
ret = bh_execve("/opt/zephyr-sdk/arc-zephyr-elf/bin/arc-zephyr-elf-gcc",
argv, 6);
/* remove temp assembly file */ /* remove temp assembly file */
snprintf(buf, sizeof(buf), "%s%s", file_name, ".s"); unlink(assembly_file_name);
unlink(buf);
if (ret != 0) { if (ret != 0) {
aot_set_last_error("failed to compile asm file to obj file " aot_set_last_error("failed to compile asm file to obj file "
@ -4388,12 +4397,10 @@ aot_obj_data_create(AOTCompContext *comp_ctx)
} }
/* create memory buffer from object file */ /* create memory buffer from object file */
snprintf(buf, sizeof(buf), "%s%s", file_name, ".o"); ret = LLVMCreateMemoryBufferWithContentsOfFile(
ret = LLVMCreateMemoryBufferWithContentsOfFile(buf, &obj_data->mem_buf, object_file_name, &obj_data->mem_buf, &err);
&err);
/* remove temp object file */ /* remove temp object file */
snprintf(buf, sizeof(buf), "%s%s", file_name, ".o"); unlink(object_file_name);
unlink(buf);
if (ret != 0) { if (ret != 0) {
if (err) { if (err) {

View File

@ -9197,6 +9197,15 @@ preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
loader_ctx->preserved_local_offset += 2; loader_ctx->preserved_local_offset += 2;
emit_label(EXT_OP_COPY_STACK_TOP_I64); emit_label(EXT_OP_COPY_STACK_TOP_I64);
} }
/* overflow */
if (preserved_offset > loader_ctx->preserved_local_offset) {
set_error_buf_v(error_buf, error_buf_size,
"too much local cells 0x%x",
loader_ctx->preserved_local_offset);
return false;
}
emit_operand(loader_ctx, local_index); emit_operand(loader_ctx, local_index);
emit_operand(loader_ctx, preserved_offset); emit_operand(loader_ctx, preserved_offset);
emit_label(opcode); emit_label(opcode);

View File

@ -4778,6 +4778,11 @@ preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
loader_ctx->preserved_local_offset += 2; loader_ctx->preserved_local_offset += 2;
emit_label(EXT_OP_COPY_STACK_TOP_I64); emit_label(EXT_OP_COPY_STACK_TOP_I64);
} }
/* overflow */
bh_assert(preserved_offset
<= loader_ctx->preserved_local_offset);
emit_operand(loader_ctx, local_index); emit_operand(loader_ctx, local_index);
emit_operand(loader_ctx, preserved_offset); emit_operand(loader_ctx, preserved_offset);
emit_label(opcode); emit_label(opcode);

View File

@ -167,15 +167,39 @@ wa_strdup(const char *s)
} }
#if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0 #if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
/* need to make sure that The `argv[]` must be terminated by a NULL pointer. */
int int
bh_system(const char *cmd) bh_execve(const char *pathname, char *const argv[], int argc)
{ {
int ret; int ret;
/* no environment variables */
char *const envp[] = { NULL };
if (pathname == NULL) {
return -1;
}
if (argc > 0) {
if (argv == NULL) {
return -1;
}
/* The `argv[]` must be terminated by a NULL pointer. */
if (argv[argc - 1] != NULL) {
return -1;
}
}
#if !(defined(_WIN32) || defined(_WIN32_)) #if !(defined(_WIN32) || defined(_WIN32_))
ret = system(cmd); ret = execve(pathname, argv, envp);
#ifndef NDEBUG
if (ret == -1) {
LOG_WARNING("execute \"%s\" failed because of \"%s\"", pathname,
strerror(errno));
}
#endif
#else #else
ret = _spawnlp(_P_WAIT, "cmd.exe", "/c", cmd, NULL); ret = _execve(pathname, argv, envp);
#endif #endif
return ret; return ret;

View File

@ -67,9 +67,19 @@ char *
wa_strdup(const char *s); wa_strdup(const char *s);
#if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0 #if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
/* Executes a system command in bash/cmd.exe */ /*
* Executes a program referred to by cmd in bash/cmd.exe
* Always be sure that argv[argc-1] == NULL
*
* @param pathname The program to execute. need to be absolute path.
* @param argv The command line arguments.
* @param argc The number of command line arguments.
*
* like to execute "ls -l /tmp":
* bh_execve("/bin/ls", (char *const []){ "-l", "/tmp", NULL }, 3);
*/
int int
bh_system(const char *cmd); bh_execve(const char *pathname, char *const argv[], int argc);
/* Tests whether can create a temporary file with the given name */ /* Tests whether can create a temporary file with the given name */
bool bool

View File

@ -1,14 +1,17 @@
# WARM API # WAMR API
* **Notice**: The python package `wamr.wamrapi.wamr` need python >= `3.10`. * **Notice**: The python package `wamr.wamrapi.wamr` requires a python version >= `3.10`.
## Setup ## Setup
### Pre-requisites ### Pre-requisites
#### Install requirements
Before proceeding it is necessary to make sure your Python environment is correctly configured. To do ths open a terminal session in this directory and perfom the following:
Install requirements,
```shell ```shell
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt pip install -r requirements.txt
``` ```

View File

@ -205,12 +205,3 @@ foreach(EX ${EXAMPLES})
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
) )
endforeach() endforeach()
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
find_program(VALGRIND
valgrind
REQUIRED
)
# run `ctest -T memcheck -V --test-dir build`
endif()

View File

@ -25,6 +25,6 @@ set (unit_test_sources
add_executable (shared_utils_test ${unit_test_sources}) add_executable (shared_utils_test ${unit_test_sources})
target_link_libraries (shared_utils_test gtest_main) target_link_libraries (shared_utils_test gtest_main ${LLVM_AVAILABLE_LIBS})
gtest_discover_tests(shared_utils_test) gtest_discover_tests(shared_utils_test)

View File

@ -94,3 +94,13 @@ TEST_F(bh_common_test_suite, b_memcpy_s)
EXPECT_EQ(-1, b_memcpy_s(dest, sizeof(dest), nullptr, sizeof(STR_TEST))); EXPECT_EQ(-1, b_memcpy_s(dest, sizeof(dest), nullptr, sizeof(STR_TEST)));
EXPECT_EQ(-1, b_memcpy_s(dest, sizeof(dest), STR_TEST, sizeof(dest) + 1)); EXPECT_EQ(-1, b_memcpy_s(dest, sizeof(dest), STR_TEST, sizeof(dest) + 1));
} }
TEST_F(bh_common_test_suite, bh_execve)
{
#if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
char *const argv[] = { "-n", "\"hello\"", "world", "\n", nullptr };
EXPECT_EQ(0, bh_execve("/usr/bin/echo", argv, 5));
#else
GTEST_SKIP() << "bh_execve() is not supported";
#endif
}