Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Macros | Functions | Variables
ProtectedMemory.cc File Reference
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <libgen.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <assert.h>
#include <signal.h>
#include <execinfo.h>
#include <sys/syscall.h>
#include <pthread.h>
#include "tls.hh"
#include "memoryprotection/UDSignals.h"
#include "memoryprotection/MemorySegment.h"

Go to the source code of this file.

Macros

#define MAXTHREADS   128
 

Functions

voidAllocateInProtectedHeap (size_t size)
 
void PhaseChange ()
 
void BuildProtectedMemory (size_t totalHeap)
 
void addThread ()
 
void setuphandlers ()
 
void starttracer ()
 
void finishtracer ()
 
void StartDetection ()
 
void FinishDetection ()
 

Variables

int numberOfThreads = 0
 
pid_t threads [MAXTHREADS]
 
pid_t tracerpid
 

Macro Definition Documentation

#define MAXTHREADS   128

Definition at line 456 of file ProtectedMemory.cc.

Referenced by addThread().

Function Documentation

void addThread ( )

Definition at line 462 of file ProtectedMemory.cc.

References assert, and MAXTHREADS.

463 {
464  numberOfThreads++;
466  threads[numberOfThreads-1] = gettid();
467 }
int numberOfThreads
#define MAXTHREADS
#define assert(x)
Definition: mymalloc.cc:1309
pid_t threads[MAXTHREADS]
void* AllocateInProtectedHeap ( size_t  size)

Definition at line 36 of file ProtectedMemory.cc.

References malloc().

37 {
38  if (startPosition != 0)
39  {
40  currentPosition = nextPosition;
41  nextPosition = &nextPosition[size];
42  if (nextPosition >= endPosition)
43  {
44  nextPosition = currentPosition;
45  return malloc(size);
46  }
47  return currentPosition;
48  }
49  else
50  {
51  return malloc(size);
52  }
53 }
void * malloc(size_t __size)
Definition: hjmalloc.cc:30
void BuildProtectedMemory ( size_t  totalHeap)

Definition at line 183 of file ProtectedMemory.cc.

Referenced by starttracer().

184 {
185  pagesize = getpagesize();
186 
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);
192 
193  show_stackframe();
194 
195  #ifdef PROTECTEDMEMORYDEBUG
196  printf("Page size: %d\n", pagesize);
197  #endif
198 
199  long tmp;
200  for (logpagesize = -1, tmp = pagesize; tmp > 0; logpagesize++)
201  tmp = tmp >> 1;
202 
203  #ifdef PROTECTEDMEMORYDEBUG
204  printf("log(page size): %d\n", logpagesize);
205  #endif
206 
207  if (totalHeap > 0)
208  {
209  AllocateProtectedHeap(totalHeap, pagesize);
210 
211  memorySegments[numOfSegments].start = getPageNumber(nextPosition);
212  memorySegments[numOfSegments].end = getPageNumber(endPosition);
213  numOfSegments++;
214  }
215 
216  //force to load backtrace library
217  // show_stackframe();
218 
219  //open output file
220  // assert(-1 != (stdOutFile = open("./memoryhotspot.out", O_CREAT, S_IRWXU)));
221 
222  char buf1[1024];
223  char buf2[51200];
224  char *tmp2 = buf2;
225  static char filename2[1024];
226  void *start;
227  void *end;
228  int writable;
229 
230  if (-1 == readFile((char *) "/proc/self/cmdline", buf1, 1024)) {
231  perror("readFile: /proc/self/cmdline");
232  abort();
233  }
234  memset(application, 0, 1024);
235  strcpy(application, buf1);
236 
237  // cout << "command line: " << application << endl;
238 
239  if (-1 == readFile((char *) "/proc/self/maps", buf2, 51200)) {
240  perror("readFile: /proc/self/maps");
241  abort();
242  }
243  static char lastfilename[1024];
244  do {
245  tmp2 = readMapsLine(tmp2, &start, &end, &writable, filename2);
246  if (strlen(filename2) > 0)
247  {
248  lastfilename[0] = 0;
249  strcpy(lastfilename, filename2);
250  }
251  else
252  {
253  strcpy(filename2, lastfilename);
254  }
255 
256  // if (writable == 1 && (long int) start < 0x700000000000 &&
257  // strncmp(filename2, "/lib64/", 7) != 0 && strncmp(filename2, "/opt/", 5) != 0 &&
258  // ((long int) nextPosition < (long int) start || (long int) nextPosition > (long int ) end) &&
259  // ((long int) endPosition < (long int) start || (long int) endPosition > (long int ) end))
260  if (writable == 1)
261  {
262  //ignore
263  if (strstr(filename2, "ld") != 0)
264  {
265  std::cout << "Not protected: " << start << ", " << end << ", "
266  << writable << ", " << filename2
267  << std::endl;
268  }
269  else if (strstr(filename2, "vdso") != 0)
270  {
271  std::cout << "Not protected: " << start << ", " << end << ", "
272  << writable << ", " << filename2
273  << std::endl;
274  }
275  else if (strstr(filename2, "stack") != 0)
276  {
277  std::cout << "Not protected: " << start << ", " << end << ", "
278  << writable << ", " << filename2
279  << std::endl;
280  }
281  else if (strstr(filename2, "libc") != 0)
282  {
283  std::cout << "Not protected: " << start << ", " << end << ", "
284  << writable << ", " << filename2
285  << std::endl;
286  }
287  else if (strstr(filename2, "libProtectedMemory.so") != 0)
288  {
289  std::cout << "Not protected: " << start << ", " << end << ", "
290  << writable << ", " << filename2
291  << std::endl;
292  }
293  else if (strstr(filename2, "libParRunManagerMP.so") != 0)
294  {
295  std::cout << "Not protected: " << start << ", " << end << ", "
296  << writable << ", " << filename2
297  << std::endl;
298  }
299  else if (strstr(filename2, "libmymalloc.so") != 0)
300  {
301  std::cout << "Not protected: " << start << ", " << end << ", "
302  << writable << ", " << filename2
303  << std::endl;
304  }
305  else if (strstr(filename2, "libtpmallocstub.so") != 0)
306  {
307  std::cout << "Not protected: " << start << ", " << end << ", "
308  << writable << ", " << filename2
309  << std::endl;
310  }
311  else if (strstr(filename2, "heap") != 0)
312  {
313  static int Heapcount = 0;
314  //Only one is the data segment follows with [heap] [thread private heap, empty, thread local storage]*
315  Heapcount++;
316  if (Heapcount == 1)
317  {
318  std::cout << "Protected: " << numOfSegments << ", " << start << ", " << end << ", "
319  << writable << ", " << filename2
320  << std::endl;
321  memorySegments[numOfSegments].start = start;
322  memorySegments[numOfSegments].end = end;
323  numOfSegments++;
324  }
325  else
326  {
327  std::cout << "Not protected: " << start << ", " << end << ", "
328  << writable << ", " << filename2
329  << std::endl;
330  }
331  }
332  else if (strstr(filename2, "ParA01") != 0)
333  {
334  static int Parmaincount = 0;
335  //Only one is the data segment follows with [heap] [thread private heap, empty, thread local storage]*
336  Parmaincount++;
337  printf("Determine: %d\n", Parmaincount);
338  // if (Parmaincount == 1 || Parmaincount == 2)
339  {
340  std::cout << "Protected: " << numOfSegments << ", " << start << ", " << end << ", "
341  << writable << ", " << filename2
342  << std::endl;
343  memorySegments[numOfSegments].start = start;
344  memorySegments[numOfSegments].end = end;
345  numOfSegments++;
346  }
347  }
348  else if (strstr(filename2, "geant4") != 0)
349  {
350  std::cout << "Protected: " << numOfSegments << ", " << start << ", " << end << ", "
351  << writable << ", " << filename2
352  << std::endl;
353  memorySegments[numOfSegments].start = start;
354  memorySegments[numOfSegments].end = end;
355  numOfSegments++;
356  }
357 
358  }
359  else
360  {
361  std::cout << "Not protected: " << start << ", " << end << ", "
362  << writable << ", " << filename2
363  << std::endl;
364  }
365 
366  } while ((*tmp2) != '\0' && numOfSegments < 4096);
367 }
void FinishDetection ( )

Definition at line 525 of file ProtectedMemory.cc.

References assert.

526 {
527  //move to main thread
528  // UnSetUpAllProtect();
529 
530  assert(0 == raise(SIGUSR1));
531  //For each thread, can not do this
532 }
#define assert(x)
Definition: mymalloc.cc:1309
void finishtracer ( )

Definition at line 505 of file ProtectedMemory.cc.

References assert.

506 {
507  UnSetUpAllProtect();
508  close(outputFile);
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);
513 }
#define assert(x)
Definition: mymalloc.cc:1309
pid_t tracerpid
void PhaseChange ( )

Definition at line 104 of file ProtectedMemory.cc.

105 {
106  int tmpoutputFile;
107  tmpoutputFile = outputFile;
108 
109  phase++;
110  pid_t myselfpid = getpid();
111  char outputfilename[1024];
112  outputfilename[0] = 0;
113  sprintf(outputfilename, "./memoryhotpot.out.%d.%d", phase, myselfpid);
114 
115  outputFile = open(outputfilename, O_CREAT|O_RDWR, S_IRWXU);
116  close(tmpoutputFile);
117 }
void setuphandlers ( )

Definition at line 570 of file ProtectedMemory.cc.

Referenced by starttracer().

571 {
572  setuphandler0();
573  setuphandler1();
574  setuphandler2();
575 }
void StartDetection ( )

Definition at line 516 of file ProtectedMemory.cc.

References assert.

517 {
518  printf("waiting for the tracer\n");
519  while (isCalled == 0) usleep(100);
520  printf("the tracer ready\n");
521  assert(0 == raise(SIGUSR2));
522 }
#define assert(x)
Definition: mymalloc.cc:1309
void starttracer ( )

Definition at line 474 of file ProtectedMemory.cc.

References BuildProtectedMemory(), numberOfThreads, and setuphandlers().

475 {
477  setuphandlers();
478 
479  if ((tracerpid = fork()) == 0) //child process
480  {
481  char processid[10240];
482  char tmptid[1024];
483  processid[0] = 0;
484  tmptid[0] = 0;
485  sprintf(tmptid, "%d", numberOfThreads);
486  strcat(processid, tmptid);
487  strcat(processid, " ");
488  int i;
489  for (i = 0; i < numberOfThreads; i++)
490  {
491  tmptid[0] = 0;
492  sprintf(tmptid, "%d", threads[i]);
493  strcat(processid, tmptid);
494  strcat(processid, " ");
495  }
496  execl("./memoryprotection/tracer", "./memoryprotection/tracer", application, processid, (char *)0);
497  exit(0);
498  }
499 
500 }
int numberOfThreads
void BuildProtectedMemory(size_t totalHeap)
pid_t tracerpid
void setuphandlers()
pid_t threads[MAXTHREADS]

Variable Documentation

int numberOfThreads = 0

Definition at line 457 of file ProtectedMemory.cc.

Referenced by starttracer().

pid_t threads[MAXTHREADS]

Definition at line 458 of file ProtectedMemory.cc.

pid_t tracerpid

Definition at line 459 of file ProtectedMemory.cc.