python / cpython

The Python programming language
https://www.python.org
Other
63.35k stars 30.34k forks source link

Embedded python crashes in internal function #122857

Open gsohler opened 2 months ago

gsohler commented 2 months ago

Bug report

Bug description:

# Add a code block here, if required

CPython versions tested on:

3.11

Operating systems tested on:

Windows

gsohler commented 2 months ago

Python is my favorite language and I was able to embed it into openscad has http://pythonscad.org/ So i created embedded python functions, which set up a geometry for openscad. It can do so many more things then pure openscad and I am absolutely stunned about the outcome.

pythonSCAD for windows was compiled with MXE in UNIX. Due to the fact, that MXE does not support python as a lib, I have taken python from msys2/64 , which also worked fine so far.

However, there are still things which dont work , mostly imported modules, which have a shared library When I add a simple line like:

import ctypes

unfortunately, every crashes alltogether, but I was able to trace back the problem to PyModule_Create2, which is defined as:

PyModule_Create2(PyModuleDef* module, int module_api_version) { if (!_PyImport_IsInitialized(_PyInterpreterState_GET())) { PyErr_SetString(PyExc_SystemError, "Python import machinery not initialized"); return NULL; } return _PyModule_CreateInitialized(module, module_api_version); }

when debugging it with gdb, it crashes quite early like:

(gdb) si 0x00007ff8236df9a7 in python311!PyModule_Create2 () from /c/PythonSCAD/bin/python311.dll (gdb) disassemble Dump of assembler code for function python311!PyModule_Create2: 0x00007ff8236df99c <+0>: sub $0x28,%rsp 0x00007ff8236df9a0 <+4>: mov 0x45e851(%rip),%rax # 0x7ff823b3e1f8 <python311!_PyRuntime+504> => 0x00007ff8236df9a7 <+11>: mov 0x10(%rax),%r8 0x00007ff8236df9ab <+15>: cmpq $0x0,0x378(%r8) 0x00007ff8236df9b3 <+23>: je 0x7ff823787984 <python311!PyThread_tss_is_created+270932> 0x00007ff8236df9b9 <+29>: add $0x28,%rsp 0x00007ff8236df9bd <+33>: jmp 0x7ff8236e0f7c <python311!_PyModule_CreateInitialized> 0x00007ff8236df9c2 <+38>: int3 0x00007ff8236df9c3 <+39>: int3 End of assembler dump. (gdb) si

Thread 1 received signal SIGSEGV, Segmentation fault. latest rax value is:

rax 0x1898ed20 412675360

right now, I have no idea whats going wrong and what the cpu is actually doing during the 2 instructions. Any further help to solve this is highly appreciated

josevalarezodev93 commented 2 months ago

It seems that the error you are having is because when creating the module there is some argument that is missing or misspelled since the error comes from a memory access problem or a null pointer or with an invalid memory address. That module is not being initialized correctly. Verify that the module you're creating is initialized correctly.

gsohler commented 2 months ago

Hi jose, Thank you for your attention and the further information. I understand you are saying that the ctypes module is uninitialized . In my code when I have "import ctypes", the program crashes, ctypes is a module belonging to the python package itself. Do I need to initialize ctypes prior to be able to import it ? I cant judge , if any of the registers are uninitialized, but none of them are null. The module "ctypes" might be created in memory, but of course i am not the maintainer or developer of this module. What could be next steps towards realizing the issue i am having ?

ZeroIntensity commented 2 months ago

Could you send some code that causes the crash?

gsohler commented 2 months ago

The python code is as simple as:

''' import ctypes '''

when feeding that to my embedded python, it will crash the whole applicartion all other python codes not involving imports with shared libs are fine.

My application intializes python (correctly) and finally calls PyRun_SimpleString with the python code.

when feeding to python.exe instead, its fine

ZeroIntensity commented 2 months ago

The python code is as simple as:

''' import ctypes '''

when feeding that to my embedded python, it will crash the whole applicartion all other python codes not involving imports with shared libs are fine.

My application intializes python (correctly) and finally calls PyRun_SimpleString with the python code.

when feeding to python.exe instead, its fine

How are you linking Python? (FWIW, I'm guessing in advance that this is not a CPython issue, but we'll see.)

gsohler commented 2 months ago

I am using x86_64-w64-mingw32.static.posix tool chain created by tools on mxe.cc to link the libpython3.11.dll.a file contained in mingw-w64-x86_64-python-3.11.5-2-any.pkg.tar.zst to my appplication. The File was downloaded from https://mirror.msys2.org/mingw/mingw64

Hope this is a viable way, I dont know any better