1 /*
2  * Copyright (C) 2013 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 <atomic>
18 
19 #include "jvmti.h"
20 
21 // Test infrastructure
22 #include "jvmti_helper.h"
23 #include "scoped_local_ref.h"
24 #include "test_env.h"
25 
26 namespace art {
27 namespace Test1951MonitorEnterNoSuspend {
28 
29 using RawMonitorEnterNoSuspend = jvmtiError(*)(jvmtiEnv* env, jrawMonitorID mon);
30 
31 template <typename T>
Dealloc(T * t)32 static void Dealloc(T* t) {
33   jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(t));
34 }
35 
36 template <typename T, typename ...Rest>
Dealloc(T * t,Rest...rs)37 static void Dealloc(T* t, Rest... rs) {
38   Dealloc(t);
39   Dealloc(rs...);
40 }
DeallocParams(jvmtiParamInfo * params,jint n_params)41 static void DeallocParams(jvmtiParamInfo* params, jint n_params) {
42   for (jint i = 0; i < n_params; i++) {
43     Dealloc(params[i].name);
44   }
45 }
46 
GetNoSuspendFunction(JNIEnv * env)47 RawMonitorEnterNoSuspend GetNoSuspendFunction(JNIEnv* env) {
48   // Get the extensions.
49   jint n_ext = 0;
50   jvmtiExtensionFunctionInfo* infos = nullptr;
51   if (JvmtiErrorToException(env, jvmti_env, jvmti_env->GetExtensionFunctions(&n_ext, &infos))) {
52     return nullptr;
53   }
54   RawMonitorEnterNoSuspend result = nullptr;
55   for (jint i = 0; i < n_ext; i++) {
56     jvmtiExtensionFunctionInfo* cur_info = &infos[i];
57     if (strcmp("com.android.art.concurrent.raw_monitor_enter_no_suspend", cur_info->id) == 0) {
58       result = reinterpret_cast<RawMonitorEnterNoSuspend>(cur_info->func);
59     }
60     // Cleanup the cur_info
61     DeallocParams(cur_info->params, cur_info->param_count);
62     Dealloc(cur_info->id, cur_info->short_description, cur_info->params, cur_info->errors);
63   }
64   // Cleanup the array.
65   Dealloc(infos);
66   return result;
67 }
68 
69 static std::atomic<bool> started(false);
70 static std::atomic<bool> resumed(false);
71 static std::atomic<bool> progress(false);
72 
Java_art_Test1951_otherThreadStart(JNIEnv * env,jclass)73 extern "C" JNIEXPORT void JNICALL Java_art_Test1951_otherThreadStart(JNIEnv* env, jclass) {
74   jrawMonitorID mon;
75   if (JvmtiErrorToException(env, jvmti_env, jvmti_env->CreateRawMonitor("test 1951", &mon))) {
76     return;
77   }
78   RawMonitorEnterNoSuspend enter_func = GetNoSuspendFunction(env);
79   if (enter_func == nullptr) {
80     return;
81   }
82   started = true;
83   while (!resumed) {}
84   jvmtiError err = enter_func(jvmti_env, mon);
85   CHECK_EQ(err, JVMTI_ERROR_NONE);
86   progress = true;
87   err = jvmti_env->RawMonitorExit(mon);
88   CHECK_EQ(err, JVMTI_ERROR_NONE);
89 }
90 
Java_art_Test1951_waitForStart(JNIEnv *,jclass)91 extern "C" JNIEXPORT void JNICALL Java_art_Test1951_waitForStart(JNIEnv*, jclass) {
92   while (!started) {}
93 }
94 
Java_art_Test1951_otherThreadResume(JNIEnv *,jclass)95 extern "C" JNIEXPORT void JNICALL Java_art_Test1951_otherThreadResume(JNIEnv*, jclass) {
96   resumed = true;
97 }
98 
Java_art_Test1951_otherThreadProgressed(JNIEnv *,jclass)99 extern "C" JNIEXPORT jboolean JNICALL Java_art_Test1951_otherThreadProgressed(JNIEnv*, jclass) {
100   return progress;
101 }
102 
103 }  // namespace Test1951MonitorEnterNoSuspend
104 }  // namespace art
105