Skip to contentSkip to conversation

Fixes broken mmap patching on 32bits archs

PATCH In review François Legal 3 participants 6 messages Nov 12 – Nov 20

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fixes broken mmap patching on 32bits archs

mmap is broken on 32 bits arch when __USE_TIME_BITS64 and __USE_FILE_OFFSET64 are set in that case, libc translate calls of mmap to mmap64 because file offset argument is a 64 bit value. We fail to catch that with the decoration logic (__STD/__RT) and plain libc mmap is called.

--

include/cobalt/sys/mman.h +1 -7
index e291f50ef..b6d650876 100644
@@ -28,16 +28,10 @@
 extern "C" {
 #endif
 
-COBALT_DECL(void *, mmap,
+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,
-	    (void *addr, size_t length, int prot, int flags, int fd,
-	     off64_t offset));
-#endif
-
 #ifdef __cplusplus
 }
 #endif
include/cobalt/wrappers.h +17 -0
index a5645a1d4..82c9aaa69 100644
@@ -46,6 +46,23 @@
 #define COBALT_DECL_TIME64(T, FN, FN_64, I) COBALT_DECL(T, FN, I)
 #endif
 
+#if __USE_FILE_OFFSET64 && __WORDSIZE == 32
+/*
+ * Make __RT() and __STD() usable in combination with time64_t related services.
+ */
+#define COBALT_IMPL_OFF64(T, FN, FN_64, I)                                    \
+	__typeof__(T) __wrap_##FN_64 I                                         \
+		__attribute__((alias("__cobalt_" __stringify(FN)), weak));     \
+	COBALT_IMPL(T, FN, I)
+#define COBALT_DECL_OFF64(T, FN, A, I)                                        \
+	__typeof__(T) __STD(A) I;                                              \
+	extern T __REDIRECT_NTH(__STD(FN), I, __real_##A);                     \
+	COBALT_DECL(T, FN, I)
+#else
+#define COBALT_IMPL_OFF64(T, FN, FN_64, I) COBALT_IMPL(T, FN, I)
+#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/rtdm.c +1 -6
index 00c1aaf4b..11a6e86e6 100644
@@ -542,7 +542,7 @@ COBALT_IMPL(int, shutdown, (int fd, int how))
 	return __STD(shutdown(fd, how));
 }
 
-COBALT_IMPL(void *, mmap64, (void *addr, size_t length, int prot, int flags,
+COBALT_IMPL_OFF64(void *, mmap, mmap64, (void *addr, size_t length, int prot, int flags,
 			     int fd, off64_t offset))
 {
 	struct _rtdm_mmap_request rma;
@@ -574,8 +574,3 @@ regular:
 #endif
 }
 
-COBALT_IMPL(void *, mmap, (void *addr, size_t length, int prot, int flags,
-			   int fd, off_t offset))
-{
-	return __COBALT(mmap64(addr, length, prot, flags, fd, offset));
-}
#1 lore raw
Quoted 71 lines · François Legal

mmap is broken on 32 bits arch when __USE_TIME_BITS64 and __USE_FILE_OFFSET64 are set in that case, libc translate calls of mmap to mmap64 because file offset argument is a 64 bit value. We fail to catch that with the decoration logic (__STD/__RT) and plain libc mmap is called.

--

include/cobalt/sys/mman.h +1 -7
index e291f50ef..b6d650876 100644
@@ -28,16 +28,10 @@
 extern "C" {
 #endif
 
-COBALT_DECL(void *, mmap,
+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,
-	    (void *addr, size_t length, int prot, int flags, int fd,
-	     off64_t offset));
-#endif
-
 #ifdef __cplusplus
 }
 #endif
include/cobalt/wrappers.h +17 -0
index a5645a1d4..82c9aaa69 100644
@@ -46,6 +46,23 @@
 #define COBALT_DECL_TIME64(T, FN, FN_64, I) COBALT_DECL(T, FN, I)
 #endif
 
+#if __USE_FILE_OFFSET64 && __WORDSIZE == 32
+/*
+ * Make __RT() and __STD() usable in combination with time64_t related services.
+ */
+#define COBALT_IMPL_OFF64(T, FN, FN_64, I)                                    \
+	__typeof__(T) __wrap_##FN_64 I                                         \
+		__attribute__((alias("__cobalt_" __stringify(FN)), weak));     \
+	COBALT_IMPL(T, FN, I)
+#define COBALT_DECL_OFF64(T, FN, A, I)                                        \
+	__typeof__(T) __STD(A) I;                                              \
+	extern T __REDIRECT_NTH(__STD(FN), I, __real_##A);                     \
+	COBALT_DECL(T, FN, I)
+#else
+#define COBALT_IMPL_OFF64(T, FN, FN_64, I) COBALT_IMPL(T, FN, I)
+#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/rtdm.c +1 -1
index 00c1aaf4b..11a6e86e6 100644
@@ -542,7 +542,7 @@ COBALT_IMPL(int, shutdown, (int fd, int how))
 	return __STD(shutdown(fd, how));
 }
 
-COBALT_IMPL(void *, mmap64, (void *addr, size_t length, int prot, int flags,
+COBALT_IMPL_OFF64(void *, mmap, mmap64, (void *addr, size_t length, int prot, int flags,
 			     int fd, off64_t offset))
 {
 	struct _rtdm_mmap_request rma;

lib/cobalt/rtdm.c regular: · line 581

Quoted 8 lines
lib/cobalt/rtdm.c +0 -5
@@ -574,8 +574,3 @@ regular:
 #endif
 }
 
-COBALT_IMPL(void *, mmap, (void *addr, size_t length, int prot, int flags,
-			   int fd, off_t offset))
-{
-	return __COBALT(mmap64(addr, length, prot, flags, fd, offset));
-}

The LLM is not convinced: On 32-bit arm with time64 libs, this version will map __RT(mmap()) to a variant that expects 64-bit off_t, while a caller which uses 32-bit off_t would rightfully expect mmap to be 32-bit then.

Its counter-proposal:

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>

I challenged it why we need that __COBALT_NO_OFF64_REDIRECT__:

"Why the time64 pattern doesn't carry over

- __USE_TIME_BITS64 && __TIMESIZE == 32 works without a marker because the native-time_t implementations are compiled with -U_TIME_BITS, so the condition is false in exactly those files.
- With glibc, the same works for mmap.c, which is built with -U_FILE_OFFSET_BITS. The counterpart of __TIMESIZE == 32 is !defined(__OFF_T_MATCHES_OFF64_T), which glibc and uClibc-ng define when off_t is 64-bit anyway.
- uClibc-ng 1.0.58 and later always define __USE_FILE_OFFSET64, so -U_FILE_OFFSET_BITS has no effect. On 32-bit targets __OFF_T_MATCHES_OFF64_T isn't defined, so the property test is true inside mmap.c as well. That brings back "__wrap_mmap aliased to undefined symbol __cobalt_mmap". Only an explicit opt-out by the file that implements both variants avoids it."

Jan

#2 lore raw
Quoted 74 lines · Jan Kiszka
Quoted 73 lines · François Legal

mmap is broken on 32 bits arch when __USE_TIME_BITS64 and __USE_FILE_OFFSET64 are set in that case, libc translate calls of mmap to mmap64 because file offset argument is a 64 bit value. We fail to catch that with the decoration logic (__STD/__RT) and plain libc mmap is called.

--

include/cobalt/sys/mman.h +1 -7
index e291f50ef..b6d650876 100644
@@ -28,16 +28,10 @@
 extern "C" {
 #endif
 
-COBALT_DECL(void *, mmap,
+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,
-	    (void *addr, size_t length, int prot, int flags, int fd,
-	     off64_t offset));
-#endif
-
 #ifdef __cplusplus
 }
 #endif
include/cobalt/wrappers.h +17 -0
index a5645a1d4..82c9aaa69 100644
@@ -46,6 +46,23 @@
 #define COBALT_DECL_TIME64(T, FN, FN_64, I) COBALT_DECL(T, FN, I)
 #endif
 
+#if __USE_FILE_OFFSET64 && __WORDSIZE == 32
+/*
+ * Make __RT() and __STD() usable in combination with time64_t related services.
+ */
+#define COBALT_IMPL_OFF64(T, FN, FN_64, I)                                    \
+	__typeof__(T) __wrap_##FN_64 I                                         \
+		__attribute__((alias("__cobalt_" __stringify(FN)), weak));     \
+	COBALT_IMPL(T, FN, I)
+#define COBALT_DECL_OFF64(T, FN, A, I)                                        \
+	__typeof__(T) __STD(A) I;                                              \
+	extern T __REDIRECT_NTH(__STD(FN), I, __real_##A);                     \
+	COBALT_DECL(T, FN, I)
+#else
+#define COBALT_IMPL_OFF64(T, FN, FN_64, I) COBALT_IMPL(T, FN, I)
+#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/rtdm.c +1 -1
index 00c1aaf4b..11a6e86e6 100644
@@ -542,7 +542,7 @@ COBALT_IMPL(int, shutdown, (int fd, int how))
 	return __STD(shutdown(fd, how));
 }
 
-COBALT_IMPL(void *, mmap64, (void *addr, size_t length, int prot, int flags,
+COBALT_IMPL_OFF64(void *, mmap, mmap64, (void *addr, size_t length, int prot, int flags,
 			     int fd, off64_t offset))
 {
 	struct _rtdm_mmap_request rma;
@@ -574,8 +574,3 @@ regular:
 #endif
 }
 
Quoted 11 lines

lib/cobalt/rtdm.c regular: · line 581

Quoted 6 lines
lib/cobalt/rtdm.c +0 -5
-COBALT_IMPL(void *, mmap, (void *addr, size_t length, int prot, int flags,
-			   int fd, off_t offset))
-{
-	return __COBALT(mmap64(addr, length, prot, flags, fd, offset));
-}

The LLM is not convinced: On 32-bit arm with time64 libs, this version will map __RT(mmap()) to a variant that expects 64-bit off_t, while a caller which uses 32-bit off_t would rightfully expect mmap to be 32-bit then.

I'm not sure about that. At least with glibc, if we want time64, we need offset64 too. So under those circumstances off_t is always 64bits long and glibc forces translation of calls to mmap to calls to mmap64 (using the __REDIRECT_NTH macros).

I'll give a try to this proposal anyway, but I have to admit that sticking to what Florian did for the time64 counterpart seemed a better approach than mixing glibc mangling like this.

François

Quoted 83 lines

Its counter-proposal:

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>

I challenged it why we need that __COBALT_NO_OFF64_REDIRECT__:

"Why the time64 pattern doesn't carry over

- __USE_TIME_BITS64 && __TIMESIZE == 32 works without a marker because the native-time_t implementations are compiled with -U_TIME_BITS, so the condition is false in exactly those files.
- With glibc, the same works for mmap.c, which is built with -U_FILE_OFFSET_BITS. The counterpart of __TIMESIZE == 32 is !defined(__OFF_T_MATCHES_OFF64_T), which glibc and uClibc-ng define when off_t is 64-bit anyway.
- uClibc-ng 1.0.58 and later always define __USE_FILE_OFFSET64, so -U_FILE_OFFSET_BITS has no effect. On 32-bit targets __OFF_T_MATCHES_OFF64_T isn't defined, so the property test is true inside mmap.c as well. That brings back "__wrap_mmap aliased to undefined symbol __cobalt_mmap". Only an explicit opt-out by the file that implements both variants avoids it."

Jan

#3 lore raw
In reply to François Legal
Quoted 71 lines · François Legal

mmap is broken on 32 bits arch when __USE_TIME_BITS64 and __USE_FILE_OFFSET64 are set in that case, libc translate calls of mmap to mmap64 because file offset argument is a 64 bit value. We fail to catch that with the decoration logic (__STD/__RT) and plain libc mmap is called.

--

include/cobalt/sys/mman.h +1 -7
index e291f50ef..b6d650876 100644
@@ -28,16 +28,10 @@
 extern "C" {
 #endif
 
-COBALT_DECL(void *, mmap,
+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,
-	    (void *addr, size_t length, int prot, int flags, int fd,
-	     off64_t offset));
-#endif
-
 #ifdef __cplusplus
 }
 #endif
include/cobalt/wrappers.h +17 -0
index a5645a1d4..82c9aaa69 100644
@@ -46,6 +46,23 @@
 #define COBALT_DECL_TIME64(T, FN, FN_64, I) COBALT_DECL(T, FN, I)
 #endif
 
+#if __USE_FILE_OFFSET64 && __WORDSIZE == 32
+/*
+ * Make __RT() and __STD() usable in combination with time64_t related services.
+ */
+#define COBALT_IMPL_OFF64(T, FN, FN_64, I)                                    \
+	__typeof__(T) __wrap_##FN_64 I                                         \
+		__attribute__((alias("__cobalt_" __stringify(FN)), weak));     \
+	COBALT_IMPL(T, FN, I)
+#define COBALT_DECL_OFF64(T, FN, A, I)                                        \
+	__typeof__(T) __STD(A) I;                                              \
+	extern T __REDIRECT_NTH(__STD(FN), I, __real_##A);                     \
+	COBALT_DECL(T, FN, I)
+#else
+#define COBALT_IMPL_OFF64(T, FN, FN_64, I) COBALT_IMPL(T, FN, I)
+#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/rtdm.c +1 -1
index 00c1aaf4b..11a6e86e6 100644
@@ -542,7 +542,7 @@ COBALT_IMPL(int, shutdown, (int fd, int how))
 	return __STD(shutdown(fd, how));
 }
 
-COBALT_IMPL(void *, mmap64, (void *addr, size_t length, int prot, int flags,
+COBALT_IMPL_OFF64(void *, mmap, mmap64, (void *addr, size_t length, int prot, int flags,
 			     int fd, off64_t offset))
 {
 	struct _rtdm_mmap_request rma;

lib/cobalt/rtdm.c regular: · line 581

Quoted 8 lines
lib/cobalt/rtdm.c +0 -5
@@ -574,8 +574,3 @@ regular:
 #endif
 }
 
-COBALT_IMPL(void *, mmap, (void *addr, size_t length, int prot, int flags,
-			   int fd, off_t offset))
-{
-	return __COBALT(mmap64(addr, length, prot, flags, fd, offset));
-}

The LLM is not convinced: On 32-bit arm with time64 libs, this version will map __RT(mmap()) to a variant that expects 64-bit off_t, while a caller which uses 32-bit off_t would rightfully expect mmap to be 32-bit then.

Its counter-proposal:

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>

I challenged it why we need that __COBALT_NO_OFF64_REDIRECT__:

"Why the time64 pattern doesn't carry over

- __USE_TIME_BITS64 && __TIMESIZE == 32 works without a marker because the native-time_t implementations are compiled with -U_TIME_BITS, so the condition is false in exactly those files.
- With glibc, the same works for mmap.c, which is built with -U_FILE_OFFSET_BITS. The counterpart of __TIMESIZE == 32 is !defined(__OFF_T_MATCHES_OFF64_T), which glibc and uClibc-ng define when off_t is 64-bit anyway.
- uClibc-ng 1.0.58 and later always define __USE_FILE_OFFSET64, so -U_FILE_OFFSET_BITS has no effect. On 32-bit targets __OFF_T_MATCHES_OFF64_T isn't defined, so the property test is true inside mmap.c as well. That brings back "__wrap_mmap aliased to undefined symbol __cobalt_mmap". Only an explicit opt-out by the file that implements both variants avoids it."

Jan

Signature · 2 lines
Siemens AG, Foundational Technologies
Linux Expert Center
#4 lore raw
Quoted 166 lines · Jan Kiszka
Quoted 71 lines · François Legal

mmap is broken on 32 bits arch when __USE_TIME_BITS64 and __USE_FILE_OFFSET64 are set in that case, libc translate calls of mmap to mmap64 because file offset argument is a 64 bit value. We fail to catch that with the decoration logic (__STD/__RT) and plain libc mmap is called.

--

include/cobalt/sys/mman.h +1 -7
index e291f50ef..b6d650876 100644
@@ -28,16 +28,10 @@
 extern "C" {
 #endif
 
-COBALT_DECL(void *, mmap,
+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,
-	    (void *addr, size_t length, int prot, int flags, int fd,
-	     off64_t offset));
-#endif
-
 #ifdef __cplusplus
 }
 #endif
include/cobalt/wrappers.h +17 -0
index a5645a1d4..82c9aaa69 100644
@@ -46,6 +46,23 @@
 #define COBALT_DECL_TIME64(T, FN, FN_64, I) COBALT_DECL(T, FN, I)
 #endif
 
+#if __USE_FILE_OFFSET64 && __WORDSIZE == 32
+/*
+ * Make __RT() and __STD() usable in combination with time64_t related services.
+ */
+#define COBALT_IMPL_OFF64(T, FN, FN_64, I)                                    \
+	__typeof__(T) __wrap_##FN_64 I                                         \
+		__attribute__((alias("__cobalt_" __stringify(FN)), weak));     \
+	COBALT_IMPL(T, FN, I)
+#define COBALT_DECL_OFF64(T, FN, A, I)                                        \
+	__typeof__(T) __STD(A) I;                                              \
+	extern T __REDIRECT_NTH(__STD(FN), I, __real_##A);                     \
+	COBALT_DECL(T, FN, I)
+#else
+#define COBALT_IMPL_OFF64(T, FN, FN_64, I) COBALT_IMPL(T, FN, I)
+#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/rtdm.c +1 -1
index 00c1aaf4b..11a6e86e6 100644
@@ -542,7 +542,7 @@ COBALT_IMPL(int, shutdown, (int fd, int how))
 	return __STD(shutdown(fd, how));
 }
 
-COBALT_IMPL(void *, mmap64, (void *addr, size_t length, int prot, int flags,
+COBALT_IMPL_OFF64(void *, mmap, mmap64, (void *addr, size_t length, int prot, int flags,
 			     int fd, off64_t offset))
 {
 	struct _rtdm_mmap_request rma;

lib/cobalt/rtdm.c regular: · line 581

Quoted 8 lines
lib/cobalt/rtdm.c +0 -5
@@ -574,8 +574,3 @@ regular:
 #endif
 }
 
-COBALT_IMPL(void *, mmap, (void *addr, size_t length, int prot, int flags,
-			   int fd, off_t offset))
-{
-	return __COBALT(mmap64(addr, length, prot, flags, fd, offset));
-}

The LLM is not convinced: On 32-bit arm with time64 libs, this version will map __RT(mmap()) to a variant that expects 64-bit off_t, while a caller which uses 32-bit off_t would rightfully expect mmap to be 32-bit then.

Its counter-proposal:

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>

I challenged it why we need that __COBALT_NO_OFF64_REDIRECT__:

"Why the time64 pattern doesn't carry over

- __USE_TIME_BITS64 && __TIMESIZE == 32 works without a marker because the native-time_t implementations are compiled with -U_TIME_BITS, so the condition is false in exactly those files.
- With glibc, the same works for mmap.c, which is built with -U_FILE_OFFSET_BITS. The counterpart of __TIMESIZE == 32 is !defined(__OFF_T_MATCHES_OFF64_T), which glibc and uClibc-ng define when off_t is 64-bit anyway.
- uClibc-ng 1.0.58 and later always define __USE_FILE_OFFSET64, so -U_FILE_OFFSET_BITS has no effect. On 32-bit targets __OFF_T_MATCHES_OFF64_T isn't defined, so the property test is true inside mmap.c as well. That brings back "__wrap_mmap aliased to undefined symbol __cobalt_mmap". Only an explicit opt-out by the file that implements both variants avoids it."

Quoted 5 lines

Jan

--
Siemens AG, Foundational Technologies
Linux Expert Center

Hi Jan,

I had a hard time making your patch apply to my tree and build (it came a little bit garbled in the message). Anyway, I ended up succeeding. It works. However, to make it build correctly, I slight modification is required in [1]. So for the records, I paste below the mix of both patches, the one that actually build and work.

François

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

From d7905501b97604e252550030698cf6b2fdc43dc5 Mon Sep 17 00:00:00 2001
From: Jan Kiszka <[email protected]>
Date: Fri, 30 Jan 2026 07:46:12 +0100
Subject: [PATCH] lib/cobalt: Avoid making mmap offset argument unconditionally

64-bit

When _FILE_OFFSET_BITS is 64, mmap is handled by the mmap64 symbol of glibc. The original mmap continues to expect native off_t size, i.e. 32 bits on 32-bit archs.

While we require applications to be rebuilt with the same settings Xenomai was built with as well (either both _TIME_BITS and _FILE_OFFSET_BITS set to 64 or both left unset) which practically shadows this issue, it remains wrong to change the wrapped mmap function in a way that is incompatible with glibc.

To avoid running into conflicts between enabling and disabling _TIME_BITS=64 and, thus, also _FILE_OFFSET_BITS=64, factor out the affected mmap wrappers as well as their __real stubs into a separate code module.

There is one catch: The cobalt-call to mmap in umm.c needs to be forced to mmap64 to become agnostic to _FILE_OFFSET_BITS used by the cobalt core internally.

Fixes fb2bd6e647a9 ("y2038: build: Introduce new build parameter --disable-y2038")
---
 include/cobalt/sys/mman.h |  6 +--
 include/cobalt/wrappers.h | 15 +++++++
 lib/cobalt/Makefile.am    | 15 +++++++
 lib/cobalt/mmap.c         | 83 +++++++++++++++++++++++++++++++++++++++
 lib/cobalt/rtdm.c         | 38 ------------------
 lib/cobalt/umm.c          |  4 +-
 lib/cobalt/wrappers.c     | 17 --------
 7 files changed, 118 insertions(+), 60 deletions(-)
 create mode 100644 lib/cobalt/mmap.c
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 a5645a1d4..3d9ec7dda 100644
@@ -46,6 +46,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/Makefile.am +15 -0
index 4318adf18..b5b184b55 100644
@@ -7,9 +7,12 @@ noinst_HEADERS =	\
 
 lib_LTLIBRARIES = libcobalt.la libmodechk.la
 
+noinst_LTLIBRARIES = libcobaltnativeoff.la
+
 libcobalt_la_LDFLAGS = @XENO_LIB_LDFLAGS@ -version-info 2:0:0 -lpthread -lrt
 
 libcobalt_la_LIBADD = \
+	libcobaltnativeoff.la			\
 	arch/@XENO_TARGET_ARCH@/libarch.la	\
 	../boilerplate/libboilerplate.la
 
@@ -43,6 +46,18 @@ libcobalt_la_CPPFLAGS =			\
 	-I$(top_srcdir)/include/cobalt	\
 	-I$(top_srcdir)/include
 
+# source files that require native off_t size
+libcobaltnativeoff_la_SOURCES =	\
+	mmap.c
+
+libcobaltnativeoff_la_LDFLAGS = @XENO_LIB_LDFLAGS@
+
+libcobaltnativeoff_la_CPPFLAGS =	\
+	@XENO_COBALT_CFLAGS@		\
+	-I$(top_srcdir)/include/cobalt	\
+	-I$(top_srcdir)/include		\
+	-U_TIME_BITS -U_FILE_OFFSET_BITS
+
 libmodechk_la_LIBADD = libcobalt.la
 
 libmodechk_la_SOURCES =	\
lib/cobalt/mmap.c new file +83 -0

83 lines not on this page · raw message

lib/cobalt/rtdm.c +0 -38
index 264de8ace..98513925a 100644
@@ -541,41 +541,3 @@ COBALT_IMPL(int, shutdown, (int fd, int how))
 
 	return __STD(shutdown(fd, how));
 }
-
-COBALT_IMPL(void *, mmap64, (void *addr, size_t length, int prot, int flags,
-			     int fd, off64_t offset))
-{
-	struct _rtdm_mmap_request rma;
-	int ret;
-
-	if (fd < 0) /* We don't do anonymous mappings. */
-		goto regular;
-
-	/* RTDM ignores the address hint, and rejects MAP_FIXED. */
-	rma.length = length;
-	rma.offset = offset;
-	rma.prot = prot;
-	rma.flags = flags;
-
-	ret = XENOMAI_SYSCALL3(sc_cobalt_mmap, fd, &rma, &addr);
-	if (ret != -EADV && ret != -ENOSYS) {
-		ret = set_errno(ret);
-		if (ret)
-			return MAP_FAILED;
-
-		return addr;
-	}
-
-regular:
-#if mmap64 == mmap
-	return __STD(mmap(addr, length, prot, flags, fd, offset));
-#else
-	return __STD(mmap64(addr, length, prot, flags, fd, offset));
-#endif
-}
-
-COBALT_IMPL(void *, mmap, (void *addr, size_t length, int prot, int flags,
-			   int fd, off_t offset))
-{
-	return __COBALT(mmap64(addr, length, prot, flags, fd, offset));
-}
lib/cobalt/umm.c +2 -2
index c0d4489d3..6b4303b72 100644
@@ -58,8 +58,8 @@ static void *__map_umm(const char *name, uint32_t *size_r)
 		return MAP_FAILED;
 	}
 
-	addr = __RT(mmap(NULL, statbuf.size, PROT_READ|PROT_WRITE,
-			 MAP_SHARED, fd, 0));
+	addr = __RT(mmap64(NULL, statbuf.size, PROT_READ|PROT_WRITE,
+			   MAP_SHARED, fd, 0));
 	__RT(close(fd));
 
 	*size_r = statbuf.size;
lib/cobalt/wrappers.c +0 -17
index 18c237734..3a0063f05 100644
@@ -28,7 +28,6 @@
 #include <sys/select.h>
 #include <sys/socket.h>
 #include <sys/time.h>
-#include <sys/mman.h>
 #include <stdio.h>
 #include <stdarg.h>
 #include <stdlib.h>
@@ -384,22 +383,6 @@ int __real_select (int __nfds, fd_set *__restrict __readfds,
 	return select(__nfds, __readfds, __writefds, __exceptfds, __timeout);
 }
 
-__weak
-void *__real_mmap(void *addr, size_t length, int prot, int flags,
-		  int fd, off_t offset)
-{
-	return mmap(addr, length, prot, flags, fd, offset);
-}
-
-#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
-
 __weak
 int __real_vfprintf(FILE *stream, const char *fmt, va_list args)
 {
#5 lore raw
Quoted 176 lines · François Legal
Quoted 166 lines · Jan Kiszka
Quoted 71 lines · François Legal

mmap is broken on 32 bits arch when __USE_TIME_BITS64 and __USE_FILE_OFFSET64 are set in that case, libc translate calls of mmap to mmap64 because file offset argument is a 64 bit value. We fail to catch that with the decoration logic (__STD/__RT) and plain libc mmap is called.

--

include/cobalt/sys/mman.h +1 -7
index e291f50ef..b6d650876 100644
@@ -28,16 +28,10 @@
 extern "C" {
 #endif
 
-COBALT_DECL(void *, mmap,
+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,
-	    (void *addr, size_t length, int prot, int flags, int fd,
-	     off64_t offset));
-#endif
-
 #ifdef __cplusplus
 }
 #endif
include/cobalt/wrappers.h +17 -0
index a5645a1d4..82c9aaa69 100644
@@ -46,6 +46,23 @@
 #define COBALT_DECL_TIME64(T, FN, FN_64, I) COBALT_DECL(T, FN, I)
 #endif
 
+#if __USE_FILE_OFFSET64 && __WORDSIZE == 32
+/*
+ * Make __RT() and __STD() usable in combination with time64_t related services.
+ */
+#define COBALT_IMPL_OFF64(T, FN, FN_64, I)                                    \
+	__typeof__(T) __wrap_##FN_64 I                                         \
+		__attribute__((alias("__cobalt_" __stringify(FN)), weak));     \
+	COBALT_IMPL(T, FN, I)
+#define COBALT_DECL_OFF64(T, FN, A, I)                                        \
+	__typeof__(T) __STD(A) I;                                              \
+	extern T __REDIRECT_NTH(__STD(FN), I, __real_##A);                     \
+	COBALT_DECL(T, FN, I)
+#else
+#define COBALT_IMPL_OFF64(T, FN, FN_64, I) COBALT_IMPL(T, FN, I)
+#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/rtdm.c +1 -1
index 00c1aaf4b..11a6e86e6 100644
@@ -542,7 +542,7 @@ COBALT_IMPL(int, shutdown, (int fd, int how))
 	return __STD(shutdown(fd, how));
 }
 
-COBALT_IMPL(void *, mmap64, (void *addr, size_t length, int prot, int flags,
+COBALT_IMPL_OFF64(void *, mmap, mmap64, (void *addr, size_t length, int prot, int flags,
 			     int fd, off64_t offset))
 {
 	struct _rtdm_mmap_request rma;

lib/cobalt/rtdm.c regular: · line 581

Quoted 8 lines
lib/cobalt/rtdm.c +0 -5
@@ -574,8 +574,3 @@ regular:
 #endif
 }
 
-COBALT_IMPL(void *, mmap, (void *addr, size_t length, int prot, int flags,
-			   int fd, off_t offset))
-{
-	return __COBALT(mmap64(addr, length, prot, flags, fd, offset));
-}

The LLM is not convinced: On 32-bit arm with time64 libs, this version will map __RT(mmap()) to a variant that expects 64-bit off_t, while a caller which uses 32-bit off_t would rightfully expect mmap to be 32-bit then.

Its counter-proposal:

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>

I challenged it why we need that __COBALT_NO_OFF64_REDIRECT__:

"Why the time64 pattern doesn't carry over

- __USE_TIME_BITS64 && __TIMESIZE == 32 works without a marker because the native-time_t implementations are compiled with -U_TIME_BITS, so the condition is false in exactly those files.
- With glibc, the same works for mmap.c, which is built with -U_FILE_OFFSET_BITS. The counterpart of __TIMESIZE == 32 is !defined(__OFF_T_MATCHES_OFF64_T), which glibc and uClibc-ng define when off_t is 64-bit anyway.
- uClibc-ng 1.0.58 and later always define __USE_FILE_OFFSET64, so -U_FILE_OFFSET_BITS has no effect. On 32-bit targets __OFF_T_MATCHES_OFF64_T isn't defined, so the property test is true inside mmap.c as well. That brings back "__wrap_mmap aliased to undefined symbol __cobalt_mmap". Only an explicit opt-out by the file that implements both variants avoids it."

Quoted 5 lines

Jan

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

Hi Jan,

Quoted 4 lines

I had a hard time making your patch apply to my tree and build (it came a little bit garbled in the message). Anyway, I ended up succeeding. It works. However, to make it build correctly, I slight modification is required in [1]. So for the records, I paste below the mix of both patches, the one that actually build and work.

I suppose your problems relate to backporting the patch from next to 3.3, right? Because it applied and was testing on next IIRC.

Anyway, let me send it out properly. Once it's validated over next/master, we can also backport what is needed.

Jan

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