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
IsFrameCompiledAndNonDebuggable(const art::StackVisitor * sv)29 bool IsFrameCompiledAndNonDebuggable(const art::StackVisitor* sv) {
30 return sv->GetCurrentShadowFrame() == nullptr &&
31 sv->GetCurrentOatQuickMethodHeader()->IsOptimized() &&
32 !Runtime::Current()->IsJavaDebuggable();
33 }
34
CheckOptimizedOutRegLiveness(const art::StackVisitor * sv,ArtMethod * m,uint32_t dex_reg,VRegKind vreg_kind,bool check_val=false,uint32_t expected=0)35 void CheckOptimizedOutRegLiveness(const art::StackVisitor* sv,
36 ArtMethod* m,
37 uint32_t dex_reg,
38 VRegKind vreg_kind,
39 bool check_val = false,
40 uint32_t expected = 0) REQUIRES_SHARED(Locks::mutator_lock_) {
41 uint32_t value = 0;
42 if (IsFrameCompiledAndNonDebuggable(sv)) {
43 CHECK_EQ(sv->GetVReg(m, dex_reg, vreg_kind, &value), false);
44 } else {
45 CHECK(sv->GetVReg(m, dex_reg, vreg_kind, &value));
46 if (check_val) {
47 CHECK_EQ(value, expected);
48 }
49 }
50 }
51
FindMethodIndex(jobject this_value_jobj)52 jint FindMethodIndex(jobject this_value_jobj) {
53 ScopedObjectAccess soa(Thread::Current());
54 std::unique_ptr<Context> context(Context::Create());
55 ObjPtr<mirror::Object> this_value = soa.Decode<mirror::Object>(this_value_jobj);
56 jint found_method_index = 0;
57 StackVisitor::WalkStack(
58 [&](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
59 ArtMethod* m = stack_visitor->GetMethod();
60 std::string m_name(m->GetName());
61
62 if (m_name.compare("$noinline$testThisWithInstanceCall") == 0) {
63 found_method_index = 1;
64 uint32_t value = 0;
65 if (IsFrameCompiledAndNonDebuggable(stack_visitor)) {
66 CheckOptimizedOutRegLiveness(stack_visitor, m, 1, kReferenceVReg);
67 } else {
68 CHECK(stack_visitor->GetVReg(m, 1, kReferenceVReg, &value));
69 CHECK_EQ(reinterpret_cast<mirror::Object*>(value), this_value);
70 CHECK_EQ(stack_visitor->GetThisObject(), this_value);
71 }
72 } else if (m_name.compare("$noinline$testThisWithStaticCall") == 0) {
73 found_method_index = 2;
74 CheckOptimizedOutRegLiveness(stack_visitor, m, 1, kReferenceVReg);
75 } else if (m_name.compare("$noinline$testParameter") == 0) {
76 found_method_index = 3;
77 CheckOptimizedOutRegLiveness(stack_visitor, m, 1, kReferenceVReg);
78 } else if (m_name.compare("$noinline$testObjectInScope") == 0) {
79 found_method_index = 4;
80 CheckOptimizedOutRegLiveness(stack_visitor, m, 0, kReferenceVReg);
81 }
82
83 return true;
84 },
85 soa.Self(),
86 context.get(),
87 art::StackVisitor::StackWalkKind::kIncludeInlinedFrames);
88 return found_method_index;
89 }
90
Java_Main_doNativeCallRef(JNIEnv *,jobject value)91 extern "C" JNIEXPORT jint JNICALL Java_Main_doNativeCallRef(JNIEnv*, jobject value) {
92 return FindMethodIndex(value);
93 }
94
Java_Main_doStaticNativeCallRef(JNIEnv *,jclass)95 extern "C" JNIEXPORT jint JNICALL Java_Main_doStaticNativeCallRef(JNIEnv*, jclass) {
96 return FindMethodIndex(nullptr);
97 }
98
99 } // namespace
100
101 } // namespace art
102