cherry pick guarded sample rate fix

This commit is contained in:
daanx 2025-05-13 15:58:45 -07:00 committed by Daan
parent 8c9f39fde1
commit a479aaba94
6 changed files with 15 additions and 14 deletions

View file

@ -1,6 +1,6 @@
set(mi_version_major 3)
set(mi_version_minor 0)
set(mi_version_patch 7)
set(mi_version_patch 8)
set(mi_version ${mi_version_major}.${mi_version_minor})
set(PACKAGE_VERSION ${mi_version})

View file

@ -8,7 +8,7 @@ terms of the MIT license. A copy of the license can be found in the file
#ifndef MIMALLOC_H
#define MIMALLOC_H
#define MI_MALLOC_VERSION 307 // major + 2 digits minor
#define MI_MALLOC_VERSION 308 // major + 2 digits minor
// ------------------------------------------------------
// Compiler specific attributes

View file

@ -446,7 +446,6 @@ struct mi_heap_s {
size_t guarded_size_min; // minimal size for guarded objects
size_t guarded_size_max; // maximal size for guarded objects
size_t guarded_sample_rate; // sample rate (set to 0 to disable guarded pages)
size_t guarded_sample_seed; // starting sample count
size_t guarded_sample_count; // current sample count (counting down to 0)
#endif
mi_page_t* pages_free_direct[MI_PAGES_DIRECT]; // optimize: array where every entry points a page with possibly free blocks in the corresponding queue for that size.

View file

@ -622,7 +622,10 @@ static void* mi_block_ptr_set_guarded(mi_block_t* block, size_t obj_size) {
mi_assert_internal(_mi_is_aligned(block, os_page_size));
mi_assert_internal(_mi_is_aligned(guard_page, os_page_size));
if (!page->memid.is_pinned && _mi_is_aligned(guard_page, os_page_size)) {
_mi_os_protect(guard_page, os_page_size);
const bool ok = _mi_os_protect(guard_page, os_page_size);
if (!ok) {
_mi_warning_message("failed to set a guard page behind object (object %p of size %zu)\n", block, block_size);
}
}
else {
_mi_warning_message("unable to set a guard page behind an object due to pinned memory (large OS pages?) (object %p of size %zu)\n", block, block_size);

View file

@ -130,7 +130,7 @@ mi_decl_cache_align const mi_heap_t _mi_heap_empty = {
true, // can eager abandon
0, // tag
#if MI_GUARDED
0, 0, 0, 0, 1, // count is 1 so we never write to it (see `internal.h:mi_heap_malloc_use_guarded`)
0, 0, 0, 1, // count is 1 so we never write to it (see `internal.h:mi_heap_malloc_use_guarded`)
#endif
MI_SMALL_PAGES_EMPTY,
MI_PAGE_QUEUES_EMPTY,
@ -167,7 +167,7 @@ mi_decl_cache_align mi_heap_t heap_main = {
true, // allow page abandon
0, // tag
#if MI_GUARDED
0, 0, 0, 0, 0,
0, 0, 0, 0,
#endif
MI_SMALL_PAGES_EMPTY,
MI_PAGE_QUEUES_EMPTY,
@ -189,15 +189,14 @@ mi_stats_t _mi_stats_main = { MI_STAT_VERSION, MI_STATS_NULL };
#if MI_GUARDED
mi_decl_export void mi_heap_guarded_set_sample_rate(mi_heap_t* heap, size_t sample_rate, size_t seed) {
heap->guarded_sample_seed = seed;
if (heap->guarded_sample_seed == 0) {
heap->guarded_sample_seed = _mi_heap_random_next(heap);
}
heap->guarded_sample_rate = sample_rate;
if (heap->guarded_sample_rate >= 1) {
heap->guarded_sample_seed = heap->guarded_sample_seed % heap->guarded_sample_rate;
heap->guarded_sample_count = sample_rate; // count down samples
if (heap->guarded_sample_rate > 1) {
if (seed == 0) {
seed = _mi_heap_random_next(heap);
}
heap->guarded_sample_count = (seed % heap->guarded_sample_rate) + 1; // start at random count between 1 and `sample_rate`
}
heap->guarded_sample_count = 1 + heap->guarded_sample_seed; // count down samples
}
mi_decl_export void mi_heap_guarded_set_size_bound(mi_heap_t* heap, size_t min, size_t max) {

View file

@ -43,7 +43,7 @@ int main() {
// corrupt_free();
// block_overflow1();
// block_overflow2();
// test_canary_leak();
test_canary_leak();
// test_aslr();
// invalid_free();
// test_reserved();