This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
PATCH: Update libmudflap support for FreeBSD
- From: Loren James Rittle <rittle at latour dot labs dot mot dot com>
- To: gcc-patches at gcc dot gnu dot org
- Date: Thu, 20 Aug 2009 15:13:37 -0500 (CDT)
- Subject: PATCH: Update libmudflap support for FreeBSD
- Reply-to: rittle at labs dot mot dot com
I have investigated why libmudflap has been failing many tests on
(i386-*-)freebsd7.2 and generated a patch. This patch solves all
current threading-related issues and an mmap portability issue in two
testcases. Tested against gcc-4.4.2@150967 and gcc mainline@150945 on
i386-unknown-freebsd7.2 (but I know the history of threading support
back to ~FreeBSD 3 and this will likely help older versions as well).
If someone can confirm that it is portable to pass -1 to mmap for the
fd when the MAP_ANON(YMOUS) flag is present, then I can simplify the
patch before committing it. It is clear that 0 works on Linux.
mmap(2) on FreeBSD 7.2 specifically states that "The file descriptor
used for creating MAP_ANON must be -1."
Please advise if I may/should commit the test case portion in their
current form. I assume that I may commit the first two portions with
my FreeBSD maintainer hat since the change is clearly bound by a
__FreeBSD__ macro check (however, I shall await feedback before I do).
Regards,
Loren
2009-08-20 Loren J. Rittle <ljrittle@acm.org>
* mf-runtime.c (__mf_init): Support FreeBSD.
Prime mutex which calls calloc upon first lock to avoid deadlock.
* mf-hooks1.c (__mf_0fn_mmap): Support FreeBSD.
Ignore red zone allocation request for initial thread's stack.
* testsuite/libmudflap.c/pass51-frag.c (MAP_FAILED): Define,
if not in system header; use it. On FreeBSD, must pass fd==-1
with MAP_ANON flag.
* testsuite/libmudflap.c/fail40-frag.c: Ditto.
Index: libmudflap/mf-runtime.c
===================================================================
--- libmudflap/mf-runtime.c (revision 150973)
+++ libmudflap/mf-runtime.c (working copy)
@@ -695,6 +695,12 @@
if (LIKELY (__mf_starting_p == 0))
return;
+#if defined(__FreeBSD__) && defined(LIBMUDFLAPTH)
+ pthread_self();
+ LOCKTH ();
+ UNLOCKTH ();
+#endif /* Prime mutex which calls calloc upon first lock to avoid deadlock. */
+
/* This initial bootstrap phase requires that __mf_starting_p = 1. */
#ifdef PIC
__mf_resolve_dynamics ();
Index: libmudflap/mf-hooks1.c
===================================================================
--- libmudflap/mf-hooks1.c (revision 150973)
+++ libmudflap/mf-hooks1.c (working copy)
@@ -321,6 +321,11 @@
void *
__mf_0fn_mmap (void *start, size_t l, int prot, int f, int fd, off_t off)
{
+#if defined(__FreeBSD__)
+ if (f == 0x1000 && fd == -1 && prot == 0 && off == 0)
+ return 0;
+#endif /* Ignore red zone allocation request for initial thread's stack. */
+
return (void *) -1;
}
#endif
Index: libmudflap/testsuite/libmudflap.c/pass51-frag.c
===================================================================
--- libmudflap/testsuite/libmudflap.c/pass51-frag.c (revision 150973)
+++ libmudflap/testsuite/libmudflap.c/pass51-frag.c (working copy)
@@ -13,14 +13,22 @@
#ifndef MAP_ANONYMOUS
#define MAP_ANONYMOUS MAP_ANON
#endif
+#ifndef MAP_FAILED
+#define MAP_FAILED NULL
+#endif
#ifdef HAVE_MMAP
void *p;
unsigned pg = getpagesize ();
int rc;
+#ifdef __FreeBSD__
p = mmap (NULL, 4 * pg, PROT_READ|PROT_WRITE,
+ MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+#else
+ p = mmap (NULL, 4 * pg, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
- if (p == NULL)
+#endif
+ if (p == MAP_FAILED)
return 1;
memset (p, 0, 4*pg);
Index: libmudflap/testsuite/libmudflap.c/fail40-frag.c
===================================================================
--- libmudflap/testsuite/libmudflap.c/fail40-frag.c (revision 150973)
+++ libmudflap/testsuite/libmudflap.c/fail40-frag.c (working copy)
@@ -14,6 +14,9 @@
#ifndef MAP_ANONYMOUS
#define MAP_ANONYMOUS MAP_ANON
#endif
+#ifndef MAP_FAILED
+#define MAP_FAILED NULL
+#endif
#ifdef HAVE_MMAP
volatile unsigned char *p;
unsigned num = getpagesize ();
@@ -23,8 +26,12 @@
/* Get a bit of usable address space. We really want an 2**N+1-sized object,
so the low/high addresses wrap when hashed into the lookup cache. So we
will manually unregister the entire mmap, then re-register a slice. */
+#ifdef __FreeBSD__
+ p = mmap (NULL, num, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+#else
p = mmap (NULL, num, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
- if (p == NULL)
+#endif
+ if (p == MAP_FAILED)
return 1;
/* Now unregister it, as if munmap was called. But don't actually munmap, so
we can write into the memory. */