From 112c37b990bde2c97070546e801c07f20b9b5345 Mon Sep 17 00:00:00 2001
From: Slava <slava@emu.(none)>
Date: Wed, 16 Jan 2008 01:16:53 -0500
Subject: [PATCH] Fix Linux and FreeBSD signal handling on x86

---
 vm/os-freebsd-x86.32.h | 8 ++++++++
 vm/os-linux-x86-32.h   | 8 ++++++++
 vm/os-unix.c           | 7 ++++++-
 vm/platform.h          | 2 --
 4 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/vm/os-freebsd-x86.32.h b/vm/os-freebsd-x86.32.h
index 34299691bc..a04755e9dd 100644
--- a/vm/os-freebsd-x86.32.h
+++ b/vm/os-freebsd-x86.32.h
@@ -1 +1,9 @@
+#include <ucontext.h>
+
+INLINE void *ucontext_stack_pointer(void *uap)
+{
+        ucontext_t *ucontext = (ucontext_t *)uap;
+        return (void *)ucontext->uc_mcontext.mc_esp;
+}
+
 #define UAP_PROGRAM_COUNTER(ucontext) (((ucontext_t *)(ucontext))->uc_mcontext.mc_eip)
diff --git a/vm/os-linux-x86-32.h b/vm/os-linux-x86-32.h
index e12133966d..b458fcbe21 100644
--- a/vm/os-linux-x86-32.h
+++ b/vm/os-linux-x86-32.h
@@ -1,2 +1,10 @@
+#include <ucontext.h>
+
+INLINE void *ucontext_stack_pointer(void *uap)
+{
+        ucontext_t *ucontext = (ucontext_t *)uap;
+        return (void *)ucontext->uc_mcontext.gregs[7];
+}
+
 #define UAP_PROGRAM_COUNTER(ucontext) \
 	(((ucontext_t *)(ucontext))->uc_mcontext.gregs[14])
diff --git a/vm/os-unix.c b/vm/os-unix.c
index 55d55f312b..41dbe9cabf 100755
--- a/vm/os-unix.c
+++ b/vm/os-unix.c
@@ -192,7 +192,12 @@ INLINE F_STACK_FRAME *uap_stack_pointer(void *uap)
 	from Factor to C is a sign of things seriously gone wrong, not just
 	a divide by zero or stack underflow in the listener */
 	if(in_code_heap_p(UAP_PROGRAM_COUNTER(uap)))
-		return ucontext_stack_pointer(uap);
+	{
+		F_STACK_FRAME *ptr = ucontext_stack_pointer(uap);
+		if(!ptr)
+			critical_error("Invalid uap",(CELL)uap);
+		return ptr;
+	}
 	else
 		return NULL;
 }
diff --git a/vm/platform.h b/vm/platform.h
index d5687b849d..40324cc330 100644
--- a/vm/platform.h
+++ b/vm/platform.h
@@ -41,7 +41,6 @@
 		#ifdef __FreeBSD__
 			#define FACTOR_OS_STRING "freebsd"
 			#include "os-freebsd.h"
-			#include "os-unix-ucontext.h"
 			
 			#if defined(FACTOR_X86)
 				#include "os-freebsd-x86.32.h"
@@ -64,7 +63,6 @@
 			#include "os-linux.h"
 
 			#if defined(FACTOR_X86)
-				#include "os-unix-ucontext.h"
 				#include "os-linux-x86-32.h"
 			#elif defined(FACTOR_PPC)
 				#include "os-unix-ucontext.h"