Skip to content

Commit 4151fe0

Browse files
committed
wasm2c: Add macro and tests to disable Wasm's force_read
1 parent 9c09684 commit 4151fe0

13 files changed

+37
-23
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ jobs:
172172
runs-on: ubuntu-latest
173173
env:
174174
USE_NINJA: "1"
175-
WASM2C_CFLAGS: "-DWASM_RT_USE_MMAP=1 -DWASM_RT_SKIP_SIGNAL_RECOVERY=1 -DWASM_RT_NONCONFORMING_UNCHECKED_STACK_EXHAUSTION=1 -DWASM2C_TEST_EMBEDDER_SIGNAL_HANDLING"
175+
WASM2C_CFLAGS: "-DWASM_RT_USE_MMAP=1 -DWASM_RT_SKIP_SIGNAL_RECOVERY=1 -DWASM_RT_NONCONFORMING_UNCHECKED_STACK_EXHAUSTION=1 -DWASM2C_NONCONFORMING_LOAD_ELISION=1 -DWASM2C_TEST_EMBEDDER_SIGNAL_HANDLING"
176176
steps:
177177
- uses: actions/setup-python@v1
178178
with:
@@ -182,5 +182,5 @@ jobs:
182182
submodules: true
183183
- run: sudo apt-get install ninja-build
184184
- run: make clang-debug
185-
- name: tests (wasm2c tests excluding memory64)
186-
run: ./test/run-tests.py *wasm2c* --exclude-dir memory64
185+
- name: tests (wasm2c tests excluding memory64 and unobserved read elimination)
186+
run: ./test/run-tests.py *wasm2c* --exclude memory64 --exclude address-overflow.txt --exclude address.txt --exclude address0.txt --exclude address1.txt --exclude traps.txt --exclude traps0.txt --exclude simd_address.txt

src/prebuilt/wasm2c_simd_source_declarations.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const char* s_simd_source_declarations = R"w2c_template(#if defined(__GNUC__) && defined(__x86_64__)
1+
const char* s_simd_source_declarations = R"w2c_template(#if defined(__GNUC__) && defined(__x86_64__) && \
2+
)w2c_template"
3+
R"w2c_template( !WASM2C_NONCONFORMING_LOAD_ELISION
24
)w2c_template"
35
R"w2c_template(#define SIMD_FORCE_READ(var) __asm__("" ::"x"(var));
46
)w2c_template"

src/prebuilt/wasm2c_source_declarations.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ R"w2c_template(#define MEMCHECK(mem, a, t) RANGE_CHECK(mem, a, sizeof(t))
135135
R"w2c_template(#endif
136136
)w2c_template"
137137
R"w2c_template(
138-
#ifdef __GNUC__
138+
#if defined(__GNUC__) && !WASM2C_NONCONFORMING_LOAD_ELISION
139139
)w2c_template"
140140
R"w2c_template(#define FORCE_READ_INT(var) __asm__("" ::"r"(var));
141141
)w2c_template"

src/template/wasm2c.declarations.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static inline bool func_types_eq(const wasm_rt_func_type_t a,
7373
#define MEMCHECK(mem, a, t) RANGE_CHECK(mem, a, sizeof(t))
7474
#endif
7575

76-
#ifdef __GNUC__
76+
#if defined(__GNUC__) && !WASM2C_NONCONFORMING_LOAD_ELISION
7777
#define FORCE_READ_INT(var) __asm__("" ::"r"(var));
7878
// Clang on Mips requires "f" constraints on floats
7979
// See https://github.com/llvm/llvm-project/issues/64241

src/template/wasm2c_simd.declarations.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#if defined(__GNUC__) && defined(__x86_64__)
1+
#if defined(__GNUC__) && defined(__x86_64__) && \
2+
!WASM2C_NONCONFORMING_LOAD_ELISION
23
#define SIMD_FORCE_READ(var) __asm__("" ::"x"(var));
34
#else
45
#define SIMD_FORCE_READ(var)

test/run-tests.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -684,17 +684,18 @@ def _Clear(self):
684684
sys.stderr.write('\r%s\r' % (' ' * self.last_length))
685685

686686

687-
def FindTestFiles(ext, filter_pattern_re, exclude_dirs):
687+
def FindTestFiles(ext, filter_pattern_re, excludes):
688688
tests = []
689689
for root, dirs, files in os.walk(TEST_DIR):
690-
for ex in exclude_dirs:
690+
for ex in excludes:
691691
if ex in dirs:
692692
# Filtering out dirs here causes os.walk not to descend into them
693693
dirs.remove(ex)
694694
for f in files:
695-
path = os.path.join(root, f)
696-
if os.path.splitext(f)[1] == ext:
697-
tests.append(os.path.relpath(path, REPO_ROOT_DIR))
695+
if f not in excludes:
696+
path = os.path.join(root, f)
697+
if os.path.splitext(f)[1] == ext:
698+
tests.append(os.path.relpath(path, REPO_ROOT_DIR))
698699
tests.sort()
699700
return [test for test in tests if re.match(filter_pattern_re, test)]
700701

@@ -928,10 +929,10 @@ def main(args):
928929
action='store_true')
929930
parser.add_argument('patterns', metavar='pattern', nargs='*',
930931
help='test patterns.')
931-
parser.add_argument('--exclude-dir', action='append', default=[],
932-
help='directory to exclude.')
932+
parser.add_argument('--exclude', action='append', default=[],
933+
help='file or directory names to exclude.')
933934
options = parser.parse_args(args)
934-
exclude_dirs = options.exclude_dir
935+
excludes = options.exclude
935936

936937
if options.jobs != 1:
937938
if options.fail_fast:
@@ -947,9 +948,9 @@ def main(args):
947948
# By default, exclude wasi tests because WASI support is not include
948949
# by int the build by default.
949950
# TODO(sbc): Find some way to detect the WASI support.
950-
exclude_dirs += ['wasi']
951+
excludes += ['wasi']
951952

952-
test_names = FindTestFiles('.txt', pattern_re, exclude_dirs)
953+
test_names = FindTestFiles('.txt', pattern_re, excludes)
953954

954955
if options.list:
955956
for test_name in test_names:

test/wasm2c/add.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ static inline bool func_types_eq(const wasm_rt_func_type_t a,
140140
#define MEMCHECK(mem, a, t) RANGE_CHECK(mem, a, sizeof(t))
141141
#endif
142142

143-
#ifdef __GNUC__
143+
#if defined(__GNUC__) && !WASM2C_NONCONFORMING_LOAD_ELISION
144144
#define FORCE_READ_INT(var) __asm__("" ::"r"(var));
145145
// Clang on Mips requires "f" constraints on floats
146146
// See https://github.com/llvm/llvm-project/issues/64241

test/wasm2c/check-imports.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ static inline bool func_types_eq(const wasm_rt_func_type_t a,
163163
#define MEMCHECK(mem, a, t) RANGE_CHECK(mem, a, sizeof(t))
164164
#endif
165165

166-
#ifdef __GNUC__
166+
#if defined(__GNUC__) && !WASM2C_NONCONFORMING_LOAD_ELISION
167167
#define FORCE_READ_INT(var) __asm__("" ::"r"(var));
168168
// Clang on Mips requires "f" constraints on floats
169169
// See https://github.com/llvm/llvm-project/issues/64241

test/wasm2c/export-names.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ static inline bool func_types_eq(const wasm_rt_func_type_t a,
163163
#define MEMCHECK(mem, a, t) RANGE_CHECK(mem, a, sizeof(t))
164164
#endif
165165

166-
#ifdef __GNUC__
166+
#if defined(__GNUC__) && !WASM2C_NONCONFORMING_LOAD_ELISION
167167
#define FORCE_READ_INT(var) __asm__("" ::"r"(var));
168168
// Clang on Mips requires "f" constraints on floats
169169
// See https://github.com/llvm/llvm-project/issues/64241

test/wasm2c/hello.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ static inline bool func_types_eq(const wasm_rt_func_type_t a,
171171
#define MEMCHECK(mem, a, t) RANGE_CHECK(mem, a, sizeof(t))
172172
#endif
173173

174-
#ifdef __GNUC__
174+
#if defined(__GNUC__) && !WASM2C_NONCONFORMING_LOAD_ELISION
175175
#define FORCE_READ_INT(var) __asm__("" ::"r"(var));
176176
// Clang on Mips requires "f" constraints on floats
177177
// See https://github.com/llvm/llvm-project/issues/64241

test/wasm2c/minimal.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ static inline bool func_types_eq(const wasm_rt_func_type_t a,
134134
#define MEMCHECK(mem, a, t) RANGE_CHECK(mem, a, sizeof(t))
135135
#endif
136136

137-
#ifdef __GNUC__
137+
#if defined(__GNUC__) && !WASM2C_NONCONFORMING_LOAD_ELISION
138138
#define FORCE_READ_INT(var) __asm__("" ::"r"(var));
139139
// Clang on Mips requires "f" constraints on floats
140140
// See https://github.com/llvm/llvm-project/issues/64241

test/wasm2c/tail-calls.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ static inline bool func_types_eq(const wasm_rt_func_type_t a,
164164
#define MEMCHECK(mem, a, t) RANGE_CHECK(mem, a, sizeof(t))
165165
#endif
166166

167-
#ifdef __GNUC__
167+
#if defined(__GNUC__) && !WASM2C_NONCONFORMING_LOAD_ELISION
168168
#define FORCE_READ_INT(var) __asm__("" ::"r"(var));
169169
// Clang on Mips requires "f" constraints on floats
170170
// See https://github.com/llvm/llvm-project/issues/64241

wasm2c/wasm-rt.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,16 @@ extern "C" {
174174
#define WASM_RT_NONCONFORMING_UNCHECKED_STACK_EXHAUSTION 0
175175
#endif
176176

177+
/* This macro, if defined, allows the embedder to specify that wasm2c can remove
178+
* linear memory reads which are unused. Normally, Wasm requires does not allow
179+
* reads to be eliminated when using guard pages, as OOB reads must still trap.
180+
* This a non conformant configuration, i.e., this does not respect Wasm's
181+
* specification. Use with caution.
182+
*/
183+
#ifndef WASM2C_NONCONFORMING_LOAD_ELISION
184+
#define WASM2C_NONCONFORMING_LOAD_ELISION 0
185+
#endif
186+
177187
/* We need to detect and trap stack overflows. If we use a signal handler on
178188
* POSIX systems, this can detect call stack overflows. On windows, or platforms
179189
* without a signal handler, we use stack depth counting. */

0 commit comments

Comments
 (0)