Rust and Shared Memory

I am building LD_PRELOAD library that propagates itself by intercepting exec variants.
I addition, the LD_PRELOAD library does the same initialization code, including and parsing some fairly large config files, during its initialization as part of each subprocess. Infact the resulting list of variables are all statics that will be accessed readonly by the rest of the subprocess.

I am looking to do this config parsing and initialiation only once at the top level process. Put this static data into into a shared memory block, and share the share memory to all subprocesses and their children. So that when they startup, they only need to access this shared memory, as a readonly access and initialize their global read only statics.

The only option I am seeing so far is raw libc calls to API like
https://www.man7.org/linux/man-pages/man3/shm_open.3.html

What options are available in RUST to initialize shared memory, share location with sub processes, and initialize subprocess static variables from this shared memory from the parent ?

My first idea was to look into how Rust manages using custom memory allocators:

Which doesn't look too good (But might get better in the future)
You can change the global allocator:

But this would result in all memory being allocated in shared memory
You would need to write your own allocator that uses shared memory and you'd probably need to ensure that shared memory is always mapped in the same virtual address space across processes so (this can be done with mmap)

As for sharing memory between subprocesses, I know the default behaviour of a forked process is to use COW for memory. I'm not sure if this is the case for shared memory. If shared memory isn't COW then it's fairly easy to share the memory.

So far the closest solution I can think of is the following

  1. Have the top level program read and parse all the config files
  2. Generate a JSON/YAML file in shared memory using libc and writing all the compiled config as json to that file.
  3. Have all the subsequent processes open the shared memory a file and read and parse the json content in shared memory.
    I am not seeing how much more of a performance advantage this can be. When configs/initialization involve opening data and trace files as well as compiling regex pattern list, which I would have to redo in each process anyway. I could set FD_CLOEXEC on file descriptors and inherit, which would be a bit of an optimization.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.