filprofiler icon indicating copy to clipboard operation
filprofiler copied to clipboard

Implement mremap()

Open itamarst opened this issue 4 years ago • 1 comments

Implementation notes:

Preventing re-occurence of #175:

  • As with free() or realloc(), we would want to remove tracking information before running mremap() to ensure thread-safety (another thread may grab the address with another mmap() in parallel, and tracking info could be modified in wrong order). However, mremap() may fail. So options are:
    • Live with race condition.
    • Live with situations where mremap() fails and we lose tracking info. This is what realloc() does, but failed realloc() results in us hitting the OOM handler, whereas mremap() default behavior is much more likely to fail because it's not "do we have memory" it's "do we have enough memory without moving".
    • Restore tracking if mremap() fails. This shouldn't result in any race condition.
    • Special handling for MREMAP_MAYMOVE which is more like realloc(), where failure means OOM.
    • Implement resize logic inside rangemap that returns information needed to undo it.
    • Split logic between shrinking (won't fail, but needs metadata first) and increasing (might fail, but metadata can happen later).*

OOM handling:

  • Failed mremap() is only an OOM condition if MREMAP_MAYMOVE is specified but MREMAP_FIXED is not. This is likely to be a common usage pattern.

MREMAP_DONTUNMAP needs to be handled specially, since it doesn't actually remove any memory from the tracking area, it just adds some.

itamarst avatar Apr 16 '21 17:04 itamarst

Given more complex logic, this also seems like good to start moving away from C code for the external API and have be in Rust like the commercial production version, and might even allow reusing the same logic. Probably would need to make it generic over some sort of reentrancy-acquisition-and-should-I-record API.

itamarst avatar May 13 '21 20:05 itamarst