Yep malloc
being called before main
was indeed the problem. Thanks @IgorTandetnik and @Mike for the detailed clarification.
To fix the issue I'm currently doing this. Its dirty but seems to work. I'll have to clean this later.
1.h
#define end 0x10007fff7fff
#define start 0x02008fff7000
#define MAPFLAGS (MAP_PRIVATE | MAP_FIXED | MAP_ANON | MAP_NORESERVE)
extern bool ismapped;
void internal_map(void);
1.cpp
#include <cstdint>
#include <sys/mman.h>
#include "1.h"
void internal_map() {
if (!ismapped) {
void *ret;
uintptr_t size = end - start + 1;
ret = mmap((void*) start, size, PROT_READ | PROT_WRITE, MAPFLAGS, -1, 0);
(MAP_FAILED == ret? ismapped = false: ismapped = true);
}
else
ismapped = true;
}
extern "C" {
void map()
{
if (ismapped)
return;
internal_map();
}
}
2.cpp
#include <cstdio>
#include <dlfcn.h>
#include "1.h"
extern "C" void map(void);
bool ismapped = false;
void* (*libc_malloc)(size_t size) = NULL;
extern "C" void* malloc(size_t usize)
{
void *ret;
if (!libc_malloc)
libc_malloc = (void*(*)(size_t)) dlsym(RTLD_NEXT, "malloc");
ret = libc_malloc(usize);
if (!ismapped) {
internal_map();
}
else {
void* ptr = (void*) 0x8003fffb000;
*(char*)ptr = 0xab;
}
return ret;
}
Basically, checking if the mapping is successful or not each time malloc is called.