Skip to content

Commit 5c5639b

Browse files
committed
Trace program break to not allocate memory over there (backported from box64)
1 parent ee0c949 commit 5c5639b

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/box86version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44
#define BOX86_MAJOR 0
55
#define BOX86_MINOR 3
6-
#define BOX86_REVISION 8
6+
#define BOX86_REVISION 9
77

88
#endif //__BOX86_VERSION_H_

src/custommem.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,9 @@ void allocProtection(uintptr_t addr, size_t size, uint32_t prot)
999999
// don't need to add precise tracking probably
10001000
}
10011001

1002+
uintptr_t pbrk = 0;
1003+
uintptr_t old_brk = 0;
1004+
uintptr_t* cur_brk = NULL;
10021005
void loadProtectionFromMap()
10031006
{
10041007
if(box86_mapclean)
@@ -1015,8 +1018,14 @@ void loadProtectionFromMap()
10151018
if(sscanf(buf, "%x-%x %c%c%c", &s, &e, &r, &w, &x)==5) {
10161019
int prot = ((r=='r')?PROT_READ:0)|((w=='w')?PROT_WRITE:0)|((x=='x')?PROT_EXEC:0);
10171020
allocProtection(s, e-s, prot);
1021+
if(!pbrk && strstr(buf, "[heap]"))
1022+
pbrk = s;
10181023
}
10191024
}
1025+
if(!pbrk) {
1026+
printf_log(LOG_INFO, "BOX86: Warning, program break not found\n");
1027+
if(cur_brk) pbrk = *cur_brk; // approximate is better than nothing
1028+
}
10201029
fclose(f);
10211030
box86_mapclean = 1;
10221031
}
@@ -1051,6 +1060,11 @@ int getMmapped(uintptr_t addr)
10511060
#define MEDIAN (void*)0x40000000
10521061
static void* findBlockHinted(void* hint, size_t size, uintptr_t mask)
10531062
{
1063+
// first, check if program break as changed
1064+
if(pbrk && cur_brk && *cur_brk!=old_brk) {
1065+
old_brk = *cur_brk;
1066+
setProtection(pbrk, old_brk-pbrk, PROT_READ|PROT_WRITE);
1067+
}
10541068
int prot;
10551069
if(hint<LOWEST) hint = LOWEST;
10561070
uintptr_t bend = 0;
@@ -1165,6 +1179,7 @@ void init_custommem_helper(box86context_t* ctx)
11651179
if(inited) // already initialized
11661180
return;
11671181
inited = 1;
1182+
cur_brk = dlsym(RTLD_NEXT, "__curbrk");
11681183
memprot = init_rbtree();
11691184
init_mutexes();
11701185
#ifdef DYNAREC

0 commit comments

Comments
 (0)