contrib/fuzz/jsonescapeu8fast.cc
author Matt Harbison <matt_harbison@yahoo.com>
Wed, 04 Jan 2023 13:47:10 -0500
changeset 49958 dd804d83822c
parent 43870 8766728dbce6
permissions -rw-r--r--
setup: drop legacy osx compiler tuning to enable universal builds This was triggering deprecation warnings about migrating to `packaging.version` from `distutils` Version classes with `make local`. But rather than migrate that code, let's just get rid of some ~10-12 year old workarounds. As a bonus, the cext libraries that are built are now universal binaries containing x86_64 and arm64 images (at least when built on macOS 11.4 with Xcode 12.5 and the universal version of Python 3.9.13). Several things to note here: - Apple dropped support for 10.15 in Nov 2022, and OS X Lion that is referenced is 10.7 (unsupported since late 2014) - `xcode4` was basically always True because of the `>=` check (10.8 used Xcode 5, and I have Xcode 10.2 on 10.14) - `xcode51` was always False for modern-ish Xcode, because of the exact version string matching - Python 3.8 only supports OS X 10.9+; the Python 3.9.1+ universal installer is macOS 11+ only, and Python 3.10 drops the x86_64 installer to deliver only the universal installer. All of this is to say, the only thing lost by dropping this code on modern Xcode is that `os.environ['ARCHFLAGS'] = ''` is no longer set. But we probably shouldn't be setting that anymore, as shown by the universal libraries now being generated. I was able to `make local` and `python3 run-tests.py --local` with python 3.9.9, Xcode 10.2, and macOS 10.14.6, and didn't incur any more than the usual few test errors, so this should still work on some older versions of macOS.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43153
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
     1
#include <Python.h>
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
     2
#include <assert.h>
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
     3
#include <stdlib.h>
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
     4
#include <unistd.h>
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
     5
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
     6
#include "pyutil.h"
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
     7
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
     8
#include <iostream>
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
     9
#include <string>
43831
5a9e2ae9899b fuzz: use a more standard approach to allow local builds of fuzzers
Augie Fackler <augie@google.com>
parents: 43153
diff changeset
    10
#include "FuzzedDataProvider.h"
43153
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    11
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    12
extern "C" {
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    13
43870
8766728dbce6 fuzz: add support for fuzzing under either Python 2 or 3
Augie Fackler <augie@google.com>
parents: 43831
diff changeset
    14
static PYCODETYPE *code;
43153
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    15
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    16
extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    17
{
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    18
	contrib::initpy(*argv[0]);
43870
8766728dbce6 fuzz: add support for fuzzing under either Python 2 or 3
Augie Fackler <augie@google.com>
parents: 43831
diff changeset
    19
	code = (PYCODETYPE *)Py_CompileString(R"py(
43153
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    20
try:
43870
8766728dbce6 fuzz: add support for fuzzing under either Python 2 or 3
Augie Fackler <augie@google.com>
parents: 43831
diff changeset
    21
    parsers.jsonescapeu8fast(data, paranoid)
43153
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    22
except Exception as e:
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    23
    pass
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    24
    # uncomment this print if you're editing this Python code
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    25
    # to debug failures.
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    26
    # print(e)
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    27
)py",
43870
8766728dbce6 fuzz: add support for fuzzing under either Python 2 or 3
Augie Fackler <augie@google.com>
parents: 43831
diff changeset
    28
	                                      "fuzzer", Py_file_input);
43153
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    29
	if (!code) {
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    30
		std::cerr << "failed to compile Python code!" << std::endl;
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    31
	}
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    32
	return 0;
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    33
}
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    34
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    35
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    36
{
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    37
	FuzzedDataProvider provider(Data, Size);
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    38
	bool paranoid = provider.ConsumeBool();
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    39
	std::string remainder = provider.ConsumeRemainingBytesAsString();
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    40
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    41
	PyObject *mtext = PyBytes_FromStringAndSize(
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    42
	    (const char *)remainder.c_str(), remainder.size());
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    43
	PyObject *locals = PyDict_New();
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    44
	PyDict_SetItemString(locals, "data", mtext);
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    45
	PyDict_SetItemString(locals, "paranoid", paranoid ? Py_True : Py_False);
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    46
	PyObject *res = PyEval_EvalCode(code, contrib::pyglobals(), locals);
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    47
	if (!res) {
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    48
		PyErr_Print();
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    49
	}
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    50
	Py_XDECREF(res);
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    51
	Py_DECREF(locals);
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    52
	Py_DECREF(mtext);
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    53
	return 0; // Non-zero return values are reserved for future use.
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    54
}
741fb1a95da2 fuzz: new target to fuzz jsonescapeu8fast
Augie Fackler <augie@google.com>
parents:
diff changeset
    55
}