Coverage Report

Created: 2026-09-14 07:03

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
84
{
28
    /* Parse input to rapidjson::Document */
29
84
    rapidjson::Document document;
30
84
    rapidjson::ParseResult pr = document.Parse<parseFlags>(s.c_str());
31
84
    if ( !pr ) {
32
57
        return;
33
57
    }
34
35
    /* Convert from rapidjson::Document to string */
36
27
    rapidjson::StringBuffer sb;
37
27
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
38
27
    document.Accept(writer);
39
27
    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
27
}
void fuzzWithFlags<0u>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
27
21
{
28
    /* Parse input to rapidjson::Document */
29
21
    rapidjson::Document document;
30
21
    rapidjson::ParseResult pr = document.Parse<parseFlags>(s.c_str());
31
21
    if ( !pr ) {
32
15
        return;
33
15
    }
34
35
    /* Convert from rapidjson::Document to string */
36
6
    rapidjson::StringBuffer sb;
37
6
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
38
6
    document.Accept(writer);
39
6
    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
6
}
void fuzzWithFlags<16u>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
27
21
{
28
    /* Parse input to rapidjson::Document */
29
21
    rapidjson::Document document;
30
21
    rapidjson::ParseResult pr = document.Parse<parseFlags>(s.c_str());
31
21
    if ( !pr ) {
32
15
        return;
33
15
    }
34
35
    /* Convert from rapidjson::Document to string */
36
6
    rapidjson::StringBuffer sb;
37
6
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
38
6
    document.Accept(writer);
39
6
    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
6
}
void fuzzWithFlags<64u>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
27
21
{
28
    /* Parse input to rapidjson::Document */
29
21
    rapidjson::Document document;
30
21
    rapidjson::ParseResult pr = document.Parse<parseFlags>(s.c_str());
31
21
    if ( !pr ) {
32
12
        return;
33
12
    }
34
35
    /* Convert from rapidjson::Document to string */
36
9
    rapidjson::StringBuffer sb;
37
9
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
38
9
    document.Accept(writer);
39
9
    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
9
}
void fuzzWithFlags<32u>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
27
21
{
28
    /* Parse input to rapidjson::Document */
29
21
    rapidjson::Document document;
30
21
    rapidjson::ParseResult pr = document.Parse<parseFlags>(s.c_str());
31
21
    if ( !pr ) {
32
15
        return;
33
15
    }
34
35
    /* Convert from rapidjson::Document to string */
36
6
    rapidjson::StringBuffer sb;
37
6
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
38
6
    document.Accept(writer);
39
6
    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
6
}
46
47
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
48
21
{
49
21
    const std::string s(data, data + size);
50
51
21
    fuzzWithFlags<rapidjson::kParseDefaultFlags>(s);
52
21
    fuzzWithFlags<rapidjson::kParseFullPrecisionFlag>(s);
53
21
    fuzzWithFlags<rapidjson::kParseNumbersAsStringsFlag>(s);
54
21
    fuzzWithFlags<rapidjson::kParseCommentsFlag>(s);
55
56
21
    return 0;
57
21
}