Suicidal-exception-handling.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd">
  2. <html>
  3. <!-- Created on November 8, 2012 by texi2html 1.82
  4. texi2html was written by:
  5. Lionel Cons <[email protected]> (original author)
  6. Karl Berry <[email protected]>
  7. Olaf Bachmann <[email protected]>
  8. and many others.
  9. Maintained by: Many creative people.
  10. Send bugs and suggestions to <[email protected]>
  11. -->
  12. <head>
  13. <title>avram - a virtual machine code interpreter: 3.9.3.3 Suicidal exception handling</title>
  14. <meta name="description" content="avram - a virtual machine code interpreter: 3.9.3.3 Suicidal exception handling">
  15. <meta name="keywords" content="avram - a virtual machine code interpreter: 3.9.3.3 Suicidal exception handling">
  16. <meta name="resource-type" content="document">
  17. <meta name="distribution" content="global">
  18. <meta name="Generator" content="texi2html 1.82">
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <style type="text/css">
  21. <!--
  22. a.summary-letter {text-decoration: none}
  23. blockquote.smallquotation {font-size: smaller}
  24. pre.display {font-family: serif}
  25. pre.format {font-family: serif}
  26. pre.menu-comment {font-family: serif}
  27. pre.menu-preformatted {font-family: serif}
  28. pre.smalldisplay {font-family: serif; font-size: smaller}
  29. pre.smallexample {font-size: smaller}
  30. pre.smallformat {font-family: serif; font-size: smaller}
  31. pre.smalllisp {font-size: smaller}
  32. span.roman {font-family:serif; font-weight:normal;}
  33. span.sansserif {font-family:sans-serif; font-weight:normal;}
  34. ul.toc {list-style: none}
  35. -->
  36. </style>
  37. </head>
  38. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  39. <a name="Suicidal-exception-handling"></a>
  40. <table cellpadding="1" cellspacing="1" border="0">
  41. <tr><td valign="middle" align="left">[<a href="Memory-leaks.html#Memory-leaks" title="Previous section in reading order"> &lt; </a>]</td>
  42. <td valign="middle" align="left">[<a href="Character-Table.html#Character-Table" title="Next section in reading order"> &gt; </a>]</td>
  43. <td valign="middle" align="left"> &nbsp; </td>
  44. <td valign="middle" align="left">[<a href="Library-Reference.html#Library-Reference" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
  45. <td valign="middle" align="left">[<a href="Working-around-library-misfeatures.html#Working-around-library-misfeatures" title="Up section"> Up </a>]</td>
  46. <td valign="middle" align="left">[<a href="Character-Table.html#Character-Table" title="Next chapter"> &gt;&gt; </a>]</td>
  47. <td valign="middle" align="left"> &nbsp; </td>
  48. <td valign="middle" align="left"> &nbsp; </td>
  49. <td valign="middle" align="left"> &nbsp; </td>
  50. <td valign="middle" align="left"> &nbsp; </td>
  51. <td valign="middle" align="left">[<a href="avram.html#Top" title="Cover (top) of document">Top</a>]</td>
  52. <td valign="middle" align="left">[<a href="avram_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
  53. <td valign="middle" align="left">[<a href="Function-Index.html#Function-Index" title="Index">Index</a>]</td>
  54. <td valign="middle" align="left">[<a href="avram_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
  55. </tr></table>
  56. <hr size="1">
  57. <a name="Suicidal-exception-handling-1"></a>
  58. <h4 class="subsubsection">3.9.3.3 Suicidal exception handling</h4>
  59. <p>An inconvenient characteristic of some external library functions is
  60. to terminate the program rather than returning an error status to the
  61. caller for routine events such as a failure of memory allocation.
  62. Although in many cases there is no simple workaround for this
  63. behavior, memory allocation failures at least can be detected and
  64. preventive action taken by using the functions described in this
  65. section.
  66. </p>
  67. <p>The general approach is to use memory management functions from
  68. &lsquo;<tt>mwrap.h</tt>&rsquo; as described previously (<a href="Memory-leaks.html#Memory-leaks">Memory leaks</a>), while
  69. additionally registering a return destination for a non-local jump to
  70. <a name="index-non_002dlocal-jumps"></a>
  71. be taken in the event of a memory overflow. The jump is taken when an
  72. external library function calls <code>malloc</code> or <code>realloc</code>
  73. unsuccessfully. The jump avoids passing control back to the library
  74. function, thereby denying it the opportunity to abort, but restores
  75. the context to that of the jump destination almost as if the library
  76. function and all of its intervening callers had returned normally.
  77. </p>
  78. <p>The interface is similar to that of the standard <code>setjmp</code>
  79. <a name="index-setjmp"></a>
  80. function defined in the system header file <code>setjmp.h</code>, and in
  81. fact is built on it, but differs in that the client module does not
  82. explicitly refer to jump buffers. Instead, the <code>mwrap</code> module
  83. internally maintains a stack of return destinations.
  84. </p>
  85. <p>If a jump is taken, it always goes to the most recently registered
  86. destination. It may revert to the previously registered destination
  87. only when the current one is cleared. This organization provides the
  88. necessary flexibility for multiple clients and recursion, but it
  89. necessitates a protocol whereby each registration of a destination
  90. must be explicitly cleared exactly once.
  91. </p>
  92. <p>The following functions implement these two features.
  93. </p>
  94. <dl>
  95. <dt><a name="index-avm_005fsetjmp"></a><u>Function:</u> int <b>avm_setjmp</b><i> ()</i></dt>
  96. <dd><p>This function specifies the point to which control will pass by a
  97. non-local jump if there is insufficient memory to complete a
  98. subsequent <code>malloc</code> or <code>realloc</code> operation. Only the
  99. operations that take place while memory is being managed due to
  100. <code>avm_manage_memory</code> are affected (<a href="Memory-leaks.html#Memory-leaks">Memory leaks</a>).
  101. </p>
  102. <p>The function returns zero when it is called normally and successfully
  103. registers the return point.
  104. </p>
  105. <p>It returns a non-zero value when it has been entered by a non-local
  106. jump (i.e., when <code>malloc</code> or <code>realloc</code> has reported
  107. insufficient memory while memory management is active), or when the
  108. return point could not be successfully registered due to insufficient
  109. memory. The client need not distinguish between these two cases,
  110. because both correspond to memory overflows and the destination must
  111. be cleared by <code>avm_clearjmp</code> regardless.
  112. </p>
  113. <p>When a non-zero value is returned due to this function being reached
  114. by a non-local jump, it has the side effects of reclaiming all managed
  115. memory by calling <code>avm_free_managed_memory</code> and disabling memory
  116. management by calling <code>avm_dont_manage_memory</code>.
  117. </p></dd></dl>
  118. <dl>
  119. <dt><a name="index-avm_005fclearjmp"></a><u>Function:</u> void <b>avm_clearjmp</b><i> ()</i></dt>
  120. <dd><p>This function cancels the effect of <code>avm_setjmp ()</code> by preventing
  121. further non-local jumps to its destination if the destination was
  122. successfully registered, or by acknowledging unsuccessful registration
  123. otherwise. It should be called before exiting any function that calls
  124. <code>avm_setjmp ()</code> or anomalous results may ensue.
  125. </p></dd></dl>
  126. <p>The memory management functions <code>avm_manage_memory</code> and
  127. <code>avm_dont_manage_memory</code> can be useful with or without
  128. <code>avm_setjmp</code>, depending on how much of a workaround is needed for
  129. a given library. If a library does not abort on memory overflows,
  130. there is no need to use <code>avm_setjmp</code>, while it may still be
  131. appropriate to use the other functions against memory leaks.
  132. </p>
  133. <p>Calling <code>avm_clearjmp</code> is particularly important if a client
  134. module with memory management that doesn&rsquo;t use <code>avm_setjmp</code> is
  135. invoked subsequently to one that does, so that memory overflows in the
  136. latter won&rsquo;t cause an attempted jump to a stale destination.
  137. </p>
  138. <p>A further complication that arises from careful consideration of these
  139. issues is the situation of a client module that does not intend to use
  140. <code>avm_setjmp</code> but is called (perhaps indirectly) by one that
  141. does. The latter will have registered a return destination that
  142. remains active and valid even if the former refrains from doing so,
  143. thereby allowing a branch to be taken that should have been prevented.
  144. Although it is an unusual situation, it can be accommodated by the
  145. following function.
  146. </p>
  147. <dl>
  148. <dt><a name="index-avm_005fsetnonjump"></a><u>Function:</u> void <b>avm_setnonjump</b><i> ()</i></dt>
  149. <dd><p>This function temporarily inhibits non-local jumps to destinations
  150. previously registered by <code>avm_setjmp</code> until the next time
  151. <code>avm_clearjmp</code> is called. Thereafter, any previously registered
  152. destinations are reinstated.
  153. </p></dd></dl>
  154. <p>A sketch of how some of these functions might be used to cope with
  155. library functions that would otherwise terminate the program in the
  156. event of a memory overflow is shown below. The GNU <code>libc</code>
  157. <a name="index-non_002dlocal-jumps-1"></a>
  158. reference manual contains a related discussion of non-local jumps.
  159. </p>
  160. <table><tr><td>&nbsp;</td><td><pre class="example">#include &lt;avm/mwrap.h&gt;
  161. ...
  162. int
  163. function foobar (foo, bar)
  164. ...
  165. {
  166. char *my_data;
  167. my_data = (char *) malloc (100);
  168. if (avm_setjmp () != 0)
  169. {
  170. avm_clearjmp ();
  171. avm_turn_on_stdout (); /* reaching here */
  172. free (my_data); /* means malloc */
  173. return ABNORMAL_STATUS; /* failed below */
  174. }
  175. avm_turn_off_stdout ();
  176. avm_manage_memory ();
  177. ...
  178. call_library_functions (foo, bar); /* may jump */
  179. ... /* to above */
  180. avm_free_managed_memory ();
  181. avm_turn_on_stdout ();
  182. avm_clearjmp ();
  183. free (my_data); /* reaching here means */
  184. return OK_STATUS; /* jumping wasn't done */
  185. }
  186. </pre></td></tr></table>
  187. <p>Portability issues with these functions are not well known at this
  188. <a name="index-portability-1"></a>
  189. writing. If the configuration script for <code>avram</code> fails to detect
  190. the required features in <code>setjmp.h</code> on the host system,
  191. conditional compilation directives will disable the functions
  192. <code>avm_setjmp</code>, <code>avm_clearjmp</code>, and <code>avm_setnonjmp</code>.
  193. However, it may still be possible for the other <code>avm_</code>* memory
  194. management functions to be configured.
  195. </p>
  196. <p>If <code>setjmp</code> is not configured, the <code>avm_setjmp</code> function
  197. is still callable but will always return a value of zero, and will
  198. provide no protection against external library functions aborting the
  199. program. The other two will perform no operation and return.
  200. </p>
  201. <hr size="1">
  202. <table cellpadding="1" cellspacing="1" border="0">
  203. <tr><td valign="middle" align="left">[<a href="Memory-leaks.html#Memory-leaks" title="Previous section in reading order"> &lt; </a>]</td>
  204. <td valign="middle" align="left">[<a href="Character-Table.html#Character-Table" title="Next section in reading order"> &gt; </a>]</td>
  205. <td valign="middle" align="left"> &nbsp; </td>
  206. <td valign="middle" align="left">[<a href="Library-Reference.html#Library-Reference" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
  207. <td valign="middle" align="left">[<a href="Working-around-library-misfeatures.html#Working-around-library-misfeatures" title="Up section"> Up </a>]</td>
  208. <td valign="middle" align="left">[<a href="Character-Table.html#Character-Table" title="Next chapter"> &gt;&gt; </a>]</td>
  209. <td valign="middle" align="left"> &nbsp; </td>
  210. <td valign="middle" align="left"> &nbsp; </td>
  211. <td valign="middle" align="left"> &nbsp; </td>
  212. <td valign="middle" align="left"> &nbsp; </td>
  213. <td valign="middle" align="left">[<a href="avram.html#Top" title="Cover (top) of document">Top</a>]</td>
  214. <td valign="middle" align="left">[<a href="avram_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
  215. <td valign="middle" align="left">[<a href="Function-Index.html#Function-Index" title="Index">Index</a>]</td>
  216. <td valign="middle" align="left">[<a href="avram_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
  217. </tr></table>
  218. <p>
  219. <font size="-1">
  220. This document was generated on <i>November 8, 2012</i> using <a href="http://www.nongnu.org/texi2html/"><i>texi2html 1.82</i></a>.
  221. </font>
  222. <br>
  223. </p>
  224. </body>
  225. </html>