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