blob: 7e1f7950eb9ebdf45997b38527581d86f999c8d2 [file] [log] [blame]
Andreas Gampe799681b2015-05-15 19:24:12 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "unstarted_runtime.h"
18
Andreas Gampe89e3b482016-04-12 18:07:36 -070019#include <limits>
Andreas Gampe8ce9c302016-04-15 21:24:28 -070020#include <locale>
Andreas Gampe89e3b482016-04-12 18:07:36 -070021
22#include "base/casts.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070023#include "base/enums.h"
Andreas Gampeb6795152016-04-21 17:23:31 -070024#include "base/memory_tool.h"
Andreas Gampe799681b2015-05-15 19:24:12 -070025#include "class_linker.h"
26#include "common_runtime_test.h"
Jeff Hao400ce002015-05-29 10:53:17 -070027#include "dex_instruction.h"
Andreas Gampe799681b2015-05-15 19:24:12 -070028#include "handle.h"
29#include "handle_scope-inl.h"
Jeff Hao400ce002015-05-29 10:53:17 -070030#include "interpreter/interpreter_common.h"
Andreas Gampe799681b2015-05-15 19:24:12 -070031#include "mirror/class_loader.h"
32#include "mirror/string-inl.h"
33#include "runtime.h"
34#include "scoped_thread_state_change.h"
35#include "thread.h"
Andreas Gampe8ce9c302016-04-15 21:24:28 -070036#include "transaction.h"
Andreas Gampe799681b2015-05-15 19:24:12 -070037
38namespace art {
39namespace interpreter {
40
41class UnstartedRuntimeTest : public CommonRuntimeTest {
42 protected:
43 // Re-expose all UnstartedRuntime implementations so we don't need to declare a million
44 // test friends.
45
46 // Methods that intercept available libcore implementations.
47#define UNSTARTED_DIRECT(Name, SigIgnored) \
48 static void Unstarted ## Name(Thread* self, \
49 ShadowFrame* shadow_frame, \
50 JValue* result, \
51 size_t arg_offset) \
Mathieu Chartier90443472015-07-16 20:32:27 -070052 SHARED_REQUIRES(Locks::mutator_lock_) { \
Andreas Gampe799681b2015-05-15 19:24:12 -070053 interpreter::UnstartedRuntime::Unstarted ## Name(self, shadow_frame, result, arg_offset); \
54 }
55#include "unstarted_runtime_list.h"
56 UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT)
57#undef UNSTARTED_RUNTIME_DIRECT_LIST
58#undef UNSTARTED_RUNTIME_JNI_LIST
59#undef UNSTARTED_DIRECT
60
61 // Methods that are native.
Mathieu Chartiere401d142015-04-22 13:56:20 -070062#define UNSTARTED_JNI(Name, SigIgnored) \
Andreas Gampe799681b2015-05-15 19:24:12 -070063 static void UnstartedJNI ## Name(Thread* self, \
Mathieu Chartiere401d142015-04-22 13:56:20 -070064 ArtMethod* method, \
Andreas Gampe799681b2015-05-15 19:24:12 -070065 mirror::Object* receiver, \
66 uint32_t* args, \
67 JValue* result) \
Mathieu Chartier90443472015-07-16 20:32:27 -070068 SHARED_REQUIRES(Locks::mutator_lock_) { \
Andreas Gampe799681b2015-05-15 19:24:12 -070069 interpreter::UnstartedRuntime::UnstartedJNI ## Name(self, method, receiver, args, result); \
70 }
71#include "unstarted_runtime_list.h"
72 UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI)
73#undef UNSTARTED_RUNTIME_DIRECT_LIST
74#undef UNSTARTED_RUNTIME_JNI_LIST
75#undef UNSTARTED_JNI
Andreas Gampe85a098a2016-03-31 13:30:53 -070076
77 // Helpers for ArrayCopy.
78 //
79 // Note: as we have to use handles, we use StackHandleScope to transfer data. Hardcode a size
80 // of three everywhere. That is enough to test all cases.
81
82 static mirror::ObjectArray<mirror::Object>* CreateObjectArray(
83 Thread* self,
84 mirror::Class* component_type,
85 const StackHandleScope<3>& data)
86 SHARED_REQUIRES(Locks::mutator_lock_) {
87 Runtime* runtime = Runtime::Current();
88 mirror::Class* array_type = runtime->GetClassLinker()->FindArrayClass(self, &component_type);
89 CHECK(array_type != nullptr);
90 mirror::ObjectArray<mirror::Object>* result =
91 mirror::ObjectArray<mirror::Object>::Alloc(self, array_type, 3);
92 CHECK(result != nullptr);
93 for (size_t i = 0; i < 3; ++i) {
94 result->Set(static_cast<int32_t>(i), data.GetReference(i));
95 CHECK(!self->IsExceptionPending());
96 }
97 return result;
98 }
99
100 static void CheckObjectArray(mirror::ObjectArray<mirror::Object>* array,
101 const StackHandleScope<3>& data)
102 SHARED_REQUIRES(Locks::mutator_lock_) {
103 CHECK_EQ(array->GetLength(), 3);
104 CHECK_EQ(data.NumberOfReferences(), 3U);
105 for (size_t i = 0; i < 3; ++i) {
106 EXPECT_EQ(data.GetReference(i), array->Get(static_cast<int32_t>(i))) << i;
107 }
108 }
109
110 void RunArrayCopy(Thread* self,
111 ShadowFrame* tmp,
112 bool expect_exception,
113 mirror::ObjectArray<mirror::Object>* src,
114 int32_t src_pos,
115 mirror::ObjectArray<mirror::Object>* dst,
116 int32_t dst_pos,
117 int32_t length)
118 SHARED_REQUIRES(Locks::mutator_lock_) {
119 JValue result;
120 tmp->SetVRegReference(0, src);
121 tmp->SetVReg(1, src_pos);
122 tmp->SetVRegReference(2, dst);
123 tmp->SetVReg(3, dst_pos);
124 tmp->SetVReg(4, length);
125 UnstartedSystemArraycopy(self, tmp, &result, 0);
126 bool exception_pending = self->IsExceptionPending();
127 EXPECT_EQ(exception_pending, expect_exception);
128 if (exception_pending) {
129 self->ClearException();
130 }
131 }
132
133 void RunArrayCopy(Thread* self,
134 ShadowFrame* tmp,
135 bool expect_exception,
136 mirror::Class* src_component_class,
137 mirror::Class* dst_component_class,
138 const StackHandleScope<3>& src_data,
139 int32_t src_pos,
140 const StackHandleScope<3>& dst_data,
141 int32_t dst_pos,
142 int32_t length,
143 const StackHandleScope<3>& expected_result)
144 SHARED_REQUIRES(Locks::mutator_lock_) {
145 StackHandleScope<3> hs_misc(self);
146 Handle<mirror::Class> dst_component_handle(hs_misc.NewHandle(dst_component_class));
147
148 Handle<mirror::ObjectArray<mirror::Object>> src_handle(
149 hs_misc.NewHandle(CreateObjectArray(self, src_component_class, src_data)));
150
151 Handle<mirror::ObjectArray<mirror::Object>> dst_handle(
152 hs_misc.NewHandle(CreateObjectArray(self, dst_component_handle.Get(), dst_data)));
153
154 RunArrayCopy(self,
155 tmp,
156 expect_exception,
157 src_handle.Get(),
158 src_pos,
159 dst_handle.Get(),
160 dst_pos,
161 length);
162 CheckObjectArray(dst_handle.Get(), expected_result);
163 }
Andreas Gampe89e3b482016-04-12 18:07:36 -0700164
165 void TestCeilFloor(bool ceil,
166 Thread* self,
167 ShadowFrame* tmp,
168 double const test_pairs[][2],
169 size_t num_pairs)
170 SHARED_REQUIRES(Locks::mutator_lock_) {
171 for (size_t i = 0; i < num_pairs; ++i) {
172 tmp->SetVRegDouble(0, test_pairs[i][0]);
173
174 JValue result;
175 if (ceil) {
176 UnstartedMathCeil(self, tmp, &result, 0);
177 } else {
178 UnstartedMathFloor(self, tmp, &result, 0);
179 }
180
181 ASSERT_FALSE(self->IsExceptionPending());
182
183 // We want precise results.
184 int64_t result_int64t = bit_cast<int64_t, double>(result.GetD());
185 int64_t expect_int64t = bit_cast<int64_t, double>(test_pairs[i][1]);
186 EXPECT_EQ(expect_int64t, result_int64t) << result.GetD() << " vs " << test_pairs[i][1];
187 }
188 }
Andreas Gampe8ce9c302016-04-15 21:24:28 -0700189
190 // Prepare for aborts. Aborts assume that the exception class is already resolved, as the
191 // loading code doesn't work under transactions.
192 void PrepareForAborts() SHARED_REQUIRES(Locks::mutator_lock_) {
193 mirror::Object* result = Runtime::Current()->GetClassLinker()->FindClass(
194 Thread::Current(),
195 Transaction::kAbortExceptionSignature,
196 ScopedNullHandle<mirror::ClassLoader>());
197 CHECK(result != nullptr);
198 }
Andreas Gampe799681b2015-05-15 19:24:12 -0700199};
200
201TEST_F(UnstartedRuntimeTest, MemoryPeekByte) {
202 Thread* self = Thread::Current();
203
204 ScopedObjectAccess soa(self);
205 constexpr const uint8_t base_array[] = "abcdefghijklmnop";
206 constexpr int32_t kBaseLen = sizeof(base_array) / sizeof(uint8_t);
207 const uint8_t* base_ptr = base_array;
208
209 JValue result;
210 ShadowFrame* tmp = ShadowFrame::CreateDeoptimizedFrame(10, nullptr, nullptr, 0);
211
212 for (int32_t i = 0; i < kBaseLen; ++i) {
213 tmp->SetVRegLong(0, static_cast<int64_t>(reinterpret_cast<intptr_t>(base_ptr + i)));
214
215 UnstartedMemoryPeekByte(self, tmp, &result, 0);
216
217 EXPECT_EQ(result.GetB(), static_cast<int8_t>(base_array[i]));
218 }
219
220 ShadowFrame::DeleteDeoptimizedFrame(tmp);
221}
222
223TEST_F(UnstartedRuntimeTest, MemoryPeekShort) {
224 Thread* self = Thread::Current();
225
226 ScopedObjectAccess soa(self);
227 constexpr const uint8_t base_array[] = "abcdefghijklmnop";
228 constexpr int32_t kBaseLen = sizeof(base_array) / sizeof(uint8_t);
229 const uint8_t* base_ptr = base_array;
230
231 JValue result;
232 ShadowFrame* tmp = ShadowFrame::CreateDeoptimizedFrame(10, nullptr, nullptr, 0);
233
234 int32_t adjusted_length = kBaseLen - sizeof(int16_t);
235 for (int32_t i = 0; i < adjusted_length; ++i) {
236 tmp->SetVRegLong(0, static_cast<int64_t>(reinterpret_cast<intptr_t>(base_ptr + i)));
237
238 UnstartedMemoryPeekShort(self, tmp, &result, 0);
239
240 typedef int16_t unaligned_short __attribute__ ((aligned (1)));
241 const unaligned_short* short_ptr = reinterpret_cast<const unaligned_short*>(base_ptr + i);
242 EXPECT_EQ(result.GetS(), *short_ptr);
243 }
244
245 ShadowFrame::DeleteDeoptimizedFrame(tmp);
246}
247
248TEST_F(UnstartedRuntimeTest, MemoryPeekInt) {
249 Thread* self = Thread::Current();
250
251 ScopedObjectAccess soa(self);
252 constexpr const uint8_t base_array[] = "abcdefghijklmnop";
253 constexpr int32_t kBaseLen = sizeof(base_array) / sizeof(uint8_t);
254 const uint8_t* base_ptr = base_array;
255
256 JValue result;
257 ShadowFrame* tmp = ShadowFrame::CreateDeoptimizedFrame(10, nullptr, nullptr, 0);
258
259 int32_t adjusted_length = kBaseLen - sizeof(int32_t);
260 for (int32_t i = 0; i < adjusted_length; ++i) {
261 tmp->SetVRegLong(0, static_cast<int64_t>(reinterpret_cast<intptr_t>(base_ptr + i)));
262
263 UnstartedMemoryPeekInt(self, tmp, &result, 0);
264
265 typedef int32_t unaligned_int __attribute__ ((aligned (1)));
266 const unaligned_int* int_ptr = reinterpret_cast<const unaligned_int*>(base_ptr + i);
267 EXPECT_EQ(result.GetI(), *int_ptr);
268 }
269
270 ShadowFrame::DeleteDeoptimizedFrame(tmp);
271}
272
273TEST_F(UnstartedRuntimeTest, MemoryPeekLong) {
274 Thread* self = Thread::Current();
275
276 ScopedObjectAccess soa(self);
277 constexpr const uint8_t base_array[] = "abcdefghijklmnop";
278 constexpr int32_t kBaseLen = sizeof(base_array) / sizeof(uint8_t);
279 const uint8_t* base_ptr = base_array;
280
281 JValue result;
282 ShadowFrame* tmp = ShadowFrame::CreateDeoptimizedFrame(10, nullptr, nullptr, 0);
283
284 int32_t adjusted_length = kBaseLen - sizeof(int64_t);
285 for (int32_t i = 0; i < adjusted_length; ++i) {
286 tmp->SetVRegLong(0, static_cast<int64_t>(reinterpret_cast<intptr_t>(base_ptr + i)));
287
288 UnstartedMemoryPeekLong(self, tmp, &result, 0);
289
290 typedef int64_t unaligned_long __attribute__ ((aligned (1)));
291 const unaligned_long* long_ptr = reinterpret_cast<const unaligned_long*>(base_ptr + i);
292 EXPECT_EQ(result.GetJ(), *long_ptr);
293 }
294
295 ShadowFrame::DeleteDeoptimizedFrame(tmp);
296}
297
298TEST_F(UnstartedRuntimeTest, StringGetCharsNoCheck) {
299 Thread* self = Thread::Current();
300
301 ScopedObjectAccess soa(self);
302 StackHandleScope<2> hs(self);
303 // TODO: Actual UTF.
304 constexpr const char base_string[] = "abcdefghijklmnop";
305 Handle<mirror::String> h_test_string(hs.NewHandle(
306 mirror::String::AllocFromModifiedUtf8(self, base_string)));
307 constexpr int32_t kBaseLen = sizeof(base_string) / sizeof(char) - 1;
308 Handle<mirror::CharArray> h_char_array(hs.NewHandle(
309 mirror::CharArray::Alloc(self, kBaseLen)));
310 // A buffer so we can make sure we only modify the elements targetted.
311 uint16_t buf[kBaseLen];
312
313 JValue result;
314 ShadowFrame* tmp = ShadowFrame::CreateDeoptimizedFrame(10, nullptr, nullptr, 0);
315
316 for (int32_t start_index = 0; start_index < kBaseLen; ++start_index) {
317 for (int32_t count = 0; count <= kBaseLen; ++count) {
318 for (int32_t trg_offset = 0; trg_offset < kBaseLen; ++trg_offset) {
319 // Only do it when in bounds.
320 if (start_index + count <= kBaseLen && trg_offset + count <= kBaseLen) {
321 tmp->SetVRegReference(0, h_test_string.Get());
322 tmp->SetVReg(1, start_index);
323 tmp->SetVReg(2, count);
324 tmp->SetVRegReference(3, h_char_array.Get());
325 tmp->SetVReg(3, trg_offset);
326
327 // Copy the char_array into buf.
328 memcpy(buf, h_char_array->GetData(), kBaseLen * sizeof(uint16_t));
329
330 UnstartedStringCharAt(self, tmp, &result, 0);
331
332 uint16_t* data = h_char_array->GetData();
333
334 bool success = true;
335
336 // First segment should be unchanged.
337 for (int32_t i = 0; i < trg_offset; ++i) {
338 success = success && (data[i] == buf[i]);
339 }
340 // Second segment should be a copy.
341 for (int32_t i = trg_offset; i < trg_offset + count; ++i) {
342 success = success && (data[i] == buf[i - trg_offset + start_index]);
343 }
344 // Third segment should be unchanged.
345 for (int32_t i = trg_offset + count; i < kBaseLen; ++i) {
346 success = success && (data[i] == buf[i]);
347 }
348
349 EXPECT_TRUE(success);
350 }
351 }
352 }
353 }
354
355 ShadowFrame::DeleteDeoptimizedFrame(tmp);
356}
357
358TEST_F(UnstartedRuntimeTest, StringCharAt) {
359 Thread* self = Thread::Current();
360
361 ScopedObjectAccess soa(self);
362 // TODO: Actual UTF.
363 constexpr const char* base_string = "abcdefghijklmnop";
364 int32_t base_len = static_cast<int32_t>(strlen(base_string));
365 mirror::String* test_string = mirror::String::AllocFromModifiedUtf8(self, base_string);
366
367 JValue result;
368 ShadowFrame* tmp = ShadowFrame::CreateDeoptimizedFrame(10, nullptr, nullptr, 0);
369
370 for (int32_t i = 0; i < base_len; ++i) {
371 tmp->SetVRegReference(0, test_string);
372 tmp->SetVReg(1, i);
373
374 UnstartedStringCharAt(self, tmp, &result, 0);
375
376 EXPECT_EQ(result.GetI(), base_string[i]);
377 }
378
379 ShadowFrame::DeleteDeoptimizedFrame(tmp);
380}
381
Jeff Hao400ce002015-05-29 10:53:17 -0700382TEST_F(UnstartedRuntimeTest, StringInit) {
383 Thread* self = Thread::Current();
384 ScopedObjectAccess soa(self);
385 mirror::Class* klass = mirror::String::GetJavaLangString();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700386 ArtMethod* method = klass->FindDeclaredDirectMethod("<init>", "(Ljava/lang/String;)V",
Andreas Gampe542451c2016-07-26 09:02:02 -0700387 kRuntimePointerSize);
Jeff Hao400ce002015-05-29 10:53:17 -0700388
389 // create instruction data for invoke-direct {v0, v1} of method with fake index
390 uint16_t inst_data[3] = { 0x2070, 0x0000, 0x0010 };
391 const Instruction* inst = Instruction::At(inst_data);
392
393 JValue result;
394 ShadowFrame* shadow_frame = ShadowFrame::CreateDeoptimizedFrame(10, nullptr, method, 0);
395 const char* base_string = "hello_world";
396 mirror::String* string_arg = mirror::String::AllocFromModifiedUtf8(self, base_string);
397 mirror::String* reference_empty_string = mirror::String::AllocFromModifiedUtf8(self, "");
398 shadow_frame->SetVRegReference(0, reference_empty_string);
399 shadow_frame->SetVRegReference(1, string_arg);
400
401 interpreter::DoCall<false, false>(method, self, *shadow_frame, inst, inst_data[0], &result);
402 mirror::String* string_result = reinterpret_cast<mirror::String*>(result.GetL());
403 EXPECT_EQ(string_arg->GetLength(), string_result->GetLength());
404 EXPECT_EQ(memcmp(string_arg->GetValue(), string_result->GetValue(),
405 string_arg->GetLength() * sizeof(uint16_t)), 0);
406
407 ShadowFrame::DeleteDeoptimizedFrame(shadow_frame);
408}
409
Andreas Gampe85a098a2016-03-31 13:30:53 -0700410// Tests the exceptions that should be checked before modifying the destination.
411// (Doesn't check the object vs primitive case ATM.)
412TEST_F(UnstartedRuntimeTest, SystemArrayCopyObjectArrayTestExceptions) {
413 Thread* self = Thread::Current();
414 ScopedObjectAccess soa(self);
415 JValue result;
416 ShadowFrame* tmp = ShadowFrame::CreateDeoptimizedFrame(10, nullptr, nullptr, 0);
417
418 // Note: all tests are not GC safe. Assume there's no GC running here with the few objects we
419 // allocate.
420 StackHandleScope<2> hs_misc(self);
421 Handle<mirror::Class> object_class(
422 hs_misc.NewHandle(mirror::Class::GetJavaLangClass()->GetSuperClass()));
423
424 StackHandleScope<3> hs_data(self);
425 hs_data.NewHandle(mirror::String::AllocFromModifiedUtf8(self, "1"));
426 hs_data.NewHandle(mirror::String::AllocFromModifiedUtf8(self, "2"));
427 hs_data.NewHandle(mirror::String::AllocFromModifiedUtf8(self, "3"));
428
429 Handle<mirror::ObjectArray<mirror::Object>> array(
430 hs_misc.NewHandle(CreateObjectArray(self, object_class.Get(), hs_data)));
431
432 RunArrayCopy(self, tmp, true, array.Get(), -1, array.Get(), 0, 0);
433 RunArrayCopy(self, tmp, true, array.Get(), 0, array.Get(), -1, 0);
434 RunArrayCopy(self, tmp, true, array.Get(), 0, array.Get(), 0, -1);
435 RunArrayCopy(self, tmp, true, array.Get(), 0, array.Get(), 0, 4);
436 RunArrayCopy(self, tmp, true, array.Get(), 0, array.Get(), 1, 3);
437 RunArrayCopy(self, tmp, true, array.Get(), 1, array.Get(), 0, 3);
438
439 mirror::ObjectArray<mirror::Object>* class_as_array =
440 reinterpret_cast<mirror::ObjectArray<mirror::Object>*>(object_class.Get());
441 RunArrayCopy(self, tmp, true, class_as_array, 0, array.Get(), 0, 0);
442 RunArrayCopy(self, tmp, true, array.Get(), 0, class_as_array, 0, 0);
443
444 ShadowFrame::DeleteDeoptimizedFrame(tmp);
445}
446
447TEST_F(UnstartedRuntimeTest, SystemArrayCopyObjectArrayTest) {
448 Thread* self = Thread::Current();
449 ScopedObjectAccess soa(self);
450 JValue result;
451 ShadowFrame* tmp = ShadowFrame::CreateDeoptimizedFrame(10, nullptr, nullptr, 0);
452
453 StackHandleScope<1> hs_object(self);
454 Handle<mirror::Class> object_class(
455 hs_object.NewHandle(mirror::Class::GetJavaLangClass()->GetSuperClass()));
456
457 // Simple test:
458 // [1,2,3]{1 @ 2} into [4,5,6] = [4,2,6]
459 {
460 StackHandleScope<3> hs_src(self);
461 hs_src.NewHandle(mirror::String::AllocFromModifiedUtf8(self, "1"));
462 hs_src.NewHandle(mirror::String::AllocFromModifiedUtf8(self, "2"));
463 hs_src.NewHandle(mirror::String::AllocFromModifiedUtf8(self, "3"));
464
465 StackHandleScope<3> hs_dst(self);
466 hs_dst.NewHandle(mirror::String::AllocFromModifiedUtf8(self, "4"));
467 hs_dst.NewHandle(mirror::String::AllocFromModifiedUtf8(self, "5"));
468 hs_dst.NewHandle(mirror::String::AllocFromModifiedUtf8(self, "6"));
469
470 StackHandleScope<3> hs_expected(self);
471 hs_expected.NewHandle(hs_dst.GetReference(0));
472 hs_expected.NewHandle(hs_dst.GetReference(1));
473 hs_expected.NewHandle(hs_src.GetReference(1));
474
475 RunArrayCopy(self,
476 tmp,
477 false,
478 object_class.Get(),
479 object_class.Get(),
480 hs_src,
481 1,
482 hs_dst,
483 2,
484 1,
485 hs_expected);
486 }
487
488 // Simple test:
489 // [1,2,3]{1 @ 1} into [4,5,6] = [4,2,6] (with dst String[])
490 {
491 StackHandleScope<3> hs_src(self);
492 hs_src.NewHandle(mirror::String::AllocFromModifiedUtf8(self, "1"));
493 hs_src.NewHandle(mirror::String::AllocFromModifiedUtf8(self, "2"));
494 hs_src.NewHandle(mirror::String::AllocFromModifiedUtf8(self, "3"));
495
496 StackHandleScope<3> hs_dst(self);
497 hs_dst.NewHandle(mirror::String::AllocFromModifiedUtf8(self, "4"));
498 hs_dst.NewHandle(mirror::String::AllocFromModifiedUtf8(self, "5"));
499 hs_dst.NewHandle(mirror::String::AllocFromModifiedUtf8(self, "6"));
500
501 StackHandleScope<3> hs_expected(self);
502 hs_expected.NewHandle(hs_dst.GetReference(0));
503 hs_expected.NewHandle(hs_src.GetReference(1));
504 hs_expected.NewHandle(hs_dst.GetReference(2));
505
506 RunArrayCopy(self,
507 tmp,
508 false,
509 object_class.Get(),
510 mirror::String::GetJavaLangString(),
511 hs_src,
512 1,
513 hs_dst,
514 1,
515 1,
516 hs_expected);
517 }
518
519 // Simple test:
520 // [1,*,3] into [4,5,6] = [1,5,6] + exc
521 {
522 StackHandleScope<3> hs_src(self);
523 hs_src.NewHandle(mirror::String::AllocFromModifiedUtf8(self, "1"));
524 hs_src.NewHandle(mirror::String::GetJavaLangString());
525 hs_src.NewHandle(mirror::String::AllocFromModifiedUtf8(self, "3"));
526
527 StackHandleScope<3> hs_dst(self);
528 hs_dst.NewHandle(mirror::String::AllocFromModifiedUtf8(self, "4"));
529 hs_dst.NewHandle(mirror::String::AllocFromModifiedUtf8(self, "5"));
530 hs_dst.NewHandle(mirror::String::AllocFromModifiedUtf8(self, "6"));
531
532 StackHandleScope<3> hs_expected(self);
533 hs_expected.NewHandle(hs_src.GetReference(0));
534 hs_expected.NewHandle(hs_dst.GetReference(1));
535 hs_expected.NewHandle(hs_dst.GetReference(2));
536
537 RunArrayCopy(self,
538 tmp,
539 true,
540 object_class.Get(),
541 mirror::String::GetJavaLangString(),
542 hs_src,
543 0,
544 hs_dst,
545 0,
546 3,
547 hs_expected);
548 }
549
550 ShadowFrame::DeleteDeoptimizedFrame(tmp);
551}
552
Andreas Gampe13fc1be2016-04-05 20:14:30 -0700553TEST_F(UnstartedRuntimeTest, IntegerParseIntTest) {
554 Thread* self = Thread::Current();
555 ScopedObjectAccess soa(self);
556
557 ShadowFrame* tmp = ShadowFrame::CreateDeoptimizedFrame(10, nullptr, nullptr, 0);
558
559 // Test string. Should be valid, and between minimal values of LONG_MIN and LONG_MAX (for all
560 // suffixes).
561 constexpr const char* test_string = "-2147483646";
562 constexpr int32_t test_values[] = {
563 6,
564 46,
565 646,
566 3646,
567 83646,
568 483646,
569 7483646,
570 47483646,
571 147483646,
572 2147483646,
573 -2147483646
574 };
575
576 static_assert(arraysize(test_values) == 11U, "test_values");
577 CHECK_EQ(strlen(test_string), 11U);
578
579 for (size_t i = 0; i <= 10; ++i) {
580 const char* test_value = &test_string[10 - i];
581
582 StackHandleScope<1> hs_str(self);
583 Handle<mirror::String> h_str(
584 hs_str.NewHandle(mirror::String::AllocFromModifiedUtf8(self, test_value)));
585 ASSERT_NE(h_str.Get(), nullptr);
586 ASSERT_FALSE(self->IsExceptionPending());
587
588 tmp->SetVRegReference(0, h_str.Get());
589
590 JValue result;
591 UnstartedIntegerParseInt(self, tmp, &result, 0);
592
593 ASSERT_FALSE(self->IsExceptionPending());
594 EXPECT_EQ(result.GetI(), test_values[i]);
595 }
596
597 ShadowFrame::DeleteDeoptimizedFrame(tmp);
598}
599
600// Right now the same as Integer.Parse
601TEST_F(UnstartedRuntimeTest, LongParseLongTest) {
602 Thread* self = Thread::Current();
603 ScopedObjectAccess soa(self);
604
605 ShadowFrame* tmp = ShadowFrame::CreateDeoptimizedFrame(10, nullptr, nullptr, 0);
606
607 // Test string. Should be valid, and between minimal values of LONG_MIN and LONG_MAX (for all
608 // suffixes).
609 constexpr const char* test_string = "-2147483646";
610 constexpr int64_t test_values[] = {
611 6,
612 46,
613 646,
614 3646,
615 83646,
616 483646,
617 7483646,
618 47483646,
619 147483646,
620 2147483646,
621 -2147483646
622 };
623
624 static_assert(arraysize(test_values) == 11U, "test_values");
625 CHECK_EQ(strlen(test_string), 11U);
626
627 for (size_t i = 0; i <= 10; ++i) {
628 const char* test_value = &test_string[10 - i];
629
630 StackHandleScope<1> hs_str(self);
631 Handle<mirror::String> h_str(
632 hs_str.NewHandle(mirror::String::AllocFromModifiedUtf8(self, test_value)));
633 ASSERT_NE(h_str.Get(), nullptr);
634 ASSERT_FALSE(self->IsExceptionPending());
635
636 tmp->SetVRegReference(0, h_str.Get());
637
638 JValue result;
639 UnstartedLongParseLong(self, tmp, &result, 0);
640
641 ASSERT_FALSE(self->IsExceptionPending());
642 EXPECT_EQ(result.GetJ(), test_values[i]);
643 }
644
645 ShadowFrame::DeleteDeoptimizedFrame(tmp);
646}
647
Andreas Gampe89e3b482016-04-12 18:07:36 -0700648TEST_F(UnstartedRuntimeTest, Ceil) {
649 Thread* self = Thread::Current();
650 ScopedObjectAccess soa(self);
651
652 ShadowFrame* tmp = ShadowFrame::CreateDeoptimizedFrame(10, nullptr, nullptr, 0);
653
654 constexpr double nan = std::numeric_limits<double>::quiet_NaN();
655 constexpr double inf = std::numeric_limits<double>::infinity();
656 constexpr double ld1 = static_cast<double>((UINT64_C(1) << 53) - 1);
657 constexpr double ld2 = static_cast<double>(UINT64_C(1) << 55);
658 constexpr double test_pairs[][2] = {
659 { -0.0, -0.0 },
660 { 0.0, 0.0 },
661 { -0.5, -0.0 },
662 { -1.0, -1.0 },
663 { 0.5, 1.0 },
664 { 1.0, 1.0 },
665 { nan, nan },
666 { inf, inf },
667 { -inf, -inf },
668 { ld1, ld1 },
669 { ld2, ld2 }
670 };
671
672 TestCeilFloor(true /* ceil */, self, tmp, test_pairs, arraysize(test_pairs));
673
674 ShadowFrame::DeleteDeoptimizedFrame(tmp);
675}
676
677TEST_F(UnstartedRuntimeTest, Floor) {
678 Thread* self = Thread::Current();
679 ScopedObjectAccess soa(self);
680
681 ShadowFrame* tmp = ShadowFrame::CreateDeoptimizedFrame(10, nullptr, nullptr, 0);
682
683 constexpr double nan = std::numeric_limits<double>::quiet_NaN();
684 constexpr double inf = std::numeric_limits<double>::infinity();
685 constexpr double ld1 = static_cast<double>((UINT64_C(1) << 53) - 1);
686 constexpr double ld2 = static_cast<double>(UINT64_C(1) << 55);
687 constexpr double test_pairs[][2] = {
688 { -0.0, -0.0 },
689 { 0.0, 0.0 },
690 { -0.5, -1.0 },
691 { -1.0, -1.0 },
692 { 0.5, 0.0 },
693 { 1.0, 1.0 },
694 { nan, nan },
695 { inf, inf },
696 { -inf, -inf },
697 { ld1, ld1 },
698 { ld2, ld2 }
699 };
700
701 TestCeilFloor(false /* floor */, self, tmp, test_pairs, arraysize(test_pairs));
702
703 ShadowFrame::DeleteDeoptimizedFrame(tmp);
704}
705
Andreas Gampe8ce9c302016-04-15 21:24:28 -0700706TEST_F(UnstartedRuntimeTest, ToLowerUpper) {
707 Thread* self = Thread::Current();
708 ScopedObjectAccess soa(self);
709
710 ShadowFrame* tmp = ShadowFrame::CreateDeoptimizedFrame(10, nullptr, nullptr, 0);
711
712 std::locale c_locale("C");
713
714 // Check ASCII.
715 for (uint32_t i = 0; i < 128; ++i) {
716 bool c_upper = std::isupper(static_cast<char>(i), c_locale);
717 bool c_lower = std::islower(static_cast<char>(i), c_locale);
718 EXPECT_FALSE(c_upper && c_lower) << i;
719
720 // Check toLowerCase.
721 {
722 JValue result;
723 tmp->SetVReg(0, static_cast<int32_t>(i));
724 UnstartedCharacterToLowerCase(self, tmp, &result, 0);
725 ASSERT_FALSE(self->IsExceptionPending());
726 uint32_t lower_result = static_cast<uint32_t>(result.GetI());
727 if (c_lower) {
728 EXPECT_EQ(i, lower_result);
729 } else if (c_upper) {
730 EXPECT_EQ(static_cast<uint32_t>(std::tolower(static_cast<char>(i), c_locale)),
731 lower_result);
732 } else {
733 EXPECT_EQ(i, lower_result);
734 }
735 }
736
737 // Check toUpperCase.
738 {
739 JValue result2;
740 tmp->SetVReg(0, static_cast<int32_t>(i));
741 UnstartedCharacterToUpperCase(self, tmp, &result2, 0);
742 ASSERT_FALSE(self->IsExceptionPending());
743 uint32_t upper_result = static_cast<uint32_t>(result2.GetI());
744 if (c_upper) {
745 EXPECT_EQ(i, upper_result);
746 } else if (c_lower) {
747 EXPECT_EQ(static_cast<uint32_t>(std::toupper(static_cast<char>(i), c_locale)),
748 upper_result);
749 } else {
750 EXPECT_EQ(i, upper_result);
751 }
752 }
753 }
754
755 // Check abort for other things. Can't test all.
756
757 PrepareForAborts();
758
759 for (uint32_t i = 128; i < 256; ++i) {
760 {
761 JValue result;
762 tmp->SetVReg(0, static_cast<int32_t>(i));
763 Transaction transaction;
764 Runtime::Current()->EnterTransactionMode(&transaction);
765 UnstartedCharacterToLowerCase(self, tmp, &result, 0);
766 Runtime::Current()->ExitTransactionMode();
767 ASSERT_TRUE(self->IsExceptionPending());
768 ASSERT_TRUE(transaction.IsAborted());
769 }
770 {
771 JValue result;
772 tmp->SetVReg(0, static_cast<int32_t>(i));
773 Transaction transaction;
774 Runtime::Current()->EnterTransactionMode(&transaction);
775 UnstartedCharacterToUpperCase(self, tmp, &result, 0);
776 Runtime::Current()->ExitTransactionMode();
777 ASSERT_TRUE(self->IsExceptionPending());
778 ASSERT_TRUE(transaction.IsAborted());
779 }
780 }
781 for (uint64_t i = 256; i <= std::numeric_limits<uint32_t>::max(); i <<= 1) {
782 {
783 JValue result;
784 tmp->SetVReg(0, static_cast<int32_t>(i));
785 Transaction transaction;
786 Runtime::Current()->EnterTransactionMode(&transaction);
787 UnstartedCharacterToLowerCase(self, tmp, &result, 0);
788 Runtime::Current()->ExitTransactionMode();
789 ASSERT_TRUE(self->IsExceptionPending());
790 ASSERT_TRUE(transaction.IsAborted());
791 }
792 {
793 JValue result;
794 tmp->SetVReg(0, static_cast<int32_t>(i));
795 Transaction transaction;
796 Runtime::Current()->EnterTransactionMode(&transaction);
797 UnstartedCharacterToUpperCase(self, tmp, &result, 0);
798 Runtime::Current()->ExitTransactionMode();
799 ASSERT_TRUE(self->IsExceptionPending());
800 ASSERT_TRUE(transaction.IsAborted());
801 }
802 }
803
804 ShadowFrame::DeleteDeoptimizedFrame(tmp);
805}
806
Andreas Gampeb8a00f92016-04-18 20:51:13 -0700807TEST_F(UnstartedRuntimeTest, Sin) {
808 Thread* self = Thread::Current();
809 ScopedObjectAccess soa(self);
810
811 ShadowFrame* tmp = ShadowFrame::CreateDeoptimizedFrame(10, nullptr, nullptr, 0);
812
813 // Test an important value, PI/6. That's the one we see in practice.
814 constexpr uint64_t lvalue = UINT64_C(0x3fe0c152382d7365);
815 tmp->SetVRegLong(0, static_cast<int64_t>(lvalue));
816
817 JValue result;
818 UnstartedMathSin(self, tmp, &result, 0);
819
820 const uint64_t lresult = static_cast<uint64_t>(result.GetJ());
821 EXPECT_EQ(UINT64_C(0x3fdfffffffffffff), lresult);
822
823 ShadowFrame::DeleteDeoptimizedFrame(tmp);
824}
825
826TEST_F(UnstartedRuntimeTest, Cos) {
827 Thread* self = Thread::Current();
828 ScopedObjectAccess soa(self);
829
830 ShadowFrame* tmp = ShadowFrame::CreateDeoptimizedFrame(10, nullptr, nullptr, 0);
831
832 // Test an important value, PI/6. That's the one we see in practice.
833 constexpr uint64_t lvalue = UINT64_C(0x3fe0c152382d7365);
834 tmp->SetVRegLong(0, static_cast<int64_t>(lvalue));
835
836 JValue result;
837 UnstartedMathCos(self, tmp, &result, 0);
838
839 const uint64_t lresult = static_cast<uint64_t>(result.GetJ());
840 EXPECT_EQ(UINT64_C(0x3febb67ae8584cab), lresult);
841
842 ShadowFrame::DeleteDeoptimizedFrame(tmp);
843}
844
845TEST_F(UnstartedRuntimeTest, Pow) {
Andreas Gampeb6795152016-04-21 17:23:31 -0700846 // Valgrind seems to get this wrong, actually. Disable for valgrind.
847 if (RUNNING_ON_MEMORY_TOOL != 0 && kMemoryToolIsValgrind) {
848 return;
849 }
850
Andreas Gampeb8a00f92016-04-18 20:51:13 -0700851 Thread* self = Thread::Current();
852 ScopedObjectAccess soa(self);
853
854 ShadowFrame* tmp = ShadowFrame::CreateDeoptimizedFrame(10, nullptr, nullptr, 0);
855
856 // Test an important pair.
857 constexpr uint64_t lvalue1 = UINT64_C(0x4079000000000000);
858 constexpr uint64_t lvalue2 = UINT64_C(0xbfe6db6dc0000000);
859
860 tmp->SetVRegLong(0, static_cast<int64_t>(lvalue1));
861 tmp->SetVRegLong(2, static_cast<int64_t>(lvalue2));
862
863 JValue result;
864 UnstartedMathPow(self, tmp, &result, 0);
865
866 const uint64_t lresult = static_cast<uint64_t>(result.GetJ());
867 EXPECT_EQ(UINT64_C(0x3f8c5c51326aa7ee), lresult);
868
869 ShadowFrame::DeleteDeoptimizedFrame(tmp);
870}
871
Andreas Gampe799681b2015-05-15 19:24:12 -0700872} // namespace interpreter
873} // namespace art