This patch updates libthread in plan9port to be MAP_STACK compliant,
replacing calls to malloc/free with mmap and munmap when allocating
thread stacks.

The most obvious effect  was sam(1), acme(1), and 9term(1) would
core when executed. All three run now with seemingly no issues.

There maybe other problems I haven't noticed yet.

--Aaron

Index: patches/patch-src_libthread_thread_c
===================================================================
RCS file: patches/patch-src_libthread_thread_c
diff -N patches/patch-src_libthread_thread_c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ patches/patch-src_libthread_thread_c        9 Mar 2018 22:14:13 -0000
@@ -0,0 +1,26 @@
+$OpenBSD$
+
+Index: src/libthread/thread.c
+--- src/libthread/thread.c.orig
++++ src/libthread/thread.c
+@@ -108,8 +108,9 @@ threadalloc(void (*fn)(void*), void *arg, uint stack)
+       ulong z;
+ 
+       /* allocate the task and stack together */
+-      t = malloc(sizeof *t+stack);
+-      if(t == nil)
++      t = mmap(0, sizeof *t+stack, PROT_READ|PROT_WRITE,
++              MAP_PRIVATE|MAP_ANON|MAP_STACK, -1, 0);
++      if(t == MAP_FAILED)
+               sysfatal("threadalloc malloc: %r");
+       memset(t, 0, sizeof *t);
+       t->stk = (uchar*)(t+1);
+@@ -364,7 +365,7 @@ procscheduler(Proc *p)
+                       delthreadinproc(p, t);
+                       p->nthread--;
+ /*print("nthread %d\n", p->nthread); */
+-                      free(t);
++                      munmap(t, t->stksize);
+               }
+       }
+ 
Index: patches/patch-src_libthread_threadimpl_h
===================================================================
RCS file: patches/patch-src_libthread_threadimpl_h
diff -N patches/patch-src_libthread_threadimpl_h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ patches/patch-src_libthread_threadimpl_h    9 Mar 2018 21:41:25 -0000
@@ -0,0 +1,14 @@
+$OpenBSD$
+
+Index: src/libthread/threadimpl.h
+--- src/libthread/threadimpl.h.orig
++++ src/libthread/threadimpl.h
+@@ -10,6 +10,8 @@
+ #             define _XOPEN_SOURCE    /* for Snow Leopard */
+ #     endif
+ #     include <ucontext.h>
++#elif defined(__OpenBSD__)
++#     include <sys/mman.h>
+ #endif
+ #include <sys/utsname.h>
+ #include "libc.h"

Reply via email to