| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | #include "master.hpp"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  | namespace factor { | 
					
						
							| 
									
										
										
										
											2009-05-04 02:46:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-11 16:29:46 -05:00
										 |  |  | bool factor_vm::fatal_erroring_p; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  | static inline void fa_diddly_atal_error() { | 
					
						
							|  |  |  |   printf("fatal_error in fatal_error!\n"); | 
					
						
							|  |  |  |   breakpoint(); | 
					
						
							|  |  |  |   ::_exit(86); | 
					
						
							| 
									
										
										
										
											2011-11-11 16:29:46 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  | void fatal_error(const char* msg, cell tagged) { | 
					
						
							|  |  |  |   if (factor_vm::fatal_erroring_p) | 
					
						
							|  |  |  |     fa_diddly_atal_error(); | 
					
						
							| 
									
										
										
										
											2011-11-11 16:29:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  |   factor_vm::fatal_erroring_p = true; | 
					
						
							| 
									
										
										
										
											2011-11-11 16:29:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  |   std::cout << "fatal_error: " << msg; | 
					
						
							|  |  |  |   std::cout << ": " << (void*)tagged; | 
					
						
							| 
									
										
										
										
											2015-10-08 14:41:59 -04:00
										 |  |  |   std::cout << std::endl << std::endl; | 
					
						
							|  |  |  |   current_vm()->dump_memory_layout(std::cout); | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  |   abort(); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  | void critical_error(const char* msg, cell tagged) { | 
					
						
							|  |  |  |   std::cout << "You have triggered a bug in Factor. Please report.\n"; | 
					
						
							|  |  |  |   std::cout << "critical_error: " << msg; | 
					
						
							|  |  |  |   std::cout << ": " << std::hex << tagged << std::dec; | 
					
						
							|  |  |  |   std::cout << std::endl; | 
					
						
							|  |  |  |   current_vm()->factorbug(); | 
					
						
							| 
									
										
										
										
											2009-10-07 09:33:54 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 01:00:08 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  | void factor_vm::general_error(vm_error_type error, cell arg1_, cell arg2_) { | 
					
						
							| 
									
										
										
										
											2014-06-21 19:24:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  |   data_root<object> arg1(arg1_, this); | 
					
						
							|  |  |  |   data_root<object> arg2(arg2_, this); | 
					
						
							| 
									
										
										
										
											2011-11-01 21:22:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  |   faulting_p = true; | 
					
						
							| 
									
										
										
										
											2010-09-17 22:20:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-04 14:45:13 -05:00
										 |  |  |   /* If we had an underflow or overflow, data or retain stack
 | 
					
						
							|  |  |  |      pointers might be out of bounds, so fix them before allocating | 
					
						
							|  |  |  |      anything */ | 
					
						
							|  |  |  |   ctx->fix_stacks(); | 
					
						
							| 
									
										
										
										
											2010-09-17 22:20:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  |   /* If error was thrown during heap scan, we re-enable the GC */ | 
					
						
							|  |  |  |   gc_off = false; | 
					
						
							| 
									
										
										
										
											2010-09-17 22:20:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  |   /* If the error handler is set, we rewind any C stack frames and
 | 
					
						
							|  |  |  |      pass the error to user-space. */ | 
					
						
							| 
									
										
										
										
											2014-12-04 14:45:13 -05:00
										 |  |  |   if (!current_gc && to_boolean(special_objects[ERROR_HANDLER_QUOT])) { | 
					
						
							| 
									
										
										
										
											2010-09-17 22:20:40 -04:00
										 |  |  | #ifdef FACTOR_DEBUG
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  |     /* Doing a GC here triggers all kinds of funny errors */ | 
					
						
							|  |  |  |     primitive_compact_gc(); | 
					
						
							| 
									
										
										
										
											2010-09-17 22:20:40 -04:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  |     /* Now its safe to allocate and GC */ | 
					
						
							|  |  |  |     cell error_object = | 
					
						
							|  |  |  |         allot_array_4(special_objects[OBJ_ERROR], tag_fixnum(error), | 
					
						
							|  |  |  |                       arg1.value(), arg2.value()); | 
					
						
							|  |  |  |     ctx->push(error_object); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-23 05:07:49 -04:00
										 |  |  |     /* Clear the data roots since arg1 and arg2's destructors won't be
 | 
					
						
							|  |  |  |        called. */ | 
					
						
							| 
									
										
										
										
											2014-06-22 12:14:36 -04:00
										 |  |  |     data_roots.clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  |     /* The unwind-native-frames subprimitive will clear faulting_p
 | 
					
						
							|  |  |  |        if it was successfully reached. */ | 
					
						
							| 
									
										
										
										
											2014-12-04 14:45:13 -05:00
										 |  |  |     unwind_native_frames(special_objects[ERROR_HANDLER_QUOT], | 
					
						
							|  |  |  |                          ctx->callstack_top); | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  |   } /* Error was thrown in early startup before error handler is set, so just
 | 
					
						
							|  |  |  |        crash. */ | 
					
						
							|  |  |  |   else { | 
					
						
							|  |  |  |     std::cout << "You have triggered a bug in Factor. Please report.\n"; | 
					
						
							|  |  |  |     std::cout << "error: " << error << std::endl; | 
					
						
							|  |  |  |     std::cout << "arg 1: "; | 
					
						
							| 
									
										
										
										
											2015-01-28 08:27:20 -05:00
										 |  |  |     print_obj(std::cout, arg1.value()); | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  |     std::cout << std::endl; | 
					
						
							|  |  |  |     std::cout << "arg 2: "; | 
					
						
							| 
									
										
										
										
											2015-01-28 08:27:20 -05:00
										 |  |  |     print_obj(std::cout, arg2.value()); | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  |     std::cout << std::endl; | 
					
						
							|  |  |  |     factorbug(); | 
					
						
							|  |  |  |     abort(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-25 04:12:45 -05:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  | void factor_vm::type_error(cell type, cell tagged) { | 
					
						
							|  |  |  |   general_error(ERROR_TYPE, tag_fixnum(type), tagged); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-25 04:12:45 -05:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  | void factor_vm::not_implemented_error() { | 
					
						
							|  |  |  |   general_error(ERROR_NOT_IMPLEMENTED, false_object, false_object); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void factor_vm::verify_memory_protection_error(cell addr) { | 
					
						
							|  |  |  |   /* Called from the OS-specific top halves of the signal handlers to
 | 
					
						
							| 
									
										
										
										
											2015-08-24 03:47:36 -04:00
										 |  |  |      make sure it's safe to dispatch to memory_signal_handler_impl. */ | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  |   if (fatal_erroring_p) | 
					
						
							|  |  |  |     fa_diddly_atal_error(); | 
					
						
							|  |  |  |   if (faulting_p && !code->safepoint_p(addr)) | 
					
						
							|  |  |  |     fatal_error("Double fault", addr); | 
					
						
							|  |  |  |   else if (fep_p) | 
					
						
							|  |  |  |     fatal_error("Memory protection fault during low-level debugger", addr); | 
					
						
							|  |  |  |   else if (atomic::load(¤t_gc_p)) | 
					
						
							|  |  |  |     fatal_error("Memory protection fault during gc", addr); | 
					
						
							| 
									
										
										
										
											2011-11-11 16:29:46 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-25 04:12:45 -05:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  | void factor_vm::divide_by_zero_error() { | 
					
						
							|  |  |  |   general_error(ERROR_DIVIDE_BY_ZERO, false_object, false_object); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-02 21:01:54 -04:00
										 |  |  | /* For testing purposes */ | 
					
						
							| 
									
										
										
										
											2014-11-25 04:12:45 -05:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  | void factor_vm::primitive_unimplemented() { not_implemented_error(); } | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-25 04:12:45 -05:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  | void factor_vm::memory_signal_handler_impl() { | 
					
						
							| 
									
										
										
										
											2015-08-24 03:47:36 -04:00
										 |  |  |   if (code->safepoint_p(signal_fault_addr)) { | 
					
						
							|  |  |  |     safepoint.handle_safepoint(this, signal_fault_pc); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else { | 
					
						
							|  |  |  |     vm_error_type type = ctx->address_to_error(signal_fault_addr); | 
					
						
							|  |  |  |     cell number = from_unsigned_cell(signal_fault_addr); | 
					
						
							|  |  |  |     general_error(type, number, false_object); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  |   if (!signal_resumable) { | 
					
						
							|  |  |  |     /* In theory we should only get here if the callstack overflowed during a
 | 
					
						
							|  |  |  |        safepoint */ | 
					
						
							|  |  |  |     general_error(ERROR_CALLSTACK_OVERFLOW, false_object, false_object); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-25 04:12:45 -05:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  | void memory_signal_handler_impl() { | 
					
						
							|  |  |  |   current_vm()->memory_signal_handler_impl(); | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:04 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-25 04:12:45 -05:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  | void factor_vm::synchronous_signal_handler_impl() { | 
					
						
							| 
									
										
										
										
											2015-08-03 17:45:20 -04:00
										 |  |  |   general_error(ERROR_SIGNAL, from_unsigned_cell(signal_number), false_object); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-05-04 02:46:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-25 04:12:45 -05:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  | void synchronous_signal_handler_impl() { | 
					
						
							|  |  |  |   current_vm()->synchronous_signal_handler_impl(); | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:04 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-25 04:12:45 -05:00
										 |  |  | /* Allocates memory (fp_trap_error())*/ | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  | void factor_vm::fp_signal_handler_impl() { | 
					
						
							|  |  |  |   /* Clear pending exceptions to avoid getting stuck in a loop */ | 
					
						
							|  |  |  |   set_fpu_state(get_fpu_state()); | 
					
						
							| 
									
										
										
										
											2010-09-01 23:46:39 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 17:45:20 -04:00
										 |  |  |   general_error(ERROR_FP_TRAP, tag_fixnum(signal_fpu_status), false_object); | 
					
						
							| 
									
										
										
										
											2009-09-06 09:44:25 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-25 04:12:45 -05:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 21:59:37 -04:00
										 |  |  | void fp_signal_handler_impl() { current_vm()->fp_signal_handler_impl(); } | 
					
						
							| 
									
										
										
										
											2009-05-04 02:46:13 -04:00
										 |  |  | } |