Skip to content

Commit 086365b

Browse files
MaskRaytru
authored andcommitted
[test] Make Linux/sem_init_glibc.cpp robust
and fix it for 32-bit ports defining sem_init@GLIBC_2.0 (i386, mips32, powerpc32) for glibc>=2.36. Fix llvm#58079 Reviewed By: mgorny Differential Revision: https://reviews.llvm.org/D135023 (cherry picked from commit 6f46ff3)
1 parent 4a2c05b commit 086365b

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6703,7 +6703,7 @@ INTERCEPTOR(int, sem_init, __sanitizer_sem_t *s, int pshared, unsigned value) {
67036703
COMMON_INTERCEPTOR_ENTER(ctx, sem_init, s, pshared, value);
67046704
// Workaround a bug in glibc's "old" semaphore implementation by
67056705
// zero-initializing the sem_t contents. This has to be done here because
6706-
// interceptors bind to the lowest symbols version by default, hitting the
6706+
// interceptors bind to the lowest version before glibc 2.36, hitting the
67076707
// buggy code path while the non-sanitized build of the same code works fine.
67086708
REAL(memset)(s, 0, sizeof(*s));
67096709
int res = REAL(sem_init)(s, pshared, value);

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

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,36 @@
11
// RUN: %clangxx -O0 -g %s -lutil -o %t && %run %t
22
// This test depends on the glibc layout of struct sem_t and checks that we
33
// don't leave sem_t::private uninitialized.
4-
// UNSUPPORTED: android, lsan-x86, ubsan, target-is-mips64, target-is-mips64el
4+
// UNSUPPORTED: android, lsan-x86, ubsan
55
#include <features.h>
66
#include <assert.h>
77
#include <semaphore.h>
88
#include <string.h>
99
#include <stdint.h>
1010

11-
// On powerpc64be semval_t must be 64 bits even with "old" versions of glibc.
12-
#if __PPC64__ && __BIG_ENDIAN__
13-
typedef uint64_t semval_t;
14-
15-
// This condition needs to correspond to __HAVE_64B_ATOMICS macro in glibc.
16-
#elif (defined(__x86_64__) || defined(__aarch64__) || defined(__powerpc64__) || \
17-
defined(__s390x__) || defined(__sparc64__) || defined(__alpha__) || \
18-
defined(__ia64__) || defined(__m68k__)) && __GLIBC_PREREQ(2, 21)
19-
typedef uint64_t semval_t;
20-
#else
11+
// musl and glibc's __HAVE_64B_ATOMICS==0 ports (e.g. arm, i386) use 32-bit sem
12+
// values. 64-bit glibc ports defining sem_init@GLIBC_2.0 (mips64) use 32-bit as
13+
// well, if the sem_init interceptor picks the oldest versioned symbol
14+
// (glibc<2.36, see https://sourceware.org/PR14932).
15+
#if !defined(__GLIBC__) || defined(__ILP32__) || \
16+
!__GLIBC_PREREQ(2, 36) && defined(__mips64__)
2117
typedef unsigned semval_t;
18+
#else
19+
typedef uint64_t semval_t;
2220
#endif
2321

24-
// glibc 2.21 has introduced some changes in the way the semaphore value is
25-
// handled for 32-bit platforms, but since these changes are not ABI-breaking
26-
// they are not versioned. On newer platforms such as ARM, there is only one
27-
// version of the symbol, so it's enough to check the glibc version. However,
28-
// for old platforms such as i386, glibc contains two or even three versions of
29-
// the sem_init symbol, and the sanitizers always pick the oldest one.
30-
// Therefore, it is not enough to rely on the __GLIBC_PREREQ macro - we should
31-
// instead check the platform as well to make sure we only expect the new
32-
// behavior on platforms where the older symbols do not exist.
33-
#if defined(__arm__) && __GLIBC_PREREQ(2, 21)
34-
#define GET_SEM_VALUE(V) ((V) >> 1)
22+
// glibc __HAVE_64B_ATOMICS==0 ports define a sem_init which shifts the value by
23+
// 1 (https://sourceware.org/PR12674 glibc 2.21). The version is picked if
24+
// either glibc>=2.36 or sem_init@GLIBC_2.0 is absent (arm and newer ports).
25+
//
26+
// The __GLIBC_PREREQ check is brittle in that it requires matched
27+
// __GLIBC_PREREQ values for build time and run time.
28+
#if defined(__GLIBC__) && defined(__ILP32__) && \
29+
(__GLIBC_PREREQ(2, 36) || (__GLIBC_PREREQ(2, 21) && !defined(__i386__) && \
30+
!defined(__mips__) && !defined(__powerpc__)))
31+
# define GET_SEM_VALUE(V) ((V) >> 1)
3532
#else
36-
#define GET_SEM_VALUE(V) (V)
33+
# define GET_SEM_VALUE(V) (V)
3734
#endif
3835

3936
void my_sem_init(bool priv, int value, semval_t *a, unsigned char *b) {

0 commit comments

Comments
 (0)