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