bool safemem_init() { char buf[16] = "/tmp/XXXXXX"; int wmfd = mkstemp(buf); if(wmfd == -1) { return false; } mfd = open(buf, O_RDONLY); if(mfd == -1) { return false; } if(unlink(buf) == -1) { return false; } if(ftruncate(wmfd, MAPPING_SIZE) == -1) { return false; } void* base_addr = mmap(NULL, MAPPING_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, wmfd, 0); if(base_addr == (void*)-1) { return false; } close(wmfd); safe_mem_allocator.reset(reinterpret_cast<char*>(base_addr), MAPPING_SIZE); return true; }
bool safemem_map_unwritable() { if(munmap(const_cast<void*>(reinterpret_cast<const void*>( safe_mem_allocator.address())), safe_mem_allocator.size())) { return false; } safe_mem_allocator.reset(NULL, 0); return true; }