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