| 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-macro-gen.h" | 9 #include "src/wasm/wasm-macro-gen.h" |
| 10 #include "src/wasm/wasm-module.h" | 10 #include "src/wasm/wasm-module.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 namespace { | 21 namespace { |
| 22 void TestModule(WasmModuleIndex* module, int32_t expected_result) { | 22 void TestModule(WasmModuleIndex* module, int32_t expected_result) { |
| 23 Isolate* isolate = CcTest::InitIsolateOnce(); | 23 Isolate* isolate = CcTest::InitIsolateOnce(); |
| 24 int32_t result = | 24 int32_t result = |
| 25 CompileAndRunWasmModule(isolate, module->Begin(), module->End()); | 25 CompileAndRunWasmModule(isolate, module->Begin(), module->End()); |
| 26 CHECK_EQ(expected_result, result); | 26 CHECK_EQ(expected_result, result); |
| 27 } | 27 } |
| 28 } // namespace | 28 } // namespace |
| 29 | 29 |
| 30 | 30 |
| 31 // TODO(tizer): Figure out why this crashes with PPC. | |
| 32 #if !defined(V8_TARGET_ARCH_PPC) && !defined(V8_TARGET_ARCH_PPC64) | |
| 33 | |
| 34 // A raw test that skips the WasmModuleBuilder. | 31 // A raw test that skips the WasmModuleBuilder. |
| 35 TEST(Run_WasmModule_CallAdd_rev) { | 32 TEST(Run_WasmModule_CallAdd_rev) { |
| 36 static const byte data[] = { | 33 static const byte data[] = { |
| 37 // sig#0 ------------------------------------------ | 34 // sig#0 ------------------------------------------ |
| 38 kDeclSignatures, 2, 0, kLocalI32, // void -> int | 35 kDeclSignatures, 2, 0, kLocalI32, // void -> int |
| 39 2, kLocalI32, kLocalI32, kLocalI32, // int,int -> int | 36 2, kLocalI32, kLocalI32, kLocalI32, // int,int -> int |
| 40 // func#0 (main) ---------------------------------- | 37 // func#0 (main) ---------------------------------- |
| 41 kDeclFunctions, 2, kDeclFunctionExport, 0, 0, // sig index | 38 kDeclFunctions, 2, kDeclFunctionExport, 0, 0, // sig index |
| 42 6, 0, // body size | 39 6, 0, // body size |
| 43 kExprCallFunction, 1, // -- | 40 kExprCallFunction, 1, // -- |
| 44 kExprI8Const, 77, // -- | 41 kExprI8Const, 77, // -- |
| 45 kExprI8Const, 22, // -- | 42 kExprI8Const, 22, // -- |
| 46 // func#1 ----------------------------------------- | 43 // func#1 ----------------------------------------- |
| 47 0, // no name, not exported | 44 0, // no name, not exported |
| 48 1, 0, // sig index | 45 1, 0, // sig index |
| 49 5, 0, // body size | 46 5, 0, // body size |
| 50 kExprI32Add, // -- | 47 kExprI32Add, // -- |
| 51 kExprGetLocal, 0, // -- | 48 kExprGetLocal, 0, // -- |
| 52 kExprGetLocal, 1, // -- | 49 kExprGetLocal, 1, // -- |
| 53 }; | 50 }; |
| 54 | 51 |
| 55 Isolate* isolate = CcTest::InitIsolateOnce(); | 52 Isolate* isolate = CcTest::InitIsolateOnce(); |
| 56 int32_t result = | 53 int32_t result = |
| 57 CompileAndRunWasmModule(isolate, data, data + arraysize(data)); | 54 CompileAndRunWasmModule(isolate, data, data + arraysize(data)); |
| 58 CHECK_EQ(99, result); | 55 CHECK_EQ(99, result); |
| 59 } | 56 } |
| 60 | 57 |
| 61 #endif | |
| 62 | |
| 63 | 58 |
| 64 TEST(Run_WasmModule_Return114) { | 59 TEST(Run_WasmModule_Return114) { |
| 65 static const int32_t kReturnValue = 114; | 60 static const int32_t kReturnValue = 114; |
| 66 Zone zone; | 61 Zone zone; |
| 67 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 62 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 68 uint16_t f_index = builder->AddFunction(); | 63 uint16_t f_index = builder->AddFunction(); |
| 69 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 64 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
| 70 f->ReturnType(kAstI32); | 65 f->ReturnType(kAstI32); |
| 71 f->Exported(1); | 66 f->Exported(1); |
| 72 byte code[] = {WASM_I8(kReturnValue)}; | 67 byte code[] = {WASM_I8(kReturnValue)}; |
| 73 f->EmitCode(code, sizeof(code)); | 68 f->EmitCode(code, sizeof(code)); |
| 74 WasmModuleWriter* writer = builder->Build(&zone); | 69 WasmModuleWriter* writer = builder->Build(&zone); |
| 75 TestModule(writer->WriteTo(&zone), kReturnValue); | 70 TestModule(writer->WriteTo(&zone), kReturnValue); |
| 76 } | 71 } |
| 77 | 72 |
| 78 | 73 |
| 79 // TODO(tizer): Figure out why this crashes with PPC. | |
| 80 #if !defined(V8_TARGET_ARCH_PPC) && !defined(V8_TARGET_ARCH_PPC64) | |
| 81 | |
| 82 TEST(Run_WasmModule_CallAdd) { | 74 TEST(Run_WasmModule_CallAdd) { |
| 83 Zone zone; | 75 Zone zone; |
| 84 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 76 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 85 uint16_t f1_index = builder->AddFunction(); | 77 uint16_t f1_index = builder->AddFunction(); |
| 86 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); | 78 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); |
| 87 f->ReturnType(kAstI32); | 79 f->ReturnType(kAstI32); |
| 88 uint16_t param1 = f->AddParam(kAstI32); | 80 uint16_t param1 = f->AddParam(kAstI32); |
| 89 uint16_t param2 = f->AddParam(kAstI32); | 81 uint16_t param2 = f->AddParam(kAstI32); |
| 90 byte code1[] = {WASM_I32_ADD(WASM_GET_LOCAL(param1), WASM_GET_LOCAL(param2))}; | 82 byte code1[] = {WASM_I32_ADD(WASM_GET_LOCAL(param1), WASM_GET_LOCAL(param2))}; |
| 91 uint32_t local_indices1[] = {2, 4}; | 83 uint32_t local_indices1[] = {2, 4}; |
| 92 f->EmitCode(code1, sizeof(code1), local_indices1, sizeof(local_indices1) / 4); | 84 f->EmitCode(code1, sizeof(code1), local_indices1, sizeof(local_indices1) / 4); |
| 93 uint16_t f2_index = builder->AddFunction(); | 85 uint16_t f2_index = builder->AddFunction(); |
| 94 f = builder->FunctionAt(f2_index); | 86 f = builder->FunctionAt(f2_index); |
| 95 f->ReturnType(kAstI32); | 87 f->ReturnType(kAstI32); |
| 96 f->Exported(1); | 88 f->Exported(1); |
| 97 byte code2[] = {WASM_CALL_FUNCTION(f1_index, WASM_I8(77), WASM_I8(22))}; | 89 byte code2[] = {WASM_CALL_FUNCTION(f1_index, WASM_I8(77), WASM_I8(22))}; |
| 98 f->EmitCode(code2, sizeof(code2)); | 90 f->EmitCode(code2, sizeof(code2)); |
| 99 WasmModuleWriter* writer = builder->Build(&zone); | 91 WasmModuleWriter* writer = builder->Build(&zone); |
| 100 TestModule(writer->WriteTo(&zone), 99); | 92 TestModule(writer->WriteTo(&zone), 99); |
| 101 } | 93 } |
| 102 | 94 |
| 103 #endif | |
| 104 | |
| 105 | 95 |
| 106 TEST(Run_WasmModule_ReadLoadedDataSegment) { | 96 TEST(Run_WasmModule_ReadLoadedDataSegment) { |
| 107 static const byte kDataSegmentDest0 = 12; | 97 static const byte kDataSegmentDest0 = 12; |
| 108 Zone zone; | 98 Zone zone; |
| 109 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 99 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 110 uint16_t f_index = builder->AddFunction(); | 100 uint16_t f_index = builder->AddFunction(); |
| 111 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 101 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
| 112 f->ReturnType(kAstI32); | 102 f->ReturnType(kAstI32); |
| 113 f->Exported(1); | 103 f->Exported(1); |
| 114 byte code[] = { | 104 byte code[] = { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 WASM_BRV(2, WASM_I8(-1)), WASM_INC_LOCAL_BY(localIndex, 4))), | 139 WASM_BRV(2, WASM_I8(-1)), WASM_INC_LOCAL_BY(localIndex, 4))), |
| 150 WASM_I8(11))}; | 140 WASM_I8(11))}; |
| 151 uint32_t local_indices[] = {7, 19, 25, 28}; | 141 uint32_t local_indices[] = {7, 19, 25, 28}; |
| 152 f->EmitCode(code, sizeof(code), local_indices, sizeof(local_indices) / 4); | 142 f->EmitCode(code, sizeof(code), local_indices, sizeof(local_indices) / 4); |
| 153 WasmModuleWriter* writer = builder->Build(&zone); | 143 WasmModuleWriter* writer = builder->Build(&zone); |
| 154 TestModule(writer->WriteTo(&zone), 11); | 144 TestModule(writer->WriteTo(&zone), 11); |
| 155 } | 145 } |
| 156 #endif | 146 #endif |
| 157 | 147 |
| 158 | 148 |
| 159 #if !defined(V8_WITH_ASAN) && !defined(V8_TARGET_ARCH_PPC) && \ | 149 #if !defined(V8_WITH_ASAN) |
| 160 !defined(V8_TARGET_ARCH_PPC64) | |
| 161 // TODO(tizer): Figure out why this crashes with PPC. | |
| 162 // TODO(bradnelson): Figure out why this crashes under asan. | 150 // TODO(bradnelson): Figure out why this crashes under asan. |
| 163 TEST(Run_WasmModule_CallMain_recursive) { | 151 TEST(Run_WasmModule_CallMain_recursive) { |
| 164 Zone zone; | 152 Zone zone; |
| 165 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 153 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 166 uint16_t f_index = builder->AddFunction(); | 154 uint16_t f_index = builder->AddFunction(); |
| 167 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 155 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
| 168 f->ReturnType(kAstI32); | 156 f->ReturnType(kAstI32); |
| 169 uint16_t localIndex = f->AddLocal(kAstI32); | 157 uint16_t localIndex = f->AddLocal(kAstI32); |
| 170 f->Exported(1); | 158 f->Exported(1); |
| 171 byte code[] = {WASM_BLOCK( | 159 byte code[] = {WASM_BLOCK( |
| 172 2, WASM_SET_LOCAL(localIndex, | 160 2, WASM_SET_LOCAL(localIndex, |
| 173 WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)), | 161 WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)), |
| 174 WASM_IF_ELSE(WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I8(5)), | 162 WASM_IF_ELSE(WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I8(5)), |
| 175 WASM_BLOCK(2, WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO, | 163 WASM_BLOCK(2, WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO, |
| 176 WASM_INC_LOCAL(localIndex)), | 164 WASM_INC_LOCAL(localIndex)), |
| 177 WASM_BRV(1, WASM_CALL_FUNCTION0(0))), | 165 WASM_BRV(1, WASM_CALL_FUNCTION0(0))), |
| 178 WASM_BRV(0, WASM_I8(55))))}; | 166 WASM_BRV(0, WASM_I8(55))))}; |
| 179 uint32_t local_indices[] = {3, 11, 21, 24}; | 167 uint32_t local_indices[] = {3, 11, 21, 24}; |
| 180 f->EmitCode(code, sizeof(code), local_indices, sizeof(local_indices) / 4); | 168 f->EmitCode(code, sizeof(code), local_indices, sizeof(local_indices) / 4); |
| 181 WasmModuleWriter* writer = builder->Build(&zone); | 169 WasmModuleWriter* writer = builder->Build(&zone); |
| 182 TestModule(writer->WriteTo(&zone), 55); | 170 TestModule(writer->WriteTo(&zone), 55); |
| 183 } | 171 } |
| 184 #endif | 172 #endif |
| 185 | 173 |
| 186 | 174 |
| 187 #if !defined(V8_WITH_ASAN) && !defined(V8_TARGET_ARCH_PPC) && \ | 175 #if !defined(V8_WITH_ASAN) |
| 188 !defined(V8_TARGET_ARCH_PPC64) | |
| 189 // TODO(tizer): Figure out why this fails with PPC. | |
| 190 // TODO(bradnelson): Figure out why this crashes under asan. | 176 // TODO(bradnelson): Figure out why this crashes under asan. |
| 191 TEST(Run_WasmModule_Global) { | 177 TEST(Run_WasmModule_Global) { |
| 192 Zone zone; | 178 Zone zone; |
| 193 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 179 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 194 uint32_t global1 = builder->AddGlobal(MachineType::Int32(), 0); | 180 uint32_t global1 = builder->AddGlobal(MachineType::Int32(), 0); |
| 195 uint32_t global2 = builder->AddGlobal(MachineType::Int32(), 0); | 181 uint32_t global2 = builder->AddGlobal(MachineType::Int32(), 0); |
| 196 uint16_t f1_index = builder->AddFunction(); | 182 uint16_t f1_index = builder->AddFunction(); |
| 197 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); | 183 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); |
| 198 f->ReturnType(kAstI32); | 184 f->ReturnType(kAstI32); |
| 199 byte code1[] = { | 185 byte code1[] = { |
| 200 WASM_I32_ADD(WASM_LOAD_GLOBAL(global1), WASM_LOAD_GLOBAL(global2))}; | 186 WASM_I32_ADD(WASM_LOAD_GLOBAL(global1), WASM_LOAD_GLOBAL(global2))}; |
| 201 f->EmitCode(code1, sizeof(code1)); | 187 f->EmitCode(code1, sizeof(code1)); |
| 202 uint16_t f2_index = builder->AddFunction(); | 188 uint16_t f2_index = builder->AddFunction(); |
| 203 f = builder->FunctionAt(f2_index); | 189 f = builder->FunctionAt(f2_index); |
| 204 f->ReturnType(kAstI32); | 190 f->ReturnType(kAstI32); |
| 205 f->Exported(1); | 191 f->Exported(1); |
| 206 byte code2[] = {WASM_STORE_GLOBAL(global1, WASM_I32(56)), | 192 byte code2[] = {WASM_STORE_GLOBAL(global1, WASM_I32(56)), |
| 207 WASM_STORE_GLOBAL(global2, WASM_I32(41)), | 193 WASM_STORE_GLOBAL(global2, WASM_I32(41)), |
| 208 WASM_RETURN(WASM_CALL_FUNCTION0(f1_index))}; | 194 WASM_RETURN(WASM_CALL_FUNCTION0(f1_index))}; |
| 209 f->EmitCode(code2, sizeof(code2)); | 195 f->EmitCode(code2, sizeof(code2)); |
| 210 WasmModuleWriter* writer = builder->Build(&zone); | 196 WasmModuleWriter* writer = builder->Build(&zone); |
| 211 TestModule(writer->WriteTo(&zone), 97); | 197 TestModule(writer->WriteTo(&zone), 97); |
| 212 } | 198 } |
| 213 #endif | 199 #endif |
| OLD | NEW |