Skip to content

Seg fault on a method call #436

Closed

Description

@wiwichips

Issue type

Bug

How did you install PythonMonkey?

Source

OS platform and distribution

Ubuntu 22.04.4 LTS x86_64

Python version (python --version)

3.10

PythonMonkey version (pip show pythonmonkey)

0.9.0

Bug Description

In Python, when a method is called, the first parameter is the instance of the object

there are no shorthands for referencing the object’s members from its methods: the method function is declared with an explicit first argument representing the object, which is provided implicitly by the call

Anyways, if I defined a function without any arguments, and call the method in Python it seg faults

Standalone code to reproduce the issue

import pythonmonkey as pm; print(pm.__version__)

class What:
    def some_method(): # should be `def some_method(the_obj_self):`...
        return 3

obj = What()

pm.eval('x => x.some_method()')(obj)

Relevant log output or backtrace

0.9.0
Segmentation fault (core dumped)

Additional info if applicable

Here is the output with asan


0.9.0
AddressSanitizer:DEADLYSIGNAL
=================================================================
==1523849==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000010 (pc 0x5bcd24ea0e3a bp 0x77d954799600 sp 0x7fff7db59020 T0)
==1523849==The signal is caused by a READ memory access.
==1523849==Hint: address points to the zero page.
    #0 0x5bcd24ea0e3a in PyObject_Call (/usr/bin/python3.10+0x169e3a)
    #1 0x77d95a3abd8d in callPyFunc(JSContext*, unsigned int, JS::Value*) /home/will/git/pythonmonkey/src/jsTypeFactory.cc:436
    #2 0x77d959107912 in js::InternalCallOrConstruct(JSContext*, JS::CallArgs const&, js::MaybeConstruct, js::CallReason) (/home/will/git/pythonmonkey/python/pythonmonkey/libmozjs-129a1.so+0x1707912)
    #3 0x77d959111d26 in js::Interpret(JSContext*, js::RunState&) (/home/will/git/pythonmonkey/python/pythonmonkey/libmozjs-129a1.so+0x1711d26)
    #4 0x77d95910750e in js::RunScript(JSContext*, js::RunState&) (/home/will/git/pythonmonkey/python/pythonmonkey/libmozjs-129a1.so+0x170750e)
    #5 0x77d959107b95 in js::InternalCallOrConstruct(JSContext*, JS::CallArgs const&, js::MaybeConstruct, js::CallReason) (/home/will/git/pythonmonkey/python/pythonmonkey/libmozjs-129a1.so+0x1707b95)
    #6 0x77d959108115 in js::Call(JSContext*, JS::Handle<JS::Value>, JS::Handle<JS::Value>, js::AnyInvokeArgs const&, JS::MutableHandle<JS::Value>, js::CallReason) (/home/will/git/pythonmonkey/python/pythonmonkey/libmozjs-129a1.so+0x1708115)
    #7 0x77d9591b955a in JS_CallFunctionValue(JSContext*, JS::Handle<JSObject*>, JS::Handle<JS::Value>, JS::HandleValueArray const&, JS::MutableHandle<JS::Value>) (/home/will/git/pythonmonkey/python/pythonmonkey/libmozjs-129a1.so+0x17b955a)
    #8 0x77d95a3358c7 in JSFunctionProxyMethodDefinitions::JSFunctionProxy_call(_object*, _object*, _object*) /home/will/git/pythonmonkey/src/JSFunctionProxy.cc:57
    #9 0x5bcd24e8852a in _PyObject_MakeTpCall (/usr/bin/python3.10+0x15152a)
    #10 0x5bcd24e80b1b in _PyEval_EvalFrameDefault (/usr/bin/python3.10+0x149b1b)
    #11 0x5bcd24e77095  (/usr/bin/python3.10+0x140095)
    #12 0x5bcd24f6cf65 in PyEval_EvalCode (/usr/bin/python3.10+0x235f65)
    #13 0x5bcd24f97e97  (/usr/bin/python3.10+0x260e97)
    #14 0x5bcd24f9179a  (/usr/bin/python3.10+0x25a79a)
    #15 0x5bcd24f97be4  (/usr/bin/python3.10+0x260be4)
    #16 0x5bcd24f970c7 in _PyRun_SimpleFileObject (/usr/bin/python3.10+0x2600c7)
    #17 0x5bcd24f96d12 in _PyRun_AnyFileObject (/usr/bin/python3.10+0x25fd12)
    #18 0x5bcd24f8970d in Py_RunMain (/usr/bin/python3.10+0x25270d)
    #19 0x5bcd24f5fdfc in Py_BytesMain (/usr/bin/python3.10+0x228dfc)
    #20 0x77d95e029d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #21 0x77d95e029e3f in __libc_start_main_impl ../csu/libc-start.c:392
    #22 0x5bcd24f5fcf4 in _start (/usr/bin/python3.10+0x228cf4)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (/usr/bin/python3.10+0x169e3a) in PyObject_Call
==1523849==ABORTING

What branch of PythonMonkey were you developing on? (If applicable)

main

Activity

  1. wiwichips commented on Sep 11, 2024

    @wiwichips
    CollaboratorAuthor

    Possibly related to #435 ?

  2. changed the title [-]Seg fault on a method defined without object reference parameter[/-] [+]Seg fault on a method call[/+] on Sep 11, 2024
  3. wiwichips commented on Sep 11, 2024

    @wiwichips
    CollaboratorAuthor

    Nevermind, I still get seg faults when I do have self in the method - so my earlier theory can be ignored...?

    import pythonmonkey as pm; print(pm.__version__)
    
    class What:
        def some_method(self):
            return 3
    
    obj = What()
    
    pm.eval('x => x.some_method()')(obj)

    OUTPUT:

    (pythonmonkey-py3.10) will@bestia:~/git/bifrost2 ➡ ./epic.py 
    AddressSanitizer:DEADLYSIGNAL
    AddressSanitizer:DEADLYSIGNAL
    AddressSanitizer:DEADLYSIGNAL
    AddressSanitizer:DEADLYSIGNAL
    AddressSanitizer:DEADLYSIGNAL
    AddressSanitizer:DEADLYSIGNAL
    AddressSanitizer:DEADLYSIGNAL
    Segmentation fault (core dumped)
    

    , but also sometimes the output shows up as:

    0.9.0
    
    =================================================================
    ==1524349==ERROR: LeakSanitizer: detected memory leaks
    
    Direct leak of 1132712 byte(s) in 576 object(s) allocated from:
        #0 0x7ac300eb4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x5889c204b9c2 in PyObject_Malloc (/usr/bin/python3.10+0x11b9c2)
    
    Direct leak of 411474 byte(s) in 183 object(s) allocated from:
        #0 0x7ac300eb4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x5889c204d288  (/usr/bin/python3.10+0x11d288)
    
    Direct leak of 74013 byte(s) in 61 object(s) allocated from:
        #0 0x7ac300eb4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x5889c206b0ce  (/usr/bin/python3.10+0x13b0ce)
    
    Direct leak of 68049 byte(s) in 8 object(s) allocated from:
        #0 0x7ac300eb4c38 in __interceptor_realloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:164
        #1 0x5889c20922b1  (/usr/bin/python3.10+0x1622b1)
    
    Direct leak of 64408 byte(s) in 72 object(s) allocated from:
        #0 0x7ac300eb4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x5889c2068593 in PyDict_Copy (/usr/bin/python3.10+0x138593)
    
    Direct leak of 33896 byte(s) in 20 object(s) allocated from:
        #0 0x7ac300eb4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x5889c2066a2d  (/usr/bin/python3.10+0x136a2d)
    
    Direct leak of 26624 byte(s) in 6 object(s) allocated from:
        #0 0x7ac300eb4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x5889c20658fc  (/usr/bin/python3.10+0x1358fc)
    
    Direct leak of 22616 byte(s) in 22 object(s) allocated from:
        #0 0x7ac300eb4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x5889c2058100 in PyType_GenericAlloc (/usr/bin/python3.10+0x128100)
    
    Direct leak of 16278 byte(s) in 22 object(s) allocated from:
        #0 0x7ac300eb4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x5889c206ab96 in PyBytes_FromStringAndSize (/usr/bin/python3.10+0x13ab96)
    
    Direct leak of 14232 byte(s) in 21 object(s) allocated from:
        #0 0x7ac300eb4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x5889c204c6b7 in _PyObject_GC_NewVar (/usr/bin/python3.10+0x11c6b7)
    
    Direct leak of 12512 byte(s) in 391 object(s) allocated from:
        #0 0x7ac300eb61e7 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:99
        #1 0x7ac2fcd9c96c in StrType::proxifyString(JSContext*, JS::Handle<JS::Value>) /home/will/git/pythonmonkey/src/StrType.cc:113
        #2 0x7ac2fcd9d5fe in StrType::getPyObject(JSContext*, JS::Handle<JS::Value>) /home/will/git/pythonmonkey/src/StrType.cc:193
        #3 0x7ac2fcdb95e3 in pyTypeFactory(JSContext*, JS::Handle<JS::Value>) /home/will/git/pythonmonkey/src/pyTypeFactory.cc:56
        #4 0x7ac2fcd406b5 in getKey /home/will/git/pythonmonkey/src/JSObjectProxy.cc:124
        #5 0x7ac2fcd41205 in JSObjectProxyMethodDefinitions::JSObjectProxy_get_subscript(JSObjectProxy*, _object*) /home/will/git/pythonmonkey/src/JSObjectProxy.cc:162
        #6 0x5889c2163ee2 in PyMapping_GetItemString (/usr/bin/python3.10+0x233ee2)
    
    Direct leak of 5603 byte(s) in 8 object(s) allocated from:
        #0 0x7ac300eb4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x5889c204ba9d  (/usr/bin/python3.10+0x11ba9d)
    
    Direct leak of 2880 byte(s) in 12 object(s) allocated from:
        #0 0x7ac300eb4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x7ac2787b76dd in CRYPTO_zalloc (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1b76dd)
    
    Direct leak of 2824 byte(s) in 4 object(s) allocated from:
        #0 0x7ac300eb4c38 in __interceptor_realloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:164
        #1 0x5889c216dbcc in _PyObject_GC_Resize (/usr/bin/python3.10+0x23dbcc)
    
    Direct leak of 1984 byte(s) in 62 object(s) allocated from:
        #0 0x7ac300eb61e7 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:99
        #1 0x7ac2fcd9c96c in StrType::proxifyString(JSContext*, JS::Handle<JS::Value>) /home/will/git/pythonmonkey/src/StrType.cc:113
        #2 0x7ac2fcd9d5fe in StrType::getPyObject(JSContext*, JS::Handle<JS::Value>) /home/will/git/pythonmonkey/src/StrType.cc:193
        #3 0x7ac2fcdb95e3 in pyTypeFactory(JSContext*, JS::Handle<JS::Value>) /home/will/git/pythonmonkey/src/pyTypeFactory.cc:56
        #4 0x7ac2fcd1f7e1 in JSArrayProxyMethodDefinitions::JSArrayProxy_get_subscript(JSArrayProxy*, _object*) /home/will/git/pythonmonkey/src/JSArrayProxy.cc:123
        #5 0x5889c2073e90 in _PyEval_EvalFrameDefault (/usr/bin/python3.10+0x143e90)
    
    Direct leak of 538 byte(s) in 11 object(s) allocated from:
        #0 0x7ac300eb4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x5889c205589a in PyMem_Malloc (/usr/bin/python3.10+0x12589a)
    
    Direct leak of 387 byte(s) in 1 object(s) allocated from:
        #0 0x7ac300eb4c38 in __interceptor_realloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:164
        #1 0x5889c20bde14 in _PyBytes_Resize (/usr/bin/python3.10+0x18de14)
    
    Direct leak of 160 byte(s) in 5 object(s) allocated from:
        #0 0x7ac300eb4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x5889c20485f3 in PyThread_allocate_lock (/usr/bin/python3.10+0x1185f3)
    
    Direct leak of 64 byte(s) in 2 object(s) allocated from:
        #0 0x7ac300eb61e7 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:99
        #1 0x7ac2fcd15348 in DictType::getPyObject(JSContext*, JS::Handle<JS::Value>) /home/will/git/pythonmonkey/src/DictType.cc:24
        #2 0x7ac2fcdba0d7 in pyTypeFactory(JSContext*, JS::Handle<JS::Value>) /home/will/git/pythonmonkey/src/pyTypeFactory.cc:121
        #3 0x7ac2fcdb32c5 in eval /home/will/git/pythonmonkey/src/modules/pythonmonkey/pythonmonkey.cc:480
        #4 0x5889c208add7  (/usr/bin/python3.10+0x15add7)
    
    Direct leak of 64 byte(s) in 2 object(s) allocated from:
        #0 0x7ac300eb61e7 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:99
        #1 0x7ac2fcd35163 in JSFunctionProxyMethodDefinitions::JSFunctionProxy_new(_typeobject*, _object*, _object*) /home/will/git/pythonmonkey/src/JSFunctionProxy.cc:30
        #2 0x5889c2081476 in _PyObject_MakeTpCall (/usr/bin/python3.10+0x151476)
    
    Direct leak of 56 byte(s) in 1 object(s) allocated from:
        #0 0x7ac300eb61e7 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:99
        #1 0x7ac2fcd52adb in JobQueue::JobQueue(JSContext*) /home/will/git/pythonmonkey/src/JobQueue.cc:26
        #2 0x7ac2fcdb3ee4 in PyInit_pythonmonkey /home/will/git/pythonmonkey/src/modules/pythonmonkey/pythonmonkey.cc:568
        #3 0x5889c2190809  (/usr/bin/python3.10+0x260809)
    
    Indirect leak of 308288 byte(s) in 317 object(s) allocated from:
        #0 0x7ac300eb4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x5889c2058100 in PyType_GenericAlloc (/usr/bin/python3.10+0x128100)
    
    Indirect leak of 18379 byte(s) in 17 object(s) allocated from:
        #0 0x7ac300eb4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x5889c2083778  (/usr/bin/python3.10+0x153778)
    
    Indirect leak of 17828 byte(s) in 16 object(s) allocated from:
        #0 0x7ac300eb4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x5889c206b0ce  (/usr/bin/python3.10+0x13b0ce)
    
    Indirect leak of 8316 byte(s) in 7 object(s) allocated from:
        #0 0x7ac300eb4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x5889c204d288  (/usr/bin/python3.10+0x11d288)
    
    Indirect leak of 7136 byte(s) in 10 object(s) allocated from:
        #0 0x7ac300eb4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x5889c204c6b7 in _PyObject_GC_NewVar (/usr/bin/python3.10+0x11c6b7)
    
    Indirect leak of 3660 byte(s) in 2 object(s) allocated from:
        #0 0x7ac300eb4c38 in __interceptor_realloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:164
        #1 0x5889c20922b1  (/usr/bin/python3.10+0x1622b1)
    
    Indirect leak of 1496 byte(s) in 2 object(s) allocated from:
        #0 0x7ac300eb4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x5889c204ba9d  (/usr/bin/python3.10+0x11ba9d)
    
    Indirect leak of 1104 byte(s) in 16 object(s) allocated from:
        #0 0x7ac300eb4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x7ac2787b76dd in CRYPTO_zalloc (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1b76dd)
    
    Indirect leak of 101 byte(s) in 12 object(s) allocated from:
        #0 0x7ac300eb4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x7ac2787b8712 in CRYPTO_strndup (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1b8712)
    
    Indirect leak of 32 byte(s) in 1 object(s) allocated from:
        #0 0x7ac300eb4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x7ac2788060f5 in OPENSSL_sk_deep_copy (/lib/x86_64-linux-gnu/libcrypto.so.3+0x2060f5)
    
    Indirect leak of 27 byte(s) in 2 object(s) allocated from:
        #0 0x7ac300eb4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x7ac2787b866a in CRYPTO_strdup (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1b866a)
    
    Indirect leak of 24 byte(s) in 1 object(s) allocated from:
        #0 0x7ac300eb4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x7ac2787b76dd in CRYPTO_zalloc (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1b76dd)
        #2 0x4490da912b20c5ff  (<unknown module>)
    
    Indirect leak of 1 byte(s) in 1 object(s) allocated from:
        #0 0x7ac300eb4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x7ac2787b5c32  (/lib/x86_64-linux-gnu/libcrypto.so.3+0x1b5c32)
    
    SUMMARY: AddressSanitizer: 2257766 byte(s) leaked in 1894 allocation(s).
    
  4. moved this from Backlog to Ready To Do in PythonMonkey Kanban Boardon Sep 11, 2024
  5. moved this from Ready To Do to In progress in PythonMonkey Kanban Boardon Sep 11, 2024
  6. added this to the v1.0.1 milestone on Sep 11, 2024
  7. philippedistributive commented on Sep 11, 2024

    @philippedistributive
    Contributor

    @wiwichips yeah you need a suppr.txt like this one to properly run the sanitizer:

    leak:_PyObject_Malloc
    leak:_PyObject_Realloc
    leak:__interceptor_malloc
    leak:__interceptor_realloc
    leak:JobQueue::JobQueue
    leak:DictType::getPyObject
    leak:ListType::getPyObject
    leak:JSFunctionProxyMethodDefinitions::JSFunctionProxy_new
    leak:BufferType::toJsTypedArray
    leak:JSMethodProxyMethodDefinitions::JSMethodProxy_new
    leak:eval

  8. philippedistributive commented on Sep 11, 2024

    @philippedistributive
    Contributor

    @wiwichips cannot repro with the self in there. Can you still repro?

  9. philippedistributive commented on Sep 11, 2024

    @philippedistributive
    Contributor

    It seems like adding the self argument solves the issue indeed, and with the following, we get

    Python 3.12.3 (main, Jul 31 2024, 17:43:48) [GCC 13.2.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.

    class What:
    ... def some_method(): # should be def some_method(the_obj_self):...
    ... return 3
    ...
    obj = What()
    obj.some_method()
    Traceback (most recent call last):
    File "", line 1, in
    TypeError: What.some_method() takes 0 positional arguments but 1 was given

    which is the same as we should get here, so instead of crash now we get:

    Traceback (most recent call last):
    File "/home/philippe/Sources/PythonMonkey/buggy3.py", line 9, in
    pm.eval('x => x.some_method()')(obj)
    pythonmonkey.SpiderMonkeyError: Error in file evaluate, on line 1, column 8:
    Error: Python TypeError: What.some_method() takes 0 positional arguments but 1 was given
    JS Stack Trace:
    @evaluate:1:8

  10. moved this from In progress to In review in PythonMonkey Kanban Boardon Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions