Skip to content

Commit

Permalink
Disallow retyping from type w/o free func to one with
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin committed Jul 9, 2018
1 parent d57f664 commit 5622c24
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/julia_gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ void JFinalizer(jl_value_t * obj)
BagHeader * hdr = (BagHeader *)obj;
Bag contents = (Bag)(hdr + 1);
UInt tnum = hdr->type;

// calling MakeImmutable on a weak pointer object can turn it into a
// plist, which no longer requires a free function; but we already
// scheduled one, so we must check TabFreeFuncBags here
if (TabFreeFuncBags[tnum])
TabFreeFuncBags[tnum]((Bag)&contents);
}
Expand Down Expand Up @@ -648,8 +652,27 @@ void RetypeBag(Bag bag, UInt new_type)
}
#endif

if (!TabFreeFuncBags[old_type] && TabFreeFuncBags[new_type])
jl_gc_schedule_foreign_sweepfunc(JuliaTLS, (jl_value_t *)header);
if (!TabFreeFuncBags[old_type] && TabFreeFuncBags[new_type]) {
// calling MakeImmutable on a weak pointer object can turn it into a
// plist, which no longer requires a free functions; we allow this,
// but we disallow changing the other way. Otherwise, we might end up
// switch an object several times between types which require a free
// callback and types which don't.
//
// But then we either end up calling jl_gc_schedule_foreign_sweepfunc
// multiple times for the same object, which then can lead to Julia
// invoking the free function on that object multiple times, which in
// turn could have bad consequences.
//
// Or else we would have to write code to to track whether
// jl_gc_schedule_foreign_sweepfunc was called for an object (e.g. by
// using an object flag). But right now no GAP code needs to do this,
// and changing the type of an object to a completely different type
// is something better to be avoided anyway. So instead of supporting
// a feature nobody uses right now, we error out and wait to see if
// somebody complains.
Panic("cannot change bag type to one which requires a 'free' callback");
}
header->type = new_type;
}

Expand Down

0 comments on commit 5622c24

Please sign in to comment.