Skip to content

Commit cf38afe

Browse files
committed
Move interceptors for libresolv functions to MSan
The functions are not relevant for most sanitizers and only required for MSan to see which regions have been written to. This eliminates a link dependency for all other sanitizers and fixes #59007: while `-lresolv` had been added for the static runtime in 6dce56b, it wasn't added to the shared runtimes. Instead of just moving the interceptors, we adapt them to MSan conventions: * We don't skip intercepting when `msan_init_is_running` is true, but directly call ENSURE_MSAN_INITED() like most other interceptors. It seems unlikely that these functions are called during initialization. * We don't unpoison `errno`, because none of the functions is specified to use it.
1 parent 94c6dd6 commit cf38afe

File tree

6 files changed

+86
-132
lines changed

6 files changed

+86
-132
lines changed

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1410,7 +1410,7 @@ void tools::linkSanitizerRuntimeDeps(const ToolChain &TC,
14101410
// libresolv.a, even if exists, is an empty archive to satisfy POSIX -lresolv
14111411
// requirement.
14121412
if (TC.getTriple().isOSLinux() && !TC.getTriple().isAndroid() &&
1413-
!TC.getTriple().isMusl())
1413+
!TC.getTriple().isMusl() && TC.getSanitizerArgs(Args).needsMsanRt())
14141414
CmdArgs.push_back("-lresolv");
14151415
}
14161416

compiler-rt/lib/msan/msan_interceptors.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,79 @@ INTERCEPTOR(int, forkpty, int *aparent, char *name, const void *termp,
13581358
#define MSAN_MAYBE_INTERCEPT_FORKPTY
13591359
#endif
13601360

1361+
#if SANITIZER_LINUX && !SANITIZER_ANDROID
1362+
INTERCEPTOR(int, __b64_ntop, unsigned char const *src, SIZE_T srclength,
1363+
char *target, SIZE_T targsize) {
1364+
ENSURE_MSAN_INITED();
1365+
CHECK_UNPOISONED(src, srclength);
1366+
InterceptorScope interceptor_scope;
1367+
int res = REAL(__b64_ntop)(src, srclength, target, targsize);
1368+
if (res >= 0)
1369+
__msan_unpoison(target, res + 1);
1370+
return res;
1371+
}
1372+
INTERCEPTOR(int, __b64_pton, char const *src, char *target, SIZE_T targsize) {
1373+
ENSURE_MSAN_INITED();
1374+
CHECK_UNPOISONED(src, internal_strlen(src) + 1);
1375+
InterceptorScope interceptor_scope;
1376+
int res = REAL(__b64_pton)(src, target, targsize);
1377+
if (res >= 0)
1378+
__msan_unpoison(target, res);
1379+
return res;
1380+
}
1381+
# define MSAN_MAYBE_INTERCEPT___B64_TO \
1382+
MSAN_INTERCEPT_FUNC(__b64_ntop); \
1383+
COMMON_INTERCEPT_FUNCTION(__b64_pton);
1384+
#else
1385+
# define MSAN_MAYBE_INTERCEPT___B64_TO
1386+
#endif
1387+
1388+
#if SANITIZER_LINUX && !SANITIZER_ANDROID
1389+
# if __GLIBC_PREREQ(2, 34)
1390+
// Changed with https://sourceware.org/git/?p=glibc.git;h=640bbdf
1391+
# define DN_COMP_INTERCEPTOR_NAME dn_comp
1392+
# define DN_EXPAND_INTERCEPTOR_NAME dn_expand
1393+
# else
1394+
# define DN_COMP_INTERCEPTOR_NAME __dn_comp
1395+
# define DN_EXPAND_INTERCEPTOR_NAME __dn_expand
1396+
# endif
1397+
INTERCEPTOR(int, DN_COMP_INTERCEPTOR_NAME, unsigned char *exp_dn,
1398+
unsigned char *comp_dn, int length, unsigned char **dnptrs,
1399+
unsigned char **lastdnptr) {
1400+
ENSURE_MSAN_INITED();
1401+
InterceptorScope interceptor_scope;
1402+
int res = REAL(DN_COMP_INTERCEPTOR_NAME)(exp_dn, comp_dn, length, dnptrs,
1403+
lastdnptr);
1404+
if (res >= 0) {
1405+
__msan_unpoison(comp_dn, res);
1406+
if (dnptrs && lastdnptr) {
1407+
unsigned char **p = dnptrs;
1408+
for (; p != lastdnptr && *p; ++p);
1409+
if (p != lastdnptr)
1410+
++p;
1411+
__msan_unpoison(dnptrs, (p - dnptrs) * sizeof(*p));
1412+
}
1413+
}
1414+
return res;
1415+
}
1416+
INTERCEPTOR(int, DN_EXPAND_INTERCEPTOR_NAME, unsigned char const *base,
1417+
unsigned char const *end, unsigned char const *src, char *dest,
1418+
int space) {
1419+
ENSURE_MSAN_INITED();
1420+
// TODO: add read check if __dn_comp intercept added
1421+
InterceptorScope interceptor_scope;
1422+
int res = REAL(DN_EXPAND_INTERCEPTOR_NAME)(base, end, src, dest, space);
1423+
if (res >= 0)
1424+
__msan_unpoison(dest, internal_strlen(dest) + 1);
1425+
return res;
1426+
}
1427+
# define MSAN_MAYBE_INTERCEPT_DN_COMP_EXPAND \
1428+
MSAN_INTERCEPT_FUNC(DN_COMP_INTERCEPTOR_NAME); \
1429+
MSAN_INTERCEPT_FUNC(DN_EXPAND_INTERCEPTOR_NAME);
1430+
#else
1431+
# define MSAN_MAYBE_INTERCEPT_DN_COMP_EXPAND
1432+
#endif
1433+
13611434
struct MSanInterceptorContext {
13621435
bool in_interceptor_scope;
13631436
};
@@ -1916,6 +1989,9 @@ void InitializeInterceptors() {
19161989
MSAN_MAYBE_INTERCEPT_OPENPTY;
19171990
MSAN_MAYBE_INTERCEPT_FORKPTY;
19181991

1992+
MSAN_MAYBE_INTERCEPT___B64_TO;
1993+
MSAN_MAYBE_INTERCEPT_DN_COMP_EXPAND;
1994+
19191995
inited = 1;
19201996
}
19211997
} // namespace __msan

compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -2538,82 +2538,6 @@ INTERCEPTOR(int, glob64, const char *pattern, int flags,
25382538
#define INIT_GLOB64
25392539
#endif // SANITIZER_INTERCEPT_GLOB64
25402540

2541-
#if SANITIZER_INTERCEPT___B64_TO
2542-
INTERCEPTOR(int, __b64_ntop, unsigned char const *src, SIZE_T srclength,
2543-
char *target, SIZE_T targsize) {
2544-
void *ctx;
2545-
COMMON_INTERCEPTOR_ENTER(ctx, __b64_ntop, src, srclength, target, targsize);
2546-
COMMON_INTERCEPTOR_READ_RANGE(ctx, src, srclength);
2547-
int res = REAL(__b64_ntop)(src, srclength, target, targsize);
2548-
if (res >= 0)
2549-
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, target, res + 1);
2550-
return res;
2551-
}
2552-
INTERCEPTOR(int, __b64_pton, char const *src, char *target, SIZE_T targsize) {
2553-
void *ctx;
2554-
COMMON_INTERCEPTOR_ENTER(ctx, __b64_pton, src, target, targsize);
2555-
COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
2556-
int res = REAL(__b64_pton)(src, target, targsize);
2557-
if (res >= 0)
2558-
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, target, res);
2559-
return res;
2560-
}
2561-
#define INIT___B64_TO \
2562-
COMMON_INTERCEPT_FUNCTION(__b64_ntop); \
2563-
COMMON_INTERCEPT_FUNCTION(__b64_pton);
2564-
#else // SANITIZER_INTERCEPT___B64_TO
2565-
#define INIT___B64_TO
2566-
#endif // SANITIZER_INTERCEPT___B64_TO
2567-
2568-
#if SANITIZER_INTERCEPT_DN_COMP_EXPAND
2569-
# if __GLIBC_PREREQ(2, 34)
2570-
// Changed with https://sourceware.org/git/?p=glibc.git;h=640bbdf
2571-
# define DN_COMP_INTERCEPTOR_NAME dn_comp
2572-
# define DN_EXPAND_INTERCEPTOR_NAME dn_expand
2573-
# else
2574-
# define DN_COMP_INTERCEPTOR_NAME __dn_comp
2575-
# define DN_EXPAND_INTERCEPTOR_NAME __dn_expand
2576-
# endif
2577-
INTERCEPTOR(int, DN_COMP_INTERCEPTOR_NAME, unsigned char *exp_dn,
2578-
unsigned char *comp_dn, int length, unsigned char **dnptrs,
2579-
unsigned char **lastdnptr) {
2580-
void *ctx;
2581-
COMMON_INTERCEPTOR_ENTER(ctx, DN_COMP_INTERCEPTOR_NAME, exp_dn, comp_dn,
2582-
length, dnptrs, lastdnptr);
2583-
int res = REAL(DN_COMP_INTERCEPTOR_NAME)(exp_dn, comp_dn, length, dnptrs,
2584-
lastdnptr);
2585-
if (res >= 0) {
2586-
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, comp_dn, res);
2587-
if (dnptrs && lastdnptr) {
2588-
unsigned char **p = dnptrs;
2589-
for (; p != lastdnptr && *p; ++p)
2590-
;
2591-
if (p != lastdnptr)
2592-
++p;
2593-
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dnptrs, (p - dnptrs) * sizeof(*p));
2594-
}
2595-
}
2596-
return res;
2597-
}
2598-
INTERCEPTOR(int, DN_EXPAND_INTERCEPTOR_NAME, unsigned char const *base,
2599-
unsigned char const *end, unsigned char const *src, char *dest,
2600-
int space) {
2601-
void *ctx;
2602-
COMMON_INTERCEPTOR_ENTER(ctx, DN_EXPAND_INTERCEPTOR_NAME, base, end, src,
2603-
dest, space);
2604-
// TODO: add read check if __dn_comp intercept added
2605-
int res = REAL(DN_EXPAND_INTERCEPTOR_NAME)(base, end, src, dest, space);
2606-
if (res >= 0)
2607-
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, internal_strlen(dest) + 1);
2608-
return res;
2609-
}
2610-
# define INIT_DN_COMP_EXPAND \
2611-
COMMON_INTERCEPT_FUNCTION(DN_COMP_INTERCEPTOR_NAME); \
2612-
COMMON_INTERCEPT_FUNCTION(DN_EXPAND_INTERCEPTOR_NAME);
2613-
#else // SANITIZER_INTERCEPT_DN_COMP_EXPAND
2614-
# define INIT_DN_COMP_EXPAND
2615-
#endif // SANITIZER_INTERCEPT_DN_COMP_EXPAND
2616-
26172541
#if SANITIZER_INTERCEPT_POSIX_SPAWN
26182542

26192543
template <class RealSpawnPtr>
@@ -10346,8 +10270,6 @@ static void InitializeCommonInterceptors() {
1034610270
INIT_TIMESPEC_GET;
1034710271
INIT_GLOB;
1034810272
INIT_GLOB64;
10349-
INIT___B64_TO;
10350-
INIT_DN_COMP_EXPAND;
1035110273
INIT_POSIX_SPAWN;
1035210274
INIT_WAIT;
1035310275
INIT_WAIT4;

compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,6 @@ SANITIZER_WEAK_IMPORT void *aligned_alloc(__sanitizer::usize __alignment,
266266
#define SANITIZER_INTERCEPT_TIMESPEC_GET SI_LINUX
267267
#define SANITIZER_INTERCEPT_GLOB (SI_GLIBC || SI_SOLARIS)
268268
#define SANITIZER_INTERCEPT_GLOB64 SI_GLIBC
269-
#define SANITIZER_INTERCEPT___B64_TO SI_LINUX_NOT_ANDROID
270-
#define SANITIZER_INTERCEPT_DN_COMP_EXPAND SI_LINUX_NOT_ANDROID
271269
#define SANITIZER_INTERCEPT_POSIX_SPAWN SI_POSIX
272270
#define SANITIZER_INTERCEPT_WAIT SI_POSIX
273271
#define SANITIZER_INTERCEPT_INET SI_POSIX

compiler-rt/test/sanitizer_common/TestCases/Linux/dn_expand.cpp renamed to compiler-rt/test/msan/Linux/dn_expand.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <resolv.h>
55
#include <string.h>
66

7-
#include "sanitizer_common/sanitizer_specific.h"
7+
#include <sanitizer/msan_interface.h>
88

99
void testWrite() {
1010
char unsigned input[] = {0xff, 0xc5, 0xf7, 0xff, 0x00, 0x00, 0xff, 0x0a, 0x00,
@@ -18,7 +18,7 @@ void testWrite() {
1818

1919
assert(res == 12);
2020
assert(strcmp(output, "google\\.com") == 0);
21-
check_mem_is_good(output, strlen(output) + 1);
21+
__msan_check_mem_is_initialized(output, strlen(output) + 1);
2222
}
2323

2424
void testWriteZeroLength() {
@@ -33,7 +33,7 @@ void testWriteZeroLength() {
3333

3434
assert(res == 1);
3535
assert(strcmp(output, "") == 0);
36-
check_mem_is_good(output, strlen(output) + 1);
36+
__msan_check_mem_is_initialized(output, strlen(output) + 1);
3737
}
3838

3939
void testComp() {
@@ -49,17 +49,17 @@ void testComp() {
4949
char unsigned *n1 = mb;
5050
int res = dn_comp("llvm.org", mb, me - mb, pb, pe);
5151
assert(res == 10);
52-
check_mem_is_good(mb, res);
52+
__msan_check_mem_is_initialized(mb, res);
5353
// pb is [msg, llvm.org, nullptr]
54-
check_mem_is_good(pb, sizeof(*pb) * 3);
54+
__msan_check_mem_is_initialized(pb, sizeof(*pb) * 3);
5555
mb += res;
5656

5757
char unsigned *n2 = mb;
5858
res = dn_comp("lab.llvm.org", mb, me - mb, pb, pe);
5959
assert(res == 6);
60-
check_mem_is_good(mb, res);
60+
__msan_check_mem_is_initialized(mb, res);
6161
// pb is [msg, llvm.org, lab.llvm.org, nullptr]
62-
check_mem_is_good(pb, sizeof(*pb) * 4);
62+
__msan_check_mem_is_initialized(pb, sizeof(*pb) * 4);
6363
mb += res;
6464

6565
{
@@ -69,7 +69,7 @@ void testComp() {
6969
fprintf(stderr, "%d\n", res);
7070
assert(res == 10);
7171
assert(strcmp(output, "llvm.org") == 0);
72-
check_mem_is_good(output, strlen(output) + 1);
72+
__msan_check_mem_is_initialized(output, strlen(output) + 1);
7373
}
7474

7575
{
@@ -78,7 +78,7 @@ void testComp() {
7878

7979
assert(res == 6);
8080
assert(strcmp(output, "lab.llvm.org") == 0);
81-
check_mem_is_good(output, strlen(output) + 1);
81+
__msan_check_mem_is_initialized(output, strlen(output) + 1);
8282
}
8383
}
8484

compiler-rt/test/sanitizer_common/TestCases/Linux/b64.cpp

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)