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 after reading files #295

Open
HSMF opened this issue Dec 20, 2024 · 4 comments
Open

Segmentation fault after reading files #295

HSMF opened this issue Dec 20, 2024 · 4 comments
Labels
bug Something isn't working high priority

Comments

@HSMF
Copy link

HSMF commented Dec 20, 2024

The below case produces a segmentation fault when compiling using the surgical linker. All of the main function is run, the crash happens at shutdown. The issue only occurs on files larger than 997 bytes. see this

The issue goes away when using --linker=legacy

❯ python3 -c "print('X' * 996)" > file
app [main] { pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.17.0/lZFLstMUCUvd5bjnnpYromZJXkQUrdhbva4xdBInicE.tar.br" }

import pf.File

parse = \s ->
    s
    |> Str.splitOn "\n"
    |> List.dropFirst 1
    |> List.dropIf Str.isEmpty

main =
    dbg (File.readUtf8! "file" |> parse)
    Task.ok {}
❯ roc build ./tmp.roc
0 errors and 0 warnings found in 156 ms while successfully building:

    ./tmp
❯ ./tmp
[./tmp.roc:12] File.readUtf8! "file" |> parse = []
fish: Job 1, './tmp' terminated by signal SIGSEGV (Address boundary error)

Roc version:

❯ roc version
roc nightly pre-release, built from commit 97ab13e on Mi 18 Dez 2024 09:02:06 UTC

OS is Linux 6.12.4

@Anton-4 Anton-4 added bug Something isn't working high priority labels Dec 20, 2024
@Anton-4
Copy link
Collaborator

Anton-4 commented Dec 23, 2024

Linking with the legacy linker still shows valgrind errors:

❯ ./roc build ./temp.roc --linker=legacy
0 errors and 0 warnings found in 300 ms while successfully building:

    ./temp

temp11/roc_nightly-linux_x86_64-latest/roc_nightly-linux_x86_64-2024-12-23-0bf249a 
❯ ./temp 
[./temp.roc:12] File.readUtf8! "file" |> parse = []

temp11/roc_nightly-linux_x86_64-latest/roc_nightly-linux_x86_64-2024-12-23-0bf249a 
❯ valgrind ./temp 
==40790== Memcheck, a memory error detector
==40790== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al.
==40790== Using Valgrind-3.23.0 and LibVEX; rerun with -h for copyright info
==40790== Command: ./temp
==40790== 
[./temp.roc:12] File.readUtf8! "file" |> parse = []
==40790== Invalid read of size 8
==40790==    at 0x13915D: decrement_refcounted_ptr_8 (roc_app:0)
==40790==    by 0x13911D: ??? (roc_app:0)
==40790==    by 0x13191D: Task_46_42f43e247a90ff93dac3c860bb219ee18693539a6e942bad35bcb7297d6e16 (roc_app:0)
==40790==    by 0x136147: Task_42_5d84da6abaf677d342986d45e3605cfd5bd1528ee5196616226adfb513950 (roc_app:0)
==40790==    by 0x137D32: _89_28b81340646419744ffe2153acaa8e39d3c2d10c2a51eb5702318112c7c5 (roc_app:0)
==40790==    by 0x13B75D: roc__mainForHost_0_caller (in /home/username/Downloads/temp11/roc_nightly-linux_x86_64-latest/roc_nightly-linux_x86_64-2024-12-23-0bf249a/temp)
==40790==    by 0x1E303B: rust_main (in /home/username/Downloads/temp11/roc_nightly-linux_x86_64-latest/roc_nightly-linux_x86_64-2024-12-23-0bf249a/temp)
==40790==    by 0x489BD8F: (below main) (libc_start_call_main.h:58)
==40790==  Address 0x4ba6560 is 0 bytes inside a block of size 1,005 free'd
==40790==    at 0x484BB2C: free (vg_replace_malloc.c:989)
==40790==    by 0x12BE25: ??? (in /home/username/Downloads/temp11/roc_nightly-linux_x86_64-latest/roc_nightly-linux_x86_64-2024-12-23-0bf249a/temp)
==40790==    by 0x1391D3: decrement_refcounted_ptr_8 (roc_app:0)
==40790==    by 0x13911D: ??? (roc_app:0)
==40790==    by 0x13A1C1: #Attr_#generic_rc_by_ref_2_dec (roc_app:0)
==40790==    by 0x13A1C1: ??? (roc_app:0)
==40790==    by 0x1357C8: Inspect_inspect_6bc1e748a2edd5eacd7f49eb7476d47387e76d8e94fcfcf34a1d5a6fa86e (roc_app:0)
==40790==    by 0x130351: Inspect_toStr_8c3fdd6849785e1b32106ad9c6ae59845e2314f0a6799376d4e3e3b9be62d181 (roc_app:0)
==40790==    by 0x136270: #UserApp_4_4bd18bc73cee8d6c664141b2e49674ebb21216aa20f0f89293181ce7b14e (roc_app:0)
==40790==    by 0x131910: Task_46_42f43e247a90ff93dac3c860bb219ee18693539a6e942bad35bcb7297d6e16 (roc_app:0)
==40790==    by 0x136147: Task_42_5d84da6abaf677d342986d45e3605cfd5bd1528ee5196616226adfb513950 (roc_app:0)
==40790==    by 0x137D32: _89_28b81340646419744ffe2153acaa8e39d3c2d10c2a51eb5702318112c7c5 (roc_app:0)
==40790==    by 0x13B75D: roc__mainForHost_0_caller (in /home/username/Downloads/temp11/roc_nightly-linux_x86_64-latest/roc_nightly-linux_x86_64-2024-12-23-0bf249a/temp)
==40790==  Block was alloc'd at
==40790==    at 0x484880F: malloc (vg_replace_malloc.c:446)
==40790==    by 0x19AA7C: roc_std::roc_list::RocList<T>::extend_from_slice (in /home/username/Downloads/temp11/roc_nightly-linux_x86_64-latest/roc_nightly-linux_x86_64-2024-12-23-0bf249a/temp)
==40790==    by 0x191866: roc_fx_fileReadBytes (in /home/username/Downloads/temp11/roc_nightly-linux_x86_64-latest/roc_nightly-linux_x86_64-2024-12-23-0bf249a/temp)
==40790==    by 0x13B739: roc_fx_fileReadBytes_fastcc_wrapper (in /home/username/Downloads/temp11/roc_nightly-linux_x86_64-latest/roc_nightly-linux_x86_64-2024-12-23-0bf249a/temp)
==40790==    by 0x138761: PlatformTasks_task_closure_fileReadBytes_c162bfc0bd74c841e2898960cc4e5160ab1032997985dd3fe7da33cf844631d (roc_app:0)
==40790==    by 0x1351F5: Task_67_c773168b5a79ac91672eeb52ab4405228b6e1da8f6c62d3ec2af603fa2ad92 (roc_app:0)
==40790==    by 0x135EBE: Task_46_3efb3241b6f76bcf29426c5d5647f69b665c3ac3b1fc474c237e0eea46afd1 (roc_app:0)
==40790==    by 0x1318DE: Task_46_42f43e247a90ff93dac3c860bb219ee18693539a6e942bad35bcb7297d6e16 (roc_app:0)
==40790==    by 0x136147: Task_42_5d84da6abaf677d342986d45e3605cfd5bd1528ee5196616226adfb513950 (roc_app:0)
==40790==    by 0x137D32: _89_28b81340646419744ffe2153acaa8e39d3c2d10c2a51eb5702318112c7c5 (roc_app:0)
==40790==    by 0x13B75D: roc__mainForHost_0_caller (in /home/username/Downloads/temp11/roc_nightly-linux_x86_64-latest/roc_nightly-linux_x86_64-2024-12-23-0bf249a/temp)
==40790==    by 0x1E303B: rust_main (in /home/username/Downloads/temp11/roc_nightly-linux_x86_64-latest/roc_nightly-linux_x86_64-2024-12-23-0bf249a/temp)
==40790== 
==40790== Invalid write of size 8
==40790==    at 0x139186: decrement_refcounted_ptr_8 (roc_app:0)
==40790==    by 0x13911D: ??? (roc_app:0)
==40790==    by 0x13191D: Task_46_42f43e247a90ff93dac3c860bb219ee18693539a6e942bad35bcb7297d6e16 (roc_app:0)
==40790==    by 0x136147: Task_42_5d84da6abaf677d342986d45e3605cfd5bd1528ee5196616226adfb513950 (roc_app:0)
==40790==    by 0x137D32: _89_28b81340646419744ffe2153acaa8e39d3c2d10c2a51eb5702318112c7c5 (roc_app:0)
==40790==    by 0x13B75D: roc__mainForHost_0_caller (in /home/username/Downloads/temp11/roc_nightly-linux_x86_64-latest/roc_nightly-linux_x86_64-2024-12-23-0bf249a/temp)
==40790==    by 0x1E303B: rust_main (in /home/username/Downloads/temp11/roc_nightly-linux_x86_64-latest/roc_nightly-linux_x86_64-2024-12-23-0bf249a/temp)
==40790==    by 0x489BD8F: (below main) (libc_start_call_main.h:58)
==40790==  Address 0x4ba6560 is 0 bytes inside a block of size 1,005 free'd
==40790==    at 0x484BB2C: free (vg_replace_malloc.c:989)
==40790==    by 0x12BE25: ??? (in /home/username/Downloads/temp11/roc_nightly-linux_x86_64-latest/roc_nightly-linux_x86_64-2024-12-23-0bf249a/temp)
==40790==    by 0x1391D3: decrement_refcounted_ptr_8 (roc_app:0)
==40790==    by 0x13911D: ??? (roc_app:0)
==40790==    by 0x13A1C1: #Attr_#generic_rc_by_ref_2_dec (roc_app:0)
==40790==    by 0x13A1C1: ??? (roc_app:0)
==40790==    by 0x1357C8: Inspect_inspect_6bc1e748a2edd5eacd7f49eb7476d47387e76d8e94fcfcf34a1d5a6fa86e (roc_app:0)
==40790==    by 0x130351: Inspect_toStr_8c3fdd6849785e1b32106ad9c6ae59845e2314f0a6799376d4e3e3b9be62d181 (roc_app:0)
==40790==    by 0x136270: #UserApp_4_4bd18bc73cee8d6c664141b2e49674ebb21216aa20f0f89293181ce7b14e (roc_app:0)
==40790==    by 0x131910: Task_46_42f43e247a90ff93dac3c860bb219ee18693539a6e942bad35bcb7297d6e16 (roc_app:0)
==40790==    by 0x136147: Task_42_5d84da6abaf677d342986d45e3605cfd5bd1528ee5196616226adfb513950 (roc_app:0)
==40790==    by 0x137D32: _89_28b81340646419744ffe2153acaa8e39d3c2d10c2a51eb5702318112c7c5 (roc_app:0)
==40790==    by 0x13B75D: roc__mainForHost_0_caller (in /home/username/Downloads/temp11/roc_nightly-linux_x86_64-latest/roc_nightly-linux_x86_64-2024-12-23-0bf249a/temp)
==40790==  Block was alloc'd at
==40790==    at 0x484880F: malloc (vg_replace_malloc.c:446)
==40790==    by 0x19AA7C: roc_std::roc_list::RocList<T>::extend_from_slice (in /home/username/Downloads/temp11/roc_nightly-linux_x86_64-latest/roc_nightly-linux_x86_64-2024-12-23-0bf249a/temp)
==40790==    by 0x191866: roc_fx_fileReadBytes (in /home/username/Downloads/temp11/roc_nightly-linux_x86_64-latest/roc_nightly-linux_x86_64-2024-12-23-0bf249a/temp)
==40790==    by 0x13B739: roc_fx_fileReadBytes_fastcc_wrapper (in /home/username/Downloads/temp11/roc_nightly-linux_x86_64-latest/roc_nightly-linux_x86_64-2024-12-23-0bf249a/temp)
==40790==    by 0x138761: PlatformTasks_task_closure_fileReadBytes_c162bfc0bd74c841e2898960cc4e5160ab1032997985dd3fe7da33cf844631d (roc_app:0)
==40790==    by 0x1351F5: Task_67_c773168b5a79ac91672eeb52ab4405228b6e1da8f6c62d3ec2af603fa2ad92 (roc_app:0)
==40790==    by 0x135EBE: Task_46_3efb3241b6f76bcf29426c5d5647f69b665c3ac3b1fc474c237e0eea46afd1 (roc_app:0)
==40790==    by 0x1318DE: Task_46_42f43e247a90ff93dac3c860bb219ee18693539a6e942bad35bcb7297d6e16 (roc_app:0)
==40790==    by 0x136147: Task_42_5d84da6abaf677d342986d45e3605cfd5bd1528ee5196616226adfb513950 (roc_app:0)
==40790==    by 0x137D32: _89_28b81340646419744ffe2153acaa8e39d3c2d10c2a51eb5702318112c7c5 (roc_app:0)
==40790==    by 0x13B75D: roc__mainForHost_0_caller (in /home/username/Downloads/temp11/roc_nightly-linux_x86_64-latest/roc_nightly-linux_x86_64-2024-12-23-0bf249a/temp)
==40790==    by 0x1E303B: rust_main (in /home/username/Downloads/temp11/roc_nightly-linux_x86_64-latest/roc_nightly-linux_x86_64-2024-12-23-0bf249a/temp)
==40790== 
==40790== 
==40790== HEAP SUMMARY:
==40790==     in use at exit: 0 bytes in 0 blocks
==40790==   total heap usage: 5 allocs, 5 frees, 2,166 bytes allocated
==40790== 
==40790== All heap blocks were freed -- no leaks are possible
==40790== 
==40790== For lists of detected and suppressed errors, rerun with: -s
==40790== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

@Anton-4
Copy link
Collaborator

Anton-4 commented Dec 23, 2024

This updated example works with the latest version of basic-cli on main:

app [main!] { pf: platform "../platform/main.roc" }

import pf.File

parse = \s ->
    s
    |> Str.splitOn "\n"
    |> List.dropFirst 1
    |> List.dropIf Str.isEmpty

main! = \_args ->
    dbg (File.read_utf8! "file" |> Result.withDefault "failed" |> parse)
    Ok {}

The valgrind errors are still there though, I'm going to open a new issue for that.

@Anton-4
Copy link
Collaborator

Anton-4 commented Dec 23, 2024

We can close this once we have confirmed it does not happen with the final release archive, because segfaults can be fickle.

@Anton-4
Copy link
Collaborator

Anton-4 commented Dec 28, 2024

The segfault happens with the release archive :/

As a temporary workaround, I recommend building with the legacy linker: roc build temp.roc --linker=legacy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working high priority
Projects
None yet
Development

No branches or pull requests

2 participants