Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration of pw_fuzzer using FuzzTest #34274

Merged
merged 20 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
520 changes: 266 additions & 254 deletions .gitmodules

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ if (current_toolchain != "${dir_pw_toolchain}/default:default") {
}
}

if (pw_enable_fuzz_test_targets) {
group("pw_fuzz_tests") {
deps = [
#":pw_module_tests.run($dir_pigweed/targets/host:host_clang_fuzz)",
#"$dir_pigweed:pw_module_tests.run($dir_pigweed/targets/host:host_clang_fuzz)",
# "${chip_root}/build/toolchain/pw_fuzzer:clang_pw_fuzz",
"${chip_root}/src/lib/format/tests:FuzzPayloadDecoderPW(//build/toolchain/pw_fuzzer:clang_pw_fuzz)",
]
}
}

# Matter's in-tree pw_python_package or pw_python_distribution targets.
_matter_python_packages = [
"//examples/chef",
Expand Down
2 changes: 2 additions & 0 deletions build/chip/fuzz_test.gni
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import("${chip_root}/build/chip/tests.gni")
declare_args() {
enable_fuzz_test_targets = is_clang && chip_build_tests &&
(current_os == "linux" || current_os == "mac")

pw_enable_fuzz_test_targets = false
}

# Define a fuzz target for chip.
Expand Down
5 changes: 5 additions & 0 deletions build/config/BUILDCONFIG.gn
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ _chip_defaults = {
declare_args() {
# Toolchain to use for host. This is usually set by default.
host_toolchain = ""

is_pw_fuzz = false
}

if (host_toolchain == "") {
Expand All @@ -68,6 +70,9 @@ if (_chip_defaults.custom_toolchain != "") {
} else {
_default_toolchain = _chip_defaults.custom_toolchain
}
# } else if (is_pw_fuzz == true) {
# _default_toolchain =
# "${_build_overrides.build_root}/toolchain/pw_fuzzer:clang_pw_fuzz"
} else if (target_os == "all") {
_default_toolchain = "${_pigweed_overrides.dir_pw_toolchain}/default"
} else if (target_os == "linux") {
Expand Down
3 changes: 2 additions & 1 deletion build/config/compiler/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ config("strict_warnings") {
config("warnings_default") {
configs = [
":warnings_common",
":strict_warnings",

# ":strict_warnings",
":disabled_warnings",
]
}
Expand Down
69 changes: 69 additions & 0 deletions build/toolchain/pw_fuzzer/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Copyright (c) 2024 Project CHIP Authors
#
# 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.

# TODO: maybe delete this file as it seems useless as of 25/06
import("//build_overrides/build.gni")
import("//build_overrides/pigweed.gni")

import("${build_root}/toolchain/gcc_toolchain.gni")

import("$dir_pigweed/targets/host/target_toolchains.gni")

#import("$dir_pigweed/pw_toolchain/host_clang/toolchains.gni")

gcc_toolchain("clang_pw_fuzz") {
toolchain_args = {
name = "clang_pw_fuzz"
print("gcc_toolchain(clang_pw_fuzz)")

# print("pw_toolchain_host_clang = $pw_toolchain_host_clang")
# forward_variables_from(pw_toolchain_host_clang.fuzz, "*", [ "name" ])

# print(
# "pw_target_toolchain_host.clang_fuzz = ${pw_target_toolchain_host.clang_fuzz}")
forward_variables_from(pw_target_toolchain_host.clang_fuzz, "*", [ "name" ])

#This is needed to have the defaults passed from pw_target_toolchain_host.clang_fuzz,
forward_variables_from(defaults, "*")

#print("defaults = $defaults")
print("pw_toolchain_FUZZING_ENABLED = ${pw_toolchain_FUZZING_ENABLED}")
pw_unit_test_MAIN = "$dir_pw_fuzzer:fuzztest_main"
pw_unit_test_BACKEND = "$dir_pw_fuzzer:gtest"

# below three lines are needed by gcc_toolchain template
current_os = host_os
current_cpu = host_cpu
is_clang = true

# pw_toolchain_FUZZING_ENABLED = true
treat_warnings_as_errors = false

# dir_pw_third_party_abseil_cpp = "$dir_pigweed/third_party/abseil-cpp/src"
# dir_pw_third_party_fuzztest = "$dir_pigweed/third_party/fuzztest"
# dir_pw_third_party_googletest = "$dir_pigweed/third_party/googletest"
# dir_pw_third_party_re2 = "$dir_pigweed/third_party/re2/src"

#dir_pw_third_party_abseil_cpp = "$dir_pw_third_party/abseil-cpp/"

dir_pw_third_party_abseil_cpp = "//third_party/abseil-cpp/src"
dir_pw_third_party_fuzztest = "//third_party/fuzztest"
dir_pw_third_party_googletest = "//third_party/googletest"
dir_pw_third_party_re2 = "//third_party/re2/src"

# use below if above dont work
#dir_pw_third_party_abseil_cpp="//third_party/abseil-cpp"
#dir_pw_third_party_re2 = "//third_party/re2"
}
}
31 changes: 31 additions & 0 deletions src/lib/format/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,34 @@ if (enable_fuzz_test_targets) {
]
}
}

if (pw_enable_fuzz_test_targets) {
# DOCSTAG: [pwfuzzer_examples_fuzztest-gn]
pw_test("FuzzPayloadDecoderPW") {
sources = [ "FuzzPayloadDecoderPW.cpp" ]

# print(" aAAAA: $dir_pw_fuzzer")
deps = [
#":pw_module_tests.run($dir_pigweed/targets/host:host_clang_fuzz)",
"$dir_pw_fuzzer:fuzztest", # <- Added!

# "${chip_root}/build/toolchain/pw_fuzzer:clang_pw_fuzz",

# "${chip_root}:fuzzers",
]
public_deps = [
"${chip_root}/src/controller/data_model:cluster-tlv-metadata",
"${chip_root}/src/lib/core",
"${chip_root}/src/lib/format:flat-tree",
"${chip_root}/src/lib/format:protocol-decoder",
"${chip_root}/src/lib/format:protocol-tlv-metadata",
"${chip_root}/src/lib/support",
"${chip_root}/src/platform/logging:stdio",
]

# this is necessary so it is not disable in third_party/pigweed/repo/pw_unit_test/test.gni
# otherwise it will be built successfully but with FuzzPayloadDecoderPW.DISABLED.ninja and no executable

enable_if = true
}
}
68 changes: 68 additions & 0 deletions src/lib/format/tests/FuzzPayloadDecoderPW.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
*
* Copyright (c) 2020-2021 Project CHIP Authors
* All rights reserved.
*
* 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 <cstddef>
#include <cstdint>
#include <lib/format/protocol_decoder.h>
#include <lib/support/StringBuilder.h>

#include <tlv/meta/clusters_meta.h>
#include <tlv/meta/protocols_meta.h>

#include <pw_unit_test/framework.h>
#include <pw_fuzzer/fuzztest.h>

namespace {

using namespace chip::Decoders;
using namespace chip::FlatTree;
using namespace chip::TLV;
using namespace chip::TLVMeta;

void RunDecodePW(const std::vector<std::uint8_t>& bytes)
{
const uint8_t* const data{bytes.data()};
const int size{static_cast<int>(bytes.size())};

chip::ByteSpan payload(data, size);

PayloadDecoderInitParams params;
params.SetProtocolDecodeTree(chip::TLVMeta::protocols_meta).SetClusterDecodeTree(chip::TLVMeta::clusters_meta);


// Try some SC variants
params.SetProtocol(chip::Protocols::SecureChannel::Id);
params.SetMessageType(0);

chip::Decoders::PayloadDecoder<64, 128> decoder(params);

decoder.StartDecoding(payload);

printf("Test is running");

PayloadEntry entry;
while (decoder.Next(entry))
{
// Nothing to do ...
}
}




FUZZ_TEST(PayloadDecoder, RunDecodePW);
}
1 change: 1 addition & 0 deletions third_party/abseil-cpp/src
Submodule src added at 3ab97e
1 change: 1 addition & 0 deletions third_party/fuzztest
Submodule fuzztest added at 6eb010
1 change: 1 addition & 0 deletions third_party/googletest
Submodule googletest added at 1d17ea
1 change: 1 addition & 0 deletions third_party/re2/src
Submodule src added at 85dd7a
Loading