microsoft / microsoft/mimalloc

mi_assert_internal(start <= pstart && (pstart + size) <= end) in mi_segment_commit_mask function is right?

Open
#688 2 comments 0 reactions 0 assignees View on GitHub
stale
Dominant language
C
Stars
13.4k
Forks
1.2k
Avg merge
4d 45m
Merged PRs (30d)
13

Description

when decommit, conservative is true, pstart will been up aligned, so start maybe greater then pstart?
```
static void mi_segment_commit_mask(mi_segment_t* segment, bool conservative, uint8_t* p, size_t size, uint8_t** start_p, size_t* full_size, mi_commit_mask_t* cm) {
mi_assert_internal(_mi_ptr_segment(p + 1) == segment);
mi_assert_internal(segment->kind != MI_SEGMENT_HUGE);
mi_commit_mask_create_empty(cm);
if (size == 0 || size > MI_SEGMENT_SIZE || segment->kind == MI_SEGMENT_HUGE) return;
const size_t segstart = mi_segment_info_size(segment);
const size_t segsize = mi_segment_size(segment);
if (p >= (uint8_t*)segment + segsize) return;

size_t pstart = (p - (uint8_t*)segment);
mi_assert_internal(pstart + size <= segsize);

size_t start;
size_t end;
if (conservative) {
// decommit conservative
start = _mi_align_up(pstart, MI_COMMIT_SIZE);
end = _mi_align_down(pstart + size, MI_COMMIT_SIZE);
mi_assert_internal(start >= segstart);
mi_assert_internal(end <= segsize);
}
else {
// commit liberal
start = _mi_align_down(pstart, MI_MINIMAL_COMMIT_SIZE);
end = _mi_align_up(pstart + size, MI_MINIMAL_COMMIT_SIZE);
}
if (pstart >= segstart && start < segstart) { // note: the mask is also calculated for an initial commit of the info area
start = segstart;
}
if (end > segsize) {
end = segsize;
}

mi_assert_internal(start <= pstart && (pstart + size) <= end);
mi_assert_internal(start % MI_COMMIT_SIZE==0 && end % MI_COMMIT_SIZE == 0);
*start_p = (uint8_t*)segment + start;
*full_size = (end > start ? end - start : 0);
if (*full_size == 0) return;

size_t bitidx = start / MI_COMMIT_SIZE;
mi_assert_internal(bitidx < MI_COMMIT_MASK_BITS);

size_t bitcount = *full_size / MI_COMMIT_SIZE; // can be 0
if (bitidx + bitcount > MI_COMMIT_MASK_BITS) {
_mi_warning_message("commit mask overflow: idx=%zu count=%zu start=%zx end=%zx p=0x%p size=%zu fullsize=%zu\n", bitidx, bitcount, start, end, p, size, *full_size);
}
mi_assert_internal((bitidx + bitcount) <= MI_COMMIT_MASK_BITS);
mi_commit_mask_create(bitidx, bitcount, cm);
}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at mi_segment_commit_mask and trace its conservative decommit path, focusing on the alignment of pstart, start, and end. Verify the final assertion against the function's callers and relevant allocator behavior; done means the assertion is shown to be valid or the issue identifies a confirmed correction.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
operating-systems
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.