Commit 06241221 authored by titzer's avatar titzer Committed by Commit bot

[wasm] Add a magic word and a version number to the binary.

R=binji@chromium.org,jfb@chromium.org
BUG=

Review URL: https://codereview.chromium.org/1740373002

Cr-Commit-Position: refs/heads/master@{#34346}
parent 8344687c
......@@ -461,6 +461,8 @@ struct Sizes {
WasmModuleIndex* WasmModuleWriter::WriteTo(Zone* zone) const {
Sizes sizes = {0, 0};
sizes.Add(2 * sizeof(uint32_t), 0); // header
sizes.Add(1, 0);
sizes.Add(kDeclMemorySize, 0);
......@@ -495,6 +497,10 @@ WasmModuleIndex* WasmModuleWriter::WriteTo(Zone* zone) const {
byte* header = buffer;
byte* body = buffer + sizes.header_size;
// -- emit magic -------------------------------------------------------------
EmitUint32(&header, kWasmMagic);
EmitUint32(&header, kWasmVersion);
// -- emit memory declaration ------------------------------------------------
EmitUint8(&header, kDeclMemory);
EmitUint8(&header, 16); // min memory size
......
......@@ -61,6 +61,27 @@ class ModuleDecoder : public Decoder {
bool sections[kMaxModuleSectionCode];
memset(sections, 0, sizeof(sections));
const byte* pos = pc_;
uint32_t magic_word = consume_u32("wasm magic");
#define BYTES(x) (x & 0xff), (x >> 8) & 0xff, (x >> 16) & 0xff, (x >> 24) & 0xff
if (magic_word != kWasmMagic) {
error(pos, pos,
"expected magic word %02x %02x %02x %02x, "
"found %02x %02x %02x %02x",
BYTES(kWasmMagic), BYTES(magic_word));
return toResult(module);
}
pos = pc_;
uint32_t magic_version = consume_u32("wasm version");
if (magic_version != kWasmVersion) {
error(pos, pos,
"expected version %02x %02x %02x %02x, "
"found %02x %02x %02x %02x",
BYTES(kWasmVersion), BYTES(magic_version));
return toResult(module);
}
// Decode the module sections.
while (pc_ < limit_) {
TRACE("DecodeSection\n");
......
......@@ -265,4 +265,16 @@
#define WASM_I32_REINTERPRET_F32(x) kExprI32ReinterpretF32, x
#define WASM_I64_REINTERPRET_F64(x) kExprI64ReinterpretF64, x
#define U32_LE(v) \
static_cast<byte>(v), static_cast<byte>((v) >> 8), \
static_cast<byte>((v) >> 16), static_cast<byte>((v) >> 24)
#define U16_LE(v) static_cast<byte>(v), static_cast<byte>((v) >> 8)
#define WASM_MODULE_HEADER U32_LE(kWasmMagic), U32_LE(kWasmVersion)
#define SIG_INDEX(v) U16_LE(v)
#define FUNC_INDEX(v) U16_LE(v)
#define NAME_OFFSET(v) U32_LE(v)
#endif // V8_WASM_MACRO_GEN_H_
......@@ -22,6 +22,8 @@ namespace wasm {
const size_t kMaxModuleSize = 1024 * 1024 * 1024;
const size_t kMaxFunctionSize = 128 * 1024;
const size_t kMaxStringSize = 256;
const uint32_t kWasmMagic = 0x6d736100;
const uint32_t kWasmVersion = 0x0a;
enum WasmSectionDeclCode {
kDeclMemory = 0x00,
......
......@@ -36,6 +36,7 @@ void TestModule(WasmModuleIndex* module, int32_t expected_result) {
// A raw test that skips the WasmModuleBuilder.
TEST(Run_WasmModule_CallAdd_rev) {
static const byte data[] = {
WASM_MODULE_HEADER,
// sig#0 ------------------------------------------
kDeclSignatures, 2, 0, kLocalI32, // void -> int
2, kLocalI32, kLocalI32, kLocalI32, // int,int -> int
......
......@@ -8,9 +8,9 @@ load("test/mjsunit/wasm/wasm-constants.js");
var module = (function () {
var kBodySize = 5;
var kNameOffset = 21 + kBodySize + 1;
var kNameOffset = kHeaderSize + 21 + kBodySize + 1;
return _WASMEXP_.instantiateModule(bytes(
return _WASMEXP_.instantiateModule(bytesWithHeader(
// -- memory
kDeclMemory,
12, 12, 1,
......@@ -62,9 +62,9 @@ assertEquals(-5555555, module.sub(3333333, 8888888));
var module = (function() {
var kBodySize = 1;
var kNameOffset2 = 19 + kBodySize + 1;
var kNameOffset2 = kHeaderSize + 19 + kBodySize + 1;
return _WASMEXP_.instantiateModule(bytes(
return _WASMEXP_.instantiateModule(bytesWithHeader(
// -- memory
kDeclMemory,
12, 12, 1,
......@@ -113,9 +113,9 @@ assertEquals(undefined, module.nop());
(function testLt() {
var kBodySize = 5;
var kNameOffset = 21 + kBodySize + 1;
var kNameOffset = kHeaderSize + 21 + kBodySize + 1;
var data = bytes(
var data = bytesWithHeader(
// -- memory
kDeclMemory,
12, 12, 1,
......
......@@ -31,9 +31,9 @@ function assertTraps(code, msg) {
function makeDivRem(opcode) {
var kBodySize = 5;
var kNameMainOffset = 6 + 11 + kBodySize + 1;
var kNameMainOffset = kHeaderSize + 6 + 11 + kBodySize + 1;
var data = bytes(
var data = bytesWithHeader(
// signatures
kDeclSignatures, 1,
2, kAstI32, kAstI32, kAstI32, // (int,int) -> int
......
......@@ -9,9 +9,9 @@ load("test/mjsunit/wasm/wasm-constants.js");
(function testExportedMain() {
var kBodySize = 3;
var kReturnValue = 99;
var kNameMainOffset = 4 + 7 + kBodySize + 8 + 1;
var kNameMainOffset = kHeaderSize + 4 + 7 + kBodySize + 8 + 1;
var data = bytes(
var data = bytesWithHeader(
// signatures
kDeclSignatures, 1,
0, kAstI32, // void -> i32
......@@ -47,10 +47,10 @@ load("test/mjsunit/wasm/wasm-constants.js");
(function testExportedTwice() {
var kBodySize = 3;
var kReturnValue = 99;
var kNameMainOffset = 4 + 7 + kBodySize + 14 + 1;
var kNameMainOffset = kHeaderSize + 4 + 7 + kBodySize + 14 + 1;
var kNameFooOffset = kNameMainOffset + 5;
var data = bytes(
var data = bytesWithHeader(
// signatures
kDeclSignatures, 1,
0, kAstI32, // void -> i32
......
......@@ -8,10 +8,10 @@ load("test/mjsunit/wasm/wasm-constants.js");
function testCallFFI(ffi) {
var kBodySize = 6;
var kNameAddOffset = 28 + kBodySize + 1;
var kNameAddOffset = kHeaderSize + 28 + kBodySize + 1;
var kNameMainOffset = kNameAddOffset + 4;
var data = bytes(
var data = bytesWithHeader(
kDeclMemory,
12, 12, 1, // memory
// -- signatures
......
......@@ -8,13 +8,13 @@ load("test/mjsunit/wasm/wasm-constants.js");
function testCallFFI(func, check) {
var kBodySize = 6;
var kNameFunOffset = 24 + kBodySize + 1;
var kNameFunOffset = kHeaderSize + 24 + kBodySize + 1;
var kNameMainOffset = kNameFunOffset + 4;
var ffi = new Object();
ffi.fun = func;
var data = bytes(
var data = bytesWithHeader(
// signatures
kDeclSignatures, 1,
2, kAstI32, kAstF64, kAstF64, // (f64,f64) -> int
......@@ -190,7 +190,7 @@ testCallFFI(returnValue(objWithValueOf), checkReturn(198));
function testCallBinopVoid(type, func, check) {
var kBodySize = 10;
var kNameFunOffset = 28 + kBodySize + 1;
var kNameFunOffset = kHeaderSize + 28 + kBodySize + 1;
var kNameMainOffset = kNameFunOffset + 4;
var ffi = new Object();
......@@ -209,7 +209,7 @@ function testCallBinopVoid(type, func, check) {
args_b = arguments[1];
}
var data = bytes(
var data = bytesWithHeader(
// -- signatures
kDeclSignatures, 2,
2, kAstStmt, type, type, // (type,type)->void
......@@ -283,13 +283,13 @@ testCallBinopVoid(kAstF64);
function testCallPrint() {
var kBodySize = 10;
var kNamePrintOffset = 10 + 7 + 7 + 9 + kBodySize + 1;
var kNamePrintOffset = kHeaderSize + 10 + 7 + 7 + 9 + kBodySize + 1;
var kNameMainOffset = kNamePrintOffset + 6;
var ffi = new Object();
ffi.print = print;
var data = bytes(
var data = bytesWithHeader(
// -- signatures
kDeclSignatures, 2,
1, kAstStmt, kAstI32, // i32->void
......
......@@ -8,13 +8,13 @@ load("test/mjsunit/wasm/wasm-constants.js");
function testCallImport(func, check) {
var kBodySize = 6;
var kNameFunOffset = 29 + kBodySize + 1;
var kNameFunOffset = kHeaderSize + 29 + kBodySize + 1;
var kNameMainOffset = kNameFunOffset + 4;
var ffi = new Object();
ffi.fun = func;
var data = bytes(
var data = bytesWithHeader(
// signatures
kDeclSignatures, 1,
2, kAstI32, kAstF64, kAstF64, // (f64,f64) -> int
......@@ -193,7 +193,7 @@ testCallImport(returnValue(objWithValueOf), checkReturn(198));
function testCallBinopVoid(type, func, check) {
var kBodySize = 10;
var kNameFunOffset = 28 + kBodySize + 1;
var kNameFunOffset = kHeaderSize + 28 + kBodySize + 1;
var kNameMainOffset = kNameFunOffset + 4;
var ffi = new Object();
......@@ -212,7 +212,7 @@ function testCallBinopVoid(type, func, check) {
args_b = arguments[1];
}
var data = bytes(
var data = bytesWithHeader(
// -- signatures
kDeclSignatures, 2,
2, kAstStmt, type, type, // (type,type)->void
......@@ -286,13 +286,13 @@ testCallBinopVoid(kAstF64);
function testCallPrint() {
var kBodySize = 10;
var kNamePrintOffset = 10 + 7 + 7 + 9 + kBodySize + 1;
var kNamePrintOffset = kHeaderSize + 10 + 7 + 7 + 9 + kBodySize + 1;
var kNameMainOffset = kNamePrintOffset + 6;
var ffi = new Object();
ffi.print = print;
var data = bytes(
var data = bytesWithHeader(
// -- signatures
kDeclSignatures, 2,
1, kAstStmt, kAstI32, // i32->void
......@@ -338,7 +338,7 @@ testCallPrint();
function testCallImport2(foo, bar, expected) {
var kBodySize = 5;
var kNameFooOffset = 37 + kBodySize + 1;
var kNameFooOffset = kHeaderSize + 37 + kBodySize + 1;
var kNameBarOffset = kNameFooOffset + 4;
var kNameMainOffset = kNameBarOffset + 4;
......@@ -346,7 +346,7 @@ function testCallImport2(foo, bar, expected) {
ffi.foo = foo;
ffi.bar = bar;
var data = bytes(
var data = bytesWithHeader(
// signatures
kDeclSignatures, 1,
0, kAstI32, // void -> i32
......
......@@ -12,14 +12,14 @@ var module = (function () {
var kBodySize1 = 5;
var kBodySize2 = 8;
var kFuncTableSize = 8;
var kSubOffset = 13 + kFuncWithBody + kBodySize1 + kFuncImported + kFuncWithBody + kBodySize2 + kFuncTableSize + 1;
var kSubOffset = kHeaderSize + 13 + kFuncWithBody + kBodySize1 + kFuncImported + kFuncWithBody + kBodySize2 + kFuncTableSize + 1;
var kAddOffset = kSubOffset + 4;
var kMainOffset = kAddOffset + 4;
var ffi = new Object();
ffi.add = (function(a, b) { return a + b | 0; });
return _WASMEXP_.instantiateModule(bytes(
return _WASMEXP_.instantiateModule(bytesWithHeader(
// -- signatures
kDeclSignatures, 2,
2, kAstI32, kAstI32, kAstI32, // int, int -> int
......
......@@ -9,9 +9,9 @@ load("test/mjsunit/wasm/wasm-constants.js");
var kReturnValue = 117;
var kBodySize = 2;
var kNameOffset = 19 + kBodySize + 1;
var kNameOffset = kHeaderSize + 19 + kBodySize + 1;
var data = bytes(
var data = bytesWithHeader(
// -- memory
kDeclMemory,
10, 10, 1,
......
......@@ -9,9 +9,9 @@ load("test/mjsunit/wasm/wasm-constants.js");
var kReturnValue = 97;
var kBodySize = 2;
var kNameOffset = 15 + kBodySize + 1;
var kNameOffset = kHeaderSize + 15 + kBodySize + 1;
var data = bytes(
var data = bytesWithHeader(
// -- signatures
kDeclSignatures, 1,
0, kAstI32, // signature: void -> int
......
......@@ -10,9 +10,9 @@ var kMemSize = 4096;
function genModule(memory) {
var kBodySize = 27;
var kNameMainOffset = 28 + kBodySize + 1;
var kNameMainOffset = kHeaderSize + 28 + kBodySize + 1;
var data = bytes(
var data = bytesWithHeader(
kDeclMemory,
12, 12, 1, // memory
// -- signatures
......@@ -133,9 +133,9 @@ testOuterMemorySurvivalAcrossGc();
function testOOBThrows() {
var kBodySize = 8;
var kNameMainOffset = 29 + kBodySize + 1;
var kNameMainOffset = kHeaderSize + 29 + kBodySize + 1;
var data = bytes(
var data = bytesWithHeader(
kDeclMemory,
12, 12, 1, // memory = 4KB
// -- signatures
......
......@@ -12,12 +12,12 @@ function runSelect2(module, which, a, b) {
function testSelect2(type) {
var kBodySize = 2;
var kNameOffset = 21 + kBodySize + 1;
var kNameOffset = kHeaderSize + 21 + kBodySize + 1;
for (var which = 0; which < 2; which++) {
print("type = " + type + ", which = " + which);
var data = bytes(
var data = bytesWithHeader(
// -- memory
kDeclMemory,
12, 12, 1, // memory
......@@ -88,13 +88,13 @@ function runSelect10(module, which, a, b) {
function testSelect10(type) {
var kBodySize = 2;
var kNameOffset = 29 + kBodySize + 1;
var kNameOffset = kHeaderSize + 29 + kBodySize + 1;
for (var which = 0; which < 10; which++) {
print("type = " + type + ", which = " + which);
var t = type;
var data = bytes(
var data = bytesWithHeader(
kDeclMemory,
12, 12, 1, // memory
// signatures
......
......@@ -8,13 +8,13 @@ load("test/mjsunit/wasm/wasm-constants.js");
function testStack(func, check) {
var kBodySize = 2;
var kNameFunOffset = 22 + kBodySize + 1;
var kNameFunOffset = kHeaderSize + 22 + kBodySize + 1;
var kNameMainOffset = kNameFunOffset + 4;
var ffi = new Object();
ffi.fun = func;
var data = bytes(
var data = bytesWithHeader(
// signatures
kDeclSignatures, 1, // --
0, kAstStmt, // () -> void
......
......@@ -8,13 +8,13 @@ load("test/mjsunit/wasm/wasm-constants.js");
function makeFFI(func) {
var kBodySize = 6;
var kNameFunOffset = 24 + kBodySize + 1;
var kNameFunOffset = kHeaderSize + 24 + kBodySize + 1;
var kNameMainOffset = kNameFunOffset + 4;
var ffi = new Object();
ffi.fun = func;
var data = bytes(
var data = bytesWithHeader(
// signatures
kDeclSignatures, 1,
2, kAstI32, kAstF64, kAstF64, // (f64,f64) -> int
......
......@@ -8,6 +8,11 @@ load("test/mjsunit/wasm/wasm-constants.js");
function instantiate(sig, body) {
var module = new Array();
module = module.concat([
// -- header
kWasmH0, kWasmH1, kWasmH2, kWasmH3,
kWasmV0, kWasmV1, kWasmV2, kWasmV3
]);
module = module.concat([
// -- signatures
kDeclSignatures, 1,
......@@ -63,7 +68,7 @@ assertFails([3, kAstI32, kAstI32, kAstF32, kAstF64], [kExprGetLocal, 0]);
(function testInvalidIndex() {
var kBodySize = 1;
var data = bytes(
var data = bytesWithHeader(
// -- signatures
kDeclSignatures, 1,
0, kAstStmt,
......@@ -84,7 +89,7 @@ assertFails([3, kAstI32, kAstI32, kAstF32, kAstF64], [kExprGetLocal, 0]);
(function testTwoStartFuncs() {
var kBodySize = 1;
var data = bytes(
var data = bytesWithHeader(
// -- signatures
kDeclSignatures, 1,
0, kAstStmt,
......@@ -109,7 +114,7 @@ assertFails([3, kAstI32, kAstI32, kAstF32, kAstF64], [kExprGetLocal, 0]);
(function testRun() {
var kBodySize = 6;
var data = bytes(
var data = bytesWithHeader(
kDeclMemory,
12, 12, 1, // memory
// -- signatures
......@@ -135,9 +140,9 @@ assertFails([3, kAstI32, kAstI32, kAstF32, kAstF64], [kExprGetLocal, 0]);
(function testStartFFI() {
var kBodySize = 2;
var kNameOffset = 4 + 9 + 7 + 3;
var kNameOffset = kHeaderSize + 4 + 9 + 7 + 3;
var data = bytes(
var data = bytesWithHeader(
// -- signatures
kDeclSignatures, 1,
0, kAstStmt,
......
......@@ -10,12 +10,12 @@ var module = (function () {
var kFuncWithBody = 9;
var kFuncImported = 7;
var kBodySize1 = 1;
var kMainOffset = 6 + kFuncWithBody + kBodySize1 + 1;
var kMainOffset = kHeaderSize + 6 + kFuncWithBody + kBodySize1 + 1;
var ffi = new Object();
ffi.add = (function(a, b) { return a + b | 0; });
return _WASMEXP_.instantiateModule(bytes(
return _WASMEXP_.instantiateModule(bytesWithHeader(
// -- signatures
kDeclSignatures, 1,
0, kAstStmt, // void -> void
......
......@@ -15,6 +15,38 @@ function bytes() {
return buffer;
}
// Header declaration constants
var kWasmH0 = 0;
var kWasmH1 = 0x61;
var kWasmH2 = 0x73;
var kWasmH3 = 0x6d;
var kWasmV0 = 10;
var kWasmV1 = 0;
var kWasmV2 = 0;
var kWasmV3 = 0;
var kHeaderSize = 8;
function bytesWithHeader() {
var buffer = new ArrayBuffer(kHeaderSize + arguments.length);
var view = new Uint8Array(buffer);
view[0] = kWasmH0;
view[1] = kWasmH1;
view[2] = kWasmH2;
view[3] = kWasmH3;
view[4] = kWasmV0;
view[5] = kWasmV1;
view[6] = kWasmV2;
view[7] = kWasmV3;
for (var i = 0; i < arguments.length; i++) {
var val = arguments[i];
if ((typeof val) == "string") val = val.charCodeAt(0);
view[kHeaderSize + i] = val | 0;
}
return buffer;
}
// Section declaration constants
var kDeclMemory = 0x00;
var kDeclSignatures = 0x01;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment