gentoo/dev-python/cffi/files/issue177_prot_exec.patch
Robin H. Johnson 56bd759df1
proj/gentoo: Initial commit
This commit represents a new era for Gentoo:
Storing the gentoo-x86 tree in Git, as converted from CVS.

This commit is the start of the NEW history.
Any historical data is intended to be grafted onto this point.

Creation process:
1. Take final CVS checkout snapshot
2. Remove ALL ChangeLog* files
3. Transform all Manifests to thin
4. Remove empty Manifests
5. Convert all stale $Header$/$Id$ CVS keywords to non-expanded Git $Id$
5.1. Do not touch files with -kb/-ko keyword flags.

Signed-off-by: Robin H. Johnson <robbat2@gentoo.org>
X-Thanks: Alec Warner <antarus@gentoo.org> - did the GSoC 2006 migration tests
X-Thanks: Robin H. Johnson <robbat2@gentoo.org> - infra guy, herding this project
X-Thanks: Nguyen Thai Ngoc Duy <pclouds@gentoo.org> - Former Gentoo developer, wrote Git features for the migration
X-Thanks: Brian Harring <ferringb@gentoo.org> - wrote much python to improve cvs2svn
X-Thanks: Rich Freeman <rich0@gentoo.org> - validation scripts
X-Thanks: Patrick Lauer <patrick@gentoo.org> - Gentoo dev, running new 2014 work in migration
X-Thanks: Michał Górny <mgorny@gentoo.org> - scripts, QA, nagging
X-Thanks: All of other Gentoo developers - many ideas and lots of paint on the bikeshed
2015-08-08 17:38:18 -07:00

80 lines
2.5 KiB
Diff

# HG changeset patch
# User Armin Rigo <arigo@tunes.org>
# Date 1424942568 -3600
# Node ID c7edb1e84eb3c29cac0674790cb4efcbcf1683b2
# Parent 95e0563201602a2e1a8d83cc95a6a70048dfeece
issue #177: workaround for some Linux kernels
diff --git a/c/malloc_closure.h b/c/malloc_closure.h
--- a/c/malloc_closure.h
+++ b/c/malloc_closure.h
@@ -14,6 +14,54 @@
# endif
#endif
+/* On PaX enable kernels that have MPROTECT enable we can't use PROT_EXEC.
+
+ This is, apparently, an undocumented change to ffi_prep_closure():
+ depending on the Linux kernel we're running on, we must give it a
+ mmap that is either PROT_READ|PROT_WRITE|PROT_EXEC or only
+ PROT_READ|PROT_WRITE. In the latter case, just trying to obtain a
+ mmap with PROT_READ|PROT_WRITE|PROT_EXEC would kill our process(!),
+ but in that situation libffi is fine with only PROT_READ|PROT_WRITE.
+ There is nothing in the libffi API to know that, though, so we have
+ to guess by parsing /proc/self/status. "Meh."
+ */
+#ifdef __linux__
+#include <stdlib.h>
+
+static int emutramp_enabled = -1;
+
+static int
+emutramp_enabled_check (void)
+{
+ char *buf = NULL;
+ size_t len = 0;
+ FILE *f;
+ int ret;
+ f = fopen ("/proc/self/status", "r");
+ if (f == NULL)
+ return 0;
+ ret = 0;
+
+ while (getline (&buf, &len, f) != -1)
+ if (!strncmp (buf, "PaX:", 4))
+ {
+ char emutramp;
+ if (sscanf (buf, "%*s %*c%c", &emutramp) == 1)
+ ret = (emutramp == 'E');
+ break;
+ }
+ free (buf);
+ fclose (f);
+ return ret;
+}
+
+#define is_emutramp_enabled() (emutramp_enabled >= 0 ? emutramp_enabled \
+ : (emutramp_enabled = emutramp_enabled_check ()))
+#else
+#define is_emutramp_enabled() 0
+#endif
+
+
/* 'allocate_num_pages' is dynamically adjusted starting from one
page. It grows by a factor of PAGE_ALLOCATION_GROWTH_RATE. This is
meant to handle both the common case of not needing a lot of pages,
@@ -77,9 +125,12 @@
if (item == NULL)
return;
#else
+ int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
+ if (is_emutramp_enabled ())
+ prot &= ~PROT_EXEC;
item = (union mmaped_block *)mmap(NULL,
allocate_num_pages * _pagesize,
- PROT_READ | PROT_WRITE | PROT_EXEC,
+ prot,
MAP_PRIVATE | MAP_ANONYMOUS,
-1,
0);