Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect PyRuntime symbol address for libpython 3.11 on linux #733

Open
yiding opened this issue Dec 4, 2024 · 0 comments
Open

Incorrect PyRuntime symbol address for libpython 3.11 on linux #733

yiding opened this issue Dec 4, 2024 · 0 comments

Comments

@yiding
Copy link

yiding commented Dec 4, 2024

I ran into a case where py-spy computes the wrong address for PyRuntime and then fails to get the PyInterpreterState head ptr.

I suspect the issue is due to binary_parser.rs computes the address of PyRuntime given the symbol from the file. Possibly due to ASLR, but I have not looked too hard at this. However after disabling ASLR (via sudo sysctl -w kernel.randomize_va_space=0) py-spy works consistently.

Python version: 3.11.10
OS: linux, nix

py-spy debug output

[2024-12-04T00:06:07.953583877Z DEBUG py_spy::python_process_info] map: 00007f31d7000000-00007f31d720c000 r-- /home/yiding/.local/share/uv/python/cpython-3.11.10-linux-x86_64-gnu/lib/libpython
3.11.so.1.0                                                                                                                                                                                     
[2024-12-04T00:06:07.953633198Z DEBUG py_spy::python_process_info] map: 00007f31d720c000-00007f31d7f95000 r-x /home/yiding/.local/share/uv/python/cpython-3.11.10-linux-x86_64-gnu/lib/libpython
3.11.so.1.0                                                                                                                                                                                     
[2024-12-04T00:06:07.953680154Z DEBUG py_spy::python_process_info] map: 00007f31d7f95000-00007f31d8513000 r-- /home/yiding/.local/share/uv/python/cpython-3.11.10-linux-x86_64-gnu/lib/libpython
3.11.so.1.0                                                                                                                                                                                     
[2024-12-04T00:06:07.953728270Z DEBUG py_spy::python_process_info] map: 00007f31d8513000-00007f31d85f1000 r-- /home/yiding/.local/share/uv/python/cpython-3.11.10-linux-x86_64-gnu/lib/libpython
3.11.so.1.0                                                                                                                                                                                     
[2024-12-04T00:06:07.953776521Z DEBUG py_spy::python_process_info] map: 00007f31d85f1000-00007f31d874b000 rw- /home/yiding/.local/share/uv/python/cpython-3.11.10-linux-x86_64-gnu/lib/libpython
3.11.so.1.0      
...
[2024-12-04T00:06:07.957910205Z INFO  py_spy::python_process_info] Found libpython binary @ /home/yiding/.local/share/uv/python/cpython-3.11.10-linux-x86_64-gnu/lib/libpython3.11.so.1.0
[2024-12-04T00:06:08.062811682Z INFO  py_spy::python_process_info] got symbol Py_GetVersion.version (0x00007f31d826bf60) from libpython binary
[2024-12-04T00:06:08.062874534Z INFO  py_spy::python_process_info] Getting version from symbol address
[2024-12-04T00:06:08.062949754Z INFO  py_spy::python_process_info] Getting version from python binary BSS
[2024-12-04T00:06:08.062968650Z INFO  py_spy::python_process_info] Failed to get version from BSS section: failed to find version string
[2024-12-04T00:06:08.062981071Z INFO  py_spy::python_process_info] Getting version from libpython BSS
[2024-12-04T00:06:08.063119642Z INFO  py_spy::version] Found matching version string '3.11.10 (main)'
[2024-12-04T00:06:08.063141548Z INFO  py_spy::python_spy] python version 3.11.10 detected
[2024-12-04T00:06:08.063156220Z INFO  py_spy::python_process_info] got symbol _PyRuntime (0x00007f31d8202850) from libpython binary
[2024-12-04T00:06:08.063239741Z WARN  py_spy::python_process_info] Interpreter address from _PyRuntime symbol is invalid 5f646e655f736576
[2024-12-04T00:06:08.063252698Z INFO  py_spy::python_process_info] Failed to find runtime address from symbols, scanning BSS section from main binary
[2024-12-04T00:06:08.063277528Z INFO  py_spy::python_process_info] Failed to get interpreter from binary BSS, scanning libpython BSS

Objdump output of the libpython binary:

architecture: i386:x86-64, flags 0x00000150:
HAS_SYMS, DYNAMIC, D_PAGED
start address 0x0000000000000000

Program Header:
    LOAD off    0x0000000000000000 vaddr 0x0000000000000000 paddr 0x0000000000000000 align 2**12
         filesz 0x000000000020baf8 memsz 0x000000000020baf8 flags r--
    LOAD off    0x000000000020c000 vaddr 0x000000000020c000 paddr 0x000000000020c000 align 2**12
         filesz 0x0000000000d88da9 memsz 0x0000000000d88da9 flags r-x
    LOAD off    0x0000000000f95000 vaddr 0x0000000000f95000 paddr 0x0000000000f95000 align 2**12
         filesz 0x000000000057d278 memsz 0x000000000057d278 flags r--
    LOAD off    0x0000000001512a00 vaddr 0x0000000001513a00 paddr 0x0000000001513a00 align 2**12
         filesz 0x0000000000236970 memsz 0x0000000000285e08 flags rw-
 DYNAMIC off    0x00000000015ef4c0 vaddr 0x00000000015f04c0 paddr 0x00000000015f04c0 align 2**3
         filesz 0x0000000000000220 memsz 0x0000000000000220 flags rw-
EH_FRAME off    0x0000000001359098 vaddr 0x0000000001359098 paddr 0x0000000001359098 align 2**2
         filesz 0x00000000000476ac memsz 0x00000000000476ac flags r--
   STACK off    0x0000000000000000 vaddr 0x0000000000000000 paddr 0x0000000000000000 align 2**4
         filesz 0x0000000000000000 memsz 0x0000000000000000 flags rw-
   RELRO off    0x0000000001512a00 vaddr 0x0000000001513a00 paddr 0x0000000001513a00 align 2**0
         filesz 0x00000000000dd600 memsz 0x00000000000dd600 flags r--

...

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .gnu.hash     00003530  0000000000000200  0000000000000200  00000200  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  1 .dynsym       0000e7a8  0000000000003730  0000000000003730  00003730  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  2 .dynstr       0000a75c  0000000000011ed8  0000000000011ed8  00011ed8  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  3 .gnu.version  0000134e  000000000001c634  000000000001c634  0001c634  2**1
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  4 .gnu.version_r 000001c0  000000000001d988  000000000001d988  0001d988  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  5 .rela.dyn     001e62a0  000000000001db48  000000000001db48  0001db48  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  6 .rela.plt     00007d10  0000000000203de8  0000000000203de8  00203de8  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  7 .init         0000001f  000000000020c000  000000000020c000  0020c000  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  8 .plt          00005370  000000000020c020  000000000020c020  0020c020  2**4
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  9 .plt.got      00000120  0000000000211390  0000000000211390  00211390  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
 10 .text         00d82da0  0000000000212000  0000000000212000  00212000  2**12
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
 11 .fini         00000009  0000000000f94da0  0000000000f94da0  00f94da0  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
 12 .rodata       003c4098  0000000000f95000  0000000000f95000  00f95000  2**6
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
 13 .eh_frame_hdr 000476ac  0000000001359098  0000000001359098  01359098  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
 14 .eh_frame     00171b30  00000000013a0748  00000000013a0748  013a0748  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
 15 .init_array   00000008  0000000001513a00  0000000001513a00  01512a00  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 16 .fini_array   00000008  0000000001513a08  0000000001513a08  01512a08  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 17 .data.rel.ro  000dcab0  0000000001513a10  0000000001513a10  01512a10  2**4
                  CONTENTS, ALLOC, LOAD, DATA
 18 .dynamic      00000220  00000000015f04c0  00000000015f04c0  015ef4c0  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 19 .got          00000908  00000000015f06e0  00000000015f06e0  015ef6e0  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 20 .got.plt      000029c8  00000000015f0fe8  00000000015f0fe8  015effe8  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 21 .data         0012de8b  00000000015f39c0  00000000015f39c0  015f29c0  2**5
                  CONTENTS, ALLOC, LOAD, DATA
 22 .PyRuntime    00028b20  0000000001721850  0000000001721850  01720850  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 23 .bss          0004f498  000000000174a370  000000000174a370  01749370  2**4
                  ALLOC
 24 .comment      00000043  0000000000000000  0000000000000000  01749370  2**0
                  CONTENTS, READONLY

...

0000000001721850 g     O .PyRuntime     0000000000028b20              _PyRuntime

Actual _PyRuntime address as computed by lldb:

(lldb) tar mo loo -s _PyRuntime -A
1 symbols match '_PyRuntime' in /home/yiding/.local/share/uv/python/cpython-3.11.10-linux-x86_64-gnu/lib/libpython3.11.so.1.0:
        Address: libpython3.11.so.1.0[0x0000000001721850] (libpython3.11.so.1.0.PT_LOAD[3]..PyRuntime + 0)
        Summary: libpython3.11.so.1.0`_PyRuntime

(lldb) p &_PyRuntime
(void **) 0x00007f31d8721850
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant