https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107646
--- Comment #3 from CVS Commits <cvs-commit at gcc dot gnu.org> --- The trunk branch has been updated by Eric Feng <ef...@gcc.gnu.org>: https://gcc.gnu.org/g:38c00edd65c39b07166aa0913c79adb9bcac943c commit r14-3162-g38c00edd65c39b07166aa0913c79adb9bcac943c Author: Eric Feng <ef2...@columbia.edu> Date: Fri Aug 11 12:30:43 2023 -0400 analyzer: More features for CPython analyzer plugin [PR107646] This patch adds known function subclasses for Python/C API functions PyList_New, PyLong_FromLong, and PyList_Append. It also adds new optional parameters for region_model::get_or_create_region_for_heap_alloc, allowing for the newly allocated region to immediately transition from the start state to the assumed non-null state in the malloc state machine if desired. Finally, it adds a new procedure, dg-require-python-h, intended as a directive in Python-related analyzer tests, to append necessary Python flags during the tests' build process. The main warnings we gain in this patch with respect to the known function subclasses mentioned are leak related. For example: rc3.c: In function âcreate_py_objectâ: â rc3.c:21:10: warning: leak of âitemâ [CWE-401] [-Wanalyzer-malloc-leak] â 21 | return list; â | ^~~~ â âcreate_py_objectâ: events 1-4 â | â | 4 | PyObject* item = PyLong_FromLong(10); â | | ^~~~~~~~~~~~~~~~~~~ â | | | â | | (1) allocated here â | | (2) when âPyLong_FromLongâ succeeds â | 5 | PyObject* list = PyList_New(2); â | | ~~~~~~~~~~~~~ â | | | â | | (3) when âPyList_Newâ fails â |...... â | 21 | return list; â | | ~~~~ â | | | â | | (4) âitemâ leaks here; was allocated at (1) â Some concessions were made to simplify the analysis process when comparing kf_PyList_Append with the real implementation. In particular, PyList_Append performs some optimization internally to try and avoid calls to realloc if possible. For simplicity, we assume that realloc is called every time. Also, we grow the size by just 1 (to ensure enough space for adding a new element) rather than abide by the heuristics that the actual implementation follows. gcc/analyzer/ChangeLog: PR analyzer/107646 * call-details.h: New function. * region-model.cc (region_model::get_or_create_region_for_heap_alloc): New optional parameters. * region-model.h (class region_model): New optional parameters. * sm-malloc.cc (on_realloc_with_move): New function. (region_model::transition_ptr_sval_non_null): New function. gcc/testsuite/ChangeLog: PR analyzer/107646 * gcc.dg/plugin/analyzer_cpython_plugin.c: Analyzer support for PyList_New, PyList_Append, PyLong_FromLong * gcc.dg/plugin/plugin.exp: New test. * lib/target-supports.exp: New procedure. * gcc.dg/plugin/cpython-plugin-test-2.c: New test. Signed-off-by: Eric Feng <ef2...@columbia.edu>