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