[AD] PATCH: updated mprotect patch for allegro-4.2.1 which also work on Linux

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


Hi all,

I've been busy debugging strange i386 crashes with allegro on machines which have an i386 cpu with NX, thus I was happy to see the mprotect patch from the openBSD folks which explained this.

Unfortunatly the openBSD version doesn't help since Linux (and POSIX) require the pointer passed to mprotect to be page aligned.

The attached version of the openBSD patch fixed this, please apply for 4.2.1

Thanks,

Hans
--- allegro-4.2.0/tools/grabber.c.mprotect	2005-09-04 18:00:18.000000000 +0200
+++ allegro-4.2.0/tools/grabber.c	2006-03-11 23:27:31.000000000 +0100
@@ -3121,6 +3121,7 @@
       case OSTYPE_SUNOS:      s = "SunOS/Solaris";              break;
       case OSTYPE_FREEBSD:    s = "FreeBSD";                    break;
       case OSTYPE_NETBSD:     s = "NetBSD";                     break;
+      case OSTYPE_OPENBSD:    s = "OpenBSD";                    break;
       case OSTYPE_IRIX:       s = "IRIX";                       break;
       case OSTYPE_DARWIN:     s = "Darwin";                     break;
       case OSTYPE_QNX:        s = "QNX";                        break;
--- allegro-4.2.0/include/allegro/platform/alunixac.hin.mprotect	2005-10-30 21:23:33.000000000 +0100
+++ allegro-4.2.0/include/allegro/platform/alunixac.hin	2006-03-11 23:27:31.000000000 +0100
@@ -169,6 +169,9 @@
 /* Define to 1 if you have the `mmap' function. */
 #undef HAVE_MMAP
 
+/* Define to 1 if you have the `mprotect' function. */
+#undef HAVE_MPROTECT
+
 /* Define to 1 if you have the <ndir.h> header file, and it defines `DIR'. */
 #undef HAVE_NDIR_H
 
--- allegro-4.2.0/include/allegro/system.h.mprotect	2005-03-15 21:32:43.000000000 +0100
+++ allegro-4.2.0/include/allegro/system.h	2006-03-11 23:27:31.000000000 +0100
@@ -53,6 +53,7 @@
 #define OSTYPE_SUNOS       AL_ID('S','U','N',' ')
 #define OSTYPE_FREEBSD     AL_ID('F','B','S','D')
 #define OSTYPE_NETBSD      AL_ID('N','B','S','D')
+#define OSTYPE_OPENBSD     AL_ID('O','B','S','D')
 #define OSTYPE_IRIX        AL_ID('I','R','I','X')
 #define OSTYPE_DARWIN      AL_ID('D','A','R','W')
 #define OSTYPE_QNX         AL_ID('Q','N','X',' ')
--- allegro-4.2.0/configure.mprotect	2005-11-06 10:54:00.000000000 +0100
+++ allegro-4.2.0/configure	2006-03-11 23:27:31.000000000 +0100
@@ -11225,7 +11225,7 @@
 
 
 
-for ac_func in mmap memcmp mkstemp stricmp strlwr strupr vprintf
+for ac_func in mmap mprotect memcmp mkstemp stricmp strlwr strupr vprintf
 do
 as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh`
 echo "$as_me:$LINENO: checking for $ac_func" >&5
--- allegro-4.2.0/configure.in.mprotect	2005-08-07 23:50:49.000000000 +0200
+++ allegro-4.2.0/configure.in	2006-03-11 23:27:31.000000000 +0100
@@ -713,7 +713,7 @@
 AC_STRUCT_TM
 AC_TYPE_SIGNAL
 
-AC_CHECK_FUNCS(mmap memcmp mkstemp stricmp strlwr strupr vprintf)
+AC_CHECK_FUNCS(mmap mprotect memcmp mkstemp stricmp strlwr strupr vprintf)
 
 dnl Tweak header files for library build
 CFLAGS="$CFLAGS -DALLEGRO_LIB_BUILD"
--- allegro-4.2.0/src/unix/usystem.c.mprotect	2005-09-04 18:00:17.000000000 +0200
+++ allegro-4.2.0/src/unix/usystem.c	2006-03-11 23:27:31.000000000 +0100
@@ -196,6 +196,9 @@
       else if (!strcmp(utsn.sysname, "NetBSD")) {
 	 os_type = OSTYPE_NETBSD;
       }
+      else if (!strcmp(utsn.sysname, "OpenBSD")) {
+	 os_type = OSTYPE_OPENBSD;
+      }
       else if ((!strcmp(utsn.sysname, "IRIX"))
 	       || (!strcmp(utsn.sysname, "IRIX64"))) {
 	 os_type = OSTYPE_IRIX;
--- allegro-4.2.0/src/unix/uesd.c.mprotect	2005-03-12 08:54:27.000000000 +0100
+++ allegro-4.2.0/src/unix/uesd.c	2006-03-11 23:27:31.000000000 +0100
@@ -27,6 +27,7 @@
 
 #include <sys/time.h>
 #include <sys/types.h>
+#include <string.h> /* FD_ZERO() needs this */
 #include <unistd.h>
 #include <esd.h>
 
--- allegro-4.2.0/src/i386/istretch.c.mprotect	2005-03-15 19:02:22.000000000 +0100
+++ allegro-4.2.0/src/i386/istretch.c	2006-03-11 23:31:44.000000000 +0100
@@ -30,6 +30,11 @@
    #include "winalleg.h"   /* For VirtualProtect */
 #endif     /* ifdef ALLEGRO_WINDOWS */
 
+#ifdef HAVE_MPROTECT
+   #include <sys/types.h>
+   #include <sys/mman.h>
+   #include <sys/user.h>
+#endif
 
 
 /* helper macro for generating stretchers in each color depth */
@@ -435,7 +440,14 @@
  #ifdef ALLEGRO_WINDOWS
    /* Play nice with Windows executable memory protection */
    VirtualProtect(_scratch_mem, _scratch_mem_size, PAGE_EXECUTE_READWRITE, &old_protect);
- #endif     /* ifdef ALLEGRO_WINDOWS */
+ #elif defined(HAVE_MPROTECT)
+   {
+      char *p = (char *)((unsigned long)_scratch_mem & ~(PAGE_SIZE-1ul));
+      if (mprotect(p, _scratch_mem_size + ((char *)_scratch_mem - p),
+            PROT_EXEC|PROT_READ|PROT_WRITE))
+         perror("allegro-error: mprotect failed during stretched blit!");
+   }
+ #endif
 
    /* call the stretcher */
    _do_stretch(source, dest, _scratch_mem, sx>>16, sy, syd, 


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/