mortencombat / stretchable

Layout library for Python
https://stretchable.readthedocs.io
MIT License
12 stars 3 forks source link

Memory error in taffylib.free() crashing scripts on exit #102

Open mk-fg opened 4 days ago

mk-fg commented 4 days ago

I've installed stretchable==1.1.1 into venv from pip on an Arch machine, but importing it into any script causes one of following memory errors on script exit:

python -c 'import stretchable'
double free or corruption (out)
zsh: IOT instruction (core dumped)  python -c 'import stretchable'
munmap_chunk(): invalid pointer
zsh: segmentation fault (core dumped)

taffylib.free(self.__ptr) line seem to be causing it, regardless of whether library is actually used in any way or not.

Module/library itself seem to work though, at least with basic examples from documentation, just apparently has some memory management issues in that final free() call.

Issue seem to be 100% reproducible on different Linux systems with stretchable 1.1.0 and 1.1.1, but not present in 1.0.0 (picked at random to find any working older release). A bit sad to see random memory failures popping up immediately with a rust library in particular.


Easy way to reproduce it anywhere can be using docker/podman (on any OS). Put following into a file named Dockerfile:

FROM python:3.12.7-bookworm

ENV HOME=/test UID=57839
RUN useradd -u $UID -d $HOME test && install -o test -g test -m700 -d $HOME
WORKDIR $HOME
USER test

RUN python3 -m venv _venv
RUN _venv/bin/pip install stretchable==1.1.1
RUN _venv/bin/python -c 'import stretchable'

Then run docker build . (or podman build . if using podman instead).

Expected result is this script running to a completion without issues, for example (with stretchable==1.0.0):

test@test:~% docker build .
[+] Building 5.1s (10/10) FINISHED                                                    docker:default
 => [internal] load build definition from Dockerfile                                            0.0s
 => => transferring dockerfile: 310B                                                            0.0s
 => [internal] load metadata for docker.io/library/python:3.12.7-bookworm                       0.0s
 => [internal] load .dockerignore                                                               0.0s
 => => transferring context: 2B                                                                 0.0s
 => [1/6] FROM docker.io/library/python:3.12.7-bookworm                                         0.0s
 => CACHED [2/6] RUN useradd -u 57839 -d /test test && install -o test -g test -m700 -d /test   0.0s
 => CACHED [3/6] WORKDIR /test                                                                  0.0s
 => CACHED [4/6] RUN python3 -m venv _venv                                                      0.0s
 => [5/6] RUN _venv/bin/pip install stretchable==1.0.0                                          3.0s
 => [6/6] RUN _venv/bin/python -c 'import stretchable'                                          0.4s
 => exporting to image                                                                          1.5s
 => => exporting layers                                                                         1.4s
 => => writing image sha256:184e4b626f0af871c9e984ef4ea2cb44d860317e5f7141d94f707ec5c88c89c4    0.0s

Actual result:

test@test:~% docker build .
[+] Building 0.5s (9/9) FINISHED                                                      docker:default
 => [internal] load build definition from Dockerfile                                            0.0s
 => => transferring dockerfile: 310B                                                            0.0s
 => [internal] load metadata for docker.io/library/python:3.12.7-bookworm                       0.0s
 => [internal] load .dockerignore                                                               0.0s
 => => transferring context: 2B                                                                 0.0s
 => [1/6] FROM docker.io/library/python:3.12.7-bookworm                                         0.0s
 => CACHED [2/6] RUN useradd -u 57839 -d /test test && install -o test -g test -m700 -d /test   0.0s
 => CACHED [3/6] WORKDIR /test                                                                  0.0s
 => CACHED [4/6] RUN python3 -m venv _venv                                                      0.0s
 => CACHED [5/6] RUN _venv/bin/pip install stretchable==1.1.1                                   0.0s
 => ERROR [6/6] RUN _venv/bin/python -c 'import stretchable'                                    0.4s
------
 > [6/6] RUN _venv/bin/python -c 'import stretchable':
0.338 double free or corruption (out)
0.339 Aborted (core dumped)
------
Dockerfile:10
--------------------
   8 |     RUN python3 -m venv _venv
   9 |     RUN _venv/bin/pip install stretchable==1.1.1
  10 | >>> RUN _venv/bin/python -c 'import stretchable'
  11 |
--------------------
ERROR: failed to solve: process "/bin/sh -c _venv/bin/python -c 'import stretchable'" did not complete successfully: exit code: 134

python:3.12.7-bookworm used here is an "docker-official" container image with debian-stable + python pre-installed.

I've also tried using alpine-based image with musl (python:3.12.7-alpine3.20), but I think rust library doesn't work there at all due to being linked against glibc.

Thanks!

mk-fg commented 4 days ago

I've also tried using alpine-based image with musl (python:3.12.7-alpine3.20), but I think rust library doesn't work there at all due to being linked against glibc.

Small correction here - no, it also segfaults with musl, not a ENOENT from linker:

Oct 06 19:33:33 test kernel: python[5303]: segfault at 10 ip 00007bf1bf6b895e sp 00007ffc6ff327d8 error 4 in ld-musl-x86_64.so.1[7bf1bf6a8000+54000] likely on CPU 2 (core 2, socket 0)
Oct 06 19:33:33 test kernel: Code: 1f 80 7f fc 00 74 11 85 f6 74 01 f4 8b 77 f8 81 fe ff ff 00 00 7f 01 f4 89 f0 c1 e0 04 48 98 48 29 c7 48 8b 47 f0 48 8d 4f f0 <48> 39 48 10 74 01 f4 0f b6 48 20 83 e1 1f 39 d1 7d 01 f4 8b 48 18