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