Skip to content

Commit

Permalink
Merge pull request #3027 from MirServer/tidy_up
Browse files Browse the repository at this point in the history
Simplify mmap interception
  • Loading branch information
RAOF authored Aug 31, 2023
2 parents 1e2ae87 + da806f6 commit 82c6c01
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions tests/mir_test_framework/mmap_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,26 @@ mtf::MmapHandlerHandle mtf::add_mmap_handler(MmapHandler handler)
return MmapInterposer::add(std::move(handler));
}

auto real_mmap_symbol_name() -> char const*
{
#if _FILE_OFFSET_BITS == 64
// mmap64 is defined everywhere, so even though off_t == off64_t on 64-bit platforms
// this is still appropriate.
return "mmap64";
#else
// This will get us the 32-bit off_t version on 32-bit platforms
return "mmap";
#endif
}

void* mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
{
if (auto val = MmapInterposer::run(addr, length, prot, flags, fd, offset))
{
return *val;
}

void* (*real_mmap)(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
*(void **)(&real_mmap) = dlsym(RTLD_NEXT, real_mmap_symbol_name());
static void* (*real_mmap)(void *addr, size_t length, int prot, int flags, int fd, off_t offset);

// Where mmap64 is defined, even if off_t == off64_t on 64-bit platforms, this is still appropriate.
if (!real_mmap)
{
*(void **)(&real_mmap) = dlsym(RTLD_NEXT, "mmap64");
}

#if _FILE_OFFSET_BITS == 64
// Empirically, mmap64 is NOT defined everywhere, but on 64-bit platforms this is appropriate
// Empirically, mmap64 is NOT defined everywhere, but then this is appropriate
if (!real_mmap)
{
*(void **)(&real_mmap) = dlsym(RTLD_NEXT, "mmap");
*(void **)(&real_mmap) = dlsym(RTLD_NEXT, __func__);
}
#endif

if (!real_mmap)
{
Expand Down

0 comments on commit 82c6c01

Please sign in to comment.