Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20210121173152.6819b23e@ncopa-desktop.lan>
Date: Thu, 21 Jan 2021 17:31:52 +0100
From: Natanael Copa <ncopa@...inelinux.org>
To: Rich Felker <dalias@...c.org>
Cc: musl@...ts.openwall.com, "Alex Xu (Hello71)" <alex_y_xu@...oo.ca>
Subject: Re: [PATCH] don't set errno in free

On Thu, 21 Jan 2021 11:18:08 -0500
Rich Felker <dalias@...c.org> wrote:

> On Thu, Jan 21, 2021 at 04:50:00PM +0100, Natanael Copa wrote:
> > On Thu, 21 Jan 2021 09:02:40 -0500
> > "Alex Xu (Hello71)" <alex_y_xu@...oo.ca> wrote:
> >   
> > > busybox echo fails if free sets errno, which madvise does on old
> > > kernels.
> > > ---
> > >  src/malloc/mallocng/free.c | 14 ++++++++++++--
> > >  1 file changed, 12 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/src/malloc/mallocng/free.c b/src/malloc/mallocng/free.c
> > > index 40745f97..82836815 100644
> > > --- a/src/malloc/mallocng/free.c
> > > +++ b/src/malloc/mallocng/free.c
> > > @@ -119,7 +119,13 @@ void free(void *p)
> > >  	if (((uintptr_t)(start-1) ^ (uintptr_t)end) >= 2*PGSZ && g->last_idx) {
> > >  		unsigned char *base = start + (-(uintptr_t)start & (PGSZ-1));
> > >  		size_t len = (end-base) & -PGSZ;
> > > -		if (len) madvise(base, len, MADV_FREE);
> > > +		if (len) {
> > > +			// madvise(..., MADV_FREE) returns -EINVAL on old kernels
> > > +			// POSIX.1-202x requires free() to not modify errno on success
> > > +			int e = errno;
> > > +			madvise(base, len, MADV_FREE);
> > > +			errno = e;
> > > +		}
> > >  	}  
> > 
> > I think we should save the errno early and make sure its restored on
> > exit of the function. you should also include <errno.h>. I suggest
> > something like:
> > 
> > diff --git a/src/malloc/mallocng/free.c b/src/malloc/mallocng/free.c
> > index 40745f97..77bed88b 100644
> > --- a/src/malloc/mallocng/free.c
> > +++ b/src/malloc/mallocng/free.c
> > @@ -1,6 +1,7 @@
> >  #define _BSD_SOURCE
> >  #include <stdlib.h>
> >  #include <sys/mman.h>
> > +#include <errno.h>
> >  
> >  #include "meta.h"
> >  
> > @@ -102,6 +103,7 @@ void free(void *p)
> >  {
> >         if (!p) return;
> >  
> > +       int orig_errno = errno;  
> 
> This is much costlier. It puts the TLS access (faulting and emulating
> on old MIPS) in the path that runs on every call.

I didn't think about that. The original suggestion is better then.

Thanks!

-nc

> 
> Rich

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.