You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: libc/config/config.json
+10Lines changed: 10 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -75,6 +75,16 @@
75
75
"LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE": {
76
76
"value": 1073741824,
77
77
"doc": "Default size for the constinit freelist buffer used for the freelist malloc implementation (default 1o 1GB)."
78
+
},
79
+
},
80
+
"unistd": {
81
+
"LIBC_CONF_ENABLE_TID_CACHE": {
82
+
"value": true,
83
+
"doc": "Enable caching mechanism for gettid to avoid syscall (only effective in fullbuild mode, default to true). Please refer to Undefined Behavior documentation for implications."
84
+
},
85
+
"LIBC_CONF_ENABLE_PID_CACHE": {
86
+
"value": true,
87
+
"doc": "Enable caching mechanism for getpid to avoid syscall (default to true). Please refer to Undefined Behavior documentation for implications."
Copy file name to clipboardExpand all lines: libc/docs/configure.rst
+3Lines changed: 3 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -52,3 +52,6 @@ to learn about the defaults for your platform and target.
52
52
* **"string" options**
53
53
- ``LIBC_CONF_MEMSET_X86_USE_SOFTWARE_PREFETCHING``: Inserts prefetch for write instructions (PREFETCHW) for memset on x86 to recover performance when hardware prefetcher is disabled.
54
54
- ``LIBC_CONF_STRING_UNSAFE_WIDE_READ``: Read more than a byte at a time to perform byte-string operations like strlen.
55
+
* **"unistd" options**
56
+
- ``LIBC_CONF_ENABLE_PID_CACHE``: Enable caching mechanism for getpid to avoid syscall (default to true). Please refer to Undefined Behavior documentation for implications.
57
+
- ``LIBC_CONF_ENABLE_TID_CACHE``: Enable caching mechanism for gettid to avoid syscall (only effective in fullbuild mode, default to true). Please refer to Undefined Behavior documentation for implications.
Copy file name to clipboardExpand all lines: libc/docs/dev/undefined_behavior.rst
+23Lines changed: 23 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -93,3 +93,26 @@ direction in this case.
93
93
Non-const Constant Return Values
94
94
--------------------------------
95
95
Some libc functions, like ``dlerror()``, return ``char *`` instead of ``const char *`` and then tell the caller they promise not to to modify this value. Any modification of this value is undefined behavior.
96
+
97
+
Cached ``getpid/gettid``
98
+
------------------------
99
+
Since version ``2.25``, glibc removes its cache mechanism for ``getpid/gettid``
100
+
(See the history section in https://man7.org/linux/man-pages/man2/getpid.2.html).
101
+
LLVM's libc still implements the cache as it is useful for fast deadlock detection.
102
+
The cache mechanism is also implemented in MUSL and bionic. The tid/pid cache can
103
+
be disabled by setting ``LIBC_CONF_ENABLE_TID_CACHE`` and ``LIBC_CONF_ENABLE_PID_CACHE``
104
+
to ``false`` respectively.
105
+
106
+
Unwrapped ``SYS_clone/SYS_fork/SYS_vfork``
107
+
------------------------------------------
108
+
It is highly discouraged to use unwrapped ``SYS_clone/SYS_fork/SYS_vfork``.
109
+
First, calling such syscalls without provided libc wrappers ignores
110
+
all the ``pthread_atfork`` entries as libc can no longer detect the ``fork``.
111
+
Second, libc relies on the ``fork/clone`` wrappers to correctly maintain cache for
112
+
process id and thread id, and other important process-specific states such as the list
113
+
of robust mutexes. Third, even if the user is to call ``exec*`` functions immediately,
114
+
there can still be other unexpected issues. For instance, there can be signal handlers
115
+
inherited from parent process triggered inside the instruction window between ``fork``
116
+
and ``exec*``. As libc failed to maintain its internal states correctly, even though the
117
+
functions used inside the signal handlers are marked as ``async-signal-safe`` (such as
118
+
``getpid``), they will still return wrong values or lead to other even worse situations.
0 commit comments