Skip to content

Commit

Permalink
fix nil pointer in prolog profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
vscosta committed May 13, 2016
1 parent b28deed commit e5891af
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 21 deletions.
2 changes: 2 additions & 0 deletions C/absmi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,8 @@ static void spy_goal(USES_REGS1) {
}
/* standard profiler */
if ((pe->PredFlags & ProfiledPredFlag)) {
if (!pe->StatisticsForPred)
Yap_initProfiler(pe);
LOCK(pe->StatisticsForPred->lock);
pe->StatisticsForPred->NOfEntries++;
UNLOCK(pe->StatisticsForPred->lock);
Expand Down
5 changes: 4 additions & 1 deletion C/adtdefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Functor Yap_MkFunctor(Atom ap, arity_t arity) {
WRITE_LOCK(ae->ARWLock);
f = InlinedUnlockedMkFunctor(ae, arity);
WRITE_UNLOCK(ae->ARWLock);
return (f);
return f;
}

/* vsc: We must guarantee that IsVarTerm(functor) returns true! */
Expand Down Expand Up @@ -818,6 +818,7 @@ Prop Yap_NewPredPropByFunctor(FunctorEntry *fe, Term cur_mod) {
p->ModuleOfPred = 0L;
else
p->ModuleOfPred = cur_mod;
p->StatisticsForPred = NULL;
Yap_NewModulePred(cur_mod, p);

#ifdef TABLING
Expand Down Expand Up @@ -887,6 +888,7 @@ Prop Yap_NewThreadPred(PredEntry *ap USES_REGS) {
return NIL;
}
INIT_LOCK(p->PELock);
p->StatisticsForPred = NULL:
p->KindOfPE = PEProp;
p->ArityOfPE = ap->ArityOfPE;
p->cs.p_code.FirstClause = p->cs.p_code.LastClause = NULL;
Expand Down Expand Up @@ -941,6 +943,7 @@ Prop Yap_NewPredPropByAtom(AtomEntry *ae, Term cur_mod) {
INIT_LOCK(p->PELock);
p->KindOfPE = PEProp;
p->ArityOfPE = 0;
p->StatisticsForPred = NULL;
p->cs.p_code.FirstClause = p->cs.p_code.LastClause = NULL;
p->cs.p_code.NOfClauses = 0;
p->PredFlags = 0L;
Expand Down
30 changes: 15 additions & 15 deletions C/amasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,17 +282,15 @@ static void a_fetch_cv(cmp_op_info *, int, struct intermediates *);
static void a_fetch_vc(cmp_op_info *, int, struct intermediates *);
static yamop *a_f2(cmp_op_info *, yamop *, int, struct intermediates *);

static profile_data *
initProfiler(PredEntry *p, struct intermediates *cip)
profile_data *
Yap_initProfiler(PredEntry *p)
{
profile_data *ptr;
if (p->StatisticsForPred)
return p->StatisticsForPred;
if ((ptr = (profile_data *)Yap_AllocCodeSpace(sizeof(profile_data ))) == NULL) {
/* OOOPS, got in trouble, must do a longjmp and recover space */
save_machine_regs();
siglongjmp(cip->CompilerBotch,2);
return NULL;
}
if (!ptr)
return NULL;
INIT_LOCK(ptr->lock);
ptr->NOfEntries = 0;
ptr->NOfHeadSuccesses = 0;
Expand Down Expand Up @@ -2010,7 +2008,7 @@ a_try(op_numbers opcode, CELL lab, CELL opr, int nofalts, int hascut, yamop *cod
if (ap->PredFlags & CountPredFlag)
newcp->opc = emit_op(_count_retry_logical);
else if (ap->PredFlags & ProfiledPredFlag) {
if (!initProfiler(ap, cip)) {
if (!Yap_initProfiler(ap)) {
return NULL;
}
newcp->opc = emit_op(_profiled_retry_logical);
Expand All @@ -2019,15 +2017,16 @@ a_try(op_numbers opcode, CELL lab, CELL opr, int nofalts, int hascut, yamop *cod
newcp->y_u.OtaLl.s = emit_count(opr);
} else {
/* trust */
if (ap->PredFlags & CountPredFlag)
if (ap->PredFlags & CountPredFlag) {
newcp->opc = emit_op(_count_trust_logical);
else if (ap->PredFlags & ProfiledPredFlag) {
if (!initProfiler(ap, cip)) {
} else if (ap->PredFlags & ProfiledPredFlag) {
if (!Yap_initProfiler(ap)) {
return NULL;
}
newcp->opc = emit_op(_profiled_trust_logical);
} else
}
newcp->opc = emit_op(_profiled_trust_logical);
} else {
newcp->opc = emit_op(_trust_logical);
}
newcp->y_u.OtILl.block = (LogUpdIndex *)(cip->code_addr);
*cip->current_trust_lab = newcp;
}
Expand Down Expand Up @@ -3664,11 +3663,12 @@ do_pass(int pass_no, yamop **entry_codep, int assembling, int *clause_has_blobsp
if ((pe->PredFlags & (CPredFlag|UserCPredFlag|AsmPredFlag)) ||
!pe->ModuleOfPred) {
code_p = a_pl(_enter_profiling, pe, code_p, pass_no);
Yap_initProfiler(pe);
}
}
break;
case retry_profiled_op:
if (!initProfiler(cip->CurrentPred, cip)) {
if (!Yap_initProfiler(cip->CurrentPred)) {
return NULL;
}
code_p = a_pl(_retry_profiled, (PredEntry *)(cip->cpc->rnd1), code_p, pass_no);
Expand Down
14 changes: 13 additions & 1 deletion C/cdmgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,9 @@ static void IPred(PredEntry *ap, UInt NSlots, yamop *next_pc) {
ap->PredFlags |= IndexedPredFlag;
}
if (ap->PredFlags & (SpiedPredFlag | CountPredFlag | ProfiledPredFlag)) {
if (ap->PredFlags & ProfiledPredFlag) {
Yap_initProfiler(ap);
}
ap->OpcodeOfPred = Yap_opcode(_spy_pred);
ap->CodeOfPred = (yamop *)(&(ap->OpcodeOfPred));
#if defined(YAPOR) || defined(THREADS)
Expand Down Expand Up @@ -980,6 +983,9 @@ static void retract_all(PredEntry *p, int in_use) {
p->cs.p_code.TrueCodeOfPred = p->CodeOfPred = (yamop *)(&(p->OpcodeOfPred));
if (trueGlobalPrologFlag(PROFILING_FLAG)) {
p->PredFlags |= ProfiledPredFlag;
if (!Yap_initProfiler(p)) {
return;
}
} else
p->PredFlags &= ~ProfiledPredFlag;
if (CALL_COUNTING) {
Expand Down Expand Up @@ -1044,7 +1050,10 @@ static void add_first_static(PredEntry *p, yamop *cp, int spy_flag) {
p->cs.p_code.NOfClauses = 1;
if (trueGlobalPrologFlag(PROFILING_FLAG)) {
p->PredFlags |= ProfiledPredFlag;
spy_flag = TRUE;
if (!Yap_initProfiler(p)) {
return;
}
spy_flag = TRUE;
} else {
p->PredFlags &= ~ProfiledPredFlag;
}
Expand Down Expand Up @@ -1072,6 +1081,9 @@ static void add_first_dynamic(PredEntry *p, yamop *cp, int spy_flag) {

if (trueGlobalPrologFlag(PROFILING_FLAG)) {
p->PredFlags |= ProfiledPredFlag;
if (!Yap_initProfiler(p)) {
return;
}
spy_flag = true;
} else {
p->PredFlags &= ~ProfiledPredFlag;
Expand Down
4 changes: 2 additions & 2 deletions C/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -1413,9 +1413,9 @@ c_functor(Term Goal, Term mod, compiler_struct *cglobs)
save_machine_regs();
siglongjmp(cglobs->cint.CompilerBotch, OUT_OF_HEAP_BOTCH);
}
if (profiling)
if (profiling) {
Yap_emit(enter_profiling_op, (CELL)RepPredProp(p0), Zero, &cglobs->cint);
else if (call_counting)
} else if (call_counting)
Yap_emit(count_call_op, (CELL)RepPredProp(p0), Zero, &cglobs->cint);
c_args(Goal, 0, cglobs);
Yap_emit(safe_call_op, (CELL)p0 , Zero, &cglobs->cint);
Expand Down
16 changes: 14 additions & 2 deletions C/dbase.c
Original file line number Diff line number Diff line change
Expand Up @@ -2681,6 +2681,11 @@ static PredEntry *new_lu_int_key(Int key) {
p->ArityOfPE = 3;
p->OpcodeOfPred = Yap_opcode(_op_fail);
p->cs.p_code.TrueCodeOfPred = p->CodeOfPred = FAILCODE;
if (p->PredFlags & ProfiledPredFlag) {
if (!Yap_initProfiler(p)) {
return NULL;
}
}
INT_LU_KEYS[hash_key] = p0;
return p;
}
Expand Down Expand Up @@ -2714,6 +2719,11 @@ static PredEntry *new_lu_entry(Term t) {
if (CurrentModule == PROLOG_MODULE)
pe->PredFlags |= StandardPredFlag;
pe->cs.p_code.TrueCodeOfPred = pe->CodeOfPred = FAILCODE;
if (pe->PredFlags & ProfiledPredFlag) {
if (!Yap_initProfiler(pe)) {
return NULL;
}
}
return pe;
}

Expand All @@ -2738,7 +2748,8 @@ static DBProp find_entry(Term t) {
at = AtomDot;
arity = 2;
}
return RepDBProp(FindDBProp(RepAtom(at), 0, arity, 0));
DBProp rc = RepDBProp(FindDBProp(RepAtom(at), 0, arity, 0));
return rc;
}

static PredEntry *find_lu_entry(Term t) {
Expand Down Expand Up @@ -3304,6 +3315,7 @@ static Int lu_recorded(PredEntry *pe USES_REGS) {
}
if (pe->PredFlags & ProfiledPredFlag) {
LOCK(pe->StatisticsForPred->lock);

pe->StatisticsForPred->NOfEntries++;
UNLOCK(pe->StatisticsForPred->lock);
}
Expand Down Expand Up @@ -4250,7 +4262,7 @@ static Int p_current_reference_counter(USES_REGS1) {
Yap_Error(INSTANTIATION_ERROR, t1, "increase_reference_counter/1");
return FALSE;
}
if (!IsDBRefTerm(t1)) {
if (!IsDBRefTerm(t1)) {
Yap_Error(TYPE_ERROR_DBREF, t1, "increase_reference_counter");
return FALSE;
}
Expand Down

0 comments on commit e5891af

Please sign in to comment.