Skip to content

Commit ec68dc1

Browse files
authored
[compiler-rt] Work around incompatible Windows definitions of (S)SIZE_T
The interceptor types are supposed to match size_t (and the non-Windows ssize_t) exactly, but on 32-bit Windows `size_t` uses `unsigned int` whereas `SIZE_T` is `unsigned long`. The current definition results in `uptr` not matching `uintptr_t` since we otherwise get typedef redefinition errors. Work around this by using a #define instead of a typedef when defining SIZE_T. It would probably be cleaner to stop using these uppercase types, but that is a rather invasive change and this one is the minimal change to allow uptr to match uintptr_t on Windows. To ensure this compiles on Windows, we also remove the interceptor.h defines of uptr (that do not always match __sanitizer::uptr) and rely on __sanitizer::uptr instead. The interceptor types most likely predate those other types so clean up the unnecessary definition while here. This also reverts commit 18e06e3 and commit bb27dd8. Reviewed By: mstorsjo, vitalybuka Pull Request: #106311
1 parent dac1f7b commit ec68dc1

File tree

3 files changed

+34
-33
lines changed

3 files changed

+34
-33
lines changed

compiler-rt/lib/interception/interception.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,19 @@
2525

2626
// These typedefs should be used only in the interceptor definitions to replace
2727
// the standard system types (e.g. SSIZE_T instead of ssize_t)
28-
typedef __sanitizer::uptr SIZE_T;
29-
typedef __sanitizer::sptr SSIZE_T;
28+
// On Windows the system headers (basetsd.h) provide a conflicting definition
29+
// of SIZE_T/SSIZE_T that do not match the real size_t/ssize_t for 32-bit
30+
// systems (using long instead of the expected int). Work around the typedef
31+
// redefinition by #defining SIZE_T instead of using a typedef.
32+
// TODO: We should be using __sanitizer::usize (and a new ssize) instead of
33+
// these new macros as long as we ensure they match the real system definitions.
34+
#if SANITIZER_WINDOWS
35+
// Ensure that (S)SIZE_T were already defined as we are about to override them.
36+
# include <basetsd.h>
37+
#endif
38+
39+
#define SIZE_T __sanitizer::usize
40+
#define SSIZE_T __sanitizer::sptr
3041
typedef __sanitizer::sptr PTRDIFF_T;
3142
typedef __sanitizer::s64 INTMAX_T;
3243
typedef __sanitizer::u64 UINTMAX_T;
@@ -338,16 +349,8 @@ const interpose_substitution substitution_##func_name[] \
338349
#endif
339350

340351
// ISO C++ forbids casting between pointer-to-function and pointer-to-object,
341-
// so we use casting via an integral type __interception::uptr,
342-
// assuming that system is POSIX-compliant. Using other hacks seem
343-
// challenging, as we don't even pass function type to
344-
// INTERCEPT_FUNCTION macro, only its name.
352+
// so we use casts via uintptr_t (the local __sanitizer::uptr equivalent).
345353
namespace __interception {
346-
#if defined(_WIN64)
347-
typedef unsigned long long uptr;
348-
#else
349-
typedef unsigned long uptr;
350-
#endif // _WIN64
351354

352355
#if defined(__ELF__) && !SANITIZER_FUCHSIA
353356
// The use of interceptors makes many sanitizers unusable for static linking.

compiler-rt/lib/interception/interception_type_test.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,35 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "interception.h"
15+
#include "sanitizer_common/sanitizer_type_traits.h"
1516

16-
#if SANITIZER_LINUX || SANITIZER_APPLE
17-
18-
#include <sys/types.h>
17+
#if __has_include(<sys/types.h>)
18+
# include <sys/types.h>
19+
#endif
1920
#include <stddef.h>
2021
#include <stdint.h>
2122

22-
COMPILER_CHECK(sizeof(::SIZE_T) == sizeof(size_t));
23-
COMPILER_CHECK(sizeof(::SSIZE_T) == sizeof(ssize_t));
24-
COMPILER_CHECK(sizeof(::PTRDIFF_T) == sizeof(ptrdiff_t));
23+
COMPILER_CHECK((__sanitizer::is_same<__sanitizer::uptr, ::uintptr_t>::value));
24+
COMPILER_CHECK((__sanitizer::is_same<__sanitizer::sptr, ::intptr_t>::value));
25+
COMPILER_CHECK((__sanitizer::is_same<__sanitizer::usize, ::size_t>::value));
26+
COMPILER_CHECK((__sanitizer::is_same<::PTRDIFF_T, ::ptrdiff_t>::value));
27+
COMPILER_CHECK((__sanitizer::is_same<::SIZE_T, ::size_t>::value));
28+
#if !SANITIZER_WINDOWS
29+
// No ssize_t on Windows.
30+
COMPILER_CHECK((__sanitizer::is_same<::SSIZE_T, ::ssize_t>::value));
31+
#endif
32+
// TODO: These are not actually the same type on Linux (long vs long long)
2533
COMPILER_CHECK(sizeof(::INTMAX_T) == sizeof(intmax_t));
34+
COMPILER_CHECK(sizeof(::UINTMAX_T) == sizeof(uintmax_t));
2635

27-
# if SANITIZER_GLIBC || SANITIZER_ANDROID
36+
#if SANITIZER_GLIBC || SANITIZER_ANDROID
2837
COMPILER_CHECK(sizeof(::OFF64_T) == sizeof(off64_t));
29-
# endif
38+
#endif
3039

3140
// The following are the cases when pread (and friends) is used instead of
3241
// pread64. In those cases we need OFF_T to match off_t. We don't care about the
3342
// rest (they depend on _FILE_OFFSET_BITS setting when building an application).
34-
# if SANITIZER_ANDROID || !defined _FILE_OFFSET_BITS || \
35-
_FILE_OFFSET_BITS != 64
43+
#if !SANITIZER_WINDOWS && (SANITIZER_ANDROID || !defined _FILE_OFFSET_BITS || \
44+
_FILE_OFFSET_BITS != 64)
3645
COMPILER_CHECK(sizeof(::OFF_T) == sizeof(off_t));
37-
# endif
38-
3946
#endif

compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ namespace __sanitizer {
143143
typedef unsigned long long uptr;
144144
typedef signed long long sptr;
145145
#else
146-
# if (SANITIZER_WORDSIZE == 64) || SANITIZER_APPLE || SANITIZER_WINDOWS
146+
# if (SANITIZER_WORDSIZE == 64) || SANITIZER_APPLE
147147
typedef unsigned long uptr;
148148
typedef signed long sptr;
149149
# else
@@ -194,16 +194,7 @@ typedef u64 OFF64_T;
194194
#ifdef __SIZE_TYPE__
195195
typedef __SIZE_TYPE__ usize;
196196
#else
197-
// Since we use this for operator new, usize must match the real size_t, but on
198-
// 32-bit Windows the definition of uptr does not actually match uintptr_t or
199-
// size_t because we are working around typedef mismatches for the (S)SIZE_T
200-
// types used in interception.h.
201-
// Until the definition of uptr has been fixed we have to special case Win32.
202-
# if SANITIZER_WINDOWS && SANITIZER_WORDSIZE == 32
203-
typedef unsigned int usize;
204-
# else
205197
typedef uptr usize;
206-
# endif
207198
#endif
208199

209200
typedef u64 tid_t;

0 commit comments

Comments
 (0)