| 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/wasm-js.h" | 9 #include "src/wasm/wasm-js.h" |
| 10 #include "src/wasm/wasm-macro-gen.h" | 10 #include "src/wasm/wasm-macro-gen.h" |
| 11 #include "src/wasm/wasm-module.h" | 11 #include "src/wasm/wasm-module.h" |
| 12 #include "src/wasm/wasm-opcodes.h" | 12 #include "src/wasm/wasm-opcodes.h" |
| 13 | 13 |
| 14 #include "test/cctest/cctest.h" | 14 #include "test/cctest/cctest.h" |
| 15 #include "test/cctest/wasm/test-signatures.h" |
| 15 | 16 |
| 16 using namespace v8::base; | 17 using namespace v8::base; |
| 17 using namespace v8::internal; | 18 using namespace v8::internal; |
| 18 using namespace v8::internal::compiler; | 19 using namespace v8::internal::compiler; |
| 19 using namespace v8::internal::wasm; | 20 using namespace v8::internal::wasm; |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 void TestModule(WasmModuleIndex* module, int32_t expected_result) { | 23 void TestModule(WasmModuleIndex* module, int32_t expected_result) { |
| 23 Isolate* isolate = CcTest::InitIsolateOnce(); | 24 Isolate* isolate = CcTest::InitIsolateOnce(); |
| 24 HandleScope scope(isolate); | 25 HandleScope scope(isolate); |
| 25 WasmJs::InstallWasmFunctionMap(isolate, isolate->native_context()); | 26 WasmJs::InstallWasmFunctionMap(isolate, isolate->native_context()); |
| 26 int32_t result = | 27 int32_t result = |
| 27 CompileAndRunWasmModule(isolate, module->Begin(), module->End()); | 28 CompileAndRunWasmModule(isolate, module->Begin(), module->End()); |
| 28 CHECK_EQ(expected_result, result); | 29 CHECK_EQ(expected_result, result); |
| 29 } | 30 } |
| 30 } // namespace | 31 } // namespace |
| 31 | 32 |
| 32 TEST(Run_WasmModule_Return114) { | 33 TEST(Run_WasmModule_Return114) { |
| 33 static const int32_t kReturnValue = 114; | 34 static const int32_t kReturnValue = 114; |
| 35 TestSignatures sigs; |
| 34 v8::base::AccountingAllocator allocator; | 36 v8::base::AccountingAllocator allocator; |
| 35 Zone zone(&allocator); | 37 Zone zone(&allocator); |
| 38 |
| 36 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 39 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 37 uint16_t f_index = builder->AddFunction(); | 40 uint16_t f_index = builder->AddFunction(); |
| 38 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 41 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
| 39 f->ReturnType(kAstI32); | 42 f->SetSignature(sigs.i_v()); |
| 40 f->Exported(1); | 43 f->Exported(1); |
| 41 byte code[] = {WASM_I8(kReturnValue)}; | 44 byte code[] = {WASM_I8(kReturnValue)}; |
| 42 f->EmitCode(code, sizeof(code)); | 45 f->EmitCode(code, sizeof(code)); |
| 43 WasmModuleWriter* writer = builder->Build(&zone); | 46 WasmModuleWriter* writer = builder->Build(&zone); |
| 44 TestModule(writer->WriteTo(&zone), kReturnValue); | 47 TestModule(writer->WriteTo(&zone), kReturnValue); |
| 45 } | 48 } |
| 46 | 49 |
| 47 TEST(Run_WasmModule_CallAdd) { | 50 TEST(Run_WasmModule_CallAdd) { |
| 48 v8::base::AccountingAllocator allocator; | 51 v8::base::AccountingAllocator allocator; |
| 49 Zone zone(&allocator); | 52 Zone zone(&allocator); |
| 53 TestSignatures sigs; |
| 54 |
| 50 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 55 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 56 |
| 51 uint16_t f1_index = builder->AddFunction(); | 57 uint16_t f1_index = builder->AddFunction(); |
| 52 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); | 58 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); |
| 53 f->ReturnType(kAstI32); | 59 f->SetSignature(sigs.i_ii()); |
| 54 uint16_t param1 = f->AddParam(kAstI32); | 60 uint16_t param1 = 0; |
| 55 uint16_t param2 = f->AddParam(kAstI32); | 61 uint16_t param2 = 1; |
| 56 byte code1[] = {WASM_I32_ADD(WASM_GET_LOCAL(param1), WASM_GET_LOCAL(param2))}; | 62 byte code1[] = {WASM_I32_ADD(WASM_GET_LOCAL(param1), WASM_GET_LOCAL(param2))}; |
| 57 f->EmitCode(code1, sizeof(code1)); | 63 f->EmitCode(code1, sizeof(code1)); |
| 64 |
| 58 uint16_t f2_index = builder->AddFunction(); | 65 uint16_t f2_index = builder->AddFunction(); |
| 59 f = builder->FunctionAt(f2_index); | 66 f = builder->FunctionAt(f2_index); |
| 60 f->ReturnType(kAstI32); | 67 f->SetSignature(sigs.i_v()); |
| 68 |
| 61 f->Exported(1); | 69 f->Exported(1); |
| 62 byte code2[] = {WASM_CALL_FUNCTION2(f1_index, WASM_I8(77), WASM_I8(22))}; | 70 byte code2[] = {WASM_CALL_FUNCTION2(f1_index, WASM_I8(77), WASM_I8(22))}; |
| 63 f->EmitCode(code2, sizeof(code2)); | 71 f->EmitCode(code2, sizeof(code2)); |
| 64 WasmModuleWriter* writer = builder->Build(&zone); | 72 WasmModuleWriter* writer = builder->Build(&zone); |
| 65 TestModule(writer->WriteTo(&zone), 99); | 73 TestModule(writer->WriteTo(&zone), 99); |
| 66 } | 74 } |
| 67 | 75 |
| 68 TEST(Run_WasmModule_ReadLoadedDataSegment) { | 76 TEST(Run_WasmModule_ReadLoadedDataSegment) { |
| 69 static const byte kDataSegmentDest0 = 12; | 77 static const byte kDataSegmentDest0 = 12; |
| 70 v8::base::AccountingAllocator allocator; | 78 v8::base::AccountingAllocator allocator; |
| 71 Zone zone(&allocator); | 79 Zone zone(&allocator); |
| 80 TestSignatures sigs; |
| 81 |
| 72 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 82 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 73 uint16_t f_index = builder->AddFunction(); | 83 uint16_t f_index = builder->AddFunction(); |
| 74 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 84 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
| 75 f->ReturnType(kAstI32); | 85 f->SetSignature(sigs.i_v()); |
| 86 |
| 76 f->Exported(1); | 87 f->Exported(1); |
| 77 byte code[] = { | 88 byte code[] = { |
| 78 WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(kDataSegmentDest0))}; | 89 WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(kDataSegmentDest0))}; |
| 79 f->EmitCode(code, sizeof(code)); | 90 f->EmitCode(code, sizeof(code)); |
| 80 byte data[] = {0xaa, 0xbb, 0xcc, 0xdd}; | 91 byte data[] = {0xaa, 0xbb, 0xcc, 0xdd}; |
| 81 builder->AddDataSegment(new (&zone) WasmDataSegmentEncoder( | 92 builder->AddDataSegment(new (&zone) WasmDataSegmentEncoder( |
| 82 &zone, data, sizeof(data), kDataSegmentDest0)); | 93 &zone, data, sizeof(data), kDataSegmentDest0)); |
| 83 WasmModuleWriter* writer = builder->Build(&zone); | 94 WasmModuleWriter* writer = builder->Build(&zone); |
| 84 TestModule(writer->WriteTo(&zone), 0xddccbbaa); | 95 TestModule(writer->WriteTo(&zone), 0xddccbbaa); |
| 85 } | 96 } |
| 86 | 97 |
| 87 TEST(Run_WasmModule_CheckMemoryIsZero) { | 98 TEST(Run_WasmModule_CheckMemoryIsZero) { |
| 88 static const int kCheckSize = 16 * 1024; | 99 static const int kCheckSize = 16 * 1024; |
| 89 v8::base::AccountingAllocator allocator; | 100 v8::base::AccountingAllocator allocator; |
| 90 Zone zone(&allocator); | 101 Zone zone(&allocator); |
| 102 TestSignatures sigs; |
| 103 |
| 91 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 104 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 92 uint16_t f_index = builder->AddFunction(); | 105 uint16_t f_index = builder->AddFunction(); |
| 93 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 106 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
| 94 f->ReturnType(kAstI32); | 107 f->SetSignature(sigs.i_v()); |
| 108 |
| 95 uint16_t localIndex = f->AddLocal(kAstI32); | 109 uint16_t localIndex = f->AddLocal(kAstI32); |
| 96 f->Exported(1); | 110 f->Exported(1); |
| 97 byte code[] = {WASM_BLOCK( | 111 byte code[] = {WASM_BLOCK( |
| 98 2, | 112 2, |
| 99 WASM_WHILE( | 113 WASM_WHILE( |
| 100 WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I32V_3(kCheckSize)), | 114 WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I32V_3(kCheckSize)), |
| 101 WASM_IF_ELSE( | 115 WASM_IF_ELSE( |
| 102 WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(localIndex)), | 116 WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(localIndex)), |
| 103 WASM_BRV(2, WASM_I8(-1)), WASM_INC_LOCAL_BY(localIndex, 4))), | 117 WASM_BRV(2, WASM_I8(-1)), WASM_INC_LOCAL_BY(localIndex, 4))), |
| 104 WASM_I8(11))}; | 118 WASM_I8(11))}; |
| 105 f->EmitCode(code, sizeof(code), nullptr, 0); | 119 f->EmitCode(code, sizeof(code)); |
| 106 WasmModuleWriter* writer = builder->Build(&zone); | 120 WasmModuleWriter* writer = builder->Build(&zone); |
| 107 TestModule(writer->WriteTo(&zone), 11); | 121 TestModule(writer->WriteTo(&zone), 11); |
| 108 } | 122 } |
| 109 | 123 |
| 110 TEST(Run_WasmModule_CallMain_recursive) { | 124 TEST(Run_WasmModule_CallMain_recursive) { |
| 111 v8::base::AccountingAllocator allocator; | 125 v8::base::AccountingAllocator allocator; |
| 112 Zone zone(&allocator); | 126 Zone zone(&allocator); |
| 127 TestSignatures sigs; |
| 128 |
| 113 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 129 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 114 uint16_t f_index = builder->AddFunction(); | 130 uint16_t f_index = builder->AddFunction(); |
| 115 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 131 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
| 116 f->ReturnType(kAstI32); | 132 f->SetSignature(sigs.i_v()); |
| 133 |
| 117 uint16_t localIndex = f->AddLocal(kAstI32); | 134 uint16_t localIndex = f->AddLocal(kAstI32); |
| 118 f->Exported(1); | 135 f->Exported(1); |
| 119 byte code[] = {WASM_BLOCK( | 136 byte code[] = {WASM_BLOCK( |
| 120 2, WASM_SET_LOCAL(localIndex, | 137 2, WASM_SET_LOCAL(localIndex, |
| 121 WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)), | 138 WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)), |
| 122 WASM_IF_ELSE(WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I8(5)), | 139 WASM_IF_ELSE(WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I8(5)), |
| 123 WASM_BLOCK(2, WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO, | 140 WASM_BLOCK(2, WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO, |
| 124 WASM_INC_LOCAL(localIndex)), | 141 WASM_INC_LOCAL(localIndex)), |
| 125 WASM_BRV(1, WASM_CALL_FUNCTION0(0))), | 142 WASM_BRV(1, WASM_CALL_FUNCTION0(0))), |
| 126 WASM_BRV(0, WASM_I8(55))))}; | 143 WASM_BRV(0, WASM_I8(55))))}; |
| 127 f->EmitCode(code, sizeof(code), nullptr, 0); | 144 f->EmitCode(code, sizeof(code)); |
| 128 WasmModuleWriter* writer = builder->Build(&zone); | 145 WasmModuleWriter* writer = builder->Build(&zone); |
| 129 TestModule(writer->WriteTo(&zone), 55); | 146 TestModule(writer->WriteTo(&zone), 55); |
| 130 } | 147 } |
| 131 | 148 |
| 132 TEST(Run_WasmModule_Global) { | 149 TEST(Run_WasmModule_Global) { |
| 133 v8::base::AccountingAllocator allocator; | 150 v8::base::AccountingAllocator allocator; |
| 134 Zone zone(&allocator); | 151 Zone zone(&allocator); |
| 152 TestSignatures sigs; |
| 153 |
| 135 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 154 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 136 uint32_t global1 = builder->AddGlobal(MachineType::Int32(), 0); | 155 uint32_t global1 = builder->AddGlobal(MachineType::Int32(), 0); |
| 137 uint32_t global2 = builder->AddGlobal(MachineType::Int32(), 0); | 156 uint32_t global2 = builder->AddGlobal(MachineType::Int32(), 0); |
| 138 uint16_t f1_index = builder->AddFunction(); | 157 uint16_t f1_index = builder->AddFunction(); |
| 139 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); | 158 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); |
| 140 f->ReturnType(kAstI32); | 159 f->SetSignature(sigs.i_v()); |
| 141 byte code1[] = { | 160 byte code1[] = { |
| 142 WASM_I32_ADD(WASM_LOAD_GLOBAL(global1), WASM_LOAD_GLOBAL(global2))}; | 161 WASM_I32_ADD(WASM_LOAD_GLOBAL(global1), WASM_LOAD_GLOBAL(global2))}; |
| 143 f->EmitCode(code1, sizeof(code1)); | 162 f->EmitCode(code1, sizeof(code1)); |
| 144 uint16_t f2_index = builder->AddFunction(); | 163 uint16_t f2_index = builder->AddFunction(); |
| 145 f = builder->FunctionAt(f2_index); | 164 f = builder->FunctionAt(f2_index); |
| 146 f->ReturnType(kAstI32); | 165 f->SetSignature(sigs.i_v()); |
| 147 f->Exported(1); | 166 f->Exported(1); |
| 148 byte code2[] = {WASM_STORE_GLOBAL(global1, WASM_I32V_1(56)), | 167 byte code2[] = {WASM_STORE_GLOBAL(global1, WASM_I32V_1(56)), |
| 149 WASM_STORE_GLOBAL(global2, WASM_I32V_1(41)), | 168 WASM_STORE_GLOBAL(global2, WASM_I32V_1(41)), |
| 150 WASM_RETURN1(WASM_CALL_FUNCTION0(f1_index))}; | 169 WASM_RETURN1(WASM_CALL_FUNCTION0(f1_index))}; |
| 151 f->EmitCode(code2, sizeof(code2)); | 170 f->EmitCode(code2, sizeof(code2)); |
| 152 WasmModuleWriter* writer = builder->Build(&zone); | 171 WasmModuleWriter* writer = builder->Build(&zone); |
| 153 TestModule(writer->WriteTo(&zone), 97); | 172 TestModule(writer->WriteTo(&zone), 97); |
| 154 } | 173 } |
| OLD | NEW |