Bug Summary

File:build/../torch/csrc/autograd/python_anomaly_mode.cpp
Warning:line 48, column 36
PyObject ownership leak with reference count of 1

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name python_anomaly_mode.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -analyzer-output=html -analyzer-checker=python -analyzer-disable-checker=deadcode -analyzer-config prune-paths=true,suppress-c++-stdlib=true,suppress-inlined-defensive-checks=false,suppress-null-return-paths=false,crosscheck-with-z3=true,model-path=/opt/pyrefcon/lib/pyrefcon/models/models -analyzer-config experimental-enable-naive-ctu-analysis=true,ctu-dir=/tmp/pyrefcon/pytorch/csa-scan,ctu-index-name=/tmp/pyrefcon/pytorch/csa-scan/externalDefMap.txt,ctu-invocation-list=/tmp/pyrefcon/pytorch/csa-scan/invocations.yaml,display-ctu-progress=false -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=none -relaxed-aliasing -fno-rounding-math -ffp-exception-behavior=ignore -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/tmp/pyrefcon/pytorch/build -resource-dir /opt/pyrefcon/lib/clang/13.0.0 -isystem third_party/gloo -isystem ../cmake/../third_party/gloo -isystem ../cmake/../third_party/googletest/googlemock/include -isystem ../cmake/../third_party/googletest/googletest/include -isystem ../third_party/protobuf/src -isystem ../third_party/gemmlowp -isystem ../third_party/neon2sse -isystem ../third_party/XNNPACK/include -isystem ../third_party -isystem ../cmake/../third_party/eigen -isystem /opt/pyrefcon/lib/pyrefcon/models/python3.8 -isystem /usr/lib/python3/dist-packages/numpy/core/include -isystem ../cmake/../third_party/pybind11/include -isystem /usr/lib/x86_64-linux-gnu/openmpi/include/openmpi -isystem /usr/lib/x86_64-linux-gnu/openmpi/include -isystem ../third_party/ideep/mkl-dnn/include -isystem ../third_party/ideep/include -D BUILDING_TESTS -D FMT_HEADER_ONLY=1 -D HAVE_MALLOC_USABLE_SIZE=1 -D HAVE_MMAP=1 -D HAVE_SHM_OPEN=1 -D HAVE_SHM_UNLINK=1 -D MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS -D ONNXIFI_ENABLE_EXT=1 -D ONNX_ML=1 -D ONNX_NAMESPACE=onnx_torch -D THP_BUILD_MAIN_LIB -D USE_C10D -D USE_C10D_GLOO -D USE_C10D_MPI -D USE_DISTRIBUTED -D USE_EXTERNAL_MZCRC -D USE_NUMPY -D USE_RPC -D USE_TENSORPIPE -D USE_VALGRIND -D _FILE_OFFSET_BITS=64 -D torch_python_EXPORTS -I aten/src -I ../aten/src -I . -I ../ -I ../cmake/../third_party/benchmark/include -I caffe2/contrib/aten -I ../third_party/onnx -I third_party/onnx -I ../third_party/foxi -I third_party/foxi -I ../torch/.. -I ../torch/../aten/src -I ../torch/../aten/src/TH -I caffe2/aten/src -I third_party -I ../torch/../third_party/valgrind-headers -I ../torch/../third_party/gloo -I ../torch/../third_party/onnx -I ../torch/csrc -I ../torch/csrc/api/include -I ../torch/lib -I ../torch/lib/libshm -I ../torch/csrc/distributed -I ../torch/csrc/api -I ../c10/.. -I third_party/ideep/mkl-dnn/include -I ../third_party/ideep/mkl-dnn/src/../include -I ../torch/lib/libshm/../../../torch/lib -I ../third_party/fmt/include -D USE_PTHREADPOOL -D NDEBUG -D USE_KINETO -D LIBKINETO_NOCUPTI -D USE_FBGEMM -D USE_QNNPACK -D USE_PYTORCH_QNNPACK -D USE_XNNPACK -D SYMBOLICATE_MOBILE_DEBUG_HANDLE -D HAVE_AVX_CPU_DEFINITION -D HAVE_AVX2_CPU_DEFINITION -D NDEBUG -D NDEBUG -D CAFFE2_USE_GLOO -D HAVE_GCC_GET_CPUID -D USE_AVX -D USE_AVX2 -D TH_HAVE_THREAD -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /opt/pyrefcon/lib/clang/13.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -Wno-narrowing -Wall -Wextra -Werror=return-type -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-result -Wno-unused-local-typedefs -Wno-strict-overflow -Wno-strict-aliasing -Wno-error=deprecated-declarations -Wno-stringop-overflow -Wno-psabi -Wno-error=pedantic -Wno-error=redundant-decls -Wno-error=old-style-cast -Wno-unused-but-set-variable -Wno-maybe-uninitialized -Werror=format -Werror=cast-function-type -Wno-stringop-overflow -Wno-write-strings -Wno-strict-aliasing -w -std=gnu++14 -fdeprecated-macro -fdebug-compilation-dir=/tmp/pyrefcon/pytorch/build -ferror-limit 19 -fvisibility-inlines-hidden -fopenmp -fopenmp-cuda-parallel-target-regions -pthread -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -faligned-allocation -fcolor-diagnostics -vectorize-loops -vectorize-slp -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/pyrefcon/pytorch/csa-scan/reports -x c++ ../torch/csrc/autograd/python_anomaly_mode.cpp

../torch/csrc/autograd/python_anomaly_mode.cpp

1#include <torch/csrc/autograd/python_anomaly_mode.h>
2#include <c10/util/Exception.h>
3#include <pybind11/pybind11.h>
4#include <torch/csrc/Exceptions.h>
5#include <torch/csrc/autograd/python_cpp_function.h>
6#include <torch/csrc/python_headers.h>
7#include <torch/csrc/utils/auto_gil.h>
8#include <torch/csrc/utils/object_ptr.h>
9#include <torch/csrc/utils/python_strings.h>
10
11#include <iostream>
12
13namespace torch { namespace autograd {
14
15void PyAnomalyMetadata::store_stack() {
16 pybind11::gil_scoped_acquire gil;
17 THPObjectPtr mod(PyImport_ImportModule("traceback"));
18 if (!mod) {
19 throw python_error();
20 }
21
22 THPObjectPtr list(PyObject_CallMethod(mod.get(), "format_stack", ""));
23 if (!list) {
24 throw python_error();
25 }
26
27 if (PyDict_SetItemString(dict(), ANOMALY_TRACE_KEY, list.get())) {
28 throw python_error();
29 }
30}
31
32void PyAnomalyMetadata::print_stack(const std::string& current_node_name) {
33 pybind11::gil_scoped_acquire gil;
34 if (!PyDict_Check(dict())((((((PyObject*)(dict()))->ob_type))->tp_flags & ((
1UL << 29))) != 0)
) {
1
Assuming the condition is true
2
Taking false branch
35 throw std::runtime_error("Anomaly metadata is not a python dictionary.");
36 }
37 PyObject* trace_stack = PyDict_GetItemString(dict(), ANOMALY_TRACE_KEY);
38 _print_stack(trace_stack, current_node_name, false);
39 PyObject* pyparent(PyDict_GetItemString(dict(), ANOMALY_PARENT_KEY));
40
41 // if there is no "parent_" in metadata, then it means this metadata's node
42 // is the root and stop printing the traceback
43 while (pyparent) {
3
Loop condition is true. Entering loop body
44 THPObjectPtr parent_metadata(PyObject_GetAttrString(pyparent, "metadata"));
45 if (!parent_metadata) {
4
Assuming the condition is false
5
Taking false branch
46 throw python_error();
47 }
48 THPObjectPtr parent_name_pyobj(PyObject_CallMethod(pyparent, "name", ""));
6
Calling 'PyObject_CallMethod'
8
Returning from 'PyObject_CallMethod'
13
PyObject ownership leak with reference count of 1
49 if (!parent_name_pyobj) {
9
Assuming the condition is false
10
Taking false branch
50 throw python_error();
51 }
52 const char* parent_name_char = PyUnicode_AsUTF8(parent_name_pyobj.get());
53 if (!parent_name_char) {
11
Assuming 'parent_name_char' is null
12
Taking true branch
54 throw python_error();
55 }
56 const std::string parent_name(parent_name_char);
57 PyObject* parent_stack = PyDict_GetItemString(parent_metadata.get(), ANOMALY_TRACE_KEY);
58 _print_stack(parent_stack, parent_name, true);
59 // get the parent of this node, if this node is a root, pyparent is simply null
60 pyparent = PyDict_GetItemString(parent_metadata.get(), ANOMALY_PARENT_KEY);
61 }
62}
63
64void PyAnomalyMetadata::assign_parent(const std::shared_ptr<Node>& parent_node) {
65 // assign the python object of parent_node in metadata["parent_"]
66 // if parent_node is nullptr, then do nothing (it can mean that "parent_" key
67 // is not in metadata)
68
69 pybind11::gil_scoped_acquire gil;
70 if (!parent_node) return;
71
72 THPObjectPtr parent_node_(functionToPyObject(parent_node));
73 if (!parent_node_) {
74 throw python_error();
75 }
76 if (PyDict_SetItemString(dict(), ANOMALY_PARENT_KEY, parent_node_.get())) {
77 throw python_error();
78 }
79}
80
81void _print_stack(PyObject* stack, const std::string& current_node_name, bool is_parent) {
82 if (!stack) {
83 TORCH_WARN("Error detected in ", current_node_name, ". ",::c10::Warning::warn( {__func__, "../torch/csrc/autograd/python_anomaly_mode.cpp"
, static_cast<uint32_t>(85)}, ::c10::str("Error detected in "
, current_node_name, ". ", "No forward pass information available. Enable detect anomaly "
"during forward pass for more information."), false)
84 "No forward pass information available. Enable detect anomaly "::c10::Warning::warn( {__func__, "../torch/csrc/autograd/python_anomaly_mode.cpp"
, static_cast<uint32_t>(85)}, ::c10::str("Error detected in "
, current_node_name, ". ", "No forward pass information available. Enable detect anomaly "
"during forward pass for more information."), false)
85 "during forward pass for more information.")::c10::Warning::warn( {__func__, "../torch/csrc/autograd/python_anomaly_mode.cpp"
, static_cast<uint32_t>(85)}, ::c10::str("Error detected in "
, current_node_name, ". ", "No forward pass information available. Enable detect anomaly "
"during forward pass for more information."), false)
;
86 return;
87 }
88
89 THPObjectPtr empty_string(PyUnicode_FromString(""));
90 if (!empty_string) {
91 throw python_error();
92 }
93
94 // stack is a list of Python strings ending with newlines. Use join to convert
95 // to a single string.
96 THPObjectPtr msg(PyUnicode_Join(empty_string, stack));
97 if (!msg) {
98 throw python_error();
99 }
100
101 if (!is_parent) {
102 TORCH_WARN("Error detected in ", current_node_name, ". ",::c10::Warning::warn( {__func__, "../torch/csrc/autograd/python_anomaly_mode.cpp"
, static_cast<uint32_t>(104)}, ::c10::str("Error detected in "
, current_node_name, ". ", "Traceback of forward call that caused the error:\n"
, THPUtils_unpackString(msg.get())), false)
103 "Traceback of forward call that caused the error:\n",::c10::Warning::warn( {__func__, "../torch/csrc/autograd/python_anomaly_mode.cpp"
, static_cast<uint32_t>(104)}, ::c10::str("Error detected in "
, current_node_name, ". ", "Traceback of forward call that caused the error:\n"
, THPUtils_unpackString(msg.get())), false)
104 THPUtils_unpackString(msg.get()))::c10::Warning::warn( {__func__, "../torch/csrc/autograd/python_anomaly_mode.cpp"
, static_cast<uint32_t>(104)}, ::c10::str("Error detected in "
, current_node_name, ". ", "Traceback of forward call that caused the error:\n"
, THPUtils_unpackString(msg.get())), false)
;
105 } else {
106 TORCH_WARN("\n\n",::c10::Warning::warn( {__func__, "../torch/csrc/autograd/python_anomaly_mode.cpp"
, static_cast<uint32_t>(109)}, ::c10::str("\n\n", "Previous calculation was induced by "
, current_node_name, ". " "Traceback of forward call that induced the previous calculation:\n"
, THPUtils_unpackString(msg.get())), false)
107 "Previous calculation was induced by ", current_node_name, ". "::c10::Warning::warn( {__func__, "../torch/csrc/autograd/python_anomaly_mode.cpp"
, static_cast<uint32_t>(109)}, ::c10::str("\n\n", "Previous calculation was induced by "
, current_node_name, ". " "Traceback of forward call that induced the previous calculation:\n"
, THPUtils_unpackString(msg.get())), false)
108 "Traceback of forward call that induced the previous calculation:\n",::c10::Warning::warn( {__func__, "../torch/csrc/autograd/python_anomaly_mode.cpp"
, static_cast<uint32_t>(109)}, ::c10::str("\n\n", "Previous calculation was induced by "
, current_node_name, ". " "Traceback of forward call that induced the previous calculation:\n"
, THPUtils_unpackString(msg.get())), false)
109 THPUtils_unpackString(msg.get()))::c10::Warning::warn( {__func__, "../torch/csrc/autograd/python_anomaly_mode.cpp"
, static_cast<uint32_t>(109)}, ::c10::str("\n\n", "Previous calculation was induced by "
, current_node_name, ". " "Traceback of forward call that induced the previous calculation:\n"
, THPUtils_unpackString(msg.get())), false)
;
110 }
111}
112
113}}

/opt/pyrefcon/lib/pyrefcon/models/models/PyObject_CallMethod.model

1#ifndef PyObject_CallMethod
2struct _object;
3typedef struct _object PyObject;
4PyObject* clang_analyzer_PyObject_New_Reference();
5PyObject* PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...) {
6 return clang_analyzer_PyObject_New_Reference();
7
Setting reference count to 1
7}
8#else
9#warning "API PyObject_CallMethod is defined as a macro."
10#endif