From 48bcffdce733ca138beafe6216c0c8c97b8cdaae Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Thu, 29 Mar 2018 22:33:53 +0100 Subject: [PATCH] try to fix exceptions --- C/errors.c | 52 +++++---- C/eval.c | 3 +- C/exec.c | 169 +++++++++++++++++------------- C/qlyr.c | 2 +- C/utilpreds.c | 1 + H/YapEval.h | 8 +- include/YapError.h | 10 +- os/iopreds.c | 7 +- packages/jpl/jpl.pl | 5 +- packages/jpl/src/c/CMakeLists.txt | 1 + 10 files changed, 145 insertions(+), 113 deletions(-) diff --git a/C/errors.c b/C/errors.c index 164882908..28f8b37f3 100755 --- a/C/errors.c +++ b/C/errors.c @@ -166,7 +166,7 @@ bool Yap_HandleError__(const char *file, const char *function, int lineno, switch (err) { case RESOURCE_ERROR_STACK: if (!Yap_gc(arity, ENV, gc_P(P, CP))) { - Yap_Error__(file, function, lineno, RESOURCE_ERROR_STACK, ARG1, serr); + Yap_Error__(false, file, function, lineno, RESOURCE_ERROR_STACK, ARG1, serr); return false; } return true; @@ -176,18 +176,18 @@ bool Yap_HandleError__(const char *file, const char *function, int lineno, } if (!Yap_ExpandPreAllocCodeSpace(0, NULL, TRUE)) { /* crash in flames */ - Yap_Error__(file, function, lineno, RESOURCE_ERROR_AUXILIARY_STACK, ARG1, + Yap_Error__(false, file, function, lineno, RESOURCE_ERROR_AUXILIARY_STACK, ARG1, serr); return false; } return true; case RESOURCE_ERROR_HEAP: if (!Yap_growheap(FALSE, 0, NULL)) { - Yap_Error__(file, function, lineno, RESOURCE_ERROR_HEAP, ARG2, serr); + Yap_Error__(false, file, function, lineno, RESOURCE_ERROR_HEAP, ARG2, serr); return false; } default: - Yap_Error__(file, function, lineno, err, TermNil, serr); + Yap_Error__(false, file, function, lineno, err, TermNil, serr); return false; } } @@ -218,24 +218,25 @@ int Yap_SWIHandleError(const char *s, ...) { Yap_Error(RESOURCE_ERROR_AUXILIARY_STACK, ARG1, serr); return FALSE; } - return TRUE; + return true; case RESOURCE_ERROR_HEAP: - if (!Yap_growheap(FALSE, 0, NULL)) { + if (!Yap_growheap(false, 0, NULL)) { Yap_Error(RESOURCE_ERROR_HEAP, ARG2, serr); - return FALSE; + return false; } default: Yap_Error(err, TermNil, serr); - return (FALSE); + return false; } } void Yap_RestartYap(int flag) { CACHE_REGS + fprintf(stderr,"HR=%p\n", HR); #if PUSH_REGS restore_absmi_regs(&Yap_standard_regs); #endif - siglongjmp(*LOCAL_RestartEnv, 1); + siglongjmp(*LOCAL_RestartEnv, flag); } static void error_exit_yap(int value) { @@ -335,7 +336,8 @@ void Yap_pushErrorContext(yap_error_descriptor_t *new_error) { static void reset_error_description(void) { - sigjmp_buf *bf = LOCAL_ActiveError->top_error; + yap_error_descriptor_t *bf = LOCAL_ActiveError->top_error; + if (Yap_HasException()) memset(LOCAL_ActiveError, 0, sizeof(*LOCAL_ActiveError)); LOCAL_ActiveError->top_error = bf; @@ -365,12 +367,15 @@ void Yap_ThrowError__(const char *file, const char *function, int lineno, (void)vsprintf(tnpbuf, fmt, ap); #endif // fprintf(stderr, "warning: "); - Yap_Error__(file, function, lineno, type, where, tmpbuf); + Yap_Error__(true, file, function, lineno, type, where, tmpbuf); } else { - Yap_Error__(file, function, lineno, type, where); + Yap_Error__(true, file, function, lineno, type, where); + } + if (LOCAL_RestartEnv) { + Yap_RestartYap(5); + } else { + exit(5); } - if (LOCAL_RestartEnv) - siglongjmp(*LOCAL_RestartEnv, 5); } /** @@ -406,7 +411,7 @@ void Yap_ThrowError__(const char *file, const char *function, int lineno, * * + i=i(Comment): an user-written comment on this bug. */ -yamop *Yap_Error__(const char *file, const char *function, int lineno, +yamop *Yap_Error__(bool throw, const char *file, const char *function, int lineno, yap_error_number type, Term where, ...) { CACHE_REGS va_list ap; @@ -573,7 +578,7 @@ yamop *Yap_Error__(const char *file, const char *function, int lineno, LOCAL_PredEntriesCounterOn = FALSE; LOCAL_RetriesCounterOn = FALSE; Yap_JumpToEnv(MkAtomTerm(AtomCallCounter)); - P = (yamop *)FAILCODE; + P = FAILCODE; LOCAL_PrologMode &= ~InErrorMode; return (P); case PRED_ENTRY_COUNTER_UNDERFLOW_EVENT: @@ -582,7 +587,7 @@ yamop *Yap_Error__(const char *file, const char *function, int lineno, LOCAL_PredEntriesCounterOn = FALSE; LOCAL_RetriesCounterOn = FALSE; Yap_JumpToEnv(MkAtomTerm(AtomCallAndRetryCounter)); - P = (yamop *)FAILCODE; + P = FAILCODE; LOCAL_PrologMode &= ~InErrorMode; return (P); case RETRY_COUNTER_UNDERFLOW_EVENT: @@ -659,11 +664,16 @@ yamop *Yap_Error__(const char *file, const char *function, int lineno, } if (LOCAL_DoingUndefp) { Yap_PrintWarning(error_t); - } else { - //memset(LOCAL_ActiveError, 0, sizeof(*LOCAL_ActiveError)); - Yap_JumpToEnv(error_t); + return P; } - P = (yamop *)FAILCODE; + //reset_error_description(); + Yap_PutException(error_t); + fprintf(stderr,"HR before jmp=%p\n", HR); + if (throw) + LOCAL_BallTerm = Yap_StoreTermInDB(error_t, 5); + else + Yap_JumpToEnv(error_t); + fprintf(stderr,"HR after jmp=%p\n", HR); LOCAL_PrologMode &= ~InErrorMode; return P; } diff --git a/C/eval.c b/C/eval.c index fefc72cee..bd4456c1c 100644 --- a/C/eval.c +++ b/C/eval.c @@ -93,6 +93,7 @@ static Term get_matrix_element(Term t1, Term t2 USES_REGS) { static Term Eval(Term t USES_REGS) { if (IsVarTerm(t)) { + fprintf(stderr,"HR before jmp=%p v=%p\n", HR, VarOfTerm(t)); Yap_ArithError(INSTANTIATION_ERROR, t, "in arithmetic"); } else if (IsNumTerm(t)) { return t; @@ -387,7 +388,7 @@ void Yap_EvalError__(const char *file, const char *function, int lineno, buf[0] = '\0'; } va_end(ap); - Yap_ThrowError__(file, function, lineno, type, where, buf); + Yap_Error__(false, file, function, lineno, type, where, buf); } /** diff --git a/C/exec.c b/C/exec.c index 0b3863b6a..b0a44ef92 100755 --- a/C/exec.c +++ b/C/exec.c @@ -1390,76 +1390,85 @@ static bool exec_absmi(bool top, yap_reset_t reset_mode USES_REGS) { LOCAL_CBorder = LCL0 - (CELL *)B; sigjmp_buf signew, *sighold = LOCAL_RestartEnv; LOCAL_RestartEnv = &signew; + REGSTORE *old_rs = Yap_regp; if (top && (lval = sigsetjmp(signew, 1)) != 0) { - switch (lval) { - case 1: { /* restart */ - /* otherwise, SetDBForThrow will fail entering critical mode */ - LOCAL_PrologMode = UserMode; - /* find out where to cut to */ - /* siglongjmp resets the TR hardware register */ - /* TR and B are crucial, they might have been changed, or not */ - restore_TR(); - restore_B(); - /* H is not so important, because we're gonna backtrack */ - restore_H(); - /* set stack */ - ASP = (CELL *)PROTECT_FROZEN_B(B); - /* forget any signals active, we're reborne */ - LOCAL_Signals = 0; - CalculateStackGap(PASS_REGS1); - LOCAL_PrologMode = UserMode; - P = (yamop *)FAILCODE; - } break; - case 2: { - /* arithmetic exception */ - /* must be done here, otherwise siglongjmp will clobber all the - * registers - */ - /* reset the registers so that we don't have trash in abstract - * machine */ - Yap_set_fpu_exceptions( - getAtomicGlobalPrologFlag(ARITHMETIC_EXCEPTIONS_FLAG)); - P = (yamop *)FAILCODE; - LOCAL_PrologMode = UserMode; - } break; - case 3: { /* saved state */ - LOCAL_CBorder = OldBorder; - LOCAL_RestartEnv = sighold; - return false; - } - case 4: - /* abort */ - /* can be called from anywhere, must reset registers, - */ - while (B) { - Yap_JumpToEnv(TermDAbort); + switch (lval) { + case 1: { /* restart */ + /* otherwise, SetDBForThrow will fail entering critical mode */ + LOCAL_PrologMode = UserMode; + /* find out where to cut to */ + /* siglongjmp resets the TR hardware register */ + /* TR and B are crucial, they might have been changed, or not */ + restore_TR(); + restore_B(); + /* H is not so important, because we're gonna backtrack */ + restore_H(); + /* set stack */ + ASP = (CELL *) PROTECT_FROZEN_B(B); + /* forget any signals active, we're reborne */ + LOCAL_Signals = 0; + CalculateStackGap(PASS_REGS1); + LOCAL_PrologMode = UserMode; + P = (yamop *) FAILCODE; + } + break; + case 2: { + /* arithmetic exception */ + /* must be done here, otherwise siglongjmp will clobber all the + * registers + */ + /* reset the registers so that we don't have trash in abstract + * machine */ + Yap_set_fpu_exceptions( + getAtomicGlobalPrologFlag(ARITHMETIC_EXCEPTIONS_FLAG)); + P = (yamop *) FAILCODE; + LOCAL_PrologMode = UserMode; + } + break; + case 3: { /* saved state */ + LOCAL_CBorder = OldBorder; + LOCAL_RestartEnv = sighold; + return false; + } + case 4: + /* abort */ + /* can be called from anywhere, must reset registers, + */ + while (B) { + Yap_JumpToEnv(TermDAbort); + } + LOCAL_PrologMode &= ~AbortMode; + P = (yamop *) FAILCODE; + LOCAL_RestartEnv = sighold; + return false; + break; + case 5: + // going up, unless there is no up to go to. or someone + // but we should inform the caller on what happened. + + Yap_regp = old_rs; + fprintf(stderr,"HR before jmp=%p\n", HR); + Yap_JumpToEnv(0); + fprintf(stderr,"HR after jmp=%p\n", HR); + LOCAL_PrologMode = UserMode; + ASP = (CELL *) B; + if (B == NULL || B->cp_b == NULL || (CELL*)(B->cp_b) > LCL0 - LOCAL_CBorder) { + LOCAL_RestartEnv = sighold; + LOCAL_CBorder = OldBorder; + fprintf(stderr, "HR after sigset A=%p\n", HR); + return false; + } + fprintf(stderr, "HR after sigset=B %p\n", HR); + P = FAILCODE; + } - LOCAL_PrologMode &= ~AbortMode; - P = (yamop *)FAILCODE; - LOCAL_RestartEnv = sighold; - return false; - break; - case 5: - // going up, unless there is no up to go to. or someone - // but we should inform the caller on what happened. - if (B && B->cp_b && B->cp_b <= (choiceptr)(LCL0 - LOCAL_CBorder)) { - break; - } - LOCAL_RestartEnv = sighold; - LOCAL_PrologMode = UserMode; - LOCAL_CBorder = OldBorder; - return false; - default: - /* do nothing */ - LOCAL_PrologMode = UserMode; - } - } else { - LOCAL_PrologMode = UserMode; } + LOCAL_PrologMode = UserMode; YENV = ASP; YENV[E_CB] = Unsigned(B); out = Yap_absmi(0); + fprintf(stderr, "HR after absmi=%p\n", HR); /* make sure we don't leave a FAIL signal hanging around */ Yap_get_signal(YAP_FAIL_SIGNAL); if (!Yap_has_a_signal()) @@ -1958,14 +1967,14 @@ static Int cut_up_to_next_disjunction(USES_REGS1) { return TRUE; } -/** +/** * Reset the Prolog engine . If _Hard_ resèt the global stack_el. If * p_no_use_'soft_float keei - * - * @param mode - * @param hard - * - * @return + * + * @param mode + * @param hard + * + * @return */ bool Yap_Reset(yap_reset_t mode, bool hard) { CACHE_REGS @@ -2008,7 +2017,7 @@ bool is_cleanup_cp(choiceptr cp_b) { return pe == PredSafeCallCleanup; } -static Int JumpToEnv() { + static Int JumpToEnv() { choiceptr handler = B; /* just keep the throwm object away, we don't need to care about it */ @@ -2025,7 +2034,7 @@ static Int JumpToEnv() { if (LOCAL_PrologMode & AsyncIntMode) { Yap_signal(YAP_FAIL_SIGNAL); } - + B = handler; P = FAILCODE; return true; @@ -2033,6 +2042,7 @@ static Int JumpToEnv() { bool Yap_JumpToEnv(Term t) { CACHE_REGS + if (t) LOCAL_BallTerm = Yap_StoreTermInDB(t, 0); if (!LOCAL_BallTerm) return false; @@ -2181,6 +2191,7 @@ static Int jump_env(USES_REGS1) { t = Yap_PopTermFromDB(LOCAL_BallTerm); } LOCAL_BallTerm = NULL; + return t; } @@ -2201,20 +2212,30 @@ static Int jump_env(USES_REGS1) { } bool Yap_ResetException(int wid) { + // reset errir descriptir + yap_error_descriptor_t *bf = REMOTE_ActiveError(wid)->top_error; if (REMOTE_ActiveError(wid)->errorTerm) { Yap_PopTermFromDB(REMOTE_ActiveError(wid)->errorTerm); } - REMOTE_ActiveError(wid)->errorTerm = NULL; + memset(REMOTE_ActiveError(wid), 0, sizeof(*LOCAL_ActiveError)); + REMOTE_ActiveError(wid)->top_error = bf; return true; } static Int reset_exception(USES_REGS1) { return Yap_ResetException(worker_id); } static Int get_exception(USES_REGS1) { + fprintf(stderr,"HR befo get_xc=%p\n", HR); Term t = Yap_GetException(); - if (t == 0) + fprintf(stderr,"HR befo get_xc=%p\n", HR); + if (t == 0) return false; - return Yap_unify(t, ARG1); + Yap_DebugPlWriteln(t); + Yap_ResetException(worker_id); + fprintf(stderr,"HR after get_xc=%p\n", HR); + Int rc= Yap_unify(t, ARG1); + Yap_DebugPlWriteln(t); +return rc; } int Yap_dogc(int extra_args, Term *tp USES_REGS) { diff --git a/C/qlyr.c b/C/qlyr.c index 6ffb8dc7f..dc25cba5c 100755 --- a/C/qlyr.c +++ b/C/qlyr.c @@ -89,7 +89,7 @@ static void QLYR_ERROR__(const char *file, const char *function, int lineno, qlfr_err_t my_err) { // __android_log_print(ANDROID_LOG_INFO, "YAP ", "error %s in saved state // %s",GLOBAL_RestoreFile, qlyr_error[my_err]); - Yap_Error__(file, function, lineno, SYSTEM_ERROR_SAVED_STATE, TermNil, "error %s in saved state %s", + Yap_Error__(false, file, function, lineno, SYSTEM_ERROR_SAVED_STATE, TermNil, "error %s in saved state %s", GLOBAL_RestoreFile, qlyr_error[my_err]); Yap_exit(1); } diff --git a/C/utilpreds.c b/C/utilpreds.c index 9d20666ef..f5be19109 100644 --- a/C/utilpreds.c +++ b/C/utilpreds.c @@ -1665,6 +1665,7 @@ static Term attvars_in_complex_term(register CELL *pt0, register CELL *pt0_end, ++ pt0; ptd0 = pt0; d0 = *ptd0; + fprintf(stderr,"d0=%lx in attvars after jmp=%p\n", d0, HR); deref_head(d0, attvars_in_term_unk); attvars_in_term_nvar: { diff --git a/H/YapEval.h b/H/YapEval.h index 98690b4ad..8cbf944e0 100644 --- a/H/YapEval.h +++ b/H/YapEval.h @@ -402,16 +402,14 @@ Term Yap_eval_binary(Int, Term, Term); Term Yap_InnerEval__(Term USES_REGS); #define Yap_EvalError(id, t, ...) \ - Yap_EvalError__(__FILE__, __FUNCTION__, __LINE__, id, t, __VA_ARGS__) -void Yap_EvalError__(const char *, const char *, int, yap_error_number, Term, - ...); + Yap_ThrowError__(__FILE__, __FUNCTION__, __LINE__, id, t, __VA_ARGS__) #define Yap_ArithError(id, t, ...) \ Yap_ThrowError__(__FILE__, __FUNCTION__, __LINE__, id, t, __VA_ARGS__) #define Yap_BinError(id) \ - Yap_Error__(__FILE__, __FUNCTION__, __LINE__, id, 0L, "") + Yap_Error__(false, __FILE__, __FUNCTION__, __LINE__, id, 0L, "") #define Yap_AbsmiError(id) \ - Yap_Error__(__FILE__, __FUNCTION__, __LINE__, id, 0L, "") + Yap_Error__(false, __FILE__, __FUNCTION__, __LINE__, id, 0L, "") #include "inline-only.h" diff --git a/include/YapError.h b/include/YapError.h index 579a77c56..58fd5b0a2 100644 --- a/include/YapError.h +++ b/include/YapError.h @@ -41,7 +41,7 @@ Yap_InitError__(const char *file, const char *function, int lineno, yap_error_number e, YAP_Term g, ...); -extern struct yami *Yap_Error__(const char *file, const char *function, +extern struct yami *Yap_Error__(bool thrw, const char *file, const char *function, int lineno, yap_error_number err, YAP_Term wheret, ...); @@ -53,13 +53,13 @@ extern void Yap_ThrowError__(const char *file, const char *function, int lineno, ; #define Yap_NilError(id, ...) \ - Yap_Error__(__FILE__, __FUNCTION__, __LINE__, id, TermNil, __VA_ARGS__) + Yap_Error__(false,__FILE__, __FUNCTION__, __LINE__, id, TermNil, __VA_ARGS__) #define Yap_InitError(id, ...) \ Yap_InitError__(__FILE__, __FUNCTION__, __LINE__, id, TermNil, __VA_ARGS__) #define Yap_Error(id, inp, ...) \ - Yap_Error__(__FILE__, __FUNCTION__, __LINE__, id, inp, __VA_ARGS__) + Yap_Error__(false,__FILE__, __FUNCTION__, __LINE__, id, inp, __VA_ARGS__) #define Yap_ThrowError(id, inp, ...) \ Yap_ThrowError__(__FILE__, __FUNCTION__, __LINE__, id, inp, __VA_ARGS__) @@ -80,11 +80,11 @@ INLINE_ONLY extern inline Term Yap_ensure_atom__(const char *fu, const char *fi, if (!IsVarTerm(t) && IsAtomTerm(t)) return t; if (IsVarTerm(t)) { - Yap_Error__(fu, fi, line, INSTANTIATION_ERROR, t, NULL); + Yap_Error__(false,fu, fi, line, INSTANTIATION_ERROR, t, NULL); } else { if (IsAtomTerm(t)) return t; - Yap_Error__(fu, fi, line, TYPE_ERROR_ATOM, t, NULL); + Yap_Error__(false,fu, fi, line, TYPE_ERROR_ATOM, t, NULL); return 0L; } diff --git a/os/iopreds.c b/os/iopreds.c index 798e0bc15..aeb6e3d80 100644 --- a/os/iopreds.c +++ b/os/iopreds.c @@ -499,7 +499,7 @@ Int PlIOError__(const char *file, const char *function, int lineno, who[0] = '\0'; } va_end(args); - Yap_Error__(file, function, lineno, type, culprit, who); + Yap_Error__(false, file, function, lineno, type, culprit, who); /* and fail */ return false; } else { @@ -1574,8 +1574,7 @@ int Yap_OpenStream(const char *fname, const char* io_mode, Term user_name, encod CACHE_REGS int sno; StreamDesc *st; - struct vfs *vfsp = NULL; - FILE *fd = NULL; + struct vfs *vfsp; int flags; sno = GetFreeStreamD(); @@ -1599,7 +1598,7 @@ int Yap_OpenStream(const char *fname, const char* io_mode, Term user_name, encod user_name = st->user_name; } else { st->file = fopen(fname, io_mode); - if (fd == NULL) { + if (st->file == NULL) { if (!strchr(io_mode, 'b') && binary_file(fname)) { UNLOCK(st->streamlock); if (errno == ENOENT && !strchr(io_mode, 'r')) { diff --git a/packages/jpl/jpl.pl b/packages/jpl/jpl.pl index 1ccfaa932..853fe39f3 100644 --- a/packages/jpl/jpl.pl +++ b/packages/jpl/jpl.pl @@ -4470,7 +4470,7 @@ add_search_path(Path, Dir) :- % =CLASSPATH=, etc. search_path_separator((;)) :- - current_prolog_flag(windo/.... ,,,,,,,,,,,,,,,,,, :l'p[KIO)_"?ws, true), !. + current_prolog_flag(windows, true), !. search_path_separator(:). /******************************* @@ -4503,7 +4503,8 @@ location( java_root, _, Home) :- getenv( 'JAVA_HOME', Home ). location(java_root, _, JRE) :- % OS well-known - member(Root, [ '/usr/lib', + member(Root, [ + /System/Library/Frameworks/JavaVM.framework/Versions/A/JavaV '/usr/lib', '/usr/local/lib', '/opt/lib', '/Library/Java/JavaVirtualMachines', diff --git a/packages/jpl/src/c/CMakeLists.txt b/packages/jpl/src/c/CMakeLists.txt index 7ae0900f1..2756f0c9c 100644 --- a/packages/jpl/src/c/CMakeLists.txt +++ b/packages/jpl/src/c/CMakeLists.txt @@ -1,3 +1,4 @@ +set(CMAKE_MACOSX_RPATH 1) add_lib(jplYap jpl.h jpl.c hacks.h)