1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2015 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include "arch/context.h"
18*795d594fSAndroid Build Coastguard Worker #include "art_method-inl.h"
19*795d594fSAndroid Build Coastguard Worker #include "jni.h"
20*795d594fSAndroid Build Coastguard Worker #include "oat/oat_quick_method_header.h"
21*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
22*795d594fSAndroid Build Coastguard Worker #include "stack.h"
23*795d594fSAndroid Build Coastguard Worker #include "thread.h"
24*795d594fSAndroid Build Coastguard Worker
25*795d594fSAndroid Build Coastguard Worker namespace art {
26*795d594fSAndroid Build Coastguard Worker
27*795d594fSAndroid Build Coastguard Worker namespace {
28*795d594fSAndroid Build Coastguard Worker
IsFrameCompiledAndNonDebuggable(const art::StackVisitor * sv)29*795d594fSAndroid Build Coastguard Worker bool IsFrameCompiledAndNonDebuggable(const art::StackVisitor* sv) {
30*795d594fSAndroid Build Coastguard Worker return sv->GetCurrentShadowFrame() == nullptr &&
31*795d594fSAndroid Build Coastguard Worker sv->GetCurrentOatQuickMethodHeader()->IsOptimized() &&
32*795d594fSAndroid Build Coastguard Worker !Runtime::Current()->IsJavaDebuggable();
33*795d594fSAndroid Build Coastguard Worker }
34*795d594fSAndroid Build Coastguard Worker
CheckOptimizedOutRegLiveness(const art::StackVisitor * sv,ArtMethod * m,uint32_t dex_reg,VRegKind vreg_kind,bool check_val=false,uint32_t expected=0)35*795d594fSAndroid Build Coastguard Worker void CheckOptimizedOutRegLiveness(const art::StackVisitor* sv,
36*795d594fSAndroid Build Coastguard Worker ArtMethod* m,
37*795d594fSAndroid Build Coastguard Worker uint32_t dex_reg,
38*795d594fSAndroid Build Coastguard Worker VRegKind vreg_kind,
39*795d594fSAndroid Build Coastguard Worker bool check_val = false,
40*795d594fSAndroid Build Coastguard Worker uint32_t expected = 0) REQUIRES_SHARED(Locks::mutator_lock_) {
41*795d594fSAndroid Build Coastguard Worker uint32_t value = 0;
42*795d594fSAndroid Build Coastguard Worker if (IsFrameCompiledAndNonDebuggable(sv)) {
43*795d594fSAndroid Build Coastguard Worker CHECK_EQ(sv->GetVReg(m, dex_reg, vreg_kind, &value), false);
44*795d594fSAndroid Build Coastguard Worker } else {
45*795d594fSAndroid Build Coastguard Worker CHECK(sv->GetVReg(m, dex_reg, vreg_kind, &value));
46*795d594fSAndroid Build Coastguard Worker if (check_val) {
47*795d594fSAndroid Build Coastguard Worker CHECK_EQ(value, expected);
48*795d594fSAndroid Build Coastguard Worker }
49*795d594fSAndroid Build Coastguard Worker }
50*795d594fSAndroid Build Coastguard Worker }
51*795d594fSAndroid Build Coastguard Worker
FindMethodIndex(jobject this_value_jobj)52*795d594fSAndroid Build Coastguard Worker jint FindMethodIndex(jobject this_value_jobj) {
53*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(Thread::Current());
54*795d594fSAndroid Build Coastguard Worker std::unique_ptr<Context> context(Context::Create());
55*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Object> this_value = soa.Decode<mirror::Object>(this_value_jobj);
56*795d594fSAndroid Build Coastguard Worker jint found_method_index = 0;
57*795d594fSAndroid Build Coastguard Worker StackVisitor::WalkStack(
58*795d594fSAndroid Build Coastguard Worker [&](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
59*795d594fSAndroid Build Coastguard Worker ArtMethod* m = stack_visitor->GetMethod();
60*795d594fSAndroid Build Coastguard Worker std::string m_name(m->GetName());
61*795d594fSAndroid Build Coastguard Worker
62*795d594fSAndroid Build Coastguard Worker if (m_name.compare("$noinline$testThisWithInstanceCall") == 0) {
63*795d594fSAndroid Build Coastguard Worker found_method_index = 1;
64*795d594fSAndroid Build Coastguard Worker uint32_t value = 0;
65*795d594fSAndroid Build Coastguard Worker if (IsFrameCompiledAndNonDebuggable(stack_visitor)) {
66*795d594fSAndroid Build Coastguard Worker CheckOptimizedOutRegLiveness(stack_visitor, m, 1, kReferenceVReg);
67*795d594fSAndroid Build Coastguard Worker } else {
68*795d594fSAndroid Build Coastguard Worker CHECK(stack_visitor->GetVReg(m, 1, kReferenceVReg, &value));
69*795d594fSAndroid Build Coastguard Worker CHECK_EQ(reinterpret_cast<mirror::Object*>(value), this_value);
70*795d594fSAndroid Build Coastguard Worker CHECK_EQ(stack_visitor->GetThisObject(), this_value);
71*795d594fSAndroid Build Coastguard Worker }
72*795d594fSAndroid Build Coastguard Worker } else if (m_name.compare("$noinline$testThisWithStaticCall") == 0) {
73*795d594fSAndroid Build Coastguard Worker found_method_index = 2;
74*795d594fSAndroid Build Coastguard Worker CheckOptimizedOutRegLiveness(stack_visitor, m, 1, kReferenceVReg);
75*795d594fSAndroid Build Coastguard Worker } else if (m_name.compare("$noinline$testParameter") == 0) {
76*795d594fSAndroid Build Coastguard Worker found_method_index = 3;
77*795d594fSAndroid Build Coastguard Worker CheckOptimizedOutRegLiveness(stack_visitor, m, 1, kReferenceVReg);
78*795d594fSAndroid Build Coastguard Worker } else if (m_name.compare("$noinline$testObjectInScope") == 0) {
79*795d594fSAndroid Build Coastguard Worker found_method_index = 4;
80*795d594fSAndroid Build Coastguard Worker CheckOptimizedOutRegLiveness(stack_visitor, m, 0, kReferenceVReg);
81*795d594fSAndroid Build Coastguard Worker }
82*795d594fSAndroid Build Coastguard Worker
83*795d594fSAndroid Build Coastguard Worker return true;
84*795d594fSAndroid Build Coastguard Worker },
85*795d594fSAndroid Build Coastguard Worker soa.Self(),
86*795d594fSAndroid Build Coastguard Worker context.get(),
87*795d594fSAndroid Build Coastguard Worker art::StackVisitor::StackWalkKind::kIncludeInlinedFrames);
88*795d594fSAndroid Build Coastguard Worker return found_method_index;
89*795d594fSAndroid Build Coastguard Worker }
90*795d594fSAndroid Build Coastguard Worker
Java_Main_doNativeCallRef(JNIEnv *,jobject value)91*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT jint JNICALL Java_Main_doNativeCallRef(JNIEnv*, jobject value) {
92*795d594fSAndroid Build Coastguard Worker return FindMethodIndex(value);
93*795d594fSAndroid Build Coastguard Worker }
94*795d594fSAndroid Build Coastguard Worker
Java_Main_doStaticNativeCallRef(JNIEnv *,jclass)95*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT jint JNICALL Java_Main_doStaticNativeCallRef(JNIEnv*, jclass) {
96*795d594fSAndroid Build Coastguard Worker return FindMethodIndex(nullptr);
97*795d594fSAndroid Build Coastguard Worker }
98*795d594fSAndroid Build Coastguard Worker
99*795d594fSAndroid Build Coastguard Worker } // namespace
100*795d594fSAndroid Build Coastguard Worker
101*795d594fSAndroid Build Coastguard Worker } // namespace art
102