]> gitweb.factorcode.org Git - factor.git/blob - vm/errors.cpp
vm: a stack underflow inside a primitive could leave a data root in an uninitialized...
[factor.git] / vm / errors.cpp
1 #include "master.hpp"
2
3 namespace factor
4 {
5
6 void fatal_error(const char *msg, cell tagged)
7 {
8         std::cout << "fatal_error: " << msg;
9         std::cout << ": " << std::hex << tagged << std::dec;
10         std::cout << std::endl;
11         exit(1);
12 }
13
14 void critical_error(const char *msg, cell tagged)
15 {
16         std::cout << "You have triggered a bug in Factor. Please report.\n";
17         std::cout << "critical_error: " << msg;
18         std::cout << ": " << std::hex << tagged << std::dec;
19         std::cout << std::endl;
20         current_vm()->factorbug();
21 }
22
23 void out_of_memory()
24 {
25         std::cout << "Out of memory\n\n";
26         current_vm()->dump_generations();
27         exit(1);
28 }
29
30 void factor_vm::general_error(vm_error_type error, cell arg1, cell arg2)
31 {
32         /* Reset local roots before allocating anything */
33         data_roots.clear();
34         bignum_roots.clear();
35         code_roots.clear();
36
37         /* If we had an underflow or overflow, data or retain stack
38         pointers might be out of bounds, so fix them before allocating
39         anything */
40         ctx->fix_stacks();
41
42         /* If error was thrown during heap scan, we re-enable the GC */
43         gc_off = false;
44
45         /* If the error handler is set, we rewind any C stack frames and
46         pass the error to user-space. */
47         if(!current_gc && to_boolean(special_objects[ERROR_HANDLER_QUOT]))
48         {
49 #ifdef FACTOR_DEBUG
50                 /* Doing a GC here triggers all kinds of funny errors */
51                 primitive_compact_gc();
52 #endif
53
54                 /* Now its safe to allocate and GC */
55                 cell error_object = allot_array_4(special_objects[OBJ_ERROR],
56                         tag_fixnum(error),arg1,arg2);
57
58                 ctx->push(error_object);
59
60                 unwind_native_frames(special_objects[ERROR_HANDLER_QUOT],
61                         ctx->callstack_top);
62         }
63         /* Error was thrown in early startup before error handler is set, just
64         crash. */
65         else
66         {
67                 std::cout << "You have triggered a bug in Factor. Please report.\n";
68                 std::cout << "error: " << error << std::endl;
69                 std::cout << "arg 1: "; print_obj(arg1); std::cout << std::endl;
70                 std::cout << "arg 2: "; print_obj(arg2); std::cout << std::endl;
71                 factorbug();
72         }
73 }
74
75 void factor_vm::type_error(cell type, cell tagged)
76 {
77         general_error(ERROR_TYPE,tag_fixnum(type),tagged);
78 }
79
80 void factor_vm::not_implemented_error()
81 {
82         general_error(ERROR_NOT_IMPLEMENTED,false_object,false_object);
83 }
84
85 void factor_vm::memory_protection_error(cell addr)
86 {
87         /* Retain and call stack underflows are not supposed to happen */
88
89         if(ctx->datastack_seg->underflow_p(addr))
90                 general_error(ERROR_DATASTACK_UNDERFLOW,false_object,false_object);
91         else if(ctx->datastack_seg->overflow_p(addr))
92                 general_error(ERROR_DATASTACK_OVERFLOW,false_object,false_object);
93         else if(ctx->retainstack_seg->underflow_p(addr))
94                 general_error(ERROR_RETAINSTACK_UNDERFLOW,false_object,false_object);
95         else if(ctx->retainstack_seg->overflow_p(addr))
96                 general_error(ERROR_RETAINSTACK_OVERFLOW,false_object,false_object);
97         else if(ctx->callstack_seg->underflow_p(addr))
98                 general_error(ERROR_CALLSTACK_OVERFLOW,false_object,false_object);
99         else if(ctx->callstack_seg->overflow_p(addr))
100                 general_error(ERROR_CALLSTACK_UNDERFLOW,false_object,false_object);
101         else
102                 general_error(ERROR_MEMORY,from_unsigned_cell(addr),false_object);
103 }
104
105 void factor_vm::signal_error(cell signal)
106 {
107         general_error(ERROR_SIGNAL,from_unsigned_cell(signal),false_object);
108 }
109
110 void factor_vm::divide_by_zero_error()
111 {
112         general_error(ERROR_DIVIDE_BY_ZERO,false_object,false_object);
113 }
114
115 void factor_vm::fp_trap_error(unsigned int fpu_status)
116 {
117         general_error(ERROR_FP_TRAP,tag_fixnum(fpu_status),false_object);
118 }
119
120 /* For testing purposes */
121 void factor_vm::primitive_unimplemented()
122 {
123         not_implemented_error();
124 }
125
126 void factor_vm::memory_signal_handler_impl()
127 {
128         scrub_return_address();
129         memory_protection_error(signal_fault_addr);
130 }
131
132 void memory_signal_handler_impl()
133 {
134         current_vm()->memory_signal_handler_impl();
135 }
136
137 void factor_vm::misc_signal_handler_impl()
138 {
139         scrub_return_address();
140         signal_error(signal_number);
141 }
142
143 void misc_signal_handler_impl()
144 {
145         current_vm()->misc_signal_handler_impl();
146 }
147
148 void factor_vm::fp_signal_handler_impl()
149 {
150         /* Clear pending exceptions to avoid getting stuck in a loop */
151         set_fpu_state(get_fpu_state());
152
153         scrub_return_address();
154         fp_trap_error(signal_fpu_status);
155 }
156
157 void fp_signal_handler_impl()
158 {
159         current_vm()->fp_signal_handler_impl();
160 }
161
162 }