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-inl.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       REQUIRES_SHARED(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() REQUIRES_SHARED(Locks::mutator_lock_) {
37     ArtMethod* m = GetMethod();
38     std::string m_name(m->GetName());
39 
40     if (m_name.compare("testThisWithInstanceCall") == 0) {
41       found_method_index_ = 1;
42       uint32_t value = 0;
43       CHECK(GetVReg(m, 1, kReferenceVReg, &value));
44       CHECK_EQ(reinterpret_cast<mirror::Object*>(value), this_value_);
45       CHECK_EQ(GetThisObject(), this_value_);
46     } else if (m_name.compare("testThisWithStaticCall") == 0) {
47       found_method_index_ = 2;
48       uint32_t value = 0;
49       CHECK(GetVReg(m, 1, kReferenceVReg, &value));
50     } else if (m_name.compare("testParameter") == 0) {
51       found_method_index_ = 3;
52       uint32_t value = 0;
53       CHECK(GetVReg(m, 1, kReferenceVReg, &value));
54     } else if (m_name.compare("testObjectInScope") == 0) {
55       found_method_index_ = 4;
56       uint32_t value = 0;
57       CHECK(GetVReg(m, 0, kReferenceVReg, &value));
58     }
59 
60     return true;
61   }
62 
63   mirror::Object* this_value_;
64 
65   // Value returned to Java to ensure the methods testSimpleVReg and testPairVReg
66   // have been found and tested.
67   jint found_method_index_;
68 };
69 
Java_Main_doNativeCallRef(JNIEnv *,jobject value)70 extern "C" JNIEXPORT jint JNICALL Java_Main_doNativeCallRef(JNIEnv*, jobject value) {
71   ScopedObjectAccess soa(Thread::Current());
72   std::unique_ptr<Context> context(Context::Create());
73   TestVisitor visitor(soa.Self(), context.get(), soa.Decode<mirror::Object>(value).Ptr());
74   visitor.WalkStack();
75   return visitor.found_method_index_;
76 }
77 
Java_Main_doStaticNativeCallRef(JNIEnv *,jclass)78 extern "C" JNIEXPORT jint JNICALL Java_Main_doStaticNativeCallRef(JNIEnv*, jclass) {
79   ScopedObjectAccess soa(Thread::Current());
80   std::unique_ptr<Context> context(Context::Create());
81   TestVisitor visitor(soa.Self(), context.get(), nullptr);
82   visitor.WalkStack();
83   return visitor.found_method_index_;
84 }
85 
86 }  // namespace
87 
88 }  // namespace art
89