mirror of
				https://github.com/GrapheneOS/hardened_malloc.git
				synced 2025-11-04 09:46:32 +01:00 
			
		
		
		
	randomize free slabs reuse
This commit is contained in:
		
							parent
							
								
									83df37436d
								
							
						
					
					
						commit
						65a7014b48
					
				
					 2 changed files with 16 additions and 5 deletions
				
			
		
							
								
								
									
										1
									
								
								config.h
									
										
									
									
									
								
							
							
						
						
									
										1
									
								
								config.h
									
										
									
									
									
								
							| 
						 | 
					@ -12,5 +12,6 @@
 | 
				
			||||||
#define REGION_QUARANTINE_RANDOM_SIZE 128
 | 
					#define REGION_QUARANTINE_RANDOM_SIZE 128
 | 
				
			||||||
#define REGION_QUARANTINE_QUEUE_SIZE 1024
 | 
					#define REGION_QUARANTINE_QUEUE_SIZE 1024
 | 
				
			||||||
#define REGION_QUARANTINE_SKIP_THRESHOLD (32 * 1024 * 1024)
 | 
					#define REGION_QUARANTINE_SKIP_THRESHOLD (32 * 1024 * 1024)
 | 
				
			||||||
 | 
					#define FREE_SLABS_QUARANTINE_RANDOM_SIZE 32
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										20
									
								
								malloc.c
									
										
									
									
									
								
							
							
						
						
									
										20
									
								
								malloc.c
									
										
									
									
									
								
							| 
						 | 
					@ -144,6 +144,7 @@ static struct size_class {
 | 
				
			||||||
    // FIFO singly-linked list
 | 
					    // FIFO singly-linked list
 | 
				
			||||||
    struct slab_metadata *free_slabs_head;
 | 
					    struct slab_metadata *free_slabs_head;
 | 
				
			||||||
    struct slab_metadata *free_slabs_tail;
 | 
					    struct slab_metadata *free_slabs_tail;
 | 
				
			||||||
 | 
					    struct slab_metadata *free_slabs_quarantine[FREE_SLABS_QUARANTINE_RANDOM_SIZE];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    struct libdivide_u32_t size_divisor;
 | 
					    struct libdivide_u32_t size_divisor;
 | 
				
			||||||
    struct libdivide_u64_t slab_size_divisor;
 | 
					    struct libdivide_u64_t slab_size_divisor;
 | 
				
			||||||
| 
						 | 
					@ -425,12 +426,21 @@ static size_t slab_usable_size(void *p) {
 | 
				
			||||||
static void enqueue_free_slab(struct size_class *c, struct slab_metadata *metadata) {
 | 
					static void enqueue_free_slab(struct size_class *c, struct slab_metadata *metadata) {
 | 
				
			||||||
    metadata->next = NULL;
 | 
					    metadata->next = NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (c->free_slabs_tail != NULL) {
 | 
					    static_assert(FREE_SLABS_QUARANTINE_RANDOM_SIZE < (u16)-1, "free slabs quarantine too large");
 | 
				
			||||||
        c->free_slabs_tail->next = metadata;
 | 
					    size_t index = get_random_u16_uniform(&c->rng, FREE_SLABS_QUARANTINE_RANDOM_SIZE);
 | 
				
			||||||
    } else {
 | 
					    struct slab_metadata *substitute = c->free_slabs_quarantine[index];
 | 
				
			||||||
        c->free_slabs_head = metadata;
 | 
					    c->free_slabs_quarantine[index] = metadata;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (substitute == NULL) {
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    c->free_slabs_tail = metadata;
 | 
					
 | 
				
			||||||
 | 
					    if (c->free_slabs_tail != NULL) {
 | 
				
			||||||
 | 
					        c->free_slabs_tail->next = substitute;
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					        c->free_slabs_head = substitute;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    c->free_slabs_tail = substitute;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static inline void deallocate_small(void *p, size_t *expected_size) {
 | 
					static inline void deallocate_small(void *p, size_t *expected_size) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue