| 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> | |
| 6 #include <string.h> | |
| 7 | |
| 8 #include "src/wasm/encoder.h" | |
| 9 #include "src/wasm/module-decoder.h" | |
| 10 #include "src/wasm/wasm-js.h" | |
| 11 #include "src/wasm/wasm-macro-gen.h" | 5 #include "src/wasm/wasm-macro-gen.h" |
| 12 #include "src/wasm/wasm-module.h" | 6 #include "src/wasm/wasm-module.h" |
| 13 #include "src/wasm/wasm-opcodes.h" | |
| 14 | 7 |
| 15 #include "test/cctest/cctest.h" | 8 #include "test/cctest/cctest.h" |
| 9 #include "test/cctest/wasm/test-run-wasm-module-helper.h" |
| 16 #include "test/cctest/wasm/test-signatures.h" | 10 #include "test/cctest/wasm/test-signatures.h" |
| 17 | 11 |
| 18 using namespace v8::base; | 12 using namespace v8::base; |
| 19 using namespace v8::internal; | 13 using namespace v8::internal; |
| 20 using namespace v8::internal::compiler; | 14 using namespace v8::internal::compiler; |
| 21 using namespace v8::internal::wasm; | 15 using namespace v8::internal::wasm; |
| 22 | 16 |
| 23 namespace { | |
| 24 void TestModule(Zone* zone, WasmModuleBuilder* builder, | |
| 25 int32_t expected_result) { | |
| 26 ZoneBuffer buffer(zone); | |
| 27 builder->WriteTo(buffer); | |
| 28 | |
| 29 Isolate* isolate = CcTest::InitIsolateOnce(); | |
| 30 HandleScope scope(isolate); | |
| 31 WasmJs::InstallWasmFunctionMap(isolate, isolate->native_context()); | |
| 32 int32_t result = | |
| 33 testing::CompileAndRunWasmModule(isolate, buffer.begin(), buffer.end()); | |
| 34 CHECK_EQ(expected_result, result); | |
| 35 } | |
| 36 | |
| 37 void ExportAs(WasmFunctionBuilder* f, const char* name) { | |
| 38 f->SetExported(); | |
| 39 f->SetName(name, static_cast<int>(strlen(name))); | |
| 40 } | |
| 41 | |
| 42 void ExportAsMain(WasmFunctionBuilder* f) { | |
| 43 static const char kMainName[] = "main"; | |
| 44 ExportAs(f, kMainName); | |
| 45 } | |
| 46 | |
| 47 } // namespace | |
| 48 | |
| 49 TEST(Run_WasmModule_Return114) { | 17 TEST(Run_WasmModule_Return114) { |
| 50 static const int32_t kReturnValue = 114; | 18 static const int32_t kReturnValue = 114; |
| 51 TestSignatures sigs; | 19 TestSignatures sigs; |
| 52 v8::base::AccountingAllocator allocator; | 20 v8::base::AccountingAllocator allocator; |
| 53 Zone zone(&allocator); | 21 Zone zone(&allocator); |
| 54 | 22 |
| 55 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 23 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 56 uint16_t f_index = builder->AddFunction(); | 24 uint16_t f_index = builder->AddFunction(); |
| 57 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 25 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
| 58 f->SetSignature(sigs.i_v()); | 26 f->SetSignature(sigs.i_v()); |
| 59 ExportAsMain(f); | 27 TestRunWasmModuleHelper::ExportAsMain(f); |
| 60 byte code[] = {WASM_I8(kReturnValue)}; | 28 byte code[] = {WASM_I8(kReturnValue)}; |
| 61 f->EmitCode(code, sizeof(code)); | 29 f->EmitCode(code, sizeof(code)); |
| 62 TestModule(&zone, builder, kReturnValue); | 30 TestRunWasmModuleHelper::TestModule(&zone, builder, kReturnValue); |
| 63 } | 31 } |
| 64 | 32 |
| 65 TEST(Run_WasmModule_CallAdd) { | 33 TEST(Run_WasmModule_CallAdd) { |
| 66 v8::base::AccountingAllocator allocator; | 34 v8::base::AccountingAllocator allocator; |
| 67 Zone zone(&allocator); | 35 Zone zone(&allocator); |
| 68 TestSignatures sigs; | 36 TestSignatures sigs; |
| 69 | 37 |
| 70 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 38 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 71 | 39 |
| 72 uint16_t f1_index = builder->AddFunction(); | 40 uint16_t f1_index = builder->AddFunction(); |
| 73 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); | 41 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); |
| 74 f->SetSignature(sigs.i_ii()); | 42 f->SetSignature(sigs.i_ii()); |
| 75 uint16_t param1 = 0; | 43 uint16_t param1 = 0; |
| 76 uint16_t param2 = 1; | 44 uint16_t param2 = 1; |
| 77 byte code1[] = {WASM_I32_ADD(WASM_GET_LOCAL(param1), WASM_GET_LOCAL(param2))}; | 45 byte code1[] = {WASM_I32_ADD(WASM_GET_LOCAL(param1), WASM_GET_LOCAL(param2))}; |
| 78 f->EmitCode(code1, sizeof(code1)); | 46 f->EmitCode(code1, sizeof(code1)); |
| 79 | 47 |
| 80 uint16_t f2_index = builder->AddFunction(); | 48 uint16_t f2_index = builder->AddFunction(); |
| 81 f = builder->FunctionAt(f2_index); | 49 f = builder->FunctionAt(f2_index); |
| 82 f->SetSignature(sigs.i_v()); | 50 f->SetSignature(sigs.i_v()); |
| 83 | 51 |
| 84 ExportAsMain(f); | 52 TestRunWasmModuleHelper::ExportAsMain(f); |
| 85 byte code2[] = {WASM_CALL_FUNCTION2(f1_index, WASM_I8(77), WASM_I8(22))}; | 53 byte code2[] = {WASM_CALL_FUNCTION2(f1_index, WASM_I8(77), WASM_I8(22))}; |
| 86 f->EmitCode(code2, sizeof(code2)); | 54 f->EmitCode(code2, sizeof(code2)); |
| 87 TestModule(&zone, builder, 99); | 55 TestRunWasmModuleHelper::TestModule(&zone, builder, 99); |
| 88 } | 56 } |
| 89 | 57 |
| 90 TEST(Run_WasmModule_ReadLoadedDataSegment) { | 58 TEST(Run_WasmModule_ReadLoadedDataSegment) { |
| 91 static const byte kDataSegmentDest0 = 12; | 59 static const byte kDataSegmentDest0 = 12; |
| 92 v8::base::AccountingAllocator allocator; | 60 v8::base::AccountingAllocator allocator; |
| 93 Zone zone(&allocator); | 61 Zone zone(&allocator); |
| 94 TestSignatures sigs; | 62 TestSignatures sigs; |
| 95 | 63 |
| 96 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 64 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 97 uint16_t f_index = builder->AddFunction(); | 65 uint16_t f_index = builder->AddFunction(); |
| 98 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 66 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
| 99 f->SetSignature(sigs.i_v()); | 67 f->SetSignature(sigs.i_v()); |
| 100 | 68 |
| 101 ExportAsMain(f); | 69 TestRunWasmModuleHelper::ExportAsMain(f); |
| 102 byte code[] = { | 70 byte code[] = { |
| 103 WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(kDataSegmentDest0))}; | 71 WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(kDataSegmentDest0))}; |
| 104 f->EmitCode(code, sizeof(code)); | 72 f->EmitCode(code, sizeof(code)); |
| 105 byte data[] = {0xaa, 0xbb, 0xcc, 0xdd}; | 73 byte data[] = {0xaa, 0xbb, 0xcc, 0xdd}; |
| 106 builder->AddDataSegment(new (&zone) WasmDataSegmentEncoder( | 74 builder->AddDataSegment(new (&zone) WasmDataSegmentEncoder( |
| 107 &zone, data, sizeof(data), kDataSegmentDest0)); | 75 &zone, data, sizeof(data), kDataSegmentDest0)); |
| 108 TestModule(&zone, builder, 0xddccbbaa); | 76 TestRunWasmModuleHelper::TestModule(&zone, builder, 0xddccbbaa); |
| 109 } | 77 } |
| 110 | 78 |
| 111 TEST(Run_WasmModule_CheckMemoryIsZero) { | 79 TEST(Run_WasmModule_CheckMemoryIsZero) { |
| 112 static const int kCheckSize = 16 * 1024; | 80 static const int kCheckSize = 16 * 1024; |
| 113 v8::base::AccountingAllocator allocator; | 81 v8::base::AccountingAllocator allocator; |
| 114 Zone zone(&allocator); | 82 Zone zone(&allocator); |
| 115 TestSignatures sigs; | 83 TestSignatures sigs; |
| 116 | 84 |
| 117 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 85 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 118 uint16_t f_index = builder->AddFunction(); | 86 uint16_t f_index = builder->AddFunction(); |
| 119 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 87 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
| 120 f->SetSignature(sigs.i_v()); | 88 f->SetSignature(sigs.i_v()); |
| 121 | 89 |
| 122 uint16_t localIndex = f->AddLocal(kAstI32); | 90 uint16_t localIndex = f->AddLocal(kAstI32); |
| 123 ExportAsMain(f); | 91 TestRunWasmModuleHelper::ExportAsMain(f); |
| 124 byte code[] = {WASM_BLOCK( | 92 byte code[] = {WASM_BLOCK( |
| 125 WASM_WHILE( | 93 WASM_WHILE( |
| 126 WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I32V_3(kCheckSize)), | 94 WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I32V_3(kCheckSize)), |
| 127 WASM_IF_ELSE( | 95 WASM_IF_ELSE( |
| 128 WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(localIndex)), | 96 WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(localIndex)), |
| 129 WASM_BRV(2, WASM_I8(-1)), WASM_INC_LOCAL_BY(localIndex, 4))), | 97 WASM_BRV(2, WASM_I8(-1)), WASM_INC_LOCAL_BY(localIndex, 4))), |
| 130 WASM_I8(11))}; | 98 WASM_I8(11))}; |
| 131 f->EmitCode(code, sizeof(code)); | 99 f->EmitCode(code, sizeof(code)); |
| 132 TestModule(&zone, builder, 11); | 100 TestRunWasmModuleHelper::TestModule(&zone, builder, 11); |
| 133 } | 101 } |
| 134 | 102 |
| 135 TEST(Run_WasmModule_CallMain_recursive) { | 103 TEST(Run_WasmModule_CallMain_recursive) { |
| 136 v8::base::AccountingAllocator allocator; | 104 v8::base::AccountingAllocator allocator; |
| 137 Zone zone(&allocator); | 105 Zone zone(&allocator); |
| 138 TestSignatures sigs; | 106 TestSignatures sigs; |
| 139 | 107 |
| 140 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 108 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 141 uint16_t f_index = builder->AddFunction(); | 109 uint16_t f_index = builder->AddFunction(); |
| 142 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 110 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
| 143 f->SetSignature(sigs.i_v()); | 111 f->SetSignature(sigs.i_v()); |
| 144 | 112 |
| 145 uint16_t localIndex = f->AddLocal(kAstI32); | 113 uint16_t localIndex = f->AddLocal(kAstI32); |
| 146 ExportAsMain(f); | 114 TestRunWasmModuleHelper::ExportAsMain(f); |
| 147 byte code[] = {WASM_BLOCK( | 115 byte code[] = {WASM_BLOCK( |
| 148 WASM_SET_LOCAL(localIndex, | 116 WASM_SET_LOCAL(localIndex, |
| 149 WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)), | 117 WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)), |
| 150 WASM_IF_ELSE(WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I8(5)), | 118 WASM_IF_ELSE(WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I8(5)), |
| 151 WASM_BLOCK(WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO, | 119 WASM_BLOCK(WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO, |
| 152 WASM_INC_LOCAL(localIndex)), | 120 WASM_INC_LOCAL(localIndex)), |
| 153 WASM_BRV(1, WASM_CALL_FUNCTION0(0))), | 121 WASM_BRV(1, WASM_CALL_FUNCTION0(0))), |
| 154 WASM_BRV(0, WASM_I8(55))))}; | 122 WASM_BRV(0, WASM_I8(55))))}; |
| 155 f->EmitCode(code, sizeof(code)); | 123 f->EmitCode(code, sizeof(code)); |
| 156 TestModule(&zone, builder, 55); | 124 TestRunWasmModuleHelper::TestModule(&zone, builder, 55); |
| 157 } | 125 } |
| 158 | 126 |
| 159 TEST(Run_WasmModule_Global) { | 127 TEST(Run_WasmModule_Global) { |
| 160 v8::base::AccountingAllocator allocator; | 128 v8::base::AccountingAllocator allocator; |
| 161 Zone zone(&allocator); | 129 Zone zone(&allocator); |
| 162 TestSignatures sigs; | 130 TestSignatures sigs; |
| 163 | 131 |
| 164 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 132 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 165 uint32_t global1 = builder->AddGlobal(kAstI32, 0); | 133 uint32_t global1 = builder->AddGlobal(kAstI32, 0); |
| 166 uint32_t global2 = builder->AddGlobal(kAstI32, 0); | 134 uint32_t global2 = builder->AddGlobal(kAstI32, 0); |
| 167 uint16_t f1_index = builder->AddFunction(); | 135 uint16_t f1_index = builder->AddFunction(); |
| 168 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); | 136 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); |
| 169 f->SetSignature(sigs.i_v()); | 137 f->SetSignature(sigs.i_v()); |
| 170 byte code1[] = { | 138 byte code1[] = { |
| 171 WASM_I32_ADD(WASM_GET_GLOBAL(global1), WASM_GET_GLOBAL(global2))}; | 139 WASM_I32_ADD(WASM_GET_GLOBAL(global1), WASM_GET_GLOBAL(global2))}; |
| 172 f->EmitCode(code1, sizeof(code1)); | 140 f->EmitCode(code1, sizeof(code1)); |
| 173 uint16_t f2_index = builder->AddFunction(); | 141 uint16_t f2_index = builder->AddFunction(); |
| 174 f = builder->FunctionAt(f2_index); | 142 f = builder->FunctionAt(f2_index); |
| 175 f->SetSignature(sigs.i_v()); | 143 f->SetSignature(sigs.i_v()); |
| 176 ExportAsMain(f); | 144 TestRunWasmModuleHelper::ExportAsMain(f); |
| 177 byte code2[] = {WASM_SET_GLOBAL(global1, WASM_I32V_1(56)), | 145 byte code2[] = {WASM_SET_GLOBAL(global1, WASM_I32V_1(56)), |
| 178 WASM_SET_GLOBAL(global2, WASM_I32V_1(41)), | 146 WASM_SET_GLOBAL(global2, WASM_I32V_1(41)), |
| 179 WASM_RETURN1(WASM_CALL_FUNCTION0(f1_index))}; | 147 WASM_RETURN1(WASM_CALL_FUNCTION0(f1_index))}; |
| 180 f->EmitCode(code2, sizeof(code2)); | 148 f->EmitCode(code2, sizeof(code2)); |
| 181 TestModule(&zone, builder, 97); | 149 TestRunWasmModuleHelper::TestModule(&zone, builder, 97); |
| 182 } | 150 } |
| 183 | 151 |
| 184 TEST(Run_WasmModule_Serialization) { | 152 TEST(Run_WasmModule_Serialization) { |
| 185 FLAG_expose_wasm = true; | 153 FLAG_expose_wasm = true; |
| 186 static const char* kFunctionName = "increment"; | 154 static const char* kFunctionName = "increment"; |
| 187 v8::base::AccountingAllocator allocator; | 155 v8::base::AccountingAllocator allocator; |
| 188 Zone zone(&allocator); | 156 Zone zone(&allocator); |
| 189 | 157 |
| 190 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 158 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 191 uint16_t f_index = builder->AddFunction(); | 159 uint16_t f_index = builder->AddFunction(); |
| 192 TestSignatures sigs; | 160 TestSignatures sigs; |
| 193 | 161 |
| 194 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 162 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
| 195 f->SetSignature(sigs.i_i()); | 163 f->SetSignature(sigs.i_i()); |
| 196 byte code[] = {WASM_GET_LOCAL(0), kExprI32Const, 1, kExprI32Add}; | 164 byte code[] = {WASM_GET_LOCAL(0), kExprI32Const, 1, kExprI32Add}; |
| 197 f->EmitCode(code, sizeof(code)); | 165 f->EmitCode(code, sizeof(code)); |
| 198 ExportAs(f, kFunctionName); | 166 TestRunWasmModuleHelper::ExportAs(f, kFunctionName); |
| 199 | 167 |
| 200 ZoneBuffer buffer(&zone); | 168 ZoneBuffer buffer(&zone); |
| 201 builder->WriteTo(buffer); | 169 builder->WriteTo(buffer); |
| 202 | 170 |
| 203 Isolate* isolate = CcTest::InitIsolateOnce(); | 171 Isolate* isolate = CcTest::InitIsolateOnce(); |
| 204 ErrorThrower thrower(isolate, ""); | 172 ErrorThrower thrower(isolate, ""); |
| 205 | 173 |
| 206 v8::WasmCompiledModule::SerializedModule data; | 174 v8::WasmCompiledModule::SerializedModule data; |
| 207 { | 175 { |
| 208 HandleScope scope(isolate); | 176 HandleScope scope(isolate); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 Handle<JSReceiver>::null(), | 217 Handle<JSReceiver>::null(), |
| 250 Handle<JSArrayBuffer>::null()) | 218 Handle<JSArrayBuffer>::null()) |
| 251 .ToHandleChecked(); | 219 .ToHandleChecked(); |
| 252 Handle<Object> params[1] = {Handle<Object>(Smi::FromInt(41), isolate)}; | 220 Handle<Object> params[1] = {Handle<Object>(Smi::FromInt(41), isolate)}; |
| 253 int32_t result = testing::CallFunction(isolate, instance, &thrower, | 221 int32_t result = testing::CallFunction(isolate, instance, &thrower, |
| 254 kFunctionName, 1, params); | 222 kFunctionName, 1, params); |
| 255 CHECK(result == 42); | 223 CHECK(result == 42); |
| 256 new_ctx->Exit(); | 224 new_ctx->Exit(); |
| 257 } | 225 } |
| 258 } | 226 } |
| OLD | NEW |