Skip to content

Commit

Permalink
wasm2c: Fix test harness UB with SIMD
Browse files Browse the repository at this point in the history
  • Loading branch information
SoniEx2 committed Nov 11, 2023
1 parent e97d53c commit bb3ef39
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
15 changes: 15 additions & 0 deletions test/harness/wasm2c/simd_formatting.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
;;; TOOL: run-spec-wasm2c
;;; ERROR: 1
(module
(func (export "x") (param $x v128) (result v128) (local.get $x))
)
(assert_return (invoke "x" (v128.const i8x16 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0A 0x0B 0x0C 0x0D 0x0E 0x0F))
(v128.const i8x16 0xFF 0xFE 0xFD 0xFC 0xFB 0xFA 0xF9 0xF8 0xF7 0xF6 0xF5 0xF4 0xF3 0xF2 0xF1 0xF0))
(;; STDERR ;;;
Error running "out/test/harness/wasm2c/simd_formatting/simd_formatting" (1):
None
out/test/harness/wasm2c/simd_formatting/simd_formatting-main.c:384: assertion failed: in w2c_simd__formatting__0__wasm_x(&simd__formatting__0__wasm_instance, simde_wasm_i8x16_make(0u,1u,2u,3u,4u,5u,6u,7u,8u,9u,10u,11u,12u,13u,14u,15u)): expected <255 254 253 252 251 250 249 248 247 246 245 244 243 242 241 240 >, got <0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 >.
;;; STDERR ;;)
(;; STDOUT ;;;
0/1 tests passed.
;;; STDOUT ;;)
7 changes: 5 additions & 2 deletions test/run-spec-wasm2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ def _SIMDConstantList(self, const):
return ', '.join(self._Constant({'type': const['lane_type'], 'value': val}) for val in const['value'])

def _SIMDFound(self, num, lane_type, lane_count):
return 'simde_wasm_%sx%d_extract_lane(actual, %d)' % (lane_type, lane_count, num)
return 'v128_%sx%d_extract_lane(actual, %d)' % (lane_type, lane_count, num)

def _SIMDFoundList(self, lane_type, lane_count):
return ', '.join(self._SIMDFound(num, lane_type, lane_count) for num in range(lane_count))
Expand Down Expand Up @@ -644,7 +644,10 @@ def main(args):

# Run the resulting binary
if options.run:
utils.Executable(main_exe, forward_stdout=True).RunWithArgs()
error = utils.Executable(main_exe, forward_stdout=True).RunWithArgsAndError()
if error:
print(error, file=sys.stderr)
return 1

return 0

Expand Down
16 changes: 14 additions & 2 deletions test/spec-wasm2c-prefix.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
#include "wasm-rt-impl.h"
#include "wasm-rt-exceptions.h"

// like is_equal_TYPE below, always use unsigned for these
#define v128_i8x16_extract_lane simde_wasm_u8x16_extract_lane
#define v128_u8x16_extract_lane simde_wasm_u8x16_extract_lane
#define v128_i16x8_extract_lane simde_wasm_u16x8_extract_lane
#define v128_u16x8_extract_lane simde_wasm_u16x8_extract_lane
#define v128_i32x4_extract_lane simde_wasm_u32x4_extract_lane
#define v128_u32x4_extract_lane simde_wasm_u32x4_extract_lane
#define v128_i64x2_extract_lane simde_wasm_u64x2_extract_lane
#define v128_u64x2_extract_lane simde_wasm_u64x2_extract_lane
#define v128_f32x4_extract_lane simde_wasm_f32x4_extract_lane
#define v128_f64x2_extract_lane simde_wasm_f64x2_extract_lane

static int g_tests_run;
static int g_tests_passed;

Expand Down Expand Up @@ -143,8 +155,8 @@ static void error(const char* file, int line, const char* format, ...) {

#define MULTI_T_UNPACK_(...) __VA_ARGS__
#define MULTI_T_UNPACK(arg) MULTI_T_UNPACK_ arg
#define MULTI_i8 "%su "
#define MULTI_i16 "%su "
#define MULTI_i8 "%" PRIu8 " "
#define MULTI_i16 "%" PRIu16 " "
#define MULTI_i32 "%u "
#define MULTI_i64 "%" PRIu64 " "
#define MULTI_f32 "%.9g "
Expand Down
7 changes: 7 additions & 0 deletions test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ def RunWithArgs(self, *args, **kwargs):
if error:
raise error

def RunWithArgsAndError(self, *args, **kwargs):
stdout, stderr, error = self._RunWithArgsInternal(*args, **kwargs)
if stdout:
sys.stdout.write(stdout)
if error:
return error

def AppendArg(self, arg):
self.after_args.append(arg)

Expand Down

0 comments on commit bb3ef39

Please sign in to comment.