|
Here we can find both the heap allocated using the setbrk() system call, and the heap allocated using anonymous maps. However there is one thing that we don't want to scan, that is the stack, otherwise the function that is testing the memory itself would easily crash.
So thanks to the Linux proc filessystem the first problem is no longer a big issue, and we use some code like this:- int memtest_test_linux_anonymous_maps(void) {
- FILE *fp = fopen("/proc/self/maps","r");
- ... some more var declaration ...
- while(fgets(line,sizeof(line),fp) != NULL) {
- char *start, *end, *p = line;
- start = p;
- p = strchr(p,'-');
- if (!p) continue;
- *p++ = '\0';
- end = p;
- p = strchr(p,' ');
- if (!p) continue;
- *p++ = '\0';
- if (strstr(p,"stack") ||
- strstr(p,"vdso") ||
- strstr(p,"vsyscall")) continue;
- if (!strstr(p,"00:00")) continue;
- if (!strstr(p,"rw")) continue;
- start_addr = strtoul(start,NULL,16);
- end_addr = strtoul(end,NULL,16);
- size = end_addr-start_addr;
- start_vect[regions] = start_addr;
- size_vect[regions] = size;
- printf("Testing %lx %lu\n", start_vect[regions], size_vect[regions]);
- regions++;
- }
- ... code to actually test the found memory regions ...
- /* NOTE: It is very important to close the file descriptor only now
- * because closing it before may result into unmapping of some memory
- * region that we are testing. */
- fclose(fp);
- }
复制代码 |
|