Stream: contributing

Topic: help with RocStr double free


view this post on Zulip Luke Boswell (Apr 24 2024 at 05:49):

I'm looking for some assistance with this platform

I've managed to stumble across a double free, and I'm having trouble tracking it down.

+ roc dev --prebuilt-platform examples/simple.roc
examples/output/banana.html successfully written to disk
roc_app_binary(39425,0x204bca080) malloc: double free for ptr 0x158008c00
roc_app_binary(39425,0x204bca080) malloc: *** set a breakpoint in malloc_error_break to debug

I think it's in roc, as I can use dbg! to see I'm passing in a correct RocStr, and then I can get it to come back out and it's garbled/corrupted.

I've been working on a platform for static-site-gen. It's mostly the bones from the version in our repo, but I've made some changes so that it is much closer in user experience to using basic-cli.

My goal here is to have a nice rust platform which builds itself (not from roc cli), and is structured nicely so that it can serve as a template for other projects.

If anyone could help me that would be super appreciated. Even just ideas for how to debug.

To reproduce the bug, all you should need to do is clone the repository and then bash run.sh, which is basically a helper for the following.

cargo build --target=aarch64-apple-darwin
cp target/aarch64-apple-darwin/debug/libhost.a platform/macos-arm64.a
roc dev --prebuilt-platform examples/simple.roc

view this post on Zulip Oskar Hahn (Apr 24 2024 at 06:17):

This is probably not the answer to your question. But does the platform really need to deallocate at all? If you would change roc_dealloc to a noop and roc_realloc to libc::malloc(new_size), it would make the platform faster and would also solve the double free bug.

view this post on Zulip Luke Boswell (Apr 24 2024 at 07:13):

Ooh, that sound like a fun workaround. Thank you

view this post on Zulip Luke Boswell (Apr 24 2024 at 09:43):

Can confirm this "solves" my double free problem, haha :sweat_smile:

view this post on Zulip Luke Boswell (Apr 24 2024 at 09:44):

Which might help me to achieve my larger purposes with that platform anyway...

view this post on Zulip Brendan Hansknecht (Apr 24 2024 at 14:05):

Even better, skip malloc and use a bumpalo arena

view this post on Zulip Brendan Hansknecht (Apr 24 2024 at 14:11):

As for the memory leak, my best guess at quick glance is that it is somehow caused by this: RocResult<RocList<roc_app::UrlPath>, RocStr>

view this post on Zulip Luke Boswell (Apr 24 2024 at 22:43):

Does this mean we might have a bug. somewhere in roc_std? Or should I be using something like std::mem::ManuallyDrop somewhere?

view this post on Zulip Brendan Hansknecht (Apr 24 2024 at 23:06):

I would assume in roc_std, but would need to dig in a lot more

view this post on Zulip Luke Boswell (Apr 24 2024 at 23:16):

Ohk, I guess I'm wondering if this is something I should sweat over and is specific to my app, or should I move on to implementing features elsewhere?

I think the strategy of never dealloc works in this particular use-case.

I'd like to clean it up a little more, and then test out the cross-compilation using cargo (from my aaarch64 mac).

view this post on Zulip Brendan Hansknecht (Apr 24 2024 at 23:27):

I wouldn't worry about it too much.

view this post on Zulip Luke Boswell (Apr 25 2024 at 01:49):

Ok, so I've made a bunch of improvements to the platform and it's working nicely on my MacOS. :smile:

Now when I test it on my Ununtu x64 Linux machine (building locally) I'm having some issues. :sad:

The first issue is if I compile it to x86_64-unknown-linux-musl then when I run it I can't get rust to see the arguments provided to the application. Using dbg! I can see the args is an empty vector.

+ cp target/x86_64-unknown-linux-musl/debug/libhost.a platform/linux-x64.a
+ roc dev --prebuilt-platform examples/main.roc -- examples/content/ examples/output/
[crates/host/src/lib.rs:132:29] std::env::args().collect() = []
Missing directory arguments, usage example: roc app.roc -- path/to/input/dir path/to/output/dir

If I switch to compiling using the native toolchain rustup gave me then I can see the arguments are provided correctly, however I get a strange memory allocation of 99459012813592 bytes failed issue.

~/Documents/Github/basic-ssg$ cargo build
    Finished dev [unoptimized + debuginfo] target(s) in 0.08s
~/Documents/Github/basic-ssg$ cp target/debug/libhost.a platform/linux-x64.a
~/Documents/Github/basic-ssg$ roc-nightly version
roc nightly pre-release, built from commit d67ba43 on Mi 24 Apr 2024 09:01:43 UTC
~/Documents/Github/basic-ssg$ roc-nightly run --prebuilt-platform --linker=legacy examples/main.roc -- ./examples/content/ ./examples/output
[crates/host/src/lib.rs:132:29] std::env::args().collect() = [
    "/proc/self/fd/3",
    "./examples/content/",
    "./examples/output",
]
memory allocation of 99459012813592 bytes failed
Aborted (core dumped)

view this post on Zulip Brendan Hansknecht (Apr 25 2024 at 01:51):

99TB....you don't have that kind of ram?

view this post on Zulip Luke Boswell (Apr 25 2024 at 02:02):

Also, based on some of the things I've learnt building this, I would like to refactor basic-cli and basic-webserver a little. I like the separation of "host" .rs files and "platform" .roc files as it's much clearer where things are and how they get built.

view this post on Zulip Anton (Aug 28 2024 at 15:15):

@Luke Boswell I'm hitting this error now, do you know how you solved it?

$ roc examples/cli/false-interpreter/False.roc examples/cli/false-interpreter/examples/sqrt.false
🔨 Rebuilding platform...
memory allocation of 93824993832009 bytes failed

@Folkert de Vries has encountered this error too in the past


Last updated: Jul 05 2025 at 12:14 UTC