[PATCH] fix PR sanitizer/55617

Jack Howarth howarth@bromo.med.uc.edu
Mon Feb 4 15:14:00 GMT 2013


On Mon, Feb 04, 2013 at 03:44:04PM +0100, Jakub Jelinek wrote:
> On Mon, Feb 04, 2013 at 09:22:27AM -0500, Jack Howarth wrote:
> >    I switched to the simple insertion of the asan priorities for two reasons...
> > 
> > 1) Mike seemed unconvinced that the single qsort with the proposed sort_ctor_records
> > of...
> > 
> > +static int
> > +sort_ctor_records (const void * a, const void * b)
> > +{
> > +  const ctor_record *ca = (const ctor_record *)a;
> > +  const ctor_record *cb = (const ctor_record *)b;
> > +  if (ca->priority > cb->priority)
> > +    return 1;
> > +  if (ca->priority < cb->priority)
> > +    return -1;
> > +  if (ca->position > cb->position)
> > +    return -1;
> 
> Obviously this should have been return 1;
> 
> > +  if (ca->position < cb->position)
> > +    return 1;
> 
> and this return -1;
> 
> > +  return 0;
> > +}
> > 
> > would really be stable in absence of a second call to qsort.
> 
> Ugh, how can that not be stable?  position is different in every vector
> entry, so even the return 0; case above would happen only if qsort
> (incorrectly) called it with two same pointers.  So, the second and any
> further calls to qsort with the same comparison function in this case
> necessarily don't change anything in the array (ok, unless you have more
> than 4billion ctors and overflow position, or unless your OS has a buggy
> qsort (which wouldn't surprise me for Darwin)).

Actually don't we need...

static int
sort_ctor_records (const void * a, const void * b)
{
  const ctor_record *ca = (const ctor_record *)a;
  const ctor_record *cb = (const ctor_record *)b;
  if (ca->priority > cb->priority)
    return 1;
  if (ca->priority < cb->priority)
    return -1;
  if ((ca->priority == cb->priority) && (ca->position > cb->position))
    return 1;
  if ((ca->priority == cb->priority) && (ca->position < cb->position))
    return -1;
  return 0;
}

so that the last two checks only sort the original positions of constructors for
the same priority?

> 
> > 2) Once I realized that darwin sets the default priority of constructors to
> > DEFAULT_INIT_PRIORITY 65535, the desired sorting method seemed rather unclear.
> > I assume we need to really sort these so that the priorities from 
> > MAX_INIT_PRIORITY-1 through 0 appear first in the queue and then those with
> > MAX_INIT_PRIORITY, right? It isn't obvious how we can achieve that in
> > sort_ctor_record with a single pass through qsort.
> 
> ??  You simply sort by priority ascending, and for same priorities, by
> position ascending.
> 
> 	Jakub



More information about the Gcc-patches mailing list