19 #include <sys/syscall.h>
26 pid_t mytid = syscall(SYS_gettid);
67 #include "memoryprotection/UDSignals.h"
69 static int readFile(
char *filename,
char *buf,
int maxSize);
70 static char *
readhex(
char *
string,
long *value);
71 static char *
readMapsLine(
char *
string,
void **start,
void **end,
int *writable,
char *filename);
90 #include "memoryprotection/MemorySegment.h"
110 pid_t myselfpid = getpid();
111 char outputfilename[1024];
112 outputfilename[0] = 0;
113 sprintf(outputfilename,
"./memoryhotpot.out.%d.%d",
phase, myselfpid);
115 outputFile = open(outputfilename, O_CREAT|O_RDWR, S_IRWXU);
116 close(tmpoutputFile);
135 #ifdef PROTECTEDMEMORYDEBUG
136 printf(
"In handler 0 p1.\n");
140 #ifdef PROTECTEDMEMORYDEBUG
141 printf(
"In handler 0 p2.\n");
145 #ifdef PROTECTEDMEMORYDEBUG
146 printf(
"In handler 0 p3.\n");
148 assert( 0 ==
raise(SIGUSR2));
154 #ifdef PROTECTEDMEMORYDEBUG
155 printf(
"In handler 1 p1.\n");
164 #ifdef PROTECTEDMEMORYDEBUG
165 printf(
"In handler 1 p2.\n");
168 assert( 0 ==
raise(SIGUSR2));
176 #ifdef PROTECTEDMEMORYDEBUG
177 printf(
"In handler 2 p1.\n");
187 pid_t myselfpid = getpid();
188 char outputfilename[1024];
189 outputfilename[0] = 0;
190 sprintf(outputfilename,
"./memoryhotpot.out.%d.%d",
phase, myselfpid);
191 outputFile = open(outputfilename, O_CREAT|O_RDWR, S_IRWXU);
195 #ifdef PROTECTEDMEMORYDEBUG
196 printf(
"Page size: %d\n",
pagesize);
203 #ifdef PROTECTEDMEMORYDEBUG
225 static char filename2[1024];
230 if (-1 ==
readFile((
char *)
"/proc/self/cmdline", buf1, 1024)) {
231 perror(
"readFile: /proc/self/cmdline");
239 if (-1 ==
readFile((
char *)
"/proc/self/maps", buf2, 51200)) {
240 perror(
"readFile: /proc/self/maps");
243 static char lastfilename[1024];
245 tmp2 =
readMapsLine(tmp2, &start, &end, &writable, filename2);
246 if (strlen(filename2) > 0)
249 strcpy(lastfilename, filename2);
253 strcpy(filename2, lastfilename);
263 if (strstr(filename2,
"ld") != 0)
265 std::cout <<
"Not protected: " << start <<
", " << end <<
", "
266 << writable <<
", " << filename2
269 else if (strstr(filename2,
"vdso") != 0)
271 std::cout <<
"Not protected: " << start <<
", " << end <<
", "
272 << writable <<
", " << filename2
275 else if (strstr(filename2,
"stack") != 0)
277 std::cout <<
"Not protected: " << start <<
", " << end <<
", "
278 << writable <<
", " << filename2
281 else if (strstr(filename2,
"libc") != 0)
283 std::cout <<
"Not protected: " << start <<
", " << end <<
", "
284 << writable <<
", " << filename2
287 else if (strstr(filename2,
"libProtectedMemory.so") != 0)
289 std::cout <<
"Not protected: " << start <<
", " << end <<
", "
290 << writable <<
", " << filename2
293 else if (strstr(filename2,
"libParRunManagerMP.so") != 0)
295 std::cout <<
"Not protected: " << start <<
", " << end <<
", "
296 << writable <<
", " << filename2
299 else if (strstr(filename2,
"libmymalloc.so") != 0)
301 std::cout <<
"Not protected: " << start <<
", " << end <<
", "
302 << writable <<
", " << filename2
305 else if (strstr(filename2,
"libtpmallocstub.so") != 0)
307 std::cout <<
"Not protected: " << start <<
", " << end <<
", "
308 << writable <<
", " << filename2
311 else if (strstr(filename2,
"heap") != 0)
313 static int Heapcount = 0;
318 std::cout <<
"Protected: " << numOfSegments <<
", " << start <<
", " << end <<
", "
319 << writable <<
", " << filename2
321 memorySegments[numOfSegments].start = start;
322 memorySegments[numOfSegments].end = end;
327 std::cout <<
"Not protected: " << start <<
", " << end <<
", "
328 << writable <<
", " << filename2
332 else if (strstr(filename2,
"ParA01") != 0)
334 static int Parmaincount = 0;
337 printf(
"Determine: %d\n", Parmaincount);
340 std::cout <<
"Protected: " << numOfSegments <<
", " << start <<
", " << end <<
", "
341 << writable <<
", " << filename2
343 memorySegments[numOfSegments].start = start;
344 memorySegments[numOfSegments].end = end;
348 else if (strstr(filename2,
"geant4") != 0)
350 std::cout <<
"Protected: " << numOfSegments <<
", " << start <<
", " << end <<
", "
351 << writable <<
", " << filename2
353 memorySegments[numOfSegments].start = start;
354 memorySegments[numOfSegments].end = end;
361 std::cout <<
"Not protected: " << start <<
", " << end <<
", "
362 << writable <<
", " << filename2
366 }
while ((*tmp2) !=
'\0' && numOfSegments < 4096);
369 static int readFile(
char *filename,
char *buf,
int maxSize) {
371 int fd = open(filename, O_RDONLY);
379 num = read(fd, buf2, 1000);
380 }
while (num != 0 && num != -1 && buf2 - buf < maxSize - 1000);
381 }
while (errno == EINTR || errno == EAGAIN);
386 return (errno == 0 ? 0 : -1);
389 static char *
readhex (
char *
string,
long *value) {
394 if ((c >=
'0') && (c <=
'9')) c -=
'0';
395 else if ((c >=
'a') && (c <=
'f')) c -=
'a' - 10;
396 else if ((c >=
'A') && (c <=
'F')) c -=
'A' - 10;
398 *value = *value * 16 + c;
400 return (
char *) --string;
403 static char *
readMapsLine(
char *
string,
void **start,
void **end,
int *writable,
char *filename) {
406 string =
readhex(
string, (
long *) start) + 1;
407 string =
readhex(
string, (
long *) end);
408 *writable = (
string[2] ==
'w');
409 for (str =
string; *str !=
'\n' && *str !=
'\0'; str++);
411 for (str =
string; (*str !=
'/' && *str !=
'[') && *str !=
'\0'; str++);
414 strcpy(filename, str);
424 for(i = 0 ; i < numOfSegments ; i++)
426 struct MemorySegment *tmpSegment = memorySegments + i;
427 #ifdef PROTECTEDMEMORYDEBUG
428 printf(
"Protect from 0x%lx to 0x%lx.\n", tmpSegment->start, tmpSegment->end);
431 if (-1 == mprotect(tmpSegment->start, (
long) tmpSegment->end - (
long) tmpSegment->start , PROT_READ))
432 perror(
"mprotect error");
434 #ifdef PROTECTEDMEMORYDEBUG
435 printf(
"Finish to protect from 0x%lx to 0x%lx.\n", tmpSegment->start, tmpSegment->end);
443 for(i = 0 ; i < numOfSegments ; i++)
445 struct MemorySegment *tmpSegment = memorySegments + i;
447 #ifdef PROTECTEDMEMORYDEBUG
448 printf(
"Remove protection from 0x%lx to 0x%lx.\n", tmpSegment->start, tmpSegment->end);
451 if (-1 == mprotect(tmpSegment->start, (
long) tmpSegment->end - (
long) tmpSegment->start , PROT_READ | PROT_WRITE))
452 perror(
"mprotect error");
456 #define MAXTHREADS 128
481 char processid[10240];
486 strcat(processid, tmptid);
487 strcat(processid,
" ");
492 sprintf(tmptid,
"%d",
threads[i]);
493 strcat(processid, tmptid);
494 strcat(processid,
" ");
496 execl(
"./memoryprotection/tracer",
"./memoryprotection/tracer",
application, processid, (
char *)0);
509 assert(0 ==
raise(SIGUSR1));
510 printf(
"Before wait for child process: %d\n",
tracerpid);
512 printf(
"After wait for child process: %d\n",
tracerpid);
518 printf(
"waiting for the tracer\n");
520 printf(
"the tracer ready\n");
521 assert(0 ==
raise(SIGUSR2));
530 assert(0 ==
raise(SIGUSR1));
581 sigfillset(&sa.sa_mask);
582 sa.sa_flags = SA_RESTART | SA_SIGINFO;
584 if (sigaction(SIGUSR2, &sa, NULL) == -1)
585 perror(
"sigaction"); ;
592 sigfillset(&sa.sa_mask);
593 sa.sa_flags = SA_RESTART | SA_SIGINFO;
595 if (sigaction(SIGSEGV, &sa, NULL) == -1)
596 perror(
"sigaction"); ;
604 sigfillset(&sa.sa_mask);
605 sa.sa_flags = SA_RESTART | SA_SIGINFO;
607 if (sigaction(SIGUSR1, &sa, NULL) == -1)
608 perror(
"sigaction"); ;
static void UnSetUpAllProtect()
static char application[1024]
static void handler2(int, siginfo_t *, void *)
static void show_stackframe()
static void setuphandler0()
static int readFile(char *filename, char *buf, int maxSize)
static G4ThreadLocal void * trace[100]
void BuildProtectedMemory(size_t totalHeap)
void * AllocateInProtectedHeap(size_t size)
static void setuphandler1()
static void setuphandler2()
static char * currentPosition
static char * readhex(char *string, long *value)
static char * readMapsLine(char *string, void **start, void **end, int *writable, char *filename)
static void AllocateProtectedHeap(size_t totalSize, size_t pagesize)
static void record_stackframe()
static void handler0(int, siginfo_t *, void *)
static char * nextPosition
static void SetUpAllProtect()
static void handler1(int, siginfo_t *, void *)
static char * endPosition
static void * getPageNumber(void *theAddress)
static char * startPosition
void * malloc(size_t __size)
pid_t threads[MAXTHREADS]