blob: 3ec0fa3291ee42ddcbb3e956aea327967d1d81f4 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Shih-wei Liao9407c602011-09-16 10:36:43 -070016
Vladimir Marko72101082019-02-05 16:16:30 +000017#include <string_view>
18
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -070019#include "art_method-inl.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010020#include "check_reference_map_visitor.h"
Shih-wei Liao9407c602011-09-16 10:36:43 -070021#include "jni.h"
22
23namespace art {
24
Nicolas Geoffray2c4ffe12018-09-06 10:05:57 +010025#define CHECK_REGS_ARE_REFERENCES(...) do { \
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010026 int t[] = {__VA_ARGS__}; \
27 int t_size = sizeof(t) / sizeof(*t); \
Nicolas Geoffrayb0bf9e22020-09-10 09:54:05 +010028 CheckReferences( \
29 t, t_size, GetDexPc(), GetNativePcOffset(), /* search_for_valid_sack_map= */ false); \
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010030} while (false);
Shih-wei Liao9407c602011-09-16 10:36:43 -070031
32static int gJava_StackWalk_refmap_calls = 0;
33
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010034class TestReferenceMapVisitor : public CheckReferenceMapVisitor {
35 public:
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070036 explicit TestReferenceMapVisitor(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_)
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010037 : CheckReferenceMapVisitor(thread) {}
Shih-wei Liao9407c602011-09-16 10:36:43 -070038
Andreas Gampefa6a1b02018-09-07 08:11:55 -070039 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010040 if (CheckReferenceMapVisitor::VisitFrame()) {
Elliott Hughes530fa002012-03-12 11:44:49 -070041 return true;
Shih-wei Liao9407c602011-09-16 10:36:43 -070042 }
Mathieu Chartiere401d142015-04-22 13:56:20 -070043 ArtMethod* m = GetMethod();
Vladimir Marko72101082019-02-05 16:16:30 +000044 std::string_view m_name(m->GetName());
Shih-wei Liao9407c602011-09-16 10:36:43 -070045
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.
Nicolas Geoffray2c4ffe12018-09-06 10:05:57 +010049 if (m_name == "$noinline$f") {
Shih-wei Liao9407c602011-09-16 10:36:43 -070050 if (gJava_StackWalk_refmap_calls == 1) {
Ian Rogers0399dde2012-06-06 17:09:28 -070051 CHECK_EQ(1U, GetDexPc());
Nicolas Geoffray2c4ffe12018-09-06 10:05:57 +010052 CHECK_REGS_ARE_REFERENCES(1);
Shih-wei Liao9407c602011-09-16 10:36:43 -070053 } else {
Elliott Hughesf5a7a472011-10-07 14:31:02 -070054 CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
Ian Rogers0399dde2012-06-06 17:09:28 -070055 CHECK_EQ(5U, GetDexPc());
Nicolas Geoffray2c4ffe12018-09-06 10:05:57 +010056 CHECK_REGS_ARE_REFERENCES(1);
Shih-wei Liao9407c602011-09-16 10:36:43 -070057 }
Nicolas Geoffray2c4ffe12018-09-06 10:05:57 +010058 found_f_ = true;
59 } else if (m_name == "$noinline$g") {
Shih-wei Liao9407c602011-09-16 10:36:43 -070060 if (gJava_StackWalk_refmap_calls == 1) {
Igor Murashkind61c3812017-06-21 11:13:16 -070061 CHECK_EQ(0xcU, GetDexPc());
Nicolas Geoffray2c4ffe12018-09-06 10:05:57 +010062 CHECK_REGS_ARE_REFERENCES(0, 2); // Note that v1 is not in the minimal root set
Shih-wei Liao9407c602011-09-16 10:36:43 -070063 } else {
Elliott Hughesf5a7a472011-10-07 14:31:02 -070064 CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
Igor Murashkind61c3812017-06-21 11:13:16 -070065 CHECK_EQ(0xcU, GetDexPc());
Nicolas Geoffray2c4ffe12018-09-06 10:05:57 +010066 CHECK_REGS_ARE_REFERENCES(0, 2);
Shih-wei Liao9407c602011-09-16 10:36:43 -070067 }
Nicolas Geoffray2c4ffe12018-09-06 10:05:57 +010068 found_g_ = true;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080069 } else if (m_name == "shlemiel") {
Shih-wei Liao9407c602011-09-16 10:36:43 -070070 if (gJava_StackWalk_refmap_calls == 1) {
Igor Murashkind61c3812017-06-21 11:13:16 -070071 CHECK_EQ(0x380U, GetDexPc());
Nicolas Geoffray2c4ffe12018-09-06 10:05:57 +010072 CHECK_REGS_ARE_REFERENCES(2, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 21, 25);
Shih-wei Liao9407c602011-09-16 10:36:43 -070073 } else {
Elliott Hughesf5a7a472011-10-07 14:31:02 -070074 CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
Igor Murashkind61c3812017-06-21 11:13:16 -070075 CHECK_EQ(0x380U, GetDexPc());
Nicolas Geoffray2c4ffe12018-09-06 10:05:57 +010076 CHECK_REGS_ARE_REFERENCES(2, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 21, 25);
Shih-wei Liao9407c602011-09-16 10:36:43 -070077 }
Nicolas Geoffray2c4ffe12018-09-06 10:05:57 +010078 found_shlemiel_ = true;
Shih-wei Liao9407c602011-09-16 10:36:43 -070079 }
Elliott Hughes530fa002012-03-12 11:44:49 -070080
81 return true;
Shih-wei Liao9407c602011-09-16 10:36:43 -070082 }
Nicolas Geoffray2c4ffe12018-09-06 10:05:57 +010083
84 ~TestReferenceMapVisitor() {
85 }
86
87 bool found_f_ = false;
88 bool found_g_ = false;
89 bool found_shlemiel_ = false;
Shih-wei Liao9407c602011-09-16 10:36:43 -070090};
91
Nicolas Geoffray2c4ffe12018-09-06 10:05:57 +010092extern "C" JNIEXPORT jint JNICALL Java_Main_testStackWalk(JNIEnv*, jobject, jint count) {
Ian Rogers7a22fa62013-01-23 12:16:16 -080093 ScopedObjectAccess soa(Thread::Current());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070094 CHECK_EQ(count, 0);
Shih-wei Liao9407c602011-09-16 10:36:43 -070095 gJava_StackWalk_refmap_calls++;
96
97 // Visitor
Ian Rogers7a22fa62013-01-23 12:16:16 -080098 TestReferenceMapVisitor mapper(soa.Self());
Ian Rogers0399dde2012-06-06 17:09:28 -070099 mapper.WalkStack();
Nicolas Geoffray2c4ffe12018-09-06 10:05:57 +0100100 CHECK(mapper.found_f_);
101 CHECK(mapper.found_g_);
102 CHECK(mapper.found_shlemiel_);
Shih-wei Liao9407c602011-09-16 10:36:43 -0700103
104 return count + 1;
105}
106
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700107} // namespace art