2
0

invalid_global_mut.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. 'use strict';
  6. let soft_validate = true;
  7. let spectest = {
  8. print: print || ((...xs) => console.log(...xs)),
  9. global: 666,
  10. table: new WebAssembly.Table({initial: 10, maximum: 20, element: 'anyfunc'}), memory: new WebAssembly.Memory({initial: 1, maximum: 2}),};
  11. let registry = {spectest};
  12. let $$;
  13. function register(name, instance) {
  14. registry[name] = instance.exports;
  15. }
  16. function module(bytes) {
  17. let buffer = new ArrayBuffer(bytes.length);
  18. let view = new Uint8Array(buffer);
  19. for (let i = 0; i < bytes.length; ++i) {
  20. view[i] = bytes.charCodeAt(i);
  21. }
  22. return new WebAssembly.Module(buffer);
  23. }
  24. function instance(bytes, imports = registry) {
  25. return new WebAssembly.Instance(module(bytes), imports);
  26. }
  27. function assert_malformed(bytes) {
  28. try { module(bytes) } catch (e) {
  29. if (e instanceof WebAssembly.CompileError) return;
  30. }
  31. throw new Error("Wasm decoding failure expected");
  32. }
  33. function assert_invalid(bytes) {
  34. try { module(bytes) } catch (e) {
  35. if (e instanceof WebAssembly.CompileError) return;
  36. }
  37. throw new Error("Wasm validation failure expected");
  38. }
  39. function assert_soft_invalid(bytes) {
  40. try { module(bytes) } catch (e) {
  41. if (e instanceof WebAssembly.CompileError) return;
  42. throw new Error("Wasm validation failure expected");
  43. }
  44. if (soft_validate)
  45. throw new Error("Wasm validation failure expected");
  46. }
  47. function assert_unlinkable(bytes) {
  48. let mod = module(bytes);
  49. try { new WebAssembly.Instance(mod, registry) } catch (e) {
  50. if (e instanceof TypeError) return;
  51. }
  52. throw new Error("Wasm linking failure expected");
  53. }
  54. function assert_uninstantiable(bytes) {
  55. let mod = module(bytes);
  56. try { new WebAssembly.Instance(mod, registry) } catch (e) {
  57. if (e instanceof WebAssembly.RuntimeError) return;
  58. }
  59. throw new Error("Wasm trap expected");
  60. }
  61. function assert_trap(action) {
  62. try { action() } catch (e) {
  63. if (e instanceof WebAssembly.RuntimeError) return;
  64. }
  65. throw new Error("Wasm trap expected");
  66. }
  67. function assert_return(action, expected) {
  68. let actual = action();
  69. if (!Object.is(actual, expected)) {
  70. throw new Error("Wasm return value " + expected + " expected, got " + actual);
  71. };
  72. }
  73. function assert_return_nan(action) {
  74. let actual = action();
  75. if (!Number.isNaN(actual)) {
  76. throw new Error("Wasm return value NaN expected, got " + actual);
  77. };
  78. }
  79. assert_malformed("\x00\x61\x73\x6d\x01\x00\x00\x00\x02\x94\x80\x80\x80\x00\x01\x08\x73\x70\x65\x63\x74\x65\x73\x74\x06\x67\x6c\x6f\x62\x61\x6c\x03\x7f\x02");
  80. assert_malformed("\x00\x61\x73\x6d\x01\x00\x00\x00\x06\x86\x80\x80\x80\x00\x01\x7f\xff\x41\x00\x0b");
  81. assert_malformed("\x00\x61\x73\x6d\x01\x00\x00\x00\x06\x86\x80\x80\x80\x00\x01\x7f\xd4\x41\x00\x0b");
  82. assert_malformed("\x00\x61\x73\x6d\x01\x00\x00\x00\x06\x86\x80\x80\x80\x00\x01\x7f\x02\x41\x00\x0b");
  83. print("PASSED");