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