Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions compiler-rt/lib/msan/tests/msan_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
68 changes: 68 additions & 0 deletions compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -10604,4 +10669,7 @@ static void InitializeCommonInterceptors() {
INIT_FREADLINK;

INIT___PRINTF_CHK;
INIT_GETSERVENT_R;
INIT_GETSERVBYNAME_R;
INIT_GETSERVBYPORT_R;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// RUN: %clangxx -O0 %s -o %t && %run %t

// REQUIRES: glibc

#include <arpa/inet.h>
#include <assert.h>
#include <netdb.h>

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);
}