$OpenBSD: patch-src_racket_src_eval_c,v 1.1 2013/10/16 19:22:37 juanfra Exp $

Upstream's commit message:
"OpenBSD provides pthread_stackseg_np(), which directly reports
the stack-bounds information that Racket needs, so we can use
that instead of the approach used on other Unix variants. The
approach used for other Unix variants seems not to work for OpenBSD
because the stack location at the point that main() is called
is already significantly far from the stack base (on the order
of 100k on a 32-bit system in my test using OpenBSD 5.2)."

Test: racket -e '(load "deep.rktl")'

https://github.com/plt/racket/pull/316
https://github.com/plt/racket/commit/3a9ad7746b6f53f68dbdc55493baf7528b524422

--- src/racket/src/eval.c.orig	Sat Aug 10 00:38:27 2013
+++ src/racket/src/eval.c	Tue Oct 15 20:14:39 2013
@@ -144,6 +144,11 @@
 #include <sys/time.h>
 #include <sys/resource.h>
 #endif
+#ifdef PTHREAD_STACKSEG_FIND_STACK_BOUNDS
+# include <sys/signal.h>
+# include <pthread.h>
+# include <pthread_np.h>
+#endif
 #ifdef WINDOWS_FIND_STACK_BOUNDS
 #include <windows.h>
 #endif
@@ -595,9 +600,6 @@ void scheme_init_stack_check()
 {
   int *v, stack_grows_up;
   uintptr_t deeper;
-#ifdef UNIX_FIND_STACK_BOUNDS
-  struct rlimit rl;
-#endif
   
   deeper = scheme_get_deeper_address();
   stack_grows_up = (deeper > (uintptr_t)&v);
@@ -665,12 +667,14 @@ void scheme_init_stack_check()
 # endif
 
 # ifdef UNIX_FIND_STACK_BOUNDS
-    getrlimit(RLIMIT_STACK, &rl);
-  
     {
+      struct rlimit rl;
       uintptr_t bnd, lim;
+
       bnd = (uintptr_t)scheme_get_current_os_thread_stack_base();
 
+      getrlimit(RLIMIT_STACK, &rl);
+
 #  ifdef LINUX_FIND_STACK_BASE
       bnd = adjust_stack_base(bnd);
 #  endif
@@ -687,6 +691,14 @@ void scheme_init_stack_check()
         bnd += (STACK_SAFETY_MARGIN - lim);
 
       scheme_stack_boundary = bnd;
+    }
+# endif
+
+# ifdef PTHREAD_STACKSEG_FIND_STACK_BOUNDS
+    {
+      stack_t stack;
+      pthread_stackseg_np(pthread_self(), &stack);
+      scheme_stack_boundary = (uintptr_t)((char *)stack.ss_sp - (stack.ss_size - STACK_SAFETY_MARGIN));
     }
 # endif
   }
