contrib/fuzz/dirs.cc
author Georges Racinet <georges.racinet@octobus.net>
Tue, 04 Apr 2023 11:46:26 +0200
changeset 50421 53f039ed6f20
parent 43859 8766728dbce6
permissions -rw-r--r--
rust-readme: mentioning clippy especially since there is a CI check for it.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43150
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
     1
#include <Python.h>
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
     2
#include <assert.h>
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
     3
#include <stdlib.h>
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
     4
#include <unistd.h>
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
     5
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
     6
#include "pyutil.h"
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
     7
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
     8
#include <string>
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
     9
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    10
extern "C" {
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    11
43859
8766728dbce6 fuzz: add support for fuzzing under either Python 2 or 3
Augie Fackler <augie@google.com>
parents: 43150
diff changeset
    12
static PYCODETYPE *code;
43150
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    13
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    14
extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    15
{
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    16
	contrib::initpy(*argv[0]);
43859
8766728dbce6 fuzz: add support for fuzzing under either Python 2 or 3
Augie Fackler <augie@google.com>
parents: 43150
diff changeset
    17
	code = (PYCODETYPE *)Py_CompileString(R"py(
43150
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    18
try:
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    19
  files = mdata.split('\n')
43859
8766728dbce6 fuzz: add support for fuzzing under either Python 2 or 3
Augie Fackler <augie@google.com>
parents: 43150
diff changeset
    20
  d = parsers.dirs(files)
43150
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    21
  list(d)
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    22
  'a' in d
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    23
  if files:
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    24
    files[0] in d
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    25
except Exception as e:
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    26
  pass
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    27
  # uncomment this print if you're editing this Python code
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    28
  # to debug failures.
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    29
  # print e
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    30
)py",
43859
8766728dbce6 fuzz: add support for fuzzing under either Python 2 or 3
Augie Fackler <augie@google.com>
parents: 43150
diff changeset
    31
	                                      "fuzzer", Py_file_input);
43150
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    32
	return 0;
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    33
}
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    34
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    35
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    36
{
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    37
	// Don't allow fuzzer inputs larger than 100k, since we'll just bog
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    38
	// down and not accomplish much.
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    39
	if (Size > 100000) {
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    40
		return 0;
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    41
	}
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    42
	PyObject *mtext =
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    43
	    PyBytes_FromStringAndSize((const char *)Data, (Py_ssize_t)Size);
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    44
	PyObject *locals = PyDict_New();
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    45
	PyDict_SetItemString(locals, "mdata", mtext);
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    46
	PyObject *res = PyEval_EvalCode(code, contrib::pyglobals(), locals);
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    47
	if (!res) {
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    48
		PyErr_Print();
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    49
	}
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    50
	Py_XDECREF(res);
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    51
	Py_DECREF(locals);
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    52
	Py_DECREF(mtext);
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    53
	return 0; // Non-zero return values are reserved for future use.
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    54
}
7ff40418c6bf fuzz: new fuzzer for dirs.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    55
}