From 061580bc8cc06dc0888f3223decd7a104f2eb1a5 Mon Sep 17 00:00:00 2001 From: Ryan Prichard Date: Wed, 7 Aug 2024 18:29:04 -0700 Subject: [PATCH 1/3] [libc++][Android] Disable fdsan in close.pass.cpp fdsan is Bionic's "File Descriptor Sanitizer". Starting in API 30+, it aborts this close.pass.cpp test, because it closes the FD belonging to std::filebuf's FILE*. Use an fdsan API to suppress the diagnostic. --- .../fstreams/filebuf.members/close.pass.cpp | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/libcxx/test/std/input.output/file.streams/fstreams/filebuf.members/close.pass.cpp b/libcxx/test/std/input.output/file.streams/fstreams/filebuf.members/close.pass.cpp index e0338e6f619b7..fa2b3415b3cd3 100644 --- a/libcxx/test/std/input.output/file.streams/fstreams/filebuf.members/close.pass.cpp +++ b/libcxx/test/std/input.output/file.streams/fstreams/filebuf.members/close.pass.cpp @@ -10,11 +10,6 @@ // basic_filebuf* close(); -// This test closes an fd that belongs to a std::filebuf, and Bionic's fdsan -// detects this and aborts the process, starting in Android R (API 30). -// See D137129. -// XFAIL: LIBCXX-ANDROID-FIXME && !android-device-api={{2[1-9]}} - #include #include #if defined(__unix__) @@ -24,6 +19,14 @@ #include "test_macros.h" #include "platform_support.h" +// If we're building for a lower __ANDROID_API__, the Bionic versioner will +// omit the function declarations from fdsan.h. We might be running on a newer +// API level, though, so declare the API function here using weak. +#if defined(__BIONIC__) +#include +enum android_fdsan_error_level android_fdsan_set_error_level(enum android_fdsan_error_level new_level) __attribute__((weak)); +#endif + int main(int, char**) { std::string temp = get_temp_file_name(); @@ -37,6 +40,13 @@ int main(int, char**) assert(f.close() == nullptr); assert(!f.is_open()); } +#if defined(__BIONIC__) + // Starting with Android API 30+, Bionic's fdsan aborts a process that + // attempts to close a file descriptor belonging to something else. Disable + // fdsan to allow closing the FD belonging to std::filebuf's FILE*. + if (android_fdsan_set_error_level != nullptr) + android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_DISABLED); +#endif #if defined(__unix__) { std::filebuf f; From 285d243972a6ad9bd8560067221d5b79cf41f879 Mon Sep 17 00:00:00 2001 From: Ryan Prichard Date: Wed, 7 Aug 2024 19:08:47 -0700 Subject: [PATCH 2/3] formatting --- .../file.streams/fstreams/filebuf.members/close.pass.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libcxx/test/std/input.output/file.streams/fstreams/filebuf.members/close.pass.cpp b/libcxx/test/std/input.output/file.streams/fstreams/filebuf.members/close.pass.cpp index fa2b3415b3cd3..15fdf6a11661e 100644 --- a/libcxx/test/std/input.output/file.streams/fstreams/filebuf.members/close.pass.cpp +++ b/libcxx/test/std/input.output/file.streams/fstreams/filebuf.members/close.pass.cpp @@ -24,7 +24,8 @@ // API level, though, so declare the API function here using weak. #if defined(__BIONIC__) #include -enum android_fdsan_error_level android_fdsan_set_error_level(enum android_fdsan_error_level new_level) __attribute__((weak)); +enum android_fdsan_error_level android_fdsan_set_error_level(enum android_fdsan_error_level new_level) + __attribute__((weak)); #endif int main(int, char**) From 282569854e0796e1a0f3aa12608e9a7fb3237418 Mon Sep 17 00:00:00 2001 From: Ryan Prichard Date: Tue, 3 Dec 2024 15:37:56 -0800 Subject: [PATCH 3/3] Skip part of the test for `__BIONIC__` --- .../fstreams/filebuf.members/close.pass.cpp | 21 ++++--------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/libcxx/test/std/input.output/file.streams/fstreams/filebuf.members/close.pass.cpp b/libcxx/test/std/input.output/file.streams/fstreams/filebuf.members/close.pass.cpp index 15fdf6a11661e..43233decf1b34 100644 --- a/libcxx/test/std/input.output/file.streams/fstreams/filebuf.members/close.pass.cpp +++ b/libcxx/test/std/input.output/file.streams/fstreams/filebuf.members/close.pass.cpp @@ -19,15 +19,6 @@ #include "test_macros.h" #include "platform_support.h" -// If we're building for a lower __ANDROID_API__, the Bionic versioner will -// omit the function declarations from fdsan.h. We might be running on a newer -// API level, though, so declare the API function here using weak. -#if defined(__BIONIC__) -#include -enum android_fdsan_error_level android_fdsan_set_error_level(enum android_fdsan_error_level new_level) - __attribute__((weak)); -#endif - int main(int, char**) { std::string temp = get_temp_file_name(); @@ -41,14 +32,10 @@ int main(int, char**) assert(f.close() == nullptr); assert(!f.is_open()); } -#if defined(__BIONIC__) - // Starting with Android API 30+, Bionic's fdsan aborts a process that - // attempts to close a file descriptor belonging to something else. Disable - // fdsan to allow closing the FD belonging to std::filebuf's FILE*. - if (android_fdsan_set_error_level != nullptr) - android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_DISABLED); -#endif -#if defined(__unix__) + // Starting with Android API 30+, Bionic's fdsan aborts a process that calls + // close() on a file descriptor tagged as belonging to something else (such + // as a FILE*). +#if defined(__unix__) && !defined(__BIONIC__) { std::filebuf f; assert(!f.is_open());