From 6ca5e0837ec84f789d27a2b296532e1cd8c45f86 Mon Sep 17 00:00:00 2001 From: funsafe-ptr Date: Wed, 13 Nov 2024 23:30:07 +0000 Subject: [PATCH 1/7] [compiler-rt] Fixed Android 8.1 `getauxval(AT_PAGESZ)` crashes if called from `.preinit_array`. (#113427) --- compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp index 8b1850f85010c..3be26e785b964 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp @@ -82,6 +82,11 @@ # include # endif +# if SANITIZER_ANDROID && __ANDROID_API__ < 35 +// The weak strerrorname_np definition allows to check for the API level at runtime. +extern "C" SANITIZER_WEAK_ATTRIBUTE const char* strerrorname_np(int); +# endif + # if SANITIZER_LINUX && defined(__loongarch__) # include # endif @@ -1214,6 +1219,12 @@ uptr GetPageSize() { CHECK_EQ(rv, 0); return (uptr)pz; # elif SANITIZER_USE_GETAUXVAL + +# if SANITIZER_ANDROID && __ANDROID_API__ < 35 + if (!strerrorname_np) + return 4096; +# endif + return getauxval(AT_PAGESZ); # else return sysconf(_SC_PAGESIZE); // EXEC_PAGESIZE may not be trustworthy. From b1af6240851b191c6dea114c3934b1918e2b4c2f Mon Sep 17 00:00:00 2001 From: funsafe-ptr Date: Sat, 23 Nov 2024 00:01:13 +0000 Subject: [PATCH 2/7] [compiler-rt] Add Android page size comment and formatting. --- compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp index 3be26e785b964..cd67328498f3d 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp @@ -83,7 +83,8 @@ # endif # if SANITIZER_ANDROID && __ANDROID_API__ < 35 -// The weak strerrorname_np definition allows to check for the API level at runtime. +// The weak strerrorname_np definition allows to check for the API level at +// runtime. extern "C" SANITIZER_WEAK_ATTRIBUTE const char* strerrorname_np(int); # endif @@ -1219,8 +1220,9 @@ uptr GetPageSize() { CHECK_EQ(rv, 0); return (uptr)pz; # elif SANITIZER_USE_GETAUXVAL - # if SANITIZER_ANDROID && __ANDROID_API__ < 35 + // The 16 KB page size was introduced in Android 15, while earlier versions + // of Android used a 4 KB page size. if (!strerrorname_np) return 4096; # endif From 8997cc5134cff9bdb75a9fe99220d12fd200f644 Mon Sep 17 00:00:00 2001 From: funsafe-ptr Date: Sat, 23 Nov 2024 00:23:19 +0000 Subject: [PATCH 3/7] [compiler-rt] Fix formatting. Signed-off-by: funsafe-ptr --- compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp index cd67328498f3d..d7fbee297f20a 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp @@ -82,11 +82,11 @@ # include # endif -# if SANITIZER_ANDROID && __ANDROID_API__ < 35 +# if SANITIZER_ANDROID && __ANDROID_API__ < 35 // The weak strerrorname_np definition allows to check for the API level at // runtime. -extern "C" SANITIZER_WEAK_ATTRIBUTE const char* strerrorname_np(int); -# endif +extern "C" SANITIZER_WEAK_ATTRIBUTE const char *strerrorname_np(int); +# endif # if SANITIZER_LINUX && defined(__loongarch__) # include @@ -1221,7 +1221,7 @@ uptr GetPageSize() { return (uptr)pz; # elif SANITIZER_USE_GETAUXVAL # if SANITIZER_ANDROID && __ANDROID_API__ < 35 - // The 16 KB page size was introduced in Android 15, while earlier versions + // The 16 KB page size was introduced in Android 15, while earlier versions // of Android used a 4 KB page size. if (!strerrorname_np) return 4096; From b17a0474f47676ae1281c63175f3b45483a8f0bc Mon Sep 17 00:00:00 2001 From: funsafe-ptr Date: Sat, 23 Nov 2024 03:56:10 +0000 Subject: [PATCH 4/7] [compiler-rt] Fix comment. --- compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp index d7fbee297f20a..9be3faccdf53f 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp @@ -83,8 +83,8 @@ # endif # if SANITIZER_ANDROID && __ANDROID_API__ < 35 -// The weak strerrorname_np definition allows to check for the API level at -// runtime. +// The weak `strerrorname_np` (introduced in API level 35) definition, +// allows for checking the API level at runtime. extern "C" SANITIZER_WEAK_ATTRIBUTE const char *strerrorname_np(int); # endif @@ -1221,8 +1221,8 @@ uptr GetPageSize() { return (uptr)pz; # elif SANITIZER_USE_GETAUXVAL # if SANITIZER_ANDROID && __ANDROID_API__ < 35 - // The 16 KB page size was introduced in Android 15, while earlier versions - // of Android used a 4 KB page size. + // The 16 KB page size was introduced in Android 15 (API level 35), while + // earlier versions of Android always used a 4 KB page size. if (!strerrorname_np) return 4096; # endif From 08d33873928638446b72f591b0c552c586d5bc0e Mon Sep 17 00:00:00 2001 From: funsafe-ptr Date: Sat, 30 Nov 2024 14:16:52 +0000 Subject: [PATCH 5/7] [compiler-rt] Add comment. --- compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp index 9be3faccdf53f..98050a341ffc3 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp @@ -1223,6 +1223,9 @@ uptr GetPageSize() { # if SANITIZER_ANDROID && __ANDROID_API__ < 35 // The 16 KB page size was introduced in Android 15 (API level 35), while // earlier versions of Android always used a 4 KB page size. + // We are checking the weak definition of `strerrorname_np` (introduced in API + // level 35) because some earlier API levels crashed when + // `getauxval(AT_PAGESZ)` was called from the `.preinit_array`. if (!strerrorname_np) return 4096; # endif From 771698b71e36268a2b37ea927f7a8657d19273e5 Mon Sep 17 00:00:00 2001 From: funsafe-ptr Date: Fri, 13 Dec 2024 00:24:57 +0000 Subject: [PATCH 6/7] [compiler-rt] Use `getpagesize` for Android. --- .../lib/sanitizer_common/sanitizer_linux.cpp | 20 ++++--------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp index 98050a341ffc3..1f3fb004185d2 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp @@ -82,12 +82,6 @@ # include # endif -# if SANITIZER_ANDROID && __ANDROID_API__ < 35 -// The weak `strerrorname_np` (introduced in API level 35) definition, -// allows for checking the API level at runtime. -extern "C" SANITIZER_WEAK_ATTRIBUTE const char *strerrorname_np(int); -# endif - # if SANITIZER_LINUX && defined(__loongarch__) # include # endif @@ -1219,17 +1213,11 @@ uptr GetPageSize() { int rv = internal_sysctl(mib, 2, &pz, &pzl, nullptr, 0); CHECK_EQ(rv, 0); return (uptr)pz; +# elif SANITIZER_ANDROID + // Using `getpagesize` because calling `getauxval` or `sysconf` from the + // `.preinit_array` can cause crashes on some older API levels. + return getpagesize(); # elif SANITIZER_USE_GETAUXVAL -# if SANITIZER_ANDROID && __ANDROID_API__ < 35 - // The 16 KB page size was introduced in Android 15 (API level 35), while - // earlier versions of Android always used a 4 KB page size. - // We are checking the weak definition of `strerrorname_np` (introduced in API - // level 35) because some earlier API levels crashed when - // `getauxval(AT_PAGESZ)` was called from the `.preinit_array`. - if (!strerrorname_np) - return 4096; -# endif - return getauxval(AT_PAGESZ); # else return sysconf(_SC_PAGESIZE); // EXEC_PAGESIZE may not be trustworthy. From 1837ce1998730e47fa90d28e3921e688816281c9 Mon Sep 17 00:00:00 2001 From: funsafe-ptr Date: Fri, 13 Dec 2024 13:04:45 +0000 Subject: [PATCH 7/7] Revert "[compiler-rt] Use `getpagesize` for Android." This reverts commit 771698b71e36268a2b37ea927f7a8657d19273e5. --- .../lib/sanitizer_common/sanitizer_linux.cpp | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp index 1f3fb004185d2..98050a341ffc3 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp @@ -82,6 +82,12 @@ # include # endif +# if SANITIZER_ANDROID && __ANDROID_API__ < 35 +// The weak `strerrorname_np` (introduced in API level 35) definition, +// allows for checking the API level at runtime. +extern "C" SANITIZER_WEAK_ATTRIBUTE const char *strerrorname_np(int); +# endif + # if SANITIZER_LINUX && defined(__loongarch__) # include # endif @@ -1213,11 +1219,17 @@ uptr GetPageSize() { int rv = internal_sysctl(mib, 2, &pz, &pzl, nullptr, 0); CHECK_EQ(rv, 0); return (uptr)pz; -# elif SANITIZER_ANDROID - // Using `getpagesize` because calling `getauxval` or `sysconf` from the - // `.preinit_array` can cause crashes on some older API levels. - return getpagesize(); # elif SANITIZER_USE_GETAUXVAL +# if SANITIZER_ANDROID && __ANDROID_API__ < 35 + // The 16 KB page size was introduced in Android 15 (API level 35), while + // earlier versions of Android always used a 4 KB page size. + // We are checking the weak definition of `strerrorname_np` (introduced in API + // level 35) because some earlier API levels crashed when + // `getauxval(AT_PAGESZ)` was called from the `.preinit_array`. + if (!strerrorname_np) + return 4096; +# endif + return getauxval(AT_PAGESZ); # else return sysconf(_SC_PAGESIZE); // EXEC_PAGESIZE may not be trustworthy.