1 | // |
2 | // immer: immutable data structures for C++ |
3 | // Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente |
4 | // |
5 | // This software is distributed under the Boost Software License, Version 1.0. |
6 | // See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt |
7 | // |
8 | |
9 | #include "fuzzer_input.hpp" |
10 | #include <immer/flex_vector.hpp> |
11 | #include <immer/flex_vector_transient.hpp> |
12 | #include <immer/heap/gc_heap.hpp> |
13 | #include <immer/refcount/no_refcount_policy.hpp> |
14 | #include <iostream> |
15 | #include <array> |
16 | |
17 | using gc_memory = immer::memory_policy< |
18 | immer::heap_policy<immer::gc_heap>, |
19 | immer::no_refcount_policy, |
20 | immer::gc_transience_policy, |
21 | false>; |
22 | |
23 | extern "C" |
24 | int LLVMFuzzerTestOneInput(const std::uint8_t* data, std::size_t size) |
25 | { |
26 | constexpr auto var_count = 4; |
27 | constexpr auto bits = 2; |
28 | |
29 | using vector_t = immer::flex_vector<int, gc_memory, bits, bits>; |
30 | using transient_t = typename vector_t::transient_type; |
31 | using size_t = std::uint8_t; |
32 | |
33 | auto vs = std::array<vector_t, var_count>{}; |
34 | auto ts = std::array<transient_t, var_count>{}; |
35 | |
36 | auto is_valid_var = [&] (auto idx) { |
37 | return idx >= 0 && idx < var_count; |
38 | }; |
39 | auto is_valid_var_neq = [](auto other) { |
40 | return [=] (auto idx) { |
41 | return idx >= 0 && idx < var_count && idx != other; |
42 | }; |
43 | }; |
44 | auto is_valid_index = [] (auto& v) { |
45 | return [&] (auto idx) { return idx >= 0 && idx < v.size(); }; |
46 | }; |
47 | auto is_valid_size = [] (auto& v) { |
48 | return [&] (auto idx) { return idx >= 0 && idx <= v.size(); }; |
49 | }; |
50 | auto can_concat = [] (auto&& v1, auto&& v2) { |
51 | using size_type = decltype(v1.size()); |
52 | auto max = std::numeric_limits<size_type>::max() >> (bits * 4); |
53 | return v1.size() < max && v2.size() < max; |
54 | }; |
55 | |
56 | return fuzzer_input{data, size}.run([&] (auto& in) |
57 | { |
58 | enum ops { |
59 | op_transient, |
60 | op_persistent, |
61 | op_push_back, |
62 | op_update, |
63 | op_take, |
64 | op_drop, |
65 | op_concat, |
66 | op_push_back_mut, |
67 | op_update_mut, |
68 | op_take_mut, |
69 | op_drop_mut, |
70 | op_prepend_mut, |
71 | op_prepend_mut_move, |
72 | op_append_mut, |
73 | op_append_mut_move, |
74 | }; |
75 | auto dst = read<char>(in, is_valid_var); |
76 | switch (read<char>(in)) |
77 | { |
78 | case op_transient: { |
79 | auto src = read<char>(in, is_valid_var); |
80 | ts[dst] = vs[src].transient(); |
81 | break; |
82 | } |
83 | case op_persistent: { |
84 | auto src = read<char>(in, is_valid_var); |
85 | vs[dst] = ts[src].persistent(); |
86 | break; |
87 | } |
88 | case op_push_back: { |
89 | auto src = read<char>(in, is_valid_var); |
90 | vs[dst] = vs[src].push_back(42); |
91 | break; |
92 | } |
93 | case op_update: { |
94 | auto src = read<char>(in, is_valid_var); |
95 | auto idx = read<size_t>(in, is_valid_index(vs[src])); |
96 | vs[dst] = vs[src].update(idx, [] (auto x) { return x + 1; }); |
97 | break; |
98 | } |
99 | case op_take: { |
100 | auto src = read<char>(in, is_valid_var); |
101 | auto idx = read<size_t>(in, is_valid_size(vs[src])); |
102 | vs[dst] = vs[src].take(idx); |
103 | break; |
104 | } |
105 | case op_drop: { |
106 | auto src = read<char>(in, is_valid_var); |
107 | auto idx = read<size_t>(in, is_valid_size(vs[src])); |
108 | vs[dst] = vs[src].drop(idx); |
109 | break; |
110 | } |
111 | case op_concat: { |
112 | auto src = read<char>(in, is_valid_var); |
113 | auto src2 = read<char>(in, is_valid_var); |
114 | if (can_concat(vs[src], vs[src2])) |
115 | vs[dst] = vs[src] + vs[src2]; |
116 | break; |
117 | } |
118 | case op_push_back_mut: { |
119 | ts[dst].push_back(13); |
120 | break; |
121 | } |
122 | case op_update_mut: { |
123 | auto idx = read<size_t>(in, is_valid_index(ts[dst])); |
124 | ts[dst].update(idx, [] (auto x) { return x + 1; }); |
125 | break; |
126 | } |
127 | case op_take_mut: { |
128 | auto idx = read<size_t>(in, is_valid_size(ts[dst])); |
129 | ts[dst].take(idx); |
130 | break; |
131 | } |
132 | case op_prepend_mut: { |
133 | auto src = read<char>(in, is_valid_var_neq(dst)); |
134 | if (can_concat(ts[dst], ts[src])) |
135 | ts[dst].prepend(ts[src]); |
136 | break; |
137 | } |
138 | case op_prepend_mut_move: { |
139 | auto src = read<char>(in, is_valid_var_neq(dst)); |
140 | if (can_concat(ts[dst], ts[src])) { |
141 | ts[dst].prepend(std::move(ts[src])); |
142 | ts[src] = {}; |
143 | } |
144 | break; |
145 | } |
146 | case op_append_mut: { |
147 | auto src = read<char>(in, is_valid_var_neq(dst)); |
148 | if (can_concat(ts[dst], ts[src])) |
149 | ts[dst].append(ts[src]); |
150 | break; |
151 | } |
152 | case op_append_mut_move: { |
153 | auto src = read<char>(in, is_valid_var_neq(dst)); |
154 | if (can_concat(ts[dst], ts[src])) { |
155 | ts[dst].append(std::move(ts[src])); |
156 | ts[src] = {}; |
157 | } |
158 | break; |
159 | } |
160 | default: |
161 | break; |
162 | }; |
163 | return true; |
164 | }); |
165 | } |
166 | |