2009-05-02 05:04:19 -04:00
|
|
|
#include "master.hpp"
|
|
|
|
|
2009-05-04 02:46:13 -04:00
|
|
|
namespace factor
|
|
|
|
{
|
|
|
|
|
2009-05-02 05:04:19 -04:00
|
|
|
/* Global variables used to pass fault handler state from signal handler to
|
|
|
|
user-space */
|
2009-05-04 05:50:24 -04:00
|
|
|
cell signal_number;
|
|
|
|
cell signal_fault_addr;
|
|
|
|
stack_frame *signal_callstack_top;
|
2009-05-02 05:04:19 -04:00
|
|
|
|
2009-05-05 12:33:35 -04:00
|
|
|
void out_of_memory()
|
2009-05-02 05:04:19 -04:00
|
|
|
{
|
|
|
|
print_string("Out of memory\n\n");
|
|
|
|
dump_generations();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2009-05-04 08:00:06 -04:00
|
|
|
void fatal_error(const char* msg, cell tagged)
|
2009-05-02 05:04:19 -04:00
|
|
|
{
|
|
|
|
print_string("fatal_error: "); print_string(msg);
|
|
|
|
print_string(": "); print_cell_hex(tagged); nl();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2009-05-04 08:00:06 -04:00
|
|
|
void critical_error(const char* msg, cell tagged)
|
2009-05-02 05:04:19 -04:00
|
|
|
{
|
|
|
|
print_string("You have triggered a bug in Factor. Please report.\n");
|
|
|
|
print_string("critical_error: "); print_string(msg);
|
|
|
|
print_string(": "); print_cell_hex(tagged); nl();
|
|
|
|
factorbug();
|
|
|
|
}
|
|
|
|
|
2009-05-04 05:50:24 -04:00
|
|
|
void 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. */
|
|
|
|
if(userenv[BREAK_ENV] != F)
|
|
|
|
{
|
|
|
|
/* If error was thrown during heap scan, we re-enable the GC */
|
|
|
|
gc_off = false;
|
|
|
|
|
|
|
|
/* Reset local roots */
|
2009-09-08 15:21:09 -04:00
|
|
|
gc_locals.clear();
|
|
|
|
gc_bignums.clear();
|
2009-05-02 05:04:19 -04:00
|
|
|
|
|
|
|
/* If we had an underflow or overflow, stack pointers might be
|
|
|
|
out of bounds */
|
|
|
|
fix_stacks();
|
|
|
|
|
|
|
|
dpush(error);
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
|
|
|
callstack_top = fix_callstack_top(callstack_top,
|
|
|
|
stack_chain->callstack_bottom);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
callstack_top = stack_chain->callstack_top;
|
|
|
|
|
|
|
|
throw_impl(userenv[BREAK_ENV],callstack_top);
|
|
|
|
}
|
|
|
|
/* Error was thrown in early startup before error handler is set, just
|
|
|
|
crash. */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
print_string("You have triggered a bug in Factor. Please report.\n");
|
|
|
|
print_string("early_error: ");
|
|
|
|
print_obj(error);
|
|
|
|
nl();
|
|
|
|
factorbug();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-04 05:50:24 -04:00
|
|
|
void general_error(vm_error_type error, cell arg1, cell arg2,
|
|
|
|
stack_frame *callstack_top)
|
2009-05-02 05:04:19 -04:00
|
|
|
{
|
|
|
|
throw_error(allot_array_4(userenv[ERROR_ENV],
|
|
|
|
tag_fixnum(error),arg1,arg2),callstack_top);
|
|
|
|
}
|
|
|
|
|
2009-05-04 05:50:24 -04:00
|
|
|
void type_error(cell type, cell tagged)
|
2009-05-02 05:04:19 -04:00
|
|
|
{
|
|
|
|
general_error(ERROR_TYPE,tag_fixnum(type),tagged,NULL);
|
|
|
|
}
|
|
|
|
|
2009-05-05 12:33:35 -04:00
|
|
|
void not_implemented_error()
|
2009-05-02 05:04:19 -04:00
|
|
|
{
|
|
|
|
general_error(ERROR_NOT_IMPLEMENTED,F,F,NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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-05-04 05:50:24 -04:00
|
|
|
bool 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-05-04 05:50:24 -04:00
|
|
|
void memory_protection_error(cell addr, stack_frame *native_stack)
|
2009-05-02 05:04:19 -04:00
|
|
|
{
|
|
|
|
if(in_page(addr, ds_bot, 0, -1))
|
|
|
|
general_error(ERROR_DS_UNDERFLOW,F,F,native_stack);
|
|
|
|
else if(in_page(addr, ds_bot, ds_size, 0))
|
|
|
|
general_error(ERROR_DS_OVERFLOW,F,F,native_stack);
|
|
|
|
else if(in_page(addr, rs_bot, 0, -1))
|
|
|
|
general_error(ERROR_RS_UNDERFLOW,F,F,native_stack);
|
|
|
|
else if(in_page(addr, rs_bot, rs_size, 0))
|
|
|
|
general_error(ERROR_RS_OVERFLOW,F,F,native_stack);
|
|
|
|
else if(in_page(addr, nursery.end, 0, 0))
|
|
|
|
critical_error("allot_object() missed GC check",0);
|
|
|
|
else
|
|
|
|
general_error(ERROR_MEMORY,allot_cell(addr),F,native_stack);
|
|
|
|
}
|
|
|
|
|
2009-05-04 05:50:24 -04:00
|
|
|
void signal_error(int signal, stack_frame *native_stack)
|
2009-05-02 05:04:19 -04:00
|
|
|
{
|
|
|
|
general_error(ERROR_SIGNAL,tag_fixnum(signal),F,native_stack);
|
|
|
|
}
|
|
|
|
|
2009-05-05 12:33:35 -04:00
|
|
|
void divide_by_zero_error()
|
2009-05-02 05:04:19 -04:00
|
|
|
{
|
|
|
|
general_error(ERROR_DIVIDE_BY_ZERO,F,F,NULL);
|
|
|
|
}
|
|
|
|
|
2009-09-12 19:15:16 -04:00
|
|
|
void fp_trap_error(stack_frame *signal_callstack_top)
|
2009-09-06 09:44:25 -04:00
|
|
|
{
|
2009-09-12 19:15:16 -04:00
|
|
|
general_error(ERROR_FP_TRAP,F,F,signal_callstack_top);
|
2009-09-06 09:44:25 -04:00
|
|
|
}
|
|
|
|
|
2009-05-04 02:00:30 -04:00
|
|
|
PRIMITIVE(call_clear)
|
2009-05-02 05:04:19 -04:00
|
|
|
{
|
2009-05-02 21:01:54 -04:00
|
|
|
throw_impl(dpop(),stack_chain->callstack_bottom);
|
2009-05-02 05:04:19 -04:00
|
|
|
}
|
|
|
|
|
2009-05-02 21:01:54 -04:00
|
|
|
/* For testing purposes */
|
2009-05-04 02:00:30 -04:00
|
|
|
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-05-05 12:33:35 -04:00
|
|
|
void 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-05-05 12:33:35 -04:00
|
|
|
void 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-09-06 09:44:25 -04:00
|
|
|
void fp_signal_handler_impl()
|
|
|
|
{
|
2009-09-12 19:15:16 -04:00
|
|
|
fp_trap_error(signal_callstack_top);
|
2009-09-06 09:44:25 -04:00
|
|
|
}
|
|
|
|
|
2009-05-04 02:46:13 -04:00
|
|
|
}
|