From 9b077bfb95dd3375ac9b9859d4fab20236091a02 Mon Sep 17 00:00:00 2001 From: Laurent Perron Date: Tue, 14 Jan 2025 18:48:55 +0100 Subject: [PATCH] remove fuzz references --- ortools/sat/BUILD.bazel | 19 -- ortools/sat/cp_model_solver_fuzz.cc | 289 ---------------------------- ortools/sat/diffn_util_test.cc | 6 - 3 files changed, 314 deletions(-) delete mode 100644 ortools/sat/cp_model_solver_fuzz.cc diff --git a/ortools/sat/BUILD.bazel b/ortools/sat/BUILD.bazel index 0465ad6305..54aa6756e2 100644 --- a/ortools/sat/BUILD.bazel +++ b/ortools/sat/BUILD.bazel @@ -3174,7 +3174,6 @@ cc_test( ":integer_base", ":util", "//ortools/base", - "//ortools/base:fuzztest", "//ortools/base:gmock", "//ortools/graph:connected_components", "//ortools/graph:strongly_connected_components", @@ -3874,24 +3873,6 @@ cc_test( ], ) -cc_test( - name = "cp_model_solver_fuzz", - size = "large", - srcs = ["cp_model_solver_fuzz.cc"], - data = glob(["fuzz_testdata/*"]), - deps = [ - ":cp_model_cc_proto", - ":cp_model_checker", - ":cp_model_solver", - "//ortools/base:fuzztest", - "//ortools/base:path", - "//ortools/util:saturated_arithmetic", - "@bazel_tools//tools/cpp/runfiles", - "@com_google_absl//absl/log:check", - "@com_google_fuzztest//fuzztest:fuzztest_gtest_main", - ], -) - cc_test( name = "flaky_models_test", size = "small", diff --git a/ortools/sat/cp_model_solver_fuzz.cc b/ortools/sat/cp_model_solver_fuzz.cc deleted file mode 100644 index 637536c174..0000000000 --- a/ortools/sat/cp_model_solver_fuzz.cc +++ /dev/null @@ -1,289 +0,0 @@ -// Copyright 2010-2025 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. - -#include -#include -#include -#include -#include -#include - -#include "absl/log/check.h" -#include "gtest/gtest.h" // IWYU pragma: keep -#include "ortools/base/fuzztest.h" -#include "ortools/base/path.h" // IWYU pragma: keep -#include "ortools/sat/cp_model.pb.h" -#include "ortools/sat/cp_model_checker.h" -#include "ortools/sat/cp_model_solver.h" -#include "ortools/util/saturated_arithmetic.h" -#include "tools/cpp/runfiles/runfiles.h" - -namespace operations_research::sat { - -namespace { - -static constexpr int kMaxNumVars = 200; - -std::string GetTestDataDir() { - return file::JoinPathRespectAbsolute(::testing::SrcDir(), - "_main/ortools/sat/fuzz_testdata"); -} - -void Solve(const CpModelProto& proto) { - SatParameters params; - params.set_max_time_in_seconds(4.0); - params.set_debug_crash_if_presolve_breaks_hint(true); - - // Enable all fancy heuristics. - params.set_linearization_level(2); - params.set_use_try_edge_reasoning_in_no_overlap_2d(true); - params.set_exploit_all_precedences(true); - params.set_use_hard_precedences_in_cumulative(true); - params.set_max_num_intervals_for_timetable_edge_finding(1000); - params.set_use_overload_checker_in_cumulative(true); - params.set_use_strong_propagation_in_disjunctive(true); - params.set_use_timetable_edge_finding_in_cumulative(true); - params.set_max_pairs_pairwise_reasoning_in_no_overlap_2d(50000); - params.set_use_timetabling_in_no_overlap_2d(true); - params.set_use_energetic_reasoning_in_no_overlap_2d(true); - params.set_use_area_energetic_reasoning_in_no_overlap_2d(true); - params.set_use_conservative_scale_overload_checker(true); - params.set_use_dual_scheduling_heuristics(true); - - const CpSolverResponse response = - operations_research::sat::SolveWithParameters(proto, params); - - params.set_cp_model_presolve(false); - const CpSolverResponse response_no_presolve = - operations_research::sat::SolveWithParameters(proto, params); - - CHECK_EQ(response.status() == CpSolverStatus::MODEL_INVALID, - response_no_presolve.status() == CpSolverStatus::MODEL_INVALID) - << "Model being invalid should not depend on presolve"; - - if (response.status() == CpSolverStatus::MODEL_INVALID) { - return; - } - - if (response.status() == CpSolverStatus::UNKNOWN || - response_no_presolve.status() == CpSolverStatus::UNKNOWN) { - return; - } - - CHECK_EQ(response.status() == CpSolverStatus::INFEASIBLE, - response_no_presolve.status() == CpSolverStatus::INFEASIBLE) - << "Presolve should not change feasibility"; -} - -fuzztest::Domain CpVariableDomain(int64_t min, - int64_t max) { - fuzztest::Domain bound_domain = fuzztest::InRange(min, max); - fuzztest::Domain> complex_domain_gap_and_size = - fuzztest::PairOf(fuzztest::InRange(1, max), - fuzztest::InRange(0, max)); - fuzztest::Domain var_domain = fuzztest::ReversibleMap( - [](int64_t bound1, int64_t bound2, - std::vector> complex_domain_segments) { - IntegerVariableProto var; - var.add_domain(std::min(bound1, bound2)); - var.add_domain(std::max(bound1, bound2)); - - for (const auto& [gap, size] : complex_domain_segments) { - var.add_domain(CapAdd(var.domain(var.domain().size() - 1), gap)); - var.add_domain(CapAdd(var.domain(var.domain().size() - 1), size)); - } - return var; - }, - [](IntegerVariableProto var) { - using Type = std::tuple>>; - Type domain_inputs; - if (var.domain().size() < 2 || var.domain().size() % 2 != 0) { - return std::optional(); - } - domain_inputs = {var.domain(0), var.domain(1), {}}; - std::vector>& complex_domain_segments = - std::get<2>(domain_inputs); - for (int i = 2; i + 1 < var.domain().size(); i += 2) { - const int64_t gap = CapSub(var.domain(i), var.domain(i - 1)); - const int64_t size = CapSub(var.domain(i + 1), var.domain(i)); - if (gap <= 0 || size < 0) { - return std::optional(); - } - complex_domain_segments.push_back({gap, size}); - } - return std::optional(domain_inputs); - }, - bound_domain, bound_domain, - fuzztest::VectorOf(complex_domain_gap_and_size)); - - return fuzztest::Filter( - [max](IntegerVariableProto var) { - return var.domain(var.domain().size() - 1) <= max; - }, - var_domain); -} - -fuzztest::Domain> -ModelProtoVariablesDomain() { - return fuzztest::Filter( - [](const std::vector& vars) { - // Check if the variables in isolation doesn't make for a invalid model. - CpModelProto model; - for (const IntegerVariableProto& var : vars) { - *model.add_variables() = var; - } - return ValidateCpModel(model).empty(); - }, - fuzztest::VectorOf( - CpVariableDomain(-std::numeric_limits::max() / 2, - std::numeric_limits::max() / 2)) - .WithMaxSize(kMaxNumVars)); -} - -fuzztest::Domain LinearExprDomain() { - fuzztest::Domain offset_domain = - fuzztest::InRange(-std::numeric_limits::max() / 2, - std::numeric_limits::max() / 2); - fuzztest::Domain> variable_and_coefficient = - fuzztest::PairOf( - fuzztest::InRange(0, kMaxNumVars - 1), - fuzztest::InRange(-std::numeric_limits::max() / 2, - std::numeric_limits::max() / 2)); - return fuzztest::ReversibleMap( - [](int64_t offset, - std::vector> variable_and_coefficient) { - LinearExpressionProto expr; - for (const auto& [var, coeff] : variable_and_coefficient) { - expr.add_vars(var); - expr.add_coeffs(coeff); - } - expr.set_offset(offset); - return expr; - }, - [](LinearExpressionProto expr) { - using Type = - std::tuple>>; - Type expr_inputs; - expr_inputs = {expr.offset(), {}}; - std::vector>& variable_and_coefficient = - std::get<1>(expr_inputs); - if (expr.vars_size() != expr.coeffs_size()) { - return std::optional(); - } - for (int i = 0; i < expr.vars_size(); ++i) { - variable_and_coefficient.push_back({expr.vars(i), expr.coeffs(i)}); - } - return std::optional(expr_inputs); - }, - offset_domain, fuzztest::VectorOf(variable_and_coefficient)); -} - -fuzztest::Domain LinearConstraintDomain() { - fuzztest::Domain> variable_and_coefficient = - fuzztest::PairOf( - fuzztest::InRange(0, kMaxNumVars - 1), - fuzztest::InRange(-std::numeric_limits::max() / 2, - std::numeric_limits::max() / 2)); - return fuzztest::ReversibleMap( - [](IntegerVariableProto domain, - std::vector> variable_and_coefficient) { - LinearConstraintProto constraint; - for (const auto& [var, coeff] : variable_and_coefficient) { - constraint.add_vars(var); - constraint.add_coeffs(coeff); - } - *constraint.mutable_domain() = domain.domain(); - return constraint; - }, - [](LinearConstraintProto constraint) { - using Type = std::tuple>>; - Type constraint_inputs = {IntegerVariableProto(), {}}; - *std::get<0>(constraint_inputs).mutable_domain() = constraint.domain(); - std::vector>& variable_and_coefficient = - std::get<1>(constraint_inputs); - if (constraint.vars_size() != constraint.coeffs_size() || - constraint.vars().empty()) { - return std::optional(); - } - for (int i = 0; i < constraint.vars_size(); ++i) { - variable_and_coefficient.push_back( - {constraint.vars(i), constraint.coeffs(i)}); - } - return std::optional(constraint_inputs); - }, - CpVariableDomain(std::numeric_limits::min(), - std::numeric_limits::max()), - fuzztest::VectorOf(variable_and_coefficient)); -} - -fuzztest::Domain IntervalConstraintDomain() { - return fuzztest::Arbitrary() - .WithProtobufField("start", LinearExprDomain()) - .WithProtobufField("end", LinearExprDomain()) - .WithProtobufField("size", LinearExprDomain()); -} - -fuzztest::Domain LinearArgumentDomain() { - return fuzztest::Arbitrary() - .WithProtobufField("target", LinearExprDomain()) - .WithRepeatedProtobufField("exprs", - fuzztest::VectorOf(LinearExprDomain())); -} - -fuzztest::Domain BoolArgumentDomain() { - return fuzztest::Arbitrary().WithRepeatedInt32Field( - "literals", fuzztest::VectorOf(fuzztest::InRange( - -kMaxNumVars, kMaxNumVars - 1))); -} - -// Fuzzing repeats solve() 100 times, and timeout after 600s. -// With a time limit of 4s, we should be fine. -FUZZ_TEST(CpModelProtoFuzzer, Solve) - .WithDomains( - fuzztest::Arbitrary() - .WithRepeatedProtobufField("variables", ModelProtoVariablesDomain()) - .WithRepeatedProtobufField( - "constraints", - fuzztest::VectorOf( - fuzztest::Arbitrary() - .WithOneofAlwaysSet("constraint") - .WithFieldUnset("name") - .WithFieldUnset("dummy_constraint") - .WithProtobufField("bool_or", BoolArgumentDomain()) - .WithProtobufField("bool_and", BoolArgumentDomain()) - .WithProtobufField("at_most_one", BoolArgumentDomain()) - .WithProtobufField("exactly_one", BoolArgumentDomain()) - .WithProtobufField("bool_xor", BoolArgumentDomain()) - .WithProtobufField("int_div", LinearArgumentDomain()) - .WithProtobufField("int_mod", LinearArgumentDomain()) - .WithProtobufField("int_prod", LinearArgumentDomain()) - .WithProtobufField("lin_max", LinearArgumentDomain()) - .WithProtobufField("linear", LinearConstraintDomain()) - .WithProtobufField("interval", - IntervalConstraintDomain()))) - .WithFieldUnset("name") - .WithFieldUnset("symmetry") - .WithProtobufField("objective", - fuzztest::Arbitrary() - .WithFieldUnset("scaling_was_exact") - .WithFieldUnset("integer_scaling_factor") - .WithFieldUnset("integer_before_offset") - .WithFieldUnset("integer_after_offset"))) - .WithSeeds([]() { - return fuzztest::ReadFilesFromDirectory(GetTestDataDir()); - }); - -} // namespace -} // namespace operations_research::sat diff --git a/ortools/sat/diffn_util_test.cc b/ortools/sat/diffn_util_test.cc index fb95b26ed2..7838ca0123 100644 --- a/ortools/sat/diffn_util_test.cc +++ b/ortools/sat/diffn_util_test.cc @@ -35,7 +35,6 @@ #include "absl/types/span.h" #include "benchmark/benchmark.h" #include "gtest/gtest.h" -#include "ortools/base/fuzztest.h" #include "ortools/base/gmock.h" #include "ortools/base/logging.h" #include "ortools/graph/connected_components.h" @@ -1127,11 +1126,6 @@ void CheckFuzzedRectangles( << RenderRectGraph(std::nullopt, rectangles, result); } -FUZZ_TEST(FindPartialIntersections, CheckFuzzedRectangles) - .WithDomains(fuzztest::VectorOf(fuzztest::TupleOf( - fuzztest::Arbitrary(), fuzztest::NonNegative(), - fuzztest::Arbitrary(), fuzztest::NonNegative()))); - void BM_FindRectangles(benchmark::State& state) { absl::BitGen random; std::vector> problems;