Skip to content

Commit 72f05e2

Browse files
committed
fix guarded sample rate of 1 (issue #1085)
1 parent a6ecb5c commit 72f05e2

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

include/mimalloc/types.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,6 @@ struct mi_heap_s {
512512
size_t guarded_size_min; // minimal size for guarded objects
513513
size_t guarded_size_max; // maximal size for guarded objects
514514
size_t guarded_sample_rate; // sample rate (set to 0 to disable guarded pages)
515-
size_t guarded_sample_seed; // starting sample count
516515
size_t guarded_sample_count; // current sample count (counting down to 0)
517516
#endif
518517
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.

src/init.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ mi_decl_cache_align const mi_heap_t _mi_heap_empty = {
110110
false, // can reclaim
111111
0, // tag
112112
#if MI_GUARDED
113-
0, 0, 0, 0, 1, // count is 1 so we never write to it (see `internal.h:mi_heap_malloc_use_guarded`)
113+
0, 0, 0, 1, // count is 1 so we never write to it (see `internal.h:mi_heap_malloc_use_guarded`)
114114
#endif
115115
MI_SMALL_PAGES_EMPTY,
116116
MI_PAGE_QUEUES_EMPTY
@@ -153,7 +153,7 @@ mi_decl_cache_align mi_heap_t _mi_heap_main = {
153153
false, // can reclaim
154154
0, // tag
155155
#if MI_GUARDED
156-
0, 0, 0, 0, 0,
156+
0, 0, 0, 0,
157157
#endif
158158
MI_SMALL_PAGES_EMPTY,
159159
MI_PAGE_QUEUES_EMPTY
@@ -165,15 +165,14 @@ mi_stats_t _mi_stats_main = { MI_STAT_VERSION, MI_STATS_NULL };
165165

166166
#if MI_GUARDED
167167
mi_decl_export void mi_heap_guarded_set_sample_rate(mi_heap_t* heap, size_t sample_rate, size_t seed) {
168-
heap->guarded_sample_seed = seed;
169-
if (heap->guarded_sample_seed == 0) {
170-
heap->guarded_sample_seed = _mi_heap_random_next(heap);
171-
}
172168
heap->guarded_sample_rate = sample_rate;
173-
if (heap->guarded_sample_rate >= 1) {
174-
heap->guarded_sample_seed = heap->guarded_sample_seed % heap->guarded_sample_rate;
169+
heap->guarded_sample_count = sample_rate; // count down samples
170+
if (heap->guarded_sample_rate > 1) {
171+
if (seed == 0) {
172+
seed = _mi_heap_random_next(heap);
173+
}
174+
heap->guarded_sample_count = (seed % heap->guarded_sample_rate) + 1; // start at random count between 1 and `sample_rate`
175175
}
176-
heap->guarded_sample_count = heap->guarded_sample_seed; // count down samples
177176
}
178177

179178
mi_decl_export void mi_heap_guarded_set_size_bound(mi_heap_t* heap, size_t min, size_t max) {

test/main-override-static.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ int main() {
4343
// corrupt_free();
4444
// block_overflow1();
4545
// block_overflow2();
46-
// test_canary_leak();
46+
test_canary_leak();
4747
// test_aslr();
4848
// invalid_free();
4949
// test_reserved();

0 commit comments

Comments
 (0)