static void verify_event(int mask) { int ret; struct fanotify_event_metadata *event; struct stat st; /* Read the event */ ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, EVENT_BUF_LEN - len); event = (struct fanotify_event_metadata *)&event_buf[len]; len += ret; if (event->mask != FAN_OPEN) { tst_resm(TFAIL, "got unexpected event %llx", (unsigned long long)event->mask); } else if (fstat(event->fd, &st) < 0) { tst_resm(TFAIL, "failed to stat event->fd (%s)", strerror(errno)); } else if ((st.st_mode & S_IFMT) != mask) { tst_resm(TFAIL, "event->fd points to object of different type " "(%o != %o)", st.st_mode & S_IFMT, mask); } else { tst_resm(TPASS, "event generated properly for type %o", mask); } close(event->fd); }
static void child_invoke(void) { char *lp, *rp; char buf[BUFSIZ]; struct iovec local, remote; /* get addr from pipe */ SAFE_CLOSE(tst_exit, pipe_fd[1]); SAFE_READ(tst_exit, 0, pipe_fd[0], buf, BUFSIZ); SAFE_CLOSE(tst_exit, pipe_fd[0]); if (sscanf(buf, "%p", &rp) != 1) tst_brkm(TBROK | TERRNO, tst_exit, "sscanf"); lp = SAFE_MALLOC(tst_exit, len + 1); local.iov_base = lp; local.iov_len = len; remote.iov_base = rp; remote.iov_len = len; tst_resm(TINFO, "child 1: reading string from same memory location."); TEST(test_process_vm_readv(pids[0], &local, 1, &remote, 1, 0)); if (TEST_RETURN != len) tst_brkm(TFAIL | TERRNO, tst_exit, "process_vm_readv"); if (strncmp(lp, tst_string, len) != 0) tst_brkm(TFAIL, tst_exit, "child 1: expected string: %s, " "received string: %256s", tst_string, lp); else tst_resm(TPASS, "expected string received."); }
static int ilbc_read_header(FILE* fp, struct amci_file_desc_t* fmt_desc) { unsigned int s; char tag[9]={'\0'}; if(!fp) return -1; rewind(fp); DBG("trying to read iLBC file\n"); SAFE_READ(tag,9,fp,s); DBG("tag = <%.9s>\n",tag); if(!strncmp(tag,"#iLBC30\n",9) ){ fmt_desc->subtype=ILBC30; } else if (!strncmp(tag,"#iLBC20\n",9)) { fmt_desc->subtype=ILBC20; } else { DBG("wrong format !"); return -1; } fmt_desc->rate = 8000; fmt_desc->channels = 1; fseek(fp, 0, SEEK_END); fmt_desc->data_size = ftell(fp) - 9; // file size - header size fseek(fp, 9, SEEK_SET); // get at start of samples return 0; }
static void child_write(void) { unsigned char *lp, *rp; char buf[bufsz]; struct iovec local, remote; long i; /* get addr from pipe */ SAFE_CLOSE(tst_exit, pipe_fd[1]); SAFE_READ(tst_exit, 0, pipe_fd[0], buf, bufsz); SAFE_CLOSE(tst_exit, pipe_fd[0]); if (sscanf(buf, "%p", &rp) != 1) tst_brkm(TBROK | TERRNO, tst_exit, "sscanf"); lp = SAFE_MALLOC(tst_exit, bufsz + PADDING_SIZE * 2); for (i = 0; i < bufsz + PADDING_SIZE * 2; i++) lp[i] = DEFAULT_CHAR; for (i = 0; i < bufsz; i++) lp[i + PADDING_SIZE] = i % 256; local.iov_base = lp + PADDING_SIZE; local.iov_len = bufsz; remote.iov_base = rp; remote.iov_len = bufsz; tst_resm(TINFO, "child 2: write to the same memory location."); TEST(ltp_syscall(__NR_process_vm_writev, pids[0], &local, 1UL, &remote, 1UL, 0UL)); if (TEST_RETURN != bufsz) tst_brkm(TFAIL | TERRNO, tst_exit, "process_vm_readv"); }
static void run_targets(const char *dirname, char *cfile, pid_t tid) { int i, k, fd; char subdir[PATH_MAX] = {0}; char *output_file; char buf[11]; const char *const cmd_run[] = {cfile, NULL}; SAFE_ASPRINTF(&output_file, "%s/cmd.out", dirname); /* run each binary */ for (i = 0; i < dirs_num; ++i) { for (k = 0; k < file_num; ++k) { snprintf(cfile, PATH_MAX, "%s%s/%d.%d.%d", dirname, subdir, tid, i, k); tst_run_cmd(cmd_run, output_file, NULL, 0); fd = SAFE_OPEN(output_file, O_RDONLY); SAFE_READ(1, fd, buf, 11); if (strncmp(buf, "hello world", 11)) tst_brk(TFAIL, "command printed wrong message"); SAFE_CLOSE(fd); } strcat(subdir, "/dir"); } free(output_file); }
static long *fetch_remote_addrs(void) { long *foo, *bar; char buf[BUFSIZ]; long len; struct iovec local, remote; /* get addr from pipe */ SAFE_CLOSE(tst_exit, pipe_fd[1]); SAFE_READ(tst_exit, 0, pipe_fd[0], buf, BUFSIZ); SAFE_CLOSE(tst_exit, pipe_fd[0]); if (sscanf(buf, "%p", &foo) != 1) tst_brkm(TBROK | TERRNO, tst_exit, "sscanf"); len = nr_iovecs * sizeof(long); bar = SAFE_MALLOC(tst_exit, len); local.iov_base = bar; local.iov_len = len; remote.iov_base = foo; remote.iov_len = len; TEST(test_process_vm_readv(pids[0], &local, 1, &remote, 1, 0)); if (TEST_RETURN != len) tst_brkm(TFAIL | TERRNO, tst_exit, "process_vm_readv"); return local.iov_base; }
static void testfunc_seekend(void) { int fd; ssize_t nread; static char read_buf[BUFSIZ]; /* reopen TEST_FILE and file offset will be 0 */ fd = SAFE_OPEN(cleanup, TEST_FILE, O_RDONLY); TEST(lseek64(fd, (loff_t) 0, SEEK_END)); if (TEST_RETURN == (loff_t) -1) { tst_resm(TFAIL | TTERRNO, "llseek failed on %s ", TEST_FILE); goto cleanup_seekend; } if (TEST_RETURN != (long)file_size) { tst_resm(TFAIL, "llseek return a incorrect file offset"); goto cleanup_seekend; } memset(read_buf, 0, sizeof(read_buf)); nread = SAFE_READ(cleanup, 0, fd, read_buf, file_size); if (nread > 0) tst_resm(TFAIL, "Read bytes after llseek to end of file"); else tst_resm(TPASS, "test SEEK_END for llseek success"); cleanup_seekend: SAFE_CLOSE(cleanup, fd); }
int ProfilerSocket::ReadByte( BYTE& b ) { int result; SAFE_READ( _s, b, result ); return result; }
myxml_node_t* myxml_loadTreeBIN(FILE *instream) { uint32_t num_nodes; uint32_t temp; SAFE_READ(&temp, sizeof(uint32_t), instream); num_nodes = MYNTOHL(temp); return myxml_loadTreeHelper(instream, NULL); }
static void mylinkat_test(struct test_struct *desc) { int fd; TEST(mylinkat (*desc->oldfd, desc->oldfn, *desc->newfd, desc->newfn, desc->flags)); if (TEST_ERRNO == desc->expected_errno) { if (STD_FUNCTIONAL_TEST) { if (TEST_RETURN == 0 && desc->referencefn1 != NULL) { int tnum = rand(), vnum = ~tnum; fd = SAFE_OPEN(cleanup, desc->referencefn1, O_RDWR); SAFE_WRITE(cleanup, 1, fd, &tnum, sizeof(tnum)); SAFE_CLOSE(cleanup, fd); fd = SAFE_OPEN(cleanup, desc->referencefn2, O_RDONLY); SAFE_READ(cleanup, 1, fd, &vnum, sizeof(vnum)); SAFE_CLOSE(cleanup, fd); if (tnum == vnum) tst_resm(TPASS, "linkat is functionality correct"); else { tst_resm(TFAIL, "The link file's content isn't " "as same as the original file's " "although linkat returned 0"); } } else { if (TEST_RETURN == 0) tst_resm(TPASS, "linkat succeeded as expected"); else tst_resm(TPASS | TTERRNO, "linkat failed as expected"); } } else tst_resm(TPASS, "Test passed"); } else { if (TEST_RETURN == 0) tst_resm(TFAIL, "linkat succeeded unexpectedly"); else tst_resm(TFAIL | TTERRNO, "linkat failed unexpectedly; expected %d - %s", desc->expected_errno, strerror(desc->expected_errno)); } }
int main(int argc, char **argv) { char buf[10]; int fds[2]; buf[9] = '\0'; system("cp " __FILE__ " " __FILE__ "~"); printf("%s\n", SAFE_BASENAME(NULL, *argv)); printf("%s\n", SAFE_DIRNAME(NULL, *argv)); fd = SAFE_OPEN(cleanup, __FILE__ "~", O_RDWR); SAFE_READ(cleanup, 0, fd, buf, 9); printf("buf: %s\n", buf); SAFE_READ(cleanup, 1, fd, buf, 9); printf("buf: %s\n", buf); SAFE_WRITE(cleanup, 0, -1, buf, 9); SAFE_WRITE(NULL, 0, fd, buf, 9); SAFE_WRITE(NULL, 1, fd, buf, 9); SAFE_PIPE(NULL, fds); return 0; }
static void check_epoll_ctl(int opt, int exp_num) { int res; unsigned int events; char write_buf[] = "test"; char read_buf[sizeof(write_buf)]; struct epoll_event res_evs[2]; events = EPOLLIN; if (exp_num == 2) events |= EPOLLOUT; SAFE_WRITE(1, fd[1], write_buf, sizeof(write_buf)); while (events) { int events_matched = 0; bzero(res_evs, sizeof(res_evs)); res = epoll_wait(epfd, res_evs, 2, -1); if (res <= 0) { tst_res(TFAIL | TERRNO, "epoll_wait() returned %i", res); goto end; } if ((events & EPOLLIN) && has_event(res_evs, 2, fd[0], EPOLLIN)) { events_matched++; events &= ~EPOLLIN; } if ((events & EPOLLOUT) && has_event(res_evs, 2, fd[1], EPOLLOUT)) { events_matched++; events &= ~EPOLLOUT; } if (res != events_matched) { tst_res(TFAIL, "epoll_wait() returned unexpected events"); goto end; } } tst_res(TPASS, "epoll_ctl() succeeds with op %i", opt); end: SAFE_READ(1, fd[0], read_buf, sizeof(write_buf)); }
static void testfunc_seekcur(void) { int fd; static char read_buf[BUFSIZ]; /* reopen TEST_FILE and file offset will be 0 */ fd = SAFE_OPEN(cleanup, TEST_FILE, O_RDONLY); /* after read, file offset will be 4 */ SAFE_READ(cleanup, 1, fd, read_buf, 4); TEST(lseek64(fd, (loff_t) 1, SEEK_CUR)); if (TEST_RETURN == (loff_t) -1) { tst_resm(TFAIL | TTERRNO, "llseek failed on %s ", TEST_FILE); goto cleanup_seekcur; } if (TEST_RETURN != 5) { tst_resm(TFAIL, "llseek return a incorrect file offset"); goto cleanup_seekcur; } memset(read_buf, 0, sizeof(read_buf)); /* the expected characters are "fgh" */ SAFE_READ(cleanup, 1, fd, read_buf, 3); if (strcmp(read_buf, "fgh")) tst_resm(TFAIL, "Read wrong bytes after llseek"); else tst_resm(TPASS, "test SEEK_SET for llseek success"); cleanup_seekcur: SAFE_CLOSE(cleanup, fd); }
static void check_file_data(const char exp_buf[], size_t size) { char rbuf[size]; tst_resm(TINFO, "reading the file, compare with expected buffer"); SAFE_LSEEK(cleanup, fd, 0, SEEK_SET); SAFE_READ(cleanup, 1, fd, rbuf, size); if (memcmp(exp_buf, rbuf, size)) { if (verbose) { tst_resm_hexd(TINFO, exp_buf, size, "expected:"); tst_resm_hexd(TINFO, rbuf, size, "but read:"); } tst_brkm(TFAIL, cleanup, "not expected file data"); } }
void testfunc_noatime(void) { struct stat file_stat, file_newstat; char buf; const char *flags[] = {"noatime", "relatime", NULL}; int ret; if ((tst_kvercmp(2, 6, 8)) < 0) { tst_resm(TCONF, "test O_NOATIME flags for openat " "needs kernel 2.6.8 or higher"); return; } ret = tst_path_has_mnt_flags(cleanup, NULL, flags); if (ret > 0) { tst_resm(TCONF, "test O_NOATIME flag for openat needs " "filesystems which are mounted without " "noatime and relatime"); return; } SAFE_STAT(cleanup, TEST_FILE, &file_stat); sleep(1); TEST(openat(AT_FDCWD, TEST_FILE, O_NOATIME | O_RDONLY, 0777)); if (TEST_RETURN == -1) { tst_resm(TFAIL | TTERRNO, "openat failed"); return; } SAFE_READ(cleanup, 1, TEST_RETURN, &buf, 1); SAFE_CLOSE(cleanup, TEST_RETURN); SAFE_STAT(cleanup, TEST_FILE, &file_newstat); if (file_stat.st_atime == file_newstat.st_atime) tst_resm(TPASS, "test O_NOATIME for openat success"); else tst_resm(TFAIL, "test O_NOATIME for openat failed"); }
static void check_file(void) { int i; char vmsplicebuffer[TEST_BLOCK_SIZE]; fd_out = SAFE_OPEN(TESTFILE, O_RDONLY); SAFE_READ(1, fd_out, vmsplicebuffer, TEST_BLOCK_SIZE); for (i = 0; i < TEST_BLOCK_SIZE; i++) { if (buffer[i] != vmsplicebuffer[i]) break; } if (i < TEST_BLOCK_SIZE) tst_res(TFAIL, "Wrong data read from the buffer at %i", i); else tst_res(TPASS, "Written data has been read back correctly"); SAFE_CLOSE(fd_out); }
static void test_noatime(void) { char read_buf; struct stat old_stat, new_stat; if ((tst_kvercmp(2, 6, 8)) < 0) { tst_resm(TCONF, "O_NOATIME flags test for open(2) needs kernel 2.6.8 " "or higher"); return; } if (skip_noatime) { tst_resm(TCONF, "test O_NOATIME flag for open needs filesystems which " "is mounted without noatime and relatime"); return; } SAFE_STAT(cleanup, TEST_FILE, &old_stat); sleep(1); TEST(open(TEST_FILE, O_RDONLY | O_NOATIME, 0777)); if (TEST_RETURN == -1) { tst_resm(TFAIL | TTERRNO, "open failed"); return; } SAFE_READ(cleanup, 1, TEST_RETURN, &read_buf, 1); SAFE_CLOSE(cleanup, TEST_RETURN); SAFE_STAT(cleanup, TEST_FILE, &new_stat); if (old_stat.st_atime == new_stat.st_atime) tst_resm(TPASS, "test O_NOATIME for open success"); else tst_resm(TFAIL, "test O_NOATIME for open failed"); }
/* read unsigned int 32 little endian */ static uint32_t readu32le( void ) { uint8_t x[ 4 ]; SAFE_READ( x, 4 ); return x[0] | ( x[1] << 8 ) | ( x[2] << 16 ) | ( x[3] << 24 ); }
int main(int ac, char **av) { int lc, i; int len, stop; tst_parse_opts(ac, av, NULL, NULL); setup(); for (lc = 0; TEST_LOOPING(lc); lc++) { /* * generate events */ fd = SAFE_OPEN(cleanup, fname, O_RDWR); for (i = 0; i < max_events; i++) { SAFE_LSEEK(cleanup, fd, 0, SEEK_SET); SAFE_READ(cleanup, 1, fd, buf, BUF_SIZE); SAFE_LSEEK(cleanup, fd, 0, SEEK_SET); SAFE_WRITE(cleanup, 1, fd, buf, BUF_SIZE); } SAFE_CLOSE(cleanup, fd); stop = 0; while (!stop) { /* * get list on events */ len = read(fd_notify, event_buf, EVENT_BUF_LEN); if (len < 0) { tst_brkm(TBROK | TERRNO, cleanup, "read(%d, buf, %zu) failed", fd_notify, EVENT_BUF_LEN); } /* * check events */ i = 0; while (i < len) { struct inotify_event *event; event = (struct inotify_event *)&event_buf[i]; if (event->mask != IN_ACCESS && event->mask != IN_MODIFY && event->mask != IN_OPEN && event->mask != IN_Q_OVERFLOW) { tst_resm(TFAIL, "get event: wd=%d mask=%x " "cookie=%u (expected 0) len=%u", event->wd, event->mask, event->cookie, event->len); stop = 1; break; } if (event->mask == IN_Q_OVERFLOW) { if (event->len != 0 || event->cookie != 0 || event->wd != -1) { tst_resm(TFAIL, "invalid overflow event: " "wd=%d mask=%x " "cookie=%u len=%u", event->wd, event->mask, event->cookie, event->len); stop = 1; break; } if ((int)(i + EVENT_SIZE) != len) { tst_resm(TFAIL, "overflow event is not last"); stop = 1; break; } tst_resm(TPASS, "get event: wd=%d " "mask=%x cookie=%u len=%u", event->wd, event->mask, event->cookie, event->len); stop = 1; break; } i += EVENT_SIZE + event->len; } } } cleanup(); tst_exit(); }
void *thread_fn_01(void *arg) { int i; unsigned char buf[write_size]; int fd = SAFE_OPEN(fname, O_RDWR); memset(buf, (intptr_t)arg, write_size); struct flock64 lck = { .l_whence = SEEK_SET, .l_start = 0, .l_len = 1, }; for (i = 0; i < writes_num; ++i) { lck.l_type = F_WRLCK; my_fcntl(fd, F_OFD_SETLKW, &lck); SAFE_LSEEK(fd, 0, SEEK_END); SAFE_WRITE(1, fd, buf, write_size); lck.l_type = F_UNLCK; my_fcntl(fd, F_OFD_SETLKW, &lck); sched_yield(); } SAFE_CLOSE(fd); return NULL; } static void test01(void) { intptr_t i; int k; pthread_t id[thread_cnt]; int res[thread_cnt]; unsigned char buf[write_size]; tst_res(TINFO, "write to a file inside threads with OFD locks"); int fd = SAFE_OPEN(fname, O_CREAT | O_TRUNC | O_RDWR, 0600); memset(res, 0, sizeof(res)); spawn_threads(id, thread_fn_01); wait_threads(id); tst_res(TINFO, "verifying file's data"); SAFE_LSEEK(fd, 0, SEEK_SET); for (i = 0; i < writes_num * thread_cnt; ++i) { SAFE_READ(1, fd, buf, write_size); if (buf[0] >= thread_cnt) { tst_res(TFAIL, "unexpected data read"); return; } ++res[buf[0]]; for (k = 1; k < write_size; ++k) { if (buf[0] != buf[k]) { tst_res(TFAIL, "unexpected data read"); return; } } } for (i = 0; i < thread_cnt; ++i) { if (res[i] != writes_num) { tst_res(TFAIL, "corrupted data found"); return; } } SAFE_CLOSE(fd); tst_res(TPASS, "OFD locks synchronized access between threads"); }
bool CFont::load(CommonLib::IReadStream *pStream) { CommonLib::FxMemoryReadStream stream; SAFE_READ(pStream->save_read(&stream, true)) SAFE_READ(stream.save_read(m_sFace)) SAFE_READ(stream.save_read(m_nSize)) SAFE_READ(stream.save_read(m_nCharSet)) SAFE_READ(m_color.load(&stream)) SAFE_READ(stream.save_read(m_nStyle)) SAFE_READ(stream.save_read(m_dOrientation)) SAFE_READ(m_bgColor.load(&stream)) SAFE_READ(stream.save_read(m_bIsTransparent)) byte vAlignment = 0; byte hAlignment = 0; SAFE_READ(stream.save_read(vAlignment)) SAFE_READ(stream.save_read(hAlignment)) m_vAlignment = (eTextVAlignment)vAlignment; m_hAlignment = (eTextHAlignment)hAlignment; stream.save_read(m_nHaloSize); return true; }
static int MP3_open(FILE* fp, struct amci_file_desc_t* fmt_desc, int options, long h_codec) { #ifdef WITH_MPG123DECODER unsigned char mp_rd_buf[20]; size_t sr, decoded_size; mp3_coder_state* coder_state; int res; DBG("mp3_open.\n"); #endif if(options == AMCI_RDONLY){ #ifndef WITH_MPG123DECODER ERROR("MP3 decoding support not compiled in.\n"); return -1; #else if (!h_codec) { ERROR("mp3 decoder not initialized!\n"); return -1; } coder_state = (mp3_coder_state*)h_codec; DBG("Initializing mpg123 codec state.\n"); res = mpg123_open_feed(coder_state->mpg123_h); if (MPG123_ERR == res) { ERROR("mpg123_open_feed returned mpg123 error '%s'\n", mpg123_strerror(coder_state->mpg123_h)); return -1; } /* read until we know the format */ while (res!= MPG123_NEW_FORMAT) { SAFE_READ(mp_rd_buf, 20, fp, sr); res = mpg123_decode(coder_state->mpg123_h, mp_rd_buf, 20, mp_rd_buf, 0, &decoded_size); if (res == MPG123_ERR) { ERROR("trying to determine MP3 file format: '%s'\n", mpg123_strerror(coder_state->mpg123_h)); return -1; } } mpg123_getformat(coder_state->mpg123_h, &coder_state->rate, &coder_state->channels, &coder_state->enc); DBG("mpg123: New format: %li Hz, %i channels, encoding value %i\n", coder_state->rate, coder_state->channels, coder_state->enc); fmt_desc->subtype = 1; // ? fmt_desc->rate = coder_state->rate; fmt_desc->channels = coder_state->channels; fmt_desc->data_size = -1; /* set buffering parameters */ fmt_desc->buffer_size = MP3_FRAMESAMPLES * 8; fmt_desc->buffer_thresh = MP3_FRAMESAMPLES; fmt_desc->buffer_full_thresh = MP3_FRAMESAMPLES * 3; return 0; #endif } return 0; }
static void umount2_verify(void) { int ret; char buf[256]; const char *str = "abcdefghijklmnopqrstuvwxyz"; SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, 0, NULL); mount_flag = 1; fd = SAFE_CREAT(cleanup, MNTPOINT "/file", FILE_MODE); TEST(umount2(MNTPOINT, MNT_DETACH)); if (TEST_RETURN != 0) { tst_resm(TFAIL | TTERRNO, "umount2(2) Failed"); goto EXIT; } mount_flag = 0; /* check the unavailability for new access */ ret = access(MNTPOINT "/file", F_OK); if (ret != -1) { tst_resm(TFAIL, "umount2(2) MNT_DETACH flag " "performed abnormally"); goto EXIT; } /* * check the old fd still points to the file * in previous mount point and is available */ SAFE_WRITE(cleanup, 1, fd, str, strlen(str)); SAFE_CLOSE(cleanup, fd); SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, 0, NULL); mount_flag = 1; fd = SAFE_OPEN(cleanup, MNTPOINT "/file", O_RDONLY); memset(buf, 0, sizeof(buf)); SAFE_READ(cleanup, 1, fd, buf, strlen(str)); if (strcmp(str, buf)) { tst_resm(TFAIL, "umount2(2) MNT_DETACH flag " "performed abnormally"); goto EXIT; } tst_resm(TPASS, "umount2(2) Passed"); EXIT: SAFE_CLOSE(cleanup, fd); fd = 0; if (mount_flag) { if (tst_umount(MNTPOINT)) tst_brkm(TBROK, cleanup, "umount() failed"); mount_flag = 0; } }
static void test_partially_valid_iovec(int initial_file_offset) { int i, fd; unsigned char buffer[BUFSIZE], fpattern[BUFSIZE], tmp[BUFSIZE]; long off_after; struct iovec wr_iovec[] = { { buffer, CHUNK }, { bad_addr, CHUNK }, { buffer + CHUNK, CHUNK }, { buffer + CHUNK * 2, CHUNK }, }; tst_res(TINFO, "starting test with initial file offset: %d ", initial_file_offset); for (i = 0; i < BUFSIZE; i++) buffer[i] = i % (CHUNK - 1); memset(fpattern, 0xff, BUFSIZE); tst_fill_file(TESTFILE, 0xff, CHUNK, BUFSIZE / CHUNK); fd = SAFE_OPEN(TESTFILE, O_RDWR, 0644); SAFE_LSEEK(fd, initial_file_offset, SEEK_SET); TEST(writev(fd, wr_iovec, ARRAY_SIZE(wr_iovec))); off_after = (long) SAFE_LSEEK(fd, 0, SEEK_CUR); /* bad errno */ if (TEST_RETURN == -1 && TEST_ERRNO != EFAULT) { tst_res(TFAIL | TTERRNO, "unexpected errno"); SAFE_CLOSE(fd); return; } /* nothing has been written */ if (TEST_RETURN == -1 && TEST_ERRNO == EFAULT) { tst_res(TINFO, "got EFAULT"); /* initial file content remains untouched */ SAFE_LSEEK(fd, 0, SEEK_SET); SAFE_READ(1, fd, tmp, BUFSIZE); if (memcmp(tmp, fpattern, BUFSIZE)) tst_res(TFAIL, "file was written to"); else tst_res(TPASS, "file stayed untouched"); /* offset hasn't changed */ if (off_after == initial_file_offset) tst_res(TPASS, "offset stayed unchanged"); else tst_res(TFAIL, "offset changed to %ld", off_after); SAFE_CLOSE(fd); return; } /* writev() wrote more bytes than bytes preceding invalid iovec */ tst_res(TINFO, "writev() has written %ld bytes", TEST_RETURN); if (TEST_RETURN > (long) wr_iovec[0].iov_len) { tst_res(TFAIL, "writev wrote more than expected"); SAFE_CLOSE(fd); return; } /* file content matches written bytes */ SAFE_LSEEK(fd, initial_file_offset, SEEK_SET); SAFE_READ(1, fd, tmp, TEST_RETURN); if (memcmp(tmp, wr_iovec[0].iov_base, TEST_RETURN) == 0) { tst_res(TPASS, "file has expected content"); } else { tst_res(TFAIL, "file has unexpected content"); tst_res_hexd(TFAIL, wr_iovec[0].iov_base, TEST_RETURN, "expected:"); tst_res_hexd(TFAIL, tmp, TEST_RETURN, "actual file content:"); } /* file offset has been updated according to written bytes */ if (off_after == initial_file_offset + TEST_RETURN) tst_res(TPASS, "offset at %ld as expected", off_after); else tst_res(TFAIL, "offset unexpected %ld", off_after); SAFE_CLOSE(fd); }
int main( int argc, char *argv[] ) { uint8_t x[ 16 ]; uint32_t u32; uint16_t u16; uint8_t u8; int i, j; (void)argc; (void)argv; SAFE_READ( &u8, 1 ); (void)printf( " script length: %d bytes\n", u8 ); u32 = readu32le(); time_t dt = VKI_EPOCH + u32; struct tm *utc = gmtime( &dt ); (void)printf( " start time (UTC): %s", asctime( utc )); u16 = readu16le(); (void)printf( " repeat time: %ds\n", u16 ); SAFE_READ( &u8, 1 ); (void)printf( " command count: %d\n", u8 ); int cc = u8; for( i = 0 ; i < cc ; ++i ) { uint8_t xor = 0x00; SAFE_READ( &u8, 1 ); if( u8 != 0x7e ) die( "garbage on stdin" ); SAFE_READ( &u8, 1 ); (void)printf( "CMD_ID: 0x%02x", u8 ); if( u8 == 0xff ) { (void)printf( "\n" ); break; } xor ^= u8; SAFE_READ( &u8, 1 ); (void)printf( ", LEN: %d", u8 ); xor ^= u8; if( u8 > 0 ) { SAFE_READ( x, u8 ); xor ^= x[0]; (void)printf( ", DATA: [ 0x%02x", x[0] ); for( j = 1 ; j < u8 ; ++j ) { (void)printf( ", 0x%02x", x[j] ); xor ^= x[j]; } (void)printf( " ]" ); } SAFE_READ( &u8, 1 ); (void)printf( ", XOR: 0x%02x %s", u8, ( u8 == xor ) ? "(match)" : "MISMATCH" ); u16 = readu16le(); (void)printf( ", DELAY:" ); if( u16 == 0xffff ) { (void)printf( " none\n" ); } else { (void)printf( " %ds\n", u16 ); } } return 0; }
int main(int ac, char **av) { int lc; const char *msg; if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); setup(); for (lc = 0; TEST_LOOPING(lc); lc++) { int ret, len, i = 0, test_num = 0; tst_count = 0; if (myfanotify_mark(fd_notify, FAN_MARK_ADD, FAN_ACCESS | FAN_MODIFY | FAN_CLOSE | FAN_OPEN | FAN_EVENT_ON_CHILD, AT_FDCWD, ".") < 0) { tst_brkm(TBROK | TERRNO, cleanup, "fanotify_mark (%d, FAN_MARK_ADD, FAN_ACCESS | " "FAN_MODIFY | FAN_CLOSE | FAN_OPEN | " "FAN_EVENT_ON_CHILD, AT_FDCWD, '.') failed", fd_notify); } /* * generate sequence of events */ fd = SAFE_OPEN(cleanup, fname, O_RDWR | O_CREAT, 0700); event_set[tst_count] = FAN_OPEN; tst_count++; SAFE_WRITE(cleanup, 1, fd, fname, strlen(fname)); event_set[tst_count] = FAN_MODIFY; tst_count++; SAFE_CLOSE(cleanup, fd); event_set[tst_count] = FAN_CLOSE_WRITE; tst_count++; /* * Get list of events so far. We get events here to avoid * merging of following events with the previous ones. */ ret = SAFE_READ(cleanup, 0, fd_notify, event_buf, EVENT_BUF_LEN); len = ret; fd = SAFE_OPEN(cleanup, fname, O_RDONLY); event_set[tst_count] = FAN_OPEN; tst_count++; SAFE_READ(cleanup, 0, fd, buf, BUF_SIZE); event_set[tst_count] = FAN_ACCESS; tst_count++; SAFE_CLOSE(cleanup, fd); event_set[tst_count] = FAN_CLOSE_NOWRITE; tst_count++; /* * get next events */ ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, EVENT_BUF_LEN - len); len += ret; /* * now remove child mark */ if (myfanotify_mark(fd_notify, FAN_MARK_REMOVE, FAN_EVENT_ON_CHILD, AT_FDCWD, ".") < 0) { tst_brkm(TBROK | TERRNO, cleanup, "fanotify_mark (%d, FAN_MARK REMOVE, " "FAN_EVENT_ON_CHILD, AT_FDCWD, '.') failed", fd_notify); } /* * Do something to verify events didn't get generated */ fd = SAFE_OPEN(cleanup, fname, O_RDONLY); SAFE_CLOSE(cleanup, fd); fd = SAFE_OPEN(cleanup, ".", O_RDONLY | O_DIRECTORY); event_set[tst_count] = FAN_OPEN; tst_count++; SAFE_CLOSE(cleanup, fd); event_set[tst_count] = FAN_CLOSE_NOWRITE; tst_count++; /* * Check events got generated only for the directory */ ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, EVENT_BUF_LEN - len); len += ret; if (TST_TOTAL != tst_count) { tst_brkm(TBROK, cleanup, "TST_TOTAL and tst_count are not equal"); } tst_count = 0; /* * check events */ while (i < len) { struct fanotify_event_metadata *event; event = (struct fanotify_event_metadata *)&event_buf[i]; if (test_num >= TST_TOTAL) { tst_resm(TFAIL, "get unnecessary event: mask=%llx " "pid=%u fd=%u", (unsigned long long)event->mask, (unsigned)event->pid, event->fd); } else if (!(event->mask & event_set[test_num])) { tst_resm(TFAIL, "get event: mask=%llx (expected %llx) " "pid=%u fd=%u", (unsigned long long)event->mask, event_set[test_num], (unsigned)event->pid, event->fd); } else if (event->pid != getpid()) { tst_resm(TFAIL, "get event: mask=%llx pid=%u " "(expected %u) fd=%u", (unsigned long long)event->mask, (unsigned)event->pid, (unsigned)getpid(), event->fd); } else { tst_resm(TPASS, "get event: mask=%llx pid=%u fd=%u", (unsigned long long)event->mask, (unsigned)event->pid, event->fd); } event->mask &= ~event_set[test_num]; /* No events left in current mask? Go for next event */ if (event->mask == 0) { i += event->event_len; close(event->fd); } test_num++; } for (; test_num < TST_TOTAL; test_num++) { tst_resm(TFAIL, "didn't get event: mask=%llx", event_set[test_num]); } } cleanup(); tst_exit(); }
int main(int ac, char **av) { int lc; const char *msg; if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); setup(); for (lc = 0; TEST_LOOPING(lc); lc++) { int ret, len, i = 0, test_num = 0; tst_count = 0; if (myfanotify_mark(fd_notify, FAN_MARK_ADD, FAN_ACCESS | FAN_MODIFY | FAN_CLOSE | FAN_OPEN, AT_FDCWD, fname) < 0) { tst_brkm(TBROK | TERRNO, cleanup, "fanotify_mark (%d, FAN_MARK_ADD, FAN_ACCESS | " "FAN_MODIFY | FAN_CLOSE | FAN_OPEN, AT_FDCWD, %s) " "failed", fd_notify, fname); } /* * generate sequence of events */ fd = SAFE_OPEN(cleanup, fname, O_RDONLY); event_set[tst_count] = FAN_OPEN; tst_count++; SAFE_READ(cleanup, 0, fd, buf, BUF_SIZE); event_set[tst_count] = FAN_ACCESS; tst_count++; SAFE_CLOSE(cleanup, fd); event_set[tst_count] = FAN_CLOSE_NOWRITE; tst_count++; /* * Get list of events so far. We get events here to avoid * merging of following events with the previous ones. */ ret = SAFE_READ(cleanup, 0, fd_notify, event_buf, EVENT_BUF_LEN); len = ret; fd = SAFE_OPEN(cleanup, fname, O_RDWR | O_CREAT, 0700); event_set[tst_count] = FAN_OPEN; tst_count++; SAFE_WRITE(cleanup, 1, fd, fname, strlen(fname)); event_set[tst_count] = FAN_MODIFY; tst_count++; SAFE_CLOSE(cleanup, fd); event_set[tst_count] = FAN_CLOSE_WRITE; tst_count++; /* * get another list of events */ ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, EVENT_BUF_LEN - len); len += ret; /* * Ignore mask testing */ /* Ignore access events */ if (myfanotify_mark(fd_notify, FAN_MARK_ADD | FAN_MARK_IGNORED_MASK, FAN_ACCESS, AT_FDCWD, fname) < 0) { tst_brkm(TBROK | TERRNO, cleanup, "fanotify_mark (%d, FAN_MARK_ADD | " "FAN_MARK_IGNORED_MASK, FAN_ACCESS, " "AT_FDCWD, %s) failed", fd_notify, fname); } fd = SAFE_OPEN(cleanup, fname, O_RDWR); event_set[tst_count] = FAN_OPEN; tst_count++; /* This event should be ignored */ SAFE_READ(cleanup, 0, fd, buf, BUF_SIZE); /* * get another list of events to verify the last one got ignored */ ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, EVENT_BUF_LEN - len); len += ret; lseek(fd, 0, SEEK_SET); /* Generate modify event to clear ignore mask */ SAFE_WRITE(cleanup, 1, fd, fname, 1); event_set[tst_count] = FAN_MODIFY; tst_count++; /* * This event shouldn't be ignored because previous modification * should have removed the ignore mask */ SAFE_READ(cleanup, 0, fd, buf, BUF_SIZE); event_set[tst_count] = FAN_ACCESS; tst_count++; SAFE_CLOSE(cleanup, fd); event_set[tst_count] = FAN_CLOSE_WRITE; tst_count++; /* Read events to verify previous access was properly generated */ ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, EVENT_BUF_LEN - len); len += ret; /* * Now ignore open & close events regardless of file * modifications */ if (myfanotify_mark(fd_notify, FAN_MARK_ADD | FAN_MARK_IGNORED_MASK | FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN | FAN_CLOSE, AT_FDCWD, fname) < 0) { tst_brkm(TBROK | TERRNO, cleanup, "fanotify_mark (%d, FAN_MARK_ADD | " "FAN_MARK_IGNORED_MASK | " "FAN_MARK_IGNORED_SURV_MODIFY, FAN_OPEN | " "FAN_CLOSE, AT_FDCWD, %s) failed", fd_notify, fname); } /* This event should be ignored */ fd = SAFE_OPEN(cleanup, fname, O_RDWR); SAFE_WRITE(cleanup, 1, fd, fname, 1); event_set[tst_count] = FAN_MODIFY; tst_count++; /* This event should be still ignored */ SAFE_CLOSE(cleanup, fd); /* This event should still be ignored */ fd = SAFE_OPEN(cleanup, fname, O_RDWR); /* Read events to verify open & close were ignored */ ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, EVENT_BUF_LEN - len); len += ret; /* Now remove open and close from ignored mask */ if (myfanotify_mark(fd_notify, FAN_MARK_REMOVE | FAN_MARK_IGNORED_MASK, FAN_OPEN | FAN_CLOSE, AT_FDCWD, fname) < 0) { tst_brkm(TBROK | TERRNO, cleanup, "fanotify_mark (%d, FAN_MARK_REMOVE | " "FAN_MARK_IGNORED_MASK, FAN_OPEN | " "FAN_CLOSE, AT_FDCWD, %s) failed", fd_notify, fname); } SAFE_CLOSE(cleanup, fd); event_set[tst_count] = FAN_CLOSE_WRITE; tst_count++; /* Read events to verify close was generated */ ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len, EVENT_BUF_LEN - len); len += ret; if (TST_TOTAL != tst_count) { tst_brkm(TBROK, cleanup, "TST_TOTAL (%d) and tst_count (%d) are not " "equal", TST_TOTAL, tst_count); } tst_count = 0; /* * check events */ while (i < len) { struct fanotify_event_metadata *event; event = (struct fanotify_event_metadata *)&event_buf[i]; if (test_num >= TST_TOTAL) { tst_resm(TFAIL, "get unnecessary event: mask=%llx " "pid=%u fd=%u", (unsigned long long)event->mask, (unsigned)event->pid, event->fd); } else if (!(event->mask & event_set[test_num])) { tst_resm(TFAIL, "get event: mask=%llx (expected %llx) " "pid=%u fd=%u", (unsigned long long)event->mask, event_set[test_num], (unsigned)event->pid, event->fd); } else if (event->pid != getpid()) { tst_resm(TFAIL, "get event: mask=%llx pid=%u " "(expected %u) fd=%u", (unsigned long long)event->mask, (unsigned)event->pid, (unsigned)getpid(), event->fd); } else { if (event->fd == -2) goto pass; ret = read(event->fd, buf, BUF_SIZE); if (ret != strlen(fname)) { tst_resm(TFAIL, "cannot read from returned fd " "of event: mask=%llx pid=%u " "fd=%u ret=%d (errno=%d)", (unsigned long long)event->mask, (unsigned)event->pid, event->fd, ret, errno); } else if (memcmp(buf, fname, strlen(fname))) { tst_resm(TFAIL, "wrong data read from returned fd " "of event: mask=%llx pid=%u " "fd=%u", (unsigned long long)event->mask, (unsigned)event->pid, event->fd); } else { pass: tst_resm(TPASS, "get event: mask=%llx pid=%u fd=%u", (unsigned long long)event->mask, (unsigned)event->pid, event->fd); } } /* * We have verified the data now so close fd and * invalidate it so that we don't check it again * unnecessarily */ close(event->fd); event->fd = -2; event->mask &= ~event_set[test_num]; /* No events left in current mask? Go for next event */ if (event->mask == 0) { i += event->event_len; } test_num++; } for (; test_num < TST_TOTAL; test_num++) { tst_resm(TFAIL, "didn't get event: mask=%llx", event_set[test_num]); } /* Remove mark to clear FAN_MARK_IGNORED_SURV_MODIFY */ if (myfanotify_mark(fd_notify, FAN_MARK_REMOVE, FAN_ACCESS | FAN_MODIFY | FAN_CLOSE | FAN_OPEN, AT_FDCWD, fname) < 0) { tst_brkm(TBROK | TERRNO, cleanup, "fanotify_mark (%d, FAN_MARK_REMOVE, FAN_ACCESS | " "FAN_MODIFY | FAN_CLOSE | FAN_OPEN, AT_FDCWD, %s) " "failed", fd_notify, fname); } } cleanup(); tst_exit(); }
/* read unsigned int 16 little endian */ static uint16_t readu16le( void ) { uint8_t x[ 2 ]; SAFE_READ( x, 2 ); return x[0] | ( x[1] << 8 ); }
bool PELoader::load(std::istream &executable, Memory *mem, addr_t &addr) { std::streambuf *in = executable.rdbuf(); const std::istream::pos_type fail_pos(std::istream::off_type(-1)); // get to the position where offset to PE signature is stored if (in->pubseekpos(PESigOffPos) == fail_pos) { // seek failed return false; } uint32_t pe_sig_off; // FIXME BE this will not work on Big Endian // PE specification does not specify whether data at 0x3c is in BE or LE // (it does not even specify how many bytes it spans) SAFE_READ(in, &pe_sig_off, Sz_PESigOff); // get to the position where PE signature itself is stored if (in->pubseekpos(pe_sig_off) == fail_pos) { // seek failed return false; } // read the PE signature char pe_sig[Sz_PESig]; SAFE_READ(in, &pe_sig, Sz_PESig); // compare required PE signature with the one stored in file if (!std::equal(pe_sig, pe_sig + Sz_PESig, PE_Signature)) { // PE signature in file is incorrect return false; } // immediately after PE signature, there come to COFF File Header // we read what we need from it // read machine type - again PE spec does not tell whether information // in the file is stored in Little or Big endian (it tells only for 2 values // at all --> assuming Little endian unless specified otherwise) uint16_t machine_type; SAFE_READ(in, &machine_type, Sz_MachineType); // make sure this executable is for ARM //if (machine_type != IMAGE_FILE_MACHINE_ARM_) { // // special case: if it is for Thumb, warn the user on stderr that they // // cannot use Thumb instruction set // if (machine_type == IMAGE_FILE_MACHINE_THUMB_) { // std::cerr << "This implementation of ARM does not support Thumb instruction set." << std::endl; // std::cerr << "Please recompile your program to use the ARM instruction set." << std::endl; // } // return false; //} // read number of sections uint16_t n_secs; SAFE_READ(in, &n_secs, Sz_NumberOfSections); SAFE_SEEK_FWD(in, 12); // read size of optional header (which is compulsory for images) uint16_t sz_hdr_opt; SAFE_READ(in, &sz_hdr_opt, Sz_SizeOfOptionalHeader); // read characteristics uint16_t charac; SAFE_READ(in, &charac, Sz_Characteristics); // check this is PE file is an executable image if (!(charac & IMAGE_FILE_EXECUTABLE_IMAGE_)) { return false; } // check this is for 32-bit word machine (which the ARMv5TE we implement is) if (!(charac & IMAGE_FILE_32BIT_MACHINE_)) { return false; } // check optional header is big enough to contain its magic if (sz_hdr_opt < Sz_PEMagic) return false; // read magic uint16_t pe_magic; SAFE_READ(in, &pe_magic, 2); // make sure this is PE32; otherwise (PE32+) if (pe_magic != PE_Magic_32) return false; // now make sure the first 2 sections (i.e. the fixed-size ones) are in // optional header if (sz_hdr_opt < Sz_OptHdr_Std + Sz_OptHdr_Win) return false; // skip major and minor linker version SAFE_SEEK_FWD(in, 2); // read size of code uint32_t sz_code; SAFE_READ(in, &sz_code, 4); // skip SizeOfInitializedData and SizeOfUninitializedData SAFE_SEEK_FWD(in, 8); // read entry point's relative virtual address (RVA) addr_t ep_rva; SAFE_READ(in, &ep_rva, 4); // read base of code RVA addr_t codebase_rva; SAFE_READ(in, &codebase_rva, 4); // skip BaseOfData SAFE_SEEK_FWD(in, 4); // read image base addr_t img_base; SAFE_READ(in, &img_base, 4); // skip section alignment (we don't need it since we will honor the image base) // skip until NumberOfRvaAndSizes SAFE_SEEK_FWD(in, 60); // read NumberOfRvaAndSizes uint32_t n_dirent; SAFE_READ(in, &n_dirent, 4); // consistency check - real size of optional header must be less or equal // to the declared size of optional header std::size_t sz_opthdr_dirents = sz_hdr_opt - Sz_OptHdr_Std - Sz_OptHdr_Win; if (sz_opthdr_dirents < 8 * n_dirent) return false; // skip the rest of optional header SAFE_SEEK_FWD(in, sz_opthdr_dirents); // read all section entries in section table and load them while (n_secs-- > 0) if (!sect_entry(in).load(executable, *mem, img_base)) return false; addr = img_base + ep_rva; return true; }
myxml_node_t* myxml_loadTreeHelper(FILE *instream, myxml_node_t *parent_node) { uint32_t temp; int i=0; myxml_node_t *curr_node = (myxml_node_t*) gasneti_malloc(sizeof(myxml_node_t)); curr_node->parent = parent_node; if(parent_node == NULL) { curr_node->nodeclass = MYXML_ROOT_NODE; } else { curr_node->nodeclass = MYXML_INTER_NODE; } SAFE_READ(&temp, sizeof(uint32_t), instream); curr_node->id = MYNTOHL(temp); SAFE_READ(&temp, sizeof(uint32_t), instream); curr_node->num_children = MYNTOHL(temp); SAFE_READ(&temp, sizeof(uint32_t), instream); curr_node->num_attributes = MYNTOHL(temp); /*read the tag length and allocate the buffer*/ SAFE_READ(&temp, sizeof(uint32_t), instream); temp = MYNTOHL(temp); curr_node->tag = (char*) gasneti_malloc(temp); /*read the tag*/ SAFE_READ(curr_node->tag, temp, instream); curr_node->attribute_list = (myxml_attribute_t*) gasneti_malloc(sizeof(myxml_attribute_t)*curr_node->num_attributes); for(i=0; i<curr_node->num_attributes; i++) { /*read the length of the string*/ SAFE_READ(&temp, sizeof(uint32_t), instream); temp = MYNTOHL(temp); curr_node->attribute_list[i].attribute_name = gasneti_malloc(temp); SAFE_READ(curr_node->attribute_list[i].attribute_name, temp, instream); SAFE_READ(&temp, sizeof(uint32_t), instream); temp = MYNTOHL(temp); curr_node->attribute_list[i].attribute_value = gasneti_malloc(temp); SAFE_READ(curr_node->attribute_list[i].attribute_value, temp, instream); } /*read the size of the value str*/ SAFE_READ(&temp, sizeof(uint32_t), instream); temp = MYNTOHL(temp); if(temp > 0) { curr_node->value = (char*) gasneti_malloc(temp); SAFE_READ(curr_node->value, temp, instream); curr_node->nodeclass = MYXML_LEAF_NODE; } curr_node->children = (myxml_node_t**) gasneti_malloc(sizeof(myxml_node_t*)*curr_node->num_children); for(i=0; i<curr_node->num_children; i++) { curr_node->children[i] = myxml_loadTreeHelper(instream, curr_node); } return curr_node; }