Skip to contentSkip to conversation

lib/cobalt: Let __STD() and __RT() of mmap follow the off_t redirection

PATCH In review Jan Kiszka 2 participants 3 messages Nov 24

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

PATCH 1/1 lib/cobalt: Let __STD() and __RT() of mmap follow the off_t redirection

From: Jan Kiszka <[email protected]>

With _FILE_OFFSET_BITS=64, glibc redirects mmap() to mmap64() on 32-bit archs. Applications get this setting by default since y2038 support was added, as xeno-config passes it along with _TIME_BITS=64, and some toolchains even predefine both (e.g. Debian armhf). uClibc-ng enforces 64-bit file offsets unconditionally since release 1.0.58.

__STD(mmap()) and __RT(mmap()) do not follow this redirection: they expand to __real_mmap() and __cobalt_mmap(), declared with the 64-bit off_t of the caller. But with the Cobalt wrappers, __real_mmap() binds to the libc's mmap(), and __cobalt_mmap() is built with native off_t, so both take a 32-bit offset. They read it from the padding word in front of the 64-bit argument and fail with EINVAL or map the wrong page.

Internal users built with 64-bit off_t are affected as well:
cobalt_unmap_umm(), the pshared heap of copperplate and the TLSF pool of boilerplate all bind to the native-off_t __real_mmap() of libcobalt.

Add COBALT_DECL_OFF64(), which redirects the __STD(), __RT() and __WRAP() symbols of a service to its 64-bit variant whenever the libc does so, and use it for mmap(). This relies on libcobalt providing __real_mmap64(). lib/cobalt/mmap.c implements both variants and opts out via __COBALT_NO_OFF64_REDIRECT__: building it with native off_t is not sufficient if the libc enforces 64-bit file offsets.

Fixes fb2bd6e647a9 ("y2038: build: Introduce new build parameter --disable-y2038")
Assisted-by opencode:Opus-5.5
---
 include/cobalt/sys/mman.h |  6 +++---
 include/cobalt/wrappers.h | 15 +++++++++++++++
 lib/cobalt/mmap.c         |  6 ++++++
 3 files changed, 24 insertions(+), 3 deletions(-)
include/cobalt/sys/mman.h +3 -3
index e291f50ef..54c028ad9 100644
@@ -28,9 +28,9 @@
 extern "C" {
 #endif
 
-COBALT_DECL(void *, mmap,
-	    (void *addr, size_t length, int prot, int flags, int fd,
-	     off_t offset));
+COBALT_DECL_OFF64(void *, mmap, mmap64,
+		  (void *addr, size_t length, int prot, int flags, int fd,
+		   off_t offset));
 
 #if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE)
 COBALT_DECL(void *, mmap64,
include/cobalt/wrappers.h +15 -0
index 6d826eedf..9dd022a75 100644
@@ -45,6 +45,21 @@
 #define COBALT_DECL_TIME64(T, FN, FN_64, I) COBALT_DECL(T, FN, I)
 #endif
 
+#if defined(__USE_FILE_OFFSET64) && !defined(__COBALT_NO_OFF64_REDIRECT__)
+/*
+ * Make __RT() and __STD() usable in combination with off_t related
+ * services: the libc redirects them to their 64-bit variants in this
+ * case, follow suit. Code implementing the native variants opts out,
+ * as some libcs enforce 64-bit file offsets (e.g. uClibc-ng).
+ */
+#define COBALT_DECL_OFF64(T, FN, FN_64, I)                                     \
+	extern T __REDIRECT_NTH(__STD(FN), I, __real_##FN_64);                 \
+	extern T __REDIRECT_NTH(__RT(FN), I, __cobalt_##FN_64);                \
+	extern T __REDIRECT_NTH(__WRAP(FN), I, __wrap_##FN_64)
+#else
+#define COBALT_DECL_OFF64(T, FN, FN_64, I) COBALT_DECL(T, FN, I)
+#endif
+
 /*
  * 
  * Each "foo" Cobalt routine shadowing a POSIX service may be
lib/cobalt/mmap.c +6 -0
index d30b1722d..bf8772f6a 100644
@@ -18,6 +18,12 @@
   USA.
  */
 
+/*
+ * We implement mmap() for the native off_t next to mmap64(): keep their
+ * names apart, even if the libc enforces 64-bit file offsets.
+ */
+#define __COBALT_NO_OFF64_REDIRECT__
+
 #include <sys/mman.h>
 #include <rtdm/rtdm.h>
 #include <cobalt/uapi/syscall.h>
Signature · 1 line
2.47.3
#1 lore raw
Quoted 98 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

With _FILE_OFFSET_BITS=64, glibc redirects mmap() to mmap64() on 32-bit archs. Applications get this setting by default since y2038 support was added, as xeno-config passes it along with _TIME_BITS=64, and some toolchains even predefine both (e.g. Debian armhf). uClibc-ng enforces 64-bit file offsets unconditionally since release 1.0.58.

__STD(mmap()) and __RT(mmap()) do not follow this redirection: they expand to __real_mmap() and __cobalt_mmap(), declared with the 64-bit off_t of the caller. But with the Cobalt wrappers, __real_mmap() binds to the libc's mmap(), and __cobalt_mmap() is built with native off_t, so both take a 32-bit offset. They read it from the padding word in front of the 64-bit argument and fail with EINVAL or map the wrong page.

Internal users built with 64-bit off_t are affected as well:
cobalt_unmap_umm(), the pshared heap of copperplate and the TLSF pool of boilerplate all bind to the native-off_t __real_mmap() of libcobalt.

Add COBALT_DECL_OFF64(), which redirects the __STD(), __RT() and __WRAP() symbols of a service to its 64-bit variant whenever the libc does so, and use it for mmap(). This relies on libcobalt providing __real_mmap64(). lib/cobalt/mmap.c implements both variants and opts out via __COBALT_NO_OFF64_REDIRECT__: building it with native off_t is not sufficient if the libc enforces 64-bit file offsets.

Fixes fb2bd6e647a9 ("y2038: build: Introduce new build parameter --disable-y2038")
Assisted-by opencode:Opus-5.5
---
 include/cobalt/sys/mman.h |  6 +++---
 include/cobalt/wrappers.h | 15 +++++++++++++++
 lib/cobalt/mmap.c         |  6 ++++++
 3 files changed, 24 insertions(+), 3 deletions(-)
include/cobalt/sys/mman.h +3 -3
index e291f50ef..54c028ad9 100644
@@ -28,9 +28,9 @@
 extern "C" {
 #endif
 
-COBALT_DECL(void *, mmap,
-	    (void *addr, size_t length, int prot, int flags, int fd,
-	     off_t offset));
+COBALT_DECL_OFF64(void *, mmap, mmap64,
+		  (void *addr, size_t length, int prot, int flags, int fd,
+		   off_t offset));
 
 #if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE)
 COBALT_DECL(void *, mmap64,
include/cobalt/wrappers.h +15 -0
index 6d826eedf..9dd022a75 100644
@@ -45,6 +45,21 @@
 #define COBALT_DECL_TIME64(T, FN, FN_64, I) COBALT_DECL(T, FN, I)
 #endif
 
+#if defined(__USE_FILE_OFFSET64) && !defined(__COBALT_NO_OFF64_REDIRECT__)
+/*
+ * Make __RT() and __STD() usable in combination with off_t related
+ * services: the libc redirects them to their 64-bit variants in this
+ * case, follow suit. Code implementing the native variants opts out,
+ * as some libcs enforce 64-bit file offsets (e.g. uClibc-ng).
+ */
+#define COBALT_DECL_OFF64(T, FN, FN_64, I)                                     \
+	extern T __REDIRECT_NTH(__STD(FN), I, __real_##FN_64);                 \
+	extern T __REDIRECT_NTH(__RT(FN), I, __cobalt_##FN_64);                \
+	extern T __REDIRECT_NTH(__WRAP(FN), I, __wrap_##FN_64)
+#else
+#define COBALT_DECL_OFF64(T, FN, FN_64, I) COBALT_DECL(T, FN, I)
+#endif
+
 /*
  *
  * Each "foo" Cobalt routine shadowing a POSIX service may be
lib/cobalt/mmap.c +6 -1
index d30b1722d..bf8772f6a 100644
@@ -18,6 +18,12 @@
   USA.
  */
 
+/*
+ * We implement mmap() for the native off_t next to mmap64(): keep their
+ * names apart, even if the libc enforces 64-bit file offsets.
+ */
+#define __COBALT_NO_OFF64_REDIRECT__
+
 #include <sys/mman.h>
 #include <rtdm/rtdm.h>
 #include <cobalt/uapi/syscall.h>
--
Quoted 1 line

2.47.3

Hi Jan,

I think this patch needs to fix [1], as it is not linking correctly (at least here for my arm target), because at some point, __real_mmap64 goes missing while it should not, because of the #if mmap64 != mmap :

#if mmap64 != mmap
__weak
void *__real_mmap64(void *addr, size_t length, int prot, int flags,
int fd, off64_t offset)

{
	return mmap64(addr, length, prot, flags, fd, offset);
}

#endif

François

[1] https://gitlab.com/Xenomai/xenomai3/xenomai/-/commit/0bb6608b6fa712

author
#2 lore raw
Quoted 110 lines · François Legal
Quoted 99 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

With _FILE_OFFSET_BITS=64, glibc redirects mmap() to mmap64() on 32-bit archs. Applications get this setting by default since y2038 support was added, as xeno-config passes it along with _TIME_BITS=64, and some toolchains even predefine both (e.g. Debian armhf). uClibc-ng enforces 64-bit file offsets unconditionally since release 1.0.58.

__STD(mmap()) and __RT(mmap()) do not follow this redirection: they expand to __real_mmap() and __cobalt_mmap(), declared with the 64-bit off_t of the caller. But with the Cobalt wrappers, __real_mmap() binds to the libc's mmap(), and __cobalt_mmap() is built with native off_t, so both take a 32-bit offset. They read it from the padding word in front of the 64-bit argument and fail with EINVAL or map the wrong page.

Internal users built with 64-bit off_t are affected as well:
cobalt_unmap_umm(), the pshared heap of copperplate and the TLSF pool of boilerplate all bind to the native-off_t __real_mmap() of libcobalt.

Add COBALT_DECL_OFF64(), which redirects the __STD(), __RT() and __WRAP() symbols of a service to its 64-bit variant whenever the libc does so, and use it for mmap(). This relies on libcobalt providing __real_mmap64(). lib/cobalt/mmap.c implements both variants and opts out via __COBALT_NO_OFF64_REDIRECT__: building it with native off_t is not sufficient if the libc enforces 64-bit file offsets.

Fixes fb2bd6e647a9 ("y2038: build: Introduce new build parameter --disable-y2038")
Assisted-by opencode:Opus-5.5
---
 include/cobalt/sys/mman.h |  6 +++---
 include/cobalt/wrappers.h | 15 +++++++++++++++
 lib/cobalt/mmap.c         |  6 ++++++
 3 files changed, 24 insertions(+), 3 deletions(-)
include/cobalt/sys/mman.h +3 -3
index e291f50ef..54c028ad9 100644
@@ -28,9 +28,9 @@
 extern "C" {
 #endif
 
-COBALT_DECL(void *, mmap,
-	    (void *addr, size_t length, int prot, int flags, int fd,
-	     off_t offset));
+COBALT_DECL_OFF64(void *, mmap, mmap64,
+		  (void *addr, size_t length, int prot, int flags, int fd,
+		   off_t offset));
 
 #if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE)
 COBALT_DECL(void *, mmap64,
include/cobalt/wrappers.h +15 -0
index 6d826eedf..9dd022a75 100644
@@ -45,6 +45,21 @@
 #define COBALT_DECL_TIME64(T, FN, FN_64, I) COBALT_DECL(T, FN, I)
 #endif
 
+#if defined(__USE_FILE_OFFSET64) && !defined(__COBALT_NO_OFF64_REDIRECT__)
+/*
+ * Make __RT() and __STD() usable in combination with off_t related
+ * services: the libc redirects them to their 64-bit variants in this
+ * case, follow suit. Code implementing the native variants opts out,
+ * as some libcs enforce 64-bit file offsets (e.g. uClibc-ng).
+ */
+#define COBALT_DECL_OFF64(T, FN, FN_64, I)                                     \
+	extern T __REDIRECT_NTH(__STD(FN), I, __real_##FN_64);                 \
+	extern T __REDIRECT_NTH(__RT(FN), I, __cobalt_##FN_64);                \
+	extern T __REDIRECT_NTH(__WRAP(FN), I, __wrap_##FN_64)
+#else
+#define COBALT_DECL_OFF64(T, FN, FN_64, I) COBALT_DECL(T, FN, I)
+#endif
+
 /*
  * 
  * Each "foo" Cobalt routine shadowing a POSIX service may be
lib/cobalt/mmap.c +6 -0
index d30b1722d..bf8772f6a 100644
@@ -18,6 +18,12 @@
   USA.
  */
 
+/*
+ * We implement mmap() for the native off_t next to mmap64(): keep their
+ * names apart, even if the libc enforces 64-bit file offsets.
+ */
+#define __COBALT_NO_OFF64_REDIRECT__
+
 #include <sys/mman.h>
 #include <rtdm/rtdm.h>
 #include <cobalt/uapi/syscall.h>
Signature · 1 line
2.47.3

Hi Jan,

I think this patch needs to fix [1], as it is not linking correctly (at least here for my arm target), because at some point, __real_mmap64 goes missing while it should not, because of the #if mmap64 != mmap :

#if mmap64 != mmap
__weak
void *__real_mmap64(void *addr, size_t length, int prot, int flags,
int fd, off64_t offset)

Quoted 9 lines
{
	return mmap64(addr, length, prot, flags, fd, offset);
}

#endif

François

[1] https://gitlab.com/Xenomai/xenomai3/xenomai/-/commit/0bb6608b6fa712

This is on top of next, thus includes [2]. When backporting to stable, I will include that as well.

Jan

[2]
https://gitlab.com/Xenomai/xenomai3/xenomai/-/commit/c3aff1938e31cfe81045decbf40dfe6d6e1b2207

Signature · 2 lines
Siemens AG, Foundational Technologies
Linux Expert Center