/src/simdjson/fuzz/fuzz_minifyimpl.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Minifies using the minify() function directly, without parsing. |
3 | | * |
4 | | * For fuzzing all of the implementations (haswell/fallback/westmere), |
5 | | * finding any difference between the output of each which would |
6 | | * indicate inconsistency. Also, it gets the non-default backend |
7 | | * some fuzzing love. |
8 | | * |
9 | | * Copyright Paul Dreik 20200912 for the simdjson project. |
10 | | */ |
11 | | |
12 | | #include "simdjson.h" |
13 | | #include <cstddef> |
14 | | #include <cstdlib> |
15 | | #include <iostream> |
16 | | #include <vector> |
17 | | #include "supported_implementations.h" |
18 | | |
19 | 698 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { |
20 | | |
21 | | // since this check is expensive, only do it once |
22 | 698 | static const auto implementations=get_runtime_supported_implementations(); |
23 | | |
24 | 698 | using Buffer=std::vector<uint8_t>; |
25 | 2.09k | auto minify=[Data,Size](const simdjson::implementation* impl) -> Buffer { |
26 | 2.09k | Buffer ret(Size); |
27 | 2.09k | std::size_t retsize=0; |
28 | 2.09k | auto err=impl->minify(Data,Size,ret.data(),retsize); |
29 | 2.09k | if(err) { |
30 | 378 | std::string tmp = error_message(err); |
31 | 378 | ret.assign(tmp.begin(),tmp.end()); |
32 | 1.71k | } else { |
33 | 1.71k | assert(retsize<=Size && "size should not grow by minimize()!"); |
34 | 1.71k | ret.resize(retsize); |
35 | 1.71k | } |
36 | 2.09k | return ret; |
37 | 2.09k | }; |
38 | | |
39 | 698 | auto const first = implementations.begin(); |
40 | 698 | auto const last = implementations.end(); |
41 | | |
42 | 698 | const auto reference=minify(*first); |
43 | | |
44 | 698 | bool failed=false; |
45 | 2.09k | for(auto it=first+1;it != last; ++it) { |
46 | 1.39k | const auto current=minify(*it); |
47 | 1.39k | if(current!=reference) { |
48 | 0 | failed=true; |
49 | 0 | } |
50 | 1.39k | } |
51 | | |
52 | 698 | if(failed) { |
53 | 0 | std::cerr<<std::boolalpha<<"Mismatch between implementations of minify() found:\n"; |
54 | 0 | for(const auto& e:implementations) { |
55 | 0 | const auto current=minify(e); |
56 | 0 | std::string tmp(current.begin(),current.end()); |
57 | 0 | std::cerr<<e->name()<<" returns "<<tmp<<std::endl; |
58 | 0 | } |
59 | 0 | std::abort(); |
60 | 0 | } |
61 | | |
62 | | //all is well |
63 | 698 | return 0; |
64 | 698 | } |