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