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

Segmentation fault when ponyc compiles this code #4477

Closed
junetried opened this issue Jan 13, 2024 · 2 comments · Fixed by #4505
Closed

Segmentation fault when ponyc compiles this code #4477

junetried opened this issue Jan 13, 2024 · 2 comments · Fixed by #4505
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@junetried
Copy link

junetried commented Jan 13, 2024

I've been told multiple times that this looks like a compiler bug, and I agree, so I'm posting this here. The following code causes a segfault in the compiler:

struct FFIBytes
    var ptr: Pointer[U8 val] = Pointer[U8].create()
    var length: USize = 0

    fun iso string(): String val =>
        recover String.from_cpointer(consume FFIBytes.ptr, consume FFIBytes.length) end

actor Main
    new create(env: Env) =>
        env.out.print("nothing to see here")

I have honestly no idea what's going on to cause this. I've reproduced it on ponyc versions 0.49.0, 0.53.0, and 0.58.0. The latter two suggest creating an issue from the backtrace, but don't actually give one:

$ ponyc
Building builtin -> /home/june/src/ponyc/packages/builtin
Building . -> /home/june/src/projects/Pony/cffi_test
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Segmentation fault (core dumped)

While ponyc 0.49.0 does provide a backtrace:

$ /home/june/.local/share/ponyup/ponyc-release-0.49.0-x86_64-linux-gnu/bin/ponyc
Building builtin -> /home/june/.local/share/ponyup/ponyc-release-0.49.0-x86_64-linux-gnu/packages/builtin
Building . -> /home/june/src/projects/Pony/cffi_test
/tmp/cirrus-ci-build/src/libponyc/pass/refer.c:144: generate_multi_dot_name: Assertion `0` failed.

Backtrace:
  /home/june/.local/share/ponyup/ponyc-release-0.49.0-x86_64-linux-gnu/bin/ponyc(ponyint_assert_fail+0x82) [0x796352]
  /home/june/.local/share/ponyup/ponyc-release-0.49.0-x86_64-linux-gnu/bin/ponyc() [0x71bb63]
  /home/june/.local/share/ponyup/ponyc-release-0.49.0-x86_64-linux-gnu/bin/ponyc(pass_refer+0x1166) [0x71b676]
  /home/june/.local/share/ponyup/ponyc-release-0.49.0-x86_64-linux-gnu/bin/ponyc(ast_visit+0x1a7) [0x7187c7]
  /home/june/.local/share/ponyup/ponyc-release-0.49.0-x86_64-linux-gnu/bin/ponyc(ast_visit+0x179) [0x718799]
  /home/june/.local/share/ponyup/ponyc-release-0.49.0-x86_64-linux-gnu/bin/ponyc(ast_visit+0x179) [0x718799]
  /home/june/.local/share/ponyup/ponyc-release-0.49.0-x86_64-linux-gnu/bin/ponyc() [0x718ea4]
  /home/june/.local/share/ponyup/ponyc-release-0.49.0-x86_64-linux-gnu/bin/ponyc(pass_pre_refer+0x1f7) [0x71a4a7]
  /home/june/.local/share/ponyup/ponyc-release-0.49.0-x86_64-linux-gnu/bin/ponyc(ast_visit+0x90) [0x7186b0]
  /home/june/.local/share/ponyup/ponyc-release-0.49.0-x86_64-linux-gnu/bin/ponyc(ast_visit+0x179) [0x718799]
  /home/june/.local/share/ponyup/ponyc-release-0.49.0-x86_64-linux-gnu/bin/ponyc(ast_visit+0x179) [0x718799]
  /home/june/.local/share/ponyup/ponyc-release-0.49.0-x86_64-linux-gnu/bin/ponyc(ast_visit+0x179) [0x718799]
  /home/june/.local/share/ponyup/ponyc-release-0.49.0-x86_64-linux-gnu/bin/ponyc(ast_visit+0x179) [0x718799]
  /home/june/.local/share/ponyup/ponyc-release-0.49.0-x86_64-linux-gnu/bin/ponyc(ast_visit+0x179) [0x718799]
  /home/june/.local/share/ponyup/ponyc-release-0.49.0-x86_64-linux-gnu/bin/ponyc(ast_visit+0x179) [0x718799]
  /home/june/.local/share/ponyup/ponyc-release-0.49.0-x86_64-linux-gnu/bin/ponyc(ast_visit+0x179) [0x718799]
  /home/june/.local/share/ponyup/ponyc-release-0.49.0-x86_64-linux-gnu/bin/ponyc(ast_visit+0x179) [0x718799]
  /home/june/.local/share/ponyup/ponyc-release-0.49.0-x86_64-linux-gnu/bin/ponyc(ast_visit+0x179) [0x718799]
  /home/june/.local/share/ponyup/ponyc-release-0.49.0-x86_64-linux-gnu/bin/ponyc() [0x718ea4]
  /home/june/.local/share/ponyup/ponyc-release-0.49.0-x86_64-linux-gnu/bin/ponyc(ast_passes_program+0x12) [0x718972]
  /home/june/.local/share/ponyup/ponyc-release-0.49.0-x86_64-linux-gnu/bin/ponyc(program_load+0x78) [0x72d0f8]
  /home/june/.local/share/ponyup/ponyc-release-0.49.0-x86_64-linux-gnu/bin/ponyc(main+0x222) [0x6e5fd2]
  /lib64/libc.so.6(+0x281b0) [0x7f25602281b0]
  /lib64/libc.so.6(__libc_start_main+0x8b) [0x7f2560228279]
  /home/june/.local/share/ponyup/ponyc-release-0.49.0-x86_64-linux-gnu/bin/ponyc(_start+0x2e) [0x6e5cee]
This is an optimised version of ponyc: the backtrace may be imprecise or incorrect.
Use a debug version to get more meaningful information.
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace.
Aborted (core dumped)
@ponylang-main ponylang-main added the discuss during sync Should be discussed during an upcoming sync label Jan 13, 2024
@SeanTAllen
Copy link
Member

This is invalid Pony code that the compiler isn't gracefully catching.

@SeanTAllen SeanTAllen added help wanted Extra attention is needed bug Something isn't working needs investigation This needs to be looked into before its "ready for work" good first issue Good for newcomers labels Jan 13, 2024
@jemc
Copy link
Member

jemc commented Jan 16, 2024

The parts of the code that are invalid are the consume expressions.

Specifically: consume FFIBytes.ptr and consume FFIBytes.length are not valid consume expressions - you can only consume a local variable (a single lowercase identifier, with no dots) or a field of this (this followed by a dot and a single lowercase identifier).

The compiler should print a nice error message explaining this when the code is invalid.

@SeanTAllen SeanTAllen removed the discuss during sync Should be discussed during an upcoming sync label Jan 16, 2024
ArthurPV added a commit to ArthurPV/ponyc that referenced this issue Apr 14, 2024
@SeanTAllen SeanTAllen removed help wanted Extra attention is needed needs investigation This needs to be looked into before its "ready for work" labels Apr 16, 2024
SeanTAllen pushed a commit that referenced this issue Apr 16, 2024
Before this PR this code didn't work and cause segmentation fault as reported in issue #4477:

```pony
struct FFIBytes
    var ptr: Pointer[U8 val] = Pointer[U8].create()
    var length: USize = 0

    fun iso string(): String val =>
        recover String.from_cpointer(consume FFIBytes.ptr, consume FFIBytes.length) end

actor Main
    new create(env: Env) =>
        env.out.print("nothing to see here")
```

Now, with the changes in this PR, here's what the compiler generates as an error:

```
Building builtin -> /home/slacturyx/Programming/Personal/Github/ponyc/packages/builtin
Building ./app -> /home/slacturyx/Programming/Personal/Github/ponyc/app
Error:
/home/slacturyx/Programming/Personal/Github/ponyc/app/app.pony:6:46: You can't consume an expression that isn't local. More specifically, you can only consume a local variable (a single lowercase identifier, with no dots) or a field of this (this followed by a dot and a single lowercase identifier).
        recover String.from_cpointer(consume FFIBytes.ptr, consume FFIBytes.length) end
                                             ^
Error:
/home/slacturyx/Programming/Personal/Github/ponyc/app/app.pony:6:38: consuming a field is only allowed if it is reassigned in the same expression
        recover String.from_cpointer(consume FFIBytes.ptr, consume FFIBytes.length) end
                                     ^
Error:
/home/slacturyx/Programming/Personal/Github/ponyc/app/app.pony:6:68: You can't consume an expression that isn't local. More specifically, you can only consume a local variable (a single lowercase identifier, with no dots) or a field of this (this followed by a dot and a single lowercase identifier).
        recover String.from_cpointer(consume FFIBytes.ptr, consume FFIBytes.length) end
                                                                   ^
Error:
/home/slacturyx/Programming/Personal/Github/ponyc/app/app.pony:6:60: consuming a field is only allowed if it is reassigned in the same expression
        recover String.from_cpointer(consume FFIBytes.ptr, consume FFIBytes.length) end
```

Fixes #4477
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants