$OpenBSD$

Index: lib/flb_libco/amd64.c
--- lib/flb_libco/amd64.c.orig
+++ lib/flb_libco/amd64.c
@@ -9,6 +9,8 @@
 #include "settings.h"
 
 #include <assert.h>
+#include <err.h>
+#include <stdint.h>
 #include <stdlib.h>
 
 #ifdef __cplusplus
@@ -129,6 +131,7 @@ cothread_t co_active() {
 cothread_t co_create(unsigned int size, void (*entrypoint)(void),
                      size_t *out_size){
   cothread_t handle;
+  long pagesize, newsize;
   if(!co_swap) {
     co_init();
     co_swap = (void (*)(cothread_t, cothread_t))co_swap_function;
@@ -139,7 +142,19 @@ cothread_t co_create(unsigned int size, void (*entrypo
   size &= ~15;  /* align stack to 16-byte boundary */
   *out_size = size;
 
-  if((handle = (cothread_t)malloc(size))) {
+  pagesize = sysconf(_SC_PAGESIZE);
+  if(pagesize == -1)
+    err(1, "sysconf failed");
+  newsize = ((size + pagesize - 1) / pagesize) * pagesize;
+  handle = (cothread_t)malloc(newsize);
+  if(handle == NULL)
+    err(1, "malloc failed");
+  if((uintptr_t)handle % pagesize)
+    err(1, "misaligned allocation");
+  handle = (cothread_t)mmap(handle, newsize, PROT_READ|PROT_WRITE, MAP_FIXED|MAP_STACK|MAP_PRIVATE|MAP_ANON, -1, 0);
+  if(handle == MAP_FAILED)
+    err(1, "mmap failed");
+  else {
     long long *p = (long long*)((char*)handle + size);  /* seek to top of stack */
     *--p = (long long)crash;                            /* crash if entrypoint returns */
     *--p = (long long)entrypoint;                       /* start of function */
