Also log corpus when aborted through libbase.

If a CHECK or LOG(FATAL) is hit, also attempt to log the current
corpus.

Test: pass
Bug: 154633114
Change-Id: Id0f376021011924f5d64eb5b591b5ebab6dc7dbc
diff --git a/fs_mgr/libsnapshot/snapshot_fuzz.cpp b/fs_mgr/libsnapshot/snapshot_fuzz.cpp
index 7b57e79..421154d 100644
--- a/fs_mgr/libsnapshot/snapshot_fuzz.cpp
+++ b/fs_mgr/libsnapshot/snapshot_fuzz.cpp
@@ -53,6 +53,8 @@
 
 namespace android::snapshot {
 
+const SnapshotFuzzData* current_data = nullptr;
+
 SnapshotFuzzEnv* GetSnapshotFuzzEnv();
 
 FUZZ_CLASS(ISnapshotManager, SnapshotManagerAction);
@@ -164,6 +166,22 @@
                      unsigned int line, const char* message) {
     if (severity == LogSeverity::FATAL) {
         StderrLogger(logid, severity, tag, file, line, message);
+
+        // If test fails by a LOG(FATAL) or CHECK(), log the corpus. If it abort()'s, there's
+        // nothing else we can do.
+        StderrLogger(logid, severity, tag, __FILE__, __LINE__,
+                     "Attempting to dump current corpus:");
+        if (current_data == nullptr) {
+            StderrLogger(logid, severity, tag, __FILE__, __LINE__, "Current corpus is nullptr.");
+        } else {
+            std::string content;
+            if (!google::protobuf::TextFormat::PrintToString(*current_data, &content)) {
+                StderrLogger(logid, severity, tag, __FILE__, __LINE__,
+                             "Failed to print corpus to string.");
+            } else {
+                StderrLogger(logid, severity, tag, __FILE__, __LINE__, content.c_str());
+            }
+        }
     }
 }
 // Stop logging (except fatal messages) after global initialization. This is only done once.
@@ -185,6 +203,8 @@
 DEFINE_PROTO_FUZZER(const SnapshotFuzzData& snapshot_fuzz_data) {
     using namespace android::snapshot;
 
+    current_data = &snapshot_fuzz_data;
+
     auto env = GetSnapshotFuzzEnv();
     env->CheckSoftReset();