Fix crash with secondary dex files on low RAM devices.

Before a secondary dex files gets dexopted, we create a vdex-only file.
Vdex-only files don't have dexlayout sections.

Test: 692-vdex-secondary-loader
Bug: 184262593
Change-Id: I020f82943bb64060bce280771e40dcd01a6d4d40
diff --git a/runtime/oat_file.cc b/runtime/oat_file.cc
index 115e0ea..857dd3a 100644
--- a/runtime/oat_file.cc
+++ b/runtime/oat_file.cc
@@ -2140,8 +2140,11 @@
   if (oat_dex_file != nullptr) {
     // Should always be there.
     const DexLayoutSections* const sections = oat_dex_file->GetDexLayoutSections();
-    CHECK(sections != nullptr);
-    sections->Madvise(&dex_file, state);
+    if (sections != nullptr) {
+      sections->Madvise(&dex_file, state);
+    } else {
+      DCHECK(oat_dex_file->IsBackedByVdexOnly());
+    }
   }
 }
 
diff --git a/test/692-vdex-secondary-loader/expected-stdout.txt b/test/692-vdex-secondary-loader/expected-stdout.txt
index a127604..a22ac74 100644
--- a/test/692-vdex-secondary-loader/expected-stdout.txt
+++ b/test/692-vdex-secondary-loader/expected-stdout.txt
@@ -2,3 +2,7 @@
 Hello
 Hello
 Hello
+JNI_OnLoad called
+Hello
+Hello
+Hello
diff --git a/test/692-vdex-secondary-loader/run b/test/692-vdex-secondary-loader/run
index 0732095..35b55d6 100644
--- a/test/692-vdex-secondary-loader/run
+++ b/test/692-vdex-secondary-loader/run
@@ -15,4 +15,12 @@
 # limitations under the License.
 
 # Disable dex2oat of secondary dex files.
-exec ${RUN} $@ --no-secondary-compilation
+${RUN} "$@" --no-secondary-compilation
+return_status1=$?
+
+# Set low RAM to hit the Madvise code which used to crash
+${RUN} "$@" --runtime-option -XX:LowMemoryMode --no-secondary-compilation
+return_status2=$?
+
+# Make sure we don't silently ignore an early failure.
+(exit $return_status1) && (exit $return_status2)