@@ -36,6 +36,10 @@ extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) {
3636 status = Py_InitializeFromConfig (&config);
3737 if (PyStatus_Exception (status)) goto fail;
3838 PyConfig_Clear (&config);
39+
40+ // Suppress Python warnings globally — all fuzzers want this.
41+ PyRun_SimpleString (" import warnings; warnings.filterwarnings('ignore')" );
42+
3943 return 0 ;
4044fail:
4145 PyConfig_Clear (&config);
@@ -117,22 +121,35 @@ static PyObject *fuzz_bytes_to_str(const std::string &data, int method) {
117121 return PyUnicode_DecodeUTF16 (
118122 data.data (), data.size (), " replace" , &order);
119123 }
120- case 3 : {
124+ default : {
121125 int order = -1 ; // little-endian
122126 return PyUnicode_DecodeUTF32 (
123127 data.data (), data.size (), " replace" , &order);
124128 }
125129 }
126- return PyUnicode_DecodeLatin1 (Y (data), NULL ); // unreachable
130+ }
131+
132+ // Run a Python code string and extract a named attribute from the resulting
133+ // globals dict. Returns a new reference. Aborts on failure — called only
134+ // during one-time init.
135+ static PyObject *run_python_and_get (const char *code, const char *name) {
136+ PyObject *globals = PyDict_New ();
137+ if (!globals) { PyErr_Print (); abort (); }
138+ PyDict_SetItemString (globals, " __builtins__" , PyEval_GetBuiltins ());
139+ PyObject *r = PyRun_String (code, Py_file_input, globals, globals);
140+ if (!r) { PyErr_Print (); Py_DECREF (globals); abort (); }
141+ Py_DECREF (r);
142+ PyObject *attr = PyDict_GetItemString (globals, name); // borrowed
143+ if (!attr) { PyErr_Print (); Py_DECREF (globals); abort (); }
144+ Py_INCREF (attr);
145+ Py_DECREF (globals);
146+ return attr;
127147}
128148
129149// ---------------------------------------------------------------------------
130150// Constants
131151// ---------------------------------------------------------------------------
132152
133- // How often (in iterations) to run PyGC_Collect().
134- static constexpr int kGcInterval = 200 ;
135-
136153// Maximum fuzz input size (1 MB).
137154static constexpr size_t kMaxInputSize = 0x100000 ;
138155
0 commit comments