From ad2d28dbe7f4976b12b613cd01d8db102d9b9f3f Mon Sep 17 00:00:00 2001 From: Alan Griffiths Date: Wed, 30 Aug 2023 14:30:52 +0100 Subject: [PATCH 1/3] Regardless of any preprocessing "magic" done to the `mmap` symbol, we can get the actual name of the function using `__func__`. --- tests/mir_test_framework/mmap_wrapper.cpp | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/tests/mir_test_framework/mmap_wrapper.cpp b/tests/mir_test_framework/mmap_wrapper.cpp index c63591fa7b2..1bc3b30483a 100644 --- a/tests/mir_test_framework/mmap_wrapper.cpp +++ b/tests/mir_test_framework/mmap_wrapper.cpp @@ -39,18 +39,6 @@ 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)) @@ -59,15 +47,7 @@ void* mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) } 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()); - -#if _FILE_OFFSET_BITS == 64 - // Empirically, mmap64 is NOT defined everywhere, but on 64-bit platforms this is appropriate - if (!real_mmap) - { - *(void **)(&real_mmap) = dlsym(RTLD_NEXT, "mmap"); - } -#endif + *(void **)(&real_mmap) = dlsym(RTLD_NEXT, __func__); if (!real_mmap) { From 8f832592da2cfe6563f12a14bc8a107cd51fd365 Mon Sep 17 00:00:00 2001 From: Alan Griffiths Date: Wed, 30 Aug 2023 15:38:48 +0100 Subject: [PATCH 2/3] When we can find mmap64, we use mmap64 --- tests/mir_test_framework/mmap_wrapper.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/mir_test_framework/mmap_wrapper.cpp b/tests/mir_test_framework/mmap_wrapper.cpp index 1bc3b30483a..f28b1ddab4d 100644 --- a/tests/mir_test_framework/mmap_wrapper.cpp +++ b/tests/mir_test_framework/mmap_wrapper.cpp @@ -47,7 +47,15 @@ void* mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) } void* (*real_mmap)(void *addr, size_t length, int prot, int flags, int fd, off_t offset); - *(void **)(&real_mmap) = dlsym(RTLD_NEXT, __func__); + + // Where mmap64 is defined, even if off_t == off64_t on 64-bit platforms, this is still appropriate. + *(void **)(&real_mmap) = dlsym(RTLD_NEXT, "mmap64"); + + // Empirically, mmap64 is NOT defined everywhere, but then this is appropriate + if (!real_mmap) + { + *(void **)(&real_mmap) = dlsym(RTLD_NEXT, __func__); + } if (!real_mmap) { From da806f608f2734ca20790c454b0b6bce4c84abe6 Mon Sep 17 00:00:00 2001 From: Alan Griffiths Date: Wed, 30 Aug 2023 16:37:15 +0100 Subject: [PATCH 3/3] Only resolve the function on the first invocation --- tests/mir_test_framework/mmap_wrapper.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/mir_test_framework/mmap_wrapper.cpp b/tests/mir_test_framework/mmap_wrapper.cpp index f28b1ddab4d..ad3f8672fd6 100644 --- a/tests/mir_test_framework/mmap_wrapper.cpp +++ b/tests/mir_test_framework/mmap_wrapper.cpp @@ -46,10 +46,13 @@ void* mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) return *val; } - void* (*real_mmap)(void *addr, size_t length, int prot, int flags, int fd, off_t offset); + 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. - *(void **)(&real_mmap) = dlsym(RTLD_NEXT, "mmap64"); + if (!real_mmap) + { + *(void **)(&real_mmap) = dlsym(RTLD_NEXT, "mmap64"); + } // Empirically, mmap64 is NOT defined everywhere, but then this is appropriate if (!real_mmap)