1 /*
2 * Copyright (C) 2022 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 "android-base/macros.h"
18 #include "jni.h"
19 #include "jvmti.h"
20 #include "jvmti_helper.h"
21 #include "test_env.h"
22
23 namespace art {
24 namespace Test2243SingleStepDefault {
25
26 jmethodID interface_default_method;
27
singleStepCB(jvmtiEnv * jvmti,JNIEnv * env,jthread thr,jmethodID method,jlocation location)28 static void singleStepCB(jvmtiEnv* jvmti,
29 JNIEnv* env,
30 jthread thr,
31 jmethodID method,
32 [[maybe_unused]] jlocation location) {
33 // We haven't reached the default method yet. Continue single stepping
34 if (method != interface_default_method) {
35 return;
36 }
37
38 // Disable single stepping
39 jvmtiError err = jvmti_env->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_SINGLE_STEP, thr);
40 if (JvmtiErrorToException(env, jvmti_env, err)) {
41 return;
42 }
43
44 // Inspect the frame.
45 jint frame_count;
46 if (JvmtiErrorToException(env, jvmti_env, jvmti_env->GetFrameCount(thr, &frame_count))) {
47 return;
48 }
49 CHECK_GT(frame_count, 0);
50
51 // Check that the method id from the stack frame is same as the one returned
52 // by single step callback
53 jmethodID m = nullptr;
54 jlong loc = -1;
55 if (JvmtiErrorToException(env, jvmti_env, jvmti_env->GetFrameLocation(thr, 0, &m, &loc))) {
56 return;
57 }
58 CHECK_EQ(m, method) << "Method id from stack walk doesn't match id from single step callback";
59
60 // Check that the method id is also present in the declaring class
61 jclass klass = nullptr;
62 if (JvmtiErrorToException(env, jvmti_env, jvmti_env->GetMethodDeclaringClass(m, &klass))) {
63 return;
64 }
65 jint count = 0;
66 jmethodID* methods = nullptr;
67 jvmtiError result = jvmti_env->GetClassMethods(klass, &count, &methods);
68 if (JvmtiErrorToException(env, jvmti_env, result)) {
69 return;
70 }
71
72 bool found_method_id = false;
73 for (int i = 0; i < count; i++) {
74 if (methods[i] == method) {
75 found_method_id = true;
76 break;
77 }
78 }
79 CHECK(found_method_id) << "Couldn't find the method id in the declaring class";
80
81 // Check it isn't copied method.
82 jint access_flags = 0;
83 if (JvmtiErrorToException(env, jvmti, jvmti->GetMethodModifiers(m, &access_flags))) {
84 return;
85 }
86 static constexpr uint32_t kAccCopied = 0x01000000;
87 static constexpr uint32_t kAccIntrinsic = 0x80000000;
88 bool is_copied = ((access_flags & (kAccIntrinsic | kAccCopied)) == kAccCopied);
89 CHECK(!is_copied) << "Got copied methodID. Missed canonicalizing?\n";
90 }
91
Java_art_Test2243_setSingleStepCallback(JNIEnv * env)92 extern "C" JNIEXPORT void JNICALL Java_art_Test2243_setSingleStepCallback(JNIEnv* env) {
93 jvmtiEventCallbacks callbacks;
94 memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
95 callbacks.SingleStep = singleStepCB;
96
97 jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks));
98 JvmtiErrorToException(env, jvmti_env, ret);
99 }
100
Java_art_Test2243_enableSingleStep(JNIEnv * env,jclass cl,jthread thr)101 extern "C" JNIEXPORT void JNICALL Java_art_Test2243_enableSingleStep(JNIEnv* env,
102 [[maybe_unused]] jclass cl,
103 jthread thr) {
104 jvmtiError err = jvmti_env->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_SINGLE_STEP, thr);
105 JvmtiErrorToException(env, jvmti_env, err);
106 }
107
Java_art_Test2243_setSingleStepUntil(JNIEnv * env,jclass cl,jobject method)108 extern "C" JNIEXPORT void JNICALL Java_art_Test2243_setSingleStepUntil(JNIEnv* env,
109 [[maybe_unused]] jclass cl,
110 jobject method) {
111 interface_default_method = env->FromReflectedMethod(method);
112 }
113
114 } // namespace Test2243SingleStepDefault
115 } // namespace art
116