You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello,
I haven't investigated this fully yet, and won't be able to find time today. I'm forwarding the report here in case someone familiar with the code wants to take a look.
According to serge_sans_paille: if self->b_ptr contains pointer, the memcpy creates sharing, and this is dangerous: if a __del__ happens to free the original pointer, we end up with a dangling reference in new_ptr. As far as I can tell, this is what happens in the clang bindings code.
The change causing the build failure comes from this commit. ea8a0dc
The commit comment says: "Fix copy of structures when passed by value."
Steve, do you recall what this change fixed?
paulmon commented on Jun 3, 2019
paulmonmannequin
Mannequin
More actions
If I undo the changes to StructUnionType_paramfunc then test_pass_by_value (ctypes.test.test_structures.StructureTestCase) fails on x64 on Windows. Looking at the code I don't think this is specific to Windows.
Reading the related bugs more carefully I think the struct/union passing conventions are different on Windows x64 and Linux. I have a fix which works for Windows but preserves the prior code for Linux.
It would be great to be able to write a reproducer and then convert it into a proper unit test.
paulmon commented on Jun 4, 2019
paulmonmannequin
Mannequin
More actions
I added a unittest to the PR that illustrates the problem. It doesn't pass yet.
paulmon commented on Jun 12, 2019
paulmonmannequin
Mannequin
More actions
I did some reading about parameter passing and it's still not clear to me whether https://bugs.python.org/issue37140 is a bug in CPython or whether the clang bindings were relying on incorrect parameter passing behavior to work.
The change in #13796 restores the previous behavior where Windows and non-Windows builds pass structs differently.
According to serge_sans_paille: if self->b_ptr contains pointer, the memcpy creates sharing, and this is dangerous: if a __del__ happens to free the original pointer, we end up with a dangling reference in new_ptr. As far as I can tell, this is what happens in the clang bindings code.
We probably need a second parg->obj to keep self alive for as long as copied_self. Or pack it into a tuple.
Having a repro test for this would be ideal, especially if we can make it happen (even crash) on all platforms. The double-free issue would seem to be real, and I don't want it to crash on Windows either.
git clone https://github.com/llvm/llvm-project.git
cd llvm-project
mkdir _build
cd _build
cmake3 ../llvm -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$PWD/install -DPYTHON_EXECUTABLE=$HOME/sources/cpython/_build/install/bin/python3
make -j4
make check-clang-python
The crash occurs in the test_access_specifiers() method defined in tests/cindex/test_access_specifiers.py. This test uses ctypes to call functions of the libclang.so dynamic library.
I reduced the test case from 4350 lines of Python code to 131 lines of Python which only depends on the standard library and libclang.so.
So I wrote a simple C library with the structure: typedef struct Point { int x; int y; } and functions modifying this structure, structured passed by copy or structure passed by reference.
I'm surprised: ctypes works as expected :-)
Try attached point.c and point.py:
$ gcc -c point.c -fpic && gcc -shared -o libpoint.so point.o && python3 point.py
p = <Point x=1 y=2>
p = <Point x=1 y=2>
p = <Point x=0 y=0>
ok
sizeof(Point) = 16 bytes
Modify CoordType in point.c and point.py to test different sizes, on x86-64, I get:
Ok, it took me a while to understand the subtle ctypes internals. serge-sans-paille and me read the C code and run a debugger (gdb) on it to undertand how passing a structure by copy work in ctypes.
The root function is StructUnionType_paramfunc(): it copies the structure if it's larger than sizeof(void*) bytes.
StructUnionType_paramfunc() creates a new internal object which is stored into parg->obj. The only purpose of this object is to release the memory copy allocated by PyMem_Malloc().
This issue is a problem with the internal object: if the struture has a finalizer, the finalizer is called twice. First, it is called on the internal object which is more and less a full copy of the structure. Second, it is called on the structure (once the last reference to the structure is removed).
The code behaves as if the the finalizer is called twice. Even if it's two separated Python object, in fact the two objects contain the same structure values. For example, if the structure contains a pointer to memory block and the finalizer calls free(ptr): free(ptr) will be called twice with the same ptr value.
copied_self reuses the exact same type of the structure. If the structure has a finalizer defined in Python, it will be called.
copied_self finalized is called at the "cleanup:" label of _ctypes_callproc():
for (i=0; i<argcount; ++i)
Py_XDECREF(args[i].keep);
--
Trying to duplicate self isn't needed. All we need is to call PyMem_Free(ptr) at the _ctypes_callproc() exit.
My PR 15612 introduces a new internal StructParam_Type type which has exactly one purpose: call PyMem_Free(ptr) in its deallocator. The type has no finalizer.
Activity
encukou commentedon Jun 3, 2019
Hello,
I haven't investigated this fully yet, and won't be able to find time today. I'm forwarding the report here in case someone familiar with the code wants to take a look.
On Fedora, "clang" fails to build with Python 3.8, probably due this change (which was supposed to be Windows-only):
32119e1#diff-998bfefaefe2ab83d5f523e18f158fa4R413
According to serge_sans_paille: if
self->b_ptrcontains pointer, thememcpycreates sharing, and this is dangerous: if a__del__happens to free the original pointer, we end up with a dangling reference innew_ptr. As far as I can tell, this is what happens in the clang bindings code.Fedora discussion with link to logs: https://bugzilla.redhat.com/show_bug.cgi?id=1715016
paulmon commentedon Jun 3, 2019
The change causing the build failure comes from this commit.
ea8a0dc
The commit comment says: "Fix copy of structures when passed by value."
Steve, do you recall what this change fixed?
paulmon commentedon Jun 3, 2019
If I undo the changes to StructUnionType_paramfunc then test_pass_by_value (ctypes.test.test_structures.StructureTestCase) fails on x64 on Windows. Looking at the code I don't think this is specific to Windows.
This is a test for fixing this issue: https://bugs.python.org/issue29565
paulmon commentedon Jun 3, 2019
Reading the related bugs more carefully I think the struct/union passing conventions are different on Windows x64 and Linux. I have a fix which works for Windows but preserves the prior code for Linux.
vstinner commentedon Jun 4, 2019
Would you mind to elaborate? It works for me on Fedora 30 with clang 8.0.0:
$ ./configure CC=clang $ make $ ./python -m test -v test_ctypes ... Tests result: SUCCESSI tested the master branch of Python.
encukou commentedon Jun 4, 2019
The issue is with building clang using Python 3.8; not building Python 3.8 using clang :)
serge-sans-paille commentedon Jun 4, 2019
@vstinner: to reproduce the issue
vstinner commentedon Jun 4, 2019
It would be great to be able to write a reproducer and then convert it into a proper unit test.
paulmon commentedon Jun 4, 2019
I added a unittest to the PR that illustrates the problem. It doesn't pass yet.
paulmon commentedon Jun 12, 2019
I did some reading about parameter passing and it's still not clear to me whether https://bugs.python.org/issue37140 is a bug in CPython or whether the clang bindings were relying on incorrect parameter passing behavior to work.
The change in #13796 restores the previous behavior where Windows and non-Windows builds pass structs differently.
zooba commentedon Jun 24, 2019
We probably need a second parg->obj to keep self alive for as long as copied_self. Or pack it into a tuple.
Having a repro test for this would be ideal, especially if we can make it happen (even crash) on all platforms. The double-free issue would seem to be real, and I don't want it to crash on Windows either.
vstinner commentedon Aug 28, 2019
Ok, I'm able to reproduce the crash using:
git clone https://github.com/llvm/llvm-project.git
cd llvm-project
mkdir _build
cd _build
cmake3 ../llvm -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$PWD/install -DPYTHON_EXECUTABLE=$HOME/sources/cpython/_build/install/bin/python3
make -j4
make check-clang-python
The crash occurs in the test_access_specifiers() method defined in tests/cindex/test_access_specifiers.py. This test uses ctypes to call functions of the libclang.so dynamic library.
I reduced the test case from 4350 lines of Python code to 131 lines of Python which only depends on the standard library and libclang.so.
$ cd llvm-project/clang/bindings/python $ wc -l tests/cindex/test_access_specifiers.py tests/cindex/util.py clang/cindex.py 41 tests/cindex/test_access_specifiers.py 90 tests/cindex/util.py 4219 clang/cindex.py 4350 total $ wc -l bug.py 131 bug.pySee attached bug.py script. Python 3.7 is fine, whereas Python 3.8 does crash:
Notice that _CXString.__del__() is only called once in Python 3.7, but called twice in Python 3.8.
vstinner commentedon Aug 30, 2019
It's a Python 3.8 regression, so I mark it as a release blocker.
vstinner commentedon Aug 30, 2019
I was surprised by the following test in StructUnionType_paramfunc():
So I wrote a simple C library with the structure: typedef struct Point { int x; int y; } and functions modifying this structure, structured passed by copy or structure passed by reference.
I'm surprised: ctypes works as expected :-)
Try attached point.c and point.py:
Modify CoordType in point.c and point.py to test different sizes, on x86-64, I get:
vstinner commentedon Aug 30, 2019
Ok, it took me a while to understand the subtle ctypes internals. serge-sans-paille and me read the C code and run a debugger (gdb) on it to undertand how passing a structure by copy work in ctypes.
The root function is StructUnionType_paramfunc(): it copies the structure if it's larger than sizeof(void*) bytes.
StructUnionType_paramfunc() creates a new internal object which is stored into parg->obj. The only purpose of this object is to release the memory copy allocated by PyMem_Malloc().
This issue is a problem with the internal object: if the struture has a finalizer, the finalizer is called twice. First, it is called on the internal object which is more and less a full copy of the structure. Second, it is called on the structure (once the last reference to the structure is removed).
The code behaves as if the the finalizer is called twice. Even if it's two separated Python object, in fact the two objects contain the same structure values. For example, if the structure contains a pointer to memory block and the finalizer calls free(ptr): free(ptr) will be called twice with the same ptr value.
This surprising behavior comes from this code:
copied_self reuses the exact same type of the structure. If the structure has a finalizer defined in Python, it will be called.
copied_self finalized is called at the "cleanup:" label of _ctypes_callproc():
--
Trying to duplicate self isn't needed. All we need is to call PyMem_Free(ptr) at the _ctypes_callproc() exit.
My PR 15612 introduces a new internal StructParam_Type type which has exactly one purpose: call PyMem_Free(ptr) in its deallocator. The type has no finalizer.
vstinner commentedon Aug 30, 2019
New changeset 96b4087 by Victor Stinner in branch 'master':
bpo-37140: Fix StructUnionType_paramfunc() (GH-15612)
96b4087
miss-islington commentedon Aug 30, 2019
New changeset 17f61ed by Miss Islington (bot) in branch '3.8':
bpo-37140: Fix StructUnionType_paramfunc() (GH-15612)
17f61ed
vstinner commentedon Aug 30, 2019
Ok, the regression is now fixed. I close the issue.
Thanks Petr Viktorin for the bug report, Paul Monson for the fix proposed fix, and serge-sans-paille for your help on investigating this complex bug.