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