Skip to content

Commit 5bb1ce8

Browse files
authored
[NFC][sanitizer] Make GetLibcVersion void (llvm#108915)
It can't fail if guarded with SANITIZER_GLIBC.
1 parent 1c0958b commit 5bb1ce8

File tree

1 file changed

+53
-57
lines changed

1 file changed

+53
-57
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp

Lines changed: 53 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -200,21 +200,6 @@ bool SetEnv(const char *name, const char *value) {
200200
}
201201
# endif
202202

203-
__attribute__((unused)) static bool GetLibcVersion(int *major, int *minor,
204-
int *patch) {
205-
# if SANITIZER_GLIBC
206-
const char *p = gnu_get_libc_version();
207-
*major = internal_simple_strtoll(p, &p, 10);
208-
// Caller does not expect anything else.
209-
CHECK_EQ(*major, 2);
210-
*minor = (*p == '.') ? internal_simple_strtoll(p + 1, &p, 10) : 0;
211-
*patch = (*p == '.') ? internal_simple_strtoll(p + 1, &p, 10) : 0;
212-
return true;
213-
# else
214-
return false;
215-
# endif
216-
}
217-
218203
// True if we can use dlpi_tls_data. glibc before 2.25 may leave NULL (BZ
219204
// #19826) so dlpi_tls_data cannot be used.
220205
//
@@ -224,11 +209,22 @@ __attribute__((unused)) static bool GetLibcVersion(int *major, int *minor,
224209
__attribute__((unused)) static int g_use_dlpi_tls_data;
225210

226211
# if SANITIZER_GLIBC && !SANITIZER_GO
212+
213+
static void GetGLibcVersion(int *major, int *minor, int *patch) {
214+
const char *p = gnu_get_libc_version();
215+
*major = internal_simple_strtoll(p, &p, 10);
216+
// Caller does not expect anything else.
217+
CHECK_EQ(*major, 2);
218+
*minor = (*p == '.') ? internal_simple_strtoll(p + 1, &p, 10) : 0;
219+
*patch = (*p == '.') ? internal_simple_strtoll(p + 1, &p, 10) : 0;
220+
}
221+
227222
__attribute__((unused)) static size_t g_tls_size;
223+
228224
void InitTlsSize() {
229225
int major, minor, patch;
230-
g_use_dlpi_tls_data =
231-
GetLibcVersion(&major, &minor, &patch) && major == 2 && minor >= 25;
226+
GetGLibcVersion(&major, &minor, &patch);
227+
g_use_dlpi_tls_data = major == 2 && minor >= 25;
232228

233229
# if defined(__aarch64__) || defined(__x86_64__) || \
234230
defined(__powerpc64__) || defined(__loongarch__)
@@ -250,53 +246,53 @@ static atomic_uintptr_t thread_descriptor_size;
250246

251247
// FIXME: Implementation is very GLIBC specific, but it's used by FREEBSD.
252248
static uptr ThreadDescriptorSizeFallback() {
253-
# if defined(__x86_64__) || defined(__i386__) || defined(__arm__)
249+
# if defined(__x86_64__) || defined(__i386__) || defined(__arm__) || \
250+
SANITIZER_RISCV64
251+
# if SANITIZER_GLIBC
254252
int major;
255253
int minor;
256254
int patch;
257-
if (GetLibcVersion(&major, &minor, &patch) && major == 2) {
258-
/* sizeof(struct pthread) values from various glibc versions. */
259-
if (SANITIZER_X32)
260-
return 1728; // Assume only one particular version for x32.
261-
// For ARM sizeof(struct pthread) changed in Glibc 2.23.
262-
if (SANITIZER_ARM)
263-
return minor <= 22 ? 1120 : 1216;
264-
if (minor <= 3)
265-
return FIRST_32_SECOND_64(1104, 1696);
266-
if (minor == 4)
267-
return FIRST_32_SECOND_64(1120, 1728);
268-
if (minor == 5)
269-
return FIRST_32_SECOND_64(1136, 1728);
270-
if (minor <= 9)
271-
return FIRST_32_SECOND_64(1136, 1712);
272-
if (minor == 10)
273-
return FIRST_32_SECOND_64(1168, 1776);
274-
if (minor == 11 || (minor == 12 && patch == 1))
275-
return FIRST_32_SECOND_64(1168, 2288);
276-
if (minor <= 14)
277-
return FIRST_32_SECOND_64(1168, 2304);
278-
if (minor < 32) // Unknown version
279-
return FIRST_32_SECOND_64(1216, 2304);
280-
// minor == 32
281-
return FIRST_32_SECOND_64(1344, 2496);
282-
}
255+
GetGLibcVersion(&major, &minor, &patch);
256+
# else // SANITIZER_GLIBC
283257
return 0;
258+
# endif // SANITIZER_GLIBC
259+
# endif
260+
261+
# if defined(__x86_64__) || defined(__i386__) || defined(__arm__)
262+
/* sizeof(struct pthread) values from various glibc versions. */
263+
if (SANITIZER_X32)
264+
return 1728; // Assume only one particular version for x32.
265+
// For ARM sizeof(struct pthread) changed in Glibc 2.23.
266+
if (SANITIZER_ARM)
267+
return minor <= 22 ? 1120 : 1216;
268+
if (minor <= 3)
269+
return FIRST_32_SECOND_64(1104, 1696);
270+
if (minor == 4)
271+
return FIRST_32_SECOND_64(1120, 1728);
272+
if (minor == 5)
273+
return FIRST_32_SECOND_64(1136, 1728);
274+
if (minor <= 9)
275+
return FIRST_32_SECOND_64(1136, 1712);
276+
if (minor == 10)
277+
return FIRST_32_SECOND_64(1168, 1776);
278+
if (minor == 11 || (minor == 12 && patch == 1))
279+
return FIRST_32_SECOND_64(1168, 2288);
280+
if (minor <= 14)
281+
return FIRST_32_SECOND_64(1168, 2304);
282+
if (minor < 32) // Unknown version
283+
return FIRST_32_SECOND_64(1216, 2304);
284+
// minor == 32
285+
return FIRST_32_SECOND_64(1344, 2496);
284286
# endif
285287

286288
# if SANITIZER_RISCV64
287-
int major;
288-
int minor;
289-
int patch;
290-
if (GetLibcVersion(&major, &minor, &patch) && major == 2) {
291-
// TODO: consider adding an optional runtime check for an unknown (untested)
292-
// glibc version
293-
if (minor <= 28) // WARNING: the highest tested version is 2.29
294-
return 1772; // no guarantees for this one
295-
if (minor <= 31)
296-
return 1772; // tested against glibc 2.29, 2.31
297-
return 1936; // tested against glibc 2.32
298-
}
299-
return 0;
289+
// TODO: consider adding an optional runtime check for an unknown (untested)
290+
// glibc version
291+
if (minor <= 28) // WARNING: the highest tested version is 2.29
292+
return 1772; // no guarantees for this one
293+
if (minor <= 31)
294+
return 1772; // tested against glibc 2.29, 2.31
295+
return 1936; // tested against glibc 2.32
300296
# endif
301297

302298
# if defined(__s390__) || defined(__sparc__)

0 commit comments

Comments
 (0)