Skip to content

Commit 793135b

Browse files
authored
Fix few integer overflowing (#4161)
- fix(interpreter): correct offset calculations in wasm_loader_get_const_offset function - fix(mem-alloc): update offset calculation in gc_migrate for memory migration - add pointer-overflow sanitizer
1 parent 8fe98f6 commit 793135b

File tree

5 files changed

+48
-11
lines changed

5 files changed

+48
-11
lines changed

build-scripts/config_common.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ elseif (WAMR_BUILD_SANITIZER STREQUAL "asan")
157157
elseif (WAMR_BUILD_SANITIZER STREQUAL "tsan")
158158
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fno-omit-frame-pointer -fsanitize=thread -fno-sanitize-recover=all" )
159159
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread")
160+
elseif (WAMR_BUILD_SANITIZER STREQUAL "posan")
161+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fno-omit-frame-pointer -fsanitize=pointer-overflow -fno-sanitize-recover=all" )
162+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=pointer-overflow")
160163
elseif (NOT (WAMR_BUILD_SANITIZER STREQUAL "") )
161164
message(SEND_ERROR "Unsupported sanitizer: ${WAMR_BUILD_SANITIZER}")
162165
endif()

core/iwasm/interpreter/wasm_loader.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9693,8 +9693,10 @@ wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
96939693
*offset = 0;
96949694
return true;
96959695
}
9696-
*offset = -(uint32)(ctx->i64_const_num * 2 + ctx->i32_const_num)
9697-
+ (uint32)(i64_const - ctx->i64_consts) * 2;
9696+
9697+
/* constant index is encoded as negative value */
9698+
*offset = -(int32)(ctx->i64_const_num * 2 + ctx->i32_const_num)
9699+
+ (int32)(i64_const - ctx->i64_consts) * 2;
96989700
}
96999701
else if (type == VALUE_TYPE_V128) {
97009702
V128 key = *(V128 *)value, *v128_const;
@@ -9704,9 +9706,12 @@ wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
97049706
*offset = 0;
97059707
return true;
97069708
}
9707-
*offset = -(uint32)(ctx->v128_const_num)
9708-
+ (uint32)(v128_const - ctx->v128_consts);
9709+
9710+
/* constant index is encoded as negative value */
9711+
*offset = -(int32)(ctx->v128_const_num)
9712+
+ (int32)(v128_const - ctx->v128_consts);
97099713
}
9714+
97109715
else {
97119716
int32 key = *(int32 *)value, *i32_const;
97129717
i32_const = bsearch(&key, ctx->i32_consts, ctx->i32_const_num,
@@ -9715,8 +9720,10 @@ wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
97159720
*offset = 0;
97169721
return true;
97179722
}
9718-
*offset = -(uint32)(ctx->i32_const_num)
9719-
+ (uint32)(i32_const - ctx->i32_consts);
9723+
9724+
/* constant index is encoded as negative value */
9725+
*offset = -(int32)(ctx->i32_const_num)
9726+
+ (int32)(i32_const - ctx->i32_consts);
97209727
}
97219728

97229729
return true;

core/shared/mem-alloc/ems/ems_kfc.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,28 @@ gc_get_heap_struct_size()
208208
static void
209209
adjust_ptr(uint8 **p_ptr, intptr_t offset)
210210
{
211-
if (*p_ptr)
212-
*p_ptr = (uint8 *)((intptr_t)(*p_ptr) + offset);
211+
if ((!*p_ptr)) {
212+
return;
213+
}
214+
215+
/*
216+
* to resolve a possible signed integer overflow issue
217+
* when p_ptr is over 0x8000000000000000 by not using
218+
* `(intptr_t)`
219+
*/
220+
uintptr_t offset_val = 0;
221+
#if UINTPTR_MAX == UINT64_MAX
222+
offset_val = labs(offset);
223+
#else
224+
offset_val = abs(offset);
225+
#endif
226+
227+
if (offset > 0) {
228+
*p_ptr = (uint8 *)((uintptr_t)(*p_ptr) + offset_val);
229+
}
230+
else {
231+
*p_ptr = (uint8 *)((uintptr_t)(*p_ptr) - offset_val);
232+
}
213233
}
214234

215235
int

tests/wamr-test-suites/spec-test-script/runtest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,7 +1575,8 @@ def recently_added_wasm(temp_file_repo):
15751575
try:
15761576
if not opts.no_cleanup:
15771577
# remove the files under /tempfiles/ and copy of .wasm files
1578-
log(f"Removing {temp_file_repo}")
1578+
log(f"Removing tmp*")
1579+
# log(f"Removing {temp_file_repo}")
15791580

15801581
for t in temp_file_repo:
15811582
# None and empty
@@ -1585,7 +1586,8 @@ def recently_added_wasm(temp_file_repo):
15851586
if os.path.exists(t):
15861587
os.remove(t)
15871588
else:
1588-
log(f"Leaving {temp_file_repo}")
1589+
log(f"Leaving tmp*")
1590+
# log(f"Leaving {temp_file_repo}")
15891591

15901592
except Exception as e:
15911593
print("Failed to remove tempfiles: %s" % e)

tests/wamr-test-suites/test_wamr.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function help()
3939
echo "-F set the firmware path used by qemu"
4040
echo "-C enable code coverage collect"
4141
echo "-j set the platform to test"
42-
echo "-T set sanitizer to use in tests(ubsan|tsan|asan)"
42+
echo "-T set sanitizer to use in tests(ubsan|tsan|asan|posan)"
4343
echo "-A use the specified wamrc command instead of building it"
4444
echo "-r [requirement name] [N [N ...]] specify a requirement name followed by one or more"
4545
echo " subrequirement IDs, if no subrequirement is specificed,"
@@ -1035,6 +1035,11 @@ function trigger()
10351035
EXTRA_COMPILE_FLAGS+=" -DWAMR_BUILD_SANITIZER=tsan"
10361036
fi
10371037

1038+
if [[ "$WAMR_BUILD_SANITIZER" == "posan" ]]; then
1039+
echo "Setting run with posan"
1040+
EXTRA_COMPILE_FLAGS+=" -DWAMR_BUILD_SANITIZER=posan"
1041+
fi
1042+
10381043
# Make sure we're using the builtin WASI libc implementation
10391044
# if we're running the wasi certification tests.
10401045
if [[ $TEST_CASE_ARR ]]; then

0 commit comments

Comments
 (0)