Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
[dfsan] Make sprintf interceptor compatible with glibc 2.37+ and musl…
Browse files Browse the repository at this point in the history
… (#78363)

snprintf interceptors call `format_buffer` with `size==~0ul`, which
may eventually lead to `snprintf(s, n, "Hello world!")` where `s+n`
wraps around. Since glibc 2.37 (https://sourceware.org/PR30441), the
snprintf call does not write the last char. musl snprintf returns -1
with EOVERFLOW when `n > INT_MAX`.

Change `size` to INT_MAX to work with glibc 2.37+ and musl.
snprintf interceptors are not changed. It's user responsibility to not
cause a compatibility issue with libc implementations.

Fix #60678
  • Loading branch information
MaskRay authored Jan 18, 2024
1 parent 430a40d commit 67e0f41
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 9 deletions.
6 changes: 3 additions & 3 deletions compiler-rt/lib/dfsan/dfsan_custom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2792,7 +2792,7 @@ int __dfsw_sprintf(char *str, const char *format, dfsan_label str_label,
va_list ap;
va_start(ap, ret_label);

int ret = format_buffer(str, ~0ul, format, va_labels, ret_label, nullptr,
int ret = format_buffer(str, INT32_MAX, format, va_labels, ret_label, nullptr,
nullptr, ap);
va_end(ap);
return ret;
Expand All @@ -2806,8 +2806,8 @@ int __dfso_sprintf(char *str, const char *format, dfsan_label str_label,
dfsan_origin *ret_origin, ...) {
va_list ap;
va_start(ap, ret_origin);
int ret = format_buffer(str, ~0ul, format, va_labels, ret_label, va_origins,
ret_origin, ap);
int ret = format_buffer(str, INT32_MAX, format, va_labels, ret_label,
va_origins, ret_origin, ap);
va_end(ap);
return ret;
}
Expand Down
3 changes: 0 additions & 3 deletions compiler-rt/test/dfsan/custom.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// https://github.com/llvm/llvm-project/issues/60678
// XFAIL: glibc-2.37

// RUN: %clang_dfsan %s -o %t && DFSAN_OPTIONS="strict_data_dependencies=0" %run %t
// RUN: %clang_dfsan -DSTRICT_DATA_DEPENDENCIES %s -o %t && %run %t
// RUN: %clang_dfsan -DORIGIN_TRACKING -mllvm -dfsan-track-origins=1 -mllvm -dfsan-combine-pointer-labels-on-load=false -DSTRICT_DATA_DEPENDENCIES %s -o %t && %run %t
Expand Down
3 changes: 0 additions & 3 deletions compiler-rt/test/dfsan/release_shadow_space.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// https://github.com/llvm/llvm-project/issues/60678
// XFAIL: glibc-2.37

// DFSAN_OPTIONS=no_huge_pages_for_shadow=false RUN: %clang_dfsan %s -o %t && %run %t
// DFSAN_OPTIONS=no_huge_pages_for_shadow=true RUN: %clang_dfsan %s -o %t && %run %t
// DFSAN_OPTIONS=no_huge_pages_for_shadow=false RUN: %clang_dfsan %s -DORIGIN_TRACKING -mllvm -dfsan-track-origins=1 -o %t && %run %t
Expand Down

0 comments on commit 67e0f41

Please sign in to comment.