Coverage Report

Created: 2025-08-24 06:21

/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
8.64k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
39
  // Setup JSONLayout
40
8.64k
  JSONLayout layout;
41
42
8.64k
  FuzzedDataProvider fdp(data, size);
43
44
  // Optional locationinfo
45
8.64k
  if (fdp.ConsumeBool()) {
46
5.40k
    layout.setOption(LOG4CXX_STR("LOCATIONINFO"), LOG4CXX_STR("true"));
47
5.40k
  }
48
  // Optional threadinfo
49
8.64k
  if (fdp.ConsumeBool()) {
50
4.48k
    layout.setOption(LOG4CXX_STR("THREADINFO"), LOG4CXX_STR("true"));
51
4.48k
  }
52
  // Optional prettyprint
53
8.64k
  if (fdp.ConsumeBool()) {
54
4.21k
    layout.setOption(LOG4CXX_STR("PRETTYPRINT"), LOG4CXX_STR("true"));
55
4.21k
  }
56
57
  // Create random strings we need later
58
8.64k
  std::string key1Str = fdp.ConsumeRandomLengthString(MaxKeyLength);
59
8.64k
  std::string val1Str = fdp.ConsumeRandomLengthString(MaxValueLength);
60
8.64k
  std::string key2Str = fdp.ConsumeRandomLengthString(MaxKeyLength);
61
8.64k
  std::string val2Str = fdp.ConsumeRandomLengthString(MaxValueLength);
62
8.64k
  std::string key3Str = fdp.ConsumeRandomLengthString(MaxKeyLength);
63
8.64k
  std::string val3Str = fdp.ConsumeRandomLengthString(MaxValueLength);
64
8.64k
  std::string key4Str = fdp.ConsumeRandomLengthString(MaxKeyLength);
65
8.64k
  std::string val4Str = fdp.ConsumeRandomLengthString(MaxValueLength);
66
8.64k
  std::string ndcMessageStr = fdp.ConsumeRandomLengthString(MaxMessageLength);
67
8.64k
  std::string loggerStr = fdp.ConsumeRandomLengthString(MaxKeyLength);
68
8.64k
  std::string contentStr = fdp.ConsumeRandomLengthString(MaxMessageLength);
69
70
8.64k
  LogString key1, val1, key2, val2, key3, val3, key4, val4, ndcMessage, logger, content;
71
8.64k
  Transcoder::decode(key1Str, key1);
72
8.64k
  Transcoder::decode(val1Str, val1);
73
8.64k
  Transcoder::decode(key2Str, key2);
74
8.64k
  Transcoder::decode(val2Str, val2);
75
8.64k
  Transcoder::decode(key3Str, key3);
76
8.64k
  Transcoder::decode(val3Str, val3);
77
8.64k
  Transcoder::decode(key4Str, key4);
78
8.64k
  Transcoder::decode(val4Str, val4);
79
8.64k
  Transcoder::decode(ndcMessageStr, ndcMessage);
80
8.64k
  Transcoder::decode(loggerStr, logger);
81
8.64k
  Transcoder::decode(contentStr, content);
82
83
8.64k
  log4cxx::LevelPtr level = log4cxx::Level::getInfo();
84
8.64k
  log4cxx::NDC::push(ndcMessage);
85
8.64k
  log4cxx::spi::LoggingEventPtr event = log4cxx::spi::LoggingEventPtr(
86
8.64k
    new log4cxx::spi::LoggingEvent(
87
8.64k
      logger, level, content, LOG4CXX_LOCATION));
88
89
  // Set properties
90
8.64k
  event->setProperty(key1, val1);
91
8.64k
  event->setProperty(key2, val2);
92
93
  // Set MDC
94
8.64k
  log4cxx::MDC::put(key3, val3);
95
8.64k
  log4cxx::MDC::put(key4, val4);
96
97
  // Call the target API
98
8.64k
  log4cxx::helpers::Pool p;
99
8.64k
  log4cxx::LogString result;
100
8.64k
  layout.format(result, event, p);
101
102
  // Clean up
103
8.64k
  log4cxx::NDC::clear();
104
8.64k
  log4cxx::MDC::clear();
105
8.64k
    return 0;
106
8.64k
}