Max OS 0.3
Loading...
Searching...
No Matches
kasan.cpp File Reference

Implementation of the Kernel Address Sanitizer (KASan) handler. More...

#include <runtime/kasan.h>
#include <common/logger.h>

Go to the source code of this file.

Functions

void __asan_load1_noabort (uintptr_t address)
 Triggered when a memory load access violation of 1 byte is detected.
 
void __asan_load2_noabort (uintptr_t address)
 Triggered when a memory load access violation of 2 bytes is detected.
 
void __asan_load4_noabort (uintptr_t address)
 Triggered when a memory load access violation of 4 bytes is detected.
 
void __asan_load8_noabort (uintptr_t address)
 Triggered when a memory load access violation of 8 bytes is detected.
 
void __asan_load16_noabort (uintptr_t address)
 Triggered when a memory load access violation of 16 bytes is detected.
 
void __asan_loadN_noabort (uintptr_t address, size_t size)
 Triggered when a memory load access violation of arbitrary size is detected.
 
void __asan_store1_noabort (uintptr_t address)
 Triggered when a memory store access violation of 1 byte is detected.
 
void __asan_store2_noabort (uintptr_t address)
 Triggered when a memory store access violation of 2 bytes is detected.
 
void __asan_store4_noabort (uintptr_t address)
 Triggered when a memory store access violation of 4 bytes is detected.
 
void __asan_store8_noabort (uintptr_t address)
 Triggered when a memory store access violation of 8 bytes is detected.
 
void __asan_store16_noabort (uintptr_t address)
 Triggered when a memory store access violation of 16 bytes is detected.
 
void __asan_storeN_noabort (uintptr_t address, size_t size)
 Triggered when a memory store access violation of arbitrary size is detected.
 
void __asan_handle_no_return ()
 Triggered when a function that is expected to not return does return.
 
void __asan_alloca_poison (uintptr_t address, size_t size)
 Poisons a region of stack memory allocated with alloca.
 
void __asan_alloca_unpoison (void *stack_top, void *stack_bottom)
 Unpoisons a region of stack memory allocated with alloca.
 
void __asan_set_shadow_00 (void *pointer, size_t size)
 Unpoisons a region of memory, marking it as accessible.
 
void __asan_set_shadow_f8 (void *pointer, size_t size)
 Poisons a region of memory, marking it as inaccessible.
 
void __asan_before_dynamic_init (const void *module_name)
 Called before dynamic initialization of a module.
 
void __asan_after_dynamic_init (const void *module_name)
 Called after dynamic initialization of a module.
 

Detailed Description

Implementation of the Kernel Address Sanitizer (KASan) handler.

Date
22nd November 2025
Author
Max Tyson

Definition in file kasan.cpp.

Function Documentation

◆ __asan_after_dynamic_init()

void __asan_after_dynamic_init ( const void *  module_name)

Called after dynamic initialization of a module.

Parameters
module_nameThe name of the module that was initialized

Definition at line 239 of file kasan.cpp.

239 {
240 // Nothing to do
241 }

◆ __asan_alloca_poison()

void __asan_alloca_poison ( uintptr_t  address,
size_t  size 
)

Poisons a region of stack memory allocated with alloca.

Parameters
addressThe starting address of the allocated memory
sizeThe size of the allocated memory in bytes

Definition at line 181 of file kasan.cpp.

181 {
182 //TODO: Implement stack poisoning
183 ASSERT(false, "KASAN ERROR: Stack poisoning not implemented for address 0x%lx of size %zu", address, size);
184}
#define ASSERT(condition, format,...)
If the specified condition is not met then the kernel will crash with the specified message.
Definition logger.h:100

References ASSERT.

◆ __asan_alloca_unpoison()

void __asan_alloca_unpoison ( void *  stack_top,
void *  stack_bottom 
)

Unpoisons a region of stack memory allocated with alloca.

Parameters
stack_topThe top address of the allocated memory
stack_bottomThe bottom address of the allocated memory

Definition at line 192 of file kasan.cpp.

192 {
193 //TODO: Implement stack unpoisoning
194 ASSERT(false, "KASAN ERROR: Stack unpoisoning not implemented for range 0x%p - 0x%p", stack_bottom, stack_top);
195}

References ASSERT.

◆ __asan_before_dynamic_init()

void __asan_before_dynamic_init ( const void *  module_name)

Called before dynamic initialization of a module.

Parameters
module_nameThe name of the module being initialized

Definition at line 230 of file kasan.cpp.

230 {
231 // Nothing to do
232}

◆ __asan_handle_no_return()

void __asan_handle_no_return ( )

Triggered when a function that is expected to not return does return.

Definition at line 171 of file kasan.cpp.

171 {
172 ASSERT(false, "KASAN ERROR: Function marked 'noreturn' has returned");
173}

References ASSERT.

◆ __asan_load16_noabort()

void __asan_load16_noabort ( uintptr_t  address)

Triggered when a memory load access violation of 16 bytes is detected.

Parameters
addressThe address that was accessed

Definition at line 99 of file kasan.cpp.

99 {
100 KASanHandler::handle(false, address, 16, __builtin_return_address(0));
101}
static void handle(bool write, uintptr_t address, size_t size, void *rip)
Handles a KASan error by panicking and printing the details.
Definition kasan.cpp:27

References MaxOS::runtime::KASanHandler::handle().

◆ __asan_load1_noabort()

void __asan_load1_noabort ( uintptr_t  address)

Triggered when a memory load access violation of 1 byte is detected.

Parameters
addressThe address that was accessed

Definition at line 63 of file kasan.cpp.

63 {
64 KASanHandler::handle(false, address, 1, __builtin_return_address(0));
65}

References MaxOS::runtime::KASanHandler::handle().

◆ __asan_load2_noabort()

void __asan_load2_noabort ( uintptr_t  address)

Triggered when a memory load access violation of 2 bytes is detected.

Parameters
addressThe address that was accessed

Definition at line 72 of file kasan.cpp.

72 {
73 KASanHandler::handle(false, address, 2, __builtin_return_address(0));
74}

References MaxOS::runtime::KASanHandler::handle().

◆ __asan_load4_noabort()

void __asan_load4_noabort ( uintptr_t  address)

Triggered when a memory load access violation of 4 bytes is detected.

Parameters
addressThe address that was accessed

Definition at line 81 of file kasan.cpp.

81 {
82 KASanHandler::handle(false, address, 4, __builtin_return_address(0));
83}

References MaxOS::runtime::KASanHandler::handle().

◆ __asan_load8_noabort()

void __asan_load8_noabort ( uintptr_t  address)

Triggered when a memory load access violation of 8 bytes is detected.

Parameters
addressThe address that was accessed

Definition at line 90 of file kasan.cpp.

90 {
91 KASanHandler::handle(false, address, 8, __builtin_return_address(0));
92}

References MaxOS::runtime::KASanHandler::handle().

◆ __asan_loadN_noabort()

void __asan_loadN_noabort ( uintptr_t  address,
size_t  size 
)

Triggered when a memory load access violation of arbitrary size is detected.

Parameters
addressThe address that was accessed
sizeThe size of the access in bytes

Definition at line 109 of file kasan.cpp.

109 {
110 KASanHandler::handle(false, address, size, __builtin_return_address(0));
111}

References MaxOS::runtime::KASanHandler::handle().

◆ __asan_set_shadow_00()

void __asan_set_shadow_00 ( void *  pointer,
size_t  size 
)

Unpoisons a region of memory, marking it as accessible.

Parameters
pointerThe starting address of the memory
sizeThe size of the memory region in bytes

Definition at line 203 of file kasan.cpp.

203 {
204
205 // Clear the memory to mark it as unpoisoned
206 for(int i = 0; i < size; ++i)
207 ((int8_t*)pointer)[i] = 0;
208
209}

◆ __asan_set_shadow_f8()

void __asan_set_shadow_f8 ( void *  pointer,
size_t  size 
)

Poisons a region of memory, marking it as inaccessible.

Parameters
pointerThe starting address of the memory
sizeThe size of the memory region in bytes

Definition at line 217 of file kasan.cpp.

217 {
218
219 // Fill the memory to mark it as poisoned
220 for(int i = 0; i < size; ++i)
221 ((int8_t*)pointer)[i] = 0xF8;
222
223}

◆ __asan_store16_noabort()

void __asan_store16_noabort ( uintptr_t  address)

Triggered when a memory store access violation of 16 bytes is detected.

Parameters
addressThe address that was accessed

Definition at line 154 of file kasan.cpp.

154 {
155 KASanHandler::handle(true, address, 16, __builtin_return_address(0));
156}

References MaxOS::runtime::KASanHandler::handle().

◆ __asan_store1_noabort()

void __asan_store1_noabort ( uintptr_t  address)

Triggered when a memory store access violation of 1 byte is detected.

Parameters
addressThe address that was accessed

Definition at line 118 of file kasan.cpp.

118 {
119 KASanHandler::handle(true, address, 1, __builtin_return_address(0));
120}

References MaxOS::runtime::KASanHandler::handle().

◆ __asan_store2_noabort()

void __asan_store2_noabort ( uintptr_t  address)

Triggered when a memory store access violation of 2 bytes is detected.

Parameters
addressThe address that was accessed

Definition at line 127 of file kasan.cpp.

127 {
128 KASanHandler::handle(true, address, 2, __builtin_return_address(0));
129}

References MaxOS::runtime::KASanHandler::handle().

◆ __asan_store4_noabort()

void __asan_store4_noabort ( uintptr_t  address)

Triggered when a memory store access violation of 4 bytes is detected.

Parameters
addressThe address that was accessed

Definition at line 136 of file kasan.cpp.

136 {
137 KASanHandler::handle(true, address, 4, __builtin_return_address(0));
138}

References MaxOS::runtime::KASanHandler::handle().

◆ __asan_store8_noabort()

void __asan_store8_noabort ( uintptr_t  address)

Triggered when a memory store access violation of 8 bytes is detected.

Parameters
addressThe address that was accessed

Definition at line 145 of file kasan.cpp.

145 {
146 KASanHandler::handle(true, address, 8, __builtin_return_address(0));
147}

References MaxOS::runtime::KASanHandler::handle().

◆ __asan_storeN_noabort()

void __asan_storeN_noabort ( uintptr_t  address,
size_t  size 
)

Triggered when a memory store access violation of arbitrary size is detected.

Parameters
addressThe address that was accessed
sizeThe size of the access in bytes

Definition at line 164 of file kasan.cpp.

164 {
165 KASanHandler::handle(false, address, size, __builtin_return_address(0));
166}

References MaxOS::runtime::KASanHandler::handle().