blob: 24ab60e972e1b7718c4d12abd23504f3bc38ed6a [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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
35#include "src/arm/macro-assembler-arm.h"
36#include "src/arm/simulator-arm.h"
37
38
39using namespace v8::internal;
40
41typedef void* (*F)(int x, int y, int p2, int p3, int p4);
42
43#define __ masm->
44
45
46static byte to_non_zero(int n) {
47 return static_cast<unsigned>(n) % 255 + 1;
48}
49
50
51static bool all_zeroes(const byte* beg, const byte* end) {
52 CHECK(beg);
53 CHECK(beg <= end);
54 while (beg < end) {
55 if (*beg++ != 0)
56 return false;
57 }
58 return true;
59}
60
61
62TEST(CopyBytes) {
63 CcTest::InitializeVM();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000064 Isolate* isolate = CcTest::i_isolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000065 HandleScope handles(isolate);
66
67 const int data_size = 1 * KB;
68 size_t act_size;
69
70 // Allocate two blocks to copy data between.
71 byte* src_buffer =
72 static_cast<byte*>(v8::base::OS::Allocate(data_size, &act_size, 0));
73 CHECK(src_buffer);
74 CHECK(act_size >= static_cast<size_t>(data_size));
75 byte* dest_buffer =
76 static_cast<byte*>(v8::base::OS::Allocate(data_size, &act_size, 0));
77 CHECK(dest_buffer);
78 CHECK(act_size >= static_cast<size_t>(data_size));
79
80 // Storage for R0 and R1.
81 byte* r0_;
82 byte* r1_;
83
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000084 MacroAssembler assembler(isolate, NULL, 0,
85 v8::internal::CodeObjectRequired::kYes);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000086 MacroAssembler* masm = &assembler;
87
88 // Code to be generated: The stuff in CopyBytes followed by a store of R0 and
89 // R1, respectively.
90 __ CopyBytes(r0, r1, r2, r3);
91 __ mov(r2, Operand(reinterpret_cast<int>(&r0_)));
92 __ mov(r3, Operand(reinterpret_cast<int>(&r1_)));
93 __ str(r0, MemOperand(r2));
94 __ str(r1, MemOperand(r3));
95 __ bx(lr);
96
97 CodeDesc desc;
98 masm->GetCode(&desc);
99 Handle<Code> code = isolate->factory()->NewCode(
100 desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
101
102 F f = FUNCTION_CAST<F>(code->entry());
103
104 // Initialise source data with non-zero bytes.
105 for (int i = 0; i < data_size; i++) {
106 src_buffer[i] = to_non_zero(i);
107 }
108
109 const int fuzz = 11;
110
111 for (int size = 0; size < 600; size++) {
112 for (const byte* src = src_buffer; src < src_buffer + fuzz; src++) {
113 for (byte* dest = dest_buffer; dest < dest_buffer + fuzz; dest++) {
114 memset(dest_buffer, 0, data_size);
115 CHECK(dest + size < dest_buffer + data_size);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000116 (void)CALL_GENERATED_CODE(isolate, f, reinterpret_cast<int>(src),
117 reinterpret_cast<int>(dest), size, 0, 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000118 // R0 and R1 should point at the first byte after the copied data.
119 CHECK_EQ(src + size, r0_);
120 CHECK_EQ(dest + size, r1_);
121 // Check that we haven't written outside the target area.
122 CHECK(all_zeroes(dest_buffer, dest));
123 CHECK(all_zeroes(dest + size, dest_buffer + data_size));
124 // Check the target area.
125 CHECK_EQ(0, memcmp(src, dest, size));
126 }
127 }
128 }
129
130 // Check that the source data hasn't been clobbered.
131 for (int i = 0; i < data_size; i++) {
132 CHECK(src_buffer[i] == to_non_zero(i));
133 }
134}
135
136
137typedef int (*F5)(void*, void*, void*, void*, void*);
138
139
140TEST(LoadAndStoreWithRepresentation) {
141 // Allocate an executable page of memory.
142 size_t actual_size;
143 byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
144 Assembler::kMinimalBufferSize, &actual_size, true));
145 CHECK(buffer);
146 Isolate* isolate = CcTest::i_isolate();
147 HandleScope handles(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000148 MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
149 v8::internal::CodeObjectRequired::kYes);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000150 MacroAssembler* masm = &assembler; // Create a pointer for the __ macro.
151 __ sub(sp, sp, Operand(1 * kPointerSize));
152 Label exit;
153
154 // Test 1.
155 __ mov(r0, Operand(1)); // Test number.
156 __ mov(r1, Operand(0));
157 __ str(r1, MemOperand(sp, 0 * kPointerSize));
158 __ mov(r2, Operand(-1));
159 __ Store(r2, MemOperand(sp, 0 * kPointerSize), Representation::UInteger8());
160 __ ldr(r3, MemOperand(sp, 0 * kPointerSize));
161 __ mov(r2, Operand(255));
162 __ cmp(r3, r2);
163 __ b(ne, &exit);
164 __ mov(r2, Operand(255));
165 __ Load(r3, MemOperand(sp, 0 * kPointerSize), Representation::UInteger8());
166 __ cmp(r3, r2);
167 __ b(ne, &exit);
168
169 // Test 2.
170 __ mov(r0, Operand(2)); // Test number.
171 __ mov(r1, Operand(0));
172 __ str(r1, MemOperand(sp, 0 * kPointerSize));
173 __ mov(r2, Operand(-1));
174 __ Store(r2, MemOperand(sp, 0 * kPointerSize), Representation::Integer8());
175 __ ldr(r3, MemOperand(sp, 0 * kPointerSize));
176 __ mov(r2, Operand(255));
177 __ cmp(r3, r2);
178 __ b(ne, &exit);
179 __ mov(r2, Operand(-1));
180 __ Load(r3, MemOperand(sp, 0 * kPointerSize), Representation::Integer8());
181 __ cmp(r3, r2);
182 __ b(ne, &exit);
183
184 // Test 3.
185 __ mov(r0, Operand(3)); // Test number.
186 __ mov(r1, Operand(0));
187 __ str(r1, MemOperand(sp, 0 * kPointerSize));
188 __ mov(r2, Operand(-1));
189 __ Store(r2, MemOperand(sp, 0 * kPointerSize), Representation::UInteger16());
190 __ ldr(r3, MemOperand(sp, 0 * kPointerSize));
191 __ mov(r2, Operand(65535));
192 __ cmp(r3, r2);
193 __ b(ne, &exit);
194 __ mov(r2, Operand(65535));
195 __ Load(r3, MemOperand(sp, 0 * kPointerSize), Representation::UInteger16());
196 __ cmp(r3, r2);
197 __ b(ne, &exit);
198
199 // Test 4.
200 __ mov(r0, Operand(4)); // Test number.
201 __ mov(r1, Operand(0));
202 __ str(r1, MemOperand(sp, 0 * kPointerSize));
203 __ mov(r2, Operand(-1));
204 __ Store(r2, MemOperand(sp, 0 * kPointerSize), Representation::Integer16());
205 __ ldr(r3, MemOperand(sp, 0 * kPointerSize));
206 __ mov(r2, Operand(65535));
207 __ cmp(r3, r2);
208 __ b(ne, &exit);
209 __ mov(r2, Operand(-1));
210 __ Load(r3, MemOperand(sp, 0 * kPointerSize), Representation::Integer16());
211 __ cmp(r3, r2);
212 __ b(ne, &exit);
213
214 __ mov(r0, Operand(0)); // Success.
215 __ bind(&exit);
216 __ add(sp, sp, Operand(1 * kPointerSize));
217 __ bx(lr);
218
219 CodeDesc desc;
220 masm->GetCode(&desc);
221 Handle<Code> code = isolate->factory()->NewCode(
222 desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
223
224 // Call the function from C++.
225 F5 f = FUNCTION_CAST<F5>(code->entry());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000226 CHECK(!CALL_GENERATED_CODE(isolate, f, 0, 0, 0, 0, 0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000227}
228
229#undef __