#ifndef ANT_CAGE_H #define ANT_CAGE_H #include #include #include #ifdef ANT_WASM_EMBED static constexpr size_t ANT_CAGE_MAX_SIZE = 1UL << 30; static constexpr size_t ANT_CAGE_MIN_SIZE = 1UL << 24; #else static constexpr size_t ANT_CAGE_MAX_SIZE = 1ULL << 35; static constexpr size_t ANT_CAGE_MIN_SIZE = 1ULL << 33; static constexpr size_t ANT_CAGE_ALIGNMENT = 1ULL << 23; #endif extern uintptr_t ant_cage_base_address; extern size_t ant_cage_reserved_size; void *ant_cage_alloc(size_t size, size_t align); void *ant_cage_reserve(size_t size, size_t align, size_t keep); void ant_cage_free(void *ptr, size_t size); bool ant_cage_set_size(size_t size); _Noreturn void ant_cage_reject_pointer(const void *ptr); static inline uintptr_t ant_cage_base(void) { return ant_cage_base_address; } static inline bool ant_cage_contains(const void *ptr) { #ifdef ANT_WASM_EMBED return ptr != NULL; #else uintptr_t value = (uintptr_t)ptr; uintptr_t base = ant_cage_base_address; return base && value >= base && value - base < ant_cage_reserved_size; #endif } static inline uint64_t ant_cage_encode(const void *ptr) { if (!ptr) return 0; #ifdef ANT_WASM_EMBED return (uint64_t)(uintptr_t)ptr; #else if (!ant_cage_contains(ptr)) ant_cage_reject_pointer(ptr); return (uint64_t)((uintptr_t)ptr - ant_cage_base_address); #endif } static inline void *ant_cage_decode(uint64_t offset) { #ifdef ANT_WASM_EMBED return (void *)(uintptr_t)offset; #else return offset ? (void *)(ant_cage_base_address + offset) : NULL; #endif } #endif