blob: 420224dd21c0ce6c890974c039ce9401fee01e42 [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
Mathieu Chartierba17f452016-05-05 15:31:12 -070017#include "art_method-inl.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010018#include "check_reference_map_visitor.h"
Shih-wei Liao9407c602011-09-16 10:36:43 -070019#include "jni.h"
20
21namespace art {
22
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010023#define CHECK_REGS(...) do { \
24 int t[] = {__VA_ARGS__}; \
25 int t_size = sizeof(t) / sizeof(*t); \
26 CheckReferences(t, t_size, GetNativePcOffset()); \
27} while (false);
Shih-wei Liao9407c602011-09-16 10:36:43 -070028
29static int gJava_StackWalk_refmap_calls = 0;
30
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010031class TestReferenceMapVisitor : public CheckReferenceMapVisitor {
32 public:
Mathieu Chartier90443472015-07-16 20:32:27 -070033 explicit TestReferenceMapVisitor(Thread* thread) SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010034 : CheckReferenceMapVisitor(thread) {}
Shih-wei Liao9407c602011-09-16 10:36:43 -070035
Mathieu Chartier90443472015-07-16 20:32:27 -070036 bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010037 if (CheckReferenceMapVisitor::VisitFrame()) {
Elliott Hughes530fa002012-03-12 11:44:49 -070038 return true;
Shih-wei Liao9407c602011-09-16 10:36:43 -070039 }
Mathieu Chartiere401d142015-04-22 13:56:20 -070040 ArtMethod* m = GetMethod();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070041 StringPiece m_name(m->GetName());
Shih-wei Liao9407c602011-09-16 10:36:43 -070042
43 // Given the method name and the number of times the method has been called,
44 // we know the Dex registers with live reference values. Assert that what we
45 // find is what is expected.
Sebastien Hertzf27e2a92016-02-29 16:54:16 +010046 if (m_name == "$noinline$f") {
Shih-wei Liao9407c602011-09-16 10:36:43 -070047 if (gJava_StackWalk_refmap_calls == 1) {
Ian Rogers0399dde2012-06-06 17:09:28 -070048 CHECK_EQ(1U, GetDexPc());
Sebastien Hertzf27e2a92016-02-29 16:54:16 +010049 CHECK_REGS(1); // v1: this
Shih-wei Liao9407c602011-09-16 10:36:43 -070050 } else {
Elliott Hughesf5a7a472011-10-07 14:31:02 -070051 CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
Ian Rogers0399dde2012-06-06 17:09:28 -070052 CHECK_EQ(5U, GetDexPc());
Sebastien Hertzf27e2a92016-02-29 16:54:16 +010053 CHECK_REGS(1); // v1: this
Shih-wei Liao9407c602011-09-16 10:36:43 -070054 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080055 } else if (m_name == "g") {
Shih-wei Liao9407c602011-09-16 10:36:43 -070056 if (gJava_StackWalk_refmap_calls == 1) {
Sebastien Hertzf27e2a92016-02-29 16:54:16 +010057 CHECK_EQ(0xdU, GetDexPc());
58 CHECK_REGS(0, 2); // v2: this (Note that v1 is not in the minimal root set)
Shih-wei Liao9407c602011-09-16 10:36:43 -070059 } else {
Elliott Hughesf5a7a472011-10-07 14:31:02 -070060 CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
Sebastien Hertzf27e2a92016-02-29 16:54:16 +010061 CHECK_EQ(0xdU, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070062 CHECK_REGS(0, 2);
Shih-wei Liao9407c602011-09-16 10:36:43 -070063 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080064 } else if (m_name == "shlemiel") {
Shih-wei Liao9407c602011-09-16 10:36:43 -070065 if (gJava_StackWalk_refmap_calls == 1) {
Sebastien Hertzf27e2a92016-02-29 16:54:16 +010066 CHECK_EQ(0x393U, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070067 CHECK_REGS(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 -070068 } else {
Elliott Hughesf5a7a472011-10-07 14:31:02 -070069 CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
Sebastien Hertzf27e2a92016-02-29 16:54:16 +010070 CHECK_EQ(0x393U, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070071 CHECK_REGS(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 -070072 }
73 }
Elliott Hughes530fa002012-03-12 11:44:49 -070074
75 return true;
Shih-wei Liao9407c602011-09-16 10:36:43 -070076 }
77};
78
Andreas Gampe1c83cbc2014-07-22 18:52:29 -070079extern "C" JNIEXPORT jint JNICALL Java_Main_stackmap(JNIEnv*, jobject, jint count) {
Ian Rogers7a22fa62013-01-23 12:16:16 -080080 ScopedObjectAccess soa(Thread::Current());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070081 CHECK_EQ(count, 0);
Shih-wei Liao9407c602011-09-16 10:36:43 -070082 gJava_StackWalk_refmap_calls++;
83
84 // Visitor
Ian Rogers7a22fa62013-01-23 12:16:16 -080085 TestReferenceMapVisitor mapper(soa.Self());
Ian Rogers0399dde2012-06-06 17:09:28 -070086 mapper.WalkStack();
Shih-wei Liao9407c602011-09-16 10:36:43 -070087
88 return count + 1;
89}
90
Andreas Gampe1c83cbc2014-07-22 18:52:29 -070091extern "C" JNIEXPORT jint JNICALL Java_Main_refmap2(JNIEnv*, jobject, jint count) {
Ian Rogers7a22fa62013-01-23 12:16:16 -080092 ScopedObjectAccess soa(Thread::Current());
Shih-wei Liao9407c602011-09-16 10:36:43 -070093 gJava_StackWalk_refmap_calls++;
94
95 // Visitor
Ian Rogers7a22fa62013-01-23 12:16:16 -080096 TestReferenceMapVisitor mapper(soa.Self());
Ian Rogers0399dde2012-06-06 17:09:28 -070097 mapper.WalkStack();
Shih-wei Liao9407c602011-09-16 10:36:43 -070098
99 return count + 1;
100}
101
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700102} // namespace art