Mathieu Chartier | fc7acf9 | 2016-01-13 16:20:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Mathieu Chartier | 06fd2cf | 2016-05-05 11:09:58 -0700 | [diff] [blame] | 17 | #include <dlfcn.h> |
Mathieu Chartier | fc7acf9 | 2016-01-13 16:20:26 -0800 | [diff] [blame] | 18 | #include <iostream> |
| 19 | |
| 20 | #include "base/casts.h" |
| 21 | #include "base/macros.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 22 | #include "jni/java_vm_ext.h" |
| 23 | #include "jni/jni_env_ext.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 24 | #include "thread-current-inl.h" |
Mathieu Chartier | fc7acf9 | 2016-01-13 16:20:26 -0800 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | namespace { |
| 28 | |
| 29 | static volatile std::atomic<bool> vm_was_shutdown(false); |
Alex Light | bc5669e | 2016-06-13 17:22:13 +0000 | [diff] [blame] | 30 | static const int kThreadCount = 4; |
| 31 | |
| 32 | static std::atomic<int> barrier_count(kThreadCount + 1); |
| 33 | |
| 34 | static void JniThreadBarrierWait() { |
| 35 | barrier_count--; |
| 36 | while (barrier_count.load() != 0) { |
| 37 | usleep(1000); |
| 38 | } |
| 39 | } |
Mathieu Chartier | fc7acf9 | 2016-01-13 16:20:26 -0800 | [diff] [blame] | 40 | |
| 41 | extern "C" JNIEXPORT void JNICALL Java_Main_waitAndCallIntoJniEnv(JNIEnv* env, jclass) { |
Alex Light | bc5669e | 2016-06-13 17:22:13 +0000 | [diff] [blame] | 42 | // Wait for all threads to enter JNI together. |
| 43 | JniThreadBarrierWait(); |
Mathieu Chartier | fc7acf9 | 2016-01-13 16:20:26 -0800 | [diff] [blame] | 44 | // Wait until the runtime is shutdown. |
| 45 | while (!vm_was_shutdown.load()) { |
| 46 | usleep(1000); |
| 47 | } |
| 48 | std::cout << "About to call exception check\n"; |
| 49 | env->ExceptionCheck(); |
| 50 | LOG(ERROR) << "Should not be reached!"; |
| 51 | } |
| 52 | |
| 53 | // NO_RETURN does not work with extern "C" for target builds. |
| 54 | extern "C" JNIEXPORT void JNICALL Java_Main_destroyJavaVMAndExit(JNIEnv* env, jclass) { |
Alex Light | bc5669e | 2016-06-13 17:22:13 +0000 | [diff] [blame] | 55 | // Wait for all threads to enter JNI together. |
| 56 | JniThreadBarrierWait(); |
Mathieu Chartier | fc7acf9 | 2016-01-13 16:20:26 -0800 | [diff] [blame] | 57 | // Fake up the managed stack so we can detach. |
| 58 | Thread* const self = Thread::Current(); |
| 59 | self->SetTopOfStack(nullptr); |
| 60 | self->SetTopOfShadowStack(nullptr); |
Ian Rogers | 55256cb | 2017-12-21 17:07:11 -0800 | [diff] [blame] | 61 | JavaVM* vm = down_cast<JNIEnvExt*>(env)->GetVm(); |
Mathieu Chartier | fc7acf9 | 2016-01-13 16:20:26 -0800 | [diff] [blame] | 62 | vm->DetachCurrentThread(); |
Mathieu Chartier | 06fd2cf | 2016-05-05 11:09:58 -0700 | [diff] [blame] | 63 | // Open ourself again to make sure the native library does not get unloaded from |
| 64 | // underneath us due to DestroyJavaVM. b/28406866 |
Mathieu Chartier | 48b2b3e | 2016-05-05 15:31:12 -0700 | [diff] [blame] | 65 | void* handle = dlopen(kIsDebugBuild ? "libarttestd.so" : "libarttest.so", RTLD_NOW); |
| 66 | CHECK(handle != nullptr); |
Mathieu Chartier | fc7acf9 | 2016-01-13 16:20:26 -0800 | [diff] [blame] | 67 | vm->DestroyJavaVM(); |
| 68 | vm_was_shutdown.store(true); |
| 69 | // Give threads some time to get stuck in ExceptionCheck. |
| 70 | usleep(1000000); |
| 71 | if (env != nullptr) { |
| 72 | // Use env != nullptr to trick noreturn. |
| 73 | exit(0); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | } // namespace |
| 78 | } // namespace art |