19 #include <sys/syscall.h>
26 pid_t mytid = syscall(SYS_gettid);
31 static char *startPosition = 0;
32 static char *currentPosition = 0;
33 static char *nextPosition = 0;
34 static char *endPosition = 0;
38 if (startPosition != 0)
40 currentPosition = nextPosition;
41 nextPosition = &nextPosition[size];
42 if (nextPosition >= endPosition)
44 nextPosition = currentPosition;
47 return currentPosition;
55 static void AllocateProtectedHeap(
size_t totalSize,
size_t pagesize)
58 if (startPosition != 0)
return;
60 startPosition = (
char *)
malloc(pagesize * 2 + totalSize);
61 currentPosition = startPosition;
62 nextPosition = currentPosition + pagesize;
63 endPosition = nextPosition + totalSize;
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);
72 static void *getPageNumber(
void *theAddress);
73 static void setuphandler0();
74 static void setuphandler1();
75 static void setuphandler2();
77 static void SetUpAllProtect();
78 static void UnSetUpAllProtect();
84 static size_t pagesize;
85 static int logpagesize;
92 static char application[1024];
98 static void *getPageNumber(
void *address) {
return (
void *) (((long) address >> logpagesize) << logpagesize);}
101 static int outputFile = -1;
102 static int trace_size;
107 tmpoutputFile = outputFile;
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);
119 static void record_stackframe() {
121 trace_size = backtrace(trace, 100);
122 backtrace_symbols_fd(trace, trace_size, outputFile);
125 static void show_stackframe() {
127 trace_size = backtrace(trace, 100);
128 backtrace_symbols_fd(trace, trace_size, STDOUT_FILENO);
131 static int isCalled = 0;
133 static void handler0(
int, siginfo_t *,
void *)
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));
152 static void handler1(
int, siginfo_t *,
void *)
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));
174 static void handler2(
int, siginfo_t *,
void *)
176 #ifdef PROTECTEDMEMORYDEBUG
177 printf(
"In handler 2 p1.\n");
185 pagesize = getpagesize();
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);
200 for (logpagesize = -1, tmp = pagesize; tmp > 0; logpagesize++)
203 #ifdef PROTECTEDMEMORYDEBUG
204 printf(
"log(page size): %d\n", logpagesize);
209 AllocateProtectedHeap(totalHeap, pagesize);
211 memorySegments[numOfSegments].start = getPageNumber(nextPosition);
212 memorySegments[numOfSegments].end = getPageNumber(endPosition);
225 static char filename2[1024];
230 if (-1 == readFile((
char *)
"/proc/self/cmdline", buf1, 1024)) {
231 perror(
"readFile: /proc/self/cmdline");
234 memset(application, 0, 1024);
235 strcpy(application, buf1);
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);
421 static void SetUpAllProtect()
424 for(i = 0 ; i < numOfSegments ; 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);
440 static void UnSetUpAllProtect()
443 for(i = 0 ; i < numOfSegments ; 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
466 threads[numberOfThreads-1] = gettid();
479 if ((tracerpid = fork()) == 0)
481 char processid[10240];
485 sprintf(tmptid,
"%d", numberOfThreads);
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);
511 waitpid(tracerpid, NULL, 0);
512 printf(
"After wait for child process: %d\n", tracerpid);
518 printf(
"waiting for the tracer\n");
519 while (isCalled == 0) usleep(100);
520 printf(
"the tracer ready\n");
521 assert(0 ==
raise(SIGUSR2));
530 assert(0 ==
raise(SIGUSR1));
577 static void setuphandler0()
580 sa.sa_sigaction = handler0;
581 sigfillset(&sa.sa_mask);
582 sa.sa_flags = SA_RESTART | SA_SIGINFO;
584 if (sigaction(SIGUSR2, &sa, NULL) == -1)
585 perror(
"sigaction"); ;
588 static void setuphandler1()
591 sa.sa_sigaction = handler1;
592 sigfillset(&sa.sa_mask);
593 sa.sa_flags = SA_RESTART | SA_SIGINFO;
595 if (sigaction(SIGSEGV, &sa, NULL) == -1)
596 perror(
"sigaction"); ;
599 static void setuphandler2()
603 sa.sa_sigaction = handler2;
604 sigfillset(&sa.sa_mask);
605 sa.sa_flags = SA_RESTART | SA_SIGINFO;
607 if (sigaction(SIGUSR1, &sa, NULL) == -1)
608 perror(
"sigaction"); ;
void BuildProtectedMemory(size_t totalHeap)
void * AllocateInProtectedHeap(size_t size)
const XML_Char int const XML_Char * value
printf("%d Experimental points found\n", nlines)
void * malloc(size_t __size)
pid_t threads[MAXTHREADS]