OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdlib.h> | 5 #include <stdlib.h> |
6 #include <string.h> | 6 #include <string.h> |
7 | 7 |
8 #include "src/wasm/encoder.h" | 8 #include "src/wasm/encoder.h" |
9 #include "src/wasm/module-decoder.h" | 9 #include "src/wasm/module-decoder.h" |
10 #include "src/wasm/wasm-macro-gen.h" | 10 #include "src/wasm/wasm-macro-gen.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 | 46 |
47 } // namespace | 47 } // namespace |
48 | 48 |
49 TEST(Run_WasmModule_Return114) { | 49 TEST(Run_WasmModule_Return114) { |
50 static const int32_t kReturnValue = 114; | 50 static const int32_t kReturnValue = 114; |
51 TestSignatures sigs; | 51 TestSignatures sigs; |
52 v8::internal::AccountingAllocator allocator; | 52 v8::internal::AccountingAllocator allocator; |
53 Zone zone(&allocator); | 53 Zone zone(&allocator); |
54 | 54 |
55 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 55 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
56 uint16_t f_index = builder->AddFunction(); | 56 WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v()); |
57 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | |
58 f->SetSignature(sigs.i_v()); | |
59 ExportAsMain(f); | 57 ExportAsMain(f); |
60 byte code[] = {WASM_I8(kReturnValue)}; | 58 byte code[] = {WASM_I8(kReturnValue)}; |
61 f->EmitCode(code, sizeof(code)); | 59 f->EmitCode(code, sizeof(code)); |
62 TestModule(&zone, builder, kReturnValue); | 60 TestModule(&zone, builder, kReturnValue); |
63 } | 61 } |
64 | 62 |
65 TEST(Run_WasmModule_CallAdd) { | 63 TEST(Run_WasmModule_CallAdd) { |
66 v8::internal::AccountingAllocator allocator; | 64 v8::internal::AccountingAllocator allocator; |
67 Zone zone(&allocator); | 65 Zone zone(&allocator); |
68 TestSignatures sigs; | 66 TestSignatures sigs; |
69 | 67 |
70 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 68 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
71 | 69 |
72 uint16_t f1_index = builder->AddFunction(); | 70 WasmFunctionBuilder* f1 = builder->AddFunction(sigs.i_ii()); |
73 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); | |
74 f->SetSignature(sigs.i_ii()); | |
75 uint16_t param1 = 0; | 71 uint16_t param1 = 0; |
76 uint16_t param2 = 1; | 72 uint16_t param2 = 1; |
77 byte code1[] = {WASM_I32_ADD(WASM_GET_LOCAL(param1), WASM_GET_LOCAL(param2))}; | 73 byte code1[] = {WASM_I32_ADD(WASM_GET_LOCAL(param1), WASM_GET_LOCAL(param2))}; |
78 f->EmitCode(code1, sizeof(code1)); | 74 f1->EmitCode(code1, sizeof(code1)); |
79 | 75 |
80 uint16_t f2_index = builder->AddFunction(); | 76 WasmFunctionBuilder* f2 = builder->AddFunction(sigs.i_v()); |
81 f = builder->FunctionAt(f2_index); | |
82 f->SetSignature(sigs.i_v()); | |
83 | 77 |
84 ExportAsMain(f); | 78 ExportAsMain(f2); |
85 byte code2[] = {WASM_CALL_FUNCTION2(f1_index, WASM_I8(77), WASM_I8(22))}; | 79 byte code2[] = { |
86 f->EmitCode(code2, sizeof(code2)); | 80 WASM_CALL_FUNCTION(f1->func_index(), WASM_I8(77), WASM_I8(22))}; |
| 81 f2->EmitCode(code2, sizeof(code2)); |
87 TestModule(&zone, builder, 99); | 82 TestModule(&zone, builder, 99); |
88 } | 83 } |
89 | 84 |
90 TEST(Run_WasmModule_ReadLoadedDataSegment) { | 85 TEST(Run_WasmModule_ReadLoadedDataSegment) { |
91 static const byte kDataSegmentDest0 = 12; | 86 static const byte kDataSegmentDest0 = 12; |
92 v8::internal::AccountingAllocator allocator; | 87 v8::internal::AccountingAllocator allocator; |
93 Zone zone(&allocator); | 88 Zone zone(&allocator); |
94 TestSignatures sigs; | 89 TestSignatures sigs; |
95 | 90 |
96 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 91 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
97 uint16_t f_index = builder->AddFunction(); | 92 WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v()); |
98 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | |
99 f->SetSignature(sigs.i_v()); | |
100 | 93 |
101 ExportAsMain(f); | 94 ExportAsMain(f); |
102 byte code[] = { | 95 byte code[] = { |
103 WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(kDataSegmentDest0))}; | 96 WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(kDataSegmentDest0))}; |
104 f->EmitCode(code, sizeof(code)); | 97 f->EmitCode(code, sizeof(code)); |
105 byte data[] = {0xaa, 0xbb, 0xcc, 0xdd}; | 98 byte data[] = {0xaa, 0xbb, 0xcc, 0xdd}; |
106 builder->AddDataSegment(new (&zone) WasmDataSegmentEncoder( | 99 builder->AddDataSegment(new (&zone) WasmDataSegmentEncoder( |
107 &zone, data, sizeof(data), kDataSegmentDest0)); | 100 &zone, data, sizeof(data), kDataSegmentDest0)); |
108 TestModule(&zone, builder, 0xddccbbaa); | 101 TestModule(&zone, builder, 0xddccbbaa); |
109 } | 102 } |
110 | 103 |
111 TEST(Run_WasmModule_CheckMemoryIsZero) { | 104 TEST(Run_WasmModule_CheckMemoryIsZero) { |
112 static const int kCheckSize = 16 * 1024; | 105 static const int kCheckSize = 16 * 1024; |
113 v8::internal::AccountingAllocator allocator; | 106 v8::internal::AccountingAllocator allocator; |
114 Zone zone(&allocator); | 107 Zone zone(&allocator); |
115 TestSignatures sigs; | 108 TestSignatures sigs; |
116 | 109 |
117 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 110 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
118 uint16_t f_index = builder->AddFunction(); | 111 WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v()); |
119 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | |
120 f->SetSignature(sigs.i_v()); | |
121 | 112 |
122 uint16_t localIndex = f->AddLocal(kAstI32); | 113 uint16_t localIndex = f->AddLocal(kAstI32); |
123 ExportAsMain(f); | 114 ExportAsMain(f); |
124 byte code[] = {WASM_BLOCK( | 115 byte code[] = {WASM_BLOCK_I( |
125 WASM_WHILE( | 116 WASM_WHILE( |
126 WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I32V_3(kCheckSize)), | 117 WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I32V_3(kCheckSize)), |
127 WASM_IF_ELSE( | 118 WASM_IF_ELSE( |
128 WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(localIndex)), | 119 WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(localIndex)), |
129 WASM_BRV(2, WASM_I8(-1)), WASM_INC_LOCAL_BY(localIndex, 4))), | 120 WASM_BRV(3, WASM_I8(-1)), WASM_INC_LOCAL_BY(localIndex, 4))), |
130 WASM_I8(11))}; | 121 WASM_I8(11))}; |
131 f->EmitCode(code, sizeof(code)); | 122 f->EmitCode(code, sizeof(code)); |
132 TestModule(&zone, builder, 11); | 123 TestModule(&zone, builder, 11); |
133 } | 124 } |
134 | 125 |
135 TEST(Run_WasmModule_CallMain_recursive) { | 126 TEST(Run_WasmModule_CallMain_recursive) { |
136 v8::internal::AccountingAllocator allocator; | 127 v8::internal::AccountingAllocator allocator; |
137 Zone zone(&allocator); | 128 Zone zone(&allocator); |
138 TestSignatures sigs; | 129 TestSignatures sigs; |
139 | 130 |
140 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 131 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
141 uint16_t f_index = builder->AddFunction(); | 132 WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v()); |
142 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | |
143 f->SetSignature(sigs.i_v()); | |
144 | 133 |
145 uint16_t localIndex = f->AddLocal(kAstI32); | 134 uint16_t localIndex = f->AddLocal(kAstI32); |
146 ExportAsMain(f); | 135 ExportAsMain(f); |
147 byte code[] = {WASM_BLOCK( | 136 byte code[] = { |
148 WASM_SET_LOCAL(localIndex, | 137 WASM_SET_LOCAL(localIndex, |
149 WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)), | 138 WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)), |
150 WASM_IF_ELSE(WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I8(5)), | 139 WASM_IF_ELSE_I(WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I8(5)), |
151 WASM_BLOCK(WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO, | 140 WASM_SEQ(WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO, |
152 WASM_INC_LOCAL(localIndex)), | 141 WASM_INC_LOCAL(localIndex)), |
153 WASM_BRV(1, WASM_CALL_FUNCTION0(0))), | 142 WASM_CALL_FUNCTION0(0)), |
154 WASM_BRV(0, WASM_I8(55))))}; | 143 WASM_I8(55))}; |
155 f->EmitCode(code, sizeof(code)); | 144 f->EmitCode(code, sizeof(code)); |
156 TestModule(&zone, builder, 55); | 145 TestModule(&zone, builder, 55); |
157 } | 146 } |
158 | 147 |
159 TEST(Run_WasmModule_Global) { | 148 TEST(Run_WasmModule_Global) { |
160 v8::internal::AccountingAllocator allocator; | 149 v8::internal::AccountingAllocator allocator; |
161 Zone zone(&allocator); | 150 Zone zone(&allocator); |
162 TestSignatures sigs; | 151 TestSignatures sigs; |
163 | 152 |
164 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 153 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
165 uint32_t global1 = builder->AddGlobal(kAstI32, 0); | 154 uint32_t global1 = builder->AddGlobal(kAstI32, 0); |
166 uint32_t global2 = builder->AddGlobal(kAstI32, 0); | 155 uint32_t global2 = builder->AddGlobal(kAstI32, 0); |
167 uint16_t f1_index = builder->AddFunction(); | 156 WasmFunctionBuilder* f1 = builder->AddFunction(sigs.i_v()); |
168 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); | |
169 f->SetSignature(sigs.i_v()); | |
170 byte code1[] = { | 157 byte code1[] = { |
171 WASM_I32_ADD(WASM_GET_GLOBAL(global1), WASM_GET_GLOBAL(global2))}; | 158 WASM_I32_ADD(WASM_GET_GLOBAL(global1), WASM_GET_GLOBAL(global2))}; |
172 f->EmitCode(code1, sizeof(code1)); | 159 f1->EmitCode(code1, sizeof(code1)); |
173 uint16_t f2_index = builder->AddFunction(); | 160 WasmFunctionBuilder* f2 = builder->AddFunction(sigs.i_v()); |
174 f = builder->FunctionAt(f2_index); | 161 ExportAsMain(f2); |
175 f->SetSignature(sigs.i_v()); | |
176 ExportAsMain(f); | |
177 byte code2[] = {WASM_SET_GLOBAL(global1, WASM_I32V_1(56)), | 162 byte code2[] = {WASM_SET_GLOBAL(global1, WASM_I32V_1(56)), |
178 WASM_SET_GLOBAL(global2, WASM_I32V_1(41)), | 163 WASM_SET_GLOBAL(global2, WASM_I32V_1(41)), |
179 WASM_RETURN1(WASM_CALL_FUNCTION0(f1_index))}; | 164 WASM_RETURN1(WASM_CALL_FUNCTION0(f1->func_index()))}; |
180 f->EmitCode(code2, sizeof(code2)); | 165 f2->EmitCode(code2, sizeof(code2)); |
181 TestModule(&zone, builder, 97); | 166 TestModule(&zone, builder, 97); |
182 } | 167 } |
183 | 168 |
184 TEST(Run_WasmModule_Serialization) { | 169 TEST(Run_WasmModule_Serialization) { |
185 static const char* kFunctionName = "increment"; | 170 static const char* kFunctionName = "increment"; |
186 v8::internal::AccountingAllocator allocator; | 171 v8::internal::AccountingAllocator allocator; |
187 Zone zone(&allocator); | 172 Zone zone(&allocator); |
188 | 173 |
189 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 174 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
190 uint16_t f_index = builder->AddFunction(); | |
191 TestSignatures sigs; | 175 TestSignatures sigs; |
192 | 176 |
193 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 177 WasmFunctionBuilder* f = builder->AddFunction(sigs.i_i()); |
194 f->SetSignature(sigs.i_i()); | |
195 byte code[] = {WASM_GET_LOCAL(0), kExprI32Const, 1, kExprI32Add}; | 178 byte code[] = {WASM_GET_LOCAL(0), kExprI32Const, 1, kExprI32Add}; |
196 f->EmitCode(code, sizeof(code)); | 179 f->EmitCode(code, sizeof(code)); |
197 ExportAs(f, kFunctionName); | 180 ExportAs(f, kFunctionName); |
198 | 181 |
199 ZoneBuffer buffer(&zone); | 182 ZoneBuffer buffer(&zone); |
200 builder->WriteTo(buffer); | 183 builder->WriteTo(buffer); |
201 | 184 |
202 Isolate* isolate = CcTest::InitIsolateOnce(); | 185 Isolate* isolate = CcTest::InitIsolateOnce(); |
203 ErrorThrower thrower(isolate, ""); | 186 ErrorThrower thrower(isolate, ""); |
204 v8::WasmCompiledModule::SerializedModule data; | 187 v8::WasmCompiledModule::SerializedModule data; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 isolate = reinterpret_cast<Isolate*>(v8_isolate); | 220 isolate = reinterpret_cast<Isolate*>(v8_isolate); |
238 testing::SetupIsolateForWasmModule(isolate); | 221 testing::SetupIsolateForWasmModule(isolate); |
239 | 222 |
240 v8::MaybeLocal<v8::WasmCompiledModule> deserialized = | 223 v8::MaybeLocal<v8::WasmCompiledModule> deserialized = |
241 v8::WasmCompiledModule::Deserialize(v8_isolate, data); | 224 v8::WasmCompiledModule::Deserialize(v8_isolate, data); |
242 v8::Local<v8::WasmCompiledModule> compiled_module; | 225 v8::Local<v8::WasmCompiledModule> compiled_module; |
243 CHECK(deserialized.ToLocal(&compiled_module)); | 226 CHECK(deserialized.ToLocal(&compiled_module)); |
244 Handle<JSObject> module_object = | 227 Handle<JSObject> module_object = |
245 Handle<JSObject>::cast(v8::Utils::OpenHandle(*compiled_module)); | 228 Handle<JSObject>::cast(v8::Utils::OpenHandle(*compiled_module)); |
246 Handle<JSObject> instance = | 229 Handle<JSObject> instance = |
247 WasmModule::Instantiate(isolate, module_object, | 230 WasmModule::Instantiate(isolate, &thrower, module_object, |
248 Handle<JSReceiver>::null(), | 231 Handle<JSReceiver>::null(), |
249 Handle<JSArrayBuffer>::null()) | 232 Handle<JSArrayBuffer>::null()) |
250 .ToHandleChecked(); | 233 .ToHandleChecked(); |
251 Handle<Object> params[1] = {Handle<Object>(Smi::FromInt(41), isolate)}; | 234 Handle<Object> params[1] = {Handle<Object>(Smi::FromInt(41), isolate)}; |
252 int32_t result = testing::CallWasmFunctionForTesting( | 235 int32_t result = testing::CallWasmFunctionForTesting( |
253 isolate, instance, &thrower, kFunctionName, 1, params, | 236 isolate, instance, &thrower, kFunctionName, 1, params, |
254 ModuleOrigin::kWasmOrigin); | 237 ModuleOrigin::kWasmOrigin); |
255 CHECK(result == 42); | 238 CHECK(result == 42); |
256 new_ctx->Exit(); | 239 new_ctx->Exit(); |
257 } | 240 } |
258 } | 241 } |
259 | 242 |
260 TEST(Run_WasmModule_MemSize_GrowMem) { | 243 TEST(Run_WasmModule_MemSize_GrowMem) { |
261 static const int kPageSize = 0x10000; | |
262 // Initial memory size = 16 + GrowMemory(10) | 244 // Initial memory size = 16 + GrowMemory(10) |
263 static const int kExpectedValue = kPageSize * 26; | 245 static const int kExpectedValue = 26; |
264 TestSignatures sigs; | 246 TestSignatures sigs; |
265 v8::internal::AccountingAllocator allocator; | 247 v8::internal::AccountingAllocator allocator; |
266 Zone zone(&allocator); | 248 Zone zone(&allocator); |
267 | 249 |
268 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 250 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
269 uint16_t f_index = builder->AddFunction(); | 251 WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v()); |
270 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | |
271 f->SetSignature(sigs.i_v()); | |
272 ExportAsMain(f); | 252 ExportAsMain(f); |
273 byte code[] = {WASM_GROW_MEMORY(WASM_I8(10)), WASM_MEMORY_SIZE}; | 253 byte code[] = {WASM_GROW_MEMORY(WASM_I8(10)), WASM_DROP, WASM_MEMORY_SIZE}; |
274 f->EmitCode(code, sizeof(code)); | 254 f->EmitCode(code, sizeof(code)); |
275 TestModule(&zone, builder, kExpectedValue); | 255 TestModule(&zone, builder, kExpectedValue); |
276 } | 256 } |
277 | 257 |
278 TEST(Run_WasmModule_GrowMemoryInIf) { | 258 TEST(Run_WasmModule_GrowMemoryInIf) { |
279 TestSignatures sigs; | 259 TestSignatures sigs; |
280 v8::internal::AccountingAllocator allocator; | 260 v8::internal::AccountingAllocator allocator; |
281 Zone zone(&allocator); | 261 Zone zone(&allocator); |
282 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 262 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
283 uint16_t f_index = builder->AddFunction(); | 263 WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v()); |
284 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | |
285 f->SetSignature(sigs.i_v()); | |
286 ExportAsMain(f); | 264 ExportAsMain(f); |
287 byte code[] = {WASM_IF_ELSE(WASM_I32V(0), WASM_GROW_MEMORY(WASM_I32V(1)), | 265 byte code[] = {WASM_IF_ELSE_I(WASM_I32V(0), WASM_GROW_MEMORY(WASM_I32V(1)), |
288 WASM_I32V(12))}; | 266 WASM_I32V(12))}; |
289 f->EmitCode(code, sizeof(code)); | 267 f->EmitCode(code, sizeof(code)); |
290 TestModule(&zone, builder, 12); | 268 TestModule(&zone, builder, 12); |
291 } | 269 } |
OLD | NEW |