1 /*
2  * Copyright (C) 2011 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 <string_view>
18 
19 #include "art_method-inl.h"
20 #include "check_reference_map_visitor.h"
21 #include "jni.h"
22 
23 namespace art {
24 
25 #define CHECK_REGS_ARE_REFERENCES(...) do { \
26   int t[] = {__VA_ARGS__}; \
27   int t_size = sizeof(t) / sizeof(*t); \
28   CheckReferences( \
29       t, t_size, GetDexPc(), GetNativePcOffset(), /* search_for_valid_sack_map= */ false); \
30 } while (false);
31 
32 static int gJava_StackWalk_refmap_calls = 0;
33 
34 class TestReferenceMapVisitor : public CheckReferenceMapVisitor {
35  public:
REQUIRES_SHARED(Locks::mutator_lock_)36   explicit TestReferenceMapVisitor(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_)
37       : CheckReferenceMapVisitor(thread) {}
38 
VisitFrame()39   bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
40     if (CheckReferenceMapVisitor::VisitFrame()) {
41       return true;
42     }
43     ArtMethod* m = GetMethod();
44     std::string_view m_name(m->GetName());
45 
46     // Given the method name and the number of times the method has been called,
47     // we know the Dex registers with live reference values. Assert that what we
48     // find is what is expected.
49     if (m_name == "$noinline$f") {
50       if (gJava_StackWalk_refmap_calls == 1) {
51         CHECK_EQ(1U, GetDexPc());
52         CHECK_REGS_ARE_REFERENCES(1);
53       } else {
54         CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
55         CHECK_EQ(5U, GetDexPc());
56         CHECK_REGS_ARE_REFERENCES(1);
57       }
58       found_f_ = true;
59     } else if (m_name == "$noinline$g") {
60       if (gJava_StackWalk_refmap_calls == 1) {
61         CHECK_EQ(0xcU, GetDexPc());
62         CHECK_REGS_ARE_REFERENCES(0, 2);  // Note that v1 is not in the minimal root set
63       } else {
64         CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
65         CHECK_EQ(0xcU, GetDexPc());
66         CHECK_REGS_ARE_REFERENCES(0, 2);
67       }
68       found_g_ = true;
69     } else if (m_name == "shlemiel") {
70       if (gJava_StackWalk_refmap_calls == 1) {
71         CHECK_EQ(0x380U, GetDexPc());
72         CHECK_REGS_ARE_REFERENCES(2, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 21, 25);
73       } else {
74         CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
75         CHECK_EQ(0x380U, GetDexPc());
76         CHECK_REGS_ARE_REFERENCES(2, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 21, 25);
77       }
78       found_shlemiel_ = true;
79     }
80 
81     return true;
82   }
83 
~TestReferenceMapVisitor()84   ~TestReferenceMapVisitor() {
85   }
86 
87   bool found_f_ = false;
88   bool found_g_ = false;
89   bool found_shlemiel_ = false;
90 };
91 
Java_Main_testStackWalk(JNIEnv *,jobject,jint count)92 extern "C" JNIEXPORT jint JNICALL Java_Main_testStackWalk(JNIEnv*, jobject, jint count) {
93   ScopedObjectAccess soa(Thread::Current());
94   CHECK_EQ(count, 0);
95   gJava_StackWalk_refmap_calls++;
96 
97   // Visitor
98   TestReferenceMapVisitor mapper(soa.Self());
99   mapper.WalkStack();
100   CHECK(mapper.found_f_);
101   CHECK(mapper.found_g_);
102   CHECK(mapper.found_shlemiel_);
103 
104   return count + 1;
105 }
106 
107 }  // namespace art
108