Fix snapshot sniffing code to give better error messages.

TEST=ci

Change-Id: If5097c74e7e4f85de2ca8cfc2cab3283f8328ae3
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/402202
Reviewed-by: Alexander Aprelev <aam@google.com>
Commit-Queue: Siva Annamalai <asiva@google.com>
diff --git a/runtime/bin/main_impl.cc b/runtime/bin/main_impl.cc
index 4a26768..f0075f1 100644
--- a/runtime/bin/main_impl.cc
+++ b/runtime/bin/main_impl.cc
@@ -1296,10 +1296,22 @@
             script_name);
         Platform::Exit(kErrorExitCode);
       }
+      if (app_snapshot->IsJIT() && Dart_IsPrecompiledRuntime()) {
+        Syslog::PrintErr(
+            "%s is a JIT snapshot, it cannot be run with 'dartaotruntime'\n",
+            script_name);
+        Platform::Exit(kErrorExitCode);
+      }
       vm_run_app_snapshot = true;
       app_snapshot->SetBuffers(&vm_snapshot_data, &vm_snapshot_instructions,
                                &app_isolate_snapshot_data,
                                &app_isolate_snapshot_instructions);
+    } else if (app_snapshot == nullptr && Dart_IsPrecompiledRuntime()) {
+      Syslog::PrintErr(
+          "%s is not an AOT snapshot,"
+          " it cannot be run with 'dartaotruntime'\n",
+          script_name);
+      Platform::Exit(kErrorExitCode);
     }
   };
 
diff --git a/runtime/bin/snapshot_utils.cc b/runtime/bin/snapshot_utils.cc
index 3ebe7d8..0ff1499 100644
--- a/runtime/bin/snapshot_utils.cc
+++ b/runtime/bin/snapshot_utils.cc
@@ -48,7 +48,6 @@
 
  private:
 };
-#endif  // !defined(DART_PRECOMPILED_RUNTIME)
 
 class MappedAppSnapshot : public AppSnapshot {
  public:
@@ -143,6 +142,7 @@
       nullptr, nullptr, isolate_data_mapping, isolate_instr_mapping);
   return app_snapshot;
 }
+#endif  // !defined(DART_PRECOMPILED_RUNTIME)
 
 #if defined(DART_PRECOMPILED_RUNTIME)
 class ElfAppSnapshot : public AppSnapshot {
@@ -594,10 +594,6 @@
   }
   DartUtils::MagicNumber magic_number =
       DartUtils::SniffForMagicNumber(header, sizeof(header));
-  if (magic_number == DartUtils::kAppJITMagicNumber) {
-    // Return the JIT snapshot.
-    return TryReadAppSnapshotBlobs(script_name, file);
-  }
 #if defined(DART_PRECOMPILED_RUNTIME)
   if (!DartUtils::IsAotMagicNumber(magic_number)) {
     return nullptr;
@@ -623,6 +619,10 @@
   return TryReadAppSnapshotElf(script_name, /*file_offset=*/0,
                                force_load_elf_from_memory);
 #else
+  if (magic_number == DartUtils::kAppJITMagicNumber) {
+    // Return the JIT snapshot.
+    return TryReadAppSnapshotBlobs(script_name, file);
+  }
   // We create a dummy snapshot object just to remember the type which
   // has already been identified by sniffing the magic number.
   return new DummySnapshot(magic_number);