Coverage Report

Created: 2025-11-24 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fuzzer.cpp
Line
Count
Source
1
/* Copyright 2024 Google LLC
2
Licensed under the Apache License, Version 2.0 (the "License");
3
you may not use this file except in compliance with the License.
4
You may obtain a copy of the License at
5
      http://www.apache.org/licenses/LICENSE-2.0
6
Unless required by applicable law or agreed to in writing, software
7
distributed under the License is distributed on an "AS IS" BASIS,
8
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9
See the License for the specific language governing permissions and
10
limitations under the License.
11
*/
12
13
#include <cstdint>
14
#include <cstddef>
15
#include <string>
16
#include <rapidjson/document.h>
17
#include <rapidjson/prettywriter.h>
18
19
#ifdef MSAN
20
extern "C" {
21
    void __msan_check_mem_is_initialized(const volatile void *x, size_t size);
22
}
23
#endif
24
25
template<unsigned parseFlags>
26
void fuzzWithFlags(const std::string &s)
27
88
{
28
    /* Parse input to rapidjson::Document */
29
88
    rapidjson::Document document;
30
88
    rapidjson::ParseResult pr = document.Parse<parseFlags>(s.c_str());
31
88
    if ( !pr ) {
32
69
        return;
33
69
    }
34
35
    /* Convert from rapidjson::Document to string */
36
19
    rapidjson::StringBuffer sb;
37
19
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
38
19
    document.Accept(writer);
39
19
    std::string str = sb.GetString();
40
#ifdef MSAN
41
    if ( str.size() ) {
42
        __msan_check_mem_is_initialized(str.data(), str.size());
43
    }
44
#endif
45
19
}
void fuzzWithFlags<0u>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
27
22
{
28
    /* Parse input to rapidjson::Document */
29
22
    rapidjson::Document document;
30
22
    rapidjson::ParseResult pr = document.Parse<parseFlags>(s.c_str());
31
22
    if ( !pr ) {
32
19
        return;
33
19
    }
34
35
    /* Convert from rapidjson::Document to string */
36
3
    rapidjson::StringBuffer sb;
37
3
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
38
3
    document.Accept(writer);
39
3
    std::string str = sb.GetString();
40
#ifdef MSAN
41
    if ( str.size() ) {
42
        __msan_check_mem_is_initialized(str.data(), str.size());
43
    }
44
#endif
45
3
}
void fuzzWithFlags<16u>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
27
22
{
28
    /* Parse input to rapidjson::Document */
29
22
    rapidjson::Document document;
30
22
    rapidjson::ParseResult pr = document.Parse<parseFlags>(s.c_str());
31
22
    if ( !pr ) {
32
19
        return;
33
19
    }
34
35
    /* Convert from rapidjson::Document to string */
36
3
    rapidjson::StringBuffer sb;
37
3
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
38
3
    document.Accept(writer);
39
3
    std::string str = sb.GetString();
40
#ifdef MSAN
41
    if ( str.size() ) {
42
        __msan_check_mem_is_initialized(str.data(), str.size());
43
    }
44
#endif
45
3
}
void fuzzWithFlags<64u>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
27
22
{
28
    /* Parse input to rapidjson::Document */
29
22
    rapidjson::Document document;
30
22
    rapidjson::ParseResult pr = document.Parse<parseFlags>(s.c_str());
31
22
    if ( !pr ) {
32
12
        return;
33
12
    }
34
35
    /* Convert from rapidjson::Document to string */
36
10
    rapidjson::StringBuffer sb;
37
10
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
38
10
    document.Accept(writer);
39
10
    std::string str = sb.GetString();
40
#ifdef MSAN
41
    if ( str.size() ) {
42
        __msan_check_mem_is_initialized(str.data(), str.size());
43
    }
44
#endif
45
10
}
void fuzzWithFlags<32u>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
27
22
{
28
    /* Parse input to rapidjson::Document */
29
22
    rapidjson::Document document;
30
22
    rapidjson::ParseResult pr = document.Parse<parseFlags>(s.c_str());
31
22
    if ( !pr ) {
32
19
        return;
33
19
    }
34
35
    /* Convert from rapidjson::Document to string */
36
3
    rapidjson::StringBuffer sb;
37
3
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
38
3
    document.Accept(writer);
39
3
    std::string str = sb.GetString();
40
#ifdef MSAN
41
    if ( str.size() ) {
42
        __msan_check_mem_is_initialized(str.data(), str.size());
43
    }
44
#endif
45
3
}
46
47
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
48
22
{
49
22
    const std::string s(data, data + size);
50
51
22
    fuzzWithFlags<rapidjson::kParseDefaultFlags>(s);
52
22
    fuzzWithFlags<rapidjson::kParseFullPrecisionFlag>(s);
53
22
    fuzzWithFlags<rapidjson::kParseNumbersAsStringsFlag>(s);
54
22
    fuzzWithFlags<rapidjson::kParseCommentsFlag>(s);
55
56
22
    return 0;
57
22
}