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