Mar*_*tin 8 c c++ boost glibc libc
我试图mmap()在预先标识的fd上替换原始系统调用LD_PRELOAD,以便调用它的进程可以读取由另一个进程先前创建的共享内存对象boost::interprocess.一切顺利,除非我最终尝试读取mmap的内存.在这种情况下,第一个进程以分段错误中止.原因是什么?我不需要对共享内存对象的写权限.
这是预加载库中的代码:
void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset) {
static void* (*o_mmap) ( void *, size_t, int, int, int, off_t ) =
o_mmap = (void*(*)( void *, size_t, int, int, int, off_t )) dlsym(RTLD_NEXT, "mmap");
if (!o_mmap)
std::cout << "mmap() preload failed\n";
if (fd != my_fd)
return (*o_mmap)( start, length, prot, flags, fd, offset );
interprocess::shared_memory_object shm (interprocess::open_only, "obj", interprocess::read_only);
interprocess::mapped_region region(shm, interprocess::read_only, 0, length, start);
std::cout << "mmap() overridden. addr =" << region.get_address() << " length: " << region.get_size() << " start: " << start << "\n";
return region.get_address();
}
Run Code Online (Sandbox Code Playgroud)
创建共享内存对象的程序代码是:
//Create a shared memory object.
shared_memory_object shm (create_only, "obj", read_write);
//Set size
shm.truncate(1000);
//Map the whole shared memory in this process
mapped_region region(shm, read_write);
//Write all the memory to 1
std::memset(region.get_address(), 1, region.get_size());
Run Code Online (Sandbox Code Playgroud)
试图读取上面共享内存的程序代码(段错误)是:
int fd = open(my_file, O_RDONLY);
void* addr = mmap(0, 1000, PROT_READ, MAP_SHARED, fd, 0); // Okay
//Check that memory was initialized to 1
char *mem = static_cast<char*>(addr);
for(std::size_t i = 0; i < 1000; ++i)
if(*mem++ != 1) // SEGFAULT!
return 1; //Error checking memory
Run Code Online (Sandbox Code Playgroud)
您的问题是您有效地返回对本地的引用,但是以一种稍微混淆的方式.你的mmap()覆盖有一个interprocess::shared_memory_object和interprocess::mapped_region堆栈上的,当你返回到客户端时会被销毁.在销毁期间,boost包装器将取消映射内存区域,因此在客户端代码中访问它不再有效.作为一个简单的修复,将这些变量设置为静态可以防止seg错误,但是根据应用程序的结构,可能需要更复杂的解决方案.