| 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(static_cast<uint32_t>(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       // Dummy values of size used here as the objective of the test is to | 
|  | 87       // verify that the immediate is patched correctly | 
|  | 88       it.rinfo()->update_wasm_memory_reference( | 
|  | 89           it.rinfo()->wasm_memory_reference(), | 
|  | 90           it.rinfo()->wasm_memory_reference() + offset, 1, 2, | 
|  | 91           SKIP_ICACHE_FLUSH); | 
|  | 92     } | 
|  | 93   } | 
|  | 94 | 
|  | 95   // Check if immediate is updated correctly | 
|  | 96   ret_value = runnable.Call(); | 
|  | 97   printf("\nret_value: %lx", ret_value); | 
|  | 98   CHECK_EQ(ret_value, imm + offset); | 
|  | 99 | 
|  | 100 #ifdef OBJECT_PRINT | 
|  | 101   code->Print(os); | 
|  | 102   begin = code->instruction_start(); | 
|  | 103   end = begin + code->instruction_size(); | 
|  | 104   disasm::Disassembler::Disassemble(stdout, begin, end); | 
|  | 105 #endif | 
|  | 106 } | 
|  | 107 | 
|  | 108 TEST(WasmRelocationX64movq32) { | 
|  | 109   Isolate* isolate = CcTest::i_isolate(); | 
|  | 110   HandleScope scope(isolate); | 
|  | 111   v8::internal::byte buffer[4096]; | 
|  | 112   Assembler assm(isolate, buffer, sizeof buffer); | 
|  | 113   DummyStaticFunction(NULL);  // just bloody use it (DELETE; debugging) | 
|  | 114   uint32_t imm = 1234567; | 
|  | 115 | 
|  | 116   __ movq(rax, Immediate(static_cast<uint32_t>(imm), | 
|  | 117                          RelocInfo::WASM_MEMORY_REFERENCE)); | 
|  | 118   __ nop(); | 
|  | 119   __ ret(0); | 
|  | 120 | 
|  | 121   CodeDesc desc; | 
|  | 122   assm.GetCode(&desc); | 
|  | 123   Handle<Code> code = isolate->factory()->NewCode( | 
|  | 124       desc, Code::ComputeFlags(Code::STUB), Handle<Code>()); | 
|  | 125   USE(code); | 
|  | 126 | 
|  | 127   CSignature0<int64_t> csig; | 
|  | 128   CodeRunner<int64_t> runnable(isolate, code, &csig); | 
|  | 129   int64_t ret_value = runnable.Call(); | 
|  | 130   CHECK_EQ(ret_value, imm); | 
|  | 131 | 
|  | 132 #ifdef OBJECT_PRINT | 
|  | 133   OFStream os(stdout); | 
|  | 134   code->Print(os); | 
|  | 135   byte* begin = code->instruction_start(); | 
|  | 136   byte* end = begin + code->instruction_size(); | 
|  | 137   disasm::Disassembler::Disassemble(stdout, begin, end); | 
|  | 138 #endif | 
|  | 139   size_t offset = 1234; | 
|  | 140 | 
|  | 141   // Relocating references by offset | 
|  | 142   int mode_mask = (1 << RelocInfo::WASM_MEMORY_REFERENCE); | 
|  | 143   for (RelocIterator it(*code, mode_mask); !it.done(); it.next()) { | 
|  | 144     RelocInfo::Mode mode = it.rinfo()->rmode(); | 
|  | 145     if (RelocInfo::IsWasmMemoryReference(mode)) { | 
|  | 146       // Dummy values of size used here as the objective of the test is to | 
|  | 147       // verify that the immediate is patched correctly | 
|  | 148       it.rinfo()->update_wasm_memory_reference( | 
|  | 149           it.rinfo()->wasm_memory_reference(), | 
|  | 150           it.rinfo()->wasm_memory_reference() + offset, 1, 2, | 
|  | 151           SKIP_ICACHE_FLUSH); | 
|  | 152     } | 
|  | 153   } | 
|  | 154 | 
|  | 155   // Check if immediate is updated correctly | 
|  | 156   ret_value = runnable.Call(); | 
|  | 157   printf("\nret_value: %lx", ret_value); | 
|  | 158   CHECK_EQ(ret_value, imm + offset); | 
|  | 159 | 
|  | 160 #ifdef OBJECT_PRINT | 
|  | 161   code->Print(os); | 
|  | 162   begin = code->instruction_start(); | 
|  | 163   end = begin + code->instruction_size(); | 
|  | 164   disasm::Disassembler::Disassemble(stdout, begin, end); | 
|  | 165 #endif | 
|  | 166 } | 
|  | 167 | 
|  | 168 TEST(WasmRelocationX64movq64) { | 
|  | 169   CcTest::InitializeVM(); | 
|  | 170   Isolate* isolate = CcTest::i_isolate(); | 
|  | 171   HandleScope scope(isolate); | 
|  | 172   v8::internal::byte buffer[4096]; | 
|  | 173   Assembler assm(isolate, buffer, sizeof buffer); | 
|  | 174   DummyStaticFunction(NULL);  // just bloody use it (DELETE; debugging) | 
|  | 175   int64_t imm = 1234567; | 
|  | 176 | 
|  | 177   __ movq(rax, imm, RelocInfo::WASM_MEMORY_REFERENCE); | 
|  | 178   __ nop(); | 
|  | 179   __ ret(0); | 
|  | 180 | 
|  | 181   CodeDesc desc; | 
|  | 182   assm.GetCode(&desc); | 
|  | 183   Handle<Code> code = isolate->factory()->NewCode( | 
|  | 184       desc, Code::ComputeFlags(Code::STUB), Handle<Code>()); | 
|  | 185   USE(code); | 
|  | 186 | 
|  | 187   CSignature0<int64_t> csig; | 
|  | 188   CodeRunner<int64_t> runnable(isolate, code, &csig); | 
|  | 189   int64_t ret_value = runnable.Call(); | 
|  | 190   CHECK_EQ(ret_value, imm); | 
|  | 191 | 
|  | 192 #ifdef OBJECT_PRINT | 
|  | 193   OFStream os(stdout); | 
|  | 194   code->Print(os); | 
|  | 195   byte* begin = code->instruction_start(); | 
|  | 196   byte* end = begin + code->instruction_size(); | 
|  | 197   disasm::Disassembler::Disassemble(stdout, begin, end); | 
|  | 198 #endif | 
|  | 199   size_t offset = 1234; | 
|  | 200 | 
|  | 201   // Relocating references by offset | 
|  | 202   int mode_mask = (1 << RelocInfo::WASM_MEMORY_REFERENCE); | 
|  | 203   for (RelocIterator it(*code, mode_mask); !it.done(); it.next()) { | 
|  | 204     RelocInfo::Mode mode = it.rinfo()->rmode(); | 
|  | 205     if (RelocInfo::IsWasmMemoryReference(mode)) { | 
|  | 206       // Dummy values of size used here as the objective of the test is to | 
|  | 207       // verify that the immediate is patched correctly | 
|  | 208       it.rinfo()->update_wasm_memory_reference( | 
|  | 209           it.rinfo()->wasm_memory_reference(), | 
|  | 210           it.rinfo()->wasm_memory_reference() + offset, 1, 2, | 
|  | 211           SKIP_ICACHE_FLUSH); | 
|  | 212     } | 
|  | 213   } | 
|  | 214 | 
|  | 215   // Check if immediate is updated correctly | 
|  | 216   ret_value = runnable.Call(); | 
|  | 217   printf("\nret_value: %lx", ret_value); | 
|  | 218   CHECK_EQ(ret_value, imm + offset); | 
|  | 219 | 
|  | 220 #ifdef OBJECT_PRINT | 
|  | 221   code->Print(os); | 
|  | 222   begin = code->instruction_start(); | 
|  | 223   end = begin + code->instruction_size(); | 
|  | 224   disasm::Disassembler::Disassemble(stdout, begin, end); | 
|  | 225 #endif | 
|  | 226 } | 
|  | 227 | 
|  | 228 #undef __ | 
| OLD | NEW | 
|---|