xref: /aosp_15_r20/art/test/1941-dispose-stress/dispose_stress.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include <atomic>
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include "android-base/logging.h"
20*795d594fSAndroid Build Coastguard Worker #include "jni.h"
21*795d594fSAndroid Build Coastguard Worker #include "scoped_local_ref.h"
22*795d594fSAndroid Build Coastguard Worker #include "scoped_primitive_array.h"
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker #include "jvmti.h"
25*795d594fSAndroid Build Coastguard Worker 
26*795d594fSAndroid Build Coastguard Worker // Test infrastructure
27*795d594fSAndroid Build Coastguard Worker #include "jvmti_helper.h"
28*795d594fSAndroid Build Coastguard Worker #include "test_env.h"
29*795d594fSAndroid Build Coastguard Worker 
30*795d594fSAndroid Build Coastguard Worker namespace art {
31*795d594fSAndroid Build Coastguard Worker namespace Test1941DisposeStress {
32*795d594fSAndroid Build Coastguard Worker 
Java_art_Test1941_setTracingOn(JNIEnv * env,jclass,jthread thr,jboolean enable)33*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT void JNICALL Java_art_Test1941_setTracingOn(JNIEnv* env,
34*795d594fSAndroid Build Coastguard Worker                                                                  jclass,
35*795d594fSAndroid Build Coastguard Worker                                                                  jthread thr,
36*795d594fSAndroid Build Coastguard Worker                                                                  jboolean enable) {
37*795d594fSAndroid Build Coastguard Worker   JvmtiErrorToException(env,
38*795d594fSAndroid Build Coastguard Worker                         jvmti_env,
39*795d594fSAndroid Build Coastguard Worker                         jvmti_env->SetEventNotificationMode(enable ? JVMTI_ENABLE : JVMTI_DISABLE,
40*795d594fSAndroid Build Coastguard Worker                                                             JVMTI_EVENT_SINGLE_STEP,
41*795d594fSAndroid Build Coastguard Worker                                                             thr));
42*795d594fSAndroid Build Coastguard Worker }
43*795d594fSAndroid Build Coastguard Worker 
Java_art_Test1941_AllocEnv(JNIEnv * env,jclass)44*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT jlong JNICALL Java_art_Test1941_AllocEnv(JNIEnv* env, jclass) {
45*795d594fSAndroid Build Coastguard Worker   JavaVM* vm = nullptr;
46*795d594fSAndroid Build Coastguard Worker   if (env->GetJavaVM(&vm) != 0) {
47*795d594fSAndroid Build Coastguard Worker     ScopedLocalRef<jclass> rt_exception(env, env->FindClass("java/lang/RuntimeException"));
48*795d594fSAndroid Build Coastguard Worker     env->ThrowNew(rt_exception.get(), "Unable to get JavaVM");
49*795d594fSAndroid Build Coastguard Worker     return -1;
50*795d594fSAndroid Build Coastguard Worker   }
51*795d594fSAndroid Build Coastguard Worker   jvmtiEnv* new_env = nullptr;
52*795d594fSAndroid Build Coastguard Worker   if (vm->GetEnv(reinterpret_cast<void**>(&new_env), JVMTI_VERSION_1_0) != 0) {
53*795d594fSAndroid Build Coastguard Worker     ScopedLocalRef<jclass> rt_exception(env, env->FindClass("java/lang/RuntimeException"));
54*795d594fSAndroid Build Coastguard Worker     env->ThrowNew(rt_exception.get(), "Unable to create new jvmtiEnv");
55*795d594fSAndroid Build Coastguard Worker     return -1;
56*795d594fSAndroid Build Coastguard Worker   }
57*795d594fSAndroid Build Coastguard Worker   return static_cast<jlong>(reinterpret_cast<intptr_t>(new_env));
58*795d594fSAndroid Build Coastguard Worker }
59*795d594fSAndroid Build Coastguard Worker 
Java_art_Test1941_FreeEnv(JNIEnv * env,jclass,jlong jvmti_env_ptr)60*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT void JNICALL Java_art_Test1941_FreeEnv(JNIEnv* env,
61*795d594fSAndroid Build Coastguard Worker                                                             jclass,
62*795d594fSAndroid Build Coastguard Worker                                                             jlong jvmti_env_ptr) {
63*795d594fSAndroid Build Coastguard Worker   JvmtiErrorToException(env,
64*795d594fSAndroid Build Coastguard Worker                         jvmti_env,
65*795d594fSAndroid Build Coastguard Worker                         reinterpret_cast<jvmtiEnv*>(jvmti_env_ptr)->DisposeEnvironment());
66*795d594fSAndroid Build Coastguard Worker }
67*795d594fSAndroid Build Coastguard Worker 
68*795d594fSAndroid Build Coastguard Worker }  // namespace Test1941DisposeStress
69*795d594fSAndroid Build Coastguard Worker }  // namespace art
70*795d594fSAndroid Build Coastguard Worker 
71