Commit a6543055 authored by Kevin Marshall's avatar Kevin Marshall Committed by Commit Bot

[legacymetrics] Fix use-after-free in LegacyMetricsClient.

Handles the crasher in which the metrics recorder channel is
disconnected while additional metrics are being gathered.

Test: cr_fuchsia_base_unittests
Bug: 1147146
Change-Id: Iff987933233bd1565ed052b1ca1e1225b30e782d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2538331
Commit-Queue: Kevin Marshall <kmarshall@chromium.org>
Reviewed-by: default avatarWez <wez@chromium.org>
Cr-Commit-Position: refs/heads/master@{#828845}
parent 38b7bc9f
......@@ -85,9 +85,13 @@ void LegacyMetricsClient::StartReport() {
void LegacyMetricsClient::Report(
std::vector<fuchsia::legacymetrics::Event> events) {
DCHECK(metrics_recorder_);
DVLOG(1) << __func__ << " called.";
// The connection might have dropped while additional metrics were being
// collected.
if (!metrics_recorder_)
return;
// Include histograms.
for (auto& histogram : GetLegacyMetricsDeltas()) {
fuchsia::legacymetrics::Event histogram_event;
......
......@@ -148,6 +148,31 @@ TEST_F(LegacyMetricsClientTest, AllTypes) {
EXPECT_EQ("bar", events[2].user_action_event().name());
}
TEST_F(LegacyMetricsClientTest, DisconnectWhileCollectingAdditionalEvents) {
// Hold the completion callback for later execution.
base::OnceCallback<void(std::vector<fuchsia::legacymetrics::Event>)>
on_report_done;
client_.SetReportAdditionalMetricsCallback(base::BindRepeating(
[](base::OnceCallback<void(std::vector<fuchsia::legacymetrics::Event>)>*
stored_on_report_done,
base::OnceCallback<void(std::vector<fuchsia::legacymetrics::Event>)>
on_report_done) {
*stored_on_report_done = std::move(on_report_done);
},
base::Unretained(&on_report_done)));
client_.Start(kReportInterval);
task_environment_.FastForwardBy(kReportInterval);
// Disconnect the service.
service_binding_.reset();
base::RunLoop().RunUntilIdle();
// Fulfill the report additional metrics callback.
std::move(on_report_done).Run({});
}
TEST_F(LegacyMetricsClientTest, ReportSkippedNoEvents) {
client_.Start(kReportInterval);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment