Bug 210322 - running dynamically generated code in shared memory
Summary: running dynamically generated code in shared memory
Status: RESOLVED FIXED
Alias: None
Product: valgrind
Classification: Developer tools
Component: general (show other bugs)
Version: 3.5.0
Platform: Compiled Sources Linux
: NOR normal
Target Milestone: ---
Assignee: Julian Seward
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-10-12 15:29 UTC by Madhan S
Modified: 2009-10-12 15:54 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed In:


Attachments
Patch to allow translations in shared memory (467 bytes, patch)
2009-10-12 15:37 UTC, Tom Hughes
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Madhan S 2009-10-12 15:29:26 UTC
The program below captures the essence of running dynamically generated code
in my application. It causes the following with --trace-signals=yes enabled.
 --smc-check=all, doesn't help. Is there a known workaround.

--4416-- translations not allowed here (0x400b000) - throwing SEGV
--4416-- delivering signal 11 (SIGSEGV):2 to thread 1
--4416-- delivering 11 (code 2) to default handler; action: terminate+core
==4416==
==4416== Process terminating with default action of signal 11 (SIGSEGV)
==4416==  Bad permissions for mapped region at address 0x400B000
==4416==    at 0x400B000: ???
==4416==    by 0x748DF2: (below main) (in /lib/tls/libc-2.3.4.so)

source code:


#include <stdio.h>
#include <sys/mman.h>
#include <sys/shm.h>
#include <assert.h>

int f1(int c)
{
 int i;
 int res = 0;

 for( i = 0; i < c; ++i )
 {
  res += i*i;
 }
 return res;
}

int f2()
{
 return 0;
}

typedef int (*f_t)(int);

int main()
{
 key_t key;
 int   smid;
 int   rc;
 void *smad;
 int   sz;
 f_t f = f1;

 struct shmid_ds buf;

 key = ftok("/dev/zero",13);
 if ( key == (key_t)(-1) ) perror("ftok");

 smid = shmget( key, 8192, 0777 | IPC_CREAT );
 if ( smid < 0 ) perror("shmget");

 smad = shmat( smid, 0, 0 );

 if ( smad == (void*)(-1) ) perror("shmat");

 rc = mprotect( smad, 8192, PROT_READ | PROT_WRITE | PROT_EXEC );

 if ( rc < 0 ) perror("mprotect");

 sz = (char*)&f2 - (char*)&f1;

 assert( sz > 0 && sz < 8192 );

 memcpy( smad, &f1, sz );

 printf("SoS(10)=%d, a=%p\n", f(10), f);

 f = (f_t)smad;

 printf("SoS(10)=%d, a=%p\n", f(10), f);

 shmctl( smid, IPC_RMID, 0 );

 return 0;
}
Comment 1 Tom Hughes 2009-10-12 15:37:22 UTC
Created attachment 37535 [details]
Patch to allow translations in shared memory

Try this patch and see if it helps...
Comment 2 Madhan S 2009-10-12 15:50:49 UTC
That certainly worked Tom. That was really quick. Thanks a lot.
Comment 3 Tom Hughes 2009-10-12 15:54:01 UTC
Committed as r10903.