← 输入框|github.com|刷新
Skip to content

lpython emulation mode segfaults #2123

Closed

Description

@certik

This fixes it:

--- a/src/runtime/lpython/lpython.py
+++ b/src/runtime/lpython/lpython.py
@@ -729,14 +729,9 @@ def c_p_pointer(cptr, targettype, targetshape=None):
         newa = ctypes.cast(cptr, targettype_ptr)
         return newa
     else:
-        if py_is_dataclass(targettype):
-            if cptr.value is None:
-                return None
-            return ctypes.cast(cptr, ctypes.py_object).value
-
         targettype_ptr = ctypes.POINTER(targettype_ptr)
         newa = ctypes.cast(cptr, targettype_ptr)
-        if is_ctypes_Structure(targettype):
+        if is_dataclass(targettype):
             # return after wrapping newa inside PointerToStruct
             return PointerToStruct(newa)
         return newa

I need to find a reproducer still. This is the bug:

(HOST)  C BEFORE : { 0 ,  0 ,  0 , ...,  0 ,  0 }
./run_full_emulation.sh: line 6: 31565 Segmentation fault: 11

Activity

  1. added
    bug
    BLOCKER
    on Jul 8, 2023
  2. certik commented on Jul 8, 2023

    @certik
    ContributorAuthor

    Everything passes except:

      File "/Users/ondrej/repos/lpython/integration_tests/bindc_09.py", line 36, in main
        assert foo.value == Value.FIVE
    AssertionError
    
  3. certik commented on Jul 9, 2023

    @certik
    ContributorAuthor

    Here is a minimal reproducible example that fails in master, but succeeds with #2124 (but this PR break another important test):

    import numpy, ctypes
    from lpython import (i64, i16, CPtr, c_p_pointer, Pointer, sizeof, packed,
            dataclass)
    
    
    global_arrays = []
    
    
    def alloc(buf_size:i64) -> i64:
        xs = numpy.empty(buf_size, dtype=numpy.uint8)
        global_arrays.append(xs)
        p = ctypes.c_void_p(xs.ctypes.data)
        return ctypes.cast(p.value, ctypes.c_void_p)
    
    
    @packed
    @dataclass
    class S:
        a: i16
        b: i64
    
    
    def main():
        p1: CPtr = alloc(sizeof(S))
        p2: Pointer[S] = c_p_pointer(p1, S)
        p2.a = i16(5)
        p2.b = i64(4)
        assert p2.a == i16(5)
        assert p2.b == i64(4)
    
    
    main()

    In master:

    $ PYTHONPATH=src/runtime/lpython/ python a.py
    zsh: segmentation fault  PYTHONPATH=src/runtime/lpython/ python a.py

    In #2124:

    $ PYTHONPATH=src/runtime/lpython/ python a.py
    $
  4. certik commented on Jul 9, 2023

    @certik
    ContributorAuthor

    The alloc is just a Python implementation of a C-style alloc, it returns a pointer to a preallocated memory.

    We need to be able to cast it to a struct correctly. We are with #2124, but then a dataclass test stops working.

  5. certik commented on Jul 9, 2023

    @certik
    ContributorAuthor

    I think the above example should NOT work. We can't take a pointer to a non bind(c) quantity. The c_loc and c_funcloc should only work on bind(c), nothing else.

  6. certik commented on Jul 9, 2023

    @certik
    ContributorAuthor

    It must be done like this:

    @ccallable
    @packed
    @dataclass
    class S:
        a: i16
        b: i64
    

    and then it works.

  7. certik commented on Jul 9, 2023

    @certik
    ContributorAuthor

    I added tests that have to work correctly in #2126, and they seem to work fine. So that's good news.

    Now the next step is to give an error message for usage that should not work.

  8. rebcabin commented on Jul 9, 2023

    @rebcabin
    Contributor

    approved

  9. certik commented on Jul 9, 2023

    @certik
    ContributorAuthor

    The error message is done here: #2133. It found many incorrect tests that I fixed.

    However, in the process I discovered further bugs with structs, such as #2134.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BLOCKERbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions