1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
import os
from scripts.test import shared
from . import utils
class FeatureValidationTest(utils.BinaryenTestCase):
def check_feature(self, module, error, flag, const_flags=[]):
p = shared.run_process(shared.WASM_OPT +
['--mvp-features', '--print', '-o', os.devnull] +
const_flags,
input=module, check=False, capture_output=True)
self.assertIn(error, p.stderr)
self.assertIn('Fatal: error validating input', p.stderr)
self.assertNotEqual(p.returncode, 0)
p = shared.run_process(
shared.WASM_OPT + ['--mvp-features', '--print', '-o', os.devnull] +
const_flags + [flag],
input=module,
check=False,
capture_output=True)
self.assertEqual(p.returncode, 0)
def check_simd(self, module, error):
self.check_feature(module, error, '--enable-simd')
def check_sign_ext(self, module, error):
self.check_feature(module, error, '--enable-sign-ext')
def check_bulk_mem(self, module, error):
self.check_feature(module, error, '--enable-bulk-memory')
def check_exception_handling(self, module, error):
self.check_feature(module, error, '--enable-exception-handling')
def check_tail_call(self, module, error):
self.check_feature(module, error, '--enable-tail-call')
def check_reference_types(self, module, error):
self.check_feature(module, error, '--enable-reference-types')
def check_multivalue(self, module, error):
self.check_feature(module, error, '--enable-multivalue')
def check_multivalue_exception_handling(self, module, error):
self.check_feature(module, error, '--enable-multivalue',
['--enable-exception-handling'])
def check_gc(self, module, error):
# GC implies reference types
self.check_feature(module, error, '--enable-gc',
['--enable-reference-types'])
def check_typed_continuations(self, module, error):
# Typed continuations implies function references (which is provided by
# gc in binaryen, and implies reference types) and exceptions
self.check_feature(module, error, '--enable-typed-continuations',
['--enable-gc', '--enable-reference-types', '--enable-exception-handling'])
def test_v128_signature(self):
module = '''
(module
(func $foo (param $0 v128) (result v128)
(local.get $0)
)
)
'''
self.check_simd(module, 'all used types should be allowed')
def test_v128_global(self):
module = '''
(module
(global $foo (mut v128) (v128.const i32x4 0 0 0 0))
)
'''
self.check_simd(module, 'all used types should be allowed')
def test_v128_local(self):
module = '''
(module
(func $foo
(local v128)
)
)
'''
self.check_simd(module, 'all used types should be allowed')
def test_simd_const(self):
module = '''
(module
(func $foo
(drop (v128.const i32x4 0 0 0 0))
)
)
'''
self.check_simd(module, 'all used features should be allowed')
def test_simd_load(self):
module = '''
(module
(memory 1 1)
(func $foo
(drop (v128.load (i32.const 0)))
)
)
'''
self.check_simd(module, 'SIMD operations require SIMD [--enable-simd]')
def test_simd_splat(self):
module = '''
(module
(func $foo
(drop (i32x4.splat (i32.const 0)))
)
)
'''
self.check_simd(module, 'all used features should be allowed')
def test_sign_ext(self):
module = '''
(module
(func $foo
(drop (i32.extend8_s (i32.const 7)))
)
)
'''
self.check_sign_ext(module, 'all used features should be allowed')
def test_bulk_mem_inst(self):
module = '''
(module
(memory 1 1)
(func $foo
(memory.copy (i32.const 0) (i32.const 8) (i32.const 8))
)
)
'''
self.check_bulk_mem(module,
'Bulk memory operations require bulk memory [--enable-bulk-memory]')
def test_bulk_mem_segment(self):
module = '''
(module
(memory 256 256)
(data "42")
)
'''
self.check_bulk_mem(module, 'nonzero segment flags require bulk memory [--enable-bulk-memory]')
def test_tail_call(self):
module = '''
(module
(func $bar)
(func $foo
(return_call $bar)
)
)
'''
self.check_tail_call(module, 'return_call* requires tail calls [--enable-tail-call]')
def test_tail_call_indirect(self):
module = '''
(module
(type $T (func))
(table $0 1 1 funcref)
(func $foo
(return_call_indirect (type $T)
(i32.const 0)
)
)
)
'''
self.check_tail_call(module, 'return_call* requires tail calls [--enable-tail-call]')
def test_reference_types_externref(self):
module = '''
(module
(import "env" "test1" (func $test1 (param externref) (result externref)))
(import "env" "test2" (global $test2 externref))
(export "test1" (func $test1))
(export "test2" (global $test2))
(func $externref_test (param $0 externref) (result externref)
(return
(call $test1
(local.get $0)
)
)
)
)
'''
self.check_reference_types(module, 'all used types should be allowed')
def test_tag(self):
module = '''
(module
(tag $e (param i32))
(func $foo
(throw $e (i32.const 0))
)
)
'''
self.check_exception_handling(module, 'Tags require exception-handling [--enable-exception-handling]')
def test_multivalue_import(self):
module = '''
(module
(import "env" "foo" (func $foo (result i32 i64)))
)
'''
self.check_multivalue(module, 'Imported multivalue function requires multivalue [--enable-multivalue]')
def test_multivalue_function(self):
module = '''
(module
(func $foo (result i32 i64)
(tuple.make 2
(i32.const 42)
(i64.const 42)
)
)
)
'''
self.check_multivalue(module, 'Multivalue function results ' +
'(multivalue is not enabled)')
def test_multivalue_tag(self):
module = '''
(module
(tag $foo (param i32 i64))
)
'''
self.check_multivalue_exception_handling(module, 'Multivalue tag type requires multivalue [--enable-multivalue]')
def test_multivalue_block(self):
module = '''
(module
(func $foo
(tuple.drop 2
(block (result i32 i64)
(tuple.make 2
(i32.const 42)
(i64.const 42)
)
)
)
)
)
'''
self.check_multivalue(module, 'Multivalue block type require multivalue [--enable-multivalue]')
def test_i31_global(self):
module = '''
(module
(global $foo (ref null i31) (ref.null i31))
)
'''
self.check_gc(module, 'all used types should be allowed')
def test_i31_local(self):
module = '''
(module
(func $foo
(local $0 (ref null i31))
)
)
'''
self.check_gc(module, 'all used types should be allowed')
def test_eqref_global(self):
module = '''
(module
(global $foo eqref (ref.null eq))
)
'''
self.check_gc(module, 'all used types should be allowed')
def test_eqref_local(self):
module = '''
(module
(func $foo
(local $0 eqref)
)
)
'''
self.check_gc(module, 'all used types should be allowed')
def test_tag_results(self):
module = '''
(module
(tag $foo (result i32))
)
'''
self.check_typed_continuations(module,
'Tags with result types require typed '
'continuations feature [--enable-typed-continuations]')
def test_cont_type(self):
module = '''
(module
(type $ft (func (param i32) (result i32)))
(type $ct (cont $ft))
(func $foo
(local $0 (ref $ct))
)
)
'''
self.check_typed_continuations(module, 'all used types should be allowed')
class TargetFeaturesSectionTest(utils.BinaryenTestCase):
def test_atomics(self):
filename = 'atomics_target_feature.wasm'
self.roundtrip(filename)
self.check_features(filename, ['threads'])
self.assertIn('i32.atomic.rmw.add', self.disassemble(filename))
def test_bulk_memory(self):
filename = 'bulkmem_target_feature.wasm'
self.roundtrip(filename)
self.check_features(filename, ['bulk-memory'])
self.assertIn('memory.copy', self.disassemble(filename))
def test_nontrapping_fptoint(self):
filename = 'truncsat_target_feature.wasm'
self.roundtrip(filename)
self.check_features(filename, ['nontrapping-float-to-int'])
self.assertIn('i32.trunc_sat_f32_u', self.disassemble(filename))
def test_mutable_globals(self):
filename = 'mutable_globals_target_feature.wasm'
self.roundtrip(filename)
self.check_features(filename, ['mutable-globals'])
self.assertIn('(import "env" "global-mut" (global $gimport$0 (mut i32)))',
self.disassemble(filename))
def test_sign_ext(self):
filename = 'signext_target_feature.wasm'
self.roundtrip(filename)
self.check_features(filename, ['sign-ext'])
self.assertIn('i32.extend8_s', self.disassemble(filename))
def test_simd(self):
filename = 'simd_target_feature.wasm'
self.roundtrip(filename)
self.check_features(filename, ['simd'])
self.assertIn('i32x4.splat', self.disassemble(filename))
def test_tailcall(self):
filename = 'tail_call_target_feature.wasm'
self.roundtrip(filename)
self.check_features(filename, ['tail-call'])
self.assertIn('return_call', self.disassemble(filename))
def test_reference_types(self):
filename = 'reference_types_target_feature.wasm'
self.roundtrip(filename)
self.check_features(filename, ['reference-types'])
self.assertIn('anyref', self.disassemble(filename))
def test_exception_handling(self):
filename = 'exception_handling_target_feature.wasm'
self.roundtrip(filename)
self.check_features(filename, ['exception-handling'])
self.assertIn('throw', self.disassemble(filename))
def test_gc(self):
filename = 'gc_target_feature.wasm'
self.roundtrip(filename)
self.check_features(filename, ['reference-types', 'gc'])
disassembly = self.disassemble(filename)
self.assertIn('externref', disassembly)
self.assertIn('eqref', disassembly)
def test_superset(self):
# It is ok to enable additional features past what is in the section.
shared.run_process(
shared.WASM_OPT + ['--print', '--detect-features', '-mvp',
'--enable-simd', '--enable-sign-ext',
self.input_path('signext_target_feature.wasm')])
def test_superset_even_without_detect_features(self):
# It is ok to enable additional features past what is in the section,
# even without passing --detect-features (which is now a no-op).
path = self.input_path('signext_target_feature.wasm')
shared.run_process(
shared.WASM_OPT + ['--print', '--enable-simd', '-o', os.devnull,
path])
def test_superset_with_detect_features(self):
path = self.input_path('signext_target_feature.wasm')
shared.run_process(
shared.WASM_OPT + ['--print', '--detect-features',
'--enable-simd', '-o', os.devnull, path])
def test_explicit_detect_features(self):
self.check_features('signext_target_feature.wasm', ['simd', 'sign-ext'],
opts=['-mvp', '--detect-features', '--enable-simd'])
def test_emit_all_features(self):
p = shared.run_process(shared.WASM_OPT +
['--emit-target-features', '-all', '-o', '-'],
input="(module)", check=False,
capture_output=True, decode_output=False)
self.assertEqual(p.returncode, 0)
p2 = shared.run_process(shared.WASM_OPT +
['--print-features', '-o', os.devnull],
input=p.stdout, check=False,
capture_output=True)
self.assertEqual(p2.returncode, 0)
self.assertEqual([
'--enable-threads',
'--enable-mutable-globals',
'--enable-nontrapping-float-to-int',
'--enable-simd',
'--enable-bulk-memory',
'--enable-sign-ext',
'--enable-exception-handling',
'--enable-tail-call',
'--enable-reference-types',
'--enable-multivalue',
'--enable-gc',
'--enable-memory64',
'--enable-relaxed-simd',
'--enable-extended-const',
'--enable-strings',
'--enable-multimemory',
'--enable-typed-continuations',
'--enable-shared-everything',
'--enable-fp16',
], p2.stdout.splitlines())
|