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