v8/test/mjsunit/wasm/unicode-validation.js
machenbach e1eee748dd Revert of [wasm] Master CL for Binary 0xC changes. (patchset #26 id:490001 of https://codereview.chromium.org/2345593003/ )
Reason for revert:
Main suspect for tsan:
https://build.chromium.org/p/client.v8/builders/V8%20Linux64%20TSAN/builds/11893

Also changes layout tests:
https://build.chromium.org/p/client.v8.fyi/builders/V8-Blink%20Linux%2064/builds/10036

+mips builder:
https://build.chromium.org/p/client.v8.ports/builders/V8%20Mips%20-%20builder/builds/4032

Original issue's description:
> [wasm] Master CL for Binary 0xC changes.
>
> [0xC] Convert to stack machine semantics.
> [0xC] Use section codes instead of names.
> [0xC] Add elements section decoding.
> [0xC] Decoding of globals section.
> [0xC] Decoding of memory section.
> [0xC] Decoding of imports section.
> [0xC] Decoding of exports section.
> [0xC] Decoding of data section.
> [0xC] Remove CallImport bytecode.
> [0xC] Function bodies have an implicit block.
> [0xC] Remove the bottom label from loops.
> [0xC] Add signatures to blocks.
> [0xC] Remove arities from branches.
> Add tests for init expression decoding.
> Rework compilation of import wrappers and how they are patched.
> Rework function indices in debugging.
> Fix ASM->WASM builder for stack machine.
> Reorganize asm.js foreign functions due to import indices change.
>
> R=ahaas@chromium.org,rossberg@chromium.org,bradnelson@chromium.org
> BUG=chromium:575167
> LOG=Y
>
> Committed: https://crrev.com/76eb976a67273b8c03c744f64ad850b0432554b9
> Cr-Commit-Position: refs/heads/master@{#39678}

TBR=ahaas@chromium.org,bradnelson@chromium.org,mtrofin@chromium.org,rossberg@chromium.org,bradnelson@google.com,titzer@chromium.org
# Skipping CQ checks because original CL landed less than 1 days ago.
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=chromium:575167

Review-Url: https://codereview.chromium.org/2361053004
Cr-Commit-Position: refs/heads/master@{#39685}
2016-09-23 17:58:24 +00:00

123 lines
3.9 KiB
JavaScript

// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --expose-wasm
load("test/mjsunit/wasm/wasm-constants.js");
load("test/mjsunit/wasm/wasm-module-builder.js");
function toByteArray(s) {
var arr = [];
for (var i = 0; i < s.length; ++i) {
arr.push(s.charCodeAt(i) & 0xff);
}
return arr;
}
function toString(arr) {
if (typeof arr === "string") return arr;
var s = "";
for (var b of arr) s += String.fromCharCode(b);
return s;
}
function toUTF8(arr) {
if (typeof arr === "string" || arr === undefined) return arr;
return decodeURIComponent(escape(toString(arr)));
}
function isValidUtf8(arr) {
if (typeof arr === "string" || arr === undefined) return true;
try {
var s = toUTF8(arr);
for (var i = 0; i < s.length; ++i)
if ((s.charCodeAt(i) & 0xfffe) == 0xfffe)
return false;
return true;
} catch (e) {
if (e instanceof URIError) return false;
throw e;
}
}
function checkImportsAndExports(imported_module_name, imported_function_name,
internal_function_name, exported_function_name, shouldThrow) {
var builder = new WasmModuleBuilder();
builder.addImportWithModule(imported_module_name, imported_function_name,
kSig_v_v);
builder.addFunction(internal_function_name, kSig_v_v)
.addBody([kExprCallImport, kArity0, 0])
.exportAs(exported_function_name);
// sanity check: does javascript agree with out shouldThrow annotation?
assertEquals(shouldThrow,
!isValidUtf8(imported_module_name) ||
!isValidUtf8(imported_function_name) ||
!isValidUtf8(exported_function_name),
"JavaScript does not agree with our shouldThrow expectation");
if (!shouldThrow) {
imported_module_name = toUTF8(imported_module_name);
imported_function_name = toUTF8(imported_function_name);
}
var ffi = new Object();
if (imported_function_name === undefined) {
ffi[imported_module_name] = function() { };
} else {
ffi[imported_module_name] = new Object();
ffi[imported_module_name][imported_function_name] = function() { };
}
var hasThrown = true;
try {
builder.instantiate(ffi);
hasThrown = false;
} catch (err) {
if (!shouldThrow) print(err);
assertTrue(shouldThrow, "Should not throw error on valid names");
assertTrue(err instanceof Error, "exception should be an Error");
assertContains("UTF-8", err.toString());
}
assertEquals(shouldThrow, hasThrown,
"Should throw validation error on invalid names");
}
function checkImportedModuleName(name, shouldThrow) {
checkImportsAndExports(name, "imp", "func", undefined, shouldThrow);
}
function checkImportedFunctionName(name, shouldThrow) {
checkImportsAndExports("module", name, "func", "func", shouldThrow);
}
function checkExportedFunctionName(name, shouldThrow) {
checkImportsAndExports("module", "func", "func", name, shouldThrow);
}
function checkInternalFunctionName(name) {
checkImportsAndExports("module", "func", name, "func", false);
}
function checkAll(name, shouldThrow) {
checkImportedModuleName(name, shouldThrow);
checkImportedFunctionName(name, shouldThrow);
checkExportedFunctionName(name, shouldThrow);
checkInternalFunctionName(name);
}
checkAll("ascii", false);
checkAll("some math: (½)² = ¼", false);
checkAll("中国历史系列条目\n北", false);
checkAll(toByteArray("\xef\xb7\x8f"), false);
checkAll(toByteArray("a\xc2\x81\xe1\x80\xbf\xf1\x80\xa0\xbf"), false);
checkAll(toByteArray("\xff"), true);
checkAll(toByteArray("\xed\xa0\x8f"), true); // surrogate code points
checkAll(toByteArray("\xe0\x82\x80"), true); // overlong sequence
checkAll(toByteArray("\xf4\x90\x80\x80"), true); // beyond limit: U+110000
checkAll(toByteArray("\xef\xbf\xbe"), true); // non-character; U+FFFE
checkAll(toByteArray("with\x00null"), false);