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

update fuzztest #1112

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ if(MANIFOLD_FUZZ)
set(FUZZTEST_FUZZING_MODE ON)
# address sanitizer required
add_compile_options(-fsanitize=address)
add_link_options(-fsanitize=address)
endif()

if(TRACY_ENABLE)
Expand Down
2 changes: 1 addition & 1 deletion cmake/manifoldDeps.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ if(MANIFOLD_FUZZ)
FetchContent_Declare(
fuzztest
GIT_REPOSITORY https://github.com/google/fuzztest.git
GIT_TAG 2606e04a43e5a7730e437a849604a61f1cb0ff28
GIT_TAG 7b107216fa6cd62659234366aee493d6f8832d46
GIT_PROGRESS TRUE
)
FetchContent_MakeAvailable(fuzztest)
Expand Down
5 changes: 5 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,9 @@ if(MANIFOLD_FUZZ)
target_link_libraries(polygon_fuzz PUBLIC manifold)
link_fuzztest(polygon_fuzz)
gtest_discover_tests(polygon_fuzz)

add_executable(manifold_fuzz manifold_fuzz.cpp)
target_link_libraries(manifold_fuzz PUBLIC manifold)
link_fuzztest(manifold_fuzz)
gtest_discover_tests(manifold_fuzz)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we going to enable MANIFOLD_FUZZ on the CI?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is something that runs indefinitely, and non-deterministically. So this is probably something we want to run in our own machines.

endif()
12 changes: 12 additions & 0 deletions test/boolean_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,15 @@ TEST(Boolean, Precision2) {
cube2 = cube2.Translate(vec3(scale * kPrecision));
EXPECT_FALSE((cube ^ cube2).IsEmpty());
}

TEST(Boolean, SimpleCubeRegression) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elalish it is now here.

ManifoldParams().intermediateChecks = true;
ManifoldParams().processOverlaps = false;
Manifold result =
Manifold::Cube().Rotate(-0.10000000000000001, 0.10000000000000001, -1.) +
Manifold::Cube() -
Manifold::Cube().Rotate(-0.10000000000000001, -0.10000000000066571, -1.);
EXPECT_EQ(result.Status(), Manifold::Error::NoError);
ManifoldParams().intermediateChecks = false;
ManifoldParams().processOverlaps = true;
}
99 changes: 99 additions & 0 deletions test/manifold_fuzz.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2024 The Manifold 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.

#include <atomic>
#include <future>

#include "fuzztest/fuzztest.h"
#include "gtest/gtest.h"
#include "manifold/manifold.h"

using namespace fuzztest;
using namespace manifold;

enum class TransformType { Translate, Rotate, Scale };
struct Transform {
TransformType ty;
std::array<double, 3> vector;
};
struct CubeOp {
std::vector<Transform> transforms;
bool isUnion;
};

// larger numbers may cause precision issues, prefer to test them later
auto GoodNumbers = OneOf(InRange(0.1, 10.0), InRange(-10.0, -0.1));
auto Vec3Domain = ArrayOf<3>(GoodNumbers);
auto TransformDomain = StructOf<Transform>(
ElementOf({TransformType::Translate, TransformType::Rotate,
TransformType::Scale}),
Vec3Domain);
auto CsgDomain =
VectorOf(StructOf<CubeOp>(VectorOf(TransformDomain).WithMaxSize(20),
ElementOf({false, true})))
.WithMaxSize(100);

void SimpleCube(const std::vector<CubeOp> &inputs) {
ManifoldParams().intermediateChecks = true;
ManifoldParams().processOverlaps = false;
Manifold result;
for (const auto &input : inputs) {
auto cube = Manifold::Cube();
for (const auto &transform : input.transforms) {
printf("transform: %d\n", static_cast<int>(transform.ty));
switch (transform.ty) {
case TransformType::Translate:
cube = cube.Translate({std::get<0>(transform.vector),
std::get<1>(transform.vector),
std::get<2>(transform.vector)});
break;
case TransformType::Rotate:
cube = cube.Rotate(std::get<0>(transform.vector),
std::get<1>(transform.vector),
std::get<2>(transform.vector));
break;
case TransformType::Scale:
cube = cube.Scale({std::get<0>(transform.vector),
std::get<1>(transform.vector),
std::get<2>(transform.vector)});
break;
}
}

printf("isUnion: %d\n", input.isUnion);
std::atomic<pid_t> tid;
std::atomic<bool> faulted(true);
auto asyncFuture = std::async(
std::launch::async, [&result, &faulted, &tid, &cube, &input]() {
tid.store(gettid());
if (input.isUnion) {
result += cube;
} else {
result -= cube;
}
EXPECT_EQ(result.Status(), Manifold::Error::NoError);
faulted.store(false);
});
if (asyncFuture.wait_for(std::chrono::milliseconds(10000)) ==
std::future_status::timeout) {
printf("timeout after %dms...\n", 10000);
pthread_cancel(tid.load());
}

EXPECT_FALSE(faulted.load());
if (faulted.load()) break;
}
}

FUZZ_TEST(ManifoldFuzz, SimpleCube).WithDomains(CsgDomain);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this use the same random seed every time or different? If it fails, will it spit out all the info we need to create a deterministic test?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is different every time, but we can override that to replay the fuzzing process. It will spit out the info needed for the test when using the minimizer script, I will document it.

3 changes: 2 additions & 1 deletion test/polygon_fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "fuzztest/fuzztest.h"
#include "gtest/gtest.h"
#include "manifold/optional_assert.h"
#include "manifold/polygon.h"

using namespace fuzztest;
Expand All @@ -43,7 +44,7 @@ void TriangulationNoCrash(
try {
manifold::Triangulate(polys, precision);
faulted.store(false);
} catch (manifold::geometryErr e) {
} catch (geometryErr e) {
// geometryErr is fine
faulted.store(false);
} catch (...) {
Expand Down
Loading