From 2ae551c0af26dd81e7e28bac4618ca3fc84019dc Mon Sep 17 00:00:00 2001 From: Charlotte Date: Wed, 28 Jun 2023 11:51:23 +1000 Subject: [PATCH] fmt: fuzz, fix (remove extraneous + incorrect fill) "blk + chunks" is often an overrun, plus the fill is unnecessary; we throw blk away immediately. --- backends/cxxrtl/cxxrtl.h | 1 - tests/fmt/fuzz/.gitignore | 2 ++ tests/fmt/fuzz/CMakeLists.txt | 23 +++++++++++++++++++ tests/fmt/fuzz/x_test.cc | 42 +++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tests/fmt/fuzz/.gitignore create mode 100644 tests/fmt/fuzz/CMakeLists.txt create mode 100644 tests/fmt/fuzz/x_test.cc diff --git a/backends/cxxrtl/cxxrtl.h b/backends/cxxrtl/cxxrtl.h index 8ea800ed9..d2f06d6bf 100644 --- a/backends/cxxrtl/cxxrtl.h +++ b/backends/cxxrtl/cxxrtl.h @@ -654,7 +654,6 @@ struct value : public expr_base> { } std::copy(blk, blk + origLen, data); - std::fill(blk + origLen, blk + chunks, 0); } static chunk::type getShiftedBlock(const value &num, size_t x, size_t y) { diff --git a/tests/fmt/fuzz/.gitignore b/tests/fmt/fuzz/.gitignore new file mode 100644 index 000000000..88ea7e2ef --- /dev/null +++ b/tests/fmt/fuzz/.gitignore @@ -0,0 +1,2 @@ +fuzztest +build diff --git a/tests/fmt/fuzz/CMakeLists.txt b/tests/fmt/fuzz/CMakeLists.txt new file mode 100644 index 000000000..6f7c43da8 --- /dev/null +++ b/tests/fmt/fuzz/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.14) +project(cxxrtl_division_fuzz) + +set(CMAKE_CXX_STANDARD 17) + +add_subdirectory(fuzztest) + +enable_testing() + +include(GoogleTest) + +fuzztest_setup_fuzzing_flags() + +include_directories(../../..) + +add_executable( + x_test + x_test.cc + ../../../libs/bigint/BigUnsigned.cc +) + +link_fuzztest(x_test) +gtest_discover_tests(x_test) diff --git a/tests/fmt/fuzz/x_test.cc b/tests/fmt/fuzz/x_test.cc new file mode 100644 index 000000000..ce4af5413 --- /dev/null +++ b/tests/fmt/fuzz/x_test.cc @@ -0,0 +1,42 @@ +#include "fuzztest/fuzztest.h" +#include "gtest/gtest.h" + +#include +#include "backends/cxxrtl/cxxrtl.h" +#include "libs/bigint/BigUnsigned.hh" + +using namespace cxxrtl_yosys; + +void Formats128BitUnsignedIntegers(uint64_t x, uint64_t y) { + // Compare output to actual BigUnsigned. + value<64> vs; + value<128> v; + vs.set(x); + v = v.blit<127, 64>(vs); + vs.set(y); + v = v.blit<63, 0>(vs); + + std::ostringstream oss; + oss << value_formatted<128>(v, false, false, ' ', 0, 10, false, false, false); + auto actual = oss.str(); + + BigUnsigned b(x); + b.bitShiftLeft(b, 64); + b.bitOr(b, y); + + std::string expected; + + if (b.isZero()) { + expected = "0"; + } else { + while (!b.isZero()) { + expected += '0' + (b % 10).toInt(); + b /= 10; + } + std::reverse(expected.begin(), expected.end()); + } + + EXPECT_EQ(actual, expected); +} +FUZZ_TEST(CxxrtlDivisionFuzz, Formats128BitUnsignedIntegers); +