blob: 05edd62a1bb485f43a7006db7225bfb1b3000211 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2015 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#include <stdlib.h>
6#include <string.h>
7
8#include "src/wasm/encoder.h"
Ben Murdoch097c5b22016-05-18 11:27:45 +01009#include "src/wasm/wasm-js.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010#include "src/wasm/wasm-macro-gen.h"
11#include "src/wasm/wasm-module.h"
12#include "src/wasm/wasm-opcodes.h"
13
14#include "test/cctest/cctest.h"
Ben Murdochc5610432016-08-08 18:44:38 +010015#include "test/cctest/wasm/test-signatures.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016
17using namespace v8::base;
18using namespace v8::internal;
19using namespace v8::internal::compiler;
20using namespace v8::internal::wasm;
21
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000022namespace {
23void TestModule(WasmModuleIndex* module, int32_t expected_result) {
24 Isolate* isolate = CcTest::InitIsolateOnce();
Ben Murdoch097c5b22016-05-18 11:27:45 +010025 HandleScope scope(isolate);
26 WasmJs::InstallWasmFunctionMap(isolate, isolate->native_context());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000027 int32_t result =
28 CompileAndRunWasmModule(isolate, module->Begin(), module->End());
29 CHECK_EQ(expected_result, result);
30}
31} // namespace
32
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000033TEST(Run_WasmModule_Return114) {
34 static const int32_t kReturnValue = 114;
Ben Murdochc5610432016-08-08 18:44:38 +010035 TestSignatures sigs;
Ben Murdochda12d292016-06-02 14:46:10 +010036 v8::base::AccountingAllocator allocator;
37 Zone zone(&allocator);
Ben Murdochc5610432016-08-08 18:44:38 +010038
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000039 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone);
40 uint16_t f_index = builder->AddFunction();
41 WasmFunctionBuilder* f = builder->FunctionAt(f_index);
Ben Murdochc5610432016-08-08 18:44:38 +010042 f->SetSignature(sigs.i_v());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000043 f->Exported(1);
44 byte code[] = {WASM_I8(kReturnValue)};
45 f->EmitCode(code, sizeof(code));
46 WasmModuleWriter* writer = builder->Build(&zone);
47 TestModule(writer->WriteTo(&zone), kReturnValue);
48}
49
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000050TEST(Run_WasmModule_CallAdd) {
Ben Murdochda12d292016-06-02 14:46:10 +010051 v8::base::AccountingAllocator allocator;
52 Zone zone(&allocator);
Ben Murdochc5610432016-08-08 18:44:38 +010053 TestSignatures sigs;
54
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000055 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone);
Ben Murdochc5610432016-08-08 18:44:38 +010056
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000057 uint16_t f1_index = builder->AddFunction();
58 WasmFunctionBuilder* f = builder->FunctionAt(f1_index);
Ben Murdochc5610432016-08-08 18:44:38 +010059 f->SetSignature(sigs.i_ii());
60 uint16_t param1 = 0;
61 uint16_t param2 = 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000062 byte code1[] = {WASM_I32_ADD(WASM_GET_LOCAL(param1), WASM_GET_LOCAL(param2))};
Ben Murdochc5610432016-08-08 18:44:38 +010063 f->EmitCode(code1, sizeof(code1));
64
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000065 uint16_t f2_index = builder->AddFunction();
66 f = builder->FunctionAt(f2_index);
Ben Murdochc5610432016-08-08 18:44:38 +010067 f->SetSignature(sigs.i_v());
68
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000069 f->Exported(1);
Ben Murdochc5610432016-08-08 18:44:38 +010070 byte code2[] = {WASM_CALL_FUNCTION2(f1_index, WASM_I8(77), WASM_I8(22))};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000071 f->EmitCode(code2, sizeof(code2));
72 WasmModuleWriter* writer = builder->Build(&zone);
73 TestModule(writer->WriteTo(&zone), 99);
74}
75
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000076TEST(Run_WasmModule_ReadLoadedDataSegment) {
77 static const byte kDataSegmentDest0 = 12;
Ben Murdochda12d292016-06-02 14:46:10 +010078 v8::base::AccountingAllocator allocator;
79 Zone zone(&allocator);
Ben Murdochc5610432016-08-08 18:44:38 +010080 TestSignatures sigs;
81
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000082 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone);
83 uint16_t f_index = builder->AddFunction();
84 WasmFunctionBuilder* f = builder->FunctionAt(f_index);
Ben Murdochc5610432016-08-08 18:44:38 +010085 f->SetSignature(sigs.i_v());
86
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000087 f->Exported(1);
88 byte code[] = {
89 WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(kDataSegmentDest0))};
90 f->EmitCode(code, sizeof(code));
91 byte data[] = {0xaa, 0xbb, 0xcc, 0xdd};
92 builder->AddDataSegment(new (&zone) WasmDataSegmentEncoder(
93 &zone, data, sizeof(data), kDataSegmentDest0));
94 WasmModuleWriter* writer = builder->Build(&zone);
95 TestModule(writer->WriteTo(&zone), 0xddccbbaa);
96}
97
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000098TEST(Run_WasmModule_CheckMemoryIsZero) {
99 static const int kCheckSize = 16 * 1024;
Ben Murdochda12d292016-06-02 14:46:10 +0100100 v8::base::AccountingAllocator allocator;
101 Zone zone(&allocator);
Ben Murdochc5610432016-08-08 18:44:38 +0100102 TestSignatures sigs;
103
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000104 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone);
105 uint16_t f_index = builder->AddFunction();
106 WasmFunctionBuilder* f = builder->FunctionAt(f_index);
Ben Murdochc5610432016-08-08 18:44:38 +0100107 f->SetSignature(sigs.i_v());
108
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000109 uint16_t localIndex = f->AddLocal(kAstI32);
110 f->Exported(1);
111 byte code[] = {WASM_BLOCK(
112 2,
113 WASM_WHILE(
Ben Murdochda12d292016-06-02 14:46:10 +0100114 WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I32V_3(kCheckSize)),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000115 WASM_IF_ELSE(
116 WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(localIndex)),
117 WASM_BRV(2, WASM_I8(-1)), WASM_INC_LOCAL_BY(localIndex, 4))),
118 WASM_I8(11))};
Ben Murdochc5610432016-08-08 18:44:38 +0100119 f->EmitCode(code, sizeof(code));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000120 WasmModuleWriter* writer = builder->Build(&zone);
121 TestModule(writer->WriteTo(&zone), 11);
122}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000123
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000124TEST(Run_WasmModule_CallMain_recursive) {
Ben Murdochda12d292016-06-02 14:46:10 +0100125 v8::base::AccountingAllocator allocator;
126 Zone zone(&allocator);
Ben Murdochc5610432016-08-08 18:44:38 +0100127 TestSignatures sigs;
128
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000129 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone);
130 uint16_t f_index = builder->AddFunction();
131 WasmFunctionBuilder* f = builder->FunctionAt(f_index);
Ben Murdochc5610432016-08-08 18:44:38 +0100132 f->SetSignature(sigs.i_v());
133
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000134 uint16_t localIndex = f->AddLocal(kAstI32);
135 f->Exported(1);
136 byte code[] = {WASM_BLOCK(
137 2, WASM_SET_LOCAL(localIndex,
138 WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)),
139 WASM_IF_ELSE(WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I8(5)),
140 WASM_BLOCK(2, WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO,
141 WASM_INC_LOCAL(localIndex)),
142 WASM_BRV(1, WASM_CALL_FUNCTION0(0))),
143 WASM_BRV(0, WASM_I8(55))))};
Ben Murdochc5610432016-08-08 18:44:38 +0100144 f->EmitCode(code, sizeof(code));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000145 WasmModuleWriter* writer = builder->Build(&zone);
146 TestModule(writer->WriteTo(&zone), 55);
147}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000148
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000149TEST(Run_WasmModule_Global) {
Ben Murdochda12d292016-06-02 14:46:10 +0100150 v8::base::AccountingAllocator allocator;
151 Zone zone(&allocator);
Ben Murdochc5610432016-08-08 18:44:38 +0100152 TestSignatures sigs;
153
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000154 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone);
155 uint32_t global1 = builder->AddGlobal(MachineType::Int32(), 0);
156 uint32_t global2 = builder->AddGlobal(MachineType::Int32(), 0);
157 uint16_t f1_index = builder->AddFunction();
158 WasmFunctionBuilder* f = builder->FunctionAt(f1_index);
Ben Murdochc5610432016-08-08 18:44:38 +0100159 f->SetSignature(sigs.i_v());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000160 byte code1[] = {
161 WASM_I32_ADD(WASM_LOAD_GLOBAL(global1), WASM_LOAD_GLOBAL(global2))};
162 f->EmitCode(code1, sizeof(code1));
163 uint16_t f2_index = builder->AddFunction();
164 f = builder->FunctionAt(f2_index);
Ben Murdochc5610432016-08-08 18:44:38 +0100165 f->SetSignature(sigs.i_v());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000166 f->Exported(1);
Ben Murdochda12d292016-06-02 14:46:10 +0100167 byte code2[] = {WASM_STORE_GLOBAL(global1, WASM_I32V_1(56)),
168 WASM_STORE_GLOBAL(global2, WASM_I32V_1(41)),
Ben Murdochc5610432016-08-08 18:44:38 +0100169 WASM_RETURN1(WASM_CALL_FUNCTION0(f1_index))};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000170 f->EmitCode(code2, sizeof(code2));
171 WasmModuleWriter* writer = builder->Build(&zone);
172 TestModule(writer->WriteTo(&zone), 97);
173}