diff options
Diffstat (limited to 'third_party/immer/test/flex_vector')
-rw-r--r-- | third_party/immer/test/flex_vector/B3-BL0.cpp | 21 | ||||
-rw-r--r-- | third_party/immer/test/flex_vector/B3-BL3.cpp | 21 | ||||
-rw-r--r-- | third_party/immer/test/flex_vector/default.cpp | 14 | ||||
-rw-r--r-- | third_party/immer/test/flex_vector/fuzzed-0.cpp | 380 | ||||
-rw-r--r-- | third_party/immer/test/flex_vector/fuzzed-1.cpp | 368 | ||||
-rw-r--r-- | third_party/immer/test/flex_vector/fuzzed-2.cpp | 188 | ||||
-rw-r--r-- | third_party/immer/test/flex_vector/fuzzed-3.cpp | 222 | ||||
-rw-r--r-- | third_party/immer/test/flex_vector/fuzzed-4.cpp | 364 | ||||
-rw-r--r-- | third_party/immer/test/flex_vector/gc.cpp | 27 | ||||
-rw-r--r-- | third_party/immer/test/flex_vector/generic.ipp | 599 | ||||
-rw-r--r-- | third_party/immer/test/flex_vector/issue-45.cpp | 35 | ||||
-rw-r--r-- | third_party/immer/test/flex_vector/issue-47.cpp | 5 | ||||
-rw-r--r-- | third_party/immer/test/flex_vector/regular-B3-BL3.cpp | 16 | ||||
-rw-r--r-- | third_party/immer/test/flex_vector/regular-default.cpp | 12 |
14 files changed, 2272 insertions, 0 deletions
diff --git a/third_party/immer/test/flex_vector/B3-BL0.cpp b/third_party/immer/test/flex_vector/B3-BL0.cpp new file mode 100644 index 000000000000..83af21d688fc --- /dev/null +++ b/third_party/immer/test/flex_vector/B3-BL0.cpp @@ -0,0 +1,21 @@ +// +// immer: immutable data structures for C++ +// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente +// +// This software is distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt +// + +#include <immer/flex_vector.hpp> +#include <immer/vector.hpp> + +template <typename T> +using test_flex_vector_t = + immer::flex_vector<T, immer::default_memory_policy, 3u, 0u>; + +template <typename T> +using test_vector_t = immer::vector<T, immer::default_memory_policy, 3u, 0u>; + +#define FLEX_VECTOR_T test_flex_vector_t +#define VECTOR_T test_vector_t +#include "generic.ipp" diff --git a/third_party/immer/test/flex_vector/B3-BL3.cpp b/third_party/immer/test/flex_vector/B3-BL3.cpp new file mode 100644 index 000000000000..e44f7e2bfaba --- /dev/null +++ b/third_party/immer/test/flex_vector/B3-BL3.cpp @@ -0,0 +1,21 @@ +// +// immer: immutable data structures for C++ +// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente +// +// This software is distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt +// + +#include <immer/flex_vector.hpp> +#include <immer/vector.hpp> + +template <typename T> +using test_flex_vector_t = + immer::flex_vector<T, immer::default_memory_policy, 3u, 3u>; + +template <typename T> +using test_vector_t = immer::vector<T, immer::default_memory_policy, 3u, 3u>; + +#define FLEX_VECTOR_T test_flex_vector_t +#define VECTOR_T test_vector_t +#include "generic.ipp" diff --git a/third_party/immer/test/flex_vector/default.cpp b/third_party/immer/test/flex_vector/default.cpp new file mode 100644 index 000000000000..7258cc07728d --- /dev/null +++ b/third_party/immer/test/flex_vector/default.cpp @@ -0,0 +1,14 @@ +// +// immer: immutable data structures for C++ +// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente +// +// This software is distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt +// + +#include <immer/flex_vector.hpp> +#include <immer/vector.hpp> + +#define FLEX_VECTOR_T ::immer::flex_vector +#define VECTOR_T ::immer::vector +#include "generic.ipp" diff --git a/third_party/immer/test/flex_vector/fuzzed-0.cpp b/third_party/immer/test/flex_vector/fuzzed-0.cpp new file mode 100644 index 000000000000..063ad133a09d --- /dev/null +++ b/third_party/immer/test/flex_vector/fuzzed-0.cpp @@ -0,0 +1,380 @@ +// +// immer: immutable data structures for C++ +// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente +// +// This software is distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt +// + +#include "extra/fuzzer/fuzzer_input.hpp" +#include <array> +#include <catch.hpp> +#include <immer/flex_vector.hpp> +#include <iostream> + +#define IMMER_FUZZED_TRACE_ENABLE 0 + +#if IMMER_FUZZED_TRACE_ENABLE +#define IMMER_FUZZED_TRACE(...) std::cout << __VA_ARGS__ << std::endl; +#else +#define IMMER_FUZZED_TRACE(...) +#endif + +namespace { + +template <std::size_t VarCount = 8, unsigned Bits = 3> +int run_input(const std::uint8_t* data, std::size_t size) +{ + using vector_t = + immer::flex_vector<int, immer::default_memory_policy, Bits, Bits>; + using size_t = std::uint8_t; + + auto vars = std::array<vector_t, VarCount>{}; + +#if IMMER_FUZZED_TRACE_ENABLE + for (auto i = 0u; i < VarCount; ++i) + std::cout << "auto var" << i << " = vector_t{};" << std::endl; +#endif + + auto is_valid_var = [&](auto idx) { return idx >= 0 && idx < VarCount; }; + auto is_valid_index = [](auto& v) { + return [&](auto idx) { return idx >= 0 && idx < v.size(); }; + }; + auto is_valid_size = [](auto& v) { + return [&](auto idx) { return idx >= 0 && idx <= v.size(); }; + }; + + return fuzzer_input{data, size}.run([&](auto& in) { + enum ops + { + op_push_back, + op_update, + op_take, + op_drop, + op_concat, + }; + auto src = read<std::uint8_t>(in, is_valid_var); + auto dst = read<std::uint8_t>(in, is_valid_var); + switch (read<char>(in)) { + case op_push_back: + IMMER_FUZZED_TRACE("var" << +dst << " = var" << +src + << ".push_back(42);"); + vars[dst] = vars[src].push_back(42); + break; + case op_update: { + auto idx = read<size_t>(in, is_valid_index(vars[src])); + IMMER_FUZZED_TRACE("var" << +dst << " = var" << +src << ".update(" + << +idx + << ", [] (auto x) { return x + 1; });"); + vars[dst] = vars[src].update(idx, [](auto x) { return x + 1; }); + break; + } + case op_take: { + auto idx = read<size_t>(in, is_valid_size(vars[src])); + IMMER_FUZZED_TRACE("var" << +dst << " = var" << +src << ".take(" + << +idx << ");"); + vars[dst] = vars[src].take(idx); + break; + } + case op_drop: { + auto idx = read<size_t>(in, is_valid_size(vars[src])); + IMMER_FUZZED_TRACE("var" << +dst << " = var" << +src << ".take(" + << +idx << ");"); + vars[dst] = vars[src].drop(idx); + break; + } + case op_concat: { + auto src2 = read<std::uint8_t>(in, is_valid_var); + IMMER_FUZZED_TRACE("var" << +dst << " = var" << +src << " + var" + << +src2 << ";"); + vars[dst] = vars[src] + vars[src2]; + break; + } + default: + break; + }; + return true; + }); +} + +} // anonymous namespace + +TEST_CASE("bug: concatenate too big vectors") +{ + // The problem here was that since we were using 32 bit sizes, + // concatenating big flex_vectors can overflow the sizes. Let's + // just use std::size_t like normal people. + // + // Still, the problem could re-ocurr with longer inputs. For this + // reason later fuzzing efforts do check that concatenation is + // valid for the given vector sizes. Similar assertions are put + // in the code too. + SECTION("simplified example") + { + using vector_t = + immer::flex_vector<int, immer::default_memory_policy, 3, 3>; + auto var0 = vector_t{}; + auto var1 = vector_t{}; + auto var2 = vector_t{}; + auto var4 = vector_t{}; + var1 = var1.push_back(42); + var0 = var0.push_back(42); + var0 = var0.push_back(42); + var0 = var2.push_back(42); + var0 = var0.push_back(42); + var2 = var0; + var0 = var0.push_back(42); + var0 = var0.push_back(42); + var4 = var4.push_back(42); + var0 = var0.push_back(42); + var0 = var0.push_back(42); + var0 = var0 + var0; + var0 = var0.push_back(42); + var0 = var0.push_back(42); + var2 = var0.push_back(42); + var0 = var0 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var0 = var0.push_back(42); + var0 = var0.push_back(42); + var1 = var2 + var4; + var4 = var4 + var4; + var0 = var1.push_back(42); + var0 = var0.push_back(42); + var0 = var0 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4.push_back(42); + } + +#if __GNUC__ != 9 + // Assertion `!p->relaxed()' failed + SECTION("") + { + constexpr std::uint8_t input[] = { + 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x39, 0x6a, 0x76, + 0xb9, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x1, 0x0, 0x0, + 0x2a, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x4, 0x0, 0x0, 0x0, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, + 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x0, 0x0, 0x0, 0x0, + 0x0, 0x0, 0x2, 0x1, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x1, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, + 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, + 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, + 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, + 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, + 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x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input, sizeof(input)) == 0); + } + + // buffer overflow when looking inside the sizes array for the + // index of a position + SECTION("") + { + constexpr std::uint8_t input[] = { + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0xff, + 0xff, 0xff, 0xff, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x4, 0x4, 0x4, 0x4, + 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, + 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x0, 0x0, 0x4, 0x4, 0x4, 0x4, + 0x4, 0x4, 0x4, 0x4, 0x4, 0x0, 0x0, 0x0, 0x4, 0x4, 0x4, 0x4, + 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, + 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x0, 0x4, + 0x4, 0x4, 0x4, 0x4, 0x3, 0xff, 0x0, 0x0, 0x4, 0x4, 0x4, 0x4, + 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, + 0x4, 0x4, 0x4, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x4, + 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, + 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, + 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x0, 0x0, 0x4, 0x4, + 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, + 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4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x4, 0xf3, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, + 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x3, 0x4, 0x4, 0x4, 0xff, + 0xff, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xad, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + }; + CHECK(run_input(input, sizeof(input)) == 0); + } +#endif +} diff --git a/third_party/immer/test/flex_vector/fuzzed-1.cpp b/third_party/immer/test/flex_vector/fuzzed-1.cpp new file mode 100644 index 000000000000..d924dc3a8310 --- /dev/null +++ b/third_party/immer/test/flex_vector/fuzzed-1.cpp @@ -0,0 +1,368 @@ +// +// immer: immutable data structures for C++ +// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente +// +// This software is distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt +// + +#include "extra/fuzzer/fuzzer_input.hpp" +#include <array> +#include <catch.hpp> +#include <immer/flex_vector.hpp> +#include <iostream> + +#define IMMER_FUZZED_TRACE_ENABLE 0 + +#if IMMER_FUZZED_TRACE_ENABLE +#define IMMER_FUZZED_TRACE(...) std::cout << __VA_ARGS__ << std::endl; +#else +#define IMMER_FUZZED_TRACE(...) +#endif + +namespace { + +template <std::size_t VarCount = 2, unsigned Bits = 2> +int run_input(const std::uint8_t* data, std::size_t size) +{ + using vector_t = + immer::flex_vector<int, immer::default_memory_policy, Bits, Bits>; + using size_t = std::uint8_t; + + auto vars = std::array<vector_t, VarCount>{}; + +#if IMMER_FUZZED_TRACE_ENABLE + std::cout << "/// new test run" << std::endl; + for (auto i = 0u; i < VarCount; ++i) + std::cout << "auto var" << i << " = vector_t{};" << std::endl; +#endif + + auto is_valid_var = [&](auto idx) { return idx >= 0 && idx < VarCount; }; + auto is_valid_index = [](auto& v) { + return [&](auto idx) { return idx >= 0 && idx < v.size(); }; + }; + auto is_valid_size = [](auto& v) { + return [&](auto idx) { return idx >= 0 && idx <= v.size(); }; + }; + auto can_concat = [](auto&& v1, auto&& v2) { + using size_type = decltype(v1.size()); + return v2.size() < (std::numeric_limits<size_type>::max() - v1.size()); + }; + auto can_insert = [](auto&& v1) { + using size_type = decltype(v1.size()); + return v1.size() < std::numeric_limits<size_type>::max(); + }; + + return fuzzer_input{data, size}.run([&](auto& in) { + enum ops + { + op_push_back, + op_update, + op_take, + op_drop, + op_concat, + op_push_back_move, + op_update_move, + }; + auto src = read<std::uint8_t>(in, is_valid_var); + auto dst = read<std::uint8_t>(in, is_valid_var); + switch (read<char>(in)) { + case op_push_back: + if (can_insert(vars[src])) { + IMMER_FUZZED_TRACE("var" << +dst << " = var" << +src + << ".push_back(42);"); + vars[dst] = vars[src].push_back(42); + } + break; + case op_update: { + auto idx = read<size_t>(in, is_valid_index(vars[src])); + IMMER_FUZZED_TRACE("var" << +dst << " = var" << +src << ".update(" + << +idx + << ", [] (auto x) { return x + 1; });"); + vars[dst] = vars[src].update(idx, [](auto x) { return x + 1; }); + break; + } + case op_take: { + auto idx = read<size_t>(in, is_valid_size(vars[src])); + IMMER_FUZZED_TRACE("var" << +dst << " = var" << +src << ".take(" + << +idx << ");"); + vars[dst] = vars[src].take(idx); + break; + } + case op_drop: { + auto idx = read<size_t>(in, is_valid_size(vars[src])); + IMMER_FUZZED_TRACE("var" << +dst << " = var" << +src << ".take(" + << +idx << ");"); + vars[dst] = vars[src].drop(idx); + break; + } + case op_concat: { + auto src2 = read<std::uint8_t>(in, is_valid_var); + if (can_concat(vars[src], vars[src2])) { + IMMER_FUZZED_TRACE("var" << +dst << " = var" << +src << " + var" + << +src2 << ";"); + vars[dst] = vars[src] + vars[src2]; + } + break; + } + case op_push_back_move: { + if (can_insert(vars[src])) { + IMMER_FUZZED_TRACE("var" << +dst << " = std::move(var" << +src + << ").push_back(21);"); + vars[dst] = std::move(vars[src]).push_back(21); + } + break; + } + case op_update_move: { + auto idx = read<size_t>(in, is_valid_index(vars[src])); + IMMER_FUZZED_TRACE("var" << +dst << " = std::move(var" << +src + << ").update(" << +idx + << ", [] (auto x) { return x + 1; });"); + vars[dst] = + std::move(vars[src]).update(idx, [](auto x) { return x + 1; }); + break; + } + default: + break; + }; + return true; + }); +} + +} // anonymous namespace + +TEST_CASE("bug: memory leak because of move update") +{ + // There was a problem caused with shared "sizes buffer" in + // relaxed nodes. In particular, the ensure_mutable_relaxed(...) + // function was not decremeting the old sizes buffer. That is why + // the last transient push_back (which uses mutable operations) + // causes some of the relaxed buffers that are created during the + // previous concatenations, and that start to be shared from the + // update() onwards, to later be leaked. + SECTION("simplified") + { + using vector_t = + immer::flex_vector<int, immer::default_memory_policy, 2, 2>; + auto var0 = vector_t{}; + auto var1 = vector_t{}; + var0 = var0.push_back(42); + var0 = var0.push_back(42); + var0 = var0.push_back(42); + var0 = var0 + var0; + var1 = var0.push_back(42); + var0 = var0 + var1; + var1 = var0.push_back(42); + var0 = var0 + var0; + var0 = var1 + var0; + var0 = var1.update(5, [](auto x) { return x + 1; }); + var0 = std::move(var0).push_back(21); + } + +#if __GNUC__ != 9 + SECTION("") + { + constexpr std::uint8_t input[] = { + 0xff, 0x0, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + 0x40, 0x0, 0x0, 0x4, 0x0, 0x6d, 0x6d, 0x0, 0x1, 0x0, 0x4, + 0x6d, 0x6d, 0x6d, 0x0, 0x0, 0x4, 0x1, 0x6d, 0x6d, 0x0, 0x1, + 0x0, 0x0, 0x0, 0x4, 0x28, 0x0, 0xfc, 0x1, 0x0, 0x4, 0x0, + 0x0, 0x0, 0xfc, 0x1, 0x0, 0x1, 0x5, 0x0, 0x0, 0x1, 0x5, + 0x0, 0x0, 0x5, 0x0, 0x0, 0xff, 0xff, 0xff, 0x27, + }; + CHECK(run_input(input, sizeof(input)) == 0); + } +#endif +} + +TEST_CASE("non-bug: crash") +{ + // This is an interesting finding that is left here for + // documentation. This test actually should not run... the + // problem is that when we build too large vectors via + // concatenation, we can sometimes "overflow the shift". This is + // a degenerate case that we won't fix, but this helped adding + // appropriate assertions to the code. + // + // To prevent this in further fuzzing, the can_concat check has + // been made stricter. + return; + + SECTION("simplified") + { + using vector_t = + immer::flex_vector<int, immer::default_memory_policy, 2, 2>; + auto var4 = vector_t{}; + var4 = var4.push_back(42); + var4 = var4.push_back(42); + var4 = var4.push_back(42); + var4 = var4.push_back(42); + var4 = var4.push_back(42); + auto var0 = var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var0 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4 + var4; + var4 = var4.update(4, [](auto x) { return x + 1; }); + } +#if __GNUC__ != 9 + SECTION("") + { + constexpr std::uint8_t input[] = { + 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x00, 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x00, 0x2a, 0x00, + 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0xfc, 0xf9, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x05, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x05, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x05, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x23, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0xd5, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x05, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x23, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x00, 0x01, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x05, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x00, + 0x01, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x05, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3a, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x21, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x00, 0x04, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0x13, 0x13, 0x13, 0x13, 0x13, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x00, 0x10, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x05, 0x04, 0x04, 0x04, 0x04, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x3a, 0x00, 0x02, 0x00, 0x00, 0x00, + 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x05, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x23, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, + 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x05, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x23, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x00, 0x01, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x05, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x00, 0x01, 0x04, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x05, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x3a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x04, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x04, 0x04, 0x04, + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, + 0x00, + }; + CHECK(run_input<8>(input, sizeof(input)) == 0); + } +#endif +} diff --git a/third_party/immer/test/flex_vector/fuzzed-2.cpp b/third_party/immer/test/flex_vector/fuzzed-2.cpp new file mode 100644 index 000000000000..ba17c1a620ec --- /dev/null +++ b/third_party/immer/test/flex_vector/fuzzed-2.cpp @@ -0,0 +1,188 @@ +// +// immer: immutable data structures for C++ +// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente +// +// This software is distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt +// + +#include "extra/fuzzer/fuzzer_input.hpp" +#include <array> +#include <catch.hpp> +#include <immer/flex_vector.hpp> +#include <iostream> + +#define IMMER_FUZZED_TRACE_ENABLE 0 + +#if IMMER_FUZZED_TRACE_ENABLE +#define IMMER_FUZZED_TRACE(...) std::cout << __VA_ARGS__ << std::endl; +#else +#define IMMER_FUZZED_TRACE(...) +#endif + +namespace { + +template <std::size_t VarCount = 2, unsigned Bits = 2> +int run_input(const std::uint8_t* data, std::size_t size) +{ + using vector_t = + immer::flex_vector<int, immer::default_memory_policy, Bits, Bits>; + using size_t = std::uint8_t; + + auto vars = std::array<vector_t, VarCount>{}; + +#if IMMER_FUZZED_TRACE_ENABLE + std::cout << "/// new test run" << std::endl; + for (auto i = 0u; i < VarCount; ++i) + std::cout << "auto var" << i << " = vector_t{};" << std::endl; +#endif + + auto is_valid_var = [&](auto idx) { return idx >= 0 && idx < VarCount; }; + auto is_valid_index = [](auto& v) { + return [&](auto idx) { return idx >= 0 && idx < v.size(); }; + }; + auto is_valid_size = [](auto& v) { + return [&](auto idx) { return idx >= 0 && idx <= v.size(); }; + }; + auto can_concat = [](auto&& v1, auto&& v2) { + using size_type = decltype(v1.size()); + return v2.size() < (std::numeric_limits<size_type>::max() - v1.size()); + }; + auto can_insert = [](auto&& v1) { + using size_type = decltype(v1.size()); + return v1.size() < std::numeric_limits<size_type>::max(); + }; + + return fuzzer_input{data, size}.run([&](auto& in) { + enum ops + { + op_push_back, + op_update, + op_take, + op_drop, + op_concat, + op_push_back_move, + op_update_move, + op_take_move, + op_drop_move, + }; + auto src = read<std::uint8_t>(in, is_valid_var); + auto dst = read<std::uint8_t>(in, is_valid_var); + switch (read<char>(in)) { + case op_push_back: + if (can_insert(vars[src])) { + IMMER_FUZZED_TRACE("var" << +dst << " = var" << +src + << ".push_back(42);"); + vars[dst] = vars[src].push_back(42); + } + break; + case op_update: { + auto idx = read<size_t>(in, is_valid_index(vars[src])); + IMMER_FUZZED_TRACE("var" << +dst << " = var" << +src << ".update(" + << +idx + << ", [] (auto x) { return x + 1; });"); + vars[dst] = vars[src].update(idx, [](auto x) { return x + 1; }); + break; + } + case op_take: { + auto idx = read<size_t>(in, is_valid_size(vars[src])); + IMMER_FUZZED_TRACE("var" << +dst << " = var" << +src << ".take(" + << +idx << ");"); + vars[dst] = vars[src].take(idx); + break; + } + case op_drop: { + auto idx = read<size_t>(in, is_valid_size(vars[src])); + IMMER_FUZZED_TRACE("var" << +dst << " = var" << +src << ".take(" + << +idx << ");"); + vars[dst] = vars[src].drop(idx); + break; + } + case op_concat: { + auto src2 = read<std::uint8_t>(in, is_valid_var); + if (can_concat(vars[src], vars[src2])) { + IMMER_FUZZED_TRACE("var" << +dst << " = var" << +src << " + var" + << +src2 << ";"); + vars[dst] = vars[src] + vars[src2]; + } + break; + } + case op_push_back_move: { + if (can_insert(vars[src])) { + IMMER_FUZZED_TRACE("var" << +dst << " = std::move(var" << +src + << ").push_back(21);"); + vars[dst] = std::move(vars[src]).push_back(21); + } + break; + } + case op_update_move: { + auto idx = read<size_t>(in, is_valid_index(vars[src])); + IMMER_FUZZED_TRACE("var" << +dst << " = std::move(var" << +src + << ").update(" << +idx + << ", [] (auto x) { return x + 1; });"); + vars[dst] = + std::move(vars[src]).update(idx, [](auto x) { return x + 1; }); + break; + } + case op_take_move: { + auto idx = read<size_t>(in, is_valid_size(vars[src])); + IMMER_FUZZED_TRACE("var" << +dst << " = std::move(var" << +src + << ").take(" << +idx << ");"); + vars[dst] = std::move(vars[src]).take(idx); + break; + } + case op_drop_move: { + auto idx = read<size_t>(in, is_valid_size(vars[src])); + IMMER_FUZZED_TRACE("var" << +dst << " = std::move(var" << +src + << ").drop(" << +idx << ");"); + vars[dst] = std::move(vars[src]).drop(idx); + break; + } + default: + break; + }; + return true; + }); +} + +} // anonymous namespace + +TEST_CASE("bug: use after free on move-take") +{ + SECTION("") + { + using vector_t = + immer::flex_vector<int, immer::default_memory_policy, 2, 2>; + auto var0 = vector_t{}; + auto var1 = vector_t{}; + var0 = var0.push_back(42); + var0 = var0.push_back(42); + var0 = var0.push_back(42); + var0 = var0.push_back(42); + var0 = var0 + var0; + var0 = var0 + var0; + var0 = var0 + var0; + var0 = var0 + var0; + var0 = var0.push_back(42); + var0 = var0.push_back(42); + var0 = var0.push_back(42); + var0 = var0.push_back(42); + var0 = var0.push_back(42); + var1 = var0.push_back(42); + var0 = std::move(var0).take(68); + } + +#if __GNUC__ != 9 + SECTION("") + { + constexpr std::uint8_t input[] = { + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, + 0x0, 0x40, 0x0, 0x0, 0x0, 0x0, 0x4, 0x4, 0x0, 0x0, 0x0, 0x4, 0x0, + 0x0, 0x0, 0x4, 0x0, 0x0, 0x0, 0x4, 0x0, 0x0, 0x4, 0x0, 0x0, 0x0, + 0x4, 0x0, 0x0, 0x0, 0x4, 0x0, 0x0, 0x0, 0x4, 0x0, 0x0, 0x0, 0x4, + 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x7, 0x41, 0xb5, 0x0, 0x0, + }; + CHECK(run_input(input, sizeof(input)) == 0); + } +#endif +} diff --git a/third_party/immer/test/flex_vector/fuzzed-3.cpp b/third_party/immer/test/flex_vector/fuzzed-3.cpp new file mode 100644 index 000000000000..5ef1eaffb755 --- /dev/null +++ b/third_party/immer/test/flex_vector/fuzzed-3.cpp @@ -0,0 +1,222 @@ +// +// immer: immutable data structures for C++ +// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente +// +// This software is distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt +// + +#include "extra/fuzzer/fuzzer_input.hpp" +#include <array> +#include <catch.hpp> +#include <immer/flex_vector.hpp> +#include <iostream> + +#define IMMER_FUZZED_TRACE_ENABLE 0 + +#if IMMER_FUZZED_TRACE_ENABLE +#define IMMER_FUZZED_TRACE(...) std::cout << __VA_ARGS__ << std::endl; +#else +#define IMMER_FUZZED_TRACE(...) +#endif + +namespace { + +template <std::size_t VarCount = 2, unsigned Bits = 2> +int run_input(const std::uint8_t* data, std::size_t size) +{ + using vector_t = + immer::flex_vector<int, immer::default_memory_policy, Bits, Bits>; + using size_t = std::uint8_t; + + auto vars = std::array<vector_t, VarCount>{}; + +#if IMMER_FUZZED_TRACE_ENABLE + std::cout << "/// new test run" << std::endl; + for (auto i = 0u; i < VarCount; ++i) + std::cout << "auto var" << i << " = vector_t{};" << std::endl; +#endif + + auto is_valid_var = [&](auto idx) { return idx >= 0 && idx < VarCount; }; + auto is_valid_var_neq = [](auto other) { + return [=](auto idx) { + return idx >= 0 && idx < VarCount && idx != other; + }; + }; + auto is_valid_index = [](auto& v) { + return [&](auto idx) { return idx >= 0 && idx < v.size(); }; + }; + auto is_valid_size = [](auto& v) { + return [&](auto idx) { return idx >= 0 && idx <= v.size(); }; + }; + auto can_concat = [](auto&& v1, auto&& v2) { + using size_type = decltype(v1.size()); + return v2.size() < (std::numeric_limits<size_type>::max() - v1.size()); + }; + auto can_insert = [](auto&& v1) { + using size_type = decltype(v1.size()); + return v1.size() < std::numeric_limits<size_type>::max(); + }; + + return fuzzer_input{data, size}.run([&](auto& in) { + enum ops + { + op_push_back, + op_update, + op_take, + op_drop, + op_concat, + op_push_back_move, + op_update_move, + op_take_move, + op_drop_move, + op_concat_move_l, + op_concat_move_r, + op_concat_move_lr, + }; + auto src = read<std::uint8_t>(in, is_valid_var); + auto dst = read<std::uint8_t>(in, is_valid_var); + switch (read<char>(in)) { + case op_push_back: + if (can_insert(vars[src])) { + IMMER_FUZZED_TRACE("var" << +dst << " = var" << +src + << ".push_back(42);"); + vars[dst] = vars[src].push_back(42); + } + break; + case op_update: { + auto idx = read<size_t>(in, is_valid_index(vars[src])); + IMMER_FUZZED_TRACE("var" << +dst << " = var" << +src << ".update(" + << +idx + << ", [] (auto x) { return x + 1; });"); + vars[dst] = vars[src].update(idx, [](auto x) { return x + 1; }); + break; + } + case op_take: { + auto idx = read<size_t>(in, is_valid_size(vars[src])); + IMMER_FUZZED_TRACE("var" << +dst << " = var" << +src << ".take(" + << +idx << ");"); + vars[dst] = vars[src].take(idx); + break; + } + case op_drop: { + auto idx = read<size_t>(in, is_valid_size(vars[src])); + IMMER_FUZZED_TRACE("var" << +dst << " = var" << +src << ".take(" + << +idx << ");"); + vars[dst] = vars[src].drop(idx); + break; + } + case op_concat: { + auto src2 = read<std::uint8_t>(in, is_valid_var); + if (can_concat(vars[src], vars[src2])) { + IMMER_FUZZED_TRACE("var" << +dst << " = var" << +src << " + var" + << +src2 << ";"); + vars[dst] = vars[src] + vars[src2]; + } + break; + } + case op_push_back_move: { + if (can_insert(vars[src])) { + IMMER_FUZZED_TRACE("var" << +dst << " = std::move(var" << +src + << ").push_back(21);"); + vars[dst] = std::move(vars[src]).push_back(21); + } + break; + } + case op_update_move: { + auto idx = read<size_t>(in, is_valid_index(vars[src])); + IMMER_FUZZED_TRACE("var" << +dst << " = std::move(var" << +src + << ").update(" << +idx + << ", [] (auto x) { return x + 1; });"); + vars[dst] = + std::move(vars[src]).update(idx, [](auto x) { return x + 1; }); + break; + } + case op_take_move: { + auto idx = read<size_t>(in, is_valid_size(vars[src])); + IMMER_FUZZED_TRACE("var" << +dst << " = std::move(var" << +src + << ").take(" << +idx << ");"); + vars[dst] = std::move(vars[src]).take(idx); + break; + } + case op_drop_move: { + auto idx = read<size_t>(in, is_valid_size(vars[src])); + IMMER_FUZZED_TRACE("var" << +dst << " = std::move(var" << +src + << ").drop(" << +idx << ");"); + vars[dst] = std::move(vars[src]).drop(idx); + break; + } + case op_concat_move_l: { + auto src2 = read<std::uint8_t>(in, is_valid_var_neq(src)); + if (can_concat(vars[src], vars[src2])) { + IMMER_FUZZED_TRACE("var" << +dst << " = std::move(var" << +src + << ") + var" << +src2 << ";"); + vars[dst] = std::move(vars[src]) + vars[src2]; + } + break; + } + case op_concat_move_r: { + auto src2 = read<std::uint8_t>(in, is_valid_var_neq(src)); + if (can_concat(vars[src], vars[src2])) { + IMMER_FUZZED_TRACE("var" << +dst << " = var" << +src + << " + std::move(var" << +src2 + << ");"); + vars[dst] = vars[src] + std::move(vars[src2]); + } + break; + } + case op_concat_move_lr: { + auto src2 = read<std::uint8_t>(in, is_valid_var_neq(src)); + if (can_concat(vars[src], vars[src2])) { + IMMER_FUZZED_TRACE("var" << +dst << " = std::move(var" << +src + << ") + std::move(var" << +src2 + << ");"); + vars[dst] = std::move(vars[src]) + std::move(vars[src2]); + } + break; + } + default: + break; + }; + return true; + }); +} + +} // namespace + +TEST_CASE("bug: concat with moving the right side") +{ + // This was a stupid bug on the concatenation of small vectors + // when moving one of the sides. + SECTION("simplified") + { + using vector_t = + immer::flex_vector<int, immer::default_memory_policy, 2, 2>; + auto var0 = vector_t{}; + auto var1 = vector_t{}; + var0 = var0.push_back(42); + var0 = var0.push_back(42); + var0 = var0.push_back(42); + var1 = var0.push_back(42); + var0 = var0 + var0; + var0 = var0.push_back(42); + var0 = var0 + var1; + var0 = var0 + var0; + var0 = var0 + std::move(var1); + } + +#if __GNUC__ != 9 + SECTION("vm") + { + constexpr std::uint8_t input[] = { + 0x0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0, + 0x0, 0x0, 0x0, 0x1, 0x0, 0x40, 0x28, 0x0, 0x4, 0x3f, + 0x20, 0x0, 0x0, 0x4, 0x3f, 0x8, 0x0, 0x0, 0x0, 0x0, + 0x1, 0x0, 0x0, 0x0, 0x4, 0x3f, 0x0, 0x0, 0x40, 0x0, + 0x0, 0x0, 0x0, 0x4, 0x3f, 0x1, 0x0, 0x0, 0x4, 0x3f, + 0x0, 0x0, 0xff, 0x0, 0xa, 0x1, 0x0, 0xff, 0x0, 0x0, + }; + CHECK(run_input(input, sizeof(input)) == 0); + } +#endif +} diff --git a/third_party/immer/test/flex_vector/fuzzed-4.cpp b/third_party/immer/test/flex_vector/fuzzed-4.cpp new file mode 100644 index 000000000000..d9ecdd7fba7f --- /dev/null +++ b/third_party/immer/test/flex_vector/fuzzed-4.cpp @@ -0,0 +1,364 @@ +// +// immer: immutable data structures for C++ +// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente +// +// This software is distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt +// + +#include "extra/fuzzer/fuzzer_input.hpp" +#include <array> +#include <catch.hpp> +#include <immer/flex_vector.hpp> +#include <immer/flex_vector_transient.hpp> +#include <immer/heap/gc_heap.hpp> +#include <immer/refcount/no_refcount_policy.hpp> +#include <iostream> + +#define IMMER_FUZZED_TRACE_ENABLE 0 + +#if IMMER_FUZZED_TRACE_ENABLE +#define IMMER_FUZZED_TRACE(...) std::cout << __VA_ARGS__ << std::endl; +#else +#define IMMER_FUZZED_TRACE(...) +#endif + +using gc_memory = immer::memory_policy<immer::heap_policy<immer::gc_heap>, + immer::no_refcount_policy, + immer::gc_transience_policy, + false>; + +namespace { + +template <std::size_t VarCount = 4, unsigned Bits = 2> +int run_input(const std::uint8_t* data, std::size_t size) +{ + using vector_t = immer::flex_vector<int, gc_memory, Bits, Bits>; + using transient_t = typename vector_t::transient_type; + using size_t = std::uint8_t; + + auto vs = std::array<vector_t, VarCount>{}; + auto ts = std::array<transient_t, VarCount>{}; + +#if IMMER_FUZZED_TRACE_ENABLE + std::cout << "/// new test run" << std::endl; + for (auto i = 0; i < VarCount; ++i) + std::cout << "auto v" << i << " = vector_t{};" << std::endl; + for (auto i = 0; i < VarCount; ++i) + std::cout << "auto t" << i << " = transient_t{};" << std::endl; +#endif + + auto is_valid_var = [&](auto idx) { return idx >= 0 && idx < VarCount; }; + auto is_valid_var_neq = [](auto other) { + return [=](auto idx) { + return idx >= 0 && idx < VarCount && idx != other; + }; + }; + auto is_valid_index = [](auto& v) { + return [&](auto idx) { return idx >= 0 && idx < v.size(); }; + }; + auto is_valid_size = [](auto& v) { + return [&](auto idx) { return idx >= 0 && idx <= v.size(); }; + }; + auto can_concat = [](auto&& v1, auto&& v2) { + using size_type = decltype(v1.size()); + auto max = std::numeric_limits<size_type>::max() >> (Bits * 4); + return v1.size() < max && v2.size() < max; + }; + + return fuzzer_input{data, size}.run([&](auto& in) { + enum ops + { + op_transient, + op_persistent, + op_push_back, + op_update, + op_take, + op_drop, + op_concat, + op_push_back_mut, + op_update_mut, + op_take_mut, + op_drop_mut, + op_prepend_mut, + op_prepend_mut_move, + op_append_mut, + op_append_mut_move, + }; + auto dst = read<std::uint8_t>(in, is_valid_var); + switch (read<char>(in)) { + case op_transient: { + auto src = read<std::uint8_t>(in, is_valid_var); + IMMER_FUZZED_TRACE("t" << +dst << " = v" << +src + << ".transient();"); + ts[dst] = vs[src].transient(); + break; + } + case op_persistent: { + auto src = read<std::uint8_t>(in, is_valid_var); + IMMER_FUZZED_TRACE("v" << +dst << " = t" << +src + << ".persistent();"); + vs[dst] = ts[src].persistent(); + break; + } + case op_push_back: { + auto src = read<std::uint8_t>(in, is_valid_var); + IMMER_FUZZED_TRACE("v" << +dst << " = v" << +src + << ".push_back(42);"); + vs[dst] = vs[src].push_back(42); + break; + } + case op_update: { + auto src = read<std::uint8_t>(in, is_valid_var); + auto idx = read<size_t>(in, is_valid_index(vs[src])); + IMMER_FUZZED_TRACE("v" << +dst << " = v" << +src << ".update(" + << +idx + << ", [] (auto x) { return x + 1; });"); + vs[dst] = vs[src].update(idx, [](auto x) { return x + 1; }); + break; + } + case op_take: { + auto src = read<std::uint8_t>(in, is_valid_var); + auto idx = read<size_t>(in, is_valid_size(vs[src])); + IMMER_FUZZED_TRACE("v" << +dst << " = v" << +src << ".take(" << +idx + << ");"); + vs[dst] = vs[src].take(idx); + break; + } + case op_drop: { + auto src = read<std::uint8_t>(in, is_valid_var); + auto idx = read<size_t>(in, is_valid_size(vs[src])); + IMMER_FUZZED_TRACE("v" << +dst << " = v" << +src << ".take(" << +idx + << ");"); + vs[dst] = vs[src].drop(idx); + break; + } + case op_concat: { + auto src = read<std::uint8_t>(in, is_valid_var); + auto src2 = read<std::uint8_t>(in, is_valid_var); + if (can_concat(vs[src], vs[src2])) { + IMMER_FUZZED_TRACE("v" << +dst << " = v" << +src << " + v" + << +src2 << ";"); + vs[dst] = vs[src] + vs[src2]; + } + break; + } + case op_push_back_mut: { + IMMER_FUZZED_TRACE("t" << +dst << ".push_back(13);"); + ts[dst].push_back(13); + break; + } + case op_update_mut: { + auto idx = read<size_t>(in, is_valid_index(ts[dst])); + IMMER_FUZZED_TRACE("t" << +dst << ".update(" << +idx + << ", [] (auto x) { return x + 1; });"); + ts[dst].update(idx, [](auto x) { return x + 1; }); + break; + } + case op_take_mut: { + auto idx = read<size_t>(in, is_valid_size(ts[dst])); + IMMER_FUZZED_TRACE("t" << +dst << ").take(" << +idx << ");"); + ts[dst].take(idx); + break; + } + case op_prepend_mut: { + auto src = read<std::uint8_t>(in, is_valid_var_neq(dst)); + if (can_concat(ts[dst], ts[src])) { + IMMER_FUZZED_TRACE("t" << +dst << ".prepend(t" << +src << ");"); + ts[dst].prepend(ts[src]); + } + break; + } + case op_prepend_mut_move: { + auto src = read<std::uint8_t>(in, is_valid_var_neq(dst)); + if (can_concat(ts[dst], ts[src])) { + IMMER_FUZZED_TRACE("t" << +dst << ".prepend(std::move(t" << +src + << "));" + << " t" << +src << " = {};"); + ts[dst].prepend(std::move(ts[src])); + ts[src] = {}; + } + break; + } + case op_append_mut: { + auto src = read<std::uint8_t>(in, is_valid_var_neq(dst)); + if (can_concat(ts[dst], ts[src])) { + IMMER_FUZZED_TRACE("t" << +dst << ".append(t" << +src << ");"); + ts[dst].append(ts[src]); + } + break; + } + case op_append_mut_move: { + auto src = read<std::uint8_t>(in, is_valid_var_neq(dst)); + if (can_concat(ts[dst], ts[src])) { + IMMER_FUZZED_TRACE("t" << +dst << ".append(std::move(t" << +src + << "));" + << " t" << +src << " = {};"); + ts[dst].append(std::move(ts[src])); + ts[src] = {}; + } + break; + } + default: + break; + }; + return true; + }); +} + +} // namespace + +TEST_CASE("bug: concatenating transients") +{ + // When concatenating two transients vectors the nodes from the + // argument become aliased in the result. As such, we need to + // reset the identitiy of the argument. + SECTION("simplified") + { + using vector_t = immer::flex_vector<int, gc_memory, 2, 2>; + auto t0 = vector_t{}.transient(); + t0.push_back(42); + t0.push_back(42); + t0.push_back(42); + t0.push_back(42); + t0.push_back(42); + t0.push_back(42); + auto t1 = t0; + t1.append(t0); + t1.append(t0); + t0.append(t1); + t1.append(t0); + } + +#if __GNUC__ != 9 + SECTION("") + { + constexpr std::uint8_t input[] = { + 0x2, 0x2, 0x2, 0x2, 0x29, 0x32, 0x0, 0x0, 0x2, 0x2, 0x2, + 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x6, 0x2, + 0x2, 0x2, 0x2, 0x2, 0x6, 0x2, 0x2, 0x2, 0x0, 0x38, 0x2, + 0x0, 0x0, 0x2, 0x2, 0xd, 0x0, 0x0, 0x3b, 0xff, 0x3a, 0x2, + 0xd, 0xd, 0x2, 0x0, 0x0, 0x10, 0xe, 0x0, 0xd, 0x0, 0x0, + 0x2, 0x2, 0xd, 0x0, 0x1, 0x5, + }; + CHECK(run_input(input, sizeof(input)) == 0); + } +#endif +} + +TEST_CASE("bug: concatenating moved transients") +{ + // A moved from concatenated transient is totally smashed, we can + // not do anything with it but reasign... + SECTION("simplified") + { + using vector_t = immer::flex_vector<int, gc_memory, 2, 2>; + using transient_t = typename vector_t::transient_type; + auto v0 = vector_t{}; + auto t0 = transient_t{}; + auto t2 = transient_t{}; + v0 = v0.push_back(42); + t2 = v0.transient(); + v0 = v0 + v0; + v0 = v0 + v0; + v0 = v0 + v0; + v0 = v0 + v0; + t0 = v0.transient(); + t0 = v0.transient(); + t0.append(std::move(t2)); + t2 = {}; + t2.append(std::move(t0)); + } + +#if __GNUC__ != 9 + SECTION("") + { + constexpr std::uint8_t input[] = { + 0x0, 0x2, 0x0, 0x2, 0x0, 0x0, 0x0, 0x6, 0x0, 0x0, 0x0, + 0x6, 0x0, 0x0, 0x0, 0x9d, 0x0, 0x6, 0x0, 0x0, 0x0, 0x6, + 0x0, 0x0, 0x0, 0x9d, 0x28, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf7, + 0xc5, 0x0, 0xa, 0xa, 0x0, 0xfa, 0xe7, 0xff, 0xe7, 0xff, 0x0, + 0xe, 0x2, 0x9, 0x0, 0x28, 0x2, 0xe, 0x0, 0x0, 0x2, 0xd, + 0x0, 0x0, 0x28, 0x0, 0xd, 0x2, 0x5, 0x0, 0x2, + }; + CHECK(run_input(input, sizeof(input)) == 0); + } +#endif + + SECTION("simplified") + { + using vector_t = immer::flex_vector<int, gc_memory, 2, 2>; + using transient_t = typename vector_t::transient_type; + auto v0 = vector_t{}; + auto t0 = transient_t{}; + auto t2 = transient_t{}; + v0 = v0.push_back(42); + v0 = v0.push_back(42); + v0 = v0 + v0; + t0 = v0.transient(); + t2.prepend(std::move(t0)); + t0 = {}; + t0 = v0.transient(); + t0.push_back(13); + t2.append(std::move(t0)); + t0 = {}; + t0 = v0.transient(); + t0.push_back(13); + t2.prepend(std::move(t0)); + t0 = {}; + } + +#if __GNUC__ != 9 + SECTION("") + { + return; + constexpr std::uint8_t input[] = { + 0x0, 0x2, 0x0, 0x0, 0x2, 0xb7, 0x1, 0x36, 0x40, 0x0, 0x0, + 0x0, 0x0, 0xb6, 0x0, 0x2, 0x0, 0x0, 0x6, 0xe, 0x0, 0x0, + 0xfe, 0x0, 0x0, 0xff, 0x0, 0x2, 0xc, 0xff, 0xfc, 0x29, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x7, 0x2, 0xe, 0xff, 0xfc, 0x29, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x7, 0x3, 0x0, 0x0, 0x2, 0xc, 0x2, + 0xc, 0x0, 0xd, 0x0, 0x0, 0x0, 0x0, 0x25, 0x6, + }; + CHECK(run_input(input, sizeof(input)) == 0); + } +#endif +} + +TEST_CASE("bug: aegsdas") +{ + SECTION("simplified") + { + using vector_t = immer::flex_vector<int, gc_memory, 2, 2>; + using transient_t = typename vector_t::transient_type; + auto v2 = vector_t{}; + auto t0 = transient_t{}; + auto t1 = transient_t{}; + v2 = v2.push_back(42); + v2 = v2.push_back(42); + v2 = v2.push_back(42); + v2 = v2.push_back(42); + v2 = v2.push_back(42); + t0 = v2.transient(); + t1.prepend(t0); + t1.prepend(t0); + t1.prepend(t0); + t0.prepend(std::move(t1)); + t1 = {}; + } + +#if __GNUC__ != 9 + SECTION("") + { + constexpr std::uint8_t input[] = { + 0xff, 0xff, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, + 0x82, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x0, 0x0, 0x3d, 0x0, + 0x0, 0x0, 0x2, 0x84, 0x0, 0x3b, 0x1, 0xb, 0x0, 0xa, 0x1, + 0xb, 0x0, 0x0, 0x3, 0x2, 0x0, 0x3b, 0x1, 0xb, 0x1, 0x0, + 0x0, 0xc, 0xb, 0x1, 0x8, 0xff, 0xff, 0xfc, 0xfd, 0x0, 0x3b, + 0x3, 0x2, 0x0, 0x3b, 0x1, 0x9, 0x1, 0x3b, + }; + CHECK(run_input(input, sizeof(input)) == 0); + } +#endif +} diff --git a/third_party/immer/test/flex_vector/gc.cpp b/third_party/immer/test/flex_vector/gc.cpp new file mode 100644 index 000000000000..57b97f5ed7d7 --- /dev/null +++ b/third_party/immer/test/flex_vector/gc.cpp @@ -0,0 +1,27 @@ +// +// immer: immutable data structures for C++ +// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente +// +// This software is distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt +// + +#include <immer/flex_vector.hpp> +#include <immer/heap/gc_heap.hpp> +#include <immer/refcount/no_refcount_policy.hpp> +#include <immer/vector.hpp> + +using gc_memory = immer::memory_policy<immer::heap_policy<immer::gc_heap>, + immer::no_refcount_policy, + immer::gc_transience_policy, + false>; + +template <typename T> +using test_flex_vector_t = immer::flex_vector<T, gc_memory, 3u>; + +template <typename T> +using test_vector_t = immer::vector<T, gc_memory, 3u>; + +#define FLEX_VECTOR_T test_flex_vector_t +#define VECTOR_T test_vector_t +#include "generic.ipp" diff --git a/third_party/immer/test/flex_vector/generic.ipp b/third_party/immer/test/flex_vector/generic.ipp new file mode 100644 index 000000000000..6dbd73f07cb6 --- /dev/null +++ b/third_party/immer/test/flex_vector/generic.ipp @@ -0,0 +1,599 @@ +// +// immer: immutable data structures for C++ +// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente +// +// This software is distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt +// + +#include "test/dada.hpp" +#include "test/transient_tester.hpp" +#include "test/util.hpp" + +#include <immer/algorithm.hpp> + +#include <boost/range/adaptors.hpp> +#include <boost/range/irange.hpp> +#include <catch.hpp> + +#include <algorithm> +#include <array> +#include <numeric> +#include <vector> + +#ifndef FLEX_VECTOR_T +#error "define the vector template to use in FLEX_VECTOR_T" +#endif + +#ifndef VECTOR_T +#error "define the vector template to use in VECTOR_T" +#endif + +template <typename V = FLEX_VECTOR_T<unsigned>> +auto make_test_flex_vector(unsigned min, unsigned max) +{ + auto v = V{}; + for (auto i = min; i < max; ++i) + v = v.push_back({i}); + return v; +} + +template <typename V = FLEX_VECTOR_T<unsigned>> +auto make_test_flex_vector_front(unsigned min, unsigned max) +{ + auto v = V{}; + for (auto i = max; i > min;) + v = v.push_front({--i}); + return v; +} + +template <std::size_t N> +auto make_many_test_flex_vector() +{ + using vektor_t = FLEX_VECTOR_T<unsigned>; + auto many = std::array<vektor_t, N>{}; + std::generate_n(many.begin(), N, [v = vektor_t{}, i = 0u]() mutable { + auto r = v; + v = v.push_back(i++); + return r; + }); + return many; +} + +template <std::size_t N> +auto make_many_test_flex_vector_front() +{ + using vektor_t = FLEX_VECTOR_T<unsigned>; + auto many = std::array<vektor_t, N>{}; + std::generate_n(many.begin(), N, [i = 0u]() mutable { + return make_test_flex_vector_front(0, i++); + }); + return many; +} + +template <std::size_t N> +auto make_many_test_flex_vector_front_remainder() +{ + using vektor_t = FLEX_VECTOR_T<unsigned>; + auto many = std::array<vektor_t, N>{}; + std::generate_n(many.begin(), N, [v = vektor_t{}, i = N - 1]() mutable { + auto r = v; + v = v.push_front(--i); + return r; + }); + return many; +} + +TEST_CASE("set relaxed") +{ + auto v = make_test_flex_vector_front(0, 666u); + for (decltype(v.size()) i = 0; i < v.size(); ++i) { + v = v.set(i, i + 1); + CHECK(v[i] == i + 1); + } +} + +TEST_CASE("push_front") +{ + const auto n = 666u; + auto v = FLEX_VECTOR_T<unsigned>{}; + + for (auto i = 0u; i < n; ++i) { + v = v.push_front(i); + CHECK(v.size() == i + 1); + for (decltype(v.size()) j = 0; j < v.size(); ++j) + CHECK(v[v.size() - j - 1] == j); + } +} + +TEST_CASE("concat") +{ +#if IMMER_SLOW_TESTS + constexpr auto n = 666u; +#else + constexpr auto n = 101u; +#endif + + auto all_lhs = make_many_test_flex_vector<n>(); + auto all_rhs = make_many_test_flex_vector_front_remainder<n>(); + + SECTION("regular plus regular") + { + for (auto i : test_irange(0u, n)) { + auto c = all_lhs[i] + all_lhs[i]; + CHECK_VECTOR_EQUALS( + c, boost::join(boost::irange(0u, i), boost::irange(0u, i))); + } + } + + SECTION("regular plus relaxed") + { + for (auto i : test_irange(0u, n)) { + auto c = all_lhs[i] + all_rhs[n - i - 1]; + CHECK_VECTOR_EQUALS(c, boost::irange(0u, n - 1)); + } + } +} + +auto make_flex_vector_concat(std::size_t min, std::size_t max) +{ + using vektor_t = FLEX_VECTOR_T<unsigned>; + + if (max == min) + return vektor_t{}; + else if (max == min + 1) + return vektor_t{}.push_back(min); + else { + auto mid = min + (max - min) / 2; + return make_flex_vector_concat(min, mid) + + make_flex_vector_concat(mid, max); + } +} + +TEST_CASE("concat recursive") +{ + const auto n = 666u; + auto v = make_flex_vector_concat(0, n); + CHECK_VECTOR_EQUALS(v, boost::irange(0u, n)); +} + +TEST_CASE("insert") +{ + SECTION("normal") + { + const auto n = 666u; + auto v = make_test_flex_vector(0, n); + v = v.insert(42, 100); + CHECK_VECTOR_EQUALS(v, + boost::join(boost::irange(0u, 42u), + boost::join(boost::irange(100u, 101u), + boost::irange(42u, n)))); + } + + SECTION("move") + { + const auto n = 666u; + auto v = make_test_flex_vector(0, n); + v = std::move(v).insert(42, 100); + CHECK_VECTOR_EQUALS(v, + boost::join(boost::irange(0u, 42u), + boost::join(boost::irange(100u, 101u), + boost::irange(42u, n)))); + } + + SECTION("vec") + { + const auto n = 666u; + auto v = make_test_flex_vector(0, n); + v = std::move(v).insert(42, {100, 101, 102}); + CHECK_VECTOR_EQUALS(v, + boost::join(boost::irange(0u, 42u), + boost::join(boost::irange(100u, 103u), + boost::irange(42u, n)))); + } + + SECTION("vec move") + { + const auto n = 666u; + auto v = make_test_flex_vector(0, n); + v = std::move(v).insert(42, {100, 101, 102}); + CHECK_VECTOR_EQUALS(v, + boost::join(boost::irange(0u, 42u), + boost::join(boost::irange(100u, 103u), + boost::irange(42u, n)))); + } +} + +TEST_CASE("erase") +{ + const auto n = 666u; + auto v = make_test_flex_vector(0, n); + auto vv = v.erase(0); + CHECK_VECTOR_EQUALS(vv, boost::irange(1u, n)); + CHECK_VECTOR_EQUALS(v.erase(v.size() - 1), boost::irange(0u, n - 1)); + CHECK_VECTOR_EQUALS(v.erase(v.size() - 1), boost::irange(0u, n - 1)); + CHECK_VECTOR_EQUALS( + v.erase(42), + boost::join(boost::irange(0u, 42u), boost::irange(43u, n))); + CHECK_VECTOR_EQUALS(v.erase(v.size() - 1, v.size()), + boost::irange(0u, n - 1)); + CHECK_VECTOR_EQUALS(v.erase(0, 0), boost::irange(0u, n)); + CHECK_VECTOR_EQUALS( + v.erase(42, 50), + boost::join(boost::irange(0u, 42u), boost::irange(50u, n))); +} + +TEST_CASE("accumulate relaxed") +{ + auto expected_n = [](auto n) { return n * (n - 1) / 2; }; + auto expected_i = [&](auto i, auto n) { + return expected_n(n) - expected_n(i); + }; + + SECTION("sum") + { + const auto n = 666u; + auto v = make_test_flex_vector_front(0, n); + + auto sum = immer::accumulate(v, 0u); + auto expected = v.size() * (v.size() - 1) / 2; + CHECK(sum == expected); + } + + SECTION("sum complex") + { + const auto n = 20u; + + auto v = FLEX_VECTOR_T<unsigned>{}; + for (auto i = 0u; i < n; ++i) + v = v.push_front(i) + v; + + auto sum = immer::accumulate(v, 0u); + auto expected = (1 << n) - n - 1; + CHECK(sum == expected); + } + + SECTION("sum range") + { + using namespace std; + const auto n = 666u; + auto v = make_test_flex_vector_front(0, n); + { + auto sum = immer::accumulate(begin(v) + 100, begin(v) + 300, 0u); + CHECK(sum == expected_i(100, 300)); + } + { + auto sum = immer::accumulate(begin(v) + 31, begin(v) + 300, 0u); + CHECK(sum == expected_i(31, 300)); + } + { + auto sum = immer::accumulate(begin(v), begin(v) + 33, 0u); + CHECK(sum == expected_i(0, 33)); + } + { + auto sum = immer::accumulate(begin(v) + 100, begin(v) + 660, 0u); + CHECK(sum == expected_i(100, 660)); + } + { + auto sum = immer::accumulate(begin(v) + 100, begin(v) + 105, 0u); + CHECK(sum == expected_i(100, 105)); + } + { + auto sum = immer::accumulate(begin(v) + 660, begin(v) + 664, 0u); + CHECK(sum == expected_i(660, 664)); + } + } +} + +TEST_CASE("equals") +{ + const auto n = 666u; + auto v = make_test_flex_vector_front(0, n); + + CHECK(v == v); + CHECK(v == v.set(42, 42)); + CHECK(v != v.set(42, 24)); + CHECK(v == v.set(42, 24).set(42, 42)); + CHECK(v.set(42, 24) == v.set(42, 24)); + CHECK(v != v.push_back(7)); + CHECK(v.push_back(7) == v.push_back(7)); + CHECK(v.push_back(5) != v.push_back(7)); + CHECK(v != v.set(v.size() - 2, 24)); + CHECK(v == v.set(v.size() - 2, 24).set(v.size() - 2, v[v.size() - 2])); + CHECK(v == v.insert(42, 12).erase(42)); + CHECK(v == v.insert(0, 12).erase(0)); +} + +TEST_CASE("equals bugs") +{ + { + const auto n = 666u; + auto v = make_test_flex_vector(0, n); + CHECK(v == v.insert(42, 12).erase(42)); + CHECK(v == v.insert(0, 12).erase(0)); + } + { + const auto n = 30u; + auto v = make_test_flex_vector(0, n); + CHECK(v == v.insert(10, 12).erase(10)); + CHECK(v == v.insert(0, 12).erase(0)); + } + { + const auto n = 666u; + auto v = make_test_flex_vector(0, n); + for (auto i : test_irange(0u, n)) + CHECK(v == v.insert(i, 42).erase(i)); + } + { + const auto n = 666u; + auto v = make_test_flex_vector_front(0, n); + for (auto i : test_irange(0u, n)) + CHECK(v == v.insert(i, 42).erase(i)); + } + { + const auto n = 666u; + auto v = FLEX_VECTOR_T<unsigned>{}; + using size_t = decltype(v.size()); + for (auto i : test_irange(0u, n)) { + while (v.size() < i) + v = std::move(v).push_back(i); + auto vv = v; + for (auto j : test_irange(size_t{}, v.size())) { + auto vz = vv.insert(j, 42).erase(j); + CHECK(v == vz); + CHECK(vv == vz); + vv = vz; + } + } + } +} + +TEST_CASE("take relaxed") +{ + const auto n = 666u; + auto v = make_test_flex_vector_front(0, n); + + for (auto i : test_irange(0u, n)) { + auto vv = v.take(i); + CHECK_VECTOR_EQUALS_RANGE(vv, v.begin(), v.begin() + i); + } +} + +TEST_CASE("drop") +{ + const auto n = 666u; + + SECTION("regular") + { + auto v = make_test_flex_vector(0, n); + + for (auto i : test_irange(0u, n)) { + auto vv = v.drop(i); + CHECK_VECTOR_EQUALS_RANGE(vv, v.begin() + i, v.end()); + } + } + + SECTION("relaxed") + { + auto v = make_test_flex_vector_front(0, n); + + for (auto i : test_irange(0u, n)) { + auto vv = v.drop(i); + CHECK_VECTOR_EQUALS_RANGE(vv, v.begin() + i, v.end()); + } + } +} + +#if IMMER_SLOW_TESTS +TEST_CASE("reconcat") +{ + constexpr auto n = 666u; + auto v = make_test_flex_vector_front(0, n); + auto all_lhs = make_many_test_flex_vector_front<n + 1>(); + auto all_rhs = make_many_test_flex_vector_front_remainder<n + 1>(); + + for (auto i = 0u; i < n; ++i) { + auto vv = all_lhs[i] + all_rhs[n - i]; + CHECK_VECTOR_EQUALS(vv, v); + CHECK_SLOW(vv == v); + } +} + +TEST_CASE("reconcat drop") +{ + constexpr auto n = 666u; + auto v = make_test_flex_vector_front(0, n); + auto all_lhs = make_many_test_flex_vector_front<n + 1>(); + + for (auto i = 0u; i < n; ++i) { + auto vv = all_lhs[i] + v.drop(i); + CHECK_VECTOR_EQUALS(vv, v); + CHECK_SLOW(vv == v); + } +} + +TEST_CASE("reconcat take") +{ + constexpr auto n = 666u; + auto v = make_test_flex_vector_front(0, n); + auto all_rhs = make_many_test_flex_vector_front_remainder<n + 1>(); + + for (auto i = 0u; i < n; ++i) { + auto vv = v.take(i) + all_rhs[n - i]; + CHECK_VECTOR_EQUALS(vv, v); + CHECK_SLOW(vv == v); + } +} +#endif + +TEST_CASE("reconcat take drop") +{ + const auto n = 666u; + auto v = make_test_flex_vector_front(0, n); + + for (auto i : test_irange(0u, n)) { + auto vv = v.take(i) + v.drop(i); + CHECK_VECTOR_EQUALS(vv, v); + CHECK_SLOW(vv == v); + } +} + +TEST_CASE("reconcat take drop feedback") +{ + const auto n = 666u; + auto v = make_test_flex_vector_front(0, n); + auto vv = v; + for (auto i : test_irange(0u, n)) { + vv = vv.take(i) + vv.drop(i); + CHECK_VECTOR_EQUALS(vv, v); + CHECK_SLOW(vv == v); + } +} + +TEST_CASE("iterator relaxed") +{ + const auto n = 666u; + auto v = make_test_flex_vector_front(0, n); + + SECTION("works with range loop") + { + auto i = 0u; + for (const auto& x : v) + CHECK(x == i++); + CHECK(i == v.size()); + } + + SECTION("works with standard algorithms") + { + auto s = std::vector<unsigned>(n); + std::iota(s.begin(), s.end(), 0u); + std::equal(v.begin(), v.end(), s.begin(), s.end()); + } + + SECTION("can go back from end") + { + auto expected = n - 1; + CHECK(expected == *--v.end()); + } + + SECTION("works with reversed range adaptor") + { + auto r = v | boost::adaptors::reversed; + auto i = n; + for (const auto& x : r) + CHECK(x == --i); + } + + SECTION("works with strided range adaptor") + { + auto r = v | boost::adaptors::strided(5); + auto i = 0u; + for (const auto& x : r) + CHECK(x == 5 * i++); + } + + SECTION("works reversed") + { + auto i = n; + for (auto iter = v.rbegin(), last = v.rend(); iter != last; ++iter) + CHECK(*iter == --i); + } + + SECTION("advance and distance") + { + auto i1 = v.begin(); + auto i2 = i1 + 100; + CHECK(100u == *i2); + CHECK(100 == i2 - i1); + CHECK(50u == *(i2 - 50)); + CHECK(-30 == (i2 - 30) - i2); + } +} + +TEST_CASE("adopt regular vector contents") +{ + const auto n = 666u; + auto v = VECTOR_T<unsigned>{}; + for (auto i = 0u; i < n; ++i) { + v = v.push_back(i); + auto fv = FLEX_VECTOR_T<unsigned>{v}; + CHECK_VECTOR_EQUALS_AUX(v, fv, [](auto&& v) { return &v; }); + } +} + +TEST_CASE("exception safety relaxed") +{ + using dadaist_vector_t = + typename dadaist_wrapper<FLEX_VECTOR_T<unsigned>>::type; + constexpr auto n = 666u; + + SECTION("push back") + { + auto half = n / 2; + auto v = make_test_flex_vector_front<dadaist_vector_t>(0, half); + auto d = dadaism{}; + for (auto i = half; v.size() < static_cast<decltype(v.size())>(n);) { + auto s = d.next(); + try { + v = v.push_back({i}); + ++i; + } catch (dada_error) {} + CHECK_VECTOR_EQUALS(v, boost::irange(0u, i)); + } + CHECK(d.happenings > 0); + IMMER_TRACE_E(d.happenings); + } + + SECTION("update") + { + auto v = make_test_flex_vector_front<dadaist_vector_t>(0, n); + auto d = dadaism{}; + for (auto i = 0u; i < n;) { + auto s = d.next(); + try { + v = v.update(i, [](auto x) { return dada(), x + 1; }); + ++i; + } catch (dada_error) {} + CHECK_VECTOR_EQUALS( + v, boost::join(boost::irange(1u, 1u + i), boost::irange(i, n))); + } + CHECK(d.happenings > 0); + IMMER_TRACE_E(d.happenings); + } + + SECTION("take") + { + auto v = make_test_flex_vector_front<dadaist_vector_t>(0, n); + auto d = dadaism{}; + for (auto i = 0u; i < n;) { + auto s = d.next(); + auto r = dadaist_vector_t{}; + try { + r = v.take(i); + CHECK_VECTOR_EQUALS(r, boost::irange(0u, i++)); + } catch (dada_error) { + CHECK_VECTOR_EQUALS(r, boost::irange(0u, 0u)); + } + } + CHECK(d.happenings > 0); + IMMER_TRACE_E(d.happenings); + } + + SECTION("concat") + { + auto v = make_test_flex_vector<dadaist_vector_t>(0, n); + auto d = dadaism{}; + for (auto i = 0u; i < n;) { + auto lhs = v.take(i); + auto rhs = v.drop(i); + auto s = d.next(); + try { + v = lhs + rhs; + ++i; + } catch (dada_error) {} + CHECK_VECTOR_EQUALS(v, boost::irange(0u, n)); + } + CHECK(d.happenings > 0); + IMMER_TRACE_E(d.happenings); + } +} diff --git a/third_party/immer/test/flex_vector/issue-45.cpp b/third_party/immer/test/flex_vector/issue-45.cpp new file mode 100644 index 000000000000..76bd77b2e240 --- /dev/null +++ b/third_party/immer/test/flex_vector/issue-45.cpp @@ -0,0 +1,35 @@ +// +// immer: immutable data structures for C++ +// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente +// +// This software is distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt +// + +// Thanks Guiguiprim for reporting this issue +// https://github.com/arximboldi/immer/issues/46 + +#include <immer/flex_vector.hpp> +#include <immer/vector.hpp> +#include <immer/vector_transient.hpp> + +#include <catch.hpp> + +#if IMMER_CXX_STANDARD >= 17 + +#include <variant> + +TEST_CASE("error when erasing an element from a " + "immer::flex_vector<std::variant/optional/any>") +{ + using Vector = immer::flex_vector<std::variant<int, double>>; + // using Vector = immer::flex_vector<std::optional<int>>; + // using Vector = immer::flex_vector<std::any>; + + Vector v{1, 2, 3, 4}; + Vector v2 = v.erase(2); + + CHECK(v2.size() == 3); +} + +#endif diff --git a/third_party/immer/test/flex_vector/issue-47.cpp b/third_party/immer/test/flex_vector/issue-47.cpp new file mode 100644 index 000000000000..7757fe4ee4e5 --- /dev/null +++ b/third_party/immer/test/flex_vector/issue-47.cpp @@ -0,0 +1,5 @@ +#include <immer/flex_vector.hpp> + +immer::flex_vector<int> v{1}; + +int main() { return 0; } diff --git a/third_party/immer/test/flex_vector/regular-B3-BL3.cpp b/third_party/immer/test/flex_vector/regular-B3-BL3.cpp new file mode 100644 index 000000000000..fb5d13d1aa8e --- /dev/null +++ b/third_party/immer/test/flex_vector/regular-B3-BL3.cpp @@ -0,0 +1,16 @@ +// +// immer: immutable data structures for C++ +// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente +// +// This software is distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt +// + +#include <immer/flex_vector.hpp> + +template <typename T> +using test_vector_t = + immer::flex_vector<T, immer::default_memory_policy, 3u, 3u>; + +#define VECTOR_T test_vector_t +#include "../vector/generic.ipp" diff --git a/third_party/immer/test/flex_vector/regular-default.cpp b/third_party/immer/test/flex_vector/regular-default.cpp new file mode 100644 index 000000000000..8f28c3d8099f --- /dev/null +++ b/third_party/immer/test/flex_vector/regular-default.cpp @@ -0,0 +1,12 @@ +// +// immer: immutable data structures for C++ +// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente +// +// This software is distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt +// + +#include <immer/flex_vector.hpp> + +#define VECTOR_T ::immer::flex_vector +#include "../vector/generic.ipp" |