Add test for JNI env modifications on shutdown

Bug: 147804269
Test: .../testrunner.py --host -b --64 -t 2033-shutdown-mechanics
Change-Id: Ie8bc12b94709e38dd043c644cdde3e02d96dedf1
diff --git a/test/2033-shutdown-mechanics/expected.txt b/test/2033-shutdown-mechanics/expected.txt
new file mode 100644
index 0000000..c95f39f
--- /dev/null
+++ b/test/2033-shutdown-mechanics/expected.txt
@@ -0,0 +1,6 @@
+JNI_OnLoad called
+Main Started
+Child started
+Main Finished
+Saw RuntimeShutdownFunctions
+Saw RuntimeDeleted
diff --git a/test/2033-shutdown-mechanics/info.txt b/test/2033-shutdown-mechanics/info.txt
new file mode 100644
index 0000000..d8b94e6
--- /dev/null
+++ b/test/2033-shutdown-mechanics/info.txt
@@ -0,0 +1,3 @@
+Check that when the main thread completes with a Daemon thread running
+in native code, the JNI environment is suitably adjusted so that we
+can avoid accessing deleted data structures.
diff --git a/test/2033-shutdown-mechanics/native_shutdown.cc b/test/2033-shutdown-mechanics/native_shutdown.cc
new file mode 100644
index 0000000..784daa7
--- /dev/null
+++ b/test/2033-shutdown-mechanics/native_shutdown.cc
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "base/time_utils.h"
+#include "base/utils.h"
+#include "jni/jni_env_ext.h"
+#include "jni/jni_internal.h"
+
+#include "jni.h"
+
+namespace art {
+
+static void MaybePrintTime() {
+  constexpr bool kPrintTime = false;
+  if (kPrintTime) {
+    printf("At %u msecs:", static_cast<int>(MilliTime()));
+  }
+}
+
+
+extern "C" [[noreturn]] JNIEXPORT void JNICALL Java_Main_monitorShutdown(
+    JNIEnv* env, jclass klass ATTRIBUTE_UNUSED) {
+  bool found_shutdown = false;
+  bool found_runtime_deleted = false;
+  JNIEnvExt* const extEnv = down_cast<JNIEnvExt*>(env);
+  while (true) {
+    if (!found_shutdown && env->functions == GetRuntimeShutdownNativeInterface()) {
+      found_shutdown = true;
+      MaybePrintTime();
+      printf("Saw RuntimeShutdownFunctions\n");
+    }
+    if (!found_runtime_deleted && extEnv->IsRuntimeDeleted()) {
+      found_runtime_deleted = true;
+      MaybePrintTime();
+      printf("Saw RuntimeDeleted\n");
+    }
+    if (found_shutdown && found_runtime_deleted) {
+      // All JNI calls should now get rerouted to SleepForever();
+      (void) env->NewByteArray(17);
+      printf("Unexpectedly returned from JNI call\n");
+      SleepForever();
+    }
+  }
+}
+
+}  // namespace art
+
+
diff --git a/test/2033-shutdown-mechanics/src/Main.java b/test/2033-shutdown-mechanics/src/Main.java
new file mode 100644
index 0000000..106143c
--- /dev/null
+++ b/test/2033-shutdown-mechanics/src/Main.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+public class Main {
+
+  private static class Child implements Runnable {
+    @Override
+    public void run() {
+      System.out.println("Child started");
+      // Enter native method and stay there, monitoring shutdown behavior.  Since we're a daemon,
+      // the process should shut down anyway, and we should be able to observe changes in the
+      // extended JNI environment.
+      monitorShutdown();
+    }
+  }
+
+  public static void main(String[] args) {
+    System.loadLibrary(args[0]);
+    System.out.println("Main Started");
+    Thread t = new Thread(new Child());
+    t.setDaemon(true);
+    t.start();
+    try {
+      Thread.sleep(200);
+    } catch (InterruptedException e) {
+      System.out.println("Unexpected interrupt");
+    }
+    System.out.println("Main Finished");
+  }
+
+  private static native void monitorShutdown();
+}
diff --git a/test/Android.bp b/test/Android.bp
index c19bb73..43f1631 100644
--- a/test/Android.bp
+++ b/test/Android.bp
@@ -594,6 +594,7 @@
         "1985-structural-redefine-stack-scope/stack_scope.cc",
         "2011-stack-walk-concurrent-instrument/stack_walk_concurrent.cc",
         "2031-zygote-compiled-frame-deopt/native-wait.cc",
+        "2033-shutdown-mechanics/native_shutdown.cc",
         "common/runtime_state.cc",
         "common/stack_inspect.cc",
     ],