On Wed, Dec 02, 2020 at 07:48:07AM +0100, Otto Moerbeek wrote:

> On Tue, Dec 01, 2020 at 01:14:22PM -0800, [email protected] wrote:
> 
> > On Tue, 1 Dec 2020, Otto Moerbeek wrote:
> > > On Tue, Dec 01, 2020 at 08:00:18PM +0100, Otto Moerbeek wrote:
> > > > On Tue, Dec 01, 2020 at 10:13:29AM -0800, [email protected] wrote:
> > ...
> > > > The man page is lacking or even wrong in this respect. It explicitly
> > > > talks about how to do deallocation.
> > 
> > Yeah, that's a bug in the manpage.
> > 
> > 
> > > And curiously, if I use 4*PTHREAD_STACK_MIN for both the mmap size arg
> > > and the pthread_attr_setstack size arg, the crash does not appear.
> > 
> > Uh, that's like noting that whether a use-after-free crashes depends on 
> > the size of the allocation: it's the UAF that's wrong, the size is 
> > irrelevant.
> 
> Of course.  I just was curious why it does npt happen with a different size.
> 
> > 
> > pthread_join() returning merely tells you that the target thread has 
> > gotten far enough into pthread_exit() as to pass its return value to the 
> > joining thread.  It still has more cleanup to do before finally entering 
> > the kernel to vanish and there's no standard API to detect when that's 
> > happened.
> > 
> > I suppose a masochists could use kvm_getprocs() to examine the caller's 
> > own threads, but the real answer is that pthread_attr_setstack() is not 
> > appropriate for threads that will come and go in a long-lived process 
> > where cleanup of the stacks is necessary; for those, if you need to set a 
> > different stack size, use pthread_attr_setstacksize() and let the 
> > implementation handle the allocation and deallocation.
> > 
> > 
> > Philip
> > 
> 

Quick test was *too* quick.

Anyway, here's a man page diff

        -Otto

Index: pthread_attr_setstack.3
===================================================================
RCS file: /cvs/src/lib/libpthread/man/pthread_attr_setstack.3,v
retrieving revision 1.5
diff -u -p -r1.5 pthread_attr_setstack.3
--- pthread_attr_setstack.3     12 Apr 2018 17:13:34 -0000      1.5
+++ pthread_attr_setstack.3     2 Dec 2020 07:28:29 -0000
@@ -46,11 +46,12 @@ the provided stack must be page-aligned.
 It will be replaced (meaning zeroed) with a new
 .Ar MAP_ANON | Ar MAP_STACK
 mapping.
-It is recommended that the initial mapping be allocated using
-an allocator which has a matching deallocator that discards whole
-pages, to clear the
-.Ar MAP_STACK
-attribute afterwards.
+The passed memory object should not be deallocated or reused,
+even when the thread using it has terminated.
+If there is no need for a specific memory object as stack,
+the
+.Xr pthread_attr_set_stacksize 3
+function should be used.
 .Sh RETURN VALUES
 Upon successful completion,
 .Fn pthread_attr_setstack

Reply via email to