static void setup(void) { int i, fd; fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, 0664); pagesize = getpagesize(); /* Writing 16 pages of random data into this file */ for (i = 0; i < (pagesize / 2); i++) SAFE_WRITE(1, fd, STR, sizeof(STR) - 1); SAFE_FSTAT(fd, &st); file1 = SAFE_MMAP(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); file2 = SAFE_MMAP(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); file3 = SAFE_MMAP(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); shared_anon = SAFE_MMAP(0, MAP_SIZE, PROT_READ, MAP_SHARED | MAP_ANONYMOUS, -1, 0); nonalign = file1 + 100; ptr_addr = SAFE_MALLOC(st.st_size); tmp_addr = (void*)LTP_ALIGN((long)ptr_addr, pagesize); /* unmap as last step to avoid subsequent mmap(s) pick same address */ SAFE_MUNMAP(file2 + st.st_size - pagesize, pagesize); SAFE_CLOSE(fd); tcases_filter(); }
static void setup(void) { int i, fd; fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, 0664); pagesize = getpagesize(); /* Writing 16 pages of random data into this file */ for (i = 0; i < (pagesize / 2); i++) SAFE_WRITE(1, fd, STR, sizeof(STR) - 1); SAFE_FSTAT(fd, &st); file1 = SAFE_MMAP(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); file2 = SAFE_MMAP(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); SAFE_MUNMAP(file2 + st.st_size - pagesize, pagesize); nonalign = file1 + 100; ptr_addr = SAFE_MALLOC(st.st_size); tmp_addr = (void*)LTP_ALIGN((long)ptr_addr, pagesize); SAFE_CLOSE(fd); tcases_filter(); }
static void setup(void) { unsigned int i; int fd; SAFE_MKDIR(TMP_DIR, 0664); SAFE_MOUNT(TMP_DIR, TMP_DIR, "tmpfs", 0, NULL); fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, 0664); /* Writing 40 KB of random data into this file [32 * 1280 = 40960] */ for (i = 0; i < 1280; i++) SAFE_WRITE(1, fd, STR, strlen(STR)); SAFE_FSTAT(fd, &st); /* Map the input file into shared memory */ sfile = SAFE_MMAP(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); /* Map the input file into private memory. MADV_HUGEPAGE only works * with private anonymous pages */ amem = SAFE_MMAP(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); SAFE_CLOSE(fd); }
static void do_child(void) { int fd, offset; char buf[FS_BLOCKSIZE]; char *addr = NULL; /* * We have changed SIGBUS' handler in parent process by calling * tst_sig(FORK, DEF_HANDLER, NULL), so here just restore it. */ if (signal(SIGBUS, SIG_DFL) == SIG_ERR) tst_brkm(TBROK | TERRNO, NULL, "signal(SIGBUS) failed"); memset(buf, 'a', FS_BLOCKSIZE); fd = SAFE_OPEN(NULL, "testfilec", O_RDWR); SAFE_PWRITE(NULL, 1, fd, buf, FS_BLOCKSIZE, 0); /* * In case mremap() may fail because that memory area can not be * expanded at current virtual address(MREMAP_MAYMOVE is not set), * we first do a mmap(page_size * 2) operation to reserve some * free address space. */ addr = SAFE_MMAP(NULL, NULL, page_size * 2, PROT_WRITE | PROT_READ, MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, -1, 0); SAFE_MUNMAP(NULL, addr, page_size * 2); addr = SAFE_MMAP(NULL, addr, FS_BLOCKSIZE, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0); addr[0] = 'a'; SAFE_FTRUNCATE(NULL, fd, page_size * 2); addr = mremap(addr, FS_BLOCKSIZE, 2 * page_size, 0); if (addr == MAP_FAILED) tst_brkm(TBROK | TERRNO, NULL, "mremap failed unexpectedly"); /* * Let parent process consume FS free blocks as many as possible, then * there'll be no free blocks allocated for this new file mmaping for * offset starting at 1024, 2048, or 3072. If this above kernel bug * has been fixed, usually child process will killed by SIGBUS signal, * if not, the data 'A', 'B', 'C' will be silently discarded later when * kernel writepage is called, that means data corruption. */ TST_SAFE_CHECKPOINT_WAKE(NULL, 0); TST_SAFE_CHECKPOINT_WAIT(NULL, 0); for (offset = FS_BLOCKSIZE; offset < page_size; offset += FS_BLOCKSIZE) addr[offset] = 'a'; SAFE_MUNMAP(NULL, addr, 2 * page_size); SAFE_CLOSE(NULL, fd); exit(TFAIL); }
static void test_advice_willneed(void) { int i; char *src; char *dst[100]; int page_fault_num_1; int page_fault_num_2; /* allocate source memory (1GB only) */ src = SAFE_MMAP(NULL, 1 * GB_SZ, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); /* allocate destination memory (array) */ for (i = 0; i < dst_max; ++i) dst[i] = SAFE_MMAP(NULL, 1 * GB_SZ, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); /* memmove source to each destination memories (for SWAP-OUT) */ for (i = 0; i < dst_max; ++i) memmove(dst[i], src, 1 * GB_SZ); tst_res(TINFO, "PageFault(no madvice): %d", get_page_fault_num()); /* Do madvice() to dst[0] */ TEST(madvise(dst[0], pg_sz, MADV_WILLNEED)); if (TEST_RETURN == -1) tst_brk(TBROK | TERRNO, "madvise failed"); page_fault_num_1 = get_page_fault_num(); tst_res(TINFO, "PageFault(madvice / no mem access): %d", page_fault_num_1); *dst[0] = 'a'; page_fault_num_2 = get_page_fault_num(); tst_res(TINFO, "PageFault(madvice / mem access): %d", page_fault_num_2); if (page_fault_num_1 != page_fault_num_2) tst_res(TFAIL, "Bug has been reproduced"); else tst_res(TPASS, "Regression test pass"); SAFE_MUNMAP(src, 1 * GB_SZ); for (i = 0; i < dst_max; ++i) SAFE_MUNMAP(dst[i], 1 * GB_SZ); }
int main(int argc, char *argv[]) { int lc; void *p; tst_parse_opts(argc, argv, NULL, NULL); setup(); for (lc = 0; TEST_LOOPING(lc); lc++) { p = SAFE_MMAP(cleanup, NULL, ALLOC_SIZE, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0); TEST(mprotect(p, ALLOC_SIZE, PROT_NONE)); if (TEST_RETURN == -1) tst_brkm(TBROK | TTERRNO, cleanup, "mprotect failed"); TEST(madvise(p, ALLOC_SIZE, MADV_WILLNEED)); SAFE_MUNMAP(cleanup, p, ALLOC_SIZE); if (TEST_RETURN == 0) continue; if (TEST_ERRNO == EBADF) tst_brkm(TCONF, cleanup, "CONFIG_SWAP=n"); else tst_brkm(TBROK | TTERRNO, cleanup, "madvise failed"); } tst_resm(TPASS, "issue has not been reproduced"); cleanup(); tst_exit(); }
static void setup(void) { struct sigaction sigptr; page_sz = getpagesize(); /* * Used as hint for mmap thread, so it doesn't interfere * with other potential (temporary) mappings from libc */ distant_area = SAFE_MMAP(0, DISTANT_MMAP_SIZE, PROT_WRITE | PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); SAFE_MUNMAP(distant_area, (size_t)DISTANT_MMAP_SIZE); distant_area += DISTANT_MMAP_SIZE / 2; if (tst_parse_float(str_exec_time, &exec_time, 0, FLT_MAX)) { tst_brk(TBROK, "Invalid number for exec_time '%s'", str_exec_time); } sigptr.sa_sigaction = sig_handler; sigemptyset(&sigptr.sa_mask); sigptr.sa_flags = SA_SIGINFO | SA_NODEFER; SAFE_SIGACTION(SIGSEGV, &sigptr, NULL); tst_set_timeout((int)(exec_time * 3600)); }
static void setup(void) { struct rlimit rl = { .rlim_cur = RLIM_INFINITY, .rlim_max = RLIM_INFINITY, }; int i; long arg_len, arg_count; bst = SAFE_MMAP(NULL, sizeof(*bst), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); bst->left = 0; bst->right = ARGS_SZ; arg_len = sysconf(_SC_PAGESIZE); arg = SAFE_MALLOC(arg_len); memset(arg, 'c', arg_len - 1); arg[arg_len - 1] = '\0'; args[0] = "true"; arg_count = ARGS_SZ; tst_res(TINFO, "Using %ld args of size %ld", arg_count, arg_len); for (i = 1; i < arg_count; i++) args[i] = arg; SAFE_SETRLIMIT(RLIMIT_STACK, &rl); }
static void setup(void) { tst_sig(NOFORK, DEF_HANDLER, cleanup); TEST_PAUSE; tst_tmpdir(); fd2 = SAFE_OPEN(cleanup, ".", O_DIRECTORY); SAFE_FILE_PRINTF(cleanup, "test_file", "A"); fd3 = SAFE_OPEN(cleanup, "test_file", O_RDWR); #if !defined(UCLINUX) outside_buf = SAFE_MMAP(cleanup, 0, 1, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); #endif addr4 = SAFE_MEMALIGN(cleanup, getpagesize(), (4096 * 10)); addr5 = addr4 + 1; fs_type = tst_fs_type(cleanup, "."); if (fs_type != TST_TMPFS_MAGIC) fd4 = SAFE_OPEN(cleanup, "test_file", O_RDWR | O_DIRECT); }
static void setup_ipc(void) { size_t size = getpagesize(); //TODO: Fallback to tst_tmpdir() if /dev/shm does not exits? snprintf(shm_path, sizeof(shm_path), "/dev/shm/ltp_%s_%d", tst_test->tid, getpid()); ipc_fd = open(shm_path, O_CREAT | O_EXCL | O_RDWR, 0600); if (ipc_fd < 0) tst_brk(TBROK | TERRNO, "open(%s)", shm_path); SAFE_FTRUNCATE(ipc_fd, size); results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0); /* Checkpoints needs to be accessible from processes started by exec() */ if (tst_test->needs_checkpoints) sprintf(ipc_path, IPC_ENV_VAR "=%s", shm_path); else SAFE_UNLINK(shm_path); SAFE_CLOSE(ipc_fd); if (tst_test->needs_checkpoints) { tst_futexes = (char*)results + sizeof(struct results); tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t); } }
static void bad_addr_setup(int i) { if (tcases[i].fname) return; tcases[i].fname = SAFE_MMAP(0, 1, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); }
static void setup(void) { int i; const char *fs_type; tst_require_root(); tst_sig(NOFORK, DEF_HANDLER, cleanup); TEST_PAUSE; tst_tmpdir(); fs_type = tst_dev_fs_type(); device = tst_acquire_device(cleanup); if (!device) tst_brkm(TCONF, cleanup, "Failed to acquire device"); tst_mkfs(cleanup, device, fs_type, NULL, NULL); SAFE_MKDIR(cleanup, MNTPOINT, DIR_MODE); if (mount(device, MNTPOINT, fs_type, 0, NULL) == -1) { tst_brkm(TBROK | TERRNO, cleanup, "mount device:%s failed", device); } SAFE_MKDIR(cleanup, TESTDIR5, DIR_MODE); if (mount(device, MNTPOINT, fs_type, MS_REMOUNT | MS_RDONLY, NULL) == -1) { tst_brkm(TBROK | TERRNO, cleanup, "mount device:%s failed", device); } mount_flag = 1; SAFE_MKDIR(cleanup, TESTDIR, DIR_MODE); SAFE_TOUCH(cleanup, TESTFILE, FILE_MODE, NULL); memset(longpathname, 'a', PATH_MAX + 1); SAFE_TOUCH(cleanup, TESTFILE2, FILE_MODE, NULL); #if !defined(UCLINUX) test_cases[4].dir = SAFE_MMAP(cleanup, 0, 1, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); #endif /* * NOTE: the ELOOP test is written based on that the * consecutive symlinks limit in kernel is hardwired * to 40. */ SAFE_MKDIR(cleanup, "loopdir", DIR_MODE); SAFE_SYMLINK(cleanup, "../loopdir", "loopdir/loopdir"); for (i = 0; i < 43; i++) strcat(looppathname, TESTDIR4); }
static struct queue *queue_init(void) { struct queue *q = SAFE_MMAP(NULL, sizeof(*q), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0); sem_init(&q->sem, 1, 0); q->front = 0; q->back = 0; return q; }
static void create_ksm_child(int child_num, int size, int unit, struct ksm_merge_data *ksm_merge_data) { int j, total_unit; char **memory; /* The total units in all */ total_unit = size / unit; /* Apply for the space for memory */ memory = SAFE_MALLOC(total_unit * sizeof(char *)); for (j = 0; j < total_unit; j++) { memory[j] = SAFE_MMAP(NULL, unit * MB, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); #ifdef HAVE_MADV_MERGEABLE if (madvise(memory[j], unit * MB, MADV_MERGEABLE) == -1) tst_brk(TBROK|TERRNO, "madvise"); #endif } tst_res(TINFO, "child %d stops.", child_num); if (raise(SIGSTOP) == -1) tst_brk(TBROK|TERRNO, "kill"); fflush(stdout); for (j = 0; j < 4; j++) { ksm_child_memset(child_num, size, total_unit, ksm_merge_data[j], memory); fflush(stdout); tst_res(TINFO, "child %d stops.", child_num); if (raise(SIGSTOP) == -1) tst_brk(TBROK|TERRNO, "kill"); if (ksm_merge_data[j].mergeable_size < size * MB) { verify(memory, 'e', child_num, total_unit - 1, total_unit, unit * MB - 1, unit * MB); verify(memory, ksm_merge_data[j].data, child_num, 0, total_unit, 0, unit * MB - 1); } else { verify(memory, ksm_merge_data[j].data, child_num, 0, total_unit, 0, unit * MB); } } tst_res(TINFO, "child %d finished.", child_num); }
void setup(void) { struct passwd *ltpuser; char *bad_addr; struct rlimit rlim; sigset_t signalset; tst_sig(NOFORK, DEF_HANDLER, cleanup); tst_require_root(); ltpuser = SAFE_GETPWNAM(cleanup, "nobody"); SAFE_SETEUID(cleanup, ltpuser->pw_uid); TEST_PAUSE; tst_tmpdir(); SAFE_TOUCH(cleanup, TEST_FILE1, NEW_MODE, NULL); SAFE_TOUCH(cleanup, "t_file", FILE_MODE, NULL); #if !defined(UCLINUX) test_cases[2].pathname = (char *)get_high_address(); bad_addr = SAFE_MMAP(cleanup, 0, 1, PROT_NONE, MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); test_cases[3].pathname = bad_addr; #endif memset(long_pathname, 'a', PATH_MAX + 1); SAFE_MKDIR(cleanup, TEST_DIR1, DIR_MODE); SAFE_TOUCH(cleanup, TEST_FILE3, FILE_MODE, NULL); SAFE_SYMLINK(cleanup, TEST_SYM1, TEST_SYM2); SAFE_SYMLINK(cleanup, TEST_SYM2, TEST_SYM1); rlim.rlim_cur = MAX_FSIZE; rlim.rlim_max = MAX_FSIZE; SAFE_SETRLIMIT(cleanup, RLIMIT_FSIZE, &rlim); sigemptyset(&signalset); sigaddset(&signalset, SIGXFSZ); TEST(sigprocmask(SIG_BLOCK, &signalset, NULL)); if (TEST_RETURN != 0) tst_brkm(TBROK | TTERRNO, cleanup, "sigprocmask"); }
static void verify_futex_wait(void) { int res; void *buf; size_t pagesize = getpagesize(); buf = SAFE_MMAP(NULL, NULL, pagesize, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); res = futex_wait(buf, 1, &to, 0); if (res == -1 && errno == EWOULDBLOCK) tst_resm(TPASS | TERRNO, "futex_wait() returned %i", res); else tst_resm(TFAIL | TERRNO, "futex_wait() returned %i", res); SAFE_MUNMAP(NULL, buf, pagesize); }
void tst_checkpoint_init(const char *file, const int lineno, void (*cleanup_fn)(void)) { int fd; unsigned int page_size; if (tst_futexes) { tst_brkm(TBROK, cleanup_fn, "%s: %d checkpoints already initialized", file, lineno); return; } /* * The parent test process is responsible for creating the temporary * directory and therefore must pass non-zero cleanup (to remove the * directory if something went wrong). * * We cannot do this check unconditionally because if we need to init * the checkpoint from a binary that was started by exec() the * tst_tmpdir_created() will return false because the tmpdir was * created by parent. In this case we expect the subprogram can call * the init as a first function with NULL as cleanup function. */ if (cleanup_fn && !tst_tmpdir_created()) { tst_brkm(TBROK, cleanup_fn, "%s:%d You have to create test temporary directory " "first (call tst_tmpdir())", file, lineno); return; } page_size = getpagesize(); fd = SAFE_OPEN(cleanup_fn, "checkpoint_futex_base_file", O_RDWR | O_CREAT, 0666); SAFE_FTRUNCATE(cleanup_fn, fd, page_size); tst_futexes = SAFE_MMAP(cleanup_fn, NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); tst_max_futexes = page_size / sizeof(uint32_t); SAFE_CLOSE(cleanup_fn, fd); }
static void setup(void) { tst_sig(NOFORK, DEF_HANDLER, cleanup); TEST_PAUSE; tst_tmpdir(); SAFE_SYMLINK(cleanup, TEST_SYMLINK, "statfs_symlink_2"); SAFE_SYMLINK(cleanup, "statfs_symlink_2", TEST_SYMLINK); memset(nametoolong, 'a', PATH_MAX+1); SAFE_TOUCH(cleanup, TEST_FILE, 0644, NULL); test_cases[0].path = SAFE_MMAP(cleanup, NULL, 1, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); }
static void setup_ipc(void) { size_t size = getpagesize(); if (access("/dev/shm", F_OK) == 0) { snprintf(shm_path, sizeof(shm_path), "/dev/shm/ltp_%s_%d", tid, getpid()); } else { char *tmpdir; if (!tst_tmpdir_created()) tst_tmpdir(); tmpdir = tst_get_tmpdir(); snprintf(shm_path, sizeof(shm_path), "%s/ltp_%s_%d", tmpdir, tid, getpid()); free(tmpdir); } ipc_fd = open(shm_path, O_CREAT | O_EXCL | O_RDWR, 0600); if (ipc_fd < 0) tst_brk(TBROK | TERRNO, "open(%s)", shm_path); SAFE_CHMOD(shm_path, 0666); SAFE_FTRUNCATE(ipc_fd, size); results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0); /* Checkpoints needs to be accessible from processes started by exec() */ if (tst_test->needs_checkpoints) { sprintf(ipc_path, IPC_ENV_VAR "=%s", shm_path); putenv(ipc_path); } else { SAFE_UNLINK(shm_path); } SAFE_CLOSE(ipc_fd); if (tst_test->needs_checkpoints) { tst_futexes = (char*)results + sizeof(struct results); tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t); } }
static void setup(void) { fd2 = SAFE_OPEN(".", O_DIRECTORY); SAFE_FILE_PRINTF("test_file", "A"); fd3 = SAFE_OPEN("test_file", O_RDWR); #if !defined(UCLINUX) outside_buf = SAFE_MMAP(0, 1, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); #endif addr4 = SAFE_MEMALIGN(getpagesize(), (4096 * 10)); addr5 = addr4 + 1; fs_type = tst_fs_type("."); if (fs_type != TST_TMPFS_MAGIC) fd4 = SAFE_OPEN("test_file", O_RDWR | O_DIRECT); }
static void setup(void) { tst_sig(NOFORK, DEF_HANDLER, cleanup); tst_require_root(NULL); ltpuser = SAFE_GETPWNAM(cleanup, nobody_uid); TEST_PAUSE; /* Initialize longmodname to LONGMODNAMECHAR character */ memset(longmodname, LONGMODNAMECHAR, MODULE_NAME_LEN - 1); /* Get unique module name for each child process */ if (sprintf(modname, "%s_%d", BASEMODNAME, getpid()) <= 0) tst_brkm(TBROK, NULL, "Failed to initialize module name"); tdat[2].modname = SAFE_MMAP(cleanup, 0, 1, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); }
void tst_reinit(void) { const char *path = getenv("LTP_IPC_PATH"); size_t size = getpagesize(); int fd; void *ptr; if (!path) tst_brk(TBROK, "LTP_IPC_PATH is not defined"); if (access(path, F_OK)) tst_brk(TBROK, "File %s does not exist!", path); fd = SAFE_OPEN(path, O_RDWR); ptr = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); tst_futexes = (char*)ptr + sizeof(struct results); tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t); SAFE_CLOSE(fd); }
static void setup(void) { size_t nwrite = 0; char write_buf[BUF_SIZE]; struct rlimit rl; tst_sig(NOFORK, DEF_HANDLER, cleanup); TEST_EXP_ENOS(exp_enos); tst_tmpdir(); TEST_PAUSE; page_sz = (size_t)sysconf(_SC_PAGESIZE); fd = SAFE_OPEN(cleanup, TEMPFILE, O_RDWR | O_CREAT, 0666); memset(write_buf, 'a', BUF_SIZE); while (nwrite < page_sz) { SAFE_WRITE(cleanup, 1, fd, write_buf, BUF_SIZE); nwrite += BUF_SIZE; } addr1 = SAFE_MMAP(cleanup, 0, page_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, fd, 0); /* addr2 is not a multiple of PAGESIZE */ addr2 = addr1 + 1; /* addr3 is outside the address space of the process */ if (getrlimit(RLIMIT_DATA, &rl) < 0) tst_brkm(TBROK | TERRNO, NULL, "getrlimit failed"); addr3 = (char *)rl.rlim_max; #if !defined(UCLINUX) /* memory pointed to by addr4 was not mapped */ addr4 = get_high_address(); #endif }
static void testfunc_protexec(void) { int page_sz; int (*func)(void); sig_caught = 0; page_sz = getpagesize(); func = SAFE_MMAP(cleanup, 0, page_sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); memcpy(func, exec_func, page_sz); /* Change the protection to PROT_EXEC. */ TEST(mprotect(func, page_sz, PROT_EXEC)); if (TEST_RETURN == -1) { tst_resm(TFAIL | TTERRNO, "mprotect failed"); } else { if (sigsetjmp(env, 1) == 0) (*func)(); switch (sig_caught) { case SIGSEGV: tst_resm(TFAIL, "test PROT_EXEC for mprotect failed"); break; case 0: tst_resm(TPASS, "test PROT_EXEC for mprotect success"); break; default: tst_brkm(TBROK, cleanup, "received an unexpected signal: %d", sig_caught); } } SAFE_MUNMAP(cleanup, func, page_sz); }
static void testfunc_protnone(void) { char *addr; int page_sz; sig_caught = 0; page_sz = getpagesize(); addr = SAFE_MMAP(cleanup, 0, page_sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); /* Change the protection to PROT_NONE. */ TEST(mprotect(addr, page_sz, PROT_NONE)); if (TEST_RETURN == -1) { tst_resm(TFAIL | TTERRNO, "mprotect failed"); } else { if (sigsetjmp(env, 1) == 0) addr[0] = 1; switch (sig_caught) { case SIGSEGV: tst_resm(TPASS, "test PROT_NONE for mprotect success"); break; case 0: tst_resm(TFAIL, "test PROT_NONE for mprotect failed"); break; default: tst_brkm(TBROK, cleanup, "received an unexpected signal: %d", sig_caught); } } SAFE_MUNMAP(cleanup, addr, page_sz); }
void setup(void) { struct passwd *ltpuser; char *bad_addr; tst_sig(NOFORK, DEF_HANDLER, cleanup); tst_require_root(NULL); ltpuser = SAFE_GETPWNAM(cleanup, "nobody"); SAFE_SETEUID(cleanup, ltpuser->pw_uid); TEST_PAUSE; tst_tmpdir(); SAFE_TOUCH(cleanup, TEST_FILE1, NEW_MODE, NULL); SAFE_TOUCH(cleanup, "t_file", FILE_MODE, NULL); #if !defined(UCLINUX) test_cases[2].pathname = (char *)get_high_address(); bad_addr = SAFE_MMAP(cleanup, 0, 1, PROT_NONE, MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); test_cases[3].pathname = bad_addr; #endif memset(long_pathname, 'a', PATH_MAX + 1); SAFE_MKDIR(cleanup, TEST_DIR1, DIR_MODE); SAFE_TOUCH(cleanup, TEST_FILE3, FILE_MODE, NULL); SAFE_SYMLINK(cleanup, TEST_SYM1, TEST_SYM2); SAFE_SYMLINK(cleanup, TEST_SYM2, TEST_SYM1); }
void setup(void) { char *cwdname = NULL; int fd; umask(0); tst_sig(NOFORK, DEF_HANDLER, cleanup); TEST_PAUSE; ltpuser = SAFE_GETPWNAM(NULL, nobody_uid); SAFE_SETGID(NULL, ltpuser->pw_gid); tst_tmpdir(); cwdname = SAFE_GETCWD(cleanup, cwdname, 0); sprintf(test_name5, "%s/fake.%d", cwdname, getpid()); fileHandle = SAFE_CREAT(cleanup, test_name5, 0444); sprintf(test_name3, "%s/fake.%d", test_name5, getpid()); /* creat() and close a zero length file with executeable permission */ sprintf(test_name6, "%s/execve03.%d", cwdname, getpid()); fd = SAFE_CREAT(cleanup, test_name6, 0755); fd = SAFE_CLOSE(cleanup, fd); #ifndef UCLINUX bad_addr = SAFE_MMAP(cleanup, NULL, 1, PROT_NONE, MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); TC[3].tname = bad_addr; #endif }
void *map_write_unmap(void *ptr) { int *fd = ptr; void *tmp; int i, j; for (i = 0; i < num_iter; i++) { map_address = SAFE_MMAP(distant_area, (size_t) file_size, PROT_WRITE | PROT_READ, MAP_SHARED, *fd, 0); tst_atomic_inc(&mapcnt); for (j = 0; j < file_size; j++) map_address[j] = 'b'; tmp = (void *)map_address; tst_atomic_inc(&unmapcnt); SAFE_MUNMAP(tmp, file_size); map_count++; } return NULL; }
static void child(void) { size_t i; char *ptr; unsigned int usage, old_limit, old_memsw_limit; int status, pid, retries = 0; SAFE_MKDIR(cgroup_path, 0777); SAFE_FILE_PRINTF(tasks_path, "%i", getpid()); ptr = SAFE_MMAP(NULL, PAGES * page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); for (i = 0; i < PAGES * page_size; i++) ptr[i] = 'a'; if (madvise(ptr, PAGES * page_size, MADV_FREE)) { if (errno == EINVAL) tst_brk(TCONF | TERRNO, "MADV_FREE is not supported"); tst_brk(TBROK | TERRNO, "MADV_FREE failed"); } if (ptr[page_size] != 'a') tst_res(TFAIL, "MADV_FREE pages were freed immediatelly"); else tst_res(TPASS, "MADV_FREE pages were not freed immediatelly"); ptr[TOUCHED_PAGE1 * page_size] = 'b'; ptr[TOUCHED_PAGE2 * page_size] = 'b'; usage = 8 * 1024 * 1024; tst_res(TINFO, "Setting memory limits to %u %u", usage, 2 * usage); SAFE_FILE_SCANF(limit_in_bytes_path, "%u", &old_limit); if (swap_accounting_enabled) SAFE_FILE_SCANF(memsw_limit_in_bytes_path, "%u", &old_memsw_limit); SAFE_FILE_PRINTF(limit_in_bytes_path, "%u", usage); if (swap_accounting_enabled) SAFE_FILE_PRINTF(memsw_limit_in_bytes_path, "%u", 2 * usage); do { sleep_between_faults++; pid = SAFE_FORK(); if (!pid) memory_pressure_child(); tst_res(TINFO, "Memory hungry child %i started, try %i", pid, retries); SAFE_WAIT(&status); } while (retries++ < 10 && count_freed(ptr) == 0); char map[PAGES+1]; unsigned int freed = 0; unsigned int corrupted = 0; for (i = 0; i < PAGES; i++) { char exp_val; if (ptr[i * page_size]) { exp_val = 'a'; map[i] = 'p'; } else { exp_val = 0; map[i] = '_'; freed++; } if (i != TOUCHED_PAGE1 && i != TOUCHED_PAGE2) { if (check_page(ptr + i * page_size, exp_val)) { map[i] = '?'; corrupted++; } } else { if (check_page_baaa(ptr + i * page_size)) { map[i] = '?'; corrupted++; } } } map[PAGES] = '\0'; tst_res(TINFO, "Memory map: %s", map); if (freed) tst_res(TPASS, "Pages MADV_FREE were freed on low memory"); else tst_res(TFAIL, "No MADV_FREE page was freed on low memory"); if (corrupted) tst_res(TFAIL, "Found corrupted page"); else tst_res(TPASS, "All pages have expected content"); if (swap_accounting_enabled) SAFE_FILE_PRINTF(memsw_limit_in_bytes_path, "%u", old_memsw_limit); SAFE_FILE_PRINTF(limit_in_bytes_path, "%u", old_limit); SAFE_MUNMAP(ptr, PAGES); exit(0); }
static void setup(void) { bad_addr = SAFE_MMAP(0, 1, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); }