Show HN: I built a Rust crate for running unsafe code safely

https://news.ycombinator.com/rss Hits: 10
Summary

mem-isolate : Run unsafe code safely mem-isolate runs your function via a fork() , waits for the result, and returns it. This grants your code access to an exact copy of memory and state at the time just before the call, but guarantees that the function will not affect the parent process's memory footprint in any way. It forces functions to be memory pure (pure with respect to memory), even if they aren't. use mem_isolate :: execute_in_isolated_process ; // No heap, stack, or program memory out here... let result = mem_isolate :: execute_in_isolated_process ( || { // ...Can be affected by anything in here unsafe { gnarly_cpp_bindings :: potentially_leaking_function ( ) ; unstable_ffi :: segfault_prone_function ( ) ; heap_fragmenting_operation ( ) ; something_that_panics_in_a_way_you_could_recover_from ( ) ; } } ) ; Example use cases: Run code with a known memory leak Run code that fragments the heap Run unsafe code code Run your code 1ms slower (har har 馃槈, see limitations) NOTE: Because of its heavy use of POSIX system calls, this crate only supports Unix-like operating systems (e.g., Linux, macOS, BSD). Windows and wasm support are not planned at this time. See the examples/ for more uses, especially the basic error handling example. How it works POSIX systems use the fork() system call to create a new child process that is a copy of the parent. On modern systems, this is relatively cheap (~1ms) even if the parent process is using a lot of memory at the time of the call. This is because the OS uses copy-on-write memory techniques to avoid duplicating the entire memory of the parent process. At the time fork() is called, the parent and child all share the same physical pages in memory. Only when one of them modifies a page is it copied to a new location. mem-isolate uses this implementation detail as a nifty hack to provide a callable function with a temporary and isolated memory space. You can think of this isolation almost like a snapshot is taken of your program'...

First seen: 2025-04-06 15:14

Last seen: 2025-04-07 00:16