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