OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 #include <stdlib.h> |
| 29 |
| 30 #include "src/v8.h" |
| 31 |
| 32 #include "src/debug/debug.h" |
| 33 #include "src/disasm.h" |
| 34 #include "src/disassembler.h" |
| 35 #include "src/ic/ic.h" |
| 36 #include "src/macro-assembler.h" |
| 37 #include "test/cctest/cctest.h" |
| 38 #include "test/cctest/compiler/c-signature.h" |
| 39 #include "test/cctest/compiler/call-tester.h" |
| 40 |
| 41 using namespace v8::internal; |
| 42 using namespace v8::internal::compiler; |
| 43 |
| 44 #define __ assm. |
| 45 |
| 46 static int32_t DummyStaticFunction(Object* result) { return 1; } |
| 47 |
| 48 TEST(WasmRelocationX64movl) { |
| 49 Isolate* isolate = CcTest::i_isolate(); |
| 50 HandleScope scope(isolate); |
| 51 v8::internal::byte buffer[4096]; |
| 52 Assembler assm(isolate, buffer, sizeof buffer); |
| 53 DummyStaticFunction(NULL); // just bloody use it (DELETE; debugging) |
| 54 int32_t imm = 1234567; |
| 55 |
| 56 __ movl(rax, Immediate(reinterpret_cast<Address>(imm), |
| 57 RelocInfo::WASM_MEMORY_REFERENCE)); |
| 58 __ nop(); |
| 59 __ ret(0); |
| 60 |
| 61 CodeDesc desc; |
| 62 assm.GetCode(&desc); |
| 63 Handle<Code> code = isolate->factory()->NewCode( |
| 64 desc, Code::ComputeFlags(Code::STUB), Handle<Code>()); |
| 65 USE(code); |
| 66 |
| 67 CSignature0<int64_t> csig; |
| 68 CodeRunner<int64_t> runnable(isolate, code, &csig); |
| 69 int64_t ret_value = runnable.Call(); |
| 70 CHECK_EQ(ret_value, imm); |
| 71 |
| 72 #ifdef OBJECT_PRINT |
| 73 OFStream os(stdout); |
| 74 code->Print(os); |
| 75 byte* begin = code->instruction_start(); |
| 76 byte* end = begin + code->instruction_size(); |
| 77 disasm::Disassembler::Disassemble(stdout, begin, end); |
| 78 #endif |
| 79 size_t offset = 1234; |
| 80 |
| 81 // Relocating references by offset |
| 82 int mode_mask = (1 << RelocInfo::WASM_MEMORY_REFERENCE); |
| 83 for (RelocIterator it(*code, mode_mask); !it.done(); it.next()) { |
| 84 RelocInfo::Mode mode = it.rinfo()->rmode(); |
| 85 if (RelocInfo::IsWasmMemoryReference(mode)) { |
| 86 it.rinfo()->update_wasm_memory_reference( |
| 87 it.rinfo()->wasm_memory_reference() + offset, SKIP_ICACHE_FLUSH); |
| 88 } |
| 89 } |
| 90 |
| 91 // Check if immediate is updated correctly |
| 92 ret_value = runnable.Call(); |
| 93 printf("\nret_value: %lx", ret_value); |
| 94 CHECK_EQ(ret_value, imm + offset); |
| 95 |
| 96 #ifdef OBJECT_PRINT |
| 97 code->Print(os); |
| 98 begin = code->instruction_start(); |
| 99 end = begin + code->instruction_size(); |
| 100 disasm::Disassembler::Disassemble(stdout, begin, end); |
| 101 #endif |
| 102 } |
| 103 |
| 104 TEST(WasmRelocationX64movq32) { |
| 105 Isolate* isolate = CcTest::i_isolate(); |
| 106 HandleScope scope(isolate); |
| 107 v8::internal::byte buffer[4096]; |
| 108 Assembler assm(isolate, buffer, sizeof buffer); |
| 109 DummyStaticFunction(NULL); // just bloody use it (DELETE; debugging) |
| 110 uint32_t imm = 1234567; |
| 111 |
| 112 __ movq(rax, Immediate(reinterpret_cast<Address>(imm), |
| 113 RelocInfo::WASM_MEMORY_REFERENCE)); |
| 114 __ nop(); |
| 115 __ ret(0); |
| 116 |
| 117 CodeDesc desc; |
| 118 assm.GetCode(&desc); |
| 119 Handle<Code> code = isolate->factory()->NewCode( |
| 120 desc, Code::ComputeFlags(Code::STUB), Handle<Code>()); |
| 121 USE(code); |
| 122 |
| 123 CSignature0<int64_t> csig; |
| 124 CodeRunner<int64_t> runnable(isolate, code, &csig); |
| 125 int64_t ret_value = runnable.Call(); |
| 126 CHECK_EQ(ret_value, imm); |
| 127 |
| 128 #ifdef OBJECT_PRINT |
| 129 OFStream os(stdout); |
| 130 code->Print(os); |
| 131 byte* begin = code->instruction_start(); |
| 132 byte* end = begin + code->instruction_size(); |
| 133 disasm::Disassembler::Disassemble(stdout, begin, end); |
| 134 #endif |
| 135 size_t offset = 1234; |
| 136 |
| 137 // Relocating references by offset |
| 138 int mode_mask = (1 << RelocInfo::WASM_MEMORY_REFERENCE); |
| 139 for (RelocIterator it(*code, mode_mask); !it.done(); it.next()) { |
| 140 RelocInfo::Mode mode = it.rinfo()->rmode(); |
| 141 if (RelocInfo::IsWasmMemoryReference(mode)) { |
| 142 it.rinfo()->update_wasm_memory_reference( |
| 143 it.rinfo()->wasm_memory_reference() + offset, SKIP_ICACHE_FLUSH); |
| 144 } |
| 145 } |
| 146 |
| 147 // Check if immediate is updated correctly |
| 148 ret_value = runnable.Call(); |
| 149 printf("\nret_value: %lx", ret_value); |
| 150 CHECK_EQ(ret_value, imm + offset); |
| 151 |
| 152 #ifdef OBJECT_PRINT |
| 153 code->Print(os); |
| 154 begin = code->instruction_start(); |
| 155 end = begin + code->instruction_size(); |
| 156 disasm::Disassembler::Disassemble(stdout, begin, end); |
| 157 #endif |
| 158 } |
| 159 |
| 160 TEST(WasmRelocationX64movq64) { |
| 161 CcTest::InitializeVM(); |
| 162 Isolate* isolate = CcTest::i_isolate(); |
| 163 HandleScope scope(isolate); |
| 164 v8::internal::byte buffer[4096]; |
| 165 Assembler assm(isolate, buffer, sizeof buffer); |
| 166 DummyStaticFunction(NULL); // just bloody use it (DELETE; debugging) |
| 167 int64_t imm = 1234567; |
| 168 |
| 169 __ movq(rax, imm, RelocInfo::WASM_MEMORY_REFERENCE); |
| 170 __ nop(); |
| 171 __ ret(0); |
| 172 |
| 173 CodeDesc desc; |
| 174 assm.GetCode(&desc); |
| 175 Handle<Code> code = isolate->factory()->NewCode( |
| 176 desc, Code::ComputeFlags(Code::STUB), Handle<Code>()); |
| 177 USE(code); |
| 178 |
| 179 CSignature0<int64_t> csig; |
| 180 CodeRunner<int64_t> runnable(isolate, code, &csig); |
| 181 int64_t ret_value = runnable.Call(); |
| 182 CHECK_EQ(ret_value, imm); |
| 183 |
| 184 #ifdef OBJECT_PRINT |
| 185 OFStream os(stdout); |
| 186 code->Print(os); |
| 187 byte* begin = code->instruction_start(); |
| 188 byte* end = begin + code->instruction_size(); |
| 189 disasm::Disassembler::Disassemble(stdout, begin, end); |
| 190 #endif |
| 191 size_t offset = 1234; |
| 192 |
| 193 // Relocating references by offset |
| 194 int mode_mask = (1 << RelocInfo::WASM_MEMORY_REFERENCE); |
| 195 for (RelocIterator it(*code, mode_mask); !it.done(); it.next()) { |
| 196 RelocInfo::Mode mode = it.rinfo()->rmode(); |
| 197 if (RelocInfo::IsWasmMemoryReference(mode)) { |
| 198 it.rinfo()->update_wasm_memory_reference( |
| 199 it.rinfo()->wasm_memory_reference() + offset, SKIP_ICACHE_FLUSH); |
| 200 } |
| 201 } |
| 202 |
| 203 // Check if immediate is updated correctly |
| 204 ret_value = runnable.Call(); |
| 205 printf("\nret_value: %lx", ret_value); |
| 206 CHECK_EQ(ret_value, imm + offset); |
| 207 |
| 208 #ifdef OBJECT_PRINT |
| 209 code->Print(os); |
| 210 begin = code->instruction_start(); |
| 211 end = begin + code->instruction_size(); |
| 212 disasm::Disassembler::Disassemble(stdout, begin, end); |
| 213 #endif |
| 214 } |
| 215 |
| 216 #undef __ |
OLD | NEW |