From 469d158c6d37ef0e02598589b6fd32e37687b635 Mon Sep 17 00:00:00 2001 From: Ram Muthiah Date: Tue, 17 Nov 2026 17:59:15 +0000 Subject: [PATCH] cvd: Acquire exclusive flock in DataViewer::WithExclusiveLock `DataViewer::WithExclusiveLock` passed `LOCK_SH` instead of `LOCK_EX` to `LockBackingFile`, allowing concurrent `cvd` writers to hold shared locks simultaneously while truncating (`ftruncate(0)`) and rewriting `instance_database.binpb`, which could interleave writes and corrupt the serialized `cvd::PersistentData` protobuf. Acquire `LOCK_EX` in `WithExclusiveLock`, pass `O_CLOEXEC` when opening the backing file so child daemons do not inherit an open database file descriptor across `exec`, centralize the signal-masked overwrite helper in `DataViewer::StoreData`, and add `WithConditionalExclusiveLock` so read-modify-write callers that leave `data` unchanged can skip truncating and rewriting the backing file. Add `data_viewer_test` (`instances/BUILD.bazel`) to verify `LOCK_EX` mutual exclusion, concurrent writer serialization, and no-op conditional locks. Bug: b/554100008 Bug: b/586523782 Test: bazel test //cuttlefish/host/commands/cvd/instances/... --- .../host/commands/cvd/instances/BUILD.bazel | 13 ++ .../commands/cvd/instances/data_viewer.cpp | 17 +- .../host/commands/cvd/instances/data_viewer.h | 30 ++- .../cvd/instances/data_viewer_test.cpp | 182 ++++++++++++++++++ 4 files changed, 231 insertions(+), 11 deletions(-) create mode 100644 base/cvd/cuttlefish/host/commands/cvd/instances/data_viewer_test.cpp diff --git a/base/cvd/cuttlefish/host/commands/cvd/instances/BUILD.bazel b/base/cvd/cuttlefish/host/commands/cvd/instances/BUILD.bazel index 3f2384370c4..9376ce52c3b 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/instances/BUILD.bazel +++ b/base/cvd/cuttlefish/host/commands/cvd/instances/BUILD.bazel @@ -47,6 +47,19 @@ cf_cc_library( ], ) +cf_cc_test( + name = "data_viewer_test", + srcs = ["data_viewer_test.cpp"], + deps = [ + ":cvd_persistent_data", + ":data_viewer", + "//cuttlefish/common/libs/fs:fd", + "//cuttlefish/result", + "//cuttlefish/result:result_matchers", + "@abseil-cpp//absl/strings", + ], +) + cf_cc_library( name = "device_name", srcs = ["device_name.cpp"], diff --git a/base/cvd/cuttlefish/host/commands/cvd/instances/data_viewer.cpp b/base/cvd/cuttlefish/host/commands/cvd/instances/data_viewer.cpp index e7dcbcff133..b452d911e29 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/instances/data_viewer.cpp +++ b/base/cvd/cuttlefish/host/commands/cvd/instances/data_viewer.cpp @@ -16,6 +16,10 @@ #include "cuttlefish/host/commands/cvd/instances/data_viewer.h" +#include +#include +#include + #include #include @@ -23,13 +27,15 @@ #include "cuttlefish/common/libs/fs/fd.h" #include "cuttlefish/common/libs/fs/shared_fd.h" +#include "cuttlefish/common/libs/utils/signals.h" #include "cuttlefish/io/string.h" #include "cuttlefish/io/write_exact.h" namespace cuttlefish { Result DataViewer::LockBackingFile(int op) const { - Fd fd = CF_EXPECT(Fd::Open(backing_file_, O_CREAT | O_RDWR, 0640)); + Fd fd = + CF_EXPECT(Fd::Open(backing_file_, O_CREAT | O_RDWR | O_CLOEXEC, 0640)); CF_EXPECTF(fd.Flock(op), "Failed to acquire lock for instance database backing file: {}", fd.StrError()); @@ -48,6 +54,15 @@ Result DataViewer::StoreData(SharedFD fd, const cvd::PersistentData& data) { std::string str; CF_EXPECT(data.SerializeToString(&str), "Failed to serialize data"); + // Block signals while writing to the instance database file. This reduces + // the chances of corrupting the file. + sigset_t all_signals; + sigfillset(&all_signals); + SignalMasker blocker(all_signals); + // Overwrite the file contents, don't append. + CF_EXPECTF(fd->Truncate(0), "Failed to truncate fd: {}", fd->StrError()); + CF_EXPECTF(fd->LSeek(0, SEEK_SET) >= 0, "Failed to seek to 0: {}", + fd->StrError()); CF_EXPECT(WriteExact(*fd, str), "Failed to write to backing file"); return {}; } diff --git a/base/cvd/cuttlefish/host/commands/cvd/instances/data_viewer.h b/base/cvd/cuttlefish/host/commands/cvd/instances/data_viewer.h index 7982a4a02dc..06c36e8dd07 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/instances/data_viewer.h +++ b/base/cvd/cuttlefish/host/commands/cvd/instances/data_viewer.h @@ -23,6 +23,7 @@ #include #include #include +#include #include "cuttlefish/common/libs/fs/shared_fd.h" #include "cuttlefish/common/libs/utils/signals.h" @@ -69,26 +70,35 @@ class DataViewer { Result WithExclusiveLock( std::function(cvd::PersistentData&)> task) { DeadlockProtector dp(*this); - auto fd = CF_EXPECT(LockBackingFile(LOCK_SH)); + auto fd = CF_EXPECT(LockBackingFile(LOCK_EX)); auto data = CF_EXPECT(LoadData(fd)); auto res = task(data); if (!res.has_value()) { // Don't update if there is an error return res; } - // Block signals while writing to the instance database file. This reduces - // the chances of corrupting the file. - sigset_t all_signals; - sigfillset(&all_signals); - SignalMasker blocker(all_signals); - // Overwrite the file contents, don't append - CF_EXPECTF(fd->Truncate(0), "Failed to truncate fd: {}", fd->StrError()); - CF_EXPECTF(fd->LSeek(0, SEEK_SET) >= 0, "Failed to seek to 0: {}", - fd->StrError()); CF_EXPECT(StoreData(fd, data)); return res; } + /** + * Like `WithExclusiveLock`, except the backing file is only truncated and + * rewritten when the second element of the returned pair is `true` + * (indicating `data` was modified). + */ + template + Result WithConditionalExclusiveLock( + std::function>(cvd::PersistentData&)> task) { + DeadlockProtector dp(*this); + auto fd = CF_EXPECT(LockBackingFile(LOCK_EX)); + auto data = CF_EXPECT(LoadData(fd)); + auto [value, modified] = CF_EXPECT(task(data)); + if (modified) { + CF_EXPECT(StoreData(fd, data)); + } + return std::move(value); + } + private: // Opens and locks the backing file. The lock will be dropped when the file // descriptor closes. diff --git a/base/cvd/cuttlefish/host/commands/cvd/instances/data_viewer_test.cpp b/base/cvd/cuttlefish/host/commands/cvd/instances/data_viewer_test.cpp new file mode 100644 index 00000000000..988284fa539 --- /dev/null +++ b/base/cvd/cuttlefish/host/commands/cvd/instances/data_viewer_test.cpp @@ -0,0 +1,182 @@ +/* + * Copyright (C) 2026 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "cuttlefish/host/commands/cvd/instances/data_viewer.h" + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "absl/strings/str_cat.h" +#include "gmock/gmock-matchers.h" +#include "gtest/gtest.h" + +#include "cuttlefish/common/libs/fs/fd.h" +#include "cuttlefish/host/commands/cvd/instances/cvd_persistent_data.pb.h" +#include "cuttlefish/result/result.h" +#include "cuttlefish/result/result_matchers.h" + +namespace cuttlefish { +namespace { + +class TempDbFile { + public: + TempDbFile() { + const char* tmpdir = getenv("TEST_TMPDIR"); + std::string base = + (tmpdir != nullptr && tmpdir[0] != '\0') ? tmpdir : "/tmp"; + std::string tmpl = absl::StrCat(base, "/cf_data_viewer_test.XXXXXX"); + int fd = mkstemp(tmpl.data()); + if (fd >= 0) { + close(fd); + path_ = tmpl; + } + } + + ~TempDbFile() { + if (!path_.empty()) { + unlink(path_.c_str()); + } + } + + const std::string& Path() const { return path_; } + + private: + std::string path_; +}; + +TEST(DataViewerTest, WithExclusiveLockAcquiresExclusiveFlock) { + TempDbFile db_file; + ASSERT_FALSE(db_file.Path().empty()); + DataViewer viewer(db_file.Path()); + + Result res = viewer.WithExclusiveLock( + [&db_file](cvd::PersistentData& data) -> Result { + // An independent file description must fail `LOCK_SH | LOCK_NB` while + // `WithExclusiveLock` holds `LOCK_EX`. + Fd probe = CF_EXPECT(Fd::Open(db_file.Path(), O_RDONLY | O_CLOEXEC)); + Result probe_lock = probe.Flock(LOCK_SH | LOCK_NB); + CF_EXPECT(!probe_lock.has_value(), + "Expected LOCK_SH | LOCK_NB to block against LOCK_EX"); + CF_EXPECT(*probe_lock.error() == EWOULDBLOCK || + *probe_lock.error() == EAGAIN); + data.add_instance_groups()->set_name("grp_1"); + return {}; + }); + ASSERT_THAT(res, IsOk()); + + Result count = viewer.WithSharedLock( + [](const cvd::PersistentData& data) -> Result { + return data.instance_groups_size(); + }); + ASSERT_THAT(count, IsOkAndValue(1)); +} + +TEST(DataViewerTest, ConcurrentWritersPreserveAllUpdates) { + TempDbFile db_file; + ASSERT_FALSE(db_file.Path().empty()); + + constexpr int kNumThreads = 8; + constexpr int kIterationsPerThread = 25; + std::vector threads; + threads.reserve(kNumThreads); + for (int t = 0; t < kNumThreads; ++t) { + threads.emplace_back([&db_file, t]() { + DataViewer viewer(db_file.Path()); + for (int i = 0; i < kIterationsPerThread; ++i) { + Result res = viewer.WithExclusiveLock( + [t, i](cvd::PersistentData& data) -> Result { + auto* grp = data.add_instance_groups(); + grp->set_name(absl::StrCat("grp_", t, "_", i)); + grp->set_home_directory(absl::StrCat("/tmp/home_", t, "_", i)); + return {}; + }); + EXPECT_THAT(res, IsOk()); + } + }); + } + for (std::thread& th : threads) { + th.join(); + } + + DataViewer reader(db_file.Path()); + Result total = reader.WithSharedLock( + [](const cvd::PersistentData& data) -> Result { + return data.instance_groups_size(); + }); + ASSERT_THAT(total, IsOkAndValue(kNumThreads * kIterationsPerThread)); +} + +TEST(DataViewerTest, WithConditionalExclusiveLockSkipsRewriteWhenUnmodified) { + TempDbFile db_file; + ASSERT_FALSE(db_file.Path().empty()); + DataViewer viewer(db_file.Path()); + + ASSERT_THAT(viewer.WithExclusiveLock( + [](cvd::PersistentData& data) -> Result { + data.add_instance_groups()->set_name("grp_initial"); + return {}; + }), + IsOk()); + + // Make the backing file's mtime in the past and confirm an unmodified + // `WithConditionalExclusiveLock` does not truncate or rewrite it. + struct timespec past[2] = {{.tv_sec = 1000000, .tv_nsec = 0}, + {.tv_sec = 1000000, .tv_nsec = 0}}; + ASSERT_EQ(utimensat(AT_FDCWD, db_file.Path().c_str(), past, 0), 0); + + Result noop = viewer.WithConditionalExclusiveLock( + [](cvd::PersistentData& data) -> Result> { + data.add_instance_groups()->set_name("unpersisted"); + return std::make_pair(42, false); + }); + ASSERT_THAT(noop, IsOkAndValue(42)); + + struct stat st{}; + ASSERT_EQ(stat(db_file.Path().c_str(), &st), 0); + EXPECT_EQ(st.st_mtim.tv_sec, 1000000); + + Result groups_after_noop = viewer.WithSharedLock( + [](const cvd::PersistentData& data) -> Result { + return data.instance_groups_size(); + }); + ASSERT_THAT(groups_after_noop, IsOkAndValue(1)); + + Result modified = viewer.WithConditionalExclusiveLock( + [](cvd::PersistentData& data) -> Result> { + data.add_instance_groups()->set_name("grp_second"); + return std::make_pair(99, true); + }); + ASSERT_THAT(modified, IsOkAndValue(99)); + + Result groups_after_mod = viewer.WithSharedLock( + [](const cvd::PersistentData& data) -> Result { + return data.instance_groups_size(); + }); + ASSERT_THAT(groups_after_mod, IsOkAndValue(2)); +} + +} // namespace +} // namespace cuttlefish