sos-os / alarm

ALARM: Another Library for Allocating and Releasing Memory
Apache License 2.0
8 stars 6 forks source link

Allocator test harness #52

Open hawkw opened 6 years ago

hawkw commented 6 years ago

Would be nice to have some code to make unit testing allocators easier:

At a minimum:

We might also want generic functions that take a generic allocator and make a bunch of assertions, since a lot of the things we'll want to test for correctness should be invariant across allocator implementations.

amanjeev commented 6 years ago

I have no idea about this but I will try to take a shot at it.

hawkw commented 6 years ago

@amanjeev it's a pretty big issue, so we probably ought talk about a plan for this, and maybe break it down into multiple issues or something. But, I'm super happy you're willing to tackle it!

amanjeev commented 6 years ago

But, I'm super happy you're willing to tackle it!

Thanks!

and maybe break it down into multiple issues or something

đź‘Ť

amanjeev commented 6 years ago

Adding notes here so I do not miss them -

Basics:

Notes:

amanjeev commented 6 years ago

More insight from @hawkw, adding here for documentation so its not lost:

I think what you'll want to do (after rebasing onto master) is write a new struct implementing Page. It should probably be something like this:

struct MockFrame {
    number: usize, // count the frame numbers for tracking allocations etc.
    frame: [u8; FRAME_SIZE], // the frame's actual memory area.
}

the frame's base address would be

&self.frame[0] as *const _ as usize

(the first byte in the frame) and the end address would be

&self.frame[PAGE_SIZE] as *const _ as usize

We'd probably want to leave the from_addr_up/from_addr_down methods unimplemented!() as we'd only expect to get MockFrames from our test allocator and not through address math.

hawkw commented 6 years ago

We'd probably want to leave the from_addr_up/from_addr_down methods unimplemented!() as we'd only expect to get MockFrames from our test allocator and not through address math.

Having thought about it a bit more, I think perhaps those methods could be defined on a separate trait that some implementers of Page can also implement, though on the other hand, that could just be introducing needless complexity in most cases... I'm still figuring out how the HAL APIs ought to be designed.

amanjeev commented 6 years ago

though on the other hand, that could just be introducing needless complexity in most cases.

Seems like it will be. Most usage won't be like this test suite that I am writing, right?

Regarding the trait Page, it has type declaration type Address: Address; which is bound by trait Address https://github.com/sos-os/hal9000/blob/master/hal9000/src/mem.rs#L48.

The compiler tells me -

error[E0277]: the trait bound `usize: hal9000::mem::Address` is not satisfied
  --> alarm-test/src/lib.rs:11:6
   |
11 | impl Page for MockFrame {
   |      ^^^^ the trait `hal9000::mem::Address` is not implemented for `usize`

I do not think I can impl Address for usize given neither is local to me. Am I missing something Rusty here or something OSy.

hawkw commented 6 years ago

@amanjeev You're going to want to define a newtype. Try something like this:

#[macro_use] extern crate hal9000_derive;

#[derive(Address)]
#[address_repr(usize)]
pub struct MockAddress(usize);

And then you should be able to use MockAddress as the Addr type for your Page type.

Note that the derive crate for hal9000 is still a very rough work in progress, so please let me know if anything goes wrong --- it's probably my fault, not yours!

amanjeev commented 6 years ago

I was getting an error of non-primitive cast

error[E0605]: non-primitive cast: `*const u8` as `MockAddress`
  --> alarm-test/src/lib.rs:41:9
   |
41 |         &self.frame[4096] as *const _ as MockAddress
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait

My solution was to do this MockAddress(self.frame[0]).

So far, a skeleton is here - https://github.com/sos-os/alarm/blob/amanjeev/allocator-test-harness-part-2/alarm-test/src/lib.rs#L36