From beeeee705bcf16d705748713ea40dca3486cc7b7 Mon Sep 17 00:00:00 2001 From: Nick Kralevich Date: Thu, 6 May 2010 15:05:52 -0700 Subject: Allow pixelflinger to work when NX (No Execute) is enabled. Instead of allocating memory from the (non executable) heap, allocate memory using mspace and ensure that we use mprotect to mark it as PROT_EXEC. This allows pixelflinger to continue to work even when NX protections are enabled. Testing: Using the ApiDemos market app, verify that Apidemos -> Graphics -> OpenGL ES -> GLSurfaceView works when "adb shell setprop debug.egl.hw 0" is set. Change-Id: Ib569cd2543c6fa25688ee76325a712bc2347450b --- libpixelflinger/codeflinger/CodeCache.cpp | 34 +++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) (limited to 'libpixelflinger/codeflinger/CodeCache.cpp') diff --git a/libpixelflinger/codeflinger/CodeCache.cpp b/libpixelflinger/codeflinger/CodeCache.cpp index 29410c8..5877ff4 100644 --- a/libpixelflinger/codeflinger/CodeCache.cpp +++ b/libpixelflinger/codeflinger/CodeCache.cpp @@ -19,6 +19,8 @@ #include #include #include +#include +#include #include #include @@ -39,15 +41,14 @@ namespace android { Assembly::Assembly(size_t size) : mCount(1), mSize(0) { - mBase = (uint32_t*)malloc(size); - if (mBase) { - mSize = size; - } + mBase = (uint32_t*)mspace_malloc(getMspace(), size); + mSize = size; + ensureMbaseExecutable(); } Assembly::~Assembly() { - free(mBase); + mspace_free(getMspace(), mBase); } void Assembly::incStrong(const void*) const @@ -75,11 +76,32 @@ uint32_t* Assembly::base() const ssize_t Assembly::resize(size_t newSize) { - mBase = (uint32_t*)realloc(mBase, newSize); + mBase = (uint32_t*)mspace_realloc(getMspace(), mBase, newSize); mSize = newSize; + ensureMbaseExecutable(); return size(); } +mspace Assembly::getMspace() +{ + static mspace msp = create_contiguous_mspace(2 * 1024, 1024 * 1024, /*locked=*/ false); + return msp; +} + +void Assembly::ensureMbaseExecutable() +{ + long pagesize = sysconf(_SC_PAGESIZE); + long pagemask = ~(pagesize - 1); // assumes pagesize is a power of 2 + + uint32_t* pageStart = (uint32_t*) (((uintptr_t) mBase) & pagemask); + size_t adjustedLength = mBase - pageStart + mSize; + + if (mBase && mprotect(pageStart, adjustedLength, PROT_READ | PROT_WRITE | PROT_EXEC) != 0) { + mspace_free(getMspace(), mBase); + mBase = NULL; + } +} + // ---------------------------------------------------------------------------- CodeCache::CodeCache(size_t size) -- cgit v1.1