Skip to content

Commit 996758c

Browse files
authored
Remove the dlen to optimize it. (#4193)
There are two reasons for this optimization: - The value of dlen can equal 0x1_0000_0000, even in wasm32 mode, because it is derived from (4G-0). This results in a truncation when it is passed to b_memmove_s(). Consequently, s1max becomes 0 and n is greater than s1max. To correct this, a longer type is required. - The dlen is only used to check if there is enough space in b_memmove_s(). However, from a different angle, after confirming that both src+len and dst+len are within the memory range, we can be assured and there is no need for this explicit check.
1 parent fc1527e commit 996758c

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

core/iwasm/interpreter/wasm_interp_classic.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5784,7 +5784,6 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
57845784
{
57855785
mem_offset_t dst, src, len;
57865786
uint8 *mdst, *msrc;
5787-
uint64 dlen;
57885787

57895788
len = POP_MEM_OFFSET();
57905789
src = POP_MEM_OFFSET();
@@ -5797,24 +5796,17 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
57975796
/* skip dst memidx */
57985797
frame_ip += 1;
57995798
#endif
5799+
// TODO: apply memidx
58005800
#if WASM_ENABLE_THREAD_MGR != 0
58015801
linear_mem_size = get_linear_mem_size();
58025802
#endif
5803-
5804-
dlen = linear_mem_size - dst;
5805-
58065803
/* dst boundary check */
58075804
#ifndef OS_ENABLE_HW_BOUND_CHECK
58085805
CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
5809-
#if WASM_ENABLE_SHARED_HEAP != 0
5810-
if (app_addr_in_shared_heap((uint64)dst, len))
5811-
dlen = shared_heap_end_off - dst + 1;
5812-
#endif
58135806
#else /* else of OS_ENABLE_HW_BOUND_CHECK */
58145807
#if WASM_ENABLE_SHARED_HEAP != 0
58155808
if (app_addr_in_shared_heap((uint64)dst, len)) {
58165809
shared_heap_addr_app_to_native((uint64)dst, mdst);
5817-
dlen = shared_heap_end_off - dst + 1;
58185810
}
58195811
else
58205812
#endif
@@ -5832,6 +5824,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
58325824
/* skip src memidx */
58335825
frame_ip += 1;
58345826
#endif
5827+
// TODO: apply memidx
58355828
#if WASM_ENABLE_THREAD_MGR != 0
58365829
linear_mem_size = get_linear_mem_size();
58375830
#endif
@@ -5851,15 +5844,21 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
58515844
}
58525845
#endif
58535846

5854-
#if WASM_ENABLE_MEMORY64 == 0
5855-
/* allowing the destination and source to overlap */
5856-
bh_memmove_s(mdst, (uint32)dlen, msrc, (uint32)len);
5857-
#else
5858-
/* use memmove when memory64 is enabled since len
5859-
may be larger than UINT32_MAX */
5860-
memmove(mdst, msrc, len);
5861-
(void)dlen;
5862-
#endif
5847+
/*
5848+
* avoid unnecessary operations
5849+
*
5850+
* since dst and src both are valid indexes in the
5851+
* linear memory, mdst and msrc can't be NULL
5852+
*
5853+
* The spec. converts memory.copy into i32.load8 and
5854+
* i32.store8; the following are runtime-specific
5855+
* optimizations.
5856+
*
5857+
*/
5858+
if (len && mdst != msrc) {
5859+
/* allowing the destination and source to overlap */
5860+
memmove(mdst, msrc, len);
5861+
}
58635862
break;
58645863
}
58655864
case WASM_OP_MEMORY_FILL:

core/iwasm/interpreter/wasm_interp_fast.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5163,7 +5163,6 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
51635163
{
51645164
uint32 dst, src, len;
51655165
uint8 *mdst, *msrc;
5166-
uint64 dlen;
51675166

51685167
len = POP_I32();
51695168
src = POP_I32();
@@ -5173,15 +5172,9 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
51735172
linear_mem_size = get_linear_mem_size();
51745173
#endif
51755174

5176-
dlen = linear_mem_size - dst;
5177-
51785175
#ifndef OS_ENABLE_HW_BOUND_CHECK
51795176
CHECK_BULK_MEMORY_OVERFLOW(src, len, msrc);
51805177
CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
5181-
#if WASM_ENABLE_SHARED_HEAP != 0
5182-
if (app_addr_in_shared_heap((uint64)dst, len))
5183-
dlen = shared_heap_end_off - dst + 1;
5184-
#endif
51855178
#else /* else of OS_ENABLE_HW_BOUND_CHECK */
51865179
#if WASM_ENABLE_SHARED_HEAP != 0
51875180
if (app_addr_in_shared_heap((uint64)src, len))
@@ -5197,7 +5190,6 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
51975190
#if WASM_ENABLE_SHARED_HEAP != 0
51985191
if (app_addr_in_shared_heap((uint64)dst, len)) {
51995192
shared_heap_addr_app_to_native((uint64)dst, mdst);
5200-
dlen = shared_heap_end_off - dst + 1;
52015193
}
52025194
else
52035195
#endif
@@ -5208,8 +5200,21 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
52085200
}
52095201
#endif /* end of OS_ENABLE_HW_BOUND_CHECK */
52105202

5211-
/* allowing the destination and source to overlap */
5212-
bh_memmove_s(mdst, (uint32)dlen, msrc, len);
5203+
/*
5204+
* avoid unnecessary operations
5205+
*
5206+
* since dst and src both are valid indexes in the
5207+
* linear memory, mdst and msrc can't be NULL
5208+
*
5209+
* The spec. converts memory.copy into i32.load8 and
5210+
* i32.store8; the following are runtime-specific
5211+
* optimizations.
5212+
*
5213+
*/
5214+
if (len && mdst != msrc) {
5215+
/* allowing the destination and source to overlap */
5216+
memmove(mdst, msrc, len);
5217+
}
52135218
break;
52145219
}
52155220
case WASM_OP_MEMORY_FILL:
@@ -7488,7 +7493,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
74887493
#if WASM_ENABLE_LABELS_AS_VALUES == 0
74897494
continue;
74907495
#else
7491-
FETCH_OPCODE_AND_DISPATCH();
7496+
FETCH_OPCODE_AND_DISPATCH();
74927497
#endif
74937498

74947499
#if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0

0 commit comments

Comments
 (0)