#314

mlock limitations

OpenGreen-Skyopened 1mo ago

What is the reasoning of only allows mmaped pages to be mlocked?

I am trying to get a program to work that uses libsodium, which has functions for mlocking memory.

Activity

5
pizlonator1mo ago

Once memory is reclaimed, the GC does things to pages that assume that those pages aren't mlocked.

On the other hand, the way the GC handles mmap'd pages is that it has a destructor associated with the allocation that remaps it to a pristine mapping (so it's fine for you to do whatever weird stuff you want to do with the memory).

I could imagine hacks that would allow you to mlock any memory, but those hacks are complex enough that I opted against going there.

How hard is it for libsodium to just mmap the memory that it wants to use with mlock?

Green-Sky1mo ago

Ah, yea was not in the GC mindset. I see why its tricky now.

I checked and technically there is a way with a correctly compiled libsodium to use mmap.

sodium_malloc() can be mmap based, for other purposes, including adding guard pages around your allocation... https://libsodium.gitbook.io/doc/memory_management#guarded-heap-allocations https://github.com/jedisct1/libsodium/blob/f6bd9140861ab6806f4a99923888b65477fc4e33/src/libsodium/sodium/utils.c#L540

I will come back with how well it works in practice to go through there.

Green-Sky1mo ago

Another consideration might be to let if fail with an error code, instead of causing a panic. Have you thought about that?

pizlonator28d ago

Another consideration might be to let if fail with an error code, instead of causing a panic. Have you thought about that?

Good idea. https://github.com/pizlonator/fil-c/issues/329

Green-Sky15d ago

Is there a plan to add an opt-in panic for this kind of "limitation". Something like a pedantic mode/flag.

The library I am using silently drops the return code and other ppl "porting" their code might not notice that this as well.