Coverage Report

Created: 2025-07-11 06:27

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