Skip to content

Commit

Permalink
#Centipede Change ByteSpan to use std::span.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 590195138
  • Loading branch information
v-ramesh authored and copybara-github committed Dec 14, 2023
1 parent 5ba240c commit 675ad3f
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 82 deletions.
8 changes: 6 additions & 2 deletions centipede/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,11 @@ cc_library(
],
)

# simple definitions only, no code, no deps other than span.
# simple definitions only, no code, no deps.
cc_library(
name = "defs",
hdrs = ["defs.h"],
visibility = PUBLIC_API_VISIBILITY,
deps = ["@com_google_absl//absl/types:span"],
)

# Various utilities.
Expand Down Expand Up @@ -1119,6 +1118,7 @@ cc_library(

cc_library(
name = "test_util",
testonly = True,
srcs = ["test_util.cc"],
hdrs = ["test_util.h"],
deps = [
Expand All @@ -1128,6 +1128,7 @@ cc_library(
":util",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@com_google_googletest//:gtest",
],
)

Expand Down Expand Up @@ -1393,6 +1394,7 @@ cc_test(
name = "execution_metadata_test",
srcs = ["execution_metadata_test.cc"],
deps = [
":defs",
":execution_metadata",
"@com_google_googletest//:gtest_main",
],
Expand All @@ -1402,6 +1404,7 @@ cc_test(
name = "runner_result_test",
srcs = ["runner_result_test.cc"],
deps = [
":defs",
":feature",
":runner_result",
":shared_memory_blob_sequence",
Expand All @@ -1425,6 +1428,7 @@ cc_test(
deps = [
":byte_array_mutator",
":defs",
":test_util",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_googletest//:gtest_main",
],
Expand Down
20 changes: 10 additions & 10 deletions centipede/blob_file_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ void TestOneBlobFile(std::unique_ptr<BlobFileReader> (*ReaderFactory)(),
auto reader = ReaderFactory();
EXPECT_OK(reader->Open(path));
EXPECT_OK(reader->Read(blob));
EXPECT_EQ(input1, blob);
EXPECT_THAT(blob, EqByteSpan(input1));
EXPECT_OK(reader->Read(blob));
EXPECT_EQ(input2, blob);
EXPECT_THAT(blob, EqByteSpan(input2));
EXPECT_EQ(reader->Read(blob), absl::OutOfRangeError("no more blobs"));
EXPECT_OK(reader->Close());
}
Expand All @@ -79,11 +79,11 @@ void TestOneBlobFile(std::unique_ptr<BlobFileReader> (*ReaderFactory)(),
auto reader = ReaderFactory();
EXPECT_OK(reader->Open(path));
EXPECT_OK(reader->Read(blob));
EXPECT_EQ(input1, blob);
EXPECT_THAT(blob, EqByteSpan(input1));
EXPECT_OK(reader->Read(blob));
EXPECT_EQ(input2, blob);
EXPECT_THAT(blob, EqByteSpan(input2));
EXPECT_OK(reader->Read(blob));
EXPECT_EQ(input3, blob);
EXPECT_THAT(blob, EqByteSpan(input3));
EXPECT_EQ(reader->Read(blob), absl::OutOfRangeError("no more blobs"));
EXPECT_OK(reader->Close());
}
Expand All @@ -101,7 +101,7 @@ void TestOneBlobFile(std::unique_ptr<BlobFileReader> (*ReaderFactory)(),
auto reader = ReaderFactory();
EXPECT_OK(reader->Open(path));
EXPECT_OK(reader->Read(blob));
EXPECT_EQ(input4, blob);
EXPECT_THAT(blob, EqByteSpan(input4));
EXPECT_EQ(reader->Read(blob), absl::OutOfRangeError("no more blobs"));
EXPECT_OK(reader->Close());
}
Expand Down Expand Up @@ -134,7 +134,7 @@ TEST_P(BlobFile, AfterFailedOpenTest) {
// are as expected.
ASSERT_OK(reader->Open(path));
ASSERT_OK(reader->Read(blob));
EXPECT_EQ(input, blob);
EXPECT_THAT(blob, EqByteSpan(input));
EXPECT_EQ(reader->Read(blob), absl::OutOfRangeError("no more blobs"));
EXPECT_OK(reader->Close());
}
Expand Down Expand Up @@ -214,7 +214,7 @@ TEST_P(ReadMultipleFiles, SingleObjectMultipleFormats) {
EXPECT_OK(writer->Close());
EXPECT_OK(reader->Open(path));
EXPECT_OK(reader->Read(blob));
EXPECT_EQ(file1_blob1, blob);
EXPECT_THAT(blob, EqByteSpan(file1_blob1));
EXPECT_EQ(reader->Read(blob), absl::OutOfRangeError("no more blobs"));
EXPECT_OK(reader->Close());

Expand All @@ -226,9 +226,9 @@ TEST_P(ReadMultipleFiles, SingleObjectMultipleFormats) {
EXPECT_OK(writer->Close());
EXPECT_OK(reader->Open(path));
EXPECT_OK(reader->Read(blob));
EXPECT_EQ(file2_blob1, blob);
EXPECT_THAT(blob, EqByteSpan(file2_blob1));
EXPECT_OK(reader->Read(blob));
EXPECT_EQ(file2_blob2, blob);
EXPECT_THAT(blob, EqByteSpan(file2_blob2));
EXPECT_EQ(reader->Read(blob), absl::OutOfRangeError("no more blobs"));
EXPECT_OK(reader->Close());
}
Expand Down
80 changes: 43 additions & 37 deletions centipede/byte_array_mutator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "gtest/gtest.h"
#include "absl/container/flat_hash_set.h"
#include "./centipede/defs.h"
#include "./centipede/test_util.h"

namespace centipede {

Expand Down Expand Up @@ -87,6 +88,9 @@ TEST(ByteArrayMutator, RoundDownToRemoveCorrectly) {

namespace {

using ::testing::ElementsAre;
using ::testing::UnorderedElementsAre;

TEST(DictEntry, DictEntry) {
uint8_t bytes[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
DictEntry a_0_10({bytes + 0, 10});
Expand Down Expand Up @@ -126,27 +130,27 @@ TEST(CmpDictionary, CmpDictionary) {
std::vector<ByteSpan> suggestions;
suggestions.reserve(5);

dict.SuggestReplacement({42, 43}, suggestions);
dict.SuggestReplacement(S({42, 43}), suggestions);
EXPECT_TRUE(suggestions.empty());

dict.SuggestReplacement({1, 2, 3}, suggestions);
EXPECT_THAT(suggestions, testing::ElementsAre(S({3, 4})));
dict.SuggestReplacement(S({1, 2, 3}), suggestions);
EXPECT_THAT(suggestions, ElementsAre(EqByteSpan(S({3, 4}))));

dict.SuggestReplacement({5, 6, 7, 8}, suggestions);
EXPECT_THAT(suggestions, testing::ElementsAre(S({8, 9, 10})));
dict.SuggestReplacement(S({5, 6, 7, 8}), suggestions);
EXPECT_THAT(suggestions, ElementsAre(EqByteSpan(S({8, 9, 10}))));

dict.SuggestReplacement({15, 16, 17, 18, 0, 0}, suggestions);
EXPECT_THAT(suggestions, testing::UnorderedElementsAre(S({11, 12, 13, 14}),
S({20, 21, 22})));
dict.SuggestReplacement(S({15, 16, 17, 18, 0, 0}), suggestions);
EXPECT_THAT(suggestions, UnorderedElementsAre(EqByteSpan(S({11, 12, 13, 14})),
EqByteSpan(S({20, 21, 22}))));

dict.SuggestReplacement({15, 16, 20}, suggestions);
EXPECT_THAT(suggestions, testing::UnorderedElementsAre(S({30, 40, 50})));
dict.SuggestReplacement(S({15, 16, 20}), suggestions);
EXPECT_THAT(suggestions, UnorderedElementsAre(EqByteSpan(S({30, 40, 50}))));

// check that we don't exceed capacity.
std::vector<ByteSpan> capacity1;
capacity1.reserve(1);
EXPECT_EQ(capacity1.capacity(), 1);
dict.SuggestReplacement({15, 16, 17, 18, 0, 0}, capacity1);
dict.SuggestReplacement(S({15, 16, 17, 18, 0, 0}), capacity1);
EXPECT_EQ(capacity1.size(), 1);
EXPECT_EQ(capacity1.capacity(), 1);
}
Expand Down Expand Up @@ -599,23 +603,23 @@ TEST(ByteArrayMutator, OverwriteFromDictionary) {
}

TEST(ByteArrayMutator, OverwriteFromCmpDictionary) {
TestMutatorFn(&ByteArrayMutator::OverwriteFromCmpDictionary,
{1, 2, 40, 50, 60},
/*expected_mutants=*/
{
{3, 4, 40, 50, 60},
{1, 2, 10, 20, 30},
},
/*unexpected_mutants=*/
{
{3, 4, 10, 20, 30},
},
/*size_alignment=*/1,
/*max_len=*/std::numeric_limits<size_t>::max(),
/*dictionary=*/
{},
/*cmp_data=*/
{/*args1*/ 2, 1, 2, 3, 4, /*args2*/ 3, 10, 20, 30, 40, 50, 60});
TestMutatorFn(
&ByteArrayMutator::OverwriteFromCmpDictionary, {1, 2, 40, 50, 60},
/*expected_mutants=*/
{
{3, 4, 40, 50, 60},
{1, 2, 10, 20, 30},
},
/*unexpected_mutants=*/
{
{3, 4, 10, 20, 30},
},
/*size_alignment=*/1,
/*max_len=*/std::numeric_limits<size_t>::max(),
/*dictionary=*/
{},
/*cmp_data=*/
ByteSpan({/*args1*/ 2, 1, 2, 3, 4, /*args2*/ 3, 10, 20, 30, 40, 50, 60}));
}

TEST(ByteArrayMutator, OverwriteFromCmpDictionaryAndSkipLongEntry) {
Expand All @@ -632,15 +636,17 @@ TEST(ByteArrayMutator, OverwriteFromCmpDictionaryAndSkipLongEntry) {
/*max_len=*/std::numeric_limits<size_t>::max(),
/*dictionary=*/
{},
/*cmp_data=*/
{/*size*/ 20, /*lhs*/ 0, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19,
/*rhs*/ 100, 101, 102, 103, 104, 105, 106,
107, 108, 109, 110, 111, 112, 113,
114, 115, 116, 117, 118, 119,
/*size*/ 4, /*lhs*/ 0, 1, 2, 3, /*rhs*/ 100, 101,
102, 103});
/*cmp_data=*/ByteSpan({/*size*/ 20, /*lhs*/ 0, 1, 2, 3,
4, 5, 6, 7, 8,
9, 10, 11, 12, 13,
14, 15, 16, 17, 18,
19,
/*rhs*/ 100, 101, 102, 103, 104,
105, 106, 107, 108, 109,
110, 111, 112, 113, 114,
115, 116, 117, 118, 119,
/*size*/ 4, /*lhs*/ 0, 1, 2, 3,
/*rhs*/ 100, 101, 102, 103}));
}

TEST(ByteArrayMutator, InsertFromDictionary) {
Expand Down
2 changes: 1 addition & 1 deletion centipede/centipede_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ int ForEachBlob(const Environment &env) {
ByteSpan blob;
while (blob_reader->Read(blob) == absl::OkStatus()) {
ByteArray bytes;
bytes.insert(bytes.begin(), blob.data(), blob.end());
bytes.insert(bytes.begin(), blob.begin(), blob.end());
// TODO(kcc): [impl] add a variant of WriteToLocalFile that accepts Span.
WriteToLocalFile(tmpfile, bytes);
std::string command_line = absl::StrReplaceAll(
Expand Down
6 changes: 2 additions & 4 deletions centipede/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,21 @@
#ifndef THIRD_PARTY_CENTIPEDE_DEFS_H_
#define THIRD_PARTY_CENTIPEDE_DEFS_H_
// Only simple definitions here. No code, no dependencies.
// span.h is an exception as it's header-only and very simple.

#include <cstddef>
#include <cstdint>
#include <random>
#include <span> // NOLINT
#include <string_view>
#include <vector>

#include "absl/types/span.h"

namespace centipede {

// Just a good random number generator.
using Rng = std::mt19937_64;

using ByteArray = std::vector<uint8_t>;
using ByteSpan = absl::Span<const uint8_t>;
using ByteSpan = std::span<const uint8_t>;

inline ByteSpan AsByteSpan(std::string_view str) {
return ByteSpan(reinterpret_cast<const uint8_t *>(str.data()), str.size());
Expand Down
48 changes: 26 additions & 22 deletions centipede/execution_metadata_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,29 @@

#include "./centipede/execution_metadata.h"

#include <utility>
#include <vector>

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "./centipede/defs.h"

namespace centipede {
namespace {

using ::testing::IsEmpty;
using ::testing::UnorderedElementsAreArray;

testing::Matcher<std::vector<std::pair<ByteSpan, ByteSpan>>>
UnorderedElementsAre(
const std::vector<std::pair<ByteArray, ByteArray>>& expected) {
std::vector<testing::Matcher<std::pair<ByteSpan, ByteSpan>>> matchers;
for (const auto& p : expected) {
auto [v1, v2] = p;
matchers.push_back(testing::Pair(testing::ElementsAreArray(v1),
testing::ElementsAreArray(v2)));
}
return testing::UnorderedElementsAreArray(matchers);
}

TEST(ExecutionMetadata, ForEachCmpEntryEnumeratesEntriesInRawBytes) {
ExecutionMetadata metadata{.cmp_data = {
Expand All @@ -38,14 +51,9 @@ TEST(ExecutionMetadata, ForEachCmpEntryEnumeratesEntriesInRawBytes) {
std::vector<std::pair<ByteSpan, ByteSpan>> enumeration_result;
EXPECT_TRUE(metadata.ForEachCmpEntry(
[&](ByteSpan a, ByteSpan b) { enumeration_result.emplace_back(a, b); }));

EXPECT_THAT(
enumeration_result,
UnorderedElementsAreArray(std::vector<std::pair<ByteSpan, ByteSpan>>{
{{1, 2}, {3, 4}},
{{}, {}},
{{5, 6, 7}, {8, 9, 10}},
}));
EXPECT_THAT(enumeration_result,
UnorderedElementsAre(
{{{1, 2}, {3, 4}}, {{}, {}}, {{5, 6, 7}, {8, 9, 10}}}));
}

TEST(ExecutionMetadata, ForEachCmpEntryHandlesEmptyCmpData) {
Expand All @@ -64,21 +72,19 @@ TEST(ExecutionMetadata,

TEST(ExecutionMetadata, ForEachCmpEntryEnumeratesEntriesFromAppendCmpEntry) {
ExecutionMetadata metadata;
ASSERT_TRUE(metadata.AppendCmpEntry({1, 2}, {3, 4}));
ASSERT_TRUE(metadata.AppendCmpEntry(ByteSpan({1, 2}), ByteSpan({3, 4})));
std::vector<std::pair<ByteSpan, ByteSpan>> enumeration_result;
EXPECT_TRUE(metadata.ForEachCmpEntry(
[&](ByteSpan a, ByteSpan b) { enumeration_result.emplace_back(a, b); }));
EXPECT_THAT(
enumeration_result,
UnorderedElementsAreArray(std::vector<std::pair<ByteSpan, ByteSpan>>{
{{1, 2}, {3, 4}},
}));
EXPECT_THAT(enumeration_result, UnorderedElementsAre({
{{1, 2}, {3, 4}},
}));
}

TEST(ExecutionMetadata, AppendCmpEntryReturnsFalseAndSkipsOnBadArgs) {
ExecutionMetadata metadata;
// Sizes don't match.
EXPECT_FALSE(metadata.AppendCmpEntry({}, {1}));
EXPECT_FALSE(metadata.AppendCmpEntry(ByteSpan({}), ByteSpan({1})));
ByteArray long_byte_array;
long_byte_array.resize(256);
// Args too long.
Expand All @@ -92,7 +98,7 @@ TEST(ExecutionMetadata, AppendCmpEntryReturnsFalseAndSkipsOnBadArgs) {

TEST(ExecutionMetadata, ReadAndWriteKeepsCmpEntries) {
ExecutionMetadata metadata_in;
ASSERT_TRUE(metadata_in.AppendCmpEntry({1, 2}, {3, 4}));
ASSERT_TRUE(metadata_in.AppendCmpEntry(ByteSpan({1, 2}), ByteSpan({3, 4})));
SharedMemoryBlobSequence blobseq("test", /*size=*/1024,
/*use_posix_shmem=*/false);
EXPECT_TRUE(metadata_in.Write(/*tag=*/1, blobseq));
Expand All @@ -103,11 +109,9 @@ TEST(ExecutionMetadata, ReadAndWriteKeepsCmpEntries) {
std::vector<std::pair<ByteSpan, ByteSpan>> enumeration_result;
EXPECT_TRUE(metadata_out.ForEachCmpEntry(
[&](ByteSpan a, ByteSpan b) { enumeration_result.emplace_back(a, b); }));
EXPECT_THAT(
enumeration_result,
UnorderedElementsAreArray(std::vector<std::pair<ByteSpan, ByteSpan>>{
{{1, 2}, {3, 4}},
}));
EXPECT_THAT(enumeration_result, UnorderedElementsAre({
{{1, 2}, {3, 4}},
}));
}

} // namespace
Expand Down
5 changes: 5 additions & 0 deletions centipede/hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <cstddef>
#include <cstdint>
#include <initializer_list>
#include <string>
#include <string_view>

Expand All @@ -40,6 +41,10 @@ std::string Hash(ByteSpan span) {
return {sha1_hex_text, sha1_hex_text + kHashLen};
}

std::string Hash(std::initializer_list<uint8_t> il) {
return Hash(ByteSpan(il));
}

std::string Hash(std::string_view str) {
static_assert(sizeof(decltype(str)::value_type) == sizeof(uint8_t));
return Hash(AsByteSpan(str));
Expand Down
Loading

0 comments on commit 675ad3f

Please sign in to comment.