setjmp.texi 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. @node Non-Local Exits, Signal Handling, Resource Usage And Limitation, Top
  2. @c %MENU% Jumping out of nested function calls
  3. @chapter Non-Local Exits
  4. @cindex non-local exits
  5. @cindex long jumps
  6. Sometimes when your program detects an unusual situation inside a deeply
  7. nested set of function calls, you would like to be able to immediately
  8. return to an outer level of control. This section describes how you can
  9. do such @dfn{non-local exits} using the @code{setjmp} and @code{longjmp}
  10. functions.
  11. @menu
  12. * Intro: Non-Local Intro. When and how to use these facilities.
  13. * Details: Non-Local Details. Functions for non-local exits.
  14. * Non-Local Exits and Signals:: Portability issues.
  15. * System V contexts:: Complete context control a la System V.
  16. @end menu
  17. @node Non-Local Intro, Non-Local Details, , Non-Local Exits
  18. @section Introduction to Non-Local Exits
  19. As an example of a situation where a non-local exit can be useful,
  20. suppose you have an interactive program that has a ``main loop'' that
  21. prompts for and executes commands. Suppose the ``read'' command reads
  22. input from a file, doing some lexical analysis and parsing of the input
  23. while processing it. If a low-level input error is detected, it would
  24. be useful to be able to return immediately to the ``main loop'' instead
  25. of having to make each of the lexical analysis, parsing, and processing
  26. phases all have to explicitly deal with error situations initially
  27. detected by nested calls.
  28. (On the other hand, if each of these phases has to do a substantial
  29. amount of cleanup when it exits---such as closing files, deallocating
  30. buffers or other data structures, and the like---then it can be more
  31. appropriate to do a normal return and have each phase do its own
  32. cleanup, because a non-local exit would bypass the intervening phases and
  33. their associated cleanup code entirely. Alternatively, you could use a
  34. non-local exit but do the cleanup explicitly either before or after
  35. returning to the ``main loop''.)
  36. In some ways, a non-local exit is similar to using the @samp{return}
  37. statement to return from a function. But while @samp{return} abandons
  38. only a single function call, transferring control back to the point at
  39. which it was called, a non-local exit can potentially abandon many
  40. levels of nested function calls.
  41. You identify return points for non-local exits by calling the function
  42. @code{setjmp}. This function saves information about the execution
  43. environment in which the call to @code{setjmp} appears in an object of
  44. type @code{jmp_buf}. Execution of the program continues normally after
  45. the call to @code{setjmp}, but if an exit is later made to this return
  46. point by calling @code{longjmp} with the corresponding @w{@code{jmp_buf}}
  47. object, control is transferred back to the point where @code{setjmp} was
  48. called. The return value from @code{setjmp} is used to distinguish
  49. between an ordinary return and a return made by a call to
  50. @code{longjmp}, so calls to @code{setjmp} usually appear in an @samp{if}
  51. statement.
  52. Here is how the example program described above might be set up:
  53. @smallexample
  54. @include setjmp.c.texi
  55. @end smallexample
  56. The function @code{abort_to_main_loop} causes an immediate transfer of
  57. control back to the main loop of the program, no matter where it is
  58. called from.
  59. The flow of control inside the @code{main} function may appear a little
  60. mysterious at first, but it is actually a common idiom with
  61. @code{setjmp}. A normal call to @code{setjmp} returns zero, so the
  62. ``else'' clause of the conditional is executed. If
  63. @code{abort_to_main_loop} is called somewhere within the execution of
  64. @code{do_command}, then it actually appears as if the @emph{same} call
  65. to @code{setjmp} in @code{main} were returning a second time with a value
  66. of @code{-1}.
  67. @need 250
  68. So, the general pattern for using @code{setjmp} looks something like:
  69. @smallexample
  70. if (setjmp (@var{buffer}))
  71. /* @r{Code to clean up after premature return.} */
  72. @dots{}
  73. else
  74. /* @r{Code to be executed normally after setting up the return point.} */
  75. @dots{}
  76. @end smallexample
  77. @node Non-Local Details, Non-Local Exits and Signals, Non-Local Intro, Non-Local Exits
  78. @section Details of Non-Local Exits
  79. Here are the details on the functions and data structures used for
  80. performing non-local exits. These facilities are declared in
  81. @file{setjmp.h}.
  82. @pindex setjmp.h
  83. @deftp {Data Type} jmp_buf
  84. @standards{ISO, setjmp.h}
  85. Objects of type @code{jmp_buf} hold the state information to
  86. be restored by a non-local exit. The contents of a @code{jmp_buf}
  87. identify a specific place to return to.
  88. @end deftp
  89. @deftypefn Macro int setjmp (jmp_buf @var{state})
  90. @standards{ISO, setjmp.h}
  91. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  92. @c _setjmp ok
  93. @c __sigsetjmp(!savemask) ok
  94. @c __sigjmp_save(!savemask) ok, does not call sigprocmask
  95. When called normally, @code{setjmp} stores information about the
  96. execution state of the program in @var{state} and returns zero. If
  97. @code{longjmp} is later used to perform a non-local exit to this
  98. @var{state}, @code{setjmp} returns a nonzero value.
  99. @end deftypefn
  100. @deftypefun void longjmp (jmp_buf @var{state}, int @var{value})
  101. @standards{ISO, setjmp.h}
  102. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuplugin{} @asucorrupt{} @asulock{/hurd}}@acunsafe{@acucorrupt{} @aculock{/hurd}}}
  103. @c __libc_siglongjmp @ascuplugin @asucorrupt @asulock/hurd @acucorrupt @aculock/hurd
  104. @c _longjmp_unwind @ascuplugin @asucorrupt @acucorrupt
  105. @c __pthread_cleanup_upto @ascuplugin @asucorrupt @acucorrupt
  106. @c plugins may be unsafe themselves, but even if they weren't, this
  107. @c function isn't robust WRT async signals and cancellation:
  108. @c cleanups aren't taken off the stack right away, only after all
  109. @c cleanups have been run. This means that async-cancelling
  110. @c longjmp, or interrupting longjmp with an async signal handler
  111. @c that calls longjmp may run the same cleanups multiple times.
  112. @c _JMPBUF_UNWINDS_ADJ ok
  113. @c *cleanup_buf->__routine @ascuplugin
  114. @c sigprocmask(SIG_SETMASK) dup @asulock/hurd @aculock/hurd
  115. @c __longjmp ok
  116. This function restores current execution to the state saved in
  117. @var{state}, and continues execution from the call to @code{setjmp} that
  118. established that return point. Returning from @code{setjmp} by means of
  119. @code{longjmp} returns the @var{value} argument that was passed to
  120. @code{longjmp}, rather than @code{0}. (But if @var{value} is given as
  121. @code{0}, @code{setjmp} returns @code{1}).@refill
  122. @end deftypefun
  123. There are a lot of obscure but important restrictions on the use of
  124. @code{setjmp} and @code{longjmp}. Most of these restrictions are
  125. present because non-local exits require a fair amount of magic on the
  126. part of the C compiler and can interact with other parts of the language
  127. in strange ways.
  128. The @code{setjmp} function is actually a macro without an actual
  129. function definition, so you shouldn't try to @samp{#undef} it or take
  130. its address. In addition, calls to @code{setjmp} are safe in only the
  131. following contexts:
  132. @itemize @bullet
  133. @item
  134. As the test expression of a selection or iteration
  135. statement (such as @samp{if}, @samp{switch}, or @samp{while}).
  136. @item
  137. As one operand of an equality or comparison operator that appears as the
  138. test expression of a selection or iteration statement. The other
  139. operand must be an integer constant expression.
  140. @item
  141. As the operand of a unary @samp{!} operator, that appears as the
  142. test expression of a selection or iteration statement.
  143. @item
  144. By itself as an expression statement.
  145. @end itemize
  146. Return points are valid only during the dynamic extent of the function
  147. that called @code{setjmp} to establish them. If you @code{longjmp} to
  148. a return point that was established in a function that has already
  149. returned, unpredictable and disastrous things are likely to happen.
  150. You should use a nonzero @var{value} argument to @code{longjmp}. While
  151. @code{longjmp} refuses to pass back a zero argument as the return value
  152. from @code{setjmp}, this is intended as a safety net against accidental
  153. misuse and is not really good programming style.
  154. When you perform a non-local exit, accessible objects generally retain
  155. whatever values they had at the time @code{longjmp} was called. The
  156. exception is that the values of automatic variables local to the
  157. function containing the @code{setjmp} call that have been changed since
  158. the call to @code{setjmp} are indeterminate, unless you have declared
  159. them @code{volatile}.
  160. @node Non-Local Exits and Signals, System V contexts, Non-Local Details, Non-Local Exits
  161. @section Non-Local Exits and Signals
  162. In BSD Unix systems, @code{setjmp} and @code{longjmp} also save and
  163. restore the set of blocked signals; see @ref{Blocking Signals}. However,
  164. the POSIX.1 standard requires @code{setjmp} and @code{longjmp} not to
  165. change the set of blocked signals, and provides an additional pair of
  166. functions (@code{sigsetjmp} and @code{siglongjmp}) to get the BSD
  167. behavior.
  168. The behavior of @code{setjmp} and @code{longjmp} in @theglibc{} is
  169. controlled by feature test macros; see @ref{Feature Test Macros}. The
  170. default in @theglibc{} is the POSIX.1 behavior rather than the BSD
  171. behavior.
  172. The facilities in this section are declared in the header file
  173. @file{setjmp.h}.
  174. @pindex setjmp.h
  175. @deftp {Data Type} sigjmp_buf
  176. @standards{POSIX.1, setjmp.h}
  177. This is similar to @code{jmp_buf}, except that it can also store state
  178. information about the set of blocked signals.
  179. @end deftp
  180. @deftypefun int sigsetjmp (sigjmp_buf @var{state}, int @var{savesigs})
  181. @standards{POSIX.1, setjmp.h}
  182. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{/hurd}}@acunsafe{@aculock{/hurd}}}
  183. @c sigsetjmp @asulock/hurd @aculock/hurd
  184. @c __sigsetjmp(savemask) @asulock/hurd @aculock/hurd
  185. @c __sigjmp_save(savemask) @asulock/hurd @aculock/hurd
  186. @c sigprocmask(SIG_BLOCK probe) dup @asulock/hurd @aculock/hurd
  187. This is similar to @code{setjmp}. If @var{savesigs} is nonzero, the set
  188. of blocked signals is saved in @var{state} and will be restored if a
  189. @code{siglongjmp} is later performed with this @var{state}.
  190. @end deftypefun
  191. @deftypefun void siglongjmp (sigjmp_buf @var{state}, int @var{value})
  192. @standards{POSIX.1, setjmp.h}
  193. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuplugin{} @asucorrupt{} @asulock{/hurd}}@acunsafe{@acucorrupt{} @aculock{/hurd}}}
  194. @c Alias to longjmp.
  195. This is similar to @code{longjmp} except for the type of its @var{state}
  196. argument. If the @code{sigsetjmp} call that set this @var{state} used a
  197. nonzero @var{savesigs} flag, @code{siglongjmp} also restores the set of
  198. blocked signals.
  199. @end deftypefun
  200. @node System V contexts,, Non-Local Exits and Signals, Non-Local Exits
  201. @section Complete Context Control
  202. The Unix standard provides one more set of functions to control the
  203. execution path and these functions are more powerful than those
  204. discussed in this chapter so far. These functions were part of the
  205. original @w{System V} API and by this route were added to the Unix
  206. API. Besides on branded Unix implementations these interfaces are not
  207. widely available. Not all platforms and/or architectures @theglibc{}
  208. is available on provide this interface. Use @file{configure} to
  209. detect the availability.
  210. Similar to the @code{jmp_buf} and @code{sigjmp_buf} types used for the
  211. variables to contain the state of the @code{longjmp} functions the
  212. interfaces of interest here have an appropriate type as well. Objects
  213. of this type are normally much larger since more information is
  214. contained. The type is also used in a few more places as we will see.
  215. The types and functions described in this section are all defined and
  216. declared respectively in the @file{ucontext.h} header file.
  217. @deftp {Data Type} ucontext_t
  218. @standards{SVID, ucontext.h}
  219. The @code{ucontext_t} type is defined as a structure with at least the
  220. following elements:
  221. @table @code
  222. @item ucontext_t *uc_link
  223. This is a pointer to the next context structure which is used if the
  224. context described in the current structure returns.
  225. @item sigset_t uc_sigmask
  226. Set of signals which are blocked when this context is used.
  227. @item stack_t uc_stack
  228. Stack used for this context. The value need not be (and normally is
  229. not) the stack pointer. @xref{Signal Stack}.
  230. @item mcontext_t uc_mcontext
  231. This element contains the actual state of the process. The
  232. @code{mcontext_t} type is also defined in this header but the definition
  233. should be treated as opaque. Any use of knowledge of the type makes
  234. applications less portable.
  235. @end table
  236. @end deftp
  237. Objects of this type have to be created by the user. The initialization
  238. and modification happens through one of the following functions:
  239. @deftypefun int getcontext (ucontext_t *@var{ucp})
  240. @standards{SVID, ucontext.h}
  241. @safety{@prelim{}@mtsafe{@mtsrace{:ucp}}@assafe{}@acsafe{}}
  242. @c Linux-only implementations in assembly, including sigprocmask
  243. @c syscall. A few cases call the sigprocmask function, but that's safe
  244. @c too. The ppc case is implemented in terms of a swapcontext syscall.
  245. The @code{getcontext} function initializes the variable pointed to by
  246. @var{ucp} with the context of the calling thread. The context contains
  247. the content of the registers, the signal mask, and the current stack.
  248. Executing the contents would start at the point where the
  249. @code{getcontext} call just returned.
  250. @strong{Compatibility Note:} Depending on the operating system,
  251. information about the current context's stack may be in the
  252. @code{uc_stack} field of @var{ucp}, or it may instead be in
  253. architecture-specific subfields of the @code{uc_mcontext} field.
  254. The function returns @code{0} if successful. Otherwise it returns
  255. @code{-1} and sets @code{errno} accordingly.
  256. @end deftypefun
  257. The @code{getcontext} function is similar to @code{setjmp} but it does
  258. not provide an indication of whether @code{getcontext} is returning for
  259. the first time or whether an initialized context has just been restored.
  260. If this is necessary the user has to determine this herself. This must
  261. be done carefully since the context contains registers which might contain
  262. register variables. This is a good situation to define variables with
  263. @code{volatile}.
  264. Once the context variable is initialized it can be used as is or it can
  265. be modified using the @code{makecontext} function. The latter is normally
  266. done when implementing co-routines or similar constructs.
  267. @deftypefun void makecontext (ucontext_t *@var{ucp}, void (*@var{func}) (void), int @var{argc}, @dots{})
  268. @standards{SVID, ucontext.h}
  269. @safety{@prelim{}@mtsafe{@mtsrace{:ucp}}@assafe{}@acsafe{}}
  270. @c Linux-only implementations mostly in assembly, nothing unsafe.
  271. The @var{ucp} parameter passed to @code{makecontext} shall be
  272. initialized by a call to @code{getcontext}. The context will be
  273. modified in a way such that if the context is resumed it will start by
  274. calling the function @code{func} which gets @var{argc} integer arguments
  275. passed. The integer arguments which are to be passed should follow the
  276. @var{argc} parameter in the call to @code{makecontext}.
  277. Before the call to this function the @code{uc_stack} and @code{uc_link}
  278. element of the @var{ucp} structure should be initialized. The
  279. @code{uc_stack} element describes the stack which is used for this
  280. context. No two contexts which are used at the same time should use the
  281. same memory region for a stack.
  282. The @code{uc_link} element of the object pointed to by @var{ucp} should
  283. be a pointer to the context to be executed when the function @var{func}
  284. returns or it should be a null pointer. See @code{setcontext} for more
  285. information about the exact use.
  286. @end deftypefun
  287. While allocating the memory for the stack one has to be careful. Most
  288. modern processors keep track of whether a certain memory region is
  289. allowed to contain code which is executed or not. Data segments and
  290. heap memory are normally not tagged to allow this. The result is that
  291. programs would fail. Examples for such code include the calling
  292. sequences the GNU C compiler generates for calls to nested functions.
  293. Safe ways to allocate stacks correctly include using memory on the
  294. original thread's stack or explicitly allocating memory tagged for
  295. execution using (@pxref{Memory-mapped I/O}).
  296. @strong{Compatibility note}: The current Unix standard is very imprecise
  297. about the way the stack is allocated. All implementations seem to agree
  298. that the @code{uc_stack} element must be used but the values stored in
  299. the elements of the @code{stack_t} value are unclear. @Theglibc{}
  300. and most other Unix implementations require the @code{ss_sp} value of
  301. the @code{uc_stack} element to point to the base of the memory region
  302. allocated for the stack and the size of the memory region is stored in
  303. @code{ss_size}. There are implementations out there which require
  304. @code{ss_sp} to be set to the value the stack pointer will have (which
  305. can, depending on the direction the stack grows, be different). This
  306. difference makes the @code{makecontext} function hard to use and it
  307. requires detection of the platform at compile time.
  308. @deftypefun int setcontext (const ucontext_t *@var{ucp})
  309. @standards{SVID, ucontext.h}
  310. @safety{@prelim{}@mtsafe{@mtsrace{:ucp}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
  311. @c Linux-only implementations mostly in assembly. Some ports use
  312. @c sigreturn or swapcontext syscalls; others restore the signal mask
  313. @c first and then proceed restore other registers in userland, which
  314. @c leaves a window for cancellation or async signals with misaligned or
  315. @c otherwise corrupt stack. ??? Switching to a different stack, or even
  316. @c to an earlier state on the same stack, may conflict with pthread
  317. @c cleanups. This is not quite MT-Unsafe, it's a different kind of
  318. @c safety issue.
  319. The @code{setcontext} function restores the context described by
  320. @var{ucp}. The context is not modified and can be reused as often as
  321. wanted.
  322. If the context was created by @code{getcontext} execution resumes with
  323. the registers filled with the same values and the same stack as if the
  324. @code{getcontext} call just returned.
  325. If the context was modified with a call to @code{makecontext} execution
  326. continues with the function passed to @code{makecontext} which gets the
  327. specified parameters passed. If this function returns execution is
  328. resumed in the context which was referenced by the @code{uc_link}
  329. element of the context structure passed to @code{makecontext} at the
  330. time of the call. If @code{uc_link} was a null pointer the application
  331. terminates normally with an exit status value of @code{EXIT_SUCCESS}
  332. (@pxref{Program Termination}).
  333. If the context was created by a call to a signal handler or from any
  334. other source then the behaviour of @code{setcontext} is unspecified.
  335. Since the context contains information about the stack no two threads
  336. should use the same context at the same time. The result in most cases
  337. would be disastrous.
  338. The @code{setcontext} function does not return unless an error occurred
  339. in which case it returns @code{-1}.
  340. @end deftypefun
  341. The @code{setcontext} function simply replaces the current context with
  342. the one described by the @var{ucp} parameter. This is often useful but
  343. there are situations where the current context has to be preserved.
  344. @deftypefun int swapcontext (ucontext_t *restrict @var{oucp}, const ucontext_t *restrict @var{ucp})
  345. @standards{SVID, ucontext.h}
  346. @safety{@prelim{}@mtsafe{@mtsrace{:oucp} @mtsrace{:ucp}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
  347. @c Linux-only implementations mostly in assembly. Some ports call or
  348. @c inline getcontext and/or setcontext, adjusting the saved context in
  349. @c between, so we inherit the potential issues of both.
  350. The @code{swapcontext} function is similar to @code{setcontext} but
  351. instead of just replacing the current context the latter is first saved
  352. in the object pointed to by @var{oucp} as if this was a call to
  353. @code{getcontext}. The saved context would resume after the call to
  354. @code{swapcontext}.
  355. Once the current context is saved the context described in @var{ucp} is
  356. installed and execution continues as described in this context.
  357. If @code{swapcontext} succeeds the function does not return unless the
  358. context @var{oucp} is used without prior modification by
  359. @code{makecontext}. The return value in this case is @code{0}. If the
  360. function fails it returns @code{-1} and sets @code{errno} accordingly.
  361. @end deftypefun
  362. @heading Example for SVID Context Handling
  363. The easiest way to use the context handling functions is as a
  364. replacement for @code{setjmp} and @code{longjmp}. The context contains
  365. on most platforms more information which may lead to fewer surprises
  366. but this also means using these functions is more expensive (besides
  367. being less portable).
  368. @smallexample
  369. int
  370. random_search (int n, int (*fp) (int, ucontext_t *))
  371. @{
  372. volatile int cnt = 0;
  373. ucontext_t uc;
  374. /* @r{Safe current context.} */
  375. if (getcontext (&uc) < 0)
  376. return -1;
  377. /* @r{If we have not tried @var{n} times try again.} */
  378. if (cnt++ < n)
  379. /* @r{Call the function with a new random number}
  380. @r{and the context}. */
  381. if (fp (rand (), &uc) != 0)
  382. /* @r{We found what we were looking for.} */
  383. return 1;
  384. /* @r{Not found.} */
  385. return 0;
  386. @}
  387. @end smallexample
  388. Using contexts in such a way enables emulating exception handling. The
  389. search functions passed in the @var{fp} parameter could be very large,
  390. nested, and complex which would make it complicated (or at least would
  391. require a lot of code) to leave the function with an error value which
  392. has to be passed down to the caller. By using the context it is
  393. possible to leave the search function in one step and allow restarting
  394. the search which also has the nice side effect that it can be
  395. significantly faster.
  396. Something which is harder to implement with @code{setjmp} and
  397. @code{longjmp} is to switch temporarily to a different execution path
  398. and then resume where execution was stopped.
  399. @smallexample
  400. @include swapcontext.c.texi
  401. @end smallexample
  402. This an example how the context functions can be used to implement
  403. co-routines or cooperative multi-threading. All that has to be done is
  404. to call every once in a while @code{swapcontext} to continue running a
  405. different context. It is not recommended to do the context switching from
  406. the signal handler directly since leaving the signal handler via
  407. @code{setcontext} if the signal was delivered during code that was not
  408. asynchronous signal safe could lead to problems. Setting a variable in
  409. the signal handler and checking it in the body of the functions which
  410. are executed is a safer approach. Since @code{swapcontext} is saving the
  411. current context it is possible to have multiple different scheduling points
  412. in the code. Execution will always resume where it was left.