Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example allocator making compartments on the heap #88

Merged
merged 1 commit into from
Nov 14, 2023

Conversation

probablytom
Copy link
Contributor

An unsophisticated hybrid allocator which create compartments on the heap, and allows mallocs against those compartments.

This is a modification of the existing bump allocator which works in hybrid mode and demonstrates these little compartments which — in theory — could sit "to the side" of the typical DDC-enforced compartments we use to police memory at the moment.

@ltratt
Copy link
Collaborator

ltratt commented Nov 6, 2023

Can we hook this into the build system so that buildbot checks it? @0152la might know the best way of doing that.

@ltratt
Copy link
Collaborator

ltratt commented Nov 6, 2023

You'll probably want to run LLVM's C formatter on this but wonderfully that formatter isn't entirely stable. By convention (since it's what we use in buildbot) we use the Morello C formatter https://github.com/capablevms/cheri-examples/blob/master/.buildbot.sh#L12

@0152la
Copy link
Contributor

0152la commented Nov 6, 2023

Can we hook this into the build system so that buildbot checks it? @0152la might know the best way of doing that.

It would have to be added somewhere appropriately in the test file [1]. Presuming in the morello-hybrid block skimming over the comments.

[1] https://github.com/capablevms/cheri-examples/blob/master/tests/run_tests.sh#L60-L93

@0152la
Copy link
Contributor

0152la commented Nov 6, 2023

Also you'll need to run clang-format [1] to have it be accepted by our CI.

[1] https://github.com/capablevms/cheri-examples/blob/master/.buildbot.sh#L9-L13

example_allocators/compartment_alloc/README.md Outdated Show resolved Hide resolved
example_allocators/compartment_alloc/compartment_alloc.c Outdated Show resolved Hide resolved
example_allocators/compartment_alloc/compartment_alloc.c Outdated Show resolved Hide resolved
example_allocators/compartment_alloc/main.c Outdated Show resolved Hide resolved
example_allocators/compartment_alloc/compartment_alloc.c Outdated Show resolved Hide resolved
example_allocators/compartment_alloc/compartment_alloc.c Outdated Show resolved Hide resolved
example_allocators/compartment_alloc/compartment_alloc.c Outdated Show resolved Hide resolved
example_allocators/compartment_alloc/compartment_alloc.c Outdated Show resolved Hide resolved
example_allocators/compartment_alloc/compartment_alloc.c Outdated Show resolved Hide resolved
probablytom added a commit to probablytom/cheri-examples that referenced this pull request Nov 7, 2023
Andrei noted that incrementing `numCompartments` is more readable if
done on its own line, rather than when indexing an array, in [PR
review](capablevms#88 (comment)). Thanks for the feedback!
probablytom added a commit to probablytom/cheri-examples that referenced this pull request Nov 7, 2023
From review comments from [Laurie](capablevms#88 (comment)) and
[Andrei](capablevms#88 (comment)). Thanks both!
probablytom added a commit to probablytom/cheri-examples that referenced this pull request Nov 7, 2023
From [review feedback from
Laurie](capablevms#88 (comment)); thanks!
@probablytom
Copy link
Contributor Author

Can we hook this into the build system so that buildbot checks it? @0152la might know the best way of doing that.

It would have to be added somewhere appropriately in the test file [1]. Presuming in the morello-hybrid block skimming over the comments.

[1] https://github.com/capablevms/cheri-examples/blob/master/tests/run_tests.sh#L60-L93

Should be added in b7d09f6. I think I've grokked the file correctly, but worth a quick check before it merges…! 🙏

@ltratt
Copy link
Collaborator

ltratt commented Nov 13, 2023

Please squash.

probablytom added a commit to probablytom/cheri-examples that referenced this pull request Nov 14, 2023
Adds an example allocator which manages "side compartments", which are
introduced in the example's README.

Add `utils.h`, accidentally ommitted earlier

`int` => `size_t` for num bytes in a compartment

Based on feedback from Andrei [in a
PR](https://github.com/capablevms/cheri-examples/pull/88/files#r1383699783).
Thanks, Andrei!

Exit on error, don't fail silently

Based on some feedback from Andrei in [PR review](https://github.com/capablevms/cheri-examples/pull/88/files#r1383704851). Thanks, Andrei!

Increment `numCompartments` on its own line

Andrei noted that incrementing `numCompartments` is more readable if
done on its own line, rather than when indexing an array, in [PR
review](capablevms#88 (comment)). Thanks for the feedback!

Formatting fixes from `clang-format`

From review comments from [Laurie](capablevms#88 (comment)) and
[Andrei](capablevms#88 (comment)). Thanks both!

README.md hard-wrapped at 80 chars

From [review feedback from
Laurie](capablevms#88 (comment)); thanks!

Explain allocator example's "side comps" in README

After feedback in review from Andrei. Thanks, Andrei!

s/`cheri_cap_build`/`cheri_address_set`/g

After review comments from Laurie and Andrei. Thanks both!

Free unused compartments, so they don't leak

Following review feedback from Andrei, Laurie. Thanks both!

Formatting, tidyup of redundant comments &c

Following review feedback from Andrei and Laurie. Thanks, both!

Add `compartment_alloc` to buildbot tests
probablytom added a commit to probablytom/cheri-examples that referenced this pull request Nov 14, 2023
Adds an example allocator which manages "side compartments", which are
introduced in this example's README.

Add `utils.h`, accidentally ommitted earlier

`int` => `size_t` for num bytes in a compartment

Based on feedback from Andrei [in a
PR](https://github.com/capablevms/cheri-examples/pull/88/files#r1383699783).
Thanks, Andrei!

Exit on error, don't fail silently

Based on some feedback from Andrei in [PR review](https://github.com/capablevms/cheri-examples/pull/88/files#r1383704851). Thanks, Andrei!

Increment `numCompartments` on its own line

Andrei noted that incrementing `numCompartments` is more readable if
done on its own line, rather than when indexing an array, in [PR
review](capablevms#88 (comment)). Thanks for the feedback!

Formatting fixes from `clang-format`

From review comments from [Laurie](capablevms#88 (comment)) and
[Andrei](capablevms#88 (comment)). Thanks both!

README.md hard-wrapped at 80 chars

From [review feedback from
Laurie](capablevms#88 (comment)); thanks!

Explain allocator example's "side comps" in README

After feedback in review from Andrei. Thanks, Andrei!

s/`cheri_cap_build`/`cheri_address_set`/g

After review comments from Laurie and Andrei. Thanks both!

Free unused compartments, so they don't leak

Following review feedback from Andrei, Laurie. Thanks both!

Formatting, tidyup of redundant comments &c

Following review feedback from Andrei and Laurie. Thanks, both!

Add `compartment_alloc` to buildbot tests
@probablytom
Copy link
Contributor Author

Squashed (sorry for the noise). From the other PRs it looks like bors is responsible for actually merging — does it matter who invokes it?

@0152la
Copy link
Contributor

0152la commented Nov 14, 2023

We just deprecated bors, and reviewers are meant to decide when to merge (and for the future, when to allow force pushes to be made).

@0152la
Copy link
Contributor

0152la commented Nov 14, 2023

@probablytom Could you please amend the commit message to be one consistent overview, rather than the squashed commit concatenation? Just discuss the overall changes, and omit small, uninteresting changes due to the review process.

Adds an example allocator which manages "side compartments", which are
introduced in this example's README. There are two functions here which
are relevant to creating compartments and allocating memory within them.

1. `init_compartment` `mmap`s an area of memory, creates a "compartment"
   (see `README` for a high-level definition, or `compartment_alloc.h`
   for the definition of the struct itself) and returns a sealed
   capability which can be used to identify a compartment when
   allocating memory.
2. `malloc_compartment` allocates memory within a compartment's `mmap`'d
   buffer. It accepts two arguments: a number of bytes to allocate, and
   a capability identifying a compartment (i.e. one returned by
   `init_compartment`). It returns a capability which points to memory
   within the compartment, assuming it's able to allocate (errors such
   as running our of space cause the program to exit with RC 1, because
   this is an example and it doesn't have to handle things particularly
   gracefully).

There's also a function to free a compartment — `free_compartment` —
which takes a compartment's identifying capability and frees the mmap'd
buffer associated with it. This is naive and not a complete
implementation; capabilities pointing to somewhere in that now-free
buffer are still valid capabilities, but will be unsafe to use.
@probablytom
Copy link
Contributor Author

Done!

@0152la 0152la added this pull request to the merge queue Nov 14, 2023
Merged via the queue into capablevms:master with commit 2e65d86 Nov 14, 2023
1 check passed
@probablytom probablytom deleted the side_compartments branch November 14, 2023 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants