Skip to content

Commit

Permalink
[fuzzer] Refactor Fuzzer to use new reference information. (#619)
Browse files Browse the repository at this point in the history

Co-authored-by: kishanps <[email protected]>
  • Loading branch information
VSuryaprasad-HCL and kishanps authored Oct 19, 2024
1 parent 56c7171 commit 079649d
Show file tree
Hide file tree
Showing 21 changed files with 1,363 additions and 845 deletions.
108 changes: 80 additions & 28 deletions p4_fuzzer/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,23 @@ cc_library(
name = "switch_state",
srcs = ["switch_state.cc"],
hdrs = ["switch_state.h"],
deps = [
"//p4_pdpi:entity_keys",
deps = [
"//gutil:collections",
"//gutil:proto",
"//gutil:status",
"//p4_pdpi:entity_keys",
"//p4_pdpi:ir",
"//p4_pdpi:ir_cc_proto",
"//p4_pdpi:translation_options",
"//p4_pdpi/internal:ordered_map",
"@com_github_google_glog//:glog",
"@com_github_p4lang_p4runtime//:p4info_cc_proto",
"@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
"@com_google_absl//absl/algorithm:container",
"@com_google_absl//absl/container:btree",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/types:optional",
Expand All @@ -51,66 +52,87 @@ cc_library(
cc_test(
name = "switch_state_test",
srcs = ["switch_state_test.cc"],
deps = [
deps = [
":switch_state",
":test_utils",
"//gutil:collections",
"//gutil:proto",
"//gutil:proto_matchers",
"//gutil:status",
"//gutil:status_matchers",
"//gutil:testing",
"//p4_pdpi:ir",
"//p4_pdpi:ir_cc_proto",
"//p4_pdpi:pd",
"//p4_pdpi/testing:main_p4_pd_cc_proto",
"//p4_pdpi/testing:test_p4info_cc",
"//p4_pdpi:pd",
"@com_github_google_glog//:glog",
"@com_github_p4lang_p4runtime//:p4info_cc_proto",
"@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:optional",
"@com_google_absl//absl/types:span",
"@com_google_googletest//:gtest_main",
],
)

cc_library(
name = "fuzzer_config",
srcs = [
"fuzzer_config.cc",
],
hdrs = [
"fuzzer_config.h",
],
deps = [
"//gutil:status",
"//lib/p4rt:p4rt_port",
"//p4_pdpi:ir",
"//p4_pdpi:ir_cc_proto",
"//p4_pdpi:reference_annotations",
"@com_github_p4lang_p4_constraints//p4_constraints/backend:constraint_info",
"@com_github_p4lang_p4runtime//:p4info_cc_proto",
"@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
"@com_google_absl//absl/container:btree",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_protobuf//:protobuf",
],
)

cc_library(
name = "mutation_and_fuzz_util",
srcs = [
srcs = [
"constraints.cc",
"fuzz_util.cc",
"mutation.cc",
],
hdrs = [
],
hdrs = [
"constraints.h",
"fuzz_util.h",
"mutation.h",
],
deps = [
],
deps = [
":annotation_util",
":fuzzer_cc_proto",
":fuzzer_config",
":switch_state",
"//gutil:collections",
"//gutil:status",
"//lib/p4rt:p4rt_port",
"//p4_pdpi:entity_keys",
"//p4_pdpi:ir_cc_proto",
"//p4_pdpi:references",
"//p4_pdpi/internal:ordered_map",
"//p4_pdpi/netaddr:ipv6_address",
"//p4_pdpi/utils:ir",
"@com_github_google_glog//:glog",
"@com_github_p4lang_p4_constraints//p4_constraints/backend:constraint_info",
"@com_github_p4lang_p4_constraints//p4_constraints/backend:symbolic_interpreter",
"@com_github_p4lang_p4runtime//:p4info_cc_proto",
"@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
"@com_github_p4lang_p4runtime//:p4types_cc_proto",
"@com_github_z3prover_z3//:api",
"@com_google_absl//absl/algorithm:container",
"@com_google_absl//absl/base",
"@com_google_absl//absl/base:endian",
Expand All @@ -123,7 +145,7 @@ cc_library(
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:optional",
"@com_google_absl//absl/types:span",
"@com_google_protobuf//:protobuf_lite",
"@com_google_protobuf//:protobuf",
],
)

Expand All @@ -135,12 +157,14 @@ cc_test(
data = ["acl_table_test.p4info.pb.txt"],
deps = [
":fuzzer_cc_proto",
":fuzzer_config",
":mutation_and_fuzz_util",
":test_utils",
"//gutil:collections",
"//gutil:proto_matchers",
"//gutil:status_matchers",
"//p4_pdpi:ir_cc_proto",
"@com_github_google_glog//:glog",
"@com_github_p4lang_p4runtime//:p4info_cc_proto",
"@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
"@com_google_absl//absl/container:flat_hash_set",
Expand All @@ -149,6 +173,7 @@ cc_test(
"@com_google_absl//absl/random:seed_sequences",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest_main",
],
)
Expand All @@ -159,25 +184,29 @@ cc_library(
hdrs = ["annotation_util.h"],
deps = [
":fuzzer_cc_proto",
"//gutil:status",
"//p4_pdpi:ir",
"//p4_pdpi:ir_cc_proto",
"//p4_pdpi/utils:ir",
"//thinkit:test_environment",
"@com_github_google_glog//:glog",
"@com_github_p4lang_p4runtime//:p4info_cc_proto",
"@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
],
)

cc_library(
name = "oracle_util",
srcs = [
srcs = [
"oracle_util.cc",
],
],
hdrs = ["oracle_util.h"],
deps = [
":fuzzer_cc_proto",
":switch_state",
"//p4_pdpi:entity_keys",
"//gutil:status",
"//p4_pdpi:entity_keys",
"//p4_pdpi:ir",
"//p4_pdpi:ir_cc_proto",
"@com_github_p4lang_p4runtime//:p4info_cc_proto",
Expand All @@ -187,7 +216,7 @@ cc_library(
"@com_google_absl//absl/status",
"@com_google_absl//absl/types:optional",
"@com_google_absl//absl/types:span",
],
],
)

cc_test(
Expand All @@ -197,15 +226,15 @@ cc_test(
":fuzzer_cc_proto",
":mutation_and_fuzz_util",
":oracle_util",
":switch_state",
":test_utils",
"//gutil:collections",
"//gutil:status",
"//gutil:status_matchers",
"//gutil:testing",
"//p4_pdpi:ir",
"//p4_pdpi:ir_cc_proto",
"//p4_pdpi:pd",
"//p4_pdpi/netaddr:ipv4_address",
"//sai_p4/instantiations/google:instantiations",
"//sai_p4/instantiations/google:sai_p4info_cc",
"//sai_p4/instantiations/google:sai_pd_cc_proto",
"@com_github_p4lang_p4runtime//:p4info_cc_proto",
Expand All @@ -224,7 +253,6 @@ cc_library(
hdrs = ["constraints_util.h"],
deps = [
"//gutil:status",
"@com_github_google_glog//:glog",
"@com_github_ivmai_cudd//:cudd",
"@com_github_p4lang_p4_constraints//p4_constraints:ast_cc_proto",
"@com_github_p4lang_p4_constraints//p4_constraints/backend:constraint_info",
Expand All @@ -251,8 +279,8 @@ cc_test(
"//gutil:proto",
"@com_github_google_glog//:glog",
"@com_github_p4lang_p4_constraints//p4_constraints/backend:constraint_info",
"@com_github_p4lang_p4_constraints//p4_constraints/backend:interpreter",
"@com_github_p4lang_p4runtime//:p4info_cc_proto",
"@com_google_absl//absl/types:optional",
"@com_google_googletest//:gtest_main",
],
)
Expand All @@ -264,11 +292,8 @@ cc_library(
hdrs = ["test_utils.h"],
deps = [
":fuzzer_config",
":mutation_and_fuzz_util",
":switch_state",
"//gutil:collections",
"//gutil:testing",
"//p4_pdpi:ir",
"//lib/p4rt:p4rt_port",
"//p4_pdpi:ir_cc_proto",
"//p4_pdpi/internal:ordered_map",
"//p4_pdpi/testing:test_p4info_cc",
Expand All @@ -282,6 +307,33 @@ cc_library(
],
)

cc_test(
name = "constraints_test",
srcs = ["constraints_test.cc"],
deps = [
":fuzzer_config",
":mutation_and_fuzz_util",
":switch_state",
":test_utils",
"//gutil:collections",
"//gutil:status",
"//gutil:status_matchers",
"//gutil:testing",
"//p4_pdpi:ir",
"//p4_pdpi:ir_cc_proto",
"@com_github_google_glog//:glog",
"@com_github_p4lang_p4_constraints//p4_constraints:ast_cc_proto",
"@com_github_p4lang_p4_constraints//p4_constraints/backend:constraint_info",
"@com_github_p4lang_p4_constraints//p4_constraints/backend:interpreter",
"@com_github_p4lang_p4runtime//:p4info_cc_proto",
"@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
"@com_google_absl//absl/random",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest_main",
],
)

# -- P4 libraries for testing -------------------------------------------------

p4_library(
Expand Down
24 changes: 11 additions & 13 deletions p4_fuzzer/constraints.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

#include <functional>
#include <string>
#include <utility>

#include "absl/container/flat_hash_map.h"
#include "absl/random/random.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
Expand Down Expand Up @@ -53,11 +51,10 @@ absl::StatusOr<bool> HasP4RuntimeTranslatedType(
} // namespace

bool UsesP4Constraints(int table_id, const FuzzerConfig& config) {
auto it = config.GetConstraintInfo().find(table_id);
if (it == config.GetConstraintInfo().end()) {
return false;
}
return it->second.constraint.has_value();
auto* table_info =
p4_constraints::GetTableInfoOrNull(config.GetConstraintInfo(), table_id);

return table_info != nullptr && table_info->constraint.has_value();
}

bool UsesP4Constraints(const pdpi::IrTableDefinition& table,
Expand All @@ -68,14 +65,15 @@ bool UsesP4Constraints(const pdpi::IrTableDefinition& table,
absl::StatusOr<TableEntry> FuzzValidConstrainedTableEntry(
const FuzzerConfig& config, const SwitchState& switch_state,
const pdpi::IrTableDefinition& table, absl::BitGen& gen) {
auto it = config.GetConstraintInfo().find(table.preamble().id());
if (it == config.GetConstraintInfo().end()) {
auto* table_info = p4_constraints::GetTableInfoOrNull(
config.GetConstraintInfo(), table.preamble().id());

if (table_info == nullptr) {
return gutil::InvalidArgumentErrorBuilder()
<< "given table with ID '" << table.preamble().id()
<< "' that does not exist in P4Info: " << table.preamble().alias();
}
const p4_constraints::TableInfo& table_info = it->second;
if (!table_info.constraint.has_value()) {
if (!table_info->constraint.has_value()) {
return gutil::InvalidArgumentErrorBuilder()
<< "given table without P4-Constraints: "
<< table.preamble().alias();
Expand Down Expand Up @@ -112,7 +110,7 @@ absl::StatusOr<TableEntry> FuzzValidConstrainedTableEntry(

ASSIGN_OR_RETURN(
p4_constraints::SymbolicEnvironment environment,
p4_constraints::EncodeValidTableEntryInZ3(table_info, solver, skip_key));
p4_constraints::EncodeValidTableEntryInZ3(*table_info, solver, skip_key));

// Try to add some randomness to get more unique entries by attempting to fuzz
// priority, skipping if the initial value yields an unsatisfiable constraint.
Expand Down Expand Up @@ -149,7 +147,7 @@ absl::StatusOr<TableEntry> FuzzValidConstrainedTableEntry(
z3::model model = solver.get_model();

ASSIGN_OR_RETURN(TableEntry table_entry,
p4_constraints::ConcretizeEntry(model, table_info,
p4_constraints::ConcretizeEntry(model, *table_info,
environment, skip_key));

// Fuzz an action.
Expand Down
44 changes: 44 additions & 0 deletions p4_fuzzer/constraints.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2023 Google LLC
//
// 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.
// =============================================================================
// Handles p4-constraints support for the P4-Fuzzer.

#ifndef PINS_INFRA_P4_FUZZER_CONSTRAINTS_H_
#define PINS_INFRA_P4_FUZZER_CONSTRAINTS_H_

#include "absl/random/random.h"
#include "absl/status/statusor.h"
#include "p4/v1/p4runtime.pb.h"
#include "p4_fuzzer/fuzzer_config.h"
#include "p4_fuzzer/switch_state.h"
#include "p4_pdpi/ir.pb.h"

namespace p4_fuzzer {

// Checks whether a table uses P4-Constraints.
bool UsesP4Constraints(const pdpi::IrTableDefinition& table,
const FuzzerConfig& config);

// Checks whether a table uses P4-Constraints.
bool UsesP4Constraints(int table_id, const FuzzerConfig& config);

// Generates a valid table entry for a table that uses P4-Constraints. Fails if
// given a table for which `!UsesP4Constraints`.
absl::StatusOr<p4::v1::TableEntry> FuzzValidConstrainedTableEntry(
const FuzzerConfig& config, const SwitchState& switch_state,
const pdpi::IrTableDefinition& table, absl::BitGen& gen);

} // namespace p4_fuzzer

#endif // PINS_INFRA_P4_FUZZER_CONSTRAINTS_H_
Loading

0 comments on commit 079649d

Please sign in to comment.