From: Nicholas Nethercote Date: Tue, 9 Nov 2004 16:20:46 +0000 (+0000) Subject: - Convert open() to the new syscall regime. X-Git-Tag: svn/VALGRIND_3_0_0~1359 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=fff14836006f25fb1f4cfc0fd093118eb68bf84d;p=thirdparty%2Fvalgrind.git - Convert open() to the new syscall regime. - Improve scalar regtest; I will add new syscalls to it as I convert them. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@2953 --- diff --git a/coregrind/vg_syscalls.c b/coregrind/vg_syscalls.c index 120571845e..bd7888c24d 100644 --- a/coregrind/vg_syscalls.c +++ b/coregrind/vg_syscalls.c @@ -44,21 +44,58 @@ #define X(syscallname, argname) syscallname"("#argname")" +#define PRRSN \ + SK_(pre_reg_read)(Vg_CoreSysCall, tid, "(syscallno)", \ + R_SYSCALL_NUM, sizeof(UWord)); + +#define PRRAn(n,s,a,t) \ + SK_(pre_reg_read)(Vg_CoreSysCall, tid, X(s,a), \ + R_SYSCALL_ARG##n, sizeof(t)); + +#define PRE_REG_READ0(tr, s) \ + if (VG_(defined_pre_reg_read)()) { \ + PRRSN; \ + } + +#define PRE_REG_READ1(tr, s, t1, a1) \ + if (VG_(defined_pre_reg_read)()) { \ + PRRSN; \ + PRRAn(1,s,); \ + } + +#define PRE_REG_READ2(tr, s, t1, a1, t2, a2) \ + if (VG_(defined_pre_reg_read)()) { \ + PRRSN; \ + PRRAn(1,s,a1,t1); PRRAn(2,s,a2,t2); \ + } + #define PRE_REG_READ3(tr, s, t1, a1, t2, a2, t3, a3) \ - do { \ - VG_TRACK( pre_reg_read, Vg_CoreSysCall, tid, "(syscallno)", R_SYSCALL_NUM, sizeof(t1)); \ - VG_TRACK( pre_reg_read, Vg_CoreSysCall, tid, X(s,a1), R_SYSCALL_ARG1, sizeof(t1)); \ - VG_TRACK( pre_reg_read, Vg_CoreSysCall, tid, X(s,a2), R_SYSCALL_ARG2, sizeof(t2)); \ - VG_TRACK( pre_reg_read, Vg_CoreSysCall, tid, X(s,a3), R_SYSCALL_ARG3, sizeof(t3)); \ - } while (0); - -#if 0 - do { \ - if (VG_(defined_)()) { \ - SK_(pre_reg_read)(args); \ - } \ - } while(0) -#endif + if (VG_(defined_pre_reg_read)()) { \ + PRRSN; \ + PRRAn(1,s,a1,t1); PRRAn(2,s,a2,t2); PRRAn(3,s,a3,t3); \ + } + +#define PRE_REG_READ4(tr, s, t1, a1, t2, a2, t3, a3, t4, a4) \ + if (VG_(defined_pre_reg_read)()) { \ + PRRSN; \ + PRRAn(1,s,a1,t1); PRRAn(2,s,a2,t2); PRRAn(3,s,a3,t3); \ + PRRAn(4,s,a4,t4); \ + } + +#define PRE_REG_READ5(tr, s, t1, a1, t2, a2, t3, a3, t4, a4, t5, a5) \ + if (VG_(defined_pre_reg_read)()) { \ + PRRSN; \ + PRRAn(1,s,a1,t1); PRRAn(2,s,a2,t2); PRRAn(3,s,a3,t3); \ + PRRAn(4,s,a4,t4); PRRAn(5,s,a5,t5); \ + } + +#define PRE_REG_READ6(tr, s, t1, a1, t2, a2, t3, a3, t4, a4, t5, a5, t6, a6) \ + if (VG_(defined_pre_reg_read)()) { \ + PRRSN; \ + PRRAn(1,s,a1,t1); PRRAn(2,s,a2,t2); PRRAn(3,s,a3,t3); \ + PRRAn(4,s,a4,t4); PRRAn(5,s,a5,t5); PRRAn(6,s,a6,t6); \ + } + #define PRE_MEM_READ(zzname, zzaddr, zzlen) \ VG_TRACK( pre_mem_read, Vg_CoreSysCall, tid, zzname, zzaddr, zzlen) @@ -4093,20 +4130,23 @@ PRE(_newselect) PRE_MEM_READ( "newselect(timeout)", arg5, sizeof(struct vki_timeval) ); } -PRE(open) +PRE(sys_open) { - /* int open(const char *pathname, int flags, mode_t mode); */ if (arg2 & VKI_O_CREAT) { - /* int open(const char *pathname, int flags, mode_t mode); */ - PRINT("open ( %p(%s), %d, %d )",arg1,arg1,arg2,arg3); + // 3-arg version + PRINT("sys_open ( %p(%s), %d, %d )",arg1,arg1,arg2,arg3); + PRE_REG_READ3(long, "open", + const char __user *, filename, int, flags, int, mode); } else { - /* int open(const char *pathname, int flags); */ - PRINT("open ( %p(%s), %d )",arg1,arg1,arg2); + // 2-arg version + PRINT("sys_open ( %p(%s), %d )",arg1,arg1,arg2); + PRE_REG_READ2(long, "open", + const char __user *, filename, int, flags); } - PRE_MEM_RASCIIZ( "open(pathname)", arg1 ); + PRE_MEM_RASCIIZ( "open(filename)", arg1 ); } -POST(open) +POST(sys_open) { if (!fd_allowed(res, "open", tid, True)) { VG_(close)(res); @@ -5879,9 +5919,7 @@ static const struct sys_info sys_info[] = { SYSBA(mincore, 0), SYSBA(nanosleep, MayBlock|PostOnFail), SYSB_(_newselect, MayBlock), - SYSBA(open, MayBlock), -// SYSBA(read, MayBlock), -// SYSB_(write, MayBlock), + SYSXY(__NR_open, sys_open, MayBlock), SYSXY(__NR_read, sys_read, MayBlock), SYSX_(__NR_write, sys_write, MayBlock), SYSBA(creat, MayBlock), diff --git a/memcheck/tests/Makefile.am b/memcheck/tests/Makefile.am index bc91fdd445..3d99c36b96 100644 --- a/memcheck/tests/Makefile.am +++ b/memcheck/tests/Makefile.am @@ -51,8 +51,8 @@ EXTRA_DIST = $(noinst_SCRIPTS) \ realloc1.stderr.exp realloc1.vgtest \ realloc2.stderr.exp realloc2.vgtest \ realloc3.stderr.exp realloc3.vgtest \ - scalar.stderr.exp scalar.vgtest \ - scalar_supp.stderr.exp scalar_supp.vgtest scalar.supp \ + scalar.stderr.exp scalar.vgtest scalar.supp \ + scalar_supp.stderr.exp scalar_supp.vgtest scalar_supp.c \ sigaltstack.stderr.exp sigaltstack.vgtest \ signal2.stderr.exp \ signal2.stdout.exp signal2.vgtest \ @@ -81,7 +81,7 @@ check_PROGRAMS = \ memalign_test memalign2 memcmptest mempool mmaptest \ nanoleak new_nothrow \ null_socket overlap \ - realloc1 realloc2 realloc3 scalar sigaltstack signal2 \ + realloc1 realloc2 realloc3 scalar scalar_supp sigaltstack signal2 \ str_tester supp1 supp2 suppfree \ trivialleak weirdioctl \ mismatches new_override metadata threadederrno \ @@ -134,6 +134,7 @@ realloc1_SOURCES = realloc1.c realloc2_SOURCES = realloc2.c realloc3_SOURCES = realloc3.c scalar_SOURCES = scalar.c +scalar_supp_SOURCES = scalar_supp.c signal2_SOURCES = signal2.c supp1_SOURCES = supp.c supp2_SOURCES = supp.c diff --git a/memcheck/tests/scalar.c b/memcheck/tests/scalar.c index 440f382eb6..0a795e28a7 100644 --- a/memcheck/tests/scalar.c +++ b/memcheck/tests/scalar.c @@ -1,22 +1,336 @@ +#include +#include +#include #include #include +#include #include +#include + +// Thorough syscall scalar arg checking. Also serves as thorough checking +// for (very) basic syscall use. Generally not trying to do anything +// meaningful with the syscalls. + +void GO(char* s) { + fprintf(stderr, "---------- %s ----------\n", s); +} int main(void) { // uninitialised, but we know pi[0] is 0x0 int* pi = malloc(sizeof(int)); + int i0 = pi[0]; // uninitialised, but we know pc[0] points to 0x0 - char** pc = malloc(sizeof(char*)); + char** pc = malloc(sizeof(char*)); + char* s0 = pc[0]; + + // All __NR_xxx numbers are taken from x86 - // Five errors: - // - the syscall number itself is undefined (but we know it's - // 0 + __NR_write :) - // - each of the scalar args are undefined - // - the 2nd arg points to unaddressable memory. - syscall(pi[0]+__NR_write, pi[0], pc[0], pi[0]+1); - - return 0; + // __NR_exit 1 + // (see below) + + // __NR_fork 2 + + // __NR_read 3 --> sys_read() + // Nb: here we are also getting an error from the syscall arg itself. + GO("__NR_read, 1+3 scalar errors"); + syscall(i0+__NR_read, i0, s0, i0); + + // __NR_write 4 --> sys_write() + GO("__NR_write, 3 scalar errors, 1 memory error"); + syscall(__NR_write, i0, s0, i0+1); + + // __NR_open 5 + GO("__NR_open(2), 2 scalar errors, 1 memory error"); + syscall(__NR_open, s0, i0, i0+1); + + GO("__NR_open(3), 1 scalar error"); + syscall(__NR_open, "tmp_write_file_foo", O_CREAT, i0); + + // __NR_close 6 + // __NR_waitpid 7 + // __NR_creat 8 + // __NR_link 9 + // __NR_unlink 10 + // __NR_execve 11 + // __NR_chdir 12 + // __NR_time 13 + // __NR_mknod 14 + // __NR_chmod 15 + // __NR_lchown 16 + // __NR_break 17 + // __NR_oldstat 18 + // __NR_lseek 19 + // __NR_getpid 20 + // __NR_mount 21 + // __NR_umount 22 + // __NR_setuid 23 + // __NR_getuid 24 + // __NR_stime 25 + // __NR_ptrace 26 + // __NR_alarm 27 + // __NR_oldfstat 28 + // __NR_pause 29 + // __NR_utime 30 + // __NR_stty 31 + // __NR_gtty 32 + // __NR_access 33 + // __NR_nice 34 + // __NR_ftime 35 + // __NR_sync 36 + // __NR_kill 37 + // __NR_rename 38 + // __NR_mkdir 39 + // __NR_rmdir 40 + // __NR_dup 41 + // __NR_pipe 42 + // __NR_times 43 + // __NR_prof 44 + // __NR_brk 45 + // __NR_setgid 46 + // __NR_getgid 47 + // __NR_signal 48 + // __NR_geteuid 49 + // __NR_getegid 50 + // __NR_acct 51 + // __NR_umount2 52 + // __NR_lock 53 + // __NR_ioctl 54 + // __NR_fcntl 55 + // __NR_mpx 56 + // __NR_setpgid 57 + // __NR_ulimit 58 + // __NR_oldolduname 59 + // __NR_umask 60 + // __NR_chroot 61 + // __NR_ustat 62 + // __NR_dup2 63 + // __NR_getppid 64 + // __NR_getpgrp 65 + // __NR_setsid 66 + // __NR_sigaction 67 + // __NR_sgetmask 68 + // __NR_ssetmask 69 + // __NR_setreuid 70 + // __NR_setregid 71 + // __NR_sigsuspend 72 + // __NR_sigpending 73 + // __NR_sethostname 74 + // __NR_setrlimit 75 + // __NR_getrlimit 76 /* Back compatible 2Gig limited rlimit */ + // __NR_getrusage 77 + // __NR_gettimeofday 78 + // __NR_settimeofday 79 + // __NR_getgroups 80 + // __NR_setgroups 81 + // __NR_select 82 + // __NR_symlink 83 + // __NR_oldlstat 84 + // __NR_readlink 85 + // __NR_uselib 86 + // __NR_swapon 87 + // __NR_reboot 88 + // __NR_readdir 89 + // __NR_mmap 90 + // __NR_munmap 91 + // __NR_truncate 92 + // __NR_ftruncate 93 + // __NR_fchmod 94 + // __NR_fchown 95 + // __NR_getpriority 96 + // __NR_setpriority 97 + // __NR_profil 98 + // __NR_statfs 99 + // __NR_fstatfs 100 + // __NR_ioperm 101 + // __NR_socketcall 102 + // __NR_syslog 103 + // __NR_setitimer 104 + // __NR_getitimer 105 + // __NR_stat 106 + // __NR_lstat 107 + // __NR_fstat 108 + // __NR_olduname 109 + // __NR_iopl 110 + // __NR_vhangup 111 + // __NR_idle 112 + // __NR_vm86old 113 + // __NR_wait4 114 + // __NR_swapoff 115 + // __NR_sysinfo 116 + // __NR_ipc 117 + // __NR_fsync 118 + // __NR_sigreturn 119 + // __NR_clone 120 + // __NR_setdomainname 121 + // __NR_uname 122 + // __NR_modify_ldt 123 + // __NR_adjtimex 124 + // __NR_mprotect 125 + // __NR_sigprocmask 126 + // __NR_create_module 127 + // __NR_init_module 128 + // __NR_delete_module 129 + // __NR_get_kernel_syms 130 + // __NR_quotactl 131 + // __NR_getpgid 132 + // __NR_fchdir 133 + // __NR_bdflush 134 + // __NR_sysfs 135 + // __NR_personality 136 + // __NR_afs_syscall 137 /* Syscall for Andrew File System */ + // __NR_setfsuid 138 + // __NR_setfsgid 139 + // __NR__llseek 140 + // __NR_getdents 141 + // __NR__newselect 142 + // __NR_flock 143 + // __NR_msync 144 + // __NR_readv 145 + // __NR_writev 146 + // __NR_getsid 147 + // __NR_fdatasync 148 + // __NR__sysctl 149 + // __NR_mlock 150 + // __NR_munlock 151 + // __NR_mlockall 152 + // __NR_munlockall 153 + // __NR_sched_setparam 154 + // __NR_sched_getparam 155 + // __NR_sched_setscheduler 156 + // __NR_sched_getscheduler 157 + // __NR_sched_yield 158 + // __NR_sched_get_priority_max 159 + // __NR_sched_get_priority_min 160 + // __NR_sched_rr_get_interval 161 + // __NR_nanosleep 162 + // __NR_mremap 163 + // __NR_setresuid 164 + // __NR_getresuid 165 + // __NR_vm86 166 + // __NR_query_module 167 + // __NR_poll 168 + // __NR_nfsservctl 169 + // __NR_setresgid 170 + // __NR_getresgid 171 + // __NR_prctl 172 + // __NR_rt_sigreturn 173 + // __NR_rt_sigaction 174 + // __NR_rt_sigprocmask 175 + // __NR_rt_sigpending 176 + // __NR_rt_sigtimedwait 177 + // __NR_rt_sigqueueinfo 178 + // __NR_rt_sigsuspend 179 + // __NR_pread64 180 + // __NR_pwrite64 181 + // __NR_chown 182 + // __NR_getcwd 183 + // __NR_capget 184 + // __NR_capset 185 + // __NR_sigaltstack 186 + // __NR_sendfile 187 + // __NR_getpmsg 188 /* some people actually want streams */ + // __NR_putpmsg 189 /* some people actually want streams */ + // __NR_vfork 190 + // __NR_ugetrlimit 191 /* SuS compliant getrlimit */ + // __NR_mmap2 192 + // __NR_truncate64 193 + // __NR_ftruncate64 194 + // __NR_stat64 195 + // __NR_lstat64 196 + // __NR_fstat64 197 + // __NR_lchown32 198 + // __NR_getuid32 199 + // __NR_getgid32 200 + // __NR_geteuid32 201 + // __NR_getegid32 202 + // __NR_setreuid32 203 + // __NR_setregid32 204 + // __NR_getgroups32 205 + // __NR_setgroups32 206 + // __NR_fchown32 207 + // __NR_setresuid32 208 + // __NR_getresuid32 209 + // __NR_setresgid32 210 + // __NR_getresgid32 211 + // __NR_chown32 212 + // __NR_setuid32 213 + // __NR_setgid32 214 + // __NR_setfsuid32 215 + // __NR_setfsgid32 216 + // __NR_pivot_root 217 + // __NR_mincore 218 + // __NR_madvise 219 + // __NR_madvise1 219 /* delete when C lib stub is removed */ + // __NR_getdents64 220 + // __NR_fcntl64 221 +/* 223 is unused */ + // __NR_gettid 224 + // __NR_readahead 225 + // __NR_setxattr 226 + // __NR_lsetxattr 227 + // __NR_fsetxattr 228 + // __NR_getxattr 229 + // __NR_lgetxattr 230 + // __NR_fgetxattr 231 + // __NR_listxattr 232 + // __NR_llistxattr 233 + // __NR_flistxattr 234 + // __NR_removexattr 235 + // __NR_lremovexattr 236 + // __NR_fremovexattr 237 + // __NR_tkill 238 + // __NR_sendfile64 239 + // __NR_futex 240 + // __NR_sched_setaffinity 241 + // __NR_sched_getaffinity 242 + // __NR_set_thread_area 243 + // __NR_get_thread_area 244 + // __NR_io_setup 245 + // __NR_io_destroy 246 + // __NR_io_getevents 247 + // __NR_io_submit 248 + // __NR_io_cancel 249 + // __NR_fadvise64 250 + + // __NR_exit_group 252 + // __NR_lookup_dcookie 253 + // __NR_epoll_create 254 + // __NR_epoll_ctl 255 + // __NR_epoll_wait 256 + // __NR_remap_file_pages 257 + // __NR_set_tid_address 258 + // __NR_timer_create 259 + // __NR_timer_settime (__NR_timer_create+1) + // __NR_timer_gettime (__NR_timer_create+2) + // __NR_timer_getoverrun (__NR_timer_create+3) + // __NR_timer_delete (__NR_timer_create+4) + // __NR_clock_settime (__NR_timer_create+5) + // __NR_clock_gettime (__NR_timer_create+6) + // __NR_clock_getres (__NR_timer_create+7) + // __NR_clock_nanosleep (__NR_timer_create+8) + // __NR_statfs64 268 + // __NR_fstatfs64 269 + // __NR_tgkill 270 + // __NR_utimes 271 + // __NR_fadvise64_64 272 + // __NR_vserver 273 + // __NR_mbind 274 + // __NR_get_mempolicy 275 + // __NR_set_mempolicy 276 + // __NR_mq_open 277 + // __NR_mq_unlink (__NR_mq_open+1) + // __NR_mq_timedsend (__NR_mq_open+2) + // __NR_mq_timedreceive (__NR_mq_open+3) + // __NR_mq_notify (__NR_mq_open+4) + // __NR_mq_getsetattr (__NR_mq_open+5) + // __NR_sys_kexec_load 283 + + // __NR_exit 1 --> sys_exit() + GO("__NR_exit, 1 scalar error"); + syscall(__NR_exit, i0); + + assert(0); } diff --git a/memcheck/tests/scalar.stderr.exp b/memcheck/tests/scalar.stderr.exp index b5ea4c8781..8824d195fb 100644 --- a/memcheck/tests/scalar.stderr.exp +++ b/memcheck/tests/scalar.stderr.exp @@ -1,8 +1,25 @@ +---------- __NR_read, 1+3 scalar errors ---------- Syscall param (syscallno) contains uninitialised byte(s) at 0x........: syscall (in /...libc...) by 0x........: __libc_start_main (...libc...) by 0x........: ... +Syscall param read(fd) contains uninitialised byte(s) + at 0x........: syscall (in /...libc...) + by 0x........: __libc_start_main (...libc...) + by 0x........: ... + +Syscall param read(buf) contains uninitialised byte(s) + at 0x........: syscall (in /...libc...) + by 0x........: __libc_start_main (...libc...) + by 0x........: ... + +Syscall param read(count) contains uninitialised byte(s) + at 0x........: syscall (in /...libc...) + by 0x........: __libc_start_main (...libc...) + by 0x........: ... +---------- __NR_write, 3 scalar errors, 1 memory error ---------- + Syscall param write(fd) contains uninitialised byte(s) at 0x........: syscall (in /...libc...) by 0x........: __libc_start_main (...libc...) @@ -23,3 +40,32 @@ Syscall param write(buf) points to unaddressable byte(s) by 0x........: __libc_start_main (...libc...) by 0x........: ... Address 0x........ is not stack'd, malloc'd or (recently) free'd +---------- __NR_open(2), 2 scalar errors, 1 memory error ---------- + +Syscall param open(filename) contains uninitialised byte(s) + at 0x........: syscall (in /...libc...) + by 0x........: __libc_start_main (...libc...) + by 0x........: ... + +Syscall param open(flags) contains uninitialised byte(s) + at 0x........: syscall (in /...libc...) + by 0x........: __libc_start_main (...libc...) + by 0x........: ... + +Syscall param open(filename) points to unaddressable byte(s) + at 0x........: syscall (in /...libc...) + by 0x........: __libc_start_main (...libc...) + by 0x........: ... + Address 0x........ is not stack'd, malloc'd or (recently) free'd +---------- __NR_open(3), 1 scalar error ---------- + +Syscall param open(mode) contains uninitialised byte(s) + at 0x........: syscall (in /...libc...) + by 0x........: __libc_start_main (...libc...) + by 0x........: ... +---------- __NR_exit, 1 scalar error ---------- + +Syscall param exit(error_code) contains uninitialised byte(s) + at 0x........: syscall (in /...libc...) + by 0x........: __libc_start_main (...libc...) + by 0x........: ... diff --git a/memcheck/tests/scalar.vgtest b/memcheck/tests/scalar.vgtest index c1e0088ee2..964ef598ee 100644 --- a/memcheck/tests/scalar.vgtest +++ b/memcheck/tests/scalar.vgtest @@ -1,2 +1,3 @@ prog: scalar vgopts: -q +delete: tmp_write_file_foo diff --git a/memcheck/tests/scalar_supp.c b/memcheck/tests/scalar_supp.c new file mode 100644 index 0000000000..440f382eb6 --- /dev/null +++ b/memcheck/tests/scalar_supp.c @@ -0,0 +1,22 @@ +#include +#include +#include + +int main(void) +{ + // uninitialised, but we know pi[0] is 0x0 + int* pi = malloc(sizeof(int)); + + // uninitialised, but we know pc[0] points to 0x0 + char** pc = malloc(sizeof(char*)); + + // Five errors: + // - the syscall number itself is undefined (but we know it's + // 0 + __NR_write :) + // - each of the scalar args are undefined + // - the 2nd arg points to unaddressable memory. + syscall(pi[0]+__NR_write, pi[0], pc[0], pi[0]+1); + + return 0; +} + diff --git a/memcheck/tests/scalar_supp.vgtest b/memcheck/tests/scalar_supp.vgtest index d3d85a75d9..c0602756e8 100644 --- a/memcheck/tests/scalar_supp.vgtest +++ b/memcheck/tests/scalar_supp.vgtest @@ -1,2 +1,2 @@ -prog: scalar +prog: scalar_supp vgopts: -q --suppressions=scalar.supp