1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
Should `NewBrick()` use `setProtection_mmap` to correctly track mmap regions?
In `NewBrick()`, after allocating memory via `box_mmap()`, the code currently uses `setProtection()`:
```c
brick_t* NewBrick(void* old)
{
brick_t* ret = (brick_t*)box_calloc(1, sizeof(brick_t));
static void* load_addr_32bits = NULL;
if(box64_is32bits)
old = load_addr_32bits;
else {
if(old)
old = old + NBRICK * sizeof(onebridge_t);
}
void* ptr = box_mmap(old, NBRICK * sizeof(onebridge_t), PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | ((!box64_is32bits && box64_wine)?0:0x40) | MAP_ANONYMOUS, -1, 0); // 0x40 is MAP_32BIT
if(ptr == MAP_FAILED)
ptr = box_mmap(NULL, NBRICK * sizeof(onebridge_t), PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | ((!box64_is32bits && box64_wine)?0:0x40) | MAP_ANONYMOUS, -1, 0);
if(ptr == MAP_FAILED) {
printf_log(LOG_NONE, "Warning, cannot allocate 0x%lx aligned bytes for bridge, will probably crash later\n", NBRICK*sizeof(onebridge_t));
}
setProtection((uintptr_t)ptr, NBRICK * sizeof(onebridge_t), PROT_READ | PROT_WRITE | PROT_EXEC | PROT_NOPROT);
dynarec_log(LOG_INFO, "New Bridge brick at %p (size 0x%zx)\n", ptr, NBRICK*sizeof(onebridge_t));
if(box64_is32bits) load_addr_32bits = ptr + NBRICK*sizeof(onebridge_t);
ret->b = ptr;
return ret;
}
```
However, `setProtection()` does not label the region as `MEM_MMAP`.
As a result, `getMmapped()` returns 0, even though the region was mmap-allocated.
I've already pushed a [local commit](https://github.com/devarajabc/box64/commit/0a2042a427c3bf6eaffa88124a84930a18126d06) that applies this change.
Before opening a PR, I’d like to confirm: Are there any design considerations that explain why `setProtection()` was originally used instead of `setProtection_mmap()`?
|