Compare commits

...

27 Commits

Author SHA1 Message Date
Alix ANNERAUD
a77ead5bdc
Merge 44486c86d2 into 0e8b57d8a8 2025-05-08 01:52:59 +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
Alix ANNERAUD
44486c86d2 docs: add instruction metering section to build_wamr documentation 2025-03-21 11:21:55 +01:00
Alix ANNERAUD
53c54b9062 fix: update instruction metering configuration for consistency and clarity 2025-03-21 11:15:08 +01:00
Alix ANNERAUD
cb5975e168 fix: update instruction limit checks to use else if for improved clarity 2025-03-18 20:34:57 +01:00
Alix ANNERAUD
e1b0411cff fix: remove unnecessary else clause in instruction limit checks for improved clarity 2025-03-18 20:34:57 +01:00
Alix ANNERAUD
446c52b8c8 fix: improve formatting of CHECK_INSTRUCTION_LIMIT macro for better readability 2025-03-18 20:34:57 +01:00
Alix ANNERAUD
76c14489c5 fix: correct module parameter in instruction limit exception handling 2025-03-18 20:34:57 +01:00
Alix ANNERAUD
56526e9776 fix: streamline HANDLE_OP macro definitions for improved readability 2025-03-18 20:34:57 +01:00
Alix ANNERAUD
0df05aff1a fix: simplify instruction limit checks for better readability 2025-03-18 20:34:57 +01:00
Alix ANNERAUD
70af781bde fix: improve instruction limit checks and enhance readability of HANDLE_OP macros 2025-03-18 20:34:57 +01:00
Alix ANNERAUD
e4ceb6b429 fix: move instructions_to_execute field in WASMExecEnv (overwriten before) 2025-03-18 20:34:57 +01:00
Alix ANNERAUD
76392b546b fix: handle null exec_env in instruction metering checks 2025-03-18 20:34:57 +01:00
Alix ANNERAUD
ad6ba3d7cf fix: update preprocessor directive for WASM_INSTRUCTION_METERING check 2025-03-18 20:34:57 +01:00
Alix ANNERAUD
a7d90ebf59 fix: format HANDLE_OP_END macro for better readability 2025-03-18 20:34:57 +01:00
Alix ANNERAUD
41bbcb7ded fix: format function declaration for wasm_runtime_set_bounds_checks 2025-03-18 20:34:57 +01:00
Alix ANNERAUD
5d34365208 fix: correct function declaration for wasm_runtime_set_instruction_count_limit 2025-03-18 20:34:57 +01:00
Alix ANNERAUD
161c945cf7 fix 2025-03-18 20:34:57 +01:00
Alix ANNERAUD
422c8fb8be fix: remove unnecessary backslash in macro definition 2025-03-18 20:34:57 +01:00
Alix ANNERAUD
0dd7fe2070 feat: initialize instruction execution limit in exec_env 2025-03-18 20:34:57 +01:00
Alix ANNERAUD
799cad659c fix: format 2025-03-18 20:34:57 +01:00
Alix ANNERAUD
8c262ba005 fix: formatting 2025-03-18 20:34:57 +01:00
Alix ANNERAUD
ca78c6644e feat: add instruction metering support with execution limit 2025-03-18 20:34:57 +01:00
16 changed files with 141 additions and 17 deletions

View File

@ -669,6 +669,10 @@ if (WAMR_BUILD_AOT_VALIDATOR EQUAL 1)
message (" AOT validator enabled")
add_definitions (-DWASM_ENABLE_AOT_VALIDATOR=1)
endif ()
if (WAMR_BUILD_INSTRUCTION_METERING EQUAL 1)
message (" Instruction metering enabled")
add_definitions (-DWASM_ENABLE_INSTRUCTION_METERING=1)
endif ()
########################################
# Show Phase4 Wasm proposals status.

View File

@ -716,4 +716,8 @@ unless used elsewhere */
#define WASM_ENABLE_AOT_VALIDATOR 0
#endif
#ifndef WASM_ENABLE_INSTRUCTION_METERING
#define WASM_ENABLE_INSTRUCTION_METERING 0
#endif
#endif /* end of _CONFIG_H_ */

View File

@ -898,6 +898,17 @@ aot_intrinsic_fill_capability_flags(AOTCompContext *comp_ctx)
if (!strncmp(comp_ctx->target_arch, "riscv32", 7)) {
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)) {
/*

View File

@ -85,6 +85,10 @@ wasm_exec_env_create_internal(struct WASMModuleInstanceCommon *module_inst,
wasm_runtime_dump_exec_env_mem_consumption(exec_env);
#endif
#if WASM_ENABLE_INSTRUCTION_METERING != 0
exec_env->instructions_to_execute = -1;
#endif
return exec_env;
#ifdef OS_ENABLE_HW_BOUND_CHECK

View File

@ -87,6 +87,11 @@ typedef struct WASMExecEnv {
uint8 *bottom;
} wasm_stack;
#if WASM_ENABLE_INSTRUCTION_METERING != 0
/* instructions to execute */
int instructions_to_execute;
#endif
#if WASM_ENABLE_FAST_JIT != 0
/**
* Cache for

View File

@ -2285,6 +2285,15 @@ wasm_runtime_access_exce_check_guard_page()
}
#endif
#if WASM_ENABLE_INSTRUCTION_METERING != 0
void
wasm_runtime_set_instruction_count_limit(WASMExecEnv *exec_env,
int instructions_to_execute)
{
exec_env->instructions_to_execute = instructions_to_execute;
}
#endif
WASMFuncType *
wasm_runtime_get_function_type(const WASMFunctionInstanceCommon *function,
uint32 module_type)

View File

@ -791,9 +791,17 @@ WASM_RUNTIME_API_EXTERN void
wasm_runtime_set_native_stack_boundary(WASMExecEnv *exec_env,
uint8 *native_stack_boundary);
#if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
#if WASM_ENABLE_INSTRUCTION_METERING != 0
/* See wasm_export.h for description */
WASM_RUNTIME_API_EXTERN void
wasm_runtime_set_instruction_count_limit(WASMExecEnv *exec_env,
int instructions_to_execute);
#endif
#if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
/* See wasm_export.h for description */
WASM_RUNTIME_API_EXTERN
void
wasm_runtime_set_bounds_checks(WASMModuleInstanceCommon *module_inst,
bool enable);

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, ".LJTI")
|| str_starts_with(relocation->symbol_name, ".LBB")
|| str_starts_with(relocation->symbol_name,
".Lswitch.table."))) {
|| str_starts_with(relocation->symbol_name, ".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
relocation->symbol_name */
LLVMSectionIteratorRef contain_section;

View File

@ -1821,6 +1821,20 @@ WASM_RUNTIME_API_EXTERN void
wasm_runtime_set_native_stack_boundary(wasm_exec_env_t exec_env,
uint8_t *native_stack_boundary);
/**
* Set the instruction count limit to the execution environment.
* By default the instruction count limit is -1, which means no limit.
* However, if the instruction count limit is set to a positive value,
* the execution will be terminated when the instruction count reaches
* the limit.
*
* @param exec_env the execution environment
* @param instruction_count the instruction count limit
*/
WASM_RUNTIME_API_EXTERN void
wasm_runtime_set_instruction_count_limit(wasm_exec_env_t exec_env,
int instruction_count);
/**
* Dump runtime memory consumption, including:
* Exec env memory consumption

View File

@ -1516,10 +1516,13 @@ wasm_interp_call_func_import(WASMModuleInstance *module_inst,
} \
os_mutex_unlock(&exec_env->wait_lock); \
} \
CHECK_INSTRUCTION_LIMIT(); \
goto *handle_table[*frame_ip++]; \
} while (0)
#else
#define HANDLE_OP_END() FETCH_OPCODE_AND_DISPATCH()
#define HANDLE_OP_END() \
CHECK_INSTRUCTION_LIMIT(); \
FETCH_OPCODE_AND_DISPATCH()
#endif
#else /* else of WASM_ENABLE_LABELS_AS_VALUES */
@ -1542,9 +1545,12 @@ wasm_interp_call_func_import(WASMModuleInstance *module_inst,
} \
os_mutex_unlock(&exec_env->wait_lock); \
} \
CHECK_INSTRUCTION_LIMIT(); \
continue;
#else
#define HANDLE_OP_END() continue
#define HANDLE_OP_END() \
CHECK_INSTRUCTION_LIMIT(); \
continue;
#endif
#endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
@ -1562,6 +1568,18 @@ get_global_addr(uint8 *global_data, WASMGlobalInstance *global)
#endif
}
#if WASM_ENABLE_INSTRUCTION_METERING != 0
#define CHECK_INSTRUCTION_LIMIT() \
if (instructions_left == 0) { \
wasm_set_exception(module, "instruction limit exceeded"); \
goto got_exception; \
} \
else if (instructions_left > 0) \
instructions_left--;
#else
#define CHECK_INSTRUCTION_LIMIT() (void)0
#endif
static void
wasm_interp_call_func_bytecode(WASMModuleInstance *module,
WASMExecEnv *exec_env,
@ -1605,6 +1623,14 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
uint32 local_idx, local_offset, global_idx;
uint8 local_type, *global_addr;
uint32 cache_index, type_index, param_cell_num, cell_num;
#if WASM_ENABLE_INSTRUCTION_METERING != 0
int instructions_left = -1;
if (exec_env) {
instructions_left = exec_env->instructions_to_execute;
}
#endif
#if WASM_ENABLE_EXCE_HANDLING != 0
int32_t exception_tag_index;
#endif

View File

@ -105,6 +105,19 @@ typedef float64 CellType_F64;
goto unaligned_atomic; \
} while (0)
#if WASM_ENABLE_INSTRUCTION_METERING != 0
#define CHECK_INSTRUCTION_LIMIT() \
if (instructions_left == 0) { \
wasm_set_exception(module, "instruction limit exceeded"); \
goto got_exception; \
} \
else if (instructions_left > 0) \
instructions_left--;
#else
#define CHECK_INSTRUCTION_LIMIT() (void)0
#endif
static inline uint32
rotl32(uint32 n, uint32 c)
{
@ -1441,6 +1454,7 @@ wasm_interp_dump_op_count()
do { \
const void *p_label_addr = *(void **)frame_ip; \
frame_ip += sizeof(void *); \
CHECK_INSTRUCTION_LIMIT(); \
goto *p_label_addr; \
} while (0)
#else
@ -1452,6 +1466,7 @@ wasm_interp_dump_op_count()
/* int32 relative offset was emitted in 64-bit target */ \
p_label_addr = label_base + (int32)LOAD_U32_WITH_2U16S(frame_ip); \
frame_ip += sizeof(int32); \
CHECK_INSTRUCTION_LIMIT(); \
goto *p_label_addr; \
} while (0)
#else
@ -1462,6 +1477,7 @@ wasm_interp_dump_op_count()
/* uint32 label address was emitted in 32-bit target */ \
p_label_addr = (void *)(uintptr_t)LOAD_U32_WITH_2U16S(frame_ip); \
frame_ip += sizeof(int32); \
CHECK_INSTRUCTION_LIMIT(); \
goto *p_label_addr; \
} while (0)
#endif
@ -1538,6 +1554,13 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
uint8 *maddr = NULL;
uint32 local_idx, local_offset, global_idx;
uint8 opcode = 0, local_type, *global_addr;
#if WASM_ENABLE_INSTRUCTION_METERING != 0
int instructions_left = -1;
if (exec_env) {
instructions_left = exec_env->instructions_to_execute;
}
#endif
#if !defined(OS_ENABLE_HW_BOUND_CHECK) \
|| WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
#if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0

View File

@ -9197,6 +9197,15 @@ preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
loader_ctx->preserved_local_offset += 2;
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, preserved_offset);
emit_label(opcode);

View File

@ -4778,6 +4778,11 @@ preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
loader_ctx->preserved_local_offset += 2;
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, preserved_offset);
emit_label(opcode);

View File

@ -327,6 +327,10 @@ And the wasm app can calls below APIs to allocate/free memory from/to the shared
- **WAMR_BUILD_SHRUNK_MEMORY**=1/0, default to enable if not set
> Note: When enabled, this feature will reduce memory usage by decreasing the size of the linear memory, particularly when the `memory.grow` opcode is not used and memory usage is somewhat predictable.
## **Instruction metering**
- **WAMR_BUILD_INSTRUCTION_METERING**=1/0, default to disable if not set
> Note: Enabling this feature allows limiting the number of instructions a wasm module instance can execute. Use the `wasm_runtime_set_instruction_count_limit(...)` API before calling `wasm_runtime_call_*(...)` APIs to enforce this limit.
## **Combination of configurations:**
We can combine the configurations. For example, if we want to disable interpreter, enable AOT and WASI, we can run command:

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
### 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
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
```

View File

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