summaryrefslogtreecommitdiffstats
path: root/mfbt/Fuzzing.h
blob: dae79480bd3dc97b32f69029d0184f15f95a027b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* Additional definitions and implementation for fuzzing code */

#ifndef mozilla_Fuzzing_h
#define mozilla_Fuzzing_h

#ifdef FUZZING_SNAPSHOT
#  include "mozilla/fuzzing/NyxWrapper.h"

#  ifdef __cplusplus
#    include "mozilla/fuzzing/Nyx.h"
#    include "mozilla/ScopeExit.h"

#    define MOZ_FUZZING_NYX_RELEASE(id)                       \
      if (mozilla::fuzzing::Nyx::instance().is_enabled(id)) { \
        mozilla::fuzzing::Nyx::instance().release();          \
      }

#    define MOZ_FUZZING_NYX_GUARD(id)                           \
      auto nyxGuard = mozilla::MakeScopeExit([&] {              \
        if (mozilla::fuzzing::Nyx::instance().is_enabled(id)) { \
          mozilla::fuzzing::Nyx::instance().release();          \
        }                                                       \
      });
#  endif

#  define MOZ_FUZZING_HANDLE_CRASH_EVENT2(aType, aReason)     \
    do {                                                      \
      if (nyx_handle_event) {                                 \
        nyx_handle_event(aType, __FILE__, __LINE__, aReason); \
      }                                                       \
    } while (false)

#  define MOZ_FUZZING_HANDLE_CRASH_EVENT4(aType, aFilename, aLine, aReason) \
    do {                                                                    \
      if (nyx_handle_event) {                                               \
        nyx_handle_event(aType, aFilename, aLine, aReason);                 \
      }                                                                     \
    } while (false)

#  define MOZ_FUZZING_NYX_PRINT(aMsg) \
    do {                              \
      if (nyx_puts) {                 \
        nyx_puts(aMsg);               \
      } else {                        \
        fprintf(stderr, aMsg);        \
      }                               \
    } while (false)

#  define MOZ_FUZZING_NYX_PRINTF(aFormat, ...)                         \
    do {                                                               \
      if (nyx_puts) {                                                  \
        char msgbuf[2048];                                             \
        snprintf(msgbuf, sizeof(msgbuf) - 1, "" aFormat, __VA_ARGS__); \
        nyx_puts(msgbuf);                                              \
      } else {                                                         \
        fprintf(stderr, aFormat, __VA_ARGS__);                         \
      }                                                                \
    } while (false)

#  ifdef FUZZ_DEBUG
#    define MOZ_FUZZING_NYX_DEBUG(x) MOZ_FUZZING_NYX_PRINT(x)
#  else
#    define MOZ_FUZZING_NYX_DEBUG(x)
#  endif
#else
#  define MOZ_FUZZING_NYX_RELEASE(id)
#  define MOZ_FUZZING_NYX_GUARD(id)
#  define MOZ_FUZZING_NYX_PRINT(aMsg)
#  define MOZ_FUZZING_NYX_PRINTF(aFormat, ...)
#  define MOZ_FUZZING_NYX_DEBUG(aMsg)
#  define MOZ_FUZZING_HANDLE_CRASH_EVENT2(aType, aReason) \
    do {                                                  \
    } while (false)
#  define MOZ_FUZZING_HANDLE_CRASH_EVENT4(aType, aFilename, aLine, aReason) \
    do {                                                                    \
    } while (false)
#endif

#endif /* mozilla_Fuzzing_h */