| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | #include "master.hpp"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-04 02:46:13 -04:00
										 |  |  | namespace factor | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-11 16:29:46 -05:00
										 |  |  | bool factor_vm::fatal_erroring_p; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline void fa_diddly_atal_error() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	printf("fatal_error in fatal_error!\n"); | 
					
						
							|  |  |  | 	breakpoint(); | 
					
						
							| 
									
										
										
										
											2011-11-12 17:47:32 -05:00
										 |  |  | 	::_exit(86); | 
					
						
							| 
									
										
										
										
											2011-11-11 16:29:46 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-07 09:33:54 -04:00
										 |  |  | void fatal_error(const char *msg, cell tagged) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-11-11 16:29:46 -05:00
										 |  |  | 	if (factor_vm::fatal_erroring_p) | 
					
						
							|  |  |  | 		fa_diddly_atal_error(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	factor_vm::fatal_erroring_p = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 	std::cout << "fatal_error: " << msg; | 
					
						
							| 
									
										
										
										
											2011-11-11 16:29:46 -05:00
										 |  |  | 	std::cout << ": " << (void*)tagged; | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 	std::cout << std::endl; | 
					
						
							| 
									
										
										
										
											2011-11-11 16:29:46 -05:00
										 |  |  | 	abort(); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-07 09:33:54 -04:00
										 |  |  | void critical_error(const char *msg, cell tagged) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 	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; | 
					
						
							| 
									
										
										
										
											2010-03-27 03:44:40 -04:00
										 |  |  | 	current_vm()->factorbug(); | 
					
						
							| 
									
										
										
										
											2009-10-07 09:33:54 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void out_of_memory() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 	std::cout << "Out of memory\n\n"; | 
					
						
							| 
									
										
										
										
											2010-03-27 03:44:40 -04:00
										 |  |  | 	current_vm()->dump_generations(); | 
					
						
							| 
									
										
										
										
											2011-11-12 17:47:32 -05:00
										 |  |  | 	abort(); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 01:00:08 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2012-08-15 22:47:59 -04:00
										 |  |  | void factor_vm::general_error(vm_error_type error, cell arg1_, cell arg2_) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-08-15 22:47:59 -04:00
										 |  |  | 	data_root<object> arg1(arg1_,this); | 
					
						
							|  |  |  | 	data_root<object> arg2(arg2_,this); | 
					
						
							| 
									
										
										
										
											2011-11-01 21:22:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-15 22:47:59 -04:00
										 |  |  | 	faulting_p = true; | 
					
						
							| 
									
										
										
										
											2010-09-17 22:20:40 -04: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(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* If error was thrown during heap scan, we re-enable the GC */ | 
					
						
							|  |  |  | 	gc_off = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	/* If the error handler is set, we rewind any C stack frames and
 | 
					
						
							|  |  |  | 	pass the error to user-space. */ | 
					
						
							| 
									
										
										
										
											2010-01-05 21:47:36 -05:00
										 |  |  | 	if(!current_gc && to_boolean(special_objects[ERROR_HANDLER_QUOT])) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2010-09-17 22:20:40 -04:00
										 |  |  | #ifdef FACTOR_DEBUG
 | 
					
						
							|  |  |  | 		/* Doing a GC here triggers all kinds of funny errors */ | 
					
						
							|  |  |  | 		primitive_compact_gc(); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-17 22:20:40 -04:00
										 |  |  | 		/* Now its safe to allocate and GC */ | 
					
						
							|  |  |  | 		cell error_object = allot_array_4(special_objects[OBJ_ERROR], | 
					
						
							| 
									
										
										
										
											2012-08-15 22:47:59 -04:00
										 |  |  | 			tag_fixnum(error),arg1.value(),arg2.value()); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-17 22:20:40 -04:00
										 |  |  | 		ctx->push(error_object); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-15 22:47:59 -04:00
										 |  |  | 		/* Reset local roots */ | 
					
						
							|  |  |  | 		data_roots.clear(); | 
					
						
							|  |  |  | 		bignum_roots.clear(); | 
					
						
							|  |  |  | 		code_roots.clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-12 15:57:24 -05:00
										 |  |  | 		/* The unwind-native-frames subprimitive will clear faulting_p
 | 
					
						
							|  |  |  | 		if it was successfully reached. */ | 
					
						
							| 
									
										
										
										
											2010-09-02 23:55:52 -04:00
										 |  |  | 		unwind_native_frames(special_objects[ERROR_HANDLER_QUOT], | 
					
						
							|  |  |  | 			ctx->callstack_top); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-12-20 17:38:01 -05:00
										 |  |  | 	/* Error was thrown in early startup before error handler is set, so just
 | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	crash. */ | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 		std::cout << "You have triggered a bug in Factor. Please report.\n"; | 
					
						
							| 
									
										
										
										
											2010-09-17 22:20:40 -04:00
										 |  |  | 		std::cout << "error: " << error << std::endl; | 
					
						
							| 
									
										
										
										
											2012-08-15 22:47:59 -04:00
										 |  |  | 		std::cout << "arg 1: "; print_obj(arg1.value()); std::cout << std::endl; | 
					
						
							|  |  |  | 		std::cout << "arg 2: "; print_obj(arg2.value()); std::cout << std::endl; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		factorbug(); | 
					
						
							| 
									
										
										
										
											2011-11-11 16:29:46 -05:00
										 |  |  | 		abort(); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-27 07:33:28 -04:00
										 |  |  | void factor_vm::type_error(cell type, cell tagged) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-03-27 07:33:28 -04:00
										 |  |  | 	general_error(ERROR_TYPE,tag_fixnum(type),tagged); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-27 07:33:28 -04:00
										 |  |  | void factor_vm::not_implemented_error() | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-03-27 07:33:28 -04:00
										 |  |  | 	general_error(ERROR_NOT_IMPLEMENTED,false_object,false_object); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-11 16:29:46 -05:00
										 |  |  | void factor_vm::verify_memory_protection_error(cell addr) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	/* Called from the OS-specific top halves of the signal handlers to
 | 
					
						
							|  |  |  | 	make sure it's safe to dispatch to memory_protection_error */ | 
					
						
							|  |  |  | 	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); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 00:30:55 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2011-11-17 20:13:59 -05:00
										 |  |  | void factor_vm::memory_protection_error(cell pc, cell addr) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-10-19 18:39:44 -04:00
										 |  |  | 	if(code->safepoint_p(addr)) | 
					
						
							| 
									
										
										
										
											2011-11-17 20:13:59 -05:00
										 |  |  | 		safepoint.handle_safepoint(this, pc); | 
					
						
							| 
									
										
										
										
											2011-10-19 18:39:44 -04:00
										 |  |  | 	else if(ctx->datastack_seg->underflow_p(addr)) | 
					
						
							| 
									
										
										
										
											2010-09-02 23:55:52 -04:00
										 |  |  | 		general_error(ERROR_DATASTACK_UNDERFLOW,false_object,false_object); | 
					
						
							| 
									
										
										
										
											2010-03-27 07:33:28 -04:00
										 |  |  | 	else if(ctx->datastack_seg->overflow_p(addr)) | 
					
						
							| 
									
										
										
										
											2010-09-02 23:55:52 -04:00
										 |  |  | 		general_error(ERROR_DATASTACK_OVERFLOW,false_object,false_object); | 
					
						
							| 
									
										
										
										
											2010-03-27 07:33:28 -04:00
										 |  |  | 	else if(ctx->retainstack_seg->underflow_p(addr)) | 
					
						
							| 
									
										
										
										
											2010-09-02 23:55:52 -04:00
										 |  |  | 		general_error(ERROR_RETAINSTACK_UNDERFLOW,false_object,false_object); | 
					
						
							| 
									
										
										
										
											2010-03-27 07:33:28 -04:00
										 |  |  | 	else if(ctx->retainstack_seg->overflow_p(addr)) | 
					
						
							| 
									
										
										
										
											2010-09-02 23:55:52 -04:00
										 |  |  | 		general_error(ERROR_RETAINSTACK_OVERFLOW,false_object,false_object); | 
					
						
							| 
									
										
										
										
											2010-03-27 07:45:11 -04:00
										 |  |  | 	else if(ctx->callstack_seg->underflow_p(addr)) | 
					
						
							| 
									
										
										
										
											2010-09-02 23:55:52 -04:00
										 |  |  | 		general_error(ERROR_CALLSTACK_OVERFLOW,false_object,false_object); | 
					
						
							| 
									
										
										
										
											2010-03-29 01:53:20 -04:00
										 |  |  | 	else if(ctx->callstack_seg->overflow_p(addr)) | 
					
						
							| 
									
										
										
										
											2010-09-02 23:55:52 -04:00
										 |  |  | 		general_error(ERROR_CALLSTACK_UNDERFLOW,false_object,false_object); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	else | 
					
						
							| 
									
										
										
										
											2010-09-02 23:55:52 -04:00
										 |  |  | 		general_error(ERROR_MEMORY,from_unsigned_cell(addr),false_object); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 00:30:55 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2010-09-02 23:55:52 -04:00
										 |  |  | void factor_vm::signal_error(cell signal) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-09-02 23:55:52 -04:00
										 |  |  | 	general_error(ERROR_SIGNAL,from_unsigned_cell(signal),false_object); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::divide_by_zero_error() | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-03-27 07:33:28 -04:00
										 |  |  | 	general_error(ERROR_DIVIDE_BY_ZERO,false_object,false_object); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-02 23:55:52 -04:00
										 |  |  | void factor_vm::fp_trap_error(unsigned int fpu_status) | 
					
						
							| 
									
										
										
										
											2009-09-06 09:44:25 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-09-02 23:55:52 -04:00
										 |  |  | 	general_error(ERROR_FP_TRAP,tag_fixnum(fpu_status),false_object); | 
					
						
							| 
									
										
										
										
											2009-09-06 09:44:25 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-02 21:01:54 -04:00
										 |  |  | /* For testing purposes */ | 
					
						
							| 
									
										
										
										
											2009-09-27 14:42:18 -04:00
										 |  |  | void factor_vm::primitive_unimplemented() | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-05-02 21:01:54 -04:00
										 |  |  | 	not_implemented_error(); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::memory_signal_handler_impl() | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-11-17 20:13:59 -05:00
										 |  |  | 	memory_protection_error(signal_fault_pc, signal_fault_addr); | 
					
						
							| 
									
										
										
										
											2011-10-25 18:42:08 -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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:04 -04:00
										 |  |  | void memory_signal_handler_impl() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-03-27 03:44:40 -04:00
										 |  |  | 	current_vm()->memory_signal_handler_impl(); | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:04 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-19 18:39:44 -04:00
										 |  |  | void factor_vm::synchronous_signal_handler_impl() | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-09-02 23:55:52 -04:00
										 |  |  | 	signal_error(signal_number); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-05-04 02:46:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-19 18:39:44 -04:00
										 |  |  | void synchronous_signal_handler_impl() | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:04 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-10-20 01:47:14 -04:00
										 |  |  | 	current_vm()->synchronous_signal_handler_impl(); | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:04 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::fp_signal_handler_impl() | 
					
						
							| 
									
										
										
										
											2009-09-06 09:44:25 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-09-01 23:46:39 -04:00
										 |  |  | 	/* Clear pending exceptions to avoid getting stuck in a loop */ | 
					
						
							|  |  |  | 	set_fpu_state(get_fpu_state()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-02 23:55:52 -04:00
										 |  |  | 	fp_trap_error(signal_fpu_status); | 
					
						
							| 
									
										
										
										
											2009-09-06 09:44:25 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:04 -04:00
										 |  |  | void fp_signal_handler_impl() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-03-27 03:44:40 -04:00
										 |  |  | 	current_vm()->fp_signal_handler_impl(); | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:04 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-05-04 02:46:13 -04:00
										 |  |  | } |