File: fuzz_utf8.cpp

package info (click to toggle)
simdjson 4.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,936 kB
  • sloc: cpp: 171,612; ansic: 19,122; sh: 1,126; python: 842; makefile: 47; ruby: 25; javascript: 13
file content (80 lines) | stat: -rw-r--r-- 2,619 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * For fuzzing all of the implementations (haswell/fallback/westmere),
 * finding any difference between the output of each which would
 * indicate inconsistency. Also, it gets the non-default backend
 * some fuzzing love.
 *
 * Copyright Paul Dreik 20200912 for the simdjson project.
 */

#include "simdjson.h"
#include <cstddef>
#include <cstdlib>
#include <iostream>
#include "supported_implementations.h"

extern "C" int VerboseTestOneInput(const uint8_t *Data, size_t Size) {
    static const auto supported_implementations=get_runtime_supported_implementations();
    for(size_t i = 0; i <= Size; i++) {
        std::cout<<"size: "<<std::dec<<std::setw(8)<<i<<std::endl;
        std::cout<<"Input: \"";
        for(size_t j = 0; j < i; j++) {
            std::cout<<"\\x"<<std::hex<<std::setw(2)<<std::setfill('0')<<uint32_t(Data[j]);
        }
        std::cout<<"\""<<std::endl;
        for(const auto& e: supported_implementations) {
            if(!e->supported_by_runtime_system()) { continue; }
            const bool current=e->validate_utf8((const char*)Data,i);
            std::cout<<e->name()<<" returns "<<current<<std::endl;
        }
        std::cout<<std::endl;
    }
    return 0;
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {

    // since this check is expensive, only do it once
    static const auto supported_implementations=get_runtime_supported_implementations();


    auto utf8verify=[Data,Size](const simdjson::implementation* impl) -> bool {
        return impl->validate_utf8((const char*)Data,Size);
    };


    auto first = supported_implementations.begin();
    auto last = supported_implementations.end();


    const bool reference=utf8verify(*first);

    bool failed=false;
    for(auto it=first+1; it != last; ++it) {
        const bool current=utf8verify(*it);
        if(current!=reference) {
            failed=true;
        }
    }

    if(failed) {
        std::cerr<<std::boolalpha<<"Mismatch between implementations of validate_utf8() found:\n";
        for(const auto& e: supported_implementations) {
            if(!e->supported_by_runtime_system()) { continue; }
            const bool current=utf8verify(e);
            std::cerr<<e->name()<<" returns "<<current<<std::endl;
        }
        std::cerr << "Offending input: \"";
        for(size_t i = 0; i < Size; i++) {
            std::cerr << "\\x" << std::hex << std::setw(2) << std::setfill('0')  << uint32_t(Data[i]);
        }
        std::cerr << "\"" <<std::endl;

        VerboseTestOneInput(Data, Size);

        std::abort();
    }

    //all is well
    return 0;
}