Skip to content

Commit

Permalink
Rename heap_claim_fast.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchisnall committed Jan 10, 2025
1 parent d172207 commit 37b94cf
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 40 deletions.
12 changes: 6 additions & 6 deletions examples/04.temporal_safety/allocate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ void __cheri_compartment("allocate") entry()
Debug::log("heap quota: {}", heap_quota_remaining(MALLOC_CAPABILITY));
}

// Sub object with a fast claim
// Sub object with an ephemeral claim
{
Debug::log("----- Sub object with a fast claim -----");
Debug::log("----- Sub object with an ephemeral claim -----");
void *x = malloc(100);

CHERI::Capability y{x};
Expand All @@ -96,12 +96,12 @@ void __cheri_compartment("allocate") entry()
Debug::log("Sub Object: {}", y);
Debug::log("heap quota: {}", heap_quota_remaining(MALLOC_CAPABILITY));

// Add a fast claim for y
// Add an ephemeral claim for y
Timeout t{10};
heap_claim_fast(&t, y);
heap_claim_ephemeral(&t, y);

// In this freeing x will invalidate both x & y because free
// is a cross compartment call, which releases any fast claims.
// is a cross compartment call, which releases any ephemeral claims.
free(x);
Debug::log("After free");
Debug::log("Allocated : {}", x);
Expand All @@ -119,7 +119,7 @@ void __cheri_compartment("allocate") entry()
Debug::log("Allocated : {}", x);
Debug::log("heap quota: {}", heap_quota_remaining(MALLOC_CAPABILITY));

// Get the claimant compartment to make a fast claim
// Get the claimant compartment to make a ephemeral claim
make_claim(x);

// free x. We get out quota back but x remains valid as
Expand Down
2 changes: 1 addition & 1 deletion sdk/include/platform/arty-a7/platform-ethernet.hh
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ class KunyanEthernet
// does not check the pointer which is coming from external
// untrusted components.
Timeout t{10};
if ((heap_claim_fast(&t, buffer) < 0) ||
if ((heap_claim_ephemeral(&t, buffer) < 0) ||
(!CHERI::check_pointer<CHERI::PermissionSet{
CHERI::Permission::Load}>(buffer, length)))
{
Expand Down
2 changes: 1 addition & 1 deletion sdk/include/platform/sunburst/platform-ethernet.hh
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ class Ksz8851Ethernet
// does not check the pointer which is coming from external
// untrusted components.
Timeout t{10};
if ((heap_claim_fast(&t, buffer) < 0) ||
if ((heap_claim_ephemeral(&t, buffer) < 0) ||
(!CHERI::check_pointer<CHERI::PermissionSet{
CHERI::Permission::Load}>(buffer, length)))
{
Expand Down
71 changes: 41 additions & 30 deletions sdk/include/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,32 +113,33 @@ static inline void __dead2 panic()
}
}

enum [[clang::flag_enum]] AllocateWaitFlags{
/**
* Non-blocking mode. This is equivalent to passing a timeout with no time
* remaining.
*/
AllocateWaitNone = 0,
/**
* If there is enough memory in the quarantine to fulfil the allocation, wait
* for the revoker to free objects from the quarantine.
*/
AllocateWaitRevocationNeeded = (1 << 0),
/**
* If the quota of the passed heap capability is exceeded, wait for other
* threads to free allocations.
*/
AllocateWaitQuotaExceeded = (1 << 1),
/**
* If the heap memory is exhausted, wait for any other thread of the system
* to free allocations.
*/
AllocateWaitHeapFull = (1 << 2),
/**
* Block on any of the above reasons. This is the default behavior.
*/
AllocateWaitAny = (AllocateWaitRevocationNeeded | AllocateWaitQuotaExceeded |
AllocateWaitHeapFull),
enum [[clang::flag_enum]] AllocateWaitFlags
{
/**
* Non-blocking mode. This is equivalent to passing a timeout with no time
* remaining.
*/
AllocateWaitNone = 0,
/**
* If there is enough memory in the quarantine to fulfil the allocation,
* wait for the revoker to free objects from the quarantine.
*/
AllocateWaitRevocationNeeded = (1 << 0),
/**
* If the quota of the passed heap capability is exceeded, wait for other
* threads to free allocations.
*/
AllocateWaitQuotaExceeded = (1 << 1),
/**
* If the heap memory is exhausted, wait for any other thread of the system
* to free allocations.
*/
AllocateWaitHeapFull = (1 << 2),
/**
* Block on any of the above reasons. This is the default behavior.
*/
AllocateWaitAny = (AllocateWaitRevocationNeeded |
AllocateWaitQuotaExceeded | AllocateWaitHeapFull),
};

/**
Expand Down Expand Up @@ -236,9 +237,19 @@ ssize_t __cheri_compartment("allocator")
* This function is provided by the compartment_helpers library, which must be
* linked for it to be available.
*/
int __cheri_libcall heap_claim_fast(Timeout *timeout,
const void *ptr,
const void *ptr2 __if_cxx(= nullptr));
int __cheri_libcall heap_claim_ephemeral(Timeout *timeout,
const void *ptr,
const void *ptr2 __if_cxx(= nullptr));

__attribute__((deprecated("heap_claim_fast was a bad name. This function has "
"been renamed heap_claim_ephemeral")))
__always_inline static int
heap_claim_fast(Timeout *timeout,
const void *ptr,
const void *ptr2 __if_cxx(= nullptr))
{
return heap_claim_ephemeral(timeout, ptr, ptr2);
}

/**
* Free a heap allocation.
Expand Down Expand Up @@ -341,7 +352,7 @@ static inline void *calloc(size_t nmemb, size_t size)
{
Timeout t = {0, MALLOC_WAIT_TICKS};
void *ptr = heap_allocate_array(
&t, MALLOC_CAPABILITY, nmemb, size, AllocateWaitRevocationNeeded);
&t, MALLOC_CAPABILITY, nmemb, size, AllocateWaitRevocationNeeded);
if (!__builtin_cheri_tag_get(ptr))
{
ptr = NULL;
Expand Down
2 changes: 1 addition & 1 deletion sdk/lib/compartment_helpers/claim_fast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <stdlib.h>
#include <switcher.h>

int heap_claim_fast(Timeout *timeout, const void *ptr, const void *ptr2)
int heap_claim_ephemeral(Timeout *timeout, const void *ptr, const void *ptr2)
{
void **hazards = switcher_thread_hazard_slots();
auto *epochCounter{const_cast<
Expand Down
2 changes: 1 addition & 1 deletion tests/allocator-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ namespace
static cheriot::atomic<int> state = 0;
async([=]() {
Timeout t{1};
int claimed = heap_claim_fast(&t, ptr, ptr2);
int claimed = heap_claim_ephemeral(&t, ptr, ptr2);
TEST(claimed == 0, "Heap claim failed: {}", claimed);
state = 1;
while (state.load() == 1) {}
Expand Down

0 comments on commit 37b94cf

Please sign in to comment.