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