Skip to content

Commit b07e339

Browse files
author
Tom Cherry
committed
logd: fix use after resize of contents_ vector
SerializedFlushToState::PopNextUnreadLog() was calling AddMinHeapEntry() to replenish the element that was just popped off of the heap, however AddMinHeapEntry() also manages reference counts for the buffers, and this resulting in the following scenario: PopNextUnreadLog() returns a pointer referencing log buffer #1 AddMinHeapEntry() sees that all logs from buffer #1 has been read, so it decrements the reference count The caller of PopNextUnreadLog() uses the result which references invalid memory. This calls CheckForNewLogs() within HasUnreadLogs() instead of requiring a separate call, which fixes an additional issue where continuing from the loop in SerializedLogBuffer::FlushTo() may not pick up subsequent logs in a given log buffer, since CheckForNewLogs() wouldn't be called. This was exacerbated by the above change. This adds a test to check the reference counts for this case and fixes an argument mismatch in SerializedFlushToStateTest. This adds the corpus that surfaced the issue. Bug: 159753229 Bug: 159783005 Test: these unit tests, run fuzzer without error Change-Id: Ib2636dfc14293b7e2cd00876b9def6e9dbbff4ce
1 parent d9299c5 commit b07e339

7 files changed

+56
-17
lines changed

logd/SerializedFlushToState.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ MinHeapElement SerializedFlushToState::PopNextUnreadLog() {
121121

122122
log_positions_[log_id]->read_offset += entry->total_len();
123123

124-
AddMinHeapEntry(log_id);
124+
logs_needed_from_next_position_[log_id] = true;
125125

126126
return top;
127127
}

logd/SerializedFlushToState.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,13 @@ class SerializedFlushToState : public FlushToState {
6161
if (logs_ == nullptr) logs_ = logs;
6262
}
6363

64-
// Checks to see if any log buffers set in logs_needed_from_next_position_ have new logs and
65-
// calls AddMinHeapEntry() if so.
66-
void CheckForNewLogs();
67-
68-
bool HasUnreadLogs() { return !min_heap_.empty(); }
64+
bool HasUnreadLogs() {
65+
CheckForNewLogs();
66+
return !min_heap_.empty();
67+
}
6968

70-
// Pops the next unread log from the min heap. Add the next log for that log_id to the min heap
71-
// if one is available otherwise set logs_needed_from_next_position_ to indicate that we're
72-
// waiting for more logs.
69+
// Pops the next unread log from the min heap and sets logs_needed_from_next_position_ to
70+
// indicate that we're waiting for more logs from the associated log buffer.
7371
MinHeapElement PopNextUnreadLog();
7472

7573
// If the parent log buffer prunes logs, the reference that this class contains may become
@@ -86,6 +84,10 @@ class SerializedFlushToState : public FlushToState {
8684
// first chunk and then first log entry within that chunk that is greater or equal to start().
8785
void CreateLogPosition(log_id_t log_id);
8886

87+
// Checks to see if any log buffers set in logs_needed_from_next_position_ have new logs and
88+
// calls AddMinHeapEntry() if so.
89+
void CheckForNewLogs();
90+
8991
std::list<SerializedLogChunk>* logs_ = nullptr;
9092
// An optional structure that contains an iterator to the serialized log buffer and offset into
9193
// it that this logger should handle next.

logd/SerializedFlushToStateTest.cpp

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ class SerializedFlushToStateTest : public testing::Test {
8989
for (uint32_t mask = 0; mask < max_mask; ++mask) {
9090
auto state = SerializedFlushToState{sequence, mask};
9191
state.InitializeLogs(log_chunks_);
92-
state.CheckForNewLogs();
9392
TestReading(sequence, mask, state);
9493
}
9594
}
@@ -109,7 +108,6 @@ class SerializedFlushToStateTest : public testing::Test {
109108
state.InitializeLogs(log_chunks_);
110109
int loop_count = 0;
111110
while (write_logs(loop_count++)) {
112-
state.CheckForNewLogs();
113111
TestReading(sequence, mask, state);
114112
sequence_numbers_per_buffer_.clear();
115113
}
@@ -149,7 +147,7 @@ class SerializedFlushToStateTest : public testing::Test {
149147

150148
// Add a chunk with the given messages to the a given log buffer. Keep track of the sequence
151149
// numbers for future validation. Optionally mark the block as having finished writing.
152-
void AddChunkWithMessages(int buffer, bool finish_writing,
150+
void AddChunkWithMessages(bool finish_writing, int buffer,
153151
const std::vector<std::string>& messages) {
154152
auto chunk = SerializedLogChunk{kChunkSize};
155153
for (const auto& message : messages) {
@@ -252,3 +250,41 @@ TEST_F(SerializedFlushToStateTest, future_writes) {
252250

253251
TestAllReadingWithFutureMessages(write_logs);
254252
}
253+
254+
TEST_F(SerializedFlushToStateTest, no_dangling_references) {
255+
AddChunkWithMessages(true, 0, {"1st", "2nd"});
256+
AddChunkWithMessages(true, 0, {"3rd", "4th"});
257+
258+
auto state = SerializedFlushToState{1, kLogMaskAll};
259+
state.InitializeLogs(log_chunks_);
260+
261+
ASSERT_EQ(log_chunks_[0].size(), 2U);
262+
auto first_chunk = log_chunks_[0].begin();
263+
auto second_chunk = std::next(first_chunk);
264+
265+
ASSERT_TRUE(state.HasUnreadLogs());
266+
auto first_log = state.PopNextUnreadLog();
267+
EXPECT_STREQ(first_log.entry->msg(), "1st");
268+
EXPECT_EQ(first_chunk->reader_ref_count(), 1U);
269+
EXPECT_EQ(second_chunk->reader_ref_count(), 0U);
270+
271+
ASSERT_TRUE(state.HasUnreadLogs());
272+
auto second_log = state.PopNextUnreadLog();
273+
EXPECT_STREQ(second_log.entry->msg(), "2nd");
274+
EXPECT_EQ(first_chunk->reader_ref_count(), 1U);
275+
EXPECT_EQ(second_chunk->reader_ref_count(), 0U);
276+
277+
ASSERT_TRUE(state.HasUnreadLogs());
278+
auto third_log = state.PopNextUnreadLog();
279+
EXPECT_STREQ(third_log.entry->msg(), "3rd");
280+
EXPECT_EQ(first_chunk->reader_ref_count(), 0U);
281+
EXPECT_EQ(second_chunk->reader_ref_count(), 1U);
282+
283+
ASSERT_TRUE(state.HasUnreadLogs());
284+
auto fourth_log = state.PopNextUnreadLog();
285+
EXPECT_STREQ(fourth_log.entry->msg(), "4th");
286+
EXPECT_EQ(first_chunk->reader_ref_count(), 0U);
287+
EXPECT_EQ(second_chunk->reader_ref_count(), 1U);
288+
289+
EXPECT_FALSE(state.HasUnreadLogs());
290+
}

logd/SerializedLogBuffer.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ bool SerializedLogBuffer::FlushTo(
276276

277277
auto& state = reinterpret_cast<SerializedFlushToState&>(abstract_state);
278278
state.InitializeLogs(logs_);
279-
state.CheckForNewLogs();
280279

281280
while (state.HasUnreadLogs()) {
282281
MinHeapElement top = state.PopNextUnreadLog();
@@ -309,10 +308,6 @@ bool SerializedLogBuffer::FlushTo(
309308
return false;
310309
}
311310
lock.lock();
312-
313-
// Since we released the log above, buffers that aren't in our min heap may now have had
314-
// logs added, so re-check them.
315-
state.CheckForNewLogs();
316311
}
317312

318313
state.set_start(state.start() + 1);

logd/SerializedLogChunk.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ class SerializedLogChunk {
6060
int write_offset() const { return write_offset_; }
6161
uint64_t highest_sequence_number() const { return highest_sequence_number_; }
6262

63+
// Exposed for testing
64+
uint32_t reader_ref_count() const { return reader_ref_count_; }
65+
6366
private:
6467
// The decompressed contents of this log buffer. Deallocated when the ref_count reaches 0 and
6568
// writer_active_ is false.

logd/fuzz/Android.bp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,7 @@ cc_fuzz {
4444
srcs: [
4545
"serialized_log_buffer_fuzzer.cpp",
4646
],
47+
corpus: [
48+
"corpus/logentry_use_after_compress",
49+
]
4750
}
3.26 KB
Binary file not shown.

0 commit comments

Comments
 (0)