aboutsummaryrefslogtreecommitdiff
path: root/man/man3/needstack.3
blob: 31588f681fd05f704892cc779096bd5d9a09ca46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
.TH NEEDSTACK 3
.SH NAME
needstack \- check for execution stack overflow
.SH SYNOPSIS
.B
#include <u.h>
.PP
.B
#include <libc.h>
.PP
.B
int	needstack(int n)
.SH DESCRIPTION
Stack overflow in the thread library leads to bugs that are
difficult to diagnose.
The Plan 9 libraries are careful about not allocating
large structures on the stack, so typically four or eight kilobytes is plenty of stack
for a thread.
Other libraries are not always as careful.
Calling
.I needstack
indicates to the thread library that an external routine is about
to be called that will require
.I n
bytes of stack space.
If there is not enough space left on the stack,
the thread library prints an error and terminates
the program.
The call
.B needstack(0)
can be used to check whether the stack is
currently overflowed.
.PP
.I Needstack
is defined in
.B libc.h
so that library functions used in threaded and non-threaded contexts
can call it.
The implementation of
.I needstack
in
.B lib9
is a no-op.
.PP
.I Needstack
should be thought of as a comment checked at run time,
like
.IR assert (3).
.SH EXAMPLE
The X Window library implementation of
.I XLookupString
allocates some very large buffers on the stack, so
.B /usr/local/plan9/src/libdraw/x11-itrans.c
calls
.B needstack(20*1024)
before making calls to
.IR XLookupString .
If a thread (in this case, the keyboard-reading thread used
inside the
.IR draw (3)
library)
does not allocate a large enough stack, the problem is diagnosed
immediately rather than left to corrupt memory.
.SH SOURCE
.B /usr/local/plan9/src/lib9/needstack.c
.br
.B /usr/local/plan9/src/libthread
.SH SEE ALSO
.IR thread (3)