nikofil / rust-os

The most secure OS that you've ever laid your eyes on: no networking, user processes or interaction of any kind! (also it's in [very unsafe] Rust)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Frame alloc can go into a dead lock

vinaychandra opened this issue · comments

Thanks for a nice blog on allocator!

I have been going through your entry and saw a bug.
In frame_alloc's dealloc method, there is a lock on the free_frames to push the data. But pushing the data can potentially make a call to realloc on GlobalAllocator whose default implementation will cause to first call an alloc and then dealloc the old data after copying which will cause a dead lock as the free_frames is already locked.

A simple repro is to alloc and drop two boxes at once in your frame_alloc instead of one by one.

Hey, thanks for figuring that out! That was a case I hadn't thought of.
On the blog, I initialise free_frames with Vec::with_capacity(0) for some reason, however on my actual code I initialise it with Vec::with_capacity(200) which mitigates this bug for the brief period of time when free_frames is in use.
I'll fix this anyway by changing lock to try_lock anyway and change the with_capacity in the blog.