1 /*
2  * Copyright (C) 2020 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 "base/time_utils.h"
18 #include "base/utils.h"
19 #include "jni/jni_env_ext.h"
20 #include "jni/jni_internal.h"
21 
22 #include "jni.h"
23 
24 #include <stdio.h>
25 
26 namespace art {
27 
MaybePrintTime()28 static void MaybePrintTime() {
29   constexpr bool kPrintTime = false;
30   if (kPrintTime) {
31     printf("At %u msecs:", static_cast<int>(MilliTime()));
32   }
33 }
34 
35 
Java_Main_monitorShutdown(JNIEnv * env,jclass klass ATTRIBUTE_UNUSED)36 extern "C" [[noreturn]] JNIEXPORT void JNICALL Java_Main_monitorShutdown(
37     JNIEnv* env, jclass klass ATTRIBUTE_UNUSED) {
38   bool found_shutdown = false;
39   bool found_runtime_deleted = false;
40   JNIEnvExt* const extEnv = down_cast<JNIEnvExt*>(env);
41   while (true) {
42     if (!found_shutdown && env->functions == GetRuntimeShutdownNativeInterface()) {
43       found_shutdown = true;
44       MaybePrintTime();
45       printf("Saw RuntimeShutdownFunctions\n");
46       fflush(stdout);
47     }
48     if (!found_runtime_deleted && extEnv->IsRuntimeDeleted()) {
49       found_runtime_deleted = true;
50       MaybePrintTime();
51       printf("Saw RuntimeDeleted\n");
52       fflush(stdout);
53     }
54     if (found_shutdown && found_runtime_deleted) {
55       // All JNI calls should now get rerouted to SleepForever();
56       (void) env->NewByteArray(17);
57       printf("Unexpectedly returned from JNI call\n");
58       fflush(stdout);
59       SleepForever();
60     }
61   }
62 }
63 
64 }  // namespace art
65 
66 
67