Hashing an AtomicPtr (was Region crate supports more functionality than I can implement)

Hello!

In an effort to try to add Wasmer support to the Haiku operating system, I'm trying to port the Region crate to Haiku. Unfortunately the unit-tests in the Region crate require the possibility of having the MMU support an allocated space that is niether readable nor writable. The Protection::NONE type is unavailable on Haiku and if you query an address space that has such protection it will deallocate the area immediately and return an error message.

I tried bypassing the area_for() subroutine in Haiku by creating a HashMap as a static member in Rust to track the allocations but Rust will have none of it. It refuses to create a static hash even if it is a static reference to a hash with mutexed types referring to it. I am at my wit's end trying to figure out how to implement a workaround. Is anyone here familiar with the Region crate that has any suggestions? I don't know if it is reasonable to ask the maintainer of Region to make changes based on the requirements of such an esoteric OS but I don't know if the API of the Region crate would be changed much if the Protection::NONE type is removed.

So you do have a workaround, but run into a borrow checking error? If you post some example code as to specifically what you are trying to do and what errors you receive, we might be able to help. (The textual description of such situations is almost never detailed enough.)

https://github.com/SamuraiCrow/region-rs/blob/native/src/os/haiku.rs

This is the current branch. The workaround is toward the top but there is no cross-toolchain for Haiku so I've been building natively under VMWare Player (the free version for Linux). The workaround is in the top 50 lines or so. I'm trying to it hash the addresses so I wrapped them in a non-public KeyType tuple so I could try to implement enough methods to make it hashable.

Edit

Just fixed a tangled and scrambled commit on GitHub. The link to the source should be correct now.

After some reorganizing I stumbled across AtomicPtr. Since an AtomicPtr cannot be compared to another AtomicPtr, I still cannot hash one though so the problem is not quite solved. The declaration of my static hash map is now as follows:
static ALLPAGES: Mutex<HashMap<KeyType, &Allocation> > = Mutex::new(HashMap::new());

The KeyType and Allocation still wrap non-atomic pointer types though so it still doesn't work. Hashing an AtomicPtr in the KeyType will help. I still have to figure out another way to wrap the Allocation structure around Haiku's AreaInfo structure also but I think I can handle that part.

I've got everything wrapped in Arc<> and atomic members. The HashMap is now fine and I'm down to one error: Rust still won't let me make a static member that is mutable. Even if it's wrapped in a Mutex and is perfectly safe. Does anybody know how to make a static HashMap?

Here's my latest commit:
https://github.com/SamuraiCrow/region-rs/blob/native/src/os/haiku.rs

Mutex is an interior mutability primitive, so you shouldn’t need to declare it as mut— the locking methods all take &self and provide access to an &mut T (via a guard object).

1 Like

Thanks! Just before you posted I found that the lazy_static crate provides functionality to initialize a HashMap with static ref mutability. It got me passed the unit tests. Now I just have to fix the documentation!

Edit

Documentation fixed and the unit tests didn't all pass but at least it all built.

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.