]> gitweb.factorcode.org Git - factor.git/blob - vm/errors.cpp
vm: allocate an extra canary page before callstack
[factor.git] / vm / errors.cpp
1 #include "master.hpp"
2
3 namespace factor
4 {
5
6 void fatal_error(const char *msg, cell tagged)
7 {
8         std::cout << "fatal_error: " << msg;
9         std::cout << ": " << std::hex << tagged << std::dec;
10         std::cout << std::endl;
11         exit(1);
12 }
13
14 void critical_error(const char *msg, cell tagged)
15 {
16         std::cout << "You have triggered a bug in Factor. Please report.\n";
17         std::cout << "critical_error: " << msg;
18         std::cout << ": " << std::hex << tagged << std::dec;
19         std::cout << std::endl;
20         current_vm()->factorbug();
21 }
22
23 void out_of_memory()
24 {
25         std::cout << "Out of memory\n\n";
26         current_vm()->dump_generations();
27         exit(1);
28 }
29
30 void factor_vm::general_error(vm_error_type error, cell arg1, cell arg2)
31 {
32         /* Reset local roots before allocating anything */
33         data_roots.clear();
34         bignum_roots.clear();
35         code_roots.clear();
36
37         /* If we had an underflow or overflow, data or retain stack
38         pointers might be out of bounds, so fix them before allocating
39         anything */
40         ctx->fix_stacks();
41
42         /* If error was thrown during heap scan, we re-enable the GC */
43         gc_off = false;
44
45         /* If the error handler is set, we rewind any C stack frames and
46         pass the error to user-space. */
47         if(!current_gc && to_boolean(special_objects[ERROR_HANDLER_QUOT]))
48         {
49 #ifdef FACTOR_DEBUG
50                 /* Doing a GC here triggers all kinds of funny errors */
51                 primitive_compact_gc();
52 #endif
53
54                 /* Now its safe to allocate and GC */
55                 cell error_object = allot_array_4(special_objects[OBJ_ERROR],
56                         tag_fixnum(error),arg1,arg2);
57
58                 ctx->push(error_object);
59
60                 unwind_native_frames(special_objects[ERROR_HANDLER_QUOT],
61                         ctx->callstack_top);
62         }
63         /* Error was thrown in early startup before error handler is set, just
64         crash. */
65         else
66         {
67                 std::cout << "You have triggered a bug in Factor. Please report.\n";
68                 std::cout << "error: " << error << std::endl;
69                 std::cout << "arg 1: "; print_obj(arg1); std::cout << std::endl;
70                 std::cout << "arg 2: "; print_obj(arg2); std::cout << std::endl;
71                 factorbug();
72         }
73 }
74
75 void factor_vm::type_error(cell type, cell tagged)
76 {
77         general_error(ERROR_TYPE,tag_fixnum(type),tagged);
78 }
79
80 void factor_vm::not_implemented_error()
81 {
82         general_error(ERROR_NOT_IMPLEMENTED,false_object,false_object);
83 }
84
85 void factor_vm::memory_protection_error(cell addr)
86 {
87         if(code->safepoint_p(addr))
88                 handle_safepoint();
89         else if(ctx->datastack_seg->underflow_p(addr))
90                 general_error(ERROR_DATASTACK_UNDERFLOW,false_object,false_object);
91         else if(ctx->datastack_seg->overflow_p(addr))
92                 general_error(ERROR_DATASTACK_OVERFLOW,false_object,false_object);
93         else if(ctx->retainstack_seg->underflow_p(addr))
94                 general_error(ERROR_RETAINSTACK_UNDERFLOW,false_object,false_object);
95         else if(ctx->retainstack_seg->overflow_p(addr))
96                 general_error(ERROR_RETAINSTACK_OVERFLOW,false_object,false_object);
97         else if(ctx->callstack_seg->underflow_p(addr))
98                 general_error(ERROR_CALLSTACK_OVERFLOW,false_object,false_object);
99         else if(ctx->callstack_seg->overflow_p(addr))
100                 general_error(ERROR_CALLSTACK_UNDERFLOW,false_object,false_object);
101         else if(ctx->callstack_seg->canary_p(addr))
102                 fatal_error("Call stack overflow in your call stack overflow", addr);
103         else
104                 general_error(ERROR_MEMORY,from_unsigned_cell(addr),false_object);
105 }
106
107 void factor_vm::signal_error(cell signal)
108 {
109         general_error(ERROR_SIGNAL,from_unsigned_cell(signal),false_object);
110 }
111
112 void factor_vm::divide_by_zero_error()
113 {
114         general_error(ERROR_DIVIDE_BY_ZERO,false_object,false_object);
115 }
116
117 void factor_vm::fp_trap_error(unsigned int fpu_status)
118 {
119         general_error(ERROR_FP_TRAP,tag_fixnum(fpu_status),false_object);
120 }
121
122 /* For testing purposes */
123 void factor_vm::primitive_unimplemented()
124 {
125         not_implemented_error();
126 }
127
128 void factor_vm::memory_signal_handler_impl()
129 {
130         memory_protection_error(signal_fault_addr);
131 }
132
133 void memory_signal_handler_impl()
134 {
135         current_vm()->memory_signal_handler_impl();
136 }
137
138 void factor_vm::synchronous_signal_handler_impl()
139 {
140         signal_error(signal_number);
141 }
142
143 void synchronous_signal_handler_impl()
144 {
145         current_vm()->synchronous_signal_handler_impl();
146 }
147
148 void factor_vm::fp_signal_handler_impl()
149 {
150         /* Clear pending exceptions to avoid getting stuck in a loop */
151         set_fpu_state(get_fpu_state());
152
153         fp_trap_error(signal_fpu_status);
154 }
155
156 void fp_signal_handler_impl()
157 {
158         current_vm()->fp_signal_handler_impl();
159 }
160
161 void factor_vm::enqueue_safepoint_fep()
162 {
163         if (fep_p)
164                 fatal_error("Low-level debugger interrupted", 0);
165         safepoint_fep = true;
166         code->guard_safepoint();
167 }
168
169 void factor_vm::enqueue_safepoint_signal(cell signal)
170 {
171         sigaddset(&safepoint_signals, signal);
172         code->guard_safepoint();
173 }
174
175 void factor_vm::enqueue_safepoint_sample()
176 {
177         if (!sampling_p)
178                 fatal_error("Received sampling signal while not sampling!", 0);
179         ++safepoint_sample_count;
180 }
181
182 void factor_vm::handle_safepoint()
183 {
184         if (signal_from_leaf)
185                 std::cout << "XXX SIGNALED FROM LEAF\n";
186
187         code->unguard_safepoint();
188         if (safepoint_fep) {
189                 std::cout << "Interrupted\n";
190                 factorbug();
191                 safepoint_fep = false;
192                 return;
193         }
194         // XXX handle sample count
195         // XXX handle queued signals
196 }
197
198 }