From e132514d80cb18e65bb63d9c04bfa7965d705dc0 Mon Sep 17 00:00:00 2001
From: Ted Mielczarek <ted@mielczarek.org>
Date: Fri, 12 Feb 2016 15:50:16 -0500
Subject: Ensure Linux minidump writer flushes minidump header early.

If the Linux minidump writer crashes while writing a dump, the dump
might contain some useful information, but the header will be empty
because TypedMDRVA's destructor flushes the data, and the header var
doesn't go out of scope until the end of the `Dump` method. This
fixes that problem by putting the header in a shorter block scope.

We've seen this problem in some Android dumps in the wild, like:
https://crash-stats.mozilla.com/report/index/cef5b777-02d1-43c2-bf40-133ab2160209

R=thestig@chromium.org
BUG=https://bugzilla.mozilla.org/show_bug.cgi?id=1247978

Review URL: https://codereview.chromium.org/1696573003 .
---
 .../linux/minidump_writer/minidump_writer.cc       | 29 ++++++++++++++--------
 1 file changed, 18 insertions(+), 11 deletions(-)

(limited to 'src')

diff --git a/src/client/linux/minidump_writer/minidump_writer.cc b/src/client/linux/minidump_writer/minidump_writer.cc
index 9a0a830b..04327338 100644
--- a/src/client/linux/minidump_writer/minidump_writer.cc
+++ b/src/client/linux/minidump_writer/minidump_writer.cc
@@ -168,19 +168,26 @@ class MinidumpWriter {
     // of stream which we write.
     unsigned kNumWriters = 13;
 
-    TypedMDRVA<MDRawHeader> header(&minidump_writer_);
     TypedMDRVA<MDRawDirectory> dir(&minidump_writer_);
-    if (!header.Allocate())
-      return false;
-    if (!dir.AllocateArray(kNumWriters))
-      return false;
-    my_memset(header.get(), 0, sizeof(MDRawHeader));
+    {
+      // Ensure the header gets flushed, as that happens in the destructor.
+      // If a crash occurs somewhere below, at least the header will be
+      // intact.
+      TypedMDRVA<MDRawHeader> header(&minidump_writer_);
+      if (!header.Allocate())
+        return false;
 
-    header.get()->signature = MD_HEADER_SIGNATURE;
-    header.get()->version = MD_HEADER_VERSION;
-    header.get()->time_date_stamp = time(NULL);
-    header.get()->stream_count = kNumWriters;
-    header.get()->stream_directory_rva = dir.position();
+      if (!dir.AllocateArray(kNumWriters))
+        return false;
+
+      my_memset(header.get(), 0, sizeof(MDRawHeader));
+
+      header.get()->signature = MD_HEADER_SIGNATURE;
+      header.get()->version = MD_HEADER_VERSION;
+      header.get()->time_date_stamp = time(NULL);
+      header.get()->stream_count = kNumWriters;
+      header.get()->stream_directory_rva = dir.position();
+    }
 
     unsigned dir_index = 0;
     MDRawDirectory dirent;
-- 
cgit v1.2.1