blob: be0428d2cd6a6738849271b4d815fc5b5cbf4071 [file] [log] [blame]
Alex Light80777ed2019-12-17 17:07:33 +00001/*
2 * Copyright (C) 2019 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 <stdio.h>
18
19#include <vector>
20
21#include "android-base/logging.h"
22#include "android-base/macros.h"
23#include "jni.h"
24#include "jvmti.h"
25
26// Test infrastructure
27#include "jvmti_helper.h"
28#include "scoped_local_ref.h"
29#include "test_env.h"
30
31namespace art {
32namespace Test2005PauseAllRedefineMultithreaded {
33
34static constexpr jlong kRedefinedObjectTag = 0xDEADBEEF;
35
36extern "C" JNIEXPORT void JNICALL
37Java_art_Test2005_UpdateFieldValuesAndResumeThreads(JNIEnv* env,
Stefano Cianciulli78f3c722023-05-16 10:32:54 +000038 [[maybe_unused]] jclass klass,
Alex Light80777ed2019-12-17 17:07:33 +000039 jobjectArray threads_arr,
40 jclass redefined_class,
41 jobjectArray new_fields,
42 jstring default_val) {
43 std::vector<jthread> threads;
Stefano Cianciulli6a5b3982023-01-11 11:21:59 +000044 threads.reserve(env->GetArrayLength(threads_arr));
Alex Light80777ed2019-12-17 17:07:33 +000045 for (jint i = 0; i < env->GetArrayLength(threads_arr); i++) {
46 threads.push_back(env->GetObjectArrayElement(threads_arr, i));
47 }
48 std::vector<jfieldID> fields;
Stefano Cianciulli6a5b3982023-01-11 11:21:59 +000049 fields.reserve(env->GetArrayLength(new_fields));
Alex Light80777ed2019-12-17 17:07:33 +000050 for (jint i = 0; i < env->GetArrayLength(new_fields); i++) {
51 fields.push_back(env->FromReflectedField(env->GetObjectArrayElement(new_fields, i)));
52 }
53 // Tag every instance of the redefined class with kRedefinedObjectTag
54 CHECK_EQ(jvmti_env->IterateOverInstancesOfClass(
55 redefined_class,
56 JVMTI_HEAP_OBJECT_EITHER,
Stefano Cianciulli78f3c722023-05-16 10:32:54 +000057 []([[maybe_unused]] jlong class_tag,
58 [[maybe_unused]] jlong size,
Alex Light80777ed2019-12-17 17:07:33 +000059 jlong* tag_ptr,
Stefano Cianciulli78f3c722023-05-16 10:32:54 +000060 [[maybe_unused]] void* user_data) -> jvmtiIterationControl {
Alex Light80777ed2019-12-17 17:07:33 +000061 *tag_ptr = kRedefinedObjectTag;
62 return JVMTI_ITERATION_CONTINUE;
63 },
64 nullptr),
65 JVMTI_ERROR_NONE);
66 jobject* objs;
67 jint cnt;
68 // Get the objects.
69 CHECK_EQ(jvmti_env->GetObjectsWithTags(1, &kRedefinedObjectTag, &cnt, &objs, nullptr),
70 JVMTI_ERROR_NONE);
71 // Set every field that's null
72 for (jint i = 0; i < cnt; i++) {
73 jobject obj = objs[i];
74 for (jfieldID field : fields) {
75 if (ScopedLocalRef<jobject>(env, env->GetObjectField(obj, field)).get() == nullptr) {
76 env->SetObjectField(obj, field, default_val);
77 }
78 }
79 }
80 LOG(INFO) << "Setting " << cnt << " objects with default values";
81 if (!threads.empty()) {
82 std::vector<jvmtiError> errs(threads.size(), JVMTI_ERROR_NONE);
83 CHECK_EQ(jvmti_env->ResumeThreadList(threads.size(), threads.data(), errs.data()),
84 JVMTI_ERROR_NONE);
85 }
86 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(objs));
87}
88
Vladimir Marko46a89102021-10-21 13:05:46 +000089extern "C" JNIEXPORT jobject JNICALL
Stefano Cianciulli78f3c722023-05-16 10:32:54 +000090Java_Main_fastNativeSleepAndReturnInteger42(JNIEnv* env, [[maybe_unused]] jclass klass) {
Vladimir Marko46a89102021-10-21 13:05:46 +000091 jclass integer_class = env->FindClass("java/lang/Integer");
92 CHECK(integer_class != nullptr);
93 jmethodID integer_value_of =
94 env->GetStaticMethodID(integer_class, "valueOf", "(I)Ljava/lang/Integer;");
95 CHECK(integer_value_of != nullptr);
96 jobject value = env->CallStaticObjectMethod(integer_class, integer_value_of, 42);
97 CHECK(value != nullptr);
98 // Sleep for 500ms, blocking thread suspension (this method is @FastNative).
99 // Except for some odd thread timing, this should ensure that the suspend
100 // request from the redefinition thread is seen by the suspend check in the
101 // JNI stub when we exit this function and then processed with the JNI stub
102 // still on the stack. The instrumentation previously erroneously
103 // intercepted returning to the JNI stub and the "instrumentation exit"
104 // handler treated the return value `jobject` as `mirror::Object*`.
105 usleep(500000);
106 return value;
107}
108
Alex Light80777ed2019-12-17 17:07:33 +0000109} // namespace Test2005PauseAllRedefineMultithreaded
110} // namespace art