Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3359,6 +3359,80 @@ TEST(WriteConnectionBuffered, ResumeReplaysUnpersistedFlushedData) {
EXPECT_STATUS_OK(f2.get());
}

class WriterConnectionBufferedDestroyTest
: public ::testing::TestWithParam<bool> {};

/// @test Verify that destroying a buffered writer while an underlying
/// `AsyncWriterConnectionImpl` write is still in flight does not cause a
/// use-after-free when the in-flight write later completes (either succeeding
/// or failing).
TEST_P(WriterConnectionBufferedDestroyTest, PendingWriteOutlivesConnection) {
bool const write_succeeds = GetParam();
using Request = google::storage::v2::BidiWriteObjectRequest;
using Response = google::storage::v2::BidiWriteObjectResponse;
using MockStream =
::google::cloud::mocks::MockAsyncStreamingReadWriteRpc<Request, Response>;

AsyncSequencer<bool> sequencer;
auto mock_stream = std::make_unique<MockStream>();
EXPECT_CALL(*mock_stream, Cancel).Times(AtLeast(1));
EXPECT_CALL(*mock_stream, Write)
.WillOnce([&](Request const&, grpc::WriteOptions) {
return sequencer.PushBack("StreamWrite");
});
EXPECT_CALL(*mock_stream, Finish).WillOnce([&]() {
return sequencer.PushBack("StreamFinish").then([](future<bool> f) {
if (f.get()) return Status{};
return PermanentError();
});
});

auto hash = std::make_shared<storage::testing::MockHashFunction>();
EXPECT_CALL(*hash, Update(::testing::_, ::testing::An<absl::Cord const&>(),
::testing::_))
.WillRepeatedly(Return(Status{}));

Request initial_request;
initial_request.set_upload_id("test-upload-id");
auto impl = std::make_unique<AsyncWriterConnectionImpl>(
google::cloud::internal::MakeImmutableOptions(TestOptions()),
std::move(initial_request), std::move(mock_stream), std::move(hash),
/*persisted_size=*/0);

MockFactory mock_factory;
EXPECT_CALL(mock_factory, Call).Times(0);
std::unique_ptr<storage::AsyncWriterConnection> connection =
MakeWriterConnectionBuffered(mock_factory.AsStdFunction(),
std::move(impl), TestOptions());

// Buffer is below the high-water mark, so `Write()` returns immediately while
// the underlying `AsyncWriterConnectionImpl::Write()` is still in flight.
future<Status> write = connection->Write(TestPayload(1024));
ASSERT_TRUE(write.is_ready());
EXPECT_STATUS_OK(write.get());

// Pop the pending stream write, then destroy the buffered writer before the
// stream write completes.
std::pair<promise<bool>, std::string> pending_write =
sequencer.PopFrontWithName();
ASSERT_EQ(pending_write.second, "StreamWrite");
connection.reset();

// Completing the pending write after destruction must not use-after-free.
pending_write.first.set_value(write_succeeds);

std::pair<promise<bool>, std::string> pending_finish =
sequencer.PopFrontWithName();
ASSERT_EQ(pending_finish.second, "StreamFinish");
pending_finish.first.set_value(write_succeeds);
}

INSTANTIATE_TEST_SUITE_P(WriteOutcome, WriterConnectionBufferedDestroyTest,
::testing::Bool(),
[](::testing::TestParamInfo<bool> const& info) {
return info.param ? "WriteSucceeds" : "WriteFails";
});

} // namespace
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage_internal
Expand Down
Loading
Loading