5863 psiginfo arguments should be const
[unleashed.git] / usr / src / lib / libc / README
blobcad02342104c6b5b82c6a13840050cd3faba44bc
2 # CDDL HEADER START
4 # The contents of this file are subject to the terms of the
5 # Common Development and Distribution License (the "License").
6 # You may not use this file except in compliance with the License.
8 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 # or http://www.opensolaris.org/os/licensing.
10 # See the License for the specific language governing permissions
11 # and limitations under the License.
13 # When distributing Covered Code, include this CDDL HEADER in each
14 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 # If applicable, add the following below this CDDL HEADER, with the
16 # fields enclosed by brackets "[]" replaced with your own identifying
17 # information: Portions Copyright [yyyy] [name of copyright owner]
19 # CDDL HEADER END
22 # Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23 # Use is subject to license terms.
26 # Process Model Unification
28 The Solaris Process Model Unification project:
29         PSARC/2002/117 Solaris Process Model Unification
30         4470917 Solaris Process Model Unification
31 folded libthread into libc and has led to some fundamental changes
32 in the rules by which code in libc must be developed and maintained.
34 All code in libc must be both MT-Safe and Fork-Safe
35 and where possible (almost everywhere), Async-Signal-Safe.
37 To this end, the following rules should be followed:
39 Almost all internal libc locks (mutexes and read-write locks)
40 should be acquired and released via these interfaces:
42         mutex_t some_lock = DEFAULTMUTEX;
44         lmutex_lock(&some_lock);
45         ... do something critical ...
46         lmutex_unlock(&some_lock);
48         rwlock_t some_rw_lock = DEFAULTRWLOCK;
50         lrw_rdlock(&some_rw_lock);
51         ... multiple threads can do something ...
52         lrw_unlock(&some_rw_lock);
54         lrw_wrlock(&some_rw_lock);
55         ... only one thread can do something ...
56         lrw_unlock(&some_rw_lock);
58 The above l* versions of the mutex and rwlock interfaces do more
59 than the ordinary interfaces:  They define critical regions in
60 which the calling thread cannot be suspended (making the region
61 fork-safe) and in which the calling thread has all signals deferred
62 (making the region async-signal-safe).
64 However, certain rules apply to the code within these critical regions:
66         - The code must be of guaranteed short duration; no calls
67           to interfaces that might block indefinitely are allowed.
68           This means no calls into stdio or syslog() and no calls
69           to cond_wait() unless there is a guarantee of an almost-
70           immediate call to cond_signal() or cond_broadcast()
71           from elsewhere.
73         - The code cannot call any non-l* synchronization
74           primitives (mutex_lock(), _private_mutex_lock(),
75           rw_wrlock(), rw_rdlock(), sema_wait(), etc.)
77         - The code cannot call any functions outside of libc,
78           including application callbacks and functions from
79           dlopen()ed objects, such as those in the I18N code.
81         - Because malloc(), calloc(), realloc(), and free()
82           are designed to be interposed upon, they fall into
83           the previous case of prohibition.  None of these can
84           be called by a thread while in a critical region.
86 There is a private memory allocator for use internally to libc.
87 It cannot be interposed upon and it is safe to use while in
88 a critical region (or for that matter while not in a critical
89 region; it is async-signal-safe and fork-safe):
91         void *lmalloc(size_t);
92         void lfree(void *, size_t);
94         void *libc_malloc(size_t);
95         void *libc_realloc(void *, size_t);
96         char *libc_strdup(const char *);
97         void libc_free(void *);
99 lmalloc() and lfree() are the basic interfaces.  The libc_*()
100 variants are built on top of lmalloc()/lfree() but they have
101 the same interface signatures as the corresponding functions
102 without the 'libc_' prefix.  lmalloc() and libc_malloc()
103 return zeroed memory blocks.  Note that lmalloc()/lfree()
104 require the caller to remember the size parameter passed
105 to lmalloc() and to pass the same value to lfree().
107 Memory allocated by lmalloc() can only be freed by lfree().
108 Memory allocated by libc_malloc(), libc_realloc(), or libc_strdup()
109 can only be freed by libc_free().  Never pass such allocated
110 memory out of libc if the caller of libc is expected to free it.
112 lmalloc()/lfree() is a small and simple power of two allocator.
113 Do not use it as a general-purpose allocator.  Be kind to it.
115 There is a special mutual exclusion interface that exists for
116 cases, like code in the I18N interfaces, where mutual exclusion
117 is required but the above rules cannot be followed:
119         int fork_lock_enter(const char *);
120         void fork_lock_exit(void);
122 fork_lock_enter() does triple-duty.  Not only does it serialize
123 calls to fork() and forkall(), but it also serializes calls to
124 thr_suspend() (fork() and forkall() also suspend other threads),
125 and furthermore it serializes I18N calls to functions in other
126 dlopen()ed L10N objects that might be calling malloc()/free().
127 Use it in general like this:
129         (void) fork_lock_enter(NULL);
130         ... serialized; do something that might call malloc ...
131         fork_lock_exit();
133 The 'const char *' argument to fork_lock_enter() should always
134 be NULL except for two special cases:
135         - When called from fork() or forkall()
136         - When called from pthread_atfork()
137 This enforces the prohibition against calling fork() or pthread_atfork()
138 from a pthread_atfork()-registered fork handler function while a fork()
139 prologue or epilogue is in progress.  If _THREAD_ERROR_DETECTION is set
140 to 1 or 2 in the environment, such cases will draw a nasty message and
141 will dump core if _THREAD_ERROR_DETECTION=2.  fork_lock_enter() returns
142 non-zero only if it is called from a fork handler.  This is of interest
143 only to callers that have to do something about this condition; the
144 return value should be ignored in all other cases (fork_lock_enter()
145 never actually fails).
147 It is an error to call fork_lock_enter() while in a critical region
148 (that is, while holding any internal libc lock).
150 On return from fork_lock_enter(), no internal libc locks are held
151 but a flag has been set to cause other callers of fork_lock_enter()
152 to delay (via _cond_wait()) until fork_lock_exit() is called.
154 These are the rules to follow for memory allocation:
156   - If a function acquires an internal libc lock or is called while
157     an internal libc lock is held:
159         * The malloc family cannot be used.
161         * lmalloc or libc_malloc should be used.  The memory must
162           be released by lfree or libc_free, respectively.
164         * lfree takes an argument to tell the size of the releasing
165           memory.  If the function does not know the size at the
166           releasing point, libc_malloc and libc_free should be used.
168         * As the memory allocated by lmalloc or libc_malloc needs
169           to be released by lfree or libc_free and these are internal
170           to libc, they cannot be used to allocate memory that might
171           be released by application code outside libc.
173         * If the memory allocation by malloc() cannot be avoided and
174           the scalability of the function does not matter much, the
175           function can be serialized with fork_lock_enter() instead
176           of lmutex_lock().
178         * If the memory allocation by malloc() cannot be avoided and
179           the scalability of the function does matter, another
180           implementation of the function will be necessary.
182 In a DEBUG build of libc:
183         make THREAD_DEBUG=-DTHREAD_DEBUG install
184 many of these rules are enforced by ASSERT() statements scattered about
185 in the libc sources.  This is the default mode for building libc when
186 a DEBUG nightly build is performed.
188 # libaio/librt Implementation In libc
190 The putback of the project:
191         6416832 libaio and librt can and should be folded into libc
192 introduced several libc-private locking interfaces:
193         void    sig_mutex_lock(mutex_t *);
194         void    sig_mutex_unlock(mutex_t *);
195         int     sig_mutex_trylock(mutex_t *);
196         int     sig_cond_wait(cond_t *, mutex_t *);
197         int     sig_cond_reltimedwait(cond_t *, mutex_t *, const timespec_t *);
198 which are declared in both "thr_uberdata.h" and "mtlib.h".
200 They are used in specialized code in libc, like the asynchronous i/o code.
201 Unlike the lmutex_lock() and lmutex_unlock() interfaces described above,
202 these interfaces do not define critical regions, but signals are
203 deferred while locks acquired by these functions are held, making
204 their use be async-signal safe.  Calls to malloc(), calloc(), realloc(),
205 and free() are permissible while holding such locks.
207 These interfaces were brought over from code in the former libaio
208 and librt and are necessary because, where they are used, the code
209 must execute potentially long-term waits and must be cancelable.
210 sig_cond_wait() and sig_cond_reltimedwait() are cancellation points.
212 These interfaces are available for other uses inside libc, as
213 the need arises.  (There is no need if the code does not perform
214 long-term waits.)  Just follow a few rules to be self-consistent:
215  - Don't mix calls to mutex_[un]lock(), lmutex_[un]lock() and
216    sig_mutex_[un]lock() on the same mutex.
217  - Don't call cond_wait() with a mutex acquired by sig_mutex_lock();
218    call sig_cond_wait() or sig_cond_reltimedwait().
219  - Use pthread_cleanup_push() and pthread_cleanup_pop() to make
220    your code cancellation-safe.
221  - The sig_*() interfaces are not in themselves fork-safe.
222    You have to employ other logic to make your code fork-safe.
223    See the tail of postfork1_child() for examples.
225 # Removal Of Synonym Symbols
227 The following project:
228     PSARC 2008/309 expunge synonyms.h
229     6700179 expunge synonyms.h
231 Removed the historical "synonym" symbols from the C Library.
233 Historically, for every public function symbol in the C library a second,
234 private, symbol of the same value was defined to be used internally by libc
235 (generally, one or the other will be a weak symbol, precisely which is
236 inconsistent).
238 These synonym symbols existed such that an application which provided
239 otherwise conflicting symbols could interpose on the version in libc without
240 compromising libc itself, that is if libc defines fopen() which needs open() it
241 would call _open() and an application defined open() would not cause fopen()
242 to break.  This was made transparent to code within libc via a header,
243 synonyms.h, which would #define open _open, for all such symbols. 
245 Since ON now uses direct bindings extensively all symbols not explicitly
246 marked "NODIRECT" are directly bound within libc anyway, and none of this is
247 actually necessary.  Thus synonyms.h was removed, and no new synonym symbols
248 need be added.  However, unfortunately, certain of the private symbols were
249 inadvertently exposed to applications, and several are known to have been
250 used, thus these existing synonyms must continue to exist to maintain
251 compatibility.  A preloadable library, /lib/c_synonyms.so.1 is provided which
252 also provides the historical names with underscore prefixes to allow any other
253 incorrect application to continue to function.
255 It should never be necessary to add additional synonym symbols to libc nor to
256 add underscore prefixed aliases to c_synonyms.so.1.
258 # libc Internals Scoped Protected
260 The integration of the fix for:
261         6689238 libc needs global protection against ld.so.1
263 Scopes all function symbols within libc protected, excepting those that need
264 to be accepting of interposition and to have a consistent version called both
265 within and without libc (basically, the malloc() family).
267 This causes references by libc to itself to be permanently and unavoidably
268 bound, and thus to never enter ld.so (and potentially from there audit
269 libraries or other such support libraries).  This maintains an otherwise
270 complicated to verify invariant: within critical sections (with any internal
271 lock held, etc) execution can never leave the context of libc.  Previously
272 this was done with a selection of known-to-be-problematic functions having
273 weak synonyms scoped private, but this was both difficult to verify, difficult
274 to remember, and thus always at least somewhat incomplete.
276 In summary, any new function symbol in libc must be scoped protected unless it
277 is one of a very small group of functions that must allow interposed versions
278 to be bound to from the C library itself -- it is grossly unlikely that more
279 of these will occur.