Skip to content

Commit

Permalink
#Centipede MacOS support 5/X: fix shared_memory_blob_sequence.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 680725159
  • Loading branch information
xinhaoyuan authored and copybara-github committed Sep 30, 2024
1 parent 53e04d4 commit 7b8958d
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 13 deletions.
18 changes: 12 additions & 6 deletions .github/workflows/bazel_test_centipede.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,25 @@ jobs:
if: ${{ !cancelled() }}
run: |
bazel test --local_test_jobs=1 --test_output=errors --no//fuzztest:use_riegeli \
centipede:binary_info_test centipede:call_graph_test \
centipede:callstack_test centipede:concurrent_bitset_test \
centipede:binary_info_test centipede:byte_array_mutator_test \
centipede:call_graph_test centipede:callstack_test \
centipede:centipede_binary_test centipede:concurrent_bitset_test \
centipede:command_test \
centipede:concurrent_byteset_test centipede:config_file_test \
centipede:config_util_test centipede:environment_test \
centipede:config_util_test centipede:corpus_io_test \
centipede:corpus_test centipede:distill_test \
centipede:environment_test centipede:execution_metadata_test \
centipede:feature_set_test centipede:feature_test \
centipede:foreach_nonzero_test \
centipede:hashed_ring_buffer_test centipede:int_utils_test \
centipede:knobs_test centipede:pc_info_test \
centipede:resource_pool_test \
centipede:knobs_test centipede:mutation_input_test \
centipede:pc_info_test centipede:resource_pool_test \
centipede:reverse_pc_table_test centipede:rolling_hash_test \
centipede:runner_cmp_trace_test centipede:runner_flags_test \
centipede:rusage_stats_test \
centipede:runner_result_test centipede:rusage_stats_test \
centipede:seed_corpus_maker_lib_test \
centipede:seed_corpus_maker_proto_lib_test \
centipede:shared_memory_blob_sequence_test \
centipede:stats_test centipede:symbol_table_test \
centipede:util_test centipede:workdir_test && \
bazel test --local_test_jobs=1 --test_output=errors --no//fuzztest:use_riegeli --test_filter='-*ValidateTimelapseSnapshots' -- centipede:rusage_profiler_test
Expand Down
7 changes: 6 additions & 1 deletion centipede/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,12 @@ cc_library(
name = "shared_memory_blob_sequence",
srcs = ["shared_memory_blob_sequence.cc"],
hdrs = ["shared_memory_blob_sequence.h"],
linkopts = ["-lrt"], # for shm_open.
linkopts = select({
"@platforms//os:macos": [],
"//conditions:default": [
"-lrt", # for shm_open
],
}),
visibility = PUBLIC_API_VISIBILITY,
deps = ["@com_google_absl//absl/base:nullability"],
# don't add any dependencies.
Expand Down
4 changes: 4 additions & 0 deletions centipede/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ struct Environment {
std::string minimize_crash_file_path;
bool batch_triage_suspect_only = false;
size_t shmem_size_mb = 1024;
#ifdef __APPLE__
bool use_posix_shmem = true;
#else
bool use_posix_shmem = false;
#endif
bool dry_run = false;
bool save_binary_info = false;
bool populate_binary_info = true;
Expand Down
6 changes: 4 additions & 2 deletions centipede/execution_metadata_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "./centipede/execution_metadata.h"

#include <cstdint>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -96,8 +97,9 @@ TEST(ExecutionMetadata, AppendCmpEntryReturnsFalseAndSkipsOnBadArgs) {
TEST(ExecutionMetadata, ReadAndWriteKeepsCmpEntries) {
ExecutionMetadata metadata_in;
ASSERT_TRUE(metadata_in.AppendCmpEntry({1, 2}, {3, 4}));
SharedMemoryBlobSequence blobseq("test", /*size=*/1024,
/*use_posix_shmem=*/false);
std::vector<uint8_t> blob_storage;
blob_storage.resize(1024);
BlobSequence blobseq(blob_storage.data(), blob_storage.size());
EXPECT_TRUE(metadata_in.Write(/*tag=*/1, blobseq));
blobseq.Reset();
Blob blob = blobseq.Read();
Expand Down
12 changes: 11 additions & 1 deletion centipede/shared_memory_blob_sequence.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ SharedMemoryBlobSequence::SharedMemoryBlobSequence(const char *name,
"shm_open() path length exceeds PATH_MAX.");
path_is_owned_ = true;
} else {
#ifdef __APPLE__
ErrorOnFailure(true, "must use POSIX shmem");
#else // __APPLE__
fd_ = memfd_create(name, MFD_CLOEXEC);
ErrorOnFailure(fd_ < 0, "memfd_create() failed");
const size_t path_size =
Expand All @@ -116,8 +119,9 @@ SharedMemoryBlobSequence::SharedMemoryBlobSequence(const char *name,
"internal fd path length exceeds PATH_MAX.");
// memfd_create descriptors are automatically freed on close().
path_is_owned_ = false;
#endif // __APPLE__
}
ErrorOnFailure(ftruncate(fd_, static_cast<__off_t>(size_)),
ErrorOnFailure(ftruncate(fd_, static_cast<off_t>(size_)),
"ftruncate() failed)");
MmapData();
}
Expand Down Expand Up @@ -154,10 +158,16 @@ SharedMemoryBlobSequence::~SharedMemoryBlobSequence() {
}

void SharedMemoryBlobSequence::ReleaseSharedMemory() {
#ifdef __APPLE__
// MacOS only allows ftruncate shm once
// (https://stackoverflow.com/questions/25502229/ftruncate-not-working-on-posix-shared-memory-in-mac-os-x).
// So nothing we can do here.
#else // __APPLE__
// Setting size to 0 releases the memory to OS.
ErrorOnFailure(ftruncate(fd_, 0) != 0, "ftruncate(0) failed)");
// Set the size back to `size`. The memory is not actually reserved.
ErrorOnFailure(ftruncate(fd_, size_) != 0, "ftruncate(size_) failed)");
#endif // __APPLE__
}

size_t SharedMemoryBlobSequence::NumBytesUsed() const {
Expand Down
18 changes: 15 additions & 3 deletions centipede/shared_memory_blob_sequence_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ namespace centipede {

std::string ShmemName() {
std::ostringstream oss;
oss << "/shared_memory_blob_sequence_test-" << getpid() << "-"
<< std::this_thread::get_id();
oss << "/shm_test-" << getpid() << "-" << std::this_thread::get_id();
return oss.str();
}

Expand All @@ -54,7 +53,17 @@ TEST(BlobSequence, WriteAndReadAnEmptyBlob) {
}

class SharedMemoryBlobSequenceTest
: public testing::TestWithParam</* use_shm */ bool> {};
: public testing::TestWithParam</* use_shm */ bool> {
public:
void SetUp() override {
#ifdef __APPLE__
const bool use_shm = GetParam();
if (!use_shm) {
GTEST_SKIP() << "Skipping test that does not use POSIX shmem on MacOS";
}
#endif // __APPLE__
}
};

INSTANTIATE_TEST_SUITE_P(SharedMemoryBlobSequenceParametrizedTest,
SharedMemoryBlobSequenceTest,
Expand Down Expand Up @@ -195,6 +204,8 @@ TEST_P(SharedMemoryBlobSequenceTest, WriteAfterReset) {
EXPECT_FALSE(blob2.IsValid());
}

// MacOS does not support releasing the shm memory.
#ifndef __APPLE__
// Test ReleaseSharedMemory and NumBytesUsed.
TEST_P(SharedMemoryBlobSequenceTest, ReleaseSharedMemory) {
// Allocate a blob sequence with 1M bytes of storage.
Expand All @@ -207,5 +218,6 @@ TEST_P(SharedMemoryBlobSequenceTest, ReleaseSharedMemory) {
EXPECT_TRUE(blobseq.Write(Blob({1, 2, 3, 4})));
EXPECT_GT(blobseq.NumBytesUsed(), 5);
}
#endif

} // namespace centipede

0 comments on commit 7b8958d

Please sign in to comment.