Prevent NULL deref in uninitialized nativehelper

Bug: 174768641
Test: atest MediaTrackTranscoderTests
Test: Invocation of JNI_GetCreatedJavaVMs before JniInvocation init.
Change-Id: Icd4c4a65cc6a933671a54242df7f3aed07053baa
Merged-In: Icd4c4a65cc6a933671a54242df7f3aed07053baa
diff --git a/JniInvocation.cpp b/JniInvocation.cpp
index 8d0c640..e5cd882 100644
--- a/JniInvocation.cpp
+++ b/JniInvocation.cpp
@@ -117,6 +117,7 @@
                                 int (*get_library_system_property)(char* buffer) = GetLibrarySystemProperty);
 
   static JniInvocationImpl& GetJniInvocation();
+  static bool IsInitialized();
 
   jint JNI_GetDefaultJavaVMInitArgs(void* vmargs);
   jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args);
@@ -278,6 +279,10 @@
   return *jni_invocation_;
 }
 
+bool JniInvocationImpl::IsInitialized() {
+  return jni_invocation_ != nullptr;
+}
+
 jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) {
   return JniInvocationImpl::GetJniInvocation().JNI_GetDefaultJavaVMInitArgs(vm_args);
 }
@@ -291,6 +296,10 @@
 }
 
 jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize size, jsize* vm_count) {
+  if (!JniInvocationImpl::IsInitialized()) {
+    *vm_count = 0;
+    return JNI_OK;
+  }
   return JniInvocationImpl::GetJniInvocation().JNI_GetCreatedJavaVMs(vms, size, vm_count);
 }
 
diff --git a/tests/Android.bp b/tests/Android.bp
index c796eb9..cbcaaeb 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -48,9 +48,6 @@
         // Enable warning of converting ints to void*.
         "-Wint-to-void-pointer-cast",
 
-        // Enable warning of wrong unused annotations.
-        "-Wused-but-marked-unused",
-
         // Enable warning for deprecated language features.
         "-Wdeprecated",
 
diff --git a/tests/JniInvocation_test.cpp b/tests/JniInvocation_test.cpp
index bb62e39..3022380 100644
--- a/tests/JniInvocation_test.cpp
+++ b/tests/JniInvocation_test.cpp
@@ -93,3 +93,37 @@
     GTEST_LOG_(WARNING) << "Host testing unsupported. Please run target tests.";
 #endif
 }
+
+TEST(JNIInvocation, GetDefaultJavaVMInitArgsBeforeInit) {
+#ifdef HAVE_TEST_STUFF
+    EXPECT_DEATH(
+        JNI_GetDefaultJavaVMInitArgs(nullptr),
+        "Failed to create JniInvocation instance before using JNI invocation API.");
+#else
+    GTEST_LOG_(WARNING) << "Host testing unsupported. Please run target tests.";
+#endif
+}
+
+TEST(JNIInvocation, CreateJavaVMBeforeInit) {
+#ifdef HAVE_TEST_STUFF
+    JavaVM *vm;
+    JNIEnv *env;
+    EXPECT_DEATH(
+        JNI_CreateJavaVM(&vm, &env, nullptr),
+        "Failed to create JniInvocation instance before using JNI invocation API.");
+#else
+    GTEST_LOG_(WARNING) << "Host testing unsupported. Please run target tests.";
+#endif
+}
+
+TEST(JNIInvocation, GetCreatedJavaVMsBeforeInit) {
+#ifdef HAVE_TEST_STUFF
+    jsize vm_count;
+    JavaVM *vm;
+    int status = JNI_GetCreatedJavaVMs(&vm, 1, &vm_count);
+    EXPECT_EQ(status, JNI_OK);
+    EXPECT_EQ(vm_count, 0);
+#else
+    GTEST_LOG_(WARNING) << "Host testing unsupported. Please run target tests.";
+#endif
+}