OSDN Git Service

Manually close the JNI libraries
authorYi Kong <yikong@google.com>
Tue, 17 Nov 2015 19:22:30 +0000 (19:22 +0000)
committerYi Kong <yikong@google.com>
Mon, 23 Nov 2015 14:32:10 +0000 (14:32 +0000)
Runtime does not support repeatedly doing JNI->CreateVM, thus we need to
manually clean up the dynamic linking loader so that gtests would not
fail.

Bug: 25785594
Change-Id: I15d55e142b9f91735b1c94c8cb43cc854ec49ffe
(cherry picked from commit 46bc2a7c6c5ae6b4d5fc065d607208193c2d162e)

runtime/common_runtime_test.cc

index d514cad..ce0d503 100644 (file)
@@ -409,6 +409,26 @@ void CommonRuntimeTest::TearDown() {
   (*icu_cleanup_fn)();
 
   Runtime::Current()->GetHeap()->VerifyHeap();  // Check for heap corruption after the test
+
+  // Manually closing the JNI libraries.
+  // Runtime does not support repeatedly doing JNI->CreateVM, thus we need to manually clean up the
+  // dynamic linking loader so that gtests would not fail.
+  // Bug: 25785594
+  if (runtime_->IsStarted()) {
+    {
+      // We retrieve the handle by calling dlopen on the library. To close it, we need to call
+      // dlclose twice, the first time to undo our dlopen and the second time to actually unload it.
+      // See man dlopen.
+      void* handle = dlopen("libjavacore.so", RTLD_LAZY);
+      dlclose(handle);
+      CHECK_EQ(0, dlclose(handle));
+    }
+    {
+      void* handle = dlopen("libopenjdk.so", RTLD_LAZY);
+      dlclose(handle);
+      CHECK_EQ(0, dlclose(handle));
+    }
+  }
 }
 
 std::vector<std::string> CommonRuntimeTest::GetLibCoreDexFileNames() {