| Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 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 | */ |
| Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 16 | |
| Vladimir Marko | 7210108 | 2019-02-05 16:16:30 +0000 | [diff] [blame] | 17 | #include <string_view> |
| 18 | |
| Mathieu Chartier | 48b2b3e | 2016-05-05 15:31:12 -0700 | [diff] [blame] | 19 | #include "art_method-inl.h" |
| Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 20 | #include "check_reference_map_visitor.h" |
| Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 21 | #include "jni.h" |
| 22 | |
| 23 | namespace art { |
| 24 | |
| Nicolas Geoffray | 2c4ffe1 | 2018-09-06 10:05:57 +0100 | [diff] [blame] | 25 | #define CHECK_REGS_ARE_REFERENCES(...) do { \ |
| Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 26 | int t[] = {__VA_ARGS__}; \ |
| 27 | int t_size = sizeof(t) / sizeof(*t); \ |
| Nicolas Geoffray | b0bf9e2 | 2020-09-10 09:54:05 +0100 | [diff] [blame] | 28 | CheckReferences( \ |
| 29 | t, t_size, GetDexPc(), GetNativePcOffset(), /* search_for_valid_sack_map= */ false); \ |
| Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 30 | } while (false); |
| Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 31 | |
| 32 | static int gJava_StackWalk_refmap_calls = 0; |
| 33 | |
| Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 34 | class TestReferenceMapVisitor : public CheckReferenceMapVisitor { |
| 35 | public: |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 36 | explicit TestReferenceMapVisitor(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_) |
| Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 37 | : CheckReferenceMapVisitor(thread) {} |
| Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 38 | |
| Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 39 | bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) { |
| Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 40 | if (CheckReferenceMapVisitor::VisitFrame()) { |
| Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 41 | return true; |
| Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 42 | } |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 43 | ArtMethod* m = GetMethod(); |
| Vladimir Marko | 7210108 | 2019-02-05 16:16:30 +0000 | [diff] [blame] | 44 | std::string_view m_name(m->GetName()); |
| Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 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. |
| Nicolas Geoffray | 2c4ffe1 | 2018-09-06 10:05:57 +0100 | [diff] [blame] | 49 | if (m_name == "$noinline$f") { |
| Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 50 | if (gJava_StackWalk_refmap_calls == 1) { |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 51 | CHECK_EQ(1U, GetDexPc()); |
| Nicolas Geoffray | 2c4ffe1 | 2018-09-06 10:05:57 +0100 | [diff] [blame] | 52 | CHECK_REGS_ARE_REFERENCES(1); |
| Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 53 | } else { |
| Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 54 | CHECK_EQ(gJava_StackWalk_refmap_calls, 2); |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 55 | CHECK_EQ(5U, GetDexPc()); |
| Nicolas Geoffray | 2c4ffe1 | 2018-09-06 10:05:57 +0100 | [diff] [blame] | 56 | CHECK_REGS_ARE_REFERENCES(1); |
| Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 57 | } |
| Nicolas Geoffray | 2c4ffe1 | 2018-09-06 10:05:57 +0100 | [diff] [blame] | 58 | found_f_ = true; |
| 59 | } else if (m_name == "$noinline$g") { |
| Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 60 | if (gJava_StackWalk_refmap_calls == 1) { |
| Igor Murashkin | d61c381 | 2017-06-21 11:13:16 -0700 | [diff] [blame] | 61 | CHECK_EQ(0xcU, GetDexPc()); |
| Nicolas Geoffray | 2c4ffe1 | 2018-09-06 10:05:57 +0100 | [diff] [blame] | 62 | CHECK_REGS_ARE_REFERENCES(0, 2); // Note that v1 is not in the minimal root set |
| Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 63 | } else { |
| Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 64 | CHECK_EQ(gJava_StackWalk_refmap_calls, 2); |
| Igor Murashkin | d61c381 | 2017-06-21 11:13:16 -0700 | [diff] [blame] | 65 | CHECK_EQ(0xcU, GetDexPc()); |
| Nicolas Geoffray | 2c4ffe1 | 2018-09-06 10:05:57 +0100 | [diff] [blame] | 66 | CHECK_REGS_ARE_REFERENCES(0, 2); |
| Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 67 | } |
| Nicolas Geoffray | 2c4ffe1 | 2018-09-06 10:05:57 +0100 | [diff] [blame] | 68 | found_g_ = true; |
| Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 69 | } else if (m_name == "shlemiel") { |
| Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 70 | if (gJava_StackWalk_refmap_calls == 1) { |
| Igor Murashkin | d61c381 | 2017-06-21 11:13:16 -0700 | [diff] [blame] | 71 | CHECK_EQ(0x380U, GetDexPc()); |
| Nicolas Geoffray | 2c4ffe1 | 2018-09-06 10:05:57 +0100 | [diff] [blame] | 72 | CHECK_REGS_ARE_REFERENCES(2, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 21, 25); |
| Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 73 | } else { |
| Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 74 | CHECK_EQ(gJava_StackWalk_refmap_calls, 2); |
| Igor Murashkin | d61c381 | 2017-06-21 11:13:16 -0700 | [diff] [blame] | 75 | CHECK_EQ(0x380U, GetDexPc()); |
| Nicolas Geoffray | 2c4ffe1 | 2018-09-06 10:05:57 +0100 | [diff] [blame] | 76 | CHECK_REGS_ARE_REFERENCES(2, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 21, 25); |
| Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 77 | } |
| Nicolas Geoffray | 2c4ffe1 | 2018-09-06 10:05:57 +0100 | [diff] [blame] | 78 | found_shlemiel_ = true; |
| Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 79 | } |
| Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 80 | |
| 81 | return true; |
| Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 82 | } |
| Nicolas Geoffray | 2c4ffe1 | 2018-09-06 10:05:57 +0100 | [diff] [blame] | 83 | |
| 84 | ~TestReferenceMapVisitor() { |
| 85 | } |
| 86 | |
| 87 | bool found_f_ = false; |
| 88 | bool found_g_ = false; |
| 89 | bool found_shlemiel_ = false; |
| Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
| Nicolas Geoffray | 2c4ffe1 | 2018-09-06 10:05:57 +0100 | [diff] [blame] | 92 | extern "C" JNIEXPORT jint JNICALL Java_Main_testStackWalk(JNIEnv*, jobject, jint count) { |
| Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 93 | ScopedObjectAccess soa(Thread::Current()); |
| Shih-wei Liao | 0839bdb | 2011-10-12 10:50:44 -0700 | [diff] [blame] | 94 | CHECK_EQ(count, 0); |
| Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 95 | gJava_StackWalk_refmap_calls++; |
| 96 | |
| 97 | // Visitor |
| Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 98 | TestReferenceMapVisitor mapper(soa.Self()); |
| Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 99 | mapper.WalkStack(); |
| Nicolas Geoffray | 2c4ffe1 | 2018-09-06 10:05:57 +0100 | [diff] [blame] | 100 | CHECK(mapper.found_f_); |
| 101 | CHECK(mapper.found_g_); |
| 102 | CHECK(mapper.found_shlemiel_); |
| Shih-wei Liao | 9407c60 | 2011-09-16 10:36:43 -0700 | [diff] [blame] | 103 | |
| 104 | return count + 1; |
| 105 | } |
| 106 | |
| Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 107 | } // namespace art |