File: runner.cpp

package info (click to toggle)
wxpython4.0 4.2.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 232,540 kB
  • sloc: cpp: 958,937; python: 233,059; ansic: 150,441; makefile: 51,662; sh: 8,687; perl: 1,563; javascript: 584; php: 326; xml: 200
file content (64 lines) | stat: -rw-r--r-- 2,009 bytes parent folder | download | duplicates (4)
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
///////////////////////////////////////////////////////////////////////////////
// Name:        tests/fuzz/runner.cpp
// Purpose:     Main function for running fuzzers with a single input file
// Author:      Vadim Zeitlin
// Created:     2017-10-28
// Copyright:   (c) 2017 Vadim Zeitlin <vadim@wxwidgets.org>
///////////////////////////////////////////////////////////////////////////////

// Normally fuzzers are run by libFuzzer, which executes the entry function
// LLVMFuzzerTestOneInput() with many different inputs, but it can be useful to
// run them with just a single input to check a particular problem found during
// fuzzing. To do this, link the fuzzer code with this file and run it with the
// file name containing the test data. E.g. an example use:
//
//  $ g++ -g -fsanitize=undefined tests/fuzz/{zip,runner}.cpp `wx-config --cxxflags --libs base`
//  $ ./a.out testcase-found-by-libfuzzer

#include "wx/buffer.h"
#include "wx/crt.h"
#include "wx/ffile.h"
#include "wx/init.h"

// The fuzzer entry function.
extern "C" int LLVMFuzzerTestOneInput(const wxUint8 *data, size_t size);

int main(int argc, char** argv)
{
    wxInitializer init(argc, argv);
    if ( !init.IsOk() )
    {
        wxPrintf("Initializing wxWidgets failed.\n");
        return 1;
    }

    if ( argc != 2 )
    {
        wxPrintf("Usage: %s <input-file>\n", argv[0]);
        return 2;
    }

    wxFFile file(argv[1], "rb");
    if ( !file.IsOpened() )
    {
        wxPrintf("Failed to open the input file \"%s\".\n", argv[1]);
        return 3;
    }

    const wxFileOffset ofs = file.Length();
    if ( ofs < 0 )
    {
        wxPrintf("Failed to get the input file \"%s\" size.\n", argv[1]);
        return 3;
    }

    const size_t len = ofs;
    wxMemoryBuffer buf(len);
    if ( file.Read(buf.GetData(), len) != len )
    {
        wxPrintf("Failed to read from the input file \"%s\".\n", argv[1]);
        return 3;
    }

    return LLVMFuzzerTestOneInput(static_cast<wxUint8*>(buf.GetData()), len);
}