Coverage Report

Created: 2025-11-16 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/simdjson/fuzz/fuzz_minifyimpl.cpp
Line
Count
Source
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
662
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
20
21
  // since this check is expensive, only do it once
22
662
  static const auto implementations=get_runtime_supported_implementations();
23
24
662
    using Buffer=std::vector<uint8_t>;
25
1.98k
    auto minify=[Data,Size](const simdjson::implementation* impl) -> Buffer {
26
1.98k
        Buffer ret(Size);
27
1.98k
        std::size_t retsize=0;
28
1.98k
        auto err=impl->minify(Data,Size,ret.data(),retsize);
29
1.98k
        if(err) {
30
354
            std::string tmp = error_message(err);
31
354
            ret.assign(tmp.begin(),tmp.end());
32
1.63k
        } else {
33
1.63k
            assert(retsize<=Size && "size should not grow by minimize()!");
34
1.63k
            ret.resize(retsize);
35
1.63k
        }
36
1.98k
        return ret;
37
1.98k
    };
38
39
662
    auto const first = implementations.begin();
40
662
    auto const last = implementations.end();
41
42
662
    const auto reference=minify(*first);
43
44
662
    bool failed=false;
45
1.98k
    for(auto it=first+1;it != last; ++it) {
46
1.32k
        const auto current=minify(*it);
47
1.32k
        if(current!=reference) {
48
0
            failed=true;
49
0
        }
50
1.32k
    }
51
52
662
    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
662
    return 0;
64
662
}