6#include <__cheri_sealed.h>
19static const size_t CHERIOTHeapMinChunkSize = 16;
26# define MALLOC_QUOTA 4096
52#define DECLARE_ALLOCATOR_CAPABILITY(name) \
53 DECLARE_STATIC_SEALED_VALUE( \
54 struct AllocatorCapabilityState, allocator, MallocKey, name);
60#define DEFINE_ALLOCATOR_CAPABILITY(name, quota) \
61 DEFINE_STATIC_SEALED_VALUE(struct AllocatorCapabilityState, \
73#define DECLARE_AND_DEFINE_ALLOCATOR_CAPABILITY(name, quota) \
74 DECLARE_ALLOCATOR_CAPABILITY(name); \
75 DEFINE_ALLOCATOR_CAPABILITY(name, quota)
77#ifndef CHERIOT_NO_AMBIENT_MALLOC
83DECLARE_ALLOCATOR_CAPABILITY(__default_malloc_capability)
84# ifndef CHERIOT_CUSTOM_DEFAULT_MALLOC_CAPABILITY
90DEFINE_ALLOCATOR_CAPABILITY(__default_malloc_capability, MALLOC_QUOTA)
96# define MALLOC_CAPABILITY STATIC_SEALED_VALUE(__default_malloc_capability)
99#ifndef MALLOC_WAIT_TICKS
107# define MALLOC_WAIT_TICKS 30
111static inline void __dead2 panic()
116 __asm
volatile(
"unimp");
120enum [[clang::flag_enum]] AllocateWaitFlags
126 AllocateWaitNone = 0,
131 AllocateWaitRevocationNeeded = (1 << 0),
136 AllocateWaitQuotaExceeded = (1 << 1),
141 AllocateWaitHeapFull = (1 << 2),
145 AllocateWaitAny = (AllocateWaitRevocationNeeded |
146 AllocateWaitQuotaExceeded | AllocateWaitHeapFull),
190void *__cheri_compartment(
"allocator")
191 heap_allocate(TimeoutArgument timeout,
192 AllocatorCapability heapCapability,
194 uint32_t flags __if_cxx(= AllocateWaitAny));
217void *__cheri_compartment(
"allocator")
218 heap_allocate_array(TimeoutArgument timeout,
219 AllocatorCapability heapCapability,
222 uint32_t flags __if_cxx(= AllocateWaitAny));
253int __cheri_compartment(
"allocator")
254 heap_free(AllocatorCapability heapCapability,
void *ptr);
266__if_c(static) inline _Bool heap_address_is_valid(const
void *
object)
268 ptraddr_t heap_start = LA_ABS(__export_mem_heap);
269 ptraddr_t heap_end = LA_ABS(__export_mem_heap_end);
275 ptraddr_t address = __builtin_cheri_base_get(
object);
276 return (address >= heap_start) && (address < heap_end);
279static inline void __dead2 abort()
284#ifndef CHERIOT_NO_AMBIENT_MALLOC
285static inline void *malloc(
size_t size)
287 Timeout t = {0, MALLOC_WAIT_TICKS};
289 heap_allocate(&t, MALLOC_CAPABILITY, size, AllocateWaitRevocationNeeded);
290 if (!__builtin_cheri_tag_get(ptr))
296static inline void *calloc(
size_t nmemb,
size_t size)
298 Timeout t = {0, MALLOC_WAIT_TICKS};
299 void *ptr = heap_allocate_array(
300 &t, MALLOC_CAPABILITY, nmemb, size, AllocateWaitRevocationNeeded);
301 if (!__builtin_cheri_tag_get(ptr))
307static inline int free(
void *ptr)
309 return heap_free(MALLOC_CAPABILITY, ptr);
313static inline void yield(
void)
315 __asm
volatile(
"ecall" :::
"memory");
328long __cheri_libcall strtol(
const char *nptr,
char **endptr,
int base);
340unsigned long __cheri_libcall strtoul(
const char *nptr,
Macros for interacting with the compartmentalisation model in CHERIoT.
The public view of state represented by a capability that is used to authorise heap allocations.
uintptr_t reserved[2]
Reserved space for internal use.
size_t unused
Reserved space for internal use.
size_t quota
The number of bytes that the capability will permit to be allocated.
Structure representing a timeout.
This file contains the types used for timeouts across scheduler APIs.