/* * smiffy by Davide Libenzi (MAP_NOZERO pages sniffer) * Copyright (C) 2007 Davide Libenzi * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Davide Libenzi * * * Smiffy continuosly creates mmap(MAP_NOZERO) anonymous mappings, and * verifies that the pages it recevies are ZERO pages. For the test to * be valid, smiffy should be run under a user that has no other * task running and that owns no files in the system. Better create a * dummy user and run smiffy under such user. */ #include #include #include #include #include #include #include #ifndef MAP_NOZERO #define MAP_NOZERO 0x04000000 #endif #define DEFAULT_SIZE_PAGES 128 static unsigned int pgsize; static int page_check(void *page) { unsigned long *p, *top; p = page; top = page + pgsize; /* * Write-fault the page before, otherwise you get ZERO_PAGE plus * copy_page() behaviour ... */ *p++ = 1; for (; p < top; p++) if (*p) return 0; return 1; } static int test_mmap(unsigned int size) { unsigned int nzcount; char *addr, *p, *top; size = ((size + pgsize - 1) / pgsize) * pgsize; addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_NOZERO, -1, 0); if (addr == MAP_FAILED) { perror("mmap"); return -1; } for (p = addr, top = addr + size, nzcount = 0; p < top; p += pgsize) { if (!page_check(p)) nzcount++; } munmap(addr, size); if (nzcount) fprintf(stderr, "mapping had %u non-zero pages\n", nzcount); return 0; } int main(int ac, char **av) { int i, sltimeo = 1000000, nice = 0; unsigned int size; pgsize = sysconf(_SC_PAGESIZE); size = pgsize * DEFAULT_SIZE_PAGES; while ((i = getopt(ac, av, "s:p:N:h")) != -1) { switch (i) { case 's': size = atoi(optarg) * pgsize; break; case 'p': sltimeo = atoi(optarg); break; case 'N': nice = atoi(optarg); break; case 'h': break; } } if (setpriority(PRIO_PROCESS, 0, nice)) perror("setpriority"); for (;;) { test_mmap(size); usleep(sltimeo); } return 0; }