1- // A fuzz test for CPython.
2- //
3- // Unusually for CPython, this is written in C++ for the benefit of linking with
4- // libFuzzer.
5- //
6- // The only exposed function is LLVMFuzzerTestOneInput, which is called by
7- // fuzzers and by the _fuzz module for smoke tests.
8- //
9- // To build exactly one fuzz test, as when running in oss-fuzz etc.,
10- // build with -D _Py_FUZZ_ONE and -D _Py_FUZZ_<test_name>. e.g. to build
11- // LLVMFuzzerTestOneInput to only run "fuzz_builtin_float", build this file with
12- // -D _Py_FUZZ_ONE -D _Py_FUZZ_fuzz_builtin_float.
13- //
14- // See the source code for LLVMFuzzerTestOneInput for details.
1+ /* A fuzz test for CPython.
2+
3+ Unusually for CPython, this is written in C++ for the benefit of linking with
4+ libFuzzer.
5+
6+ The only exposed function is LLVMFuzzerTestOneInput, which is called by
7+ fuzzers and by the _fuzz module for smoke tests.
8+
9+ To build exactly one fuzz test, as when running in oss-fuzz etc.,
10+ build with -D _Py_FUZZ_ONE and -D _Py_FUZZ_<test_name>. e.g. to build
11+ LLVMFuzzerTestOneInput to only run "fuzz_builtin_float", build this file with
12+ -D _Py_FUZZ_ONE -D _Py_FUZZ_fuzz_builtin_float.
13+
14+ See the source code for LLVMFuzzerTestOneInput for details. */
1515
1616#include < Python.h>
1717#include < stdlib.h>
1818#include < inttypes.h>
1919
20- // Fuzz PyFloat_FromString as a proxy for float(str).
20+ /* Fuzz PyFloat_FromString as a proxy for float(str). */
2121static int fuzz_builtin_float (const char * data, size_t size) {
2222 PyObject* s = PyBytes_FromStringAndSize (data, size);
2323 if (s == NULL ) return 0 ;
@@ -31,7 +31,7 @@ static int fuzz_builtin_float(const char* data, size_t size) {
3131 return 0 ;
3232}
3333
34- // Fuzz PyLong_FromUnicodeObject as a proxy for int(str).
34+ /* Fuzz PyLong_FromUnicodeObject as a proxy for int(str). */
3535static int fuzz_builtin_int (const char * data, size_t size) {
3636 int base = _Py_HashBytes (data, size) % 36 ;
3737 if (base == 1 ) {
@@ -61,7 +61,7 @@ static int fuzz_builtin_int(const char* data, size_t size) {
6161 return 0 ;
6262}
6363
64- // Fuzz PyUnicode_FromStringAndSize as a proxy for unicode(str).
64+ /* Fuzz PyUnicode_FromStringAndSize as a proxy for unicode(str). */
6565static int fuzz_builtin_unicode (const char * data, size_t size) {
6666 PyObject* s = PyUnicode_FromStringAndSize (data, size);
6767 if (PyErr_Occurred () && PyErr_ExceptionMatches (PyExc_UnicodeDecodeError)) {
@@ -71,33 +71,33 @@ static int fuzz_builtin_unicode(const char* data, size_t size) {
7171 return 0 ;
7272}
7373
74- // Run fuzzer and abort on failure.
74+ /* Run fuzzer and abort on failure. */
7575static int _run_fuzz (const uint8_t *data, size_t size, int (*fuzzer)(const char * , size_t )) {
7676 int rv = fuzzer (data, size);
7777 if (PyErr_Occurred ()) {
78- // Fuzz tests should handle expected errors for themselves.
78+ /* Fuzz tests should handle expected errors for themselves. */
7979 PyErr_Print ();
8080 abort ();
8181 }
82- // Someday the return value might mean something, propagate it.
82+ /* Someday the return value might mean something, propagate it. */
8383 return rv;
8484}
8585
86- // CPython generates a lot of leak warnings for whatever reason.
86+ /* CPython generates a lot of leak warnings for whatever reason. */
8787extern " C" int __lsan_is_turned_off (void ) { return 1 ; }
8888
89- // Fuzz test interface.
90- // This returns the bitwise or of all fuzz test's return values.
91- //
92- // All fuzz tests must return 0, as all nonzero return codes are reserved for
93- // future use -- we propagate the return values for that future case.
94- // (And we bitwise or when running multiple tests to verify that normally we
95- // only return 0.)
89+ /* Fuzz test interface.
90+ This returns the bitwise or of all fuzz test's return values.
91+
92+ All fuzz tests must return 0, as all nonzero return codes are reserved for
93+ future use -- we propagate the return values for that future case.
94+ (And we bitwise or when running multiple tests to verify that normally we
95+ only return 0.) */
9696extern " C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) {
9797 if (!Py_IsInitialized ()) {
98- // LLVMFuzzerTestOneInput is called repeatedly from the same process, with
99- // no separate initialization phase, sadly, so we need to initialize CPython
100- // ourselves on the first run.
98+ /* LLVMFuzzerTestOneInput is called repeatedly from the same process,
99+ with no separate initialization phase, sadly, so we need to
100+ initialize CPython ourselves on the first run. */
101101 Py_InitializeEx (0 );
102102 }
103103
0 commit comments