Fix races in record allocs.
When recording allocation operations, it's possible to create invalid
traces if operations get out of order because an entry is added
after the operation is completed. To fix this, reserve the entry
before executing the operation, then fill it in afterwards.
Need to allow for a reserved entry to be invalid in cases where the
operation can fail.
Test: All unit tests pass.
Change-Id: Ic7d01a1682da8742f34750e86dd163c085e709a3
diff --git a/libc/malloc_debug/RecordData.h b/libc/malloc_debug/RecordData.h
index f4b0d82..ce71da1 100644
--- a/libc/malloc_debug/RecordData.h
+++ b/libc/malloc_debug/RecordData.h
@@ -51,8 +51,7 @@
bool Initialize(const Config& config);
- void AddEntry(const memory_trace::Entry& entry);
- void AddEntryOnly(const memory_trace::Entry& entry);
+ memory_trace::Entry* ReserveEntry();
const std::string& file() { return file_; }
pthread_key_t key() { return key_; }
@@ -63,9 +62,13 @@
static void WriteData(int, siginfo_t*, void*);
static RecordData* record_obj_;
+ static void ThreadKeyDelete(void* data);
+
void WriteEntries();
void WriteEntries(const std::string& file);
+ memory_trace::Entry* InternalReserveEntry();
+
std::mutex entries_lock_;
pthread_key_t key_;
std::vector<memory_trace::Entry> entries_;