Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into fuzzer_18
  • Loading branch information
VSuryaprasad-HCL committed Oct 16, 2024
2 parents 15a707c + 56c7171 commit 53bbdf6
Show file tree
Hide file tree
Showing 70 changed files with 3,867 additions and 4,363 deletions.
32 changes: 23 additions & 9 deletions install_dependencies.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
#!/bin/bash
# Copyright 2021 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
# Please read carefully before adding new dependencies:
#
# System dependencies are disallowed by default, and the bar for exceptions is
# high.
#
# pins-infra strives for a hermetic build, i.e. one that is insensitive to the
# libraries and other software installed on your machine, other than Bazel and
# the compilers. This ensure the build is reproducible and portable.
#
# Before adding a new system dependency, consider the following:
#
# http://www.apache.org/licenses/LICENSE-2.0
# 1. Please read the note on dependencies in pins_infra_deps.bzl.
#
# 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.
# 2. Can the dependency be avoided altogether? Consider that there is a
# non-trival cost to maintaining dependencies over time.
#
# 3. Can the dependency be built with Bazel instead?
# - For many libraries, there are existing Bazel BUILD files. Try a quick
# Google search.
# - If there is no existing BUILD file, can you write your own BUILD file?
# See the bazel/ folder for examples. Ideally, we strive to upstream such
# BUILD files so everyone can benefit and share the maintenance burden.
# - If it's too hard to write a native BUILD file, try writing a BUILD file
# using rules_foreign_cc (https://github.com/bazelbuild/rules_foreign_cc).
#. See the bazel/ folder for examples.

# Please read carefully before adding new dependencies:
#
Expand Down
17 changes: 10 additions & 7 deletions p4_fuzzer/fuzz_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "absl/strings/str_cat.h"
#include "absl/strings/str_join.h"
#include "absl/strings/string_view.h"
#include "absl/strings/substitute.h"
#include "absl/types/span.h"
#include "google/protobuf/repeated_field.h"
#include "google/protobuf/repeated_ptr_field.h"
Expand All @@ -55,12 +56,14 @@
#include "p4_pdpi/netaddr/ipv6_address.h"
#include "p4_pdpi/references.h"
#include "p4_pdpi/utils/ir.h"
#include "p4_pdpi/entity_keys.h"

namespace p4_fuzzer {

using ::absl::gntohll;

using ::absl::Uniform;
using ::pdpi::TableEntryKey;
using ::p4::v1::Action;
using ::p4::v1::Entity;
using ::p4::v1::FieldMatch;
Expand Down Expand Up @@ -728,7 +731,6 @@ const std::vector<pdpi::IrActionReference> AllValidActions(
for (const auto& action : table.entry_actions()) {
// Skip deprecated, unused, and disallowed actions.
if (pdpi::IsElementDeprecated(action.action().preamble().annotations()) ||
action.action().is_unsupported() ||
IsDisabledForFuzzing(config, action.action().preamble().name()))
continue;
actions.push_back(action);
Expand Down Expand Up @@ -1055,6 +1057,7 @@ absl::StatusOr<p4::v1::Action> FuzzAction(
return action;
}


// Gets a set of actions with a skewed distribution of weights, which add up to
// at most the max_group_size of the action profile by repeatedly sampling a
// uniform weight from 1 to the maximum possible weight remaining. We could
Expand All @@ -1079,8 +1082,8 @@ absl::StatusOr<p4::v1::ActionProfileActionSet> FuzzActionProfileActionSet(
? action_profile.max_group_size()
: kActionProfileActionSetMaxCardinality;
int number_of_actions = Uniform<int>(
absl::IntervalClosedClosed, *gen,
config.no_empty_action_profile_groups ? 1 : 0, max_number_of_actions);
absl::IntervalClosedClosed, *gen,
config.no_empty_action_profile_groups ? 1 : 0, max_number_of_actions);

// Get the max member weight from the P4Info if it is set.
int max_member_weight =
Expand Down Expand Up @@ -1136,10 +1139,10 @@ absl::StatusOr<p4::v1::ActionProfileActionSet> FuzzActionProfileActionSet(
// We want to randomly select some number of actions up to our max
// cardinality; however, we can't have more actions than the amount of
// weight we support since every action must have weight >= 1.
int number_of_actions = Uniform<int>(
absl::IntervalClosedClosed, *gen,
config.no_empty_action_profile_groups ? 1 : 0,
std::min(unallocated_weight, kActionProfileActionSetMaxCardinality));
int number_of_actions = Uniform<int>(
absl::IntervalClosedClosed, *gen,
config.no_empty_action_profile_groups ? 1 : 0,
std::min(unallocated_weight, kActionProfileActionSetMaxCardinality));

for (int i = 0; i < number_of_actions; i++) {
// Since each action must have at least weight 1, we need to take the
Expand Down
36 changes: 0 additions & 36 deletions p4_fuzzer/fuzz_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -399,42 +399,6 @@ TEST(FuzzUtilTest, FuzzWriteRequestRespectsDisallowList) {
}
}

TEST(FuzzUtilTest, FuzzValidTableEntryRespectsDisallowList) {
FuzzerTestState fuzzer_state = ConstructStandardFuzzerTestState();
fuzzer_state.config.disabled_fully_qualified_names = {
"ingress.ternary_table.ipv6_upper_64_bits",
"ingress.ternary_table.normal",
"ingress.ternary_table.mac",
"ingress.ternary_table.unsupported_field",
};

ASSERT_OK_AND_ASSIGN(
const pdpi::IrTableDefinition& ternary_table,
gutil::FindOrStatus(fuzzer_state.config.GetIrP4Info().tables_by_name(),
"ternary_table"));

absl::flat_hash_set<uint32_t> disallowed_ids;
for (const auto& path : fuzzer_state.config.disabled_fully_qualified_names) {
std::vector<std::string> parts = absl::StrSplit(path, '.');
ASSERT_OK_AND_ASSIGN(
const pdpi::IrMatchFieldDefinition& match,
gutil::FindOrStatus(ternary_table.match_fields_by_name(),
parts[parts.size() - 1]));
disallowed_ids.insert(match.match_field().id());
}

for (int i = 0; i < 1000; i++) {
ASSERT_OK_AND_ASSIGN(
p4::v1::TableEntry entry,
FuzzValidTableEntry(&fuzzer_state.gen, fuzzer_state.config,
fuzzer_state.switch_state,
ternary_table.preamble().id()));
for (const auto& match : entry.match()) {
EXPECT_THAT(match.field_id(), Not(AnyOfArray(disallowed_ids)));
}
}
}

TEST(FuzzUtilTest, FuzzActionRespectsDisallowList) {
FuzzerTestState fuzzer_state = ConstructStandardFuzzerTestState();
ASSERT_OK_AND_ASSIGN(
Expand Down
1 change: 1 addition & 0 deletions p4_fuzzer/switch_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "absl/container/btree_map.h"
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/types/optional.h"
Expand Down
33 changes: 18 additions & 15 deletions p4_symbolic/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@

# This file contains build rules for the main binary of p4_symbolic.

load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("//p4_pdpi/testing:diff_test.bzl", "cmd_diff_test", "diff_test")
load("//p4_pdpi:pdgen.bzl", "p4_pd_proto")
load("@com_github_p4lang_p4c//:bazel/p4_library.bzl", "p4_library")

package(
default_visibility = ["//visibility:public"],
licenses = ["notice"],
Expand All @@ -29,8 +24,6 @@ cc_binary(
srcs = [
"main.cc",
],
# linkopts = ["-lz3"],
visibility = ["//p4_symbolic/symbolic:__pkg__"],
deps = [
":parser",
"//gutil:status",
Expand All @@ -49,15 +42,10 @@ cc_binary(

cc_library(
name = "parser",
srcs = [
"parser.cc",
],
hdrs = [
"parser.h",
],
visibility = ["//visibility:public"],
srcs = ["parser.cc"],
hdrs = ["parser.h"],
deps = [
"//gutil:proto",
"//gutil:proto",
"//gutil:status",
"//p4_pdpi:ir_cc_proto",
"//p4_symbolic/bmv2",
Expand All @@ -69,3 +57,18 @@ cc_library(
"@com_google_absl//absl/types:span",
],
)

cc_library(
name = "z3_util",
srcs = ["z3_util.cc"],
hdrs = ["z3_util.h"],
deps = [
"//gutil:status",
"//p4_pdpi/string_encodings:hex_string",
"@com_github_z3prover_z3//:api",
"@com_gnu_gmp//:gmp",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
],
)
12 changes: 4 additions & 8 deletions p4_symbolic/bmv2/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("//p4_pdpi/testing:diff_test.bzl", "cmd_diff_test", "diff_test")
load("//p4_pdpi:pdgen.bzl", "p4_pd_proto")
load("@com_github_p4lang_p4c//:bazel/p4_library.bzl", "p4_library")
load("//p4_symbolic/bmv2:test.bzl", "bmv2_protobuf_parsing_test")

package(licenses = ["notice"])
package(
default_visibility = ["//visibility:public"],
licenses = ["notice"],
)

cc_proto_library(
name = "bmv2_cc_proto",
visibility = ["//p4_symbolic:__subpackages__"],
deps = [":bmv2_proto"],
)

Expand All @@ -31,7 +29,6 @@ proto_library(
srcs = [
"bmv2.proto",
],
visibility = ["//p4_symbolic:__subpackages__"],
deps = [
"@com_google_protobuf//:struct_proto",
],
Expand All @@ -45,7 +42,6 @@ cc_library(
hdrs = [
"bmv2.h",
],
visibility = ["//p4_symbolic:__subpackages__"],
deps = [
":bmv2_cc_proto",
"//gutil:status",
Expand Down
41 changes: 18 additions & 23 deletions p4_symbolic/ir/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,26 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("//p4_pdpi/testing:diff_test.bzl", "cmd_diff_test", "diff_test")
load("//p4_pdpi:pdgen.bzl", "p4_pd_proto")
load("@com_github_p4lang_p4c//:bazel/p4_library.bzl", "p4_library")
load("//p4_symbolic/ir:test.bzl", "ir_parsing_test")

package(licenses = ["notice"])
package(
default_visibility = ["//visibility:public"],
licenses = ["notice"],
)

cc_proto_library(
name = "ir_cc_proto",
deps = [":ir_proto"],
)

proto_library(
name = "ir_proto",
srcs = ["ir.proto"],
deps = [
"//p4_pdpi:ir_proto",
"//p4_symbolic/bmv2:bmv2_proto",
],
)

cc_library(
name = "pdpi_driver",
Expand All @@ -28,7 +41,6 @@ cc_library(
hdrs = [
"pdpi_driver.h",
],
visibility = ["//p4_symbolic:__subpackages__"],
deps = [
"//gutil:proto",
"//gutil:status",
Expand All @@ -37,21 +49,6 @@ cc_library(
],
)

cc_proto_library(
name = "ir_cc_proto",
visibility = ["//p4_symbolic:__subpackages__"],
deps = [":ir_proto"],
)

proto_library(
name = "ir_proto",
srcs = ["ir.proto"],
deps = [
"//p4_pdpi:ir_proto",
"//p4_symbolic/bmv2:bmv2_proto",
],
)

cc_library(
name = "table_entries",
srcs = [
Expand All @@ -60,7 +57,6 @@ cc_library(
hdrs = [
"table_entries.h",
],
visibility = ["//p4_symbolic:__subpackages__"],
deps = [
":ir_cc_proto",
"//gutil:status",
Expand All @@ -82,7 +78,6 @@ cc_library(
hdrs = [
"ir.h",
],
visibility = ["//p4_symbolic:__subpackages__"],
deps = [
":ir_cc_proto",
":table_entries",
Expand Down
4 changes: 2 additions & 2 deletions p4_symbolic/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ absl::Status ParseAndEvaluate() {
// Loop over tables in a deterministic order for output consistency (important
// for ci tests).
std::string debug_smt_formula = "";
std::map ordered_tables(dataplane.program.tables().cbegin(),
dataplane.program.tables().cend());
auto ordered_tables = absl::btree_map<std::string, p4_symbolic::ir::Table>(
dataplane.program.tables().cbegin(), dataplane.program.tables().cend());
for (const auto &[name, table] : ordered_tables) {
int row_count = static_cast<int>(dataplane.entries[name].size());
for (int i = -1; i < row_count; i++) {
Expand Down
13 changes: 6 additions & 7 deletions p4_symbolic/sai/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("//p4_pdpi/testing:diff_test.bzl", "cmd_diff_test", "diff_test")
load("//p4_pdpi:pdgen.bzl", "p4_pd_proto")
load("@com_github_p4lang_p4c//:bazel/p4_library.bzl", "p4_library")

package(licenses = ["notice"])
package(
default_visibility = ["//visibility:public"],
licenses = ["notice"],
)

cc_library(
name = "fields",
srcs = ["fields.cc"],
hdrs = ["fields.h"],
deps = [
"//gutil:status",
"//p4_symbolic:z3_util",
"//p4_symbolic/symbolic",
"@com_github_z3prover_z3//:api",
"@com_google_absl//absl/strings",
Expand All @@ -35,10 +34,10 @@ cc_library(
name = "parser",
srcs = ["parser.cc"],
hdrs = ["parser.h"],
visibility = ["//p4_symbolic:__subpackages__"],
deps = [
":fields",
"//gutil:status",
"//p4_symbolic:z3_util",
"//p4_symbolic/symbolic",
"@com_github_z3prover_z3//:api",
"@com_google_absl//absl/status",
Expand Down
2 changes: 1 addition & 1 deletion p4_symbolic/sai/fields.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
#include "absl/strings/str_join.h"
#include "gutil/status.h"
#include "p4_symbolic/symbolic/symbolic.h"
#include "p4_symbolic/z3_util.h"
#include "z3++.h"

namespace p4_symbolic {

namespace {

using ::p4_symbolic::symbolic::SymbolicPerPacketState;
using ::p4_symbolic::symbolic::Z3Context;

// The p4c compiler mangles field names from the local_metadata struct.
// As a workaround, we unmangle the names, best effort.
Expand Down
Loading

0 comments on commit 53bbdf6

Please sign in to comment.