Skip to content

ffi/read segfaults when trying to read :string from a pointer #1722

@CFiggers

Description

@CFiggers

I'm not 100% sure what the expected behavior is here. Short version: (ffi/read :string raw-ptr) segfaults, even though the same function called using ffi/call and a signature that defines :string as the return behaves as expected.

Janet 1.41.0-dev-306ce892 linux/x64/clang - '(doc)' for help
repl:1:> (def symbols (ffi/native "./main.so"))
<core/ffi-native 0x55B6EB9E48F0>
repl:2:> (def return_string (ffi/lookup symbols "return_string"))
<pointer 0x7F5C3B7DA110>
repl:3:> (def signature-str-return (ffi/signature :default :string))
<core/ffi-signature 0x55B6EB9F8BB0>
repl:4:> (def string-ret (ffi/call return_string signature-str-return))
"Here's a string for you!"
repl:5:> (def signature-ptr-return (ffi/signature :default :ptr))
<core/ffi-signature 0x55B6EB9FBD40>
repl:6:> (def ptr-ret (ffi/call return_string signature-ptr-return))
<pointer 0x7F5C3B7DB000>
repl:7:> (ffi/read :string ptr-ret)
zsh: segmentation fault (core dumped)  janet

Am I correctly reading and understanding the source behind ffi/call and ffi/read that both of them are using the same janet_ffi_read_one() function? If so, I'm confused as to why ffi/call returns the string, but ffi/read segfaults. Are they invoking janet_ffi_read_one() differently?

The above was captured on Linux, but I'm seeing the same behavior on Windows too.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions