1 // Copyright 2013 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 #include "test/cctest/cctest.h"
32
33 #include "src/macro-assembler.h"
34 #include "src/mips64/macro-assembler-mips64.h"
35 #include "src/mips64/simulator-mips64.h"
36
37
38 using namespace v8::internal;
39
40 typedef void* (*F)(int64_t x, int64_t y, int p2, int p3, int p4);
41
42 #define __ masm->
43
44
to_non_zero(int n)45 static byte to_non_zero(int n) {
46 return static_cast<unsigned>(n) % 255 + 1;
47 }
48
49
all_zeroes(const byte * beg,const byte * end)50 static bool all_zeroes(const byte* beg, const byte* end) {
51 CHECK(beg);
52 CHECK(beg <= end);
53 while (beg < end) {
54 if (*beg++ != 0)
55 return false;
56 }
57 return true;
58 }
59
60
TEST(CopyBytes)61 TEST(CopyBytes) {
62 CcTest::InitializeVM();
63 Isolate* isolate = Isolate::Current();
64 HandleScope handles(isolate);
65
66 const int data_size = 1 * KB;
67 size_t act_size;
68
69 // Allocate two blocks to copy data between.
70 byte* src_buffer =
71 static_cast<byte*>(v8::base::OS::Allocate(data_size, &act_size, 0));
72 CHECK(src_buffer);
73 CHECK(act_size >= static_cast<size_t>(data_size));
74 byte* dest_buffer =
75 static_cast<byte*>(v8::base::OS::Allocate(data_size, &act_size, 0));
76 CHECK(dest_buffer);
77 CHECK(act_size >= static_cast<size_t>(data_size));
78
79 // Storage for a0 and a1.
80 byte* a0_;
81 byte* a1_;
82
83 MacroAssembler assembler(isolate, NULL, 0);
84 MacroAssembler* masm = &assembler;
85
86 // Code to be generated: The stuff in CopyBytes followed by a store of a0 and
87 // a1, respectively.
88 __ CopyBytes(a0, a1, a2, a3);
89 __ li(a2, Operand(reinterpret_cast<int64_t>(&a0_)));
90 __ li(a3, Operand(reinterpret_cast<int64_t>(&a1_)));
91 __ sd(a0, MemOperand(a2));
92 __ jr(ra);
93 __ sd(a1, MemOperand(a3));
94
95 CodeDesc desc;
96 masm->GetCode(&desc);
97 Handle<Code> code = isolate->factory()->NewCode(
98 desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
99
100 ::F f = FUNCTION_CAST< ::F>(code->entry());
101
102 // Initialise source data with non-zero bytes.
103 for (int i = 0; i < data_size; i++) {
104 src_buffer[i] = to_non_zero(i);
105 }
106
107 const int fuzz = 11;
108
109 for (int size = 0; size < 600; size++) {
110 for (const byte* src = src_buffer; src < src_buffer + fuzz; src++) {
111 for (byte* dest = dest_buffer; dest < dest_buffer + fuzz; dest++) {
112 memset(dest_buffer, 0, data_size);
113 CHECK(dest + size < dest_buffer + data_size);
114 (void) CALL_GENERATED_CODE(f, reinterpret_cast<int64_t>(src),
115 reinterpret_cast<int64_t>(dest),
116 size, 0, 0);
117 // a0 and a1 should point at the first byte after the copied data.
118 CHECK_EQ(src + size, a0_);
119 CHECK_EQ(dest + size, a1_);
120 // Check that we haven't written outside the target area.
121 CHECK(all_zeroes(dest_buffer, dest));
122 CHECK(all_zeroes(dest + size, dest_buffer + data_size));
123 // Check the target area.
124 CHECK_EQ(0, memcmp(src, dest, size));
125 }
126 }
127 }
128
129 // Check that the source data hasn't been clobbered.
130 for (int i = 0; i < data_size; i++) {
131 CHECK(src_buffer[i] == to_non_zero(i));
132 }
133 }
134
135
TEST(LoadConstants)136 TEST(LoadConstants) {
137 CcTest::InitializeVM();
138 Isolate* isolate = Isolate::Current();
139 HandleScope handles(isolate);
140
141 int64_t refConstants[64];
142 int64_t result[64];
143
144 int64_t mask = 1;
145 for (int i = 0; i < 64; i++) {
146 refConstants[i] = ~(mask << i);
147 }
148
149 MacroAssembler assembler(isolate, NULL, 0);
150 MacroAssembler* masm = &assembler;
151
152 __ mov(a4, a0);
153 for (int i = 0; i < 64; i++) {
154 // Load constant.
155 __ li(a5, Operand(refConstants[i]));
156 __ sd(a5, MemOperand(a4));
157 __ Daddu(a4, a4, Operand(kPointerSize));
158 }
159
160 __ jr(ra);
161 __ nop();
162
163 CodeDesc desc;
164 masm->GetCode(&desc);
165 Handle<Code> code = isolate->factory()->NewCode(
166 desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
167
168 ::F f = FUNCTION_CAST< ::F>(code->entry());
169 (void) CALL_GENERATED_CODE(f, reinterpret_cast<int64_t>(result),
170 0, 0, 0, 0);
171 // Check results.
172 for (int i = 0; i < 64; i++) {
173 CHECK(refConstants[i] == result[i]);
174 }
175 }
176
177
TEST(LoadAddress)178 TEST(LoadAddress) {
179 CcTest::InitializeVM();
180 Isolate* isolate = Isolate::Current();
181 HandleScope handles(isolate);
182
183 MacroAssembler assembler(isolate, NULL, 0);
184 MacroAssembler* masm = &assembler;
185 Label to_jump, skip;
186 __ mov(a4, a0);
187
188 __ Branch(&skip);
189 __ bind(&to_jump);
190 __ nop();
191 __ nop();
192 __ jr(ra);
193 __ nop();
194 __ bind(&skip);
195 __ li(a4, Operand(masm->jump_address(&to_jump)), ADDRESS_LOAD);
196 int check_size = masm->InstructionsGeneratedSince(&skip);
197 CHECK_EQ(check_size, 4);
198 __ jr(a4);
199 __ nop();
200 __ stop("invalid");
201 __ stop("invalid");
202 __ stop("invalid");
203 __ stop("invalid");
204 __ stop("invalid");
205
206
207 CodeDesc desc;
208 masm->GetCode(&desc);
209 Handle<Code> code = isolate->factory()->NewCode(
210 desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
211
212 ::F f = FUNCTION_CAST< ::F>(code->entry());
213 (void) CALL_GENERATED_CODE(f, 0, 0, 0, 0, 0);
214 // Check results.
215 }
216
217 #undef __
218