blob: 7a79d677923889964c7908b27ccd98582fc01727 [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 {
Ben Murdoch61f157c2016-09-16 13:49:30 +010023void TestModule(Zone* zone, WasmModuleBuilder* builder,
24 int32_t expected_result) {
25 ZoneBuffer buffer(zone);
26 builder->WriteTo(buffer);
27
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000028 Isolate* isolate = CcTest::InitIsolateOnce();
Ben Murdoch097c5b22016-05-18 11:27:45 +010029 HandleScope scope(isolate);
30 WasmJs::InstallWasmFunctionMap(isolate, isolate->native_context());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000031 int32_t result =
Ben Murdoch61f157c2016-09-16 13:49:30 +010032 testing::CompileAndRunWasmModule(isolate, buffer.begin(), buffer.end());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000033 CHECK_EQ(expected_result, result);
34}
Ben Murdoch61f157c2016-09-16 13:49:30 +010035
36void ExportAsMain(WasmFunctionBuilder* f) {
37 static const char kMainName[] = "main";
38 f->SetExported();
39 f->SetName(kMainName, arraysize(kMainName) - 1);
40}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041} // namespace
42
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000043TEST(Run_WasmModule_Return114) {
44 static const int32_t kReturnValue = 114;
Ben Murdochc5610432016-08-08 18:44:38 +010045 TestSignatures sigs;
Ben Murdochda12d292016-06-02 14:46:10 +010046 v8::base::AccountingAllocator allocator;
47 Zone zone(&allocator);
Ben Murdochc5610432016-08-08 18:44:38 +010048
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000049 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone);
50 uint16_t f_index = builder->AddFunction();
51 WasmFunctionBuilder* f = builder->FunctionAt(f_index);
Ben Murdochc5610432016-08-08 18:44:38 +010052 f->SetSignature(sigs.i_v());
Ben Murdoch61f157c2016-09-16 13:49:30 +010053 ExportAsMain(f);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000054 byte code[] = {WASM_I8(kReturnValue)};
55 f->EmitCode(code, sizeof(code));
Ben Murdoch61f157c2016-09-16 13:49:30 +010056 TestModule(&zone, builder, kReturnValue);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000057}
58
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000059TEST(Run_WasmModule_CallAdd) {
Ben Murdochda12d292016-06-02 14:46:10 +010060 v8::base::AccountingAllocator allocator;
61 Zone zone(&allocator);
Ben Murdochc5610432016-08-08 18:44:38 +010062 TestSignatures sigs;
63
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000064 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone);
Ben Murdochc5610432016-08-08 18:44:38 +010065
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000066 uint16_t f1_index = builder->AddFunction();
67 WasmFunctionBuilder* f = builder->FunctionAt(f1_index);
Ben Murdochc5610432016-08-08 18:44:38 +010068 f->SetSignature(sigs.i_ii());
69 uint16_t param1 = 0;
70 uint16_t param2 = 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000071 byte code1[] = {WASM_I32_ADD(WASM_GET_LOCAL(param1), WASM_GET_LOCAL(param2))};
Ben Murdochc5610432016-08-08 18:44:38 +010072 f->EmitCode(code1, sizeof(code1));
73
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000074 uint16_t f2_index = builder->AddFunction();
75 f = builder->FunctionAt(f2_index);
Ben Murdochc5610432016-08-08 18:44:38 +010076 f->SetSignature(sigs.i_v());
77
Ben Murdoch61f157c2016-09-16 13:49:30 +010078 ExportAsMain(f);
Ben Murdochc5610432016-08-08 18:44:38 +010079 byte code2[] = {WASM_CALL_FUNCTION2(f1_index, WASM_I8(77), WASM_I8(22))};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000080 f->EmitCode(code2, sizeof(code2));
Ben Murdoch61f157c2016-09-16 13:49:30 +010081 TestModule(&zone, builder, 99);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000082}
83
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000084TEST(Run_WasmModule_ReadLoadedDataSegment) {
85 static const byte kDataSegmentDest0 = 12;
Ben Murdochda12d292016-06-02 14:46:10 +010086 v8::base::AccountingAllocator allocator;
87 Zone zone(&allocator);
Ben Murdochc5610432016-08-08 18:44:38 +010088 TestSignatures sigs;
89
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000090 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone);
91 uint16_t f_index = builder->AddFunction();
92 WasmFunctionBuilder* f = builder->FunctionAt(f_index);
Ben Murdochc5610432016-08-08 18:44:38 +010093 f->SetSignature(sigs.i_v());
94
Ben Murdoch61f157c2016-09-16 13:49:30 +010095 ExportAsMain(f);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000096 byte code[] = {
97 WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(kDataSegmentDest0))};
98 f->EmitCode(code, sizeof(code));
99 byte data[] = {0xaa, 0xbb, 0xcc, 0xdd};
100 builder->AddDataSegment(new (&zone) WasmDataSegmentEncoder(
101 &zone, data, sizeof(data), kDataSegmentDest0));
Ben Murdoch61f157c2016-09-16 13:49:30 +0100102 TestModule(&zone, builder, 0xddccbbaa);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000103}
104
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000105TEST(Run_WasmModule_CheckMemoryIsZero) {
106 static const int kCheckSize = 16 * 1024;
Ben Murdochda12d292016-06-02 14:46:10 +0100107 v8::base::AccountingAllocator allocator;
108 Zone zone(&allocator);
Ben Murdochc5610432016-08-08 18:44:38 +0100109 TestSignatures sigs;
110
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000111 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone);
112 uint16_t f_index = builder->AddFunction();
113 WasmFunctionBuilder* f = builder->FunctionAt(f_index);
Ben Murdochc5610432016-08-08 18:44:38 +0100114 f->SetSignature(sigs.i_v());
115
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000116 uint16_t localIndex = f->AddLocal(kAstI32);
Ben Murdoch61f157c2016-09-16 13:49:30 +0100117 ExportAsMain(f);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000118 byte code[] = {WASM_BLOCK(
119 2,
120 WASM_WHILE(
Ben Murdochda12d292016-06-02 14:46:10 +0100121 WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I32V_3(kCheckSize)),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000122 WASM_IF_ELSE(
123 WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(localIndex)),
124 WASM_BRV(2, WASM_I8(-1)), WASM_INC_LOCAL_BY(localIndex, 4))),
125 WASM_I8(11))};
Ben Murdochc5610432016-08-08 18:44:38 +0100126 f->EmitCode(code, sizeof(code));
Ben Murdoch61f157c2016-09-16 13:49:30 +0100127 TestModule(&zone, builder, 11);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000128}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000129
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000130TEST(Run_WasmModule_CallMain_recursive) {
Ben Murdochda12d292016-06-02 14:46:10 +0100131 v8::base::AccountingAllocator allocator;
132 Zone zone(&allocator);
Ben Murdochc5610432016-08-08 18:44:38 +0100133 TestSignatures sigs;
134
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000135 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone);
136 uint16_t f_index = builder->AddFunction();
137 WasmFunctionBuilder* f = builder->FunctionAt(f_index);
Ben Murdochc5610432016-08-08 18:44:38 +0100138 f->SetSignature(sigs.i_v());
139
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000140 uint16_t localIndex = f->AddLocal(kAstI32);
Ben Murdoch61f157c2016-09-16 13:49:30 +0100141 ExportAsMain(f);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000142 byte code[] = {WASM_BLOCK(
143 2, WASM_SET_LOCAL(localIndex,
144 WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)),
145 WASM_IF_ELSE(WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I8(5)),
146 WASM_BLOCK(2, WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO,
147 WASM_INC_LOCAL(localIndex)),
148 WASM_BRV(1, WASM_CALL_FUNCTION0(0))),
149 WASM_BRV(0, WASM_I8(55))))};
Ben Murdochc5610432016-08-08 18:44:38 +0100150 f->EmitCode(code, sizeof(code));
Ben Murdoch61f157c2016-09-16 13:49:30 +0100151 TestModule(&zone, builder, 55);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000152}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000153
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000154TEST(Run_WasmModule_Global) {
Ben Murdochda12d292016-06-02 14:46:10 +0100155 v8::base::AccountingAllocator allocator;
156 Zone zone(&allocator);
Ben Murdochc5610432016-08-08 18:44:38 +0100157 TestSignatures sigs;
158
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000159 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone);
160 uint32_t global1 = builder->AddGlobal(MachineType::Int32(), 0);
161 uint32_t global2 = builder->AddGlobal(MachineType::Int32(), 0);
162 uint16_t f1_index = builder->AddFunction();
163 WasmFunctionBuilder* f = builder->FunctionAt(f1_index);
Ben Murdochc5610432016-08-08 18:44:38 +0100164 f->SetSignature(sigs.i_v());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000165 byte code1[] = {
166 WASM_I32_ADD(WASM_LOAD_GLOBAL(global1), WASM_LOAD_GLOBAL(global2))};
167 f->EmitCode(code1, sizeof(code1));
168 uint16_t f2_index = builder->AddFunction();
169 f = builder->FunctionAt(f2_index);
Ben Murdochc5610432016-08-08 18:44:38 +0100170 f->SetSignature(sigs.i_v());
Ben Murdoch61f157c2016-09-16 13:49:30 +0100171 ExportAsMain(f);
Ben Murdochda12d292016-06-02 14:46:10 +0100172 byte code2[] = {WASM_STORE_GLOBAL(global1, WASM_I32V_1(56)),
173 WASM_STORE_GLOBAL(global2, WASM_I32V_1(41)),
Ben Murdochc5610432016-08-08 18:44:38 +0100174 WASM_RETURN1(WASM_CALL_FUNCTION0(f1_index))};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000175 f->EmitCode(code2, sizeof(code2));
Ben Murdoch61f157c2016-09-16 13:49:30 +0100176 TestModule(&zone, builder, 97);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000177}