1.0.13.30: more README touchups
[sbcl.git] / tests / win32-stack-unwind.c
blob4ed4aa80b05bc447507f3386667714a13095c120
1 /* Compiled and loaded by win32-foreign-stack-unwind.impure.lisp
3 * establish_return_frame(callback_ptr) establishes an SEH frame
4 * that will cause an unwind to itself followed by a return on
5 * any exception, and then calls the callback_ptr.
7 * perform_test_unwind() does an unwind to the SEH frame
8 * established by establish_return_frame().
10 * The name of the game for the tests is to establish a callback
11 * that establishes something with a dynamic contour and
12 * possibly a control transfer semantic (such as a binding or an
13 * unwind-protect) and then call perform_test_unwind() or cause
14 * an exception that should be handled by SBCL and see what
15 * breaks.
18 /* This software is part of the SBCL system. See the README file for
19 * more information.
21 * While most of SBCL is derived from the CMU CL system, the test
22 * files (like this one) were written from scratch after the fork
23 * from CMU CL.
25 * This software is in the public domain and is provided with
26 * absolutely no warranty. See the COPYING and CREDITS files for
27 * more information.
30 #include <stdio.h>
31 #include <stdlib.h>
33 #define WIN32_LEAN_AND_MEAN
34 #include <windows.h>
35 #include <excpt.h>
38 /* The "public" API */
40 typedef void (*callback_ptr)(void);
42 void establish_return_frame(callback_ptr callback);
43 void perform_test_unwind(void);
46 /* The implementation */
48 static
49 void **saved_exception_frame;
51 static
52 DWORD saved_ebp;
54 static void *get_seh_frame(void)
56 void* retval;
57 asm volatile ("movl %%fs:0,%0": "=r" (retval));
58 return retval;
61 static void set_seh_frame(void *frame)
63 asm volatile ("movl %0,%%fs:0": : "r" (frame));
67 static
68 EXCEPTION_DISPOSITION handle_exception(EXCEPTION_RECORD *exception_record,
69 void **exception_frame,
70 CONTEXT *context,
71 void *dc)
73 /* If an exception occurs and is passed to us to handle, just
74 * unwind. One or more test cases check for SBCL handling
75 * breakpoint exceptions properly. This makes sure that it
76 * doesn't unless a new exception frame is estabished when a
77 * callback occurs. */
78 if (!(exception_record->ExceptionFlags
79 & (EH_UNWINDING | EH_EXIT_UNWIND))) {
80 perform_test_unwind();
83 return ExceptionContinueSearch;
86 static void invoke_callback(callback_ptr callback, DWORD *unwind_token);
88 asm("_invoke_callback:"
89 "pushl %ebp; movl %esp, %ebp;"
90 "movl 12(%ebp), %eax;"
91 "movl %ebp, (%eax);"
92 "call *8(%ebp);"
93 "movl %ebp, %esp; popl %ebp; ret");
95 static void do_unwind(void *target_frame, DWORD unwind_token);
96 asm("_do_unwind:"
97 "pushl $target; pushl %ebp; movl %esp, %ebp;"
98 "pushl $0xcafe; pushl $0; pushl $-1; pushl 12(%ebp); call _RtlUnwind@16;"
99 "target:"
100 "movl 16(%ebp), %esp; popl %ebp; ret");
103 void establish_return_frame(callback_ptr callback)
105 void *exception_frame[2];
107 saved_exception_frame = exception_frame;
108 exception_frame[0] = get_seh_frame();
109 exception_frame[1] = handle_exception;
110 set_seh_frame(exception_frame);
112 invoke_callback(callback, &saved_ebp);
114 if (exception_frame != get_seh_frame()) {
115 /* It is never good for this to happen. */
116 printf("exception frame mismatch on callback return.\n");
119 set_seh_frame(exception_frame[0]);
122 void perform_test_unwind(void)
124 do_unwind(saved_exception_frame, saved_ebp);
127 /* EOF */