rerun pidl
[metze/wireshark/wip.git] / epan / exceptions.h
1 /* exceptions.h
2  * Wireshark's exceptions.
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #ifndef __EXCEPTIONS_H__
26 #define __EXCEPTIONS_H__
27
28 #include "except.h"
29
30 /* Wireshark has only one exception group, to make these macros simple */
31 #define XCEPT_GROUP_WIRESHARK 1
32
33 /**
34     Index is out of range.
35     An attempt was made to read past the end of a buffer.
36     This generally means that the capture was done with a "slice"
37     length or "snapshot" length less than the maximum packet size,
38     and a link-layer packet was cut short by that, so not all of the
39     data in the link-layer packet was available.
40 **/
41 #define BoundsError             1
42
43 /**
44     Index is beyond reported length (not cap_len)
45     An attempt was made to read past the logical end of a buffer. This
46     differs from a BoundsError in that the parent protocol established a
47     limit past which this dissector should not process in the buffer and that
48     limit was exceeded.
49     This generally means that the packet is invalid, i.e. whatever
50     code constructed the packet and put it on the wire didn't put enough
51     data into it.  It is therefore currently reported as a "Malformed
52     packet".
53 **/
54 #define ReportedBoundsError     2
55
56 /**
57     Index is beyond fragment length but not reported length.
58     This means that the packet wasn't reassembled.
59 **/
60 #define FragmentBoundsError     3
61
62 /**
63     During dfilter parsing
64 **/
65 #define TypeError               4
66
67 /**
68     A bug was detected in a dissector.
69
70     DO NOT throw this with THROW(); that means that no details about
71     the dissector error will be reported.  (Instead, the message will
72     blame you for not providing details.)
73
74     Instead, use the DISSECTOR_ASSERT(), etc. macros in epan/proto.h.
75 **/
76 #define DissectorError          5
77
78 /**
79     Index is out of range.
80     An attempt was made to read past the end of a buffer.
81     This error is specific to SCSI data transfers where for some CDBs
82     it is normal that the data PDU might be short.
83     I.e. ReportLuns initially called with allocation_length=8, just enough
84     to get the "size" of lun list back after which the initiator will
85     reissue the command with an allocation_length that is big enough.
86 **/
87 #define ScsiBoundsError         6
88
89 /**
90     Running out of memory.
91     A dissector tried to allocate memory but that failed.
92 **/
93 #define OutOfMemoryError        7
94
95 /**
96     The reassembly state machine was passed a bad fragment offset,
97     or other similar issues. We used to use DissectorError in these
98     cases, but they're not necessarily the dissector's fault - if the packet
99     contains a bad fragment offset, the dissector shouldn't have to figure
100     that out by itself since that's what the reassembly machine is for.
101 **/
102 #define ReassemblyError         8
103
104 /*
105  * Catch errors that, if you're calling a subdissector and catching
106  * exceptions from the subdissector, and possibly dissecting more
107  * stuff after the subdissector returns or fails, mean it makes
108  * sense to continue dissecting:
109  *
110  * BoundsError indicates a configuration problem (the capture was
111  * set up to throw away data, and it did); there's no point in
112  * trying to dissect any more data, as there's no more data to dissect.
113  *
114  * FragmentBoundsError indicates a configuration problem (reassembly
115  * wasn't enabled or couldn't be done); there's no point in trying
116  * to dissect any more data, as there's no more data to dissect.
117  *
118  * OutOfMemoryError indicates what its name suggests; there's no point
119  * in trying to dissect any more data, as you're probably not going to
120  * have any more memory to use when dissecting them.
121  *
122  * Other errors indicate that there's some sort of problem with
123  * the packet; you should continue dissecting data, as it might
124  * be OK, and, even if it's not, you should report its problem
125  * separately.
126  */
127 #define CATCH_NONFATAL_ERRORS \
128         CATCH3(ReportedBoundsError, ScsiBoundsError, ReassemblyError)
129
130 /*
131  * Catch all bounds-checking errors.
132  */
133 #define CATCH_BOUNDS_ERRORS \
134         CATCH4(BoundsError, FragmentBoundsError, ReportedBoundsError, \
135                ScsiBoundsError)
136
137 /*
138  * Catch all bounds-checking errors, and catch dissector bugs.
139  * Should only be used at the top level, so that dissector bugs
140  * go all the way to the top level and get reported immediately.
141  */
142 #define CATCH_BOUNDS_AND_DISSECTOR_ERRORS \
143         CATCH6(BoundsError, FragmentBoundsError, ReportedBoundsError, \
144                ScsiBoundsError, DissectorError, ReassemblyError)
145
146 /* Usage:
147  *
148  * TRY {
149  *      code;
150  * }
151  *
152  * CATCH(exception) {
153  *      code;
154  * }
155  *
156  * CATCH2(exception1, exception2) {
157  *      code;
158  * }
159  *
160  * CATCH3(exception1, exception2, exception3) {
161  *      code;
162  * }
163  *
164  * CATCH4(exception1, exception2, exception3, exception4) {
165  *      code;
166  * }
167  *
168  * CATCH5(exception1, exception2, exception3, exception4, exception5) {
169  *      code;
170  * }
171  *
172  * CATCH6(exception1, exception2, exception3, exception4, exception5, exception6) {
173  *      code;
174  * }
175  *
176  * CATCH_NONFATAL_ERRORS {
177  *      code;
178  * }
179  *
180  * CATCH_BOUNDS_ERRORS {
181  *      code;
182  * }
183  *
184  * CATCH_BOUNDS_AND_DISSECTOR_ERRORS {
185  *      code;
186  * }
187  *
188  * CATCH_ALL {
189  *      code;
190  * }
191  *
192  * FINALLY {
193  *      code;
194  * }
195  *
196  * ENDTRY;
197  *
198  * ********* Never use 'goto' or 'return' inside the TRY, CATCH*, or
199  * ********* FINALLY blocks. Execution must proceed through ENDTRY before
200  * ********* branching out.
201  *
202  * This is really something like:
203  *
204  * {
205  *      caught = FALSE:
206  *      x = setjmp();
207  *      if (x == 0) {
208  *              <TRY code>
209  *      }
210  *      if (!caught && x == 1) {
211  *              caught = TRUE;
212  *              <CATCH(1) code>
213  *      }
214  *      if (!caught && x == 2) {
215  *              caught = TRUE;
216  *              <CATCH(2) code>
217  *      }
218  *      if (!caught && (x == 3 || x == 4)) {
219  *              caught = TRUE;
220  *              <CATCH2(3,4) code>
221  *      }
222  *      if (!caught && (x == 5 || x == 6 || x == 7)) {
223  *              caught = TRUE;
224  *              <CATCH3(5,6,7) code>
225  *      }
226  *      if (!caught && x != 0) {
227  *              caught = TRUE;
228  *              <CATCH_ALL code>
229  *      }
230  *      <FINALLY code>
231  *      if(!caught) {
232  *              RETHROW(x)
233  *      }
234  * }<ENDTRY tag>
235  *
236  * All CATCH's must precede a CATCH_ALL.
237  * FINALLY must occur after any CATCH or CATCH_ALL.
238  * ENDTRY marks the end of the TRY code.
239  * TRY and ENDTRY are the mandatory parts of a TRY block.
240  * CATCH, CATCH_ALL, and FINALLY are all optional (although
241  * you'll probably use at least one, otherwise why "TRY"?)
242  *
243  * GET_MESSAGE  returns string ptr to exception message
244  *              when exception is thrown via THROW_MESSAGE()
245  *
246  * To throw/raise an exception.
247  *
248  * THROW(exception)
249  * RETHROW                              rethrow the caught exception
250  *
251  * A cleanup callback is a function called in case an exception occurs
252  * and is not caught. It should be used to free any dynamically-allocated data.
253  * A pop or call_and_pop should occur at the same statement-nesting level
254  * as the push.
255  *
256  * CLEANUP_CB_PUSH(func, data)
257  * CLEANUP_CB_POP
258  * CLEANUP_CB_CALL_AND_POP
259  */
260
261 /* we do up to three passes through the bit of code after except_try_push(),
262  * and except_state is used to keep track of where we are.
263  */
264 #define EXCEPT_CAUGHT   1 /* exception has been caught, no need to rethrow at
265                            * ENDTRY */
266
267 #define EXCEPT_RETHROWN 2 /* the exception was rethrown from a CATCH
268                            * block. Don't reenter the CATCH blocks, but do
269                            * execute FINALLY and rethrow at ENDTRY */
270
271 #define EXCEPT_FINALLY  4 /* we've entered the FINALLY block - don't allow
272                            * RETHROW, and don't reenter FINALLY if a
273                            * different exception is thrown */
274
275 #define TRY \
276 {\
277         except_t *exc; \
278         volatile int except_state = 0; \
279         static const except_id_t catch_spec[] = { \
280                 { XCEPT_GROUP_WIRESHARK, XCEPT_CODE_ANY } }; \
281         except_try_push(catch_spec, 1, &exc); \
282                                                        \
283         if(except_state & EXCEPT_CAUGHT)               \
284             except_state |= EXCEPT_RETHROWN;           \
285         except_state &= ~EXCEPT_CAUGHT;                \
286                                                        \
287         if (except_state == 0 && exc == 0)             \
288                 /* user's code goes here */
289
290 #define ENDTRY \
291         /* rethrow the exception if necessary */ \
292         if(!(except_state&EXCEPT_CAUGHT) && exc != 0)  \
293             except_rethrow(exc);                 \
294         except_try_pop();\
295 }
296
297 /* the (except_state |= EXCEPT_CAUGHT) in the below is a way of setting
298  * except_state before the user's code, without disrupting the user's code if
299  * it's a one-liner.
300  */
301 #define CATCH(x) \
302         if (except_state == 0 && exc != 0 && \
303             exc->except_id.except_code == (x) && \
304             (except_state |= EXCEPT_CAUGHT)) \
305                 /* user's code goes here */
306
307 #define CATCH2(x,y) \
308         if (except_state == 0 && exc != 0 && \
309             (exc->except_id.except_code == (x) || \
310              exc->except_id.except_code == (y)) && \
311             (except_state|=EXCEPT_CAUGHT)) \
312                 /* user's code goes here */
313
314 #define CATCH3(x,y,z) \
315         if (except_state == 0 && exc != 0 && \
316             (exc->except_id.except_code == (x) || \
317              exc->except_id.except_code == (y) || \
318              exc->except_id.except_code == (z)) && \
319             (except_state|=EXCEPT_CAUGHT)) \
320                 /* user's code goes here */
321
322 #define CATCH4(w,x,y,z) \
323         if (except_state == 0 && exc != 0 && \
324             (exc->except_id.except_code == (w) || \
325              exc->except_id.except_code == (x) || \
326              exc->except_id.except_code == (y) || \
327              exc->except_id.except_code == (z)) && \
328             (except_state|=EXCEPT_CAUGHT)) \
329                 /* user's code goes here */
330
331 #define CATCH5(v,w,x,y,z) \
332         if (except_state == 0 && exc != 0 && \
333             (exc->except_id.except_code == (v) || \
334              exc->except_id.except_code == (w) || \
335              exc->except_id.except_code == (x) || \
336              exc->except_id.except_code == (y) || \
337              exc->except_id.except_code == (z)) && \
338             (except_state|=EXCEPT_CAUGHT)) \
339                 /* user's code goes here */
340
341 #define CATCH6(u,v,w,x,y,z) \
342         if (except_state == 0 && exc != 0 && \
343             (exc->except_id.except_code == (u) || \
344              exc->except_id.except_code == (v) || \
345              exc->except_id.except_code == (w) || \
346              exc->except_id.except_code == (x) || \
347              exc->except_id.except_code == (y) || \
348              exc->except_id.except_code == (z)) && \
349             (except_state|=EXCEPT_CAUGHT)) \
350                 /* user's code goes here */
351
352 #define CATCH_ALL \
353         if (except_state == 0 && exc != 0 && \
354             (except_state|=EXCEPT_CAUGHT)) \
355                 /* user's code goes here */
356
357 #define FINALLY \
358         if( !(except_state & EXCEPT_FINALLY) && (except_state|=EXCEPT_FINALLY)) \
359                 /* user's code goes here */
360
361 #define THROW(x) \
362         except_throw(XCEPT_GROUP_WIRESHARK, (x), NULL)
363
364 #define THROW_ON(cond, x) G_STMT_START { \
365         if ((cond)) \
366                 except_throw(XCEPT_GROUP_WIRESHARK, (x), NULL); \
367 } G_STMT_END
368
369 #define THROW_MESSAGE(x, y) \
370         except_throw(XCEPT_GROUP_WIRESHARK, (x), (y))
371
372 #define THROW_MESSAGE_ON(cond, x, y) G_STMT_START { \
373         if ((cond)) \
374                 except_throw(XCEPT_GROUP_WIRESHARK, (x), (y)); \
375 } G_STMT_END
376
377 #define GET_MESSAGE                     except_message(exc)
378
379 #define RETHROW                                     \
380     {                                               \
381         /* check we're in a catch block */          \
382         g_assert(except_state == EXCEPT_CAUGHT);    \
383         /* we can't use except_rethrow here, as that pops a catch block \
384          * off the stack, and we don't want to do that, because we want to \
385          * excecute the FINALLY {} block first.     \
386          * except_throw doesn't provide an interface to rethrow an existing \
387          * exception; however, longjmping back to except_try_push() has the \
388          * desired effect.                          \
389          *                                          \
390          * Note also that THROW and RETHROW should provide much the same \
391          * functionality in terms of which blocks to enter, so any messing \
392          * about with except_state in here would indicate that THROW is \
393          * doing the wrong thing.                   \
394          */                                         \
395         longjmp(except_ch.except_jmp,1);            \
396     }
397
398 #define EXCEPT_CODE                     except_code(exc)
399
400 /* Register cleanup functions in case an exception is thrown and not caught.
401  * From the Kazlib documentation, with modifications for use with the
402  * Wireshark-specific macros:
403  *
404  * CLEANUP_PUSH(func, arg)
405  *
406  *  The call to CLEANUP_PUSH shall be matched with a call to
407  *  CLEANUP_CALL_AND_POP or CLEANUP_POP which must occur in the same
408  *  statement block at the same level of nesting. This requirement allows
409  *  an implementation to provide a CLEANUP_PUSH macro which opens up a
410  *  statement block and a CLEANUP_POP which closes the statement block.
411  *  The space for the registered pointers can then be efficiently
412  *  allocated from automatic storage.
413  *
414  *  The CLEANUP_PUSH macro registers a cleanup handler that will be
415  *  called if an exception subsequently occurs before the matching
416  *  CLEANUP_[CALL_AND_]POP is executed, and is not intercepted and
417  *  handled by a try-catch region that is nested between the two.
418  *
419  *  The first argument to CLEANUP_PUSH is a pointer to the cleanup
420  *  handler, a function that returns nothing and takes a single
421  *  argument of type void*. The second argument is a void* value that
422  *  is registered along with the handler.  This value is what is passed
423  *  to the registered handler, should it be called.
424  *
425  *  Cleanup handlers are called in the reverse order of their nesting:
426  *  inner handlers are called before outer handlers.
427  *
428  *  The program shall not leave the cleanup region between
429  *  the call to the macro CLEANUP_PUSH and the matching call to
430  *  CLEANUP_[CALL_AND_]POP by means other than throwing an exception,
431  *  or calling CLEANUP_[CALL_AND_]POP.
432  *
433  *  Within the call to the cleanup handler, it is possible that new
434  *  exceptions may happen.  Such exceptions must be handled before the
435  *  cleanup handler terminates. If the call to the cleanup handler is
436  *  terminated by an exception, the behavior is undefined. The exception
437  *  which triggered the cleanup is not yet caught; thus the program
438  *  would be effectively trying to replace an exception with one that
439  *  isn't in a well-defined state.
440  *
441  *
442  * CLEANUP_POP and CLEANUP_CALL_AND_POP
443  *
444  *  A call to the CLEANUP_POP or CLEANUP_CALL_AND_POP macro shall match
445  *  each call to CLEANUP_PUSH which shall be in the same statement block
446  *  at the same nesting level.  It shall match the most recent such a
447  *  call that is not matched by a previous CLEANUP_[CALL_AND_]POP at
448  *  the same level.
449  *
450  *  These macros causes the registered cleanup handler to be removed. If
451  *  CLEANUP_CALL_AND_POP is called, the cleanup handler is called.
452  *  In that case, the registered context pointer is passed to the cleanup
453  *  handler. If CLEANUP_POP is called, the cleanup handler is not called.
454  *
455  *  The program shall not leave the region between the call to the
456  *  macro CLEANUP_PUSH and the matching call to CLEANUP_[CALL_AND_]POP
457  *  other than by throwing an exception, or by executing the
458  *  CLEANUP_CALL_AND_POP.
459  *
460  */
461
462
463 #define CLEANUP_PUSH(f,a)               except_cleanup_push((f),(a))
464 #define CLEANUP_POP                     except_cleanup_pop(0)
465 #define CLEANUP_CALL_AND_POP            except_cleanup_pop(1)
466
467 /* Variants to allow nesting of except_cleanup_push w/o "shadowing" variables */
468 #define CLEANUP_PUSH_PFX(pfx,f,a)       except_cleanup_push_pfx(pfx,(f),(a))
469 #define CLEANUP_POP_PFX(pfx)            except_cleanup_pop_pfx(pfx,0)
470 #define CLEANUP_CALL_AND_POP_PFX(pfx)   except_cleanup_pop_pfx(pfx,1)
471
472
473
474 #endif /* __EXCEPTIONS_H__ */