| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | #include "master.hpp"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-04 02:46:13 -04:00
										 |  |  | namespace factor | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-07 09:33:54 -04:00
										 |  |  | void fatal_error(const char *msg, cell tagged) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 	std::cout << "fatal_error: " << msg; | 
					
						
							|  |  |  | 	std::cout << ": " << std::hex << tagged << std::dec; | 
					
						
							|  |  |  | 	std::cout << std::endl; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	exit(1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2009-10-14 14:14:57 -04:00
										 |  |  | 	tls_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"; | 
					
						
							| 
									
										
										
										
											2009-10-14 14:14:57 -04:00
										 |  |  | 	tls_vm()->dump_generations(); | 
					
						
							| 
									
										
										
										
											2009-10-07 09:33:54 -04:00
										 |  |  | 	exit(1); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::throw_error(cell error, stack_frame *callstack_top) | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	{ | 
					
						
							|  |  |  | 		/* If error was thrown during heap scan, we re-enable the GC */ | 
					
						
							|  |  |  | 		gc_off = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* Reset local roots */ | 
					
						
							| 
									
										
										
										
											2009-11-02 19:10:34 -05:00
										 |  |  | 		data_roots.clear(); | 
					
						
							|  |  |  | 		bignum_roots.clear(); | 
					
						
							| 
									
										
										
										
											2009-11-03 05:56:58 -05:00
										 |  |  | 		code_roots.clear(); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/* If we had an underflow or overflow, stack pointers might be
 | 
					
						
							|  |  |  | 		out of bounds */ | 
					
						
							| 
									
										
										
										
											2009-12-18 16:59:56 -05:00
										 |  |  | 		ctx->fix_stacks(); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-18 16:59:56 -05:00
										 |  |  | 		ctx->push(error); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/* Errors thrown from C code pass NULL for this parameter.
 | 
					
						
							|  |  |  | 		Errors thrown from Factor code, or signal handlers, pass the | 
					
						
							|  |  |  | 		actual stack pointer at the time, since the saved pointer is | 
					
						
							|  |  |  | 		not necessarily up to date at that point. */ | 
					
						
							|  |  |  | 		if(callstack_top) | 
					
						
							| 
									
										
										
										
											2009-10-16 12:39:22 -04:00
										 |  |  | 			callstack_top = fix_callstack_top(callstack_top,ctx->callstack_bottom); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		else | 
					
						
							| 
									
										
										
										
											2009-10-16 12:39:22 -04:00
										 |  |  | 			callstack_top = ctx->callstack_top; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-05 21:47:36 -05:00
										 |  |  | 		unwind_native_frames(special_objects[ERROR_HANDLER_QUOT],callstack_top); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	/* Error was thrown in early startup before error handler is set, just
 | 
					
						
							|  |  |  | 	crash. */ | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 		std::cout << "You have triggered a bug in Factor. Please report.\n"; | 
					
						
							|  |  |  | 		std::cout << "early_error: "; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		print_obj(error); | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | 		std::cout << std::endl; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		factorbug(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::general_error(vm_error_type error, cell arg1, cell arg2, stack_frame *callstack_top) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-23 01:33:53 -04:00
										 |  |  | 	throw_error(allot_array_4(special_objects[OBJ_ERROR], | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 		tag_fixnum(error),arg1,arg2),callstack_top); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::type_error(cell type, cell tagged) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							|  |  |  | 	general_error(ERROR_TYPE,tag_fixnum(type),tagged,NULL); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::not_implemented_error() | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-18 21:26:21 -04:00
										 |  |  | 	general_error(ERROR_NOT_IMPLEMENTED,false_object,false_object,NULL); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Test if 'fault' is in the guard page at the top or bottom (depending on
 | 
					
						
							|  |  |  | offset being 0 or -1) of area+area_size */ | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | bool factor_vm::in_page(cell fault, cell area, cell area_size, int offset) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							|  |  |  | 	int pagesize = getpagesize(); | 
					
						
							|  |  |  | 	area += area_size; | 
					
						
							|  |  |  | 	area += offset * pagesize; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return fault >= area && fault <= area + pagesize; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::memory_protection_error(cell addr, stack_frame *native_stack) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-03-26 22:44:43 -04:00
										 |  |  | 	if(in_page(addr, ctx->datastack_seg->start, 0, -1)) | 
					
						
							| 
									
										
										
										
											2009-10-18 21:26:21 -04:00
										 |  |  | 		general_error(ERROR_DS_UNDERFLOW,false_object,false_object,native_stack); | 
					
						
							| 
									
										
										
										
											2010-03-26 22:44:43 -04:00
										 |  |  | 	else if(in_page(addr, ctx->datastack_seg->start, datastack_size, 0)) | 
					
						
							| 
									
										
										
										
											2009-10-18 21:26:21 -04:00
										 |  |  | 		general_error(ERROR_DS_OVERFLOW,false_object,false_object,native_stack); | 
					
						
							| 
									
										
										
										
											2010-03-26 22:44:43 -04:00
										 |  |  | 	else if(in_page(addr, ctx->retainstack_seg->start, 0, -1)) | 
					
						
							| 
									
										
										
										
											2009-10-18 21:26:21 -04:00
										 |  |  | 		general_error(ERROR_RS_UNDERFLOW,false_object,false_object,native_stack); | 
					
						
							| 
									
										
										
										
											2010-03-26 22:44:43 -04:00
										 |  |  | 	else if(in_page(addr, ctx->retainstack_seg->start, retainstack_size, 0)) | 
					
						
							| 
									
										
										
										
											2009-10-18 21:26:21 -04:00
										 |  |  | 		general_error(ERROR_RS_OVERFLOW,false_object,false_object,native_stack); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 	else if(in_page(addr, nursery.end, 0, 0)) | 
					
						
							|  |  |  | 		critical_error("allot_object() missed GC check",0); | 
					
						
							|  |  |  | 	else | 
					
						
							| 
									
										
										
										
											2009-10-18 21:26:21 -04:00
										 |  |  | 		general_error(ERROR_MEMORY,allot_cell(addr),false_object,native_stack); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-24 08:17:18 -05:00
										 |  |  | void factor_vm::signal_error(cell signal, stack_frame *native_stack) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-22 00:37:51 -04:00
										 |  |  | 	general_error(ERROR_SIGNAL,allot_cell(signal),false_object,native_stack); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-18 21:26:21 -04:00
										 |  |  | 	general_error(ERROR_DIVIDE_BY_ZERO,false_object,false_object,NULL); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::fp_trap_error(unsigned int fpu_status, stack_frame *signal_callstack_top) | 
					
						
							| 
									
										
										
										
											2009-09-06 09:44:25 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-18 21:26:21 -04:00
										 |  |  | 	general_error(ERROR_FP_TRAP,tag_fixnum(fpu_status),false_object,signal_callstack_top); | 
					
						
							| 
									
										
										
										
											2009-09-06 09:44:25 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-27 14:42:18 -04:00
										 |  |  | void factor_vm::primitive_call_clear() | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-01-05 21:47:36 -05:00
										 |  |  | 	unwind_native_frames(ctx->pop(),ctx->callstack_bottom); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-05-02 21:01:54 -04:00
										 |  |  | 	memory_protection_error(signal_fault_addr,signal_callstack_top); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:04 -04:00
										 |  |  | void memory_signal_handler_impl() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-14 14:14:57 -04:00
										 |  |  | 	tls_vm()->memory_signal_handler_impl(); | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:04 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 14:05:46 -04:00
										 |  |  | void factor_vm::misc_signal_handler_impl() | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-05-02 21:01:54 -04:00
										 |  |  | 	signal_error(signal_number,signal_callstack_top); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-05-04 02:46:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:04 -04:00
										 |  |  | void misc_signal_handler_impl() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-14 14:14:57 -04:00
										 |  |  | 	tls_vm()->misc_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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-09-14 00:37:28 -04:00
										 |  |  | 	fp_trap_error(signal_fpu_status,signal_callstack_top); | 
					
						
							| 
									
										
										
										
											2009-09-06 09:44:25 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:04 -04:00
										 |  |  | void fp_signal_handler_impl() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-14 14:14:57 -04:00
										 |  |  | 	tls_vm()->fp_signal_handler_impl(); | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:04 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-04 02:46:13 -04:00
										 |  |  | } |