Coverage Report

Created: 2025-07-18 06:43

/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
61
        return;
33
61
    }
34
35
    /* Convert from rapidjson::Document to string */
36
23
    rapidjson::StringBuffer sb;
37
23
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
38
23
    document.Accept(writer);
39
23
    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
23
}
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
16
        return;
33
16
    }
34
35
    /* Convert from rapidjson::Document to string */
36
5
    rapidjson::StringBuffer sb;
37
5
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
38
5
    document.Accept(writer);
39
5
    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
5
}
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
16
        return;
33
16
    }
34
35
    /* Convert from rapidjson::Document to string */
36
5
    rapidjson::StringBuffer sb;
37
5
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
38
5
    document.Accept(writer);
39
5
    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
5
}
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
13
        return;
33
13
    }
34
35
    /* Convert from rapidjson::Document to string */
36
8
    rapidjson::StringBuffer sb;
37
8
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
38
8
    document.Accept(writer);
39
8
    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
8
}
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
16
        return;
33
16
    }
34
35
    /* Convert from rapidjson::Document to string */
36
5
    rapidjson::StringBuffer sb;
37
5
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
38
5
    document.Accept(writer);
39
5
    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
5
}
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
}