5 ms·
You can't do that in C++ or any language. You need to do your own relocations and remember enough information to do them. You can't count on any particular virt
by quotemstr 6y ago
You can't do that in C++ or any language. You need to do your own relocations and remember enough information to do them. You can't count on any particular virtual address being available on a modern system, not if you want to take advantage of ASLR.
The trouble is that we have to mark relocated pages dirty because the kernel isn't smart enough to understand that it can demand fault and relocate on its own. Well, either that, or do the relocation anew on each access.
- secondcoming 6y agoIt works with C++ if you use boost::interprocess. Its data structures use offset_ptr internally rather than assuming every pointer is on the heap.
- quotemstr 6y agoSure. But that counts as "doing your own relocations". Unsafe Rust could do the same, yes?
- whimsicalism 6y agoWhat is being relocated?
- ithkuil 6y agoIf you use offsets instead of pointers you're doing relocations "on the fly"
- secondcoming 6y agoI don't know enough about Rust to say. If it doesn't have the concept of a 'fancy pointer' then I assume no, you'd have to essentially reproduce what boost::interprocess does.
- ekimekim 6y agoI'm still learning Rust, but iiuc you could do this by creating an OffsetPtr type that implements the Deref trait (https://doc.rust-lang.org/std/ops/trait.Deref.html https://doc.rust-lang.org/std/ops/trait.Deref.html). This is exactly a "fancy pointer" as you describe.
- amelius 6y agoThat introduces different data-types, rather than using the existing ones (instantiated with different pointer-types).
- secondcoming 6y agoIndeed. I don't know if there's a plan for the standard type to move to offset-ptr, or if there's even a std::offset_ptr, but it would be great if there was. For us, some of the 'different data type' pain was alleviated with transparent comparators. YMMV. Edit: It seems C++11 has added some form of support for it... 'fancy pointers' https://en.cppreference.com/w/cpp/named_req/Allocator#Fancy_pointers https://en.cppreference.com/w/cpp/named_req/Allocator#Fancy_...
- whimsicalism 6y agoI don't see what the issue in doing this is in C++. The only thing that'll break will be the pointers and references to things outside of the mmap'd area.
- simias 6y agoBy that logic you can do it in unsafe Rust as well then. Obviously in safe Rust having potentially dangling "pointers and references to things outside of the mmap'd area" is a big no-no. And note that even intra-area pointers would have to be offset if the base address changes. Unless you go through the trouble of only storing relative offsets to begin with, but the performance overhead might be significant.
- Hello71 6y agolibsigsegv (slow) or userfaultfd (less slow) can be used for this purpose.