classification
Title: tracemalloc causes segfault in "make profile-opt"
Type: crash Stage:
Components: Versions: Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: matejcik, neologix, pitrou, python-dev, skrah, vstinner
Priority: normal Keywords: patch

Created on 2014-01-22 18:59 by matejcik, last changed 2014-02-01 03:08 by vstinner. This issue is now closed.

Files
File name Uploaded Description Edit
tracemalloc_gcov.patch vstinner, 2014-01-22 23:18 review
Messages (7)
msg208842 - (view) Author: jan matejek (matejcik) * Date: 2014-01-22 18:59
When compiling "make profile-opt", the instrumented python executable segfaults upon exit, even though it appears to run fine.

This breaks the build process, because make evaluates the segfault as if the respective compilation step failed.

GDB yields the following backtrace for the crash:

#0  gcov_exit () at ../../../libgcc/libgcov.c:397
#1  0x00007ffff69b875f in __cxa_finalize (d=0x7ffff7c38a60) at cxa_finalize.c:56
#2  0x00007ffff771b983 in __do_global_dtors_aux () from ./libpython3.4m.so.1.0
#3  0x00007fffffffdd60 in ?? ()
#4  0x00007ffff7debe6a in _dl_fini () at dl-fini.c:252
Backtrace stopped: frame did not save the PC


This problem first appears with changeset 6e2089dbc5ad [1] that introduced tracemalloc. It is still reproducible on today's tip.

AFAICT, the mere presence of tracemalloc in the compiled executable causes this crash; I tried commenting out _PyTraceMalloc_Init from pythonrun.c and it did not help.

To reproduce:
./configure --enable-shared
make profile-opt

i'm on x86_64 SUSE Linux, gcc version is 4.8.1

[1] hg.python.org/cpython/rev/6e2089dbc5ad
msg208863 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-01-22 23:14
> This problem first appears with changeset 6e2089dbc5ad [1] that introduced tracemalloc. It is still reproducible on today's tip.

It looks like the bug doesn't come from the code of tracemalloc. I removed all functions, Python does still crash. No. The problem just comes from the line "#pragma pack(4)" of:

#pragma pack(4)
typedef struct
{
    PyObject *filename;
    int lineno;
} frame_t;
msg208864 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-01-22 23:18
Oops, the original code is:
---
/* Pack the frame_t structure to reduce the memory footprint on 64-bit
   architectures: 12 bytes instead of 16. This optimization might produce
   SIGBUS on architectures not supporting unaligned memory accesses (64-bit
   IPS CPU?): on such architecture, the structure must not be packed. */
#pragma pack(4)
typedef struct
#ifdef __GNUC__
__attribute__((packed))
#endif
{
    PyObject *filename;
    int lineno;
} frame_t;
---

"#pragma pack(4)" is for Microsoft Visual Studio. For GCC, there is already the "__attribute__((packed))" line. See attached workaround tracemalloc_gcov.patch. But I would interested to understand why it does crash. It might be a GCC or gcov bug.
msg208869 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2014-01-23 00:18
For gcc, #pragma pack(n) apparently sets the new aligment for the entire
compilation unit:

http://gcc.gnu.org/onlinedocs/gcc/Structure-Packing-Pragmas.html
msg209865 - (view) Author: Roundup Robot (python-dev) Date: 2014-02-01 02:54
New changeset fb2cdec2c70c by Victor Stinner in branch 'default':
Issue #20354: Fix alignment issue in the tracemalloc module on 64-bit
http://hg.python.org/cpython/rev/fb2cdec2c70c
msg209867 - (view) Author: Roundup Robot (python-dev) Date: 2014-02-01 03:07
New changeset 44b554454971 by Victor Stinner in branch 'default':
Issue #20354: Mention the fix in Misc/NEWS
http://hg.python.org/cpython/rev/44b554454971
msg209868 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-02-01 03:08
I tested "./configure --enable-shared && make profile-opt" with the fix and it worked. So I close the issue. Thanks for the report Jan Matejek.
History
Date User Action Args
2014-02-01 03:08:02vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg209868
2014-02-01 03:07:19python-devsetmessages: + msg209867
2014-02-01 02:54:12python-devsetnosy: + python-dev
messages: + msg209865
2014-01-23 00:18:16skrahsetnosy: + skrah
messages: + msg208869
2014-01-22 23:18:57vstinnersetfiles: + tracemalloc_gcov.patch
keywords: + patch
messages: + msg208864
2014-01-22 23:14:20vstinnersetnosy: + pitrou, neologix
messages: + msg208863
2014-01-22 20:48:39r.david.murraysetnosy: + vstinner
2014-01-22 18:59:07matejcikcreate