Coverage Report

Created: 2025-07-01 06:08

/src/logging-log4cxx/src/fuzzers/cpp/JSONLayoutFuzzer.cpp
Line
Count
Source
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
#include "stdint.h"
19
#include <log4cxx/logstring.h>
20
#include <log4cxx/ndc.h>
21
#include <log4cxx/helpers/stringhelper.h>
22
#include <fuzzer/FuzzedDataProvider.h>
23
#include <log4cxx/mdc.h>
24
#include <log4cxx/jsonlayout.h>
25
#include <log4cxx/helpers/transcoder.h>
26
27
namespace
28
{
29
  const int MaxKeyLength = 50;
30
  const int MaxValueLength = 500;
31
  const int MaxMessageLength = 2000;
32
}
33
34
using namespace log4cxx;
35
using namespace log4cxx::helpers;
36
using namespace log4cxx::spi;
37
38
6.09k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
39
  // Setup JSONLayout
40
6.09k
  JSONLayout layout;
41
42
6.09k
  FuzzedDataProvider fdp(data, size);
43
44
  // Optional locationinfo
45
6.09k
  if (fdp.ConsumeBool()) {
46
3.67k
    layout.setOption(LOG4CXX_STR("LOCATIONINFO"), LOG4CXX_STR("true"));
47
3.67k
  }
48
  // Optional threadinfo
49
6.09k
  if (fdp.ConsumeBool()) {
50
3.05k
    layout.setOption(LOG4CXX_STR("THREADINFO"), LOG4CXX_STR("true"));
51
3.05k
  }
52
  // Optional prettyprint
53
6.09k
  if (fdp.ConsumeBool()) {
54
3.02k
    layout.setOption(LOG4CXX_STR("PRETTYPRINT"), LOG4CXX_STR("true"));
55
3.02k
  }
56
57
  // Create random strings we need later
58
6.09k
  std::string key1Str = fdp.ConsumeRandomLengthString(MaxKeyLength);
59
6.09k
  std::string val1Str = fdp.ConsumeRandomLengthString(MaxValueLength);
60
6.09k
  std::string key2Str = fdp.ConsumeRandomLengthString(MaxKeyLength);
61
6.09k
  std::string val2Str = fdp.ConsumeRandomLengthString(MaxValueLength);
62
6.09k
  std::string key3Str = fdp.ConsumeRandomLengthString(MaxKeyLength);
63
6.09k
  std::string val3Str = fdp.ConsumeRandomLengthString(MaxValueLength);
64
6.09k
  std::string key4Str = fdp.ConsumeRandomLengthString(MaxKeyLength);
65
6.09k
  std::string val4Str = fdp.ConsumeRandomLengthString(MaxValueLength);
66
6.09k
  std::string ndcMessageStr = fdp.ConsumeRandomLengthString(MaxMessageLength);
67
6.09k
  std::string loggerStr = fdp.ConsumeRandomLengthString(MaxKeyLength);
68
6.09k
  std::string contentStr = fdp.ConsumeRandomLengthString(MaxMessageLength);
69
70
6.09k
  LogString key1, val1, key2, val2, key3, val3, key4, val4, ndcMessage, logger, content;
71
6.09k
  Transcoder::decode(key1Str, key1);
72
6.09k
  Transcoder::decode(val1Str, val1);
73
6.09k
  Transcoder::decode(key2Str, key2);
74
6.09k
  Transcoder::decode(val2Str, val2);
75
6.09k
  Transcoder::decode(key3Str, key3);
76
6.09k
  Transcoder::decode(val3Str, val3);
77
6.09k
  Transcoder::decode(key4Str, key4);
78
6.09k
  Transcoder::decode(val4Str, val4);
79
6.09k
  Transcoder::decode(ndcMessageStr, ndcMessage);
80
6.09k
  Transcoder::decode(loggerStr, logger);
81
6.09k
  Transcoder::decode(contentStr, content);
82
83
6.09k
  log4cxx::LevelPtr level = log4cxx::Level::getInfo();
84
6.09k
  log4cxx::NDC::push(ndcMessage);
85
6.09k
  log4cxx::spi::LoggingEventPtr event = log4cxx::spi::LoggingEventPtr(
86
6.09k
    new log4cxx::spi::LoggingEvent(
87
6.09k
      logger, level, content, LOG4CXX_LOCATION));
88
89
  // Set properties
90
6.09k
  event->setProperty(key1, val1);
91
6.09k
  event->setProperty(key2, val2);
92
93
  // Set MDC
94
6.09k
  log4cxx::MDC::put(key3, val3);
95
6.09k
  log4cxx::MDC::put(key4, val4);
96
97
  // Call the target API
98
6.09k
  log4cxx::helpers::Pool p;
99
6.09k
  log4cxx::LogString result;
100
6.09k
  layout.format(result, event, p);
101
102
  // Clean up
103
6.09k
  log4cxx::NDC::clear();
104
6.09k
  log4cxx::MDC::clear();
105
6.09k
    return 0;
106
6.09k
}