From 2aafa62ee8e269a396ba36b006ae835013f55496 Mon Sep 17 00:00:00 2001 From: Florian Mayer Date: Fri, 28 Mar 2025 15:25:55 -0700 Subject: [PATCH 01/11] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20?= =?UTF-8?q?initial=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Created using spr 1.3.4 --- compiler-rt/lib/msan/tests/msan_test.cpp | 69 +++++++++++++++++++ .../sanitizer_common_interceptors.inc | 68 ++++++++++++++++++ .../sanitizer_platform_interceptors.h | 4 ++ .../sanitizer_platform_limits_posix.h | 9 +++ 4 files changed, 150 insertions(+) diff --git a/compiler-rt/lib/msan/tests/msan_test.cpp b/compiler-rt/lib/msan/tests/msan_test.cpp index a126dd4fdd55e..8b7df85e0ff04 100644 --- a/compiler-rt/lib/msan/tests/msan_test.cpp +++ b/compiler-rt/lib/msan/tests/msan_test.cpp @@ -4908,5 +4908,74 @@ TEST(MemorySanitizer, timer_create) { EXPECT_POISONED(timer2); timer_delete(timer); } + +TEST(MemorySanitizer, getservent_r) { + struct servent result_buf; + struct servent *result; + char buf[1024]; + EXPECT_POISONED(result_buf); + EXPECT_POISONED(result); + EXPECT_POISONED(buf); + ASSERT_EQ(getservent_r(&result_buf, buf, sizeof(buf), &result), 0); + EXPECT_NOT_POISONED(result); + ASSERT_NE(result, nullptr); + EXPECT_NOT_POISONED(result_buf); + EXPECT_NOT_POISONED(buf); +} + +TEST(MemorySanitizer, getservbyname_r) { + struct servent result_buf; + struct servent *result; + char buf[1024]; + EXPECT_POISONED(result_buf); + EXPECT_POISONED(result); + EXPECT_POISONED(buf); + ASSERT_EQ( + getservbyname_r("ssh", nullptr, &result_buf, buf, sizeof(buf), &result), + 0); + EXPECT_NOT_POISONED(result); + // If this fails, check /etc/services if "ssh" exists. I picked this because + // it should exist everywhere, if it doesn't, I am sorry. Disable the test + // then please. + ASSERT_NE(result, nullptr); + EXPECT_NOT_POISONED(result_buf); + EXPECT_NOT_POISONED(buf); +} + +TEST(MemorySanitizer, getservbyname_r_unknown) { + struct servent result_buf; + struct servent *result; + char buf[1024]; + EXPECT_POISONED(result_buf); + EXPECT_POISONED(result); + EXPECT_POISONED(buf); + ASSERT_EQ(getservbyname_r("invalidhadfuiasdhi", nullptr, &result_buf, buf, + sizeof(buf), &result), + 0); + EXPECT_NOT_POISONED(result); + ASSERT_EQ(result, nullptr); + EXPECT_POISONED(result_buf); + EXPECT_POISONED(buf); +} + +TEST(MemorySanitizer, getservbyport_r) { + struct servent result_buf; + struct servent *result; + char buf[1024]; + EXPECT_POISONED(result_buf); + EXPECT_POISONED(result); + EXPECT_POISONED(buf); + ASSERT_EQ(getservbyport_r(htons(22), nullptr, &result_buf, buf, sizeof(buf), + &result), + 0); + EXPECT_NOT_POISONED(result); + // If this fails, check /etc/services if "ssh" exists. I picked this because + // it should exist everywhere, if it doesn't, I am sorry. Disable the test + // then please. + ASSERT_NE(result, nullptr); + EXPECT_NOT_POISONED(result_buf); + EXPECT_NOT_POISONED(buf); +} + #endif } // namespace diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc index 761dbd3f5a679..29b977c050579 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -10279,6 +10279,71 @@ INTERCEPTOR(SSIZE_T, freadlink, int fd, char *buf, SIZE_T bufsiz) { # define INIT_FREADLINK #endif +#if SANITIZER_INTERCEPT_GETSERVENT_R || SANITIZER_INTERCEPT_GETSERVBYNAME_R || \ + SANITIZER_INTERCEPT_GETSERVBYPORT_R + +UNUSED static void HandleGetServentReentrantResult( + void *ctx, int res, struct __sanitizer_servent *result_buf, char *buf, + SIZE_T buflen, struct __sanitizer_servent **result) { + if (res) + return; + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (char *)result, sizeof(void *)); + if (*result) { + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (char *)*result, + sizeof(__sanitizer_servent)); + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen); + } +} + +#endif + +#if SANITIZER_INTERCEPT_GETSERVENT_R +INTERCEPTOR(int, getservent_r, struct __sanitizer_servent *result_buf, + char *buf, SIZE_T buflen, struct __sanitizer_servent **result) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, getservent_r, result_buf, buf, buflen, result); + int res = REAL(getservent_r)(result_buf, buf, buflen, result); + HandleGetServentReentrantResult(ctx, res, result_buf, buf, buflen, result); + return res; +} +# define INIT_GETSERVENT_R COMMON_INTERCEPT_FUNCTION(getservent_r) +#else +# define INIT_GETSERVENT_R +#endif + +#if SANITIZER_INTERCEPT_GETSERVBYNAME_R +INTERCEPTOR(int, getservbyname_r, const char *name, const char *proto, + struct __sanitizer_servent *result_buf, char *buf, SIZE_T buflen, + struct __sanitizer_servent **result) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, getservbyname_r, name, proto, result_buf, buf, + buflen, result); + COMMON_INTERCEPTOR_READ_STRING(ctx, name, internal_strlen(name)); + int res = REAL(getservbyname_r)(name, proto, result_buf, buf, buflen, result); + HandleGetServentReentrantResult(ctx, res, result_buf, buf, buflen, result); + return res; +} +# define INIT_GETSERVBYNAME_R COMMON_INTERCEPT_FUNCTION(getservbyname_r) +#else +# define INIT_GETSERVBYNAME_R +#endif + +#if SANITIZER_INTERCEPT_GETSERVBYPORT_R +INTERCEPTOR(int, getservbyport_r, int port, const char *proto, + struct __sanitizer_servent *result_buf, char *buf, SIZE_T buflen, + struct __sanitizer_servent **result) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, getservbyport_r, port, proto, result_buf, buf, + buflen, result); + int res = REAL(getservbyport_r)(port, proto, result_buf, buf, buflen, result); + HandleGetServentReentrantResult(ctx, res, result_buf, buf, buflen, result); + return res; +} +# define INIT_GETSERVBYPORT_R COMMON_INTERCEPT_FUNCTION(getservbyport_r) +#else +# define INIT_GETSERVBYPORT_R +#endif + #include "sanitizer_common_interceptors_netbsd_compat.inc" namespace __sanitizer { @@ -10604,4 +10669,7 @@ static void InitializeCommonInterceptors() { INIT_FREADLINK; INIT___PRINTF_CHK; + INIT_GETSERVENT_R; + INIT_GETSERVBYNAME_R; + INIT_GETSERVBYPORT_R; } diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h index 468b5494d0092..b8f2f738e7478 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h @@ -645,6 +645,10 @@ SANITIZER_WEAK_IMPORT void *aligned_alloc(__sanitizer::usize __alignment, # define SI_MAC_OS_DEPLOYMENT_MIN_13_00 0 #endif #define SANITIZER_INTERCEPT_FREADLINK (SI_MAC && SI_MAC_OS_DEPLOYMENT_MIN_13_00) +#define SANITIZER_INTERCEPT_GETSERVENT_R SI_GLIBC +#define SANITIZER_INTERCEPT_GETSERVBYNAME_R SI_GLIBC +#define SANITIZER_INTERCEPT_GETSERVBYPORT_R SI_GLIBC + // This macro gives a way for downstream users to override the above // interceptor macros irrespective of the platform they are on. They have // to do two things: diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_posix.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_posix.h index 67f00ff6f9e72..1f7e3d21b6a6f 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_posix.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_posix.h @@ -1509,6 +1509,15 @@ extern unsigned IOCTL_KIOCSOUND; extern unsigned IOCTL_PIO_SCRNMAP; #endif +# if SANITIZER_GLIBC +struct __sanitizer_servent { + char *s_name; + char **s_aliases; + int s_port; + char *s_proto; +}; +# endif + extern const int si_SEGV_MAPERR; extern const int si_SEGV_ACCERR; } // namespace __sanitizer From 7871fb47f15f34b527be4e119be0a035e2e1a29e Mon Sep 17 00:00:00 2001 From: Florian Mayer Date: Fri, 28 Mar 2025 15:29:49 -0700 Subject: [PATCH 02/11] add test Created using spr 1.3.4 --- .../TestCases/Linux/getservent_r.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp diff --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp b/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp new file mode 100644 index 0000000000000..a0444477a9ff0 --- /dev/null +++ b/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp @@ -0,0 +1,29 @@ +// RUN: %clangxx -O0 %s -o %t && %run %t + +// REQUIRES: glibc + +#include +#include +#include + +int main(void) { + struct servent result_buf; + struct servent *result; + char buf[1024]; + assert(getservent_r(&result_buf, buf, sizeof(buf), &result) == 0); + assert(result != nullptr); + + // If these fail, check /etc/services if "ssh" exists. I picked this because + // it should exist everywhere, if it doesn't, I am sorry. Disable the test + // then please. + assert(getservbyname_r("ssh", nullptr, &result_buf, buf, sizeof(buf), + &result) == 0); + assert(result != nullptr); + assert(getservbyport_r(htons(22), nullptr, &result_buf, buf, sizeof(buf), + &result) == 0); + assert(result != nullptr); + + assert(getservbyname_r("invalidhadfuiasdhi", nullptr, &result_buf, buf, + sizeof(buf), &result) == 0); + assert(result == nullptr); +} From 3392ff361a2005e79ae42f5199625255df0a3690 Mon Sep 17 00:00:00 2001 From: Florian Mayer Date: Fri, 28 Mar 2025 16:50:17 -0700 Subject: [PATCH 03/11] better errors Created using spr 1.3.4 --- .../TestCases/Linux/getservent_r.cpp | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp b/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp index a0444477a9ff0..9f9a4544a3db1 100644 --- a/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp +++ b/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp @@ -4,26 +4,37 @@ #include #include +#include #include +#include +#include +#include + +void CheckResult(int ret) { + if (ret != 0) { + fprintf(stderr, "%s\n", strerror(errno)); + abort(); + } +} int main(void) { struct servent result_buf; struct servent *result; char buf[1024]; - assert(getservent_r(&result_buf, buf, sizeof(buf), &result) == 0); + CheckResult(getservent_r(&result_buf, buf, sizeof(buf), &result)); assert(result != nullptr); // If these fail, check /etc/services if "ssh" exists. I picked this because // it should exist everywhere, if it doesn't, I am sorry. Disable the test // then please. - assert(getservbyname_r("ssh", nullptr, &result_buf, buf, sizeof(buf), - &result) == 0); + CheckResult( + getservbyname_r("ssh", nullptr, &result_buf, buf, sizeof(buf), &result)); assert(result != nullptr); - assert(getservbyport_r(htons(22), nullptr, &result_buf, buf, sizeof(buf), - &result) == 0); + CheckResult(getservbyport_r(htons(22), nullptr, &result_buf, buf, sizeof(buf), + &result)); assert(result != nullptr); - assert(getservbyname_r("invalidhadfuiasdhi", nullptr, &result_buf, buf, - sizeof(buf), &result) == 0); + CheckResult(getservbyname_r("invalidhadfuiasdhi", nullptr, &result_buf, buf, + sizeof(buf), &result)); assert(result == nullptr); } From 185bde8d101314e96393589311a1042d1179e97e Mon Sep 17 00:00:00 2001 From: Florian Mayer Date: Mon, 31 Mar 2025 10:51:33 -0700 Subject: [PATCH 04/11] test update Created using spr 1.3.4 --- .../test/sanitizer_common/TestCases/Linux/getservent_r.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp b/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp index 9f9a4544a3db1..73979ea1ecb5d 100644 --- a/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp +++ b/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp @@ -12,7 +12,7 @@ void CheckResult(int ret) { if (ret != 0) { - fprintf(stderr, "%s\n", strerror(errno)); + fprintf(stderr, "ERROR: %s\n", strerror(ret)); abort(); } } From b6967d4b0f2fa276847683b3b07636f0624c3002 Mon Sep 17 00:00:00 2001 From: Florian Mayer Date: Mon, 31 Mar 2025 12:33:05 -0700 Subject: [PATCH 05/11] fix test Created using spr 1.3.4 --- compiler-rt/lib/msan/tests/msan_test.cpp | 4 +++- .../test/sanitizer_common/TestCases/Linux/getservent_r.cpp | 5 +++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/compiler-rt/lib/msan/tests/msan_test.cpp b/compiler-rt/lib/msan/tests/msan_test.cpp index 8b7df85e0ff04..bf565dbc6a303 100644 --- a/compiler-rt/lib/msan/tests/msan_test.cpp +++ b/compiler-rt/lib/msan/tests/msan_test.cpp @@ -4916,7 +4916,9 @@ TEST(MemorySanitizer, getservent_r) { EXPECT_POISONED(result_buf); EXPECT_POISONED(result); EXPECT_POISONED(buf); - ASSERT_EQ(getservent_r(&result_buf, buf, sizeof(buf), &result), 0); + // This can fail with ENOENT, which we cannot control. + if (getservent_r(&result_buf, buf, sizeof(buf), &result) != 0) + return; EXPECT_NOT_POISONED(result); ASSERT_NE(result, nullptr); EXPECT_NOT_POISONED(result_buf); diff --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp b/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp index 73979ea1ecb5d..a2cc7784c49f1 100644 --- a/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp +++ b/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp @@ -21,8 +21,9 @@ int main(void) { struct servent result_buf; struct servent *result; char buf[1024]; - CheckResult(getservent_r(&result_buf, buf, sizeof(buf), &result)); - assert(result != nullptr); + // This can fail with ENOENT, which we cannot control. + if (getservent_r(&result_buf, buf, sizeof(buf), &result) == 0) + assert(result != nullptr); // If these fail, check /etc/services if "ssh" exists. I picked this because // it should exist everywhere, if it doesn't, I am sorry. Disable the test From c04c2e6ed0d7f4856647f9e7decaf0a0b437db38 Mon Sep 17 00:00:00 2001 From: Florian Mayer Date: Mon, 31 Mar 2025 13:05:51 -0700 Subject: [PATCH 06/11] test Created using spr 1.3.4 --- compiler-rt/lib/msan/tests/msan_test.cpp | 19 +++++++++++++++++-- .../sanitizer_common_interceptors.inc | 2 +- .../TestCases/Linux/getservent_r.cpp | 7 +++---- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/compiler-rt/lib/msan/tests/msan_test.cpp b/compiler-rt/lib/msan/tests/msan_test.cpp index bf565dbc6a303..38be48bdb116a 100644 --- a/compiler-rt/lib/msan/tests/msan_test.cpp +++ b/compiler-rt/lib/msan/tests/msan_test.cpp @@ -4917,8 +4917,7 @@ TEST(MemorySanitizer, getservent_r) { EXPECT_POISONED(result); EXPECT_POISONED(buf); // This can fail with ENOENT, which we cannot control. - if (getservent_r(&result_buf, buf, sizeof(buf), &result) != 0) - return; + ASSERT_EQ(getservent_r(&result_buf, buf, sizeof(buf), &result), 0); EXPECT_NOT_POISONED(result); ASSERT_NE(result, nullptr); EXPECT_NOT_POISONED(result_buf); @@ -4979,5 +4978,21 @@ TEST(MemorySanitizer, getservbyport_r) { EXPECT_NOT_POISONED(buf); } +TEST(MemorySanitizer, getservbyport_r_smallbuf) { + struct servent result_buf; + struct servent *result; + char buf[1]; + EXPECT_POISONED(result_buf); + EXPECT_POISONED(result); + EXPECT_POISONED(buf); + ASSERT_EQ(getservbyport_r(htons(22), nullptr, &result_buf, buf, sizeof(buf), + &result), + ERANGE); + EXPECT_NOT_POISONED(result); + ASSERT_EQ(result, nullptr); + EXPECT_POISONED(result_buf); + EXPECT_POISONED(buf); +} + #endif } // namespace diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc index 29b977c050579..5a15d75f0c86a 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -10285,9 +10285,9 @@ INTERCEPTOR(SSIZE_T, freadlink, int fd, char *buf, SIZE_T bufsiz) { UNUSED static void HandleGetServentReentrantResult( void *ctx, int res, struct __sanitizer_servent *result_buf, char *buf, SIZE_T buflen, struct __sanitizer_servent **result) { + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (char *)result, sizeof(void *)); if (res) return; - COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (char *)result, sizeof(void *)); if (*result) { COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (char *)*result, sizeof(__sanitizer_servent)); diff --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp b/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp index a2cc7784c49f1..950ee0f3e5fc4 100644 --- a/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp +++ b/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp @@ -21,10 +21,6 @@ int main(void) { struct servent result_buf; struct servent *result; char buf[1024]; - // This can fail with ENOENT, which we cannot control. - if (getservent_r(&result_buf, buf, sizeof(buf), &result) == 0) - assert(result != nullptr); - // If these fail, check /etc/services if "ssh" exists. I picked this because // it should exist everywhere, if it doesn't, I am sorry. Disable the test // then please. @@ -35,6 +31,9 @@ int main(void) { &result)); assert(result != nullptr); + CheckResult(getservent_r(&result_buf, buf, sizeof(buf), &result)); + assert(result != nullptr); + CheckResult(getservbyname_r("invalidhadfuiasdhi", nullptr, &result_buf, buf, sizeof(buf), &result)); assert(result == nullptr); From b36da7152fd2973420fde7aba54656db9ad44064 Mon Sep 17 00:00:00 2001 From: Florian Mayer Date: Mon, 31 Mar 2025 14:08:40 -0700 Subject: [PATCH 07/11] more test Created using spr 1.3.4 --- .../test/sanitizer_common/TestCases/Linux/getservent_r.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp b/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp index 950ee0f3e5fc4..5b8eebcf93835 100644 --- a/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp +++ b/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp @@ -13,8 +13,8 @@ void CheckResult(int ret) { if (ret != 0) { fprintf(stderr, "ERROR: %s\n", strerror(ret)); - abort(); } + assert(ret == 0); } int main(void) { From 59b018e745ca643f4cad0c307ac7afaa76fbbc9f Mon Sep 17 00:00:00 2001 From: Florian Mayer Date: Mon, 31 Mar 2025 15:43:10 -0700 Subject: [PATCH 08/11] test Created using spr 1.3.4 --- .../TestCases/Linux/getservent_r.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp b/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp index 5b8eebcf93835..b2074cb9a2fc5 100644 --- a/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp +++ b/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp @@ -10,13 +10,15 @@ #include #include -void CheckResult(int ret) { +void CheckResult(const char *file, int line, int ret) { if (ret != 0) { - fprintf(stderr, "ERROR: %s\n", strerror(ret)); + fprintf(stderr, "ERROR: %s:%d - %s\n", file, line, strerror(ret)); } assert(ret == 0); } +#define CHECK_RESULT(ret) CheckResult(__FILE__, __LINE__, ret) + int main(void) { struct servent result_buf; struct servent *result; @@ -24,17 +26,17 @@ int main(void) { // If these fail, check /etc/services if "ssh" exists. I picked this because // it should exist everywhere, if it doesn't, I am sorry. Disable the test // then please. - CheckResult( + CHECK_RESULT( getservbyname_r("ssh", nullptr, &result_buf, buf, sizeof(buf), &result)); assert(result != nullptr); - CheckResult(getservbyport_r(htons(22), nullptr, &result_buf, buf, sizeof(buf), - &result)); + CHECK_RESULT(getservbyport_r(htons(22), nullptr, &result_buf, buf, + sizeof(buf), &result)); assert(result != nullptr); - CheckResult(getservent_r(&result_buf, buf, sizeof(buf), &result)); + CHECK_RESULT(getservent_r(&result_buf, buf, sizeof(buf), &result)); assert(result != nullptr); - CheckResult(getservbyname_r("invalidhadfuiasdhi", nullptr, &result_buf, buf, - sizeof(buf), &result)); + CHECK_RESULT(getservbyname_r("invalidhadfuiasdhi", nullptr, &result_buf, buf, + sizeof(buf), &result)); assert(result == nullptr); } From aea1d954b479a061b8ba572f562d3cf33373c43a Mon Sep 17 00:00:00 2001 From: Florian Mayer Date: Mon, 31 Mar 2025 16:37:32 -0700 Subject: [PATCH 09/11] test Created using spr 1.3.4 --- compiler-rt/lib/msan/tests/msan_test.cpp | 1 - .../test/sanitizer_common/TestCases/Linux/getservent_r.cpp | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/compiler-rt/lib/msan/tests/msan_test.cpp b/compiler-rt/lib/msan/tests/msan_test.cpp index 38be48bdb116a..19c974c85e072 100644 --- a/compiler-rt/lib/msan/tests/msan_test.cpp +++ b/compiler-rt/lib/msan/tests/msan_test.cpp @@ -4916,7 +4916,6 @@ TEST(MemorySanitizer, getservent_r) { EXPECT_POISONED(result_buf); EXPECT_POISONED(result); EXPECT_POISONED(buf); - // This can fail with ENOENT, which we cannot control. ASSERT_EQ(getservent_r(&result_buf, buf, sizeof(buf), &result), 0); EXPECT_NOT_POISONED(result); ASSERT_NE(result, nullptr); diff --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp b/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp index b2074cb9a2fc5..7e16893da5e92 100644 --- a/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp +++ b/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp @@ -4,11 +4,12 @@ #include #include -#include +#include #include #include #include #include +#include void CheckResult(const char *file, int line, int ret) { if (ret != 0) { @@ -20,6 +21,7 @@ void CheckResult(const char *file, int line, int ret) { #define CHECK_RESULT(ret) CheckResult(__FILE__, __LINE__, ret) int main(void) { + assert(access("/etc/services", O_RDONLY) == 0); struct servent result_buf; struct servent *result; char buf[1024]; From 8f7d19a0a7f191a8b01497a4d2c4ba6ac8a60a52 Mon Sep 17 00:00:00 2001 From: Florian Mayer Date: Tue, 1 Apr 2025 13:15:16 -0700 Subject: [PATCH 10/11] requires Created using spr 1.3.4 --- .../test/sanitizer_common/TestCases/Linux/getservent_r.cpp | 2 +- compiler-rt/test/sanitizer_common/lit.common.cfg.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp b/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp index 7e16893da5e92..b356c0ed807f6 100644 --- a/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp +++ b/compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp @@ -1,6 +1,6 @@ // RUN: %clangxx -O0 %s -o %t && %run %t -// REQUIRES: glibc +// REQUIRES: glibc, netbase #include #include diff --git a/compiler-rt/test/sanitizer_common/lit.common.cfg.py b/compiler-rt/test/sanitizer_common/lit.common.cfg.py index 5406e8838f2fc..c3c1336bacd53 100644 --- a/compiler-rt/test/sanitizer_common/lit.common.cfg.py +++ b/compiler-rt/test/sanitizer_common/lit.common.cfg.py @@ -100,3 +100,6 @@ def build_invocation(compile_flags): if config.host_os == "NetBSD": config.substitutions.insert(0, ("%run", config.netbsd_noaslr_prefix)) + +if os.path.exists("/etc/services"): + config.available_features.add("netbase") From e8ec3587e44ebaf7830109455622874967aceb5a Mon Sep 17 00:00:00 2001 From: Florian Mayer Date: Tue, 1 Apr 2025 13:27:07 -0700 Subject: [PATCH 11/11] skip Created using spr 1.3.4 --- compiler-rt/lib/msan/tests/msan_test.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/compiler-rt/lib/msan/tests/msan_test.cpp b/compiler-rt/lib/msan/tests/msan_test.cpp index 19c974c85e072..d1c481483dfad 100644 --- a/compiler-rt/lib/msan/tests/msan_test.cpp +++ b/compiler-rt/lib/msan/tests/msan_test.cpp @@ -4910,6 +4910,8 @@ TEST(MemorySanitizer, timer_create) { } TEST(MemorySanitizer, getservent_r) { + if (access("/etc/services", O_RDONLY) != 0) + GTEST_SKIP() << "Missing /etc/services"; struct servent result_buf; struct servent *result; char buf[1024]; @@ -4924,6 +4926,8 @@ TEST(MemorySanitizer, getservent_r) { } TEST(MemorySanitizer, getservbyname_r) { + if (access("/etc/services", O_RDONLY) != 0) + GTEST_SKIP() << "Missing /etc/services"; struct servent result_buf; struct servent *result; char buf[1024]; @@ -4943,6 +4947,8 @@ TEST(MemorySanitizer, getservbyname_r) { } TEST(MemorySanitizer, getservbyname_r_unknown) { + if (access("/etc/services", O_RDONLY) != 0) + GTEST_SKIP() << "Missing /etc/services"; struct servent result_buf; struct servent *result; char buf[1024]; @@ -4959,6 +4965,8 @@ TEST(MemorySanitizer, getservbyname_r_unknown) { } TEST(MemorySanitizer, getservbyport_r) { + if (access("/etc/services", O_RDONLY) != 0) + GTEST_SKIP() << "Missing /etc/services"; struct servent result_buf; struct servent *result; char buf[1024]; @@ -4978,6 +4986,8 @@ TEST(MemorySanitizer, getservbyport_r) { } TEST(MemorySanitizer, getservbyport_r_smallbuf) { + if (access("/etc/services", O_RDONLY) != 0) + GTEST_SKIP() << "Missing /etc/services"; struct servent result_buf; struct servent *result; char buf[1];