| 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" |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 byte code[] = { | 115 byte code[] = { |
| 116 WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(kDataSegmentDest0))}; | 116 WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(kDataSegmentDest0))}; |
| 117 f->EmitCode(code, sizeof(code)); | 117 f->EmitCode(code, sizeof(code)); |
| 118 byte data[] = {0xaa, 0xbb, 0xcc, 0xdd}; | 118 byte data[] = {0xaa, 0xbb, 0xcc, 0xdd}; |
| 119 builder->AddDataSegment(new (&zone) WasmDataSegmentEncoder( | 119 builder->AddDataSegment(new (&zone) WasmDataSegmentEncoder( |
| 120 &zone, data, sizeof(data), kDataSegmentDest0)); | 120 &zone, data, sizeof(data), kDataSegmentDest0)); |
| 121 WasmModuleWriter* writer = builder->Build(&zone); | 121 WasmModuleWriter* writer = builder->Build(&zone); |
| 122 TestModule(writer->WriteTo(&zone), 0xddccbbaa); | 122 TestModule(writer->WriteTo(&zone), 0xddccbbaa); |
| 123 } | 123 } |
| 124 | 124 |
| 125 | |
| 126 #if defined(__has_feature) | |
| 127 #if __has_feature(address_sanitizer) | |
| 128 #define V8_WITH_ASAN 1 | |
| 129 #endif | |
| 130 #endif | |
| 131 | |
| 132 | |
| 133 #if !defined(V8_WITH_ASAN) | |
| 134 // TODO(bradnelson): Figure out why this crashes under asan. | |
| 135 TEST(Run_WasmModule_CheckMemoryIsZero) { | 125 TEST(Run_WasmModule_CheckMemoryIsZero) { |
| 136 static const int kCheckSize = 16 * 1024; | 126 static const int kCheckSize = 16 * 1024; |
| 137 Zone zone; | 127 Zone zone; |
| 138 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 128 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 139 uint16_t f_index = builder->AddFunction(); | 129 uint16_t f_index = builder->AddFunction(); |
| 140 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 130 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
| 141 f->ReturnType(kAstI32); | 131 f->ReturnType(kAstI32); |
| 142 uint16_t localIndex = f->AddLocal(kAstI32); | 132 uint16_t localIndex = f->AddLocal(kAstI32); |
| 143 f->Exported(1); | 133 f->Exported(1); |
| 144 byte code[] = {WASM_BLOCK( | 134 byte code[] = {WASM_BLOCK( |
| 145 2, | 135 2, |
| 146 WASM_WHILE( | 136 WASM_WHILE( |
| 147 WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I32V_3(kCheckSize)), | 137 WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I32V_3(kCheckSize)), |
| 148 WASM_IF_ELSE( | 138 WASM_IF_ELSE( |
| 149 WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(localIndex)), | 139 WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(localIndex)), |
| 150 WASM_BRV(2, WASM_I8(-1)), WASM_INC_LOCAL_BY(localIndex, 4))), | 140 WASM_BRV(2, WASM_I8(-1)), WASM_INC_LOCAL_BY(localIndex, 4))), |
| 151 WASM_I8(11))}; | 141 WASM_I8(11))}; |
| 152 f->EmitCode(code, sizeof(code), nullptr, 0); | 142 f->EmitCode(code, sizeof(code), nullptr, 0); |
| 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 | |
| 157 | 146 |
| 158 | |
| 159 #if !defined(V8_WITH_ASAN) | |
| 160 // TODO(bradnelson): Figure out why this crashes under asan. | |
| 161 TEST(Run_WasmModule_CallMain_recursive) { | 147 TEST(Run_WasmModule_CallMain_recursive) { |
| 162 Zone zone; | 148 Zone zone; |
| 163 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 149 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 164 uint16_t f_index = builder->AddFunction(); | 150 uint16_t f_index = builder->AddFunction(); |
| 165 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 151 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
| 166 f->ReturnType(kAstI32); | 152 f->ReturnType(kAstI32); |
| 167 uint16_t localIndex = f->AddLocal(kAstI32); | 153 uint16_t localIndex = f->AddLocal(kAstI32); |
| 168 f->Exported(1); | 154 f->Exported(1); |
| 169 byte code[] = {WASM_BLOCK( | 155 byte code[] = {WASM_BLOCK( |
| 170 2, WASM_SET_LOCAL(localIndex, | 156 2, WASM_SET_LOCAL(localIndex, |
| 171 WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)), | 157 WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)), |
| 172 WASM_IF_ELSE(WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I8(5)), | 158 WASM_IF_ELSE(WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I8(5)), |
| 173 WASM_BLOCK(2, WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO, | 159 WASM_BLOCK(2, WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO, |
| 174 WASM_INC_LOCAL(localIndex)), | 160 WASM_INC_LOCAL(localIndex)), |
| 175 WASM_BRV(1, WASM_CALL_FUNCTION0(0))), | 161 WASM_BRV(1, WASM_CALL_FUNCTION0(0))), |
| 176 WASM_BRV(0, WASM_I8(55))))}; | 162 WASM_BRV(0, WASM_I8(55))))}; |
| 177 f->EmitCode(code, sizeof(code), nullptr, 0); | 163 f->EmitCode(code, sizeof(code), nullptr, 0); |
| 178 WasmModuleWriter* writer = builder->Build(&zone); | 164 WasmModuleWriter* writer = builder->Build(&zone); |
| 179 TestModule(writer->WriteTo(&zone), 55); | 165 TestModule(writer->WriteTo(&zone), 55); |
| 180 } | 166 } |
| 181 #endif | |
| 182 | 167 |
| 183 | |
| 184 #if !defined(V8_WITH_ASAN) | |
| 185 // TODO(bradnelson): Figure out why this crashes under asan. | |
| 186 TEST(Run_WasmModule_Global) { | 168 TEST(Run_WasmModule_Global) { |
| 187 Zone zone; | 169 Zone zone; |
| 188 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 170 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 189 uint32_t global1 = builder->AddGlobal(MachineType::Int32(), 0); | 171 uint32_t global1 = builder->AddGlobal(MachineType::Int32(), 0); |
| 190 uint32_t global2 = builder->AddGlobal(MachineType::Int32(), 0); | 172 uint32_t global2 = builder->AddGlobal(MachineType::Int32(), 0); |
| 191 uint16_t f1_index = builder->AddFunction(); | 173 uint16_t f1_index = builder->AddFunction(); |
| 192 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); | 174 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); |
| 193 f->ReturnType(kAstI32); | 175 f->ReturnType(kAstI32); |
| 194 byte code1[] = { | 176 byte code1[] = { |
| 195 WASM_I32_ADD(WASM_LOAD_GLOBAL(global1), WASM_LOAD_GLOBAL(global2))}; | 177 WASM_I32_ADD(WASM_LOAD_GLOBAL(global1), WASM_LOAD_GLOBAL(global2))}; |
| 196 f->EmitCode(code1, sizeof(code1)); | 178 f->EmitCode(code1, sizeof(code1)); |
| 197 uint16_t f2_index = builder->AddFunction(); | 179 uint16_t f2_index = builder->AddFunction(); |
| 198 f = builder->FunctionAt(f2_index); | 180 f = builder->FunctionAt(f2_index); |
| 199 f->ReturnType(kAstI32); | 181 f->ReturnType(kAstI32); |
| 200 f->Exported(1); | 182 f->Exported(1); |
| 201 byte code2[] = {WASM_STORE_GLOBAL(global1, WASM_I32V_1(56)), | 183 byte code2[] = {WASM_STORE_GLOBAL(global1, WASM_I32V_1(56)), |
| 202 WASM_STORE_GLOBAL(global2, WASM_I32V_1(41)), | 184 WASM_STORE_GLOBAL(global2, WASM_I32V_1(41)), |
| 203 WASM_RETURN(WASM_CALL_FUNCTION0(f1_index))}; | 185 WASM_RETURN(WASM_CALL_FUNCTION0(f1_index))}; |
| 204 f->EmitCode(code2, sizeof(code2)); | 186 f->EmitCode(code2, sizeof(code2)); |
| 205 WasmModuleWriter* writer = builder->Build(&zone); | 187 WasmModuleWriter* writer = builder->Build(&zone); |
| 206 TestModule(writer->WriteTo(&zone), 97); | 188 TestModule(writer->WriteTo(&zone), 97); |
| 207 } | 189 } |
| 208 #endif | |
| OLD | NEW |