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