blob: 6b4bc110862ca014359172582446d3bc04a2e842 [file] [log] [blame]
Sebastien Hertz7cde48c2015-01-20 16:06:43 +01001/*
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 "arch/context.h"
18#include "jni.h"
19#include "mirror/art_method-inl.h"
20#include "scoped_thread_state_change.h"
21#include "stack.h"
22#include "thread.h"
23
24namespace art {
25
26namespace {
27
28class TestVisitor : public StackVisitor {
29 public:
30 TestVisitor(Thread* thread, Context* context, mirror::Object* this_value)
31 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
32 : StackVisitor(thread, context), this_value_(this_value), found_method_index_(0) {}
33
34 bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
35 mirror::ArtMethod* m = GetMethod();
36 std::string m_name(m->GetName());
37
38 if (m_name.compare("testSimpleVReg") == 0) {
39 found_method_index_ = 1;
40 uint32_t value = 0;
41
42 CHECK(GetVReg(m, 0, kIntVReg, &value));
43 CHECK_EQ(value, 42u);
44
45 bool success = GetVReg(m, 1, kIntVReg, &value);
46 if (m->IsOptimized(sizeof(void*))) CHECK(!success);
47
48 success = GetVReg(m, 2, kIntVReg, &value);
49 if (m->IsOptimized(sizeof(void*))) CHECK(!success);
50
51 CHECK(GetVReg(m, 3, kReferenceVReg, &value));
52 CHECK_EQ(reinterpret_cast<mirror::Object*>(value), this_value_);
53
54 CHECK(GetVReg(m, 4, kIntVReg, &value));
55 CHECK_EQ(value, 1u);
56
57 CHECK(GetVReg(m, 5, kFloatVReg, &value));
Roland Levillainda4d79b2015-03-24 14:36:11 +000058 uint32_t cast = bit_cast<uint32_t, float>(1.0f);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +010059 CHECK_EQ(value, cast);
60
61 CHECK(GetVReg(m, 6, kIntVReg, &value));
62 CHECK_EQ(value, 2u);
63
64 CHECK(GetVReg(m, 7, kIntVReg, &value));
65 CHECK_EQ(value, true);
66
67 CHECK(GetVReg(m, 8, kIntVReg, &value));
68 CHECK_EQ(value, 3u);
69
70 CHECK(GetVReg(m, 9, kIntVReg, &value));
71 CHECK_EQ(value, static_cast<uint32_t>('c'));
72 } else if (m_name.compare("testPairVReg") == 0) {
73 found_method_index_ = 2;
74 uint64_t value = 0;
75 CHECK(GetVRegPair(m, 0, kLongLoVReg, kLongHiVReg, &value));
76 CHECK_EQ(value, 42u);
77
78 bool success = GetVRegPair(m, 2, kLongLoVReg, kLongHiVReg, &value);
79 if (m->IsOptimized(sizeof(void*))) CHECK(!success);
80
81 success = GetVRegPair(m, 4, kLongLoVReg, kLongHiVReg, &value);
82 if (m->IsOptimized(sizeof(void*))) CHECK(!success);
83
84 uint32_t value32 = 0;
85 CHECK(GetVReg(m, 6, kReferenceVReg, &value32));
86 CHECK_EQ(reinterpret_cast<mirror::Object*>(value32), this_value_);
87
88 CHECK(GetVRegPair(m, 7, kLongLoVReg, kLongHiVReg, &value));
89 CHECK_EQ(static_cast<int64_t>(value), std::numeric_limits<int64_t>::min());
90
91 CHECK(GetVRegPair(m, 9, kLongLoVReg, kLongHiVReg, &value));
92 CHECK_EQ(static_cast<int64_t>(value), std::numeric_limits<int64_t>::max());
93
94 CHECK(GetVRegPair(m, 11, kLongLoVReg, kLongHiVReg, &value));
95 CHECK_EQ(value, 0u);
96
97 CHECK(GetVRegPair(m, 13, kDoubleLoVReg, kDoubleHiVReg, &value));
Roland Levillainda4d79b2015-03-24 14:36:11 +000098 uint64_t cast = bit_cast<uint64_t, double>(2.0);
Sebastien Hertz7cde48c2015-01-20 16:06:43 +010099 CHECK_EQ(value, cast);
100 }
101
102 return true;
103 }
104
105 mirror::Object* this_value_;
106
107 // Value returned to Java to ensure the methods testSimpleVReg and testPairVReg
108 // have been found and tested.
109 jint found_method_index_;
110};
111
112extern "C" JNIEXPORT jint JNICALL Java_Main_doNativeCall(JNIEnv*, jobject value) {
113 ScopedObjectAccess soa(Thread::Current());
114 std::unique_ptr<Context> context(Context::Create());
115 TestVisitor visitor(soa.Self(), context.get(), soa.Decode<mirror::Object*>(value));
116 visitor.WalkStack();
117 return visitor.found_method_index_;
118}
119
120} // namespace
121
122} // namespace art