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 "dex/code_item_accessors-inl.h"
20 #include "jni.h"
21 #include "oat_quick_method_header.h"
22 #include "scoped_thread_state_change-inl.h"
23 #include "stack.h"
24 #include "thread.h"
25 
26 namespace art {
27 
28 namespace {
29 
30 class TestVisitor : public StackVisitor {
31  public:
TestVisitor(Thread * thread,Context * context)32   TestVisitor(Thread* thread, Context* context) REQUIRES_SHARED(Locks::mutator_lock_)
33       : StackVisitor(thread, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
34 
VisitFrame()35   bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
36     ArtMethod* m = GetMethod();
37     std::string m_name(m->GetName());
38 
39     if (m_name.compare("$noinline$testLiveArgument") == 0) {
40       found_method_ = true;
41       CHECK_EQ(CodeItemDataAccessor(m->DexInstructionData()).RegistersSize(), 3u);
42       CheckOptimizedOutRegLiveness(m, 1, kIntVReg, true, 42);
43       CheckOptimizedOutRegLiveness(m, 2, kReferenceVReg);
44     } else if (m_name.compare("$noinline$testIntervalHole") == 0) {
45       found_method_ = true;
46       uint32_t number_of_dex_registers =
47           CodeItemDataAccessor(m->DexInstructionData()).RegistersSize();
48       uint32_t dex_register_of_first_parameter = number_of_dex_registers - 2;
49       CheckOptimizedOutRegLiveness(m, dex_register_of_first_parameter, kIntVReg, true, 1);
50     } else if (m_name.compare("$noinline$testCodeSinking") == 0) {
51       found_method_ = true;
52       CheckOptimizedOutRegLiveness(m, 0, kReferenceVReg);
53     }
54 
55     return true;
56   }
57 
CheckOptimizedOutRegLiveness(ArtMethod * m,uint32_t dex_reg,VRegKind vreg_kind,bool check_val=false,uint32_t expected=0)58   void CheckOptimizedOutRegLiveness(ArtMethod* m,
59                                     uint32_t dex_reg,
60                                     VRegKind vreg_kind,
61                                     bool check_val = false,
62                                     uint32_t expected = 0) REQUIRES_SHARED(Locks::mutator_lock_) {
63     uint32_t value = 0;
64     if (GetCurrentQuickFrame() != nullptr &&
65         GetCurrentOatQuickMethodHeader()->IsOptimized() &&
66         !Runtime::Current()->IsJavaDebuggable()) {
67       CHECK_EQ(GetVReg(m, dex_reg, vreg_kind, &value), false);
68     } else {
69       CHECK(GetVReg(m, dex_reg, vreg_kind, &value));
70       if (check_val) {
71         CHECK_EQ(value, expected);
72       }
73     }
74   }
75 
76   // Value returned to Java to ensure the required methods have been found and tested.
77   bool found_method_ = false;
78 };
79 
Java_Main_doStaticNativeCallLiveVreg(JNIEnv *,jclass)80 extern "C" JNIEXPORT void JNICALL Java_Main_doStaticNativeCallLiveVreg(JNIEnv*, jclass) {
81   ScopedObjectAccess soa(Thread::Current());
82   std::unique_ptr<Context> context(Context::Create());
83   TestVisitor visitor(soa.Self(), context.get());
84   visitor.WalkStack();
85   CHECK(visitor.found_method_);
86 }
87 
88 }  // namespace
89 
90 }  // namespace art
91