Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 1 | // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Flags: --expose-wasm |
| 6 | |
| 7 | load("test/mjsunit/wasm/wasm-constants.js"); |
| 8 | load("test/mjsunit/wasm/wasm-module-builder.js"); |
| 9 | |
| 10 | function toByteArray(s) { |
| 11 | var arr = []; |
| 12 | for (var i = 0; i < s.length; ++i) { |
| 13 | arr.push(s.charCodeAt(i) & 0xff); |
| 14 | } |
| 15 | return arr; |
| 16 | } |
| 17 | |
| 18 | function toString(arr) { |
| 19 | if (typeof arr === "string") return arr; |
| 20 | var s = ""; |
| 21 | for (var b of arr) s += String.fromCharCode(b); |
| 22 | return s; |
| 23 | } |
| 24 | |
| 25 | function toUTF8(arr) { |
| 26 | if (typeof arr === "string" || arr === undefined) return arr; |
| 27 | return decodeURIComponent(escape(toString(arr))); |
| 28 | } |
| 29 | |
| 30 | function isValidUtf8(arr) { |
| 31 | if (typeof arr === "string" || arr === undefined) return true; |
| 32 | try { |
| 33 | var s = toUTF8(arr); |
| 34 | for (var i = 0; i < s.length; ++i) |
| 35 | if ((s.charCodeAt(i) & 0xfffe) == 0xfffe) |
| 36 | return false; |
| 37 | return true; |
| 38 | } catch (e) { |
| 39 | if (e instanceof URIError) return false; |
| 40 | throw e; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | function checkImportsAndExports(imported_module_name, imported_function_name, |
| 45 | internal_function_name, exported_function_name, shouldThrow) { |
| 46 | var builder = new WasmModuleBuilder(); |
| 47 | |
| 48 | builder.addImportWithModule(imported_module_name, imported_function_name, |
| 49 | kSig_v_v); |
| 50 | |
| 51 | builder.addFunction(internal_function_name, kSig_v_v) |
| 52 | .addBody([kExprCallImport, kArity0, 0]) |
| 53 | .exportAs(exported_function_name); |
| 54 | |
| 55 | // sanity check: does javascript agree with out shouldThrow annotation? |
| 56 | assertEquals(shouldThrow, |
| 57 | !isValidUtf8(imported_module_name) || |
| 58 | !isValidUtf8(imported_function_name) || |
| 59 | !isValidUtf8(exported_function_name), |
| 60 | "JavaScript does not agree with our shouldThrow expectation"); |
| 61 | |
| 62 | if (!shouldThrow) { |
| 63 | imported_module_name = toUTF8(imported_module_name); |
| 64 | imported_function_name = toUTF8(imported_function_name); |
| 65 | } |
| 66 | |
| 67 | var ffi = new Object(); |
| 68 | if (imported_function_name === undefined) { |
| 69 | ffi[imported_module_name] = function() { }; |
| 70 | } else { |
| 71 | ffi[imported_module_name] = new Object(); |
| 72 | ffi[imported_module_name][imported_function_name] = function() { }; |
| 73 | } |
| 74 | |
| 75 | var hasThrown = true; |
| 76 | try { |
| 77 | builder.instantiate(ffi); |
| 78 | hasThrown = false; |
| 79 | } catch (err) { |
| 80 | if (!shouldThrow) print(err); |
| 81 | assertTrue(shouldThrow, "Should not throw error on valid names"); |
| 82 | assertContains("UTF-8", err.toString()); |
| 83 | } |
| 84 | assertEquals(shouldThrow, hasThrown, |
| 85 | "Should throw validation error on invalid names"); |
| 86 | } |
| 87 | |
| 88 | function checkImportedModuleName(name, shouldThrow) { |
| 89 | checkImportsAndExports(name, "imp", "func", undefined, shouldThrow); |
| 90 | } |
| 91 | |
| 92 | function checkImportedFunctionName(name, shouldThrow) { |
| 93 | checkImportsAndExports("module", name, "func", "func", shouldThrow); |
| 94 | } |
| 95 | |
| 96 | function checkExportedFunctionName(name, shouldThrow) { |
| 97 | checkImportsAndExports("module", "func", "func", name, shouldThrow); |
| 98 | } |
| 99 | |
| 100 | function checkInternalFunctionName(name) { |
| 101 | checkImportsAndExports("module", "func", name, "func", false); |
| 102 | } |
| 103 | |
| 104 | function checkAll(name, shouldThrow) { |
| 105 | checkImportedModuleName(name, shouldThrow); |
| 106 | checkImportedFunctionName(name, shouldThrow); |
| 107 | checkExportedFunctionName(name, shouldThrow); |
| 108 | checkInternalFunctionName(name); |
| 109 | } |
| 110 | |
| 111 | checkAll("ascii", false); |
| 112 | checkAll("some math: (½)² = ¼", false); |
| 113 | checkAll("中国历史系列条目\n北", false); |
| 114 | checkAll(toByteArray("\xef\xb7\x8f"), false); |
| 115 | checkAll(toByteArray("a\xc2\x81\xe1\x80\xbf\xf1\x80\xa0\xbf"), false); |
| 116 | checkAll(toByteArray("\xff"), true); |
| 117 | checkAll(toByteArray("\xed\xa0\x8f"), true); // surrogate code points |
| 118 | checkAll(toByteArray("\xe0\x82\x80"), true); // overlong sequence |
| 119 | checkAll(toByteArray("\xf4\x90\x80\x80"), true); // beyond limit: U+110000 |
| 120 | checkAll(toByteArray("\xef\xbf\xbe"), true); // non-character; U+FFFE |
| 121 | checkAll(toByteArray("with\x00null"), false); |