Merge "Make LD_DEBUG more user-friendly." into main
diff --git a/docs/fdtrack.md b/docs/fdtrack.md
index 07c69b3..8928a5c 100644
--- a/docs/fdtrack.md
+++ b/docs/fdtrack.md
@@ -4,9 +4,11 @@
fdtrack is a file descriptor leak checker added to Android in API level 30.
-fdtrack consists of two parts: a set of hooks in bionic to register a callback
-that's invoked on file descriptor operations, and a library that implements a
-hook to perform and store backtraces for file descriptor creation.
+fdtrack consists of several parts: a set of hooks in bionic to register a
+callback that's invoked on file descriptor operations, a library that implements
+a hook to perform and store backtraces for file descriptor creation, and
+code in frameworks to automatically enable it (and deliberately crash a process
+that's leaking).
### bionic hooks
bionic provides a header in the `bionic_libc_platform_headers` header_lib at <[bionic/fdtrack.h](https://android.googlesource.com/platform/bionic/+/refs/heads/main/libc/platform/bionic/fdtrack.h)>.
@@ -21,6 +23,28 @@
[libfdtrack](https://android.googlesource.com/platform/bionic/+/refs/heads/main/libfdtrack)
implements a library that uses libunwindstack to unwind and store fd creation backtraces.
+### frameworks
+As the name implies, `spawnFdLeakCheckThread` in SystemServer spawns a thread
+to monitor the number of open file descriptors every so often.
+If that passes a certain threshold, fdtrack is enabled.
+If it passes another threshold, the process is aborted.
+These thresholds are configurable via system properties:
+```
+ // Number of open file descriptors before fdtrack starts; default 1600.
+ private static final String SYSPROP_FDTRACK_ENABLE_THRESHOLD =
+ "persist.sys.debug.fdtrack_enable_threshold";
+
+ // Number of open file descriptors before aborting; default 3000.
+ private static final String SYSPROP_FDTRACK_ABORT_THRESHOLD =
+ "persist.sys.debug.fdtrack_abort_threshold";
+
+ // Number of seconds between open fd count checks; default 120s.
+ private static final String SYSPROP_FDTRACK_INTERVAL =
+ "persist.sys.debug.fdtrack_interval";
+```
+Note that it's also possible to monitor the number of open file descriptors for
+a given process from the shell. `adb shell watch ls -l /proc/<pid>/fd` will show
+them (and you can choose your own update rate as an argument to `watch`).
#### Using libfdtrack
libfdtrack registers its hook upon being loaded, so to start capturing
diff --git a/libc/Android.bp b/libc/Android.bp
index d450f41..776f101 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -1686,8 +1686,7 @@
llndk: {
symbol_file: "libc.map.txt",
export_headers_as_system: true,
- export_preprocessed_headers: ["include"],
- export_llndk_headers: ["libc_uapi_headers"],
+ export_llndk_headers: ["libc_headers"],
},
}
@@ -2330,17 +2329,6 @@
name: "libc",
symbol_file: "libc.map.txt",
first_version: "9",
- export_header_libs: [
- "common_libc",
- "libc_uapi",
- "libc_kernel_android_uapi_linux",
- "libc_kernel_android_scsi",
- "libc_asm_arm",
- "libc_asm_arm64",
- "libc_asm_riscv64",
- "libc_asm_x86",
- "libc_asm_x86_64",
- ],
}
ndk_library {
diff --git a/libc/bionic/pthread_mutex.cpp b/libc/bionic/pthread_mutex.cpp
index 9b37225..0a452e9 100644
--- a/libc/bionic/pthread_mutex.cpp
+++ b/libc/bionic/pthread_mutex.cpp
@@ -182,7 +182,12 @@
return 0;
}
if (ret == EBUSY) {
- ScopedTrace trace("Contending for pthread mutex");
+ char trace_msg[64];
+ const pid_t owner = atomic_load_explicit(&mutex.owner_tid, memory_order_relaxed)
+ & FUTEX_TID_MASK;
+ snprintf(trace_msg, sizeof(trace_msg),
+ "Contending for pthread mutex owned by tid: %d", owner);
+ ScopedTrace trace(trace_msg);
ret = -__futex_pi_lock_ex(&mutex.owner_tid, mutex.shared, use_realtime_clock, abs_timeout);
}
return ret;
diff --git a/libc/include/bits/getentropy.h b/libc/include/bits/getentropy.h
index 4cd44f7..b9be4d1 100644
--- a/libc/include/bits/getentropy.h
+++ b/libc/include/bits/getentropy.h
@@ -48,6 +48,6 @@
*
* See also arc4random_buf() which is available in all API levels.
*/
-int getentropy(void* _Nonnull __buffer, size_t __buffer_size) __wur __INTRODUCED_IN(28);
+__wur int getentropy(void* _Nonnull __buffer, size_t __buffer_size) __INTRODUCED_IN(28);
__END_DECLS
diff --git a/libc/include/malloc.h b/libc/include/malloc.h
index 5904519..77b5b02 100644
--- a/libc/include/malloc.h
+++ b/libc/include/malloc.h
@@ -55,7 +55,7 @@
* other processes. Obviously this is not the case for apps, which will
* be killed in preference to killing other processes.
*/
-void* _Nullable malloc(size_t __byte_count) __mallocfunc __BIONIC_ALLOC_SIZE(1) __wur;
+__wur void* _Nullable malloc(size_t __byte_count) __mallocfunc __BIONIC_ALLOC_SIZE(1);
/**
* [calloc(3)](https://man7.org/linux/man-pages/man3/calloc.3.html) allocates
@@ -64,7 +64,7 @@
* Returns a pointer to the allocated memory on success and returns a null
* pointer and sets `errno` on failure (but see the notes for malloc()).
*/
-void* _Nullable calloc(size_t __item_count, size_t __item_size) __mallocfunc __BIONIC_ALLOC_SIZE(1,2) __wur;
+__wur void* _Nullable calloc(size_t __item_count, size_t __item_size) __mallocfunc __BIONIC_ALLOC_SIZE(1,2);
/**
* [realloc(3)](https://man7.org/linux/man-pages/man3/realloc.3.html) resizes
@@ -74,11 +74,8 @@
* memory on success and returns a null pointer and sets `errno` on failure
* (but see the notes for malloc()).
*/
-void* _Nullable realloc(void* _Nullable __ptr, size_t __byte_count) __BIONIC_ALLOC_SIZE(2) __wur;
+__wur void* _Nullable realloc(void* _Nullable __ptr, size_t __byte_count) __BIONIC_ALLOC_SIZE(2);
-// Remove the explicit guard once //external/giflib has been fixed so that it no
-// longer provides a conflicting definition: http://b/352784252
-#if __ANDROID_API__ >= 29
/**
* [reallocarray(3)](https://man7.org/linux/man-pages/man3/realloc.3.html) resizes
* allocated memory on the heap.
@@ -90,7 +87,18 @@
* memory on success and returns a null pointer and sets `errno` on failure
* (but see the notes for malloc()).
*/
-void* _Nullable reallocarray(void* _Nullable __ptr, size_t __item_count, size_t __item_size) __BIONIC_ALLOC_SIZE(2, 3) __wur __INTRODUCED_IN(29);
+#if __ANDROID_API__ >= 29
+__wur void* _Nullable reallocarray(void* _Nullable __ptr, size_t __item_count, size_t __item_size) __BIONIC_ALLOC_SIZE(2, 3) __INTRODUCED_IN(29);
+#else
+#include <errno.h>
+static __inline __wur void* _Nullable reallocarray(void* _Nullable __ptr, size_t __item_count, size_t __item_size) {
+ size_t __new_size;
+ if (__builtin_mul_overflow(__item_count, __item_size, &__new_size)) {
+ errno = ENOMEM;
+ return NULL;
+ }
+ return realloc(__ptr, __new_size);
+}
#endif
/**
@@ -108,13 +116,13 @@
*
* See also posix_memalign().
*/
-void* _Nullable memalign(size_t __alignment, size_t __byte_count) __mallocfunc __BIONIC_ALLOC_SIZE(2) __wur;
+__wur void* _Nullable memalign(size_t __alignment, size_t __byte_count) __mallocfunc __BIONIC_ALLOC_SIZE(2);
/**
* [malloc_usable_size(3)](https://man7.org/linux/man-pages/man3/malloc_usable_size.3.html)
* returns the actual size of the given heap block.
*/
-size_t malloc_usable_size(const void* _Nullable __ptr) __wur;
+__wur size_t malloc_usable_size(const void* _Nullable __ptr);
#define __MALLINFO_BODY \
/** Total number of non-mmapped bytes currently allocated from OS. */ \
diff --git a/libc/include/sys/random.h b/libc/include/sys/random.h
index fcea419..6aba1fc 100644
--- a/libc/include/sys/random.h
+++ b/libc/include/sys/random.h
@@ -52,6 +52,6 @@
*
* See also arc4random_buf() which is available in all API levels.
*/
-ssize_t getrandom(void* _Nonnull __buffer, size_t __buffer_size, unsigned int __flags) __wur __INTRODUCED_IN(28);
+__wur ssize_t getrandom(void* _Nonnull __buffer, size_t __buffer_size, unsigned int __flags) __INTRODUCED_IN(28);
__END_DECLS
diff --git a/libdl/Android.bp b/libdl/Android.bp
index 1bbd902..87db4b1 100644
--- a/libdl/Android.bp
+++ b/libdl/Android.bp
@@ -123,7 +123,6 @@
},
apex_available: [
- "//apex_available:platform",
"com.android.runtime",
],
}
@@ -170,7 +169,6 @@
},
apex_available: [
- "//apex_available:platform",
"com.android.runtime",
],
}
diff --git a/libm/Android.bp b/libm/Android.bp
index 9fd79f8..ee86959 100644
--- a/libm/Android.bp
+++ b/libm/Android.bp
@@ -435,7 +435,6 @@
},
apex_available: [
- "//apex_available:platform",
"com.android.runtime",
],
diff --git a/tests/prebuilt-elf-files/arm64/libtest_invalid-rw_load_segment.so b/tests/prebuilt-elf-files/arm64/libtest_invalid-rw_load_segment.so
index 6463c6b..46af37f 100755
--- a/tests/prebuilt-elf-files/arm64/libtest_invalid-rw_load_segment.so
+++ b/tests/prebuilt-elf-files/arm64/libtest_invalid-rw_load_segment.so
Binary files differ
diff --git a/tests/prebuilt-elf-files/x86_64/libtest_invalid-rw_load_segment.so b/tests/prebuilt-elf-files/x86_64/libtest_invalid-rw_load_segment.so
index 113e455..9d2c5f1 100755
--- a/tests/prebuilt-elf-files/x86_64/libtest_invalid-rw_load_segment.so
+++ b/tests/prebuilt-elf-files/x86_64/libtest_invalid-rw_load_segment.so
Binary files differ