wasmtime: Help debugging a seemingly random segfault in wasmtime
Hey,
In the context of Zellij, ever since a recent (still unreleased) upgrade of wasmtime, I’ve been getting intermittent and seemingly random segfaults.
I am afraid I don’t have a way to reproduce them, but I have been gathering some information about them and am hoping the maintainers can help pin-point the issue or provide more insight.
Version info: wasmtime version: 29.0.1 (probably) did not occur in wasmtime version: 21.0.2
What happens: Zellij crashes, without a panic, and syslog tells me that it got a segfault:
* [343995.595910] async-std/runti[2487041]: segfault at 705a8ea73970 ip 0000705a8ea73970 sp 0000705a8ea73948 error 15 likely on CPU 3 (core 1, socket 0)
With some debug logging, I managed to narrow the crash down to this block in our app: https://github.com/zellij-org/zellij/blob/main/zellij-server/src/plugins/wasm_bridge.rs#L1589-L1596
I have a core dump, but unfortunately not one with debug symbols so I’m not sure it’s a lot of help.
This happens about once or twice a day for me and so is extremely gnarly to pin-point. I’m not 100% sure this is a wasmtime issue, but the crash location would seem to indicate that it is.
Any help in troubleshooting or mitigating this somehow would be appreciated. On my part I’m going to try to get a coredump with some debug symbols so we’ll hopefully be less blind.
Thanks!
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 28 (14 by maintainers)
Hi all - I’m coming back to report that I’ve been working with a separate cache folder on my dev instance for 3 days and experienced no crashes. I think we nailed it on the head. Very cool and much appreciated.
TL;DR for those who don’t want to read the whole thread: This was an issue very likely stemming from this
unsafe: https://docs.rs/wasmtime/latest/wasmtime/struct.Module.html#method.deserialize_file. Thedeserialize_filemethod requires that “the file referenced must remain unchanged and a valid precompiled module for the entire lifetime of the Module”. The Zellij cache was being shared between instances that had a different compiler using the same cache indices for modules.I was not aware of this unsafe, and now that I am I’m going to get around it in one way or another on the Zellij side (likely keep the modules in memory if resource utilization / performance permits).
Thanks everyone for the excellent support!
Yes, we
mmap()the.cwasmfile directly, so if the data at offsets in the file corresponding to the running code are changed, then the running code in memory will be changed underneath the instruction pointer, wherever it is. This could very well explain the RIP-in-the-middle-of-zeroes seen above.Question: when you write out the files into the cache directory, do you open as writable and truncate, then write, or do you write a new file then rename it over the old file? I suspect the latter (“atomically replacing the file”, as described here, often the method used by text editors on save) is what one wants. The effect of this is that the old file will be unlinked from the directory when replaced, but Wasmtime will still hold the file open (via the mmap) and so the old inode with old data will continue to exist and be used.
Separately, it might be reasonable to add the compiler config to your cache key, so both variants can remain cached, but if the replacement is done properly as above then keying them separately should be “just an optimization” rather than load-bearing for correctness.
Hey @alexcrichton - first things first - I just want to make sure I do not come across as non-sympathizing or unappreciative of your situation. I very much get where you are coming from and understand the difficulty in debugging this very theoretical issue. You are not by my machine and even if you were - this is not consistently reproducible and very broad. I’m not expecting any magical solutions and am very grateful for the support I’m getting from all of you. Thank you for continuing to work with me on this!
I want to concentrate on this, because I think this is a very good guess and could very well be the issue.
The way we load plugins (modules) is to first compile them and then keep the compiled assets in our cache folder. The cache folder is indexed by a sha256 hash of the wasm-file.
When developing, I use the winch compiler while in production we use the cranelift compiler. As far as I understand it, these two would have different compiled assets but the same hash index. Could it be that while developing I override the cache index with an asset compiled by a different compiler and thus cause an already instantiated module to crash? Am I understanding the docs correctly that even already instantiated modules page from their loaded HD path?
Just the default Zellij ones.