ftmemsim-valgrind/coregrind/m_syswrap/priv_syswrap-generic.h
Philippe Waroquiers 51c6c85e22 The semantic of the stack bounds is not consistent or is not described.
At various places, there were either some assumption that the 'end'
boundary (highest address) was either not included, included,
or was the highest addressable word, or the highest addressable byte.
This e.g. was very visible when doing:
  ./vg-in-place -d -d ./helgrind/tests/tc01_simple_race|&grep regi
giving
  --24040:2:stacks     register 0xBEDB4000-0xBEDB4FFF as stack 0
  --24040:2:stacks     register 0x402C000-0x4A2C000 as stack 1
showing that the main stack end was (on x86) not the highest word
but the highest byte, while for the thread 1, the registered end
was a byte not part of the stack.

The attached patch ensures that stack bounds semantic are documented and
consistent. Also, some of the stack handling code is factorised.

The convention that the patch ensures and documents is:
start is the lowest addressable byte, end is the highest addressable byte.
(the words 'min' and 'max' have been kept when already used, as this wording is 
consistent with the new semantic of start/end).

In various debug log, used brackets [ and ] to make clear that
both bounds are included.

The code to guess and register the client stack was duplicated
in all the platform specific syswrap-<plat>-<os>.c files.
Code has been factorised in syswrap-generic.c

The patch has been regression tested on
   x86, amd64, ppc32/64, s390x.
It has been compiled and one test run on arm64.
Not compiled/not tested on darwin, android, mips32/64, arm


More in details, the patch does the following:

coregrind/pub_core_aspacemgr.h
include/valgrind.h
include/pub_tool_machine.h
coregrind/pub_core_scheduler.h
coregrind/pub_core_stacks.h
  - document start/end semantic in various functions
 also in pub_tool_machine.h:
  - replaces unclear 'bottommost address' by 'lowest address'
    (unclear as stack bottom is or at least can be interpreted as
     the 'functional' bottom of the stack, which is the highest
      address for 'stack growing downwards').
coregrind/pub_core_initimg.h
  replace unclear clstack_top by clstack_end
coregrind/m_main.c
  updated to clstack_end

coregrind/pub_core_threadstate.h
  renamed client_stack_highest_word to client_stack_highest_byte
coregrind/m_scheduler/scheduler.c
  computes client_stack_highest_byte as the highest addressable byte
  Update comments in call to VG_(show_sched_status)
coregrind/m_machine.c
coregrind/m_stacktrace.c
  updated to client_stack_highest_byte, and switched 
    stack_lowest/highest_word to stack_lowest/highest_byte accordingly

coregrind/m_stacks.c
  clarify semantic of start/end,
  added a comment to indicate why we invert start/end in register call
  (note that the code find_stack_by_addr was already assuming that
  end was included as the checks were doing e.g.
    sp >= i->start && sp <= i->end

coregrind/pub_core_clientstate.h
coregrind/m_clientstate.c
  renames Addr  VG_(clstk_base) to Addr  VG_(clstk_start_base)
    (start to indicate it is the lowest address, base suffix kept
     to indicate it is the initial lowest address).

coregrind/m_initimg/initimg-darwin.c
   updated to  VG_(clstk_start_base)
   replace unclear iicii.clstack_top by iicii.clstack_end
   updated clstack_max_size computation according to both bounds included.

coregrind/m_initimg/initimg-linux.c
   updated to  VG_(clstk_start_base)
   updated VG_(clstk_end) computation according to both bounds included.
   replace unclear iicii.clstack_top by iicii.clstack_end

coregrind/pub_core_aspacemgr.h
  extern Addr VG_(am_startup) : clarify semantic of the returned value
coregrind/m_aspacemgr/aspacemgr-linux.c
   removed a copy of a comment that was already in pub_core_aspacemgr.h
     (avoid double maintenance)
   renamed unclear suggested_clstack_top to suggested_clstack_end
    (note that here, it looks like suggested_clstack_top was already
     the last addressable byte)

* factorisation of the stack guessing and registration causes
  mechanical changes in the following files:
      coregrind/m_syswrap/syswrap-ppc64-linux.c
      coregrind/m_syswrap/syswrap-x86-darwin.c
      coregrind/m_syswrap/syswrap-amd64-linux.c
      coregrind/m_syswrap/syswrap-arm-linux.c
      coregrind/m_syswrap/syswrap-generic.c
      coregrind/m_syswrap/syswrap-mips64-linux.c
      coregrind/m_syswrap/syswrap-ppc32-linux.c
      coregrind/m_syswrap/syswrap-amd64-darwin.c
      coregrind/m_syswrap/syswrap-mips32-linux.c
      coregrind/m_syswrap/priv_syswrap-generic.h
      coregrind/m_syswrap/syswrap-x86-linux.c
      coregrind/m_syswrap/syswrap-s390x-linux.c
      coregrind/m_syswrap/syswrap-darwin.c
      coregrind/m_syswrap/syswrap-arm64-linux.c
 Some files to look at more in details:
  syswrap-darwin.c : the handling of sysctl(kern.usrstack) looked
    buggy to me, and has probably be made correct by the fact that
     VG_(clstk_end) is now the last addressable byte. However,unsure
    about this, as I could not find any documentation about 
    sysctl(kern.usrstack). I only find several occurences on the web,
    showing that the result of this is page aligned, which I guess
    means it must be 1+ the last addressable byte.
  syswrap-x86-darwin.c and syswrap-amd64-darwin.c
   I suspect the code that was computing client_stack_highest_word
   was wrong, and the patch makes it correct.
  syswrap-mips64-linux.c
    not sure what to do for this code. This is the only code
    that was guessing the stack differently from others.
    Kept (almost) untouched. To be discussed with mips maintainers.

coregrind/pub_core_libcassert.h
coregrind/m_libcassert.c
  * void VG_(show_sched_status):
     renamed Bool valgrind_stack_usage to Bool stack_usage
     if stack_usage, shows both the valgrind stack usage and
     the client stack boundaries
coregrind/m_scheduler/scheduler.c
coregrind/m_gdbserver/server.c
coregrind/m_gdbserver/remote-utils.c
   Updated comments in callers to VG_(show_sched_status)



git-svn-id: svn://svn.valgrind.org/valgrind/trunk@14392
2014-08-29 22:53:19 +00:00

300 lines
13 KiB
C

/*--------------------------------------------------------------------*/
/*--- Private syscalls header. priv_syswrap-generic.h ---*/
/*--------------------------------------------------------------------*/
/*
This file is part of Valgrind, a dynamic binary instrumentation
framework.
Copyright (C) 2000-2013 Julian Seward
jseward@acm.org
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.
The GNU General Public License is contained in the file COPYING.
*/
#ifndef __PRIV_SYSWRAP_GENERIC_H
#define __PRIV_SYSWRAP_GENERIC_H
#include "pub_core_basics.h" // ThreadId
#include "pub_core_vki.h" // vki_msghdr
#include "priv_types_n_macros.h" // DECL_TEMPLATE
/* Guess the client stack from the segment in which sp is mapped.
Register the guessed stack using VG_(register_stack).
Setup tst client_stack_highest_byte and client_stack_szB.
If sp is not in a mapped segment, does nothing. */
extern void ML_(guess_and_register_stack) (Addr sp, ThreadState* tst);
// Return true if address range entirely contained within client
// address space.
extern
Bool ML_(valid_client_addr)(Addr start, SizeT size, ThreadId tid,
const HChar *syscallname);
/* Handy small function to help stop wrappers from segfaulting when
presented with bogus client addresses. Is not used for generating
user-visible errors. */
extern Bool ML_(safe_to_deref) ( void* start, SizeT size );
// Returns True if the signal is OK for the client to use.
extern Bool ML_(client_signal_OK)(Int sigNo);
// Return true if we're allowed to use or create this fd.
extern
Bool ML_(fd_allowed)(Int fd, const HChar *syscallname, ThreadId tid,
Bool isNewFD);
extern void ML_(record_fd_open_named) (ThreadId tid, Int fd);
extern void ML_(record_fd_open_nameless) (ThreadId tid, Int fd);
extern void ML_(record_fd_open_with_given_name)(ThreadId tid, Int fd,
char *pathname);
// Used when killing threads -- we must not kill a thread if it's the thread
// that would do Valgrind's final cleanup and output.
extern
Bool ML_(do_sigkill)(Int pid, Int tgid);
/* When a client mmap or munmap has been successfully done, both the core
and the tool need to be notified of the new mapping. Hence this fn. */
extern void
ML_(notify_core_and_tool_of_mmap) ( Addr a, SizeT len, UInt prot,
UInt mm_flags, Int fd, Off64T offset );
extern void
ML_(notify_core_and_tool_of_munmap) ( Addr a, SizeT len );
extern void
ML_(notify_core_and_tool_of_mprotect) ( Addr a, SizeT len, Int prot );
extern void
ML_(buf_and_len_pre_check) ( ThreadId tid, Addr buf_p, Addr buflen_p,
const HChar* buf_s, const HChar* buflen_s );
extern void
ML_(buf_and_len_post_check) ( ThreadId tid, SysRes res,
Addr buf_p, Addr buflen_p, const HChar* s );
/* PRE and POST for unknown ioctls based on ioctl request encoding */
extern
void ML_(PRE_unknown_ioctl)(ThreadId tid, UWord request, UWord arg);
extern
void ML_(POST_unknown_ioctl)(ThreadId tid, UInt res, UWord request, UWord arg);
DECL_TEMPLATE(generic, sys_ni_syscall); // * P -- unimplemented
DECL_TEMPLATE(generic, sys_exit);
DECL_TEMPLATE(generic, sys_fork);
DECL_TEMPLATE(generic, sys_read);
DECL_TEMPLATE(generic, sys_write);
DECL_TEMPLATE(generic, sys_open);
DECL_TEMPLATE(generic, sys_close);
DECL_TEMPLATE(generic, sys_waitpid);
DECL_TEMPLATE(generic, sys_creat);
DECL_TEMPLATE(generic, sys_link);
DECL_TEMPLATE(generic, sys_unlink);
DECL_TEMPLATE(generic, sys_execve); // (*??) P
DECL_TEMPLATE(generic, sys_chdir);
DECL_TEMPLATE(generic, sys_time);
DECL_TEMPLATE(generic, sys_mknod);
DECL_TEMPLATE(generic, sys_chmod);
DECL_TEMPLATE(generic, sys_getpid);
DECL_TEMPLATE(generic, sys_alarm);
DECL_TEMPLATE(generic, sys_pause);
DECL_TEMPLATE(generic, sys_access);
DECL_TEMPLATE(generic, sys_kill);
DECL_TEMPLATE(generic, sys_rename);
DECL_TEMPLATE(generic, sys_mkdir);
DECL_TEMPLATE(generic, sys_rmdir);
DECL_TEMPLATE(generic, sys_dup);
DECL_TEMPLATE(generic, sys_times);
DECL_TEMPLATE(generic, sys_setpgid);
DECL_TEMPLATE(generic, sys_umask);
DECL_TEMPLATE(generic, sys_dup2);
DECL_TEMPLATE(generic, sys_getppid);
DECL_TEMPLATE(generic, sys_getpgrp);
DECL_TEMPLATE(generic, sys_setsid);
DECL_TEMPLATE(generic, sys_munmap);
DECL_TEMPLATE(generic, sys_truncate);
DECL_TEMPLATE(generic, sys_ftruncate);
DECL_TEMPLATE(generic, sys_fchmod);
DECL_TEMPLATE(generic, sys_msync);
DECL_TEMPLATE(generic, sys_readv);
DECL_TEMPLATE(generic, sys_writev);
DECL_TEMPLATE(generic, sys_getsid);
DECL_TEMPLATE(generic, sys_fdatasync);
DECL_TEMPLATE(generic, sys_mlock);
DECL_TEMPLATE(generic, sys_munlock);
DECL_TEMPLATE(generic, sys_mlockall);
DECL_TEMPLATE(generic, sys_nanosleep);
DECL_TEMPLATE(generic, sys_mremap); // POSIX, but Linux arg order may be odd
DECL_TEMPLATE(generic, sys_getuid);
DECL_TEMPLATE(generic, sys_getgid);
DECL_TEMPLATE(generic, sys_geteuid);
DECL_TEMPLATE(generic, sys_getegid);
DECL_TEMPLATE(generic, sys_getpgid);
DECL_TEMPLATE(generic, sys_fsync);
DECL_TEMPLATE(generic, sys_wait4);
DECL_TEMPLATE(generic, sys_mprotect);
DECL_TEMPLATE(generic, sys_getcwd);
DECL_TEMPLATE(generic, sys_symlink);
DECL_TEMPLATE(generic, sys_getgroups);
DECL_TEMPLATE(generic, sys_setgroups); // SVr4, SVID, X/OPEN, 4.3BSD
DECL_TEMPLATE(generic, sys_chown);
DECL_TEMPLATE(generic, sys_setuid);
DECL_TEMPLATE(generic, sys_gettimeofday);
DECL_TEMPLATE(generic, sys_madvise);
DECL_TEMPLATE(generic, sys_sethostname);
// These ones aren't POSIX, but are in some standard and look reasonably
// generic, and are the same for all architectures under Linux.
DECL_TEMPLATE(generic, sys_nice); // SVr4, SVID EXT, AT&T, X/OPEN, BSD 4.3
DECL_TEMPLATE(generic, sys_sync); // SVr4, SVID, X/OPEN, BSD 4.3
DECL_TEMPLATE(generic, sys_brk); // 4.3BSD
DECL_TEMPLATE(generic, sys_acct); // SVR4, non-POSIX
DECL_TEMPLATE(generic, sys_chroot); // SVr4, SVID, 4.4BSD, X/OPEN
DECL_TEMPLATE(generic, sys_readlink); // X/OPEN, 4.4BSD
DECL_TEMPLATE(generic, sys_fchdir); // SVr4, SVID, POSIX, X/OPEN, 4.4BSD
DECL_TEMPLATE(generic, sys_getdents); // SVr4,SVID
DECL_TEMPLATE(generic, sys_select); // 4.4BSD
DECL_TEMPLATE(generic, sys_flock); // 4.4BSD
DECL_TEMPLATE(generic, sys_poll); // XPG4-UNIX
DECL_TEMPLATE(generic, sys_getrusage); // SVr4, 4.3BSD
DECL_TEMPLATE(generic, sys_stime); // SVr4, SVID, X/OPEN
DECL_TEMPLATE(generic, sys_settimeofday); // SVr4, 4.3BSD (non-POSIX)
DECL_TEMPLATE(generic, sys_getpriority); // SVr4, 4.4BSD
DECL_TEMPLATE(generic, sys_setpriority); // SVr4, 4.4BSD
DECL_TEMPLATE(generic, sys_setitimer); // SVr4, 4.4BSD
DECL_TEMPLATE(generic, sys_getitimer); // SVr4, 4.4BSD
DECL_TEMPLATE(generic, sys_setreuid); // 4.3BSD
DECL_TEMPLATE(generic, sys_setregid); // 4.3BSD
DECL_TEMPLATE(generic, sys_fchown); // SVr4,4.3BSD
DECL_TEMPLATE(generic, sys_setgid); // SVr4,SVID
DECL_TEMPLATE(generic, sys_utimes); // 4.3BSD
// May not be generic for every architecture under Linux.
DECL_TEMPLATE(generic, sys_sigaction); // (x86) P
// Funny names, not sure...
DECL_TEMPLATE(generic, sys_newstat); // * P
DECL_TEMPLATE(generic, sys_newlstat); // *
DECL_TEMPLATE(generic, sys_newfstat); // * P (SVr4,BSD4.3)
// For the remainder, not really sure yet
DECL_TEMPLATE(generic, sys_ptrace); // (x86?) (almost-P)
DECL_TEMPLATE(generic, sys_setrlimit); // SVr4, 4.3BSD
DECL_TEMPLATE(generic, sys_old_getrlimit); // SVr4, 4.3BSD L?
DECL_TEMPLATE(generic, sys_statfs); // * L?
DECL_TEMPLATE(generic, sys_fstatfs); // * L?
DECL_TEMPLATE(generic, sys_iopl); // (x86/amd64) L
DECL_TEMPLATE(generic, sys_newuname); // * P
DECL_TEMPLATE(generic, sys_pread64); // * (Unix98?)
DECL_TEMPLATE(generic, sys_pwrite64); // * (Unix98?)
DECL_TEMPLATE(generic, sys_sigaltstack); // (x86) (XPG4-UNIX)
DECL_TEMPLATE(generic, sys_getpmsg); // (?) (?)
DECL_TEMPLATE(generic, sys_putpmsg); // (?) (?)
DECL_TEMPLATE(generic, sys_getrlimit); // * (?)
DECL_TEMPLATE(generic, sys_truncate64); // %% (P?)
DECL_TEMPLATE(generic, sys_ftruncate64); // %% (P?)
DECL_TEMPLATE(generic, sys_lchown); // * (L?)
DECL_TEMPLATE(generic, sys_mincore); // * L?
DECL_TEMPLATE(generic, sys_getdents64); // * (SVr4,SVID?)
DECL_TEMPLATE(generic, sys_statfs64); // * (?)
DECL_TEMPLATE(generic, sys_fstatfs64); // * (?)
/* ---------------------------------------------------------------------
Wrappers for sockets and ipc-ery. These are split into standalone
procedures because some platforms hides them inside multiplexors
(sys_socketcall and sys_ipc).
------------------------------------------------------------------ */
#define TId ThreadId
#define UW UWord
#define SR SysRes
extern void ML_(generic_PRE_sys_socketpair) ( TId, UW, UW, UW, UW );
extern SysRes ML_(generic_POST_sys_socketpair) ( TId, SR, UW, UW, UW, UW );
extern SysRes ML_(generic_POST_sys_socket) ( TId, SR );
extern void ML_(generic_PRE_sys_bind) ( TId, UW, UW, UW );
extern void ML_(generic_PRE_sys_accept) ( TId, UW, UW, UW );
extern SysRes ML_(generic_POST_sys_accept) ( TId, SR, UW, UW, UW );
extern void ML_(generic_PRE_sys_sendto) ( TId, UW, UW, UW, UW, UW, UW );
extern void ML_(generic_PRE_sys_send) ( TId, UW, UW, UW );
extern void ML_(generic_PRE_sys_recvfrom) ( TId, UW, UW, UW, UW, UW, UW );
extern void ML_(generic_POST_sys_recvfrom) ( TId, SR, UW, UW, UW, UW, UW, UW );
extern void ML_(generic_PRE_sys_recv) ( TId, UW, UW, UW );
extern void ML_(generic_POST_sys_recv) ( TId, UW, UW, UW, UW );
extern void ML_(generic_PRE_sys_connect) ( TId, UW, UW, UW );
extern void ML_(generic_PRE_sys_setsockopt) ( TId, UW, UW, UW, UW, UW );
extern void ML_(generic_PRE_sys_getsockname) ( TId, UW, UW, UW );
extern void ML_(generic_POST_sys_getsockname) ( TId, SR, UW, UW, UW );
extern void ML_(generic_PRE_sys_getpeername) ( TId, UW, UW, UW );
extern void ML_(generic_POST_sys_getpeername) ( TId, SR, UW, UW, UW );
extern void ML_(generic_PRE_sys_sendmsg) ( TId, const HChar *,
struct vki_msghdr * );
extern void ML_(generic_PRE_sys_recvmsg) ( TId, const HChar *,
struct vki_msghdr * );
extern void ML_(generic_POST_sys_recvmsg) ( TId, const HChar *,
struct vki_msghdr *, UInt );
extern void ML_(generic_PRE_sys_semop) ( TId, UW, UW, UW );
extern void ML_(generic_PRE_sys_semtimedop) ( TId, UW, UW, UW, UW );
extern void ML_(generic_PRE_sys_semctl) ( TId, UW, UW, UW, UW );
extern void ML_(generic_POST_sys_semctl) ( TId, UW, UW, UW, UW, UW );
extern UWord ML_(generic_PRE_sys_shmat) ( TId, UW, UW, UW );
extern void ML_(generic_POST_sys_shmat) ( TId, UW, UW, UW, UW );
extern Bool ML_(generic_PRE_sys_shmdt) ( TId, UW );
extern void ML_(generic_POST_sys_shmdt) ( TId, UW, UW );
extern void ML_(generic_PRE_sys_shmctl) ( TId, UW, UW, UW );
extern void ML_(generic_POST_sys_shmctl) ( TId, UW, UW, UW, UW );
extern SysRes ML_(generic_PRE_sys_mmap) ( TId, UW, UW, UW, UW, UW, Off64T );
#define PRE_timeval_READ(zzname, zzarg) \
do { \
struct vki_timeval *zztv = (struct vki_timeval *)(zzarg); \
PRE_FIELD_READ(zzname, zztv->tv_sec); \
PRE_FIELD_READ(zzname, zztv->tv_usec); \
} while (0)
#define PRE_timeval_WRITE(zzname, zzarg) \
do { \
struct vki_timeval *zztv = (struct vki_timeval *)(zzarg); \
PRE_FIELD_WRITE(zzname, zztv->tv_sec); \
PRE_FIELD_WRITE(zzname, zztv->tv_usec); \
} while (0)
#define POST_timeval_WRITE(zzarg) \
do { \
struct vki_timeval *zztv = (struct vki_timeval *)(zzarg); \
POST_FIELD_WRITE(zztv->tv_sec); \
POST_FIELD_WRITE(zztv->tv_usec); \
} while (0)
#undef TId
#undef UW
#undef SR
/////////////////////////////////////////////////////////////////
#endif // __PRIV_SYSWRAP_GENERIC_H
/*--------------------------------------------------------------------*/
/*--- end ---*/
/*--------------------------------------------------------------------*/