florian | 95f1037 | 2015-02-26 21:48:19 +0000 | [diff] [blame] | 1 | /* -*- mode: C; c-basic-offset: 3; -*- */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2 | |
| 3 | /*--------------------------------------------------------------------*/ |
sewardj | 6e9de46 | 2011-06-28 07:25:29 +0000 | [diff] [blame] | 4 | /*--- Wrappers for generic Unix system calls ---*/ |
njn | 8b68b64 | 2009-06-24 00:37:09 +0000 | [diff] [blame] | 5 | /*--- syswrap-generic.c ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 6 | /*--------------------------------------------------------------------*/ |
| 7 | |
| 8 | /* |
njn | b9c427c | 2004-12-01 14:14:42 +0000 | [diff] [blame] | 9 | This file is part of Valgrind, a dynamic binary instrumentation |
| 10 | framework. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 11 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 12 | Copyright (C) 2000-2017 Julian Seward |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 13 | jseward@acm.org |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 14 | |
| 15 | This program is free software; you can redistribute it and/or |
| 16 | modify it under the terms of the GNU General Public License as |
| 17 | published by the Free Software Foundation; either version 2 of the |
| 18 | License, or (at your option) any later version. |
| 19 | |
| 20 | This program is distributed in the hope that it will be useful, but |
| 21 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 23 | General Public License for more details. |
| 24 | |
| 25 | You should have received a copy of the GNU General Public License |
| 26 | along with this program; if not, write to the Free Software |
| 27 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 28 | 02111-1307, USA. |
| 29 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 30 | The GNU General Public License is contained in the file COPYING. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 31 | */ |
| 32 | |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 33 | #if defined(VGO_linux) || defined(VGO_darwin) || defined(VGO_solaris) |
njn | 8b68b64 | 2009-06-24 00:37:09 +0000 | [diff] [blame] | 34 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 35 | #include "pub_core_basics.h" |
sewardj | 4cfea4f | 2006-10-14 19:26:10 +0000 | [diff] [blame] | 36 | #include "pub_core_vki.h" |
| 37 | #include "pub_core_vkiscnums.h" |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 38 | #include "pub_core_threadstate.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 39 | #include "pub_core_debuginfo.h" // VG_(di_notify_*) |
sewardj | 55f9d1a | 2005-04-25 11:11:44 +0000 | [diff] [blame] | 40 | #include "pub_core_aspacemgr.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 41 | #include "pub_core_transtab.h" // VG_(discard_translations) |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 42 | #include "pub_core_xarray.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 43 | #include "pub_core_clientstate.h" // VG_(brk_base), VG_(brk_limit) |
njn | 899ce73 | 2005-06-21 00:28:11 +0000 | [diff] [blame] | 44 | #include "pub_core_debuglog.h" |
njn | 0087c50 | 2005-07-01 04:15:36 +0000 | [diff] [blame] | 45 | #include "pub_core_errormgr.h" |
florian | c91f584 | 2013-09-15 10:42:26 +0000 | [diff] [blame] | 46 | #include "pub_core_gdbserver.h" // VG_(gdbserver) |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 47 | #include "pub_core_libcbase.h" |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 48 | #include "pub_core_libcassert.h" |
njn | de62cbf | 2005-06-10 22:08:14 +0000 | [diff] [blame] | 49 | #include "pub_core_libcfile.h" |
| 50 | #include "pub_core_libcprint.h" |
njn | f39e9a3 | 2005-06-12 02:43:17 +0000 | [diff] [blame] | 51 | #include "pub_core_libcproc.h" |
njn | de62cbf | 2005-06-10 22:08:14 +0000 | [diff] [blame] | 52 | #include "pub_core_libcsignal.h" |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 53 | #include "pub_core_machine.h" // VG_(get_SP) |
njn | af1d7df | 2005-06-11 01:31:52 +0000 | [diff] [blame] | 54 | #include "pub_core_mallocfree.h" |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 55 | #include "pub_core_options.h" |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 56 | #include "pub_core_scheduler.h" |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 57 | #include "pub_core_signals.h" |
njn | 419bbcb | 2005-06-21 03:52:49 +0000 | [diff] [blame] | 58 | #include "pub_core_stacktrace.h" // For VG_(get_and_pp_StackTrace)() |
njn | 9abd608 | 2005-06-17 21:31:45 +0000 | [diff] [blame] | 59 | #include "pub_core_syscall.h" |
njn | c1b0181 | 2005-06-17 22:19:06 +0000 | [diff] [blame] | 60 | #include "pub_core_syswrap.h" |
njn | 419bbcb | 2005-06-21 03:52:49 +0000 | [diff] [blame] | 61 | #include "pub_core_tooliface.h" |
njn | 7375061 | 2005-10-14 03:11:30 +0000 | [diff] [blame] | 62 | #include "pub_core_ume.h" |
philippe | 38a74d2 | 2014-08-29 22:53:19 +0000 | [diff] [blame] | 63 | #include "pub_core_stacks.h" |
sewardj | 55f9d1a | 2005-04-25 11:11:44 +0000 | [diff] [blame] | 64 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 65 | #include "priv_types_n_macros.h" |
njn | c1b0181 | 2005-06-17 22:19:06 +0000 | [diff] [blame] | 66 | #include "priv_syswrap-generic.h" |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 67 | |
bart | 870f745 | 2009-12-29 16:56:18 +0000 | [diff] [blame] | 68 | #include "config.h" |
| 69 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 70 | |
philippe | 38a74d2 | 2014-08-29 22:53:19 +0000 | [diff] [blame] | 71 | void ML_(guess_and_register_stack) (Addr sp, ThreadState* tst) |
| 72 | { |
| 73 | Bool debug = False; |
| 74 | NSegment const* seg; |
| 75 | |
| 76 | /* We don't really know where the client stack is, because its |
| 77 | allocated by the client. The best we can do is look at the |
| 78 | memory mappings and try to derive some useful information. We |
| 79 | assume that sp starts near its highest possible value, and can |
| 80 | only go down to the start of the mmaped segment. */ |
| 81 | seg = VG_(am_find_nsegment)(sp); |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 82 | if (seg |
| 83 | && VG_(am_is_valid_for_client)(sp, 1, VKI_PROT_READ | VKI_PROT_WRITE)) { |
philippe | 38a74d2 | 2014-08-29 22:53:19 +0000 | [diff] [blame] | 84 | tst->client_stack_highest_byte = (Addr)VG_PGROUNDUP(sp)-1; |
| 85 | tst->client_stack_szB = tst->client_stack_highest_byte - seg->start + 1; |
| 86 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 87 | tst->os_state.stk_id |
| 88 | = VG_(register_stack)(seg->start, tst->client_stack_highest_byte); |
philippe | 38a74d2 | 2014-08-29 22:53:19 +0000 | [diff] [blame] | 89 | |
| 90 | if (debug) |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 91 | VG_(printf)("tid %u: guessed client stack range [%#lx-%#lx]" |
| 92 | " as stk_id %lu\n", |
| 93 | tst->tid, seg->start, tst->client_stack_highest_byte, |
| 94 | tst->os_state.stk_id); |
philippe | 38a74d2 | 2014-08-29 22:53:19 +0000 | [diff] [blame] | 95 | } else { |
| 96 | VG_(message)(Vg_UserMsg, |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 97 | "!? New thread %u starts with SP(%#lx) unmapped\n", |
philippe | 38a74d2 | 2014-08-29 22:53:19 +0000 | [diff] [blame] | 98 | tst->tid, sp); |
| 99 | tst->client_stack_highest_byte = 0; |
| 100 | tst->client_stack_szB = 0; |
| 101 | } |
| 102 | } |
| 103 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 104 | /* Returns True iff address range is something the client can |
| 105 | plausibly mess with: all of it is either already belongs to the |
| 106 | client or is free or a reservation. */ |
| 107 | |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 108 | Bool ML_(valid_client_addr)(Addr start, SizeT size, ThreadId tid, |
florian | e543f30 | 2012-10-21 19:43:43 +0000 | [diff] [blame] | 109 | const HChar *syscallname) |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 110 | { |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 111 | Bool ret; |
| 112 | |
| 113 | if (size == 0) |
| 114 | return True; |
| 115 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 116 | ret = VG_(am_is_valid_for_client_or_free_or_resvn) |
| 117 | (start,size,VKI_PROT_NONE); |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 118 | |
| 119 | if (0) |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 120 | VG_(printf)("%s: test=%#lx-%#lx ret=%d\n", |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 121 | syscallname, start, start+size-1, (Int)ret); |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 122 | |
nethercote | 1543adf | 2004-10-25 15:43:21 +0000 | [diff] [blame] | 123 | if (!ret && syscallname != NULL) { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 124 | VG_(message)(Vg_UserMsg, "Warning: client syscall %s tried " |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 125 | "to modify addresses %#lx-%#lx\n", |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 126 | syscallname, start, start+size-1); |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 127 | if (VG_(clo_verbosity) > 1) { |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 128 | VG_(get_and_pp_StackTrace)(tid, VG_(clo_backtrace_size)); |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | return ret; |
| 133 | } |
| 134 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 135 | |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 136 | Bool ML_(client_signal_OK)(Int sigNo) |
njn | 419bbcb | 2005-06-21 03:52:49 +0000 | [diff] [blame] | 137 | { |
| 138 | /* signal 0 is OK for kill */ |
njn | 351d006 | 2005-06-21 22:23:59 +0000 | [diff] [blame] | 139 | Bool ret = sigNo >= 0 && sigNo <= VG_SIGVGRTUSERMAX; |
njn | 419bbcb | 2005-06-21 03:52:49 +0000 | [diff] [blame] | 140 | |
| 141 | //VG_(printf)("client_signal_OK(%d) -> %d\n", sigNo, ret); |
| 142 | |
| 143 | return ret; |
| 144 | } |
| 145 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 146 | |
| 147 | /* Handy small function to help stop wrappers from segfaulting when |
| 148 | presented with bogus client addresses. Is not used for generating |
| 149 | user-visible errors. */ |
| 150 | |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 151 | Bool ML_(safe_to_deref) ( const void *start, SizeT size ) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 152 | { |
njn | 143377e | 2009-06-10 06:13:34 +0000 | [diff] [blame] | 153 | return VG_(am_is_valid_for_client)( (Addr)start, size, VKI_PROT_READ ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 157 | /* --------------------------------------------------------------------- |
nethercote | 27ea8bc | 2004-07-10 17:21:14 +0000 | [diff] [blame] | 158 | Doing mmap, mremap |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 159 | ------------------------------------------------------------------ */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 160 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 161 | /* AFAICT from kernel sources (mm/mprotect.c) and general experimentation, |
| 162 | munmap, mprotect (and mremap??) work at the page level. So addresses |
| 163 | and lengths must be adjusted for this. */ |
| 164 | |
| 165 | /* Mash around start and length so that the area exactly covers |
| 166 | an integral number of pages. If we don't do that, memcheck's |
| 167 | idea of addressible memory diverges from that of the |
| 168 | kernel's, which causes the leak detector to crash. */ |
| 169 | static |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 170 | void page_align_addr_and_len( Addr* a, SizeT* len) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 171 | { |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 172 | Addr ra; |
| 173 | |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 174 | ra = VG_PGROUNDDN(*a); |
| 175 | *len = VG_PGROUNDUP(*a + *len) - ra; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 176 | *a = ra; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 177 | } |
| 178 | |
njn | dd37258 | 2009-05-10 22:42:19 +0000 | [diff] [blame] | 179 | static void notify_core_of_mmap(Addr a, SizeT len, UInt prot, |
| 180 | UInt flags, Int fd, Off64T offset) |
bart | 510521e | 2008-05-29 08:52:44 +0000 | [diff] [blame] | 181 | { |
| 182 | Bool d; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 183 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 184 | /* 'a' is the return value from a real kernel mmap, hence: */ |
| 185 | vg_assert(VG_IS_PAGE_ALIGNED(a)); |
| 186 | /* whereas len is whatever the syscall supplied. So: */ |
| 187 | len = VG_PGROUNDUP(len); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 188 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 189 | d = VG_(am_notify_client_mmap)( a, len, prot, flags, fd, offset ); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 190 | |
bart | 510521e | 2008-05-29 08:52:44 +0000 | [diff] [blame] | 191 | if (d) |
florian | ddd61ff | 2015-01-04 17:20:45 +0000 | [diff] [blame] | 192 | VG_(discard_translations)( a, (ULong)len, |
njn | dd37258 | 2009-05-10 22:42:19 +0000 | [diff] [blame] | 193 | "notify_core_of_mmap" ); |
bart | 510521e | 2008-05-29 08:52:44 +0000 | [diff] [blame] | 194 | } |
| 195 | |
njn | dd37258 | 2009-05-10 22:42:19 +0000 | [diff] [blame] | 196 | static void notify_tool_of_mmap(Addr a, SizeT len, UInt prot, ULong di_handle) |
bart | 510521e | 2008-05-29 08:52:44 +0000 | [diff] [blame] | 197 | { |
| 198 | Bool rr, ww, xx; |
| 199 | |
| 200 | /* 'a' is the return value from a real kernel mmap, hence: */ |
| 201 | vg_assert(VG_IS_PAGE_ALIGNED(a)); |
| 202 | /* whereas len is whatever the syscall supplied. So: */ |
| 203 | len = VG_PGROUNDUP(len); |
| 204 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 205 | rr = toBool(prot & VKI_PROT_READ); |
| 206 | ww = toBool(prot & VKI_PROT_WRITE); |
| 207 | xx = toBool(prot & VKI_PROT_EXEC); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 208 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 209 | VG_TRACK( new_mem_mmap, a, len, rr, ww, xx, di_handle ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 210 | } |
| 211 | |
njn | dd37258 | 2009-05-10 22:42:19 +0000 | [diff] [blame] | 212 | |
| 213 | /* When a client mmap has been successfully done, this function must |
| 214 | be called. It notifies both aspacem and the tool of the new |
| 215 | mapping. |
| 216 | |
| 217 | JRS 2008-Aug-14: But notice this is *very* obscure. The only place |
| 218 | it is called from is POST(sys_io_setup). In particular, |
| 219 | ML_(generic_PRE_sys_mmap), in m_syswrap, is the "normal case" handler for |
| 220 | client mmap. But it doesn't call this function; instead it does the |
| 221 | relevant notifications itself. Here, we just pass di_handle=0 to |
| 222 | notify_tool_of_mmap as we have no better information. But really this |
| 223 | function should be done away with; problem is I don't understand what |
| 224 | POST(sys_io_setup) does or how it works. |
| 225 | |
| 226 | [However, this function is used lots for Darwin, because |
| 227 | ML_(generic_PRE_sys_mmap) cannot be used for Darwin.] |
| 228 | */ |
| 229 | void |
| 230 | ML_(notify_core_and_tool_of_mmap) ( Addr a, SizeT len, UInt prot, |
| 231 | UInt flags, Int fd, Off64T offset ) |
| 232 | { |
| 233 | // XXX: unlike the other notify_core_and_tool* functions, this one doesn't |
| 234 | // do anything with debug info (ie. it doesn't call VG_(di_notify_mmap)). |
| 235 | // Should it? --njn |
| 236 | notify_core_of_mmap(a, len, prot, flags, fd, offset); |
| 237 | notify_tool_of_mmap(a, len, prot, 0/*di_handle*/); |
| 238 | } |
| 239 | |
| 240 | void |
| 241 | ML_(notify_core_and_tool_of_munmap) ( Addr a, SizeT len ) |
| 242 | { |
| 243 | Bool d; |
| 244 | |
| 245 | page_align_addr_and_len(&a, &len); |
| 246 | d = VG_(am_notify_munmap)(a, len); |
| 247 | VG_TRACK( die_mem_munmap, a, len ); |
| 248 | VG_(di_notify_munmap)( a, len ); |
| 249 | if (d) |
florian | ddd61ff | 2015-01-04 17:20:45 +0000 | [diff] [blame] | 250 | VG_(discard_translations)( a, (ULong)len, |
njn | dd37258 | 2009-05-10 22:42:19 +0000 | [diff] [blame] | 251 | "ML_(notify_core_and_tool_of_munmap)" ); |
| 252 | } |
| 253 | |
| 254 | void |
| 255 | ML_(notify_core_and_tool_of_mprotect) ( Addr a, SizeT len, Int prot ) |
| 256 | { |
| 257 | Bool rr = toBool(prot & VKI_PROT_READ); |
| 258 | Bool ww = toBool(prot & VKI_PROT_WRITE); |
| 259 | Bool xx = toBool(prot & VKI_PROT_EXEC); |
| 260 | Bool d; |
| 261 | |
| 262 | page_align_addr_and_len(&a, &len); |
| 263 | d = VG_(am_notify_mprotect)(a, len, prot); |
| 264 | VG_TRACK( change_mem_mprotect, a, len, rr, ww, xx ); |
| 265 | VG_(di_notify_mprotect)( a, len, prot ); |
| 266 | if (d) |
florian | ddd61ff | 2015-01-04 17:20:45 +0000 | [diff] [blame] | 267 | VG_(discard_translations)( a, (ULong)len, |
njn | dd37258 | 2009-05-10 22:42:19 +0000 | [diff] [blame] | 268 | "ML_(notify_core_and_tool_of_mprotect)" ); |
| 269 | } |
| 270 | |
| 271 | |
| 272 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 273 | #if HAVE_MREMAP |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 274 | /* Expand (or shrink) an existing mapping, potentially moving it at |
| 275 | the same time (controlled by the MREMAP_MAYMOVE flag). Nightmare. |
| 276 | */ |
| 277 | static |
| 278 | SysRes do_mremap( Addr old_addr, SizeT old_len, |
| 279 | Addr new_addr, SizeT new_len, |
| 280 | UWord flags, ThreadId tid ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 281 | { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 282 | # define MIN_SIZET(_aa,_bb) (_aa) < (_bb) ? (_aa) : (_bb) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 283 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 284 | Bool ok, d; |
sewardj | ef1cf8b | 2006-10-17 01:47:30 +0000 | [diff] [blame] | 285 | NSegment const* old_seg; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 286 | Addr advised; |
| 287 | Bool f_fixed = toBool(flags & VKI_MREMAP_FIXED); |
| 288 | Bool f_maymove = toBool(flags & VKI_MREMAP_MAYMOVE); |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 289 | |
| 290 | if (0) |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 291 | VG_(printf)("do_remap (old %#lx %lu) (new %#lx %lu) %s %s\n", |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 292 | old_addr,old_len,new_addr,new_len, |
| 293 | flags & VKI_MREMAP_MAYMOVE ? "MAYMOVE" : "", |
| 294 | flags & VKI_MREMAP_FIXED ? "FIXED" : ""); |
sewardj | 63a622f | 2006-08-16 14:22:29 +0000 | [diff] [blame] | 295 | if (0) |
| 296 | VG_(am_show_nsegments)(0, "do_remap: before"); |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 297 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 298 | if (flags & ~(VKI_MREMAP_FIXED | VKI_MREMAP_MAYMOVE)) |
| 299 | goto eINVAL; |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 300 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 301 | if (!VG_IS_PAGE_ALIGNED(old_addr)) |
| 302 | goto eINVAL; |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 303 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 304 | old_len = VG_PGROUNDUP(old_len); |
| 305 | new_len = VG_PGROUNDUP(new_len); |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 306 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 307 | if (new_len == 0) |
| 308 | goto eINVAL; |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 309 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 310 | /* kernel doesn't reject this, but we do. */ |
| 311 | if (old_len == 0) |
| 312 | goto eINVAL; |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 313 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 314 | /* reject wraparounds */ |
tom | a5e13dd | 2011-08-11 14:51:47 +0000 | [diff] [blame] | 315 | if (old_addr + old_len < old_addr) |
| 316 | goto eINVAL; |
| 317 | if (f_fixed == True && new_addr + new_len < new_len) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 318 | goto eINVAL; |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 319 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 320 | /* kernel rejects all fixed, no-move requests (which are |
| 321 | meaningless). */ |
| 322 | if (f_fixed == True && f_maymove == False) |
| 323 | goto eINVAL; |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 324 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 325 | /* Stay away from non-client areas. */ |
| 326 | if (!ML_(valid_client_addr)(old_addr, old_len, tid, "mremap(old_addr)")) |
| 327 | goto eINVAL; |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 328 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 329 | /* In all remaining cases, if the old range does not fall within a |
| 330 | single segment, fail. */ |
| 331 | old_seg = VG_(am_find_nsegment)( old_addr ); |
| 332 | if (old_addr < old_seg->start || old_addr+old_len-1 > old_seg->end) |
| 333 | goto eINVAL; |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 334 | if (old_seg->kind != SkAnonC && old_seg->kind != SkFileC |
| 335 | && old_seg->kind != SkShmC) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 336 | goto eINVAL; |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 337 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 338 | vg_assert(old_len > 0); |
| 339 | vg_assert(new_len > 0); |
| 340 | vg_assert(VG_IS_PAGE_ALIGNED(old_len)); |
| 341 | vg_assert(VG_IS_PAGE_ALIGNED(new_len)); |
| 342 | vg_assert(VG_IS_PAGE_ALIGNED(old_addr)); |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 343 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 344 | /* There are 3 remaining cases: |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 345 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 346 | * maymove == False |
| 347 | |
| 348 | new space has to be at old address, so: |
| 349 | - shrink -> unmap end |
| 350 | - same size -> do nothing |
| 351 | - grow -> if can grow in-place, do so, else fail |
| 352 | |
| 353 | * maymove == True, fixed == False |
| 354 | |
| 355 | new space can be anywhere, so: |
| 356 | - shrink -> unmap end |
| 357 | - same size -> do nothing |
| 358 | - grow -> if can grow in-place, do so, else |
| 359 | move to anywhere large enough, else fail |
| 360 | |
| 361 | * maymove == True, fixed == True |
| 362 | |
| 363 | new space must be at new address, so: |
| 364 | |
| 365 | - if new address is not page aligned, fail |
| 366 | - if new address range overlaps old one, fail |
| 367 | - if new address range cannot be allocated, fail |
| 368 | - else move to new address range with new size |
| 369 | - else fail |
| 370 | */ |
| 371 | |
| 372 | if (f_maymove == False) { |
| 373 | /* new space has to be at old address */ |
| 374 | if (new_len < old_len) |
| 375 | goto shrink_in_place; |
| 376 | if (new_len > old_len) |
| 377 | goto grow_in_place_or_fail; |
| 378 | goto same_in_place; |
| 379 | } |
| 380 | |
| 381 | if (f_maymove == True && f_fixed == False) { |
| 382 | /* new space can be anywhere */ |
| 383 | if (new_len < old_len) |
| 384 | goto shrink_in_place; |
| 385 | if (new_len > old_len) |
| 386 | goto grow_in_place_or_move_anywhere_or_fail; |
| 387 | goto same_in_place; |
| 388 | } |
| 389 | |
| 390 | if (f_maymove == True && f_fixed == True) { |
| 391 | /* new space can only be at the new address */ |
| 392 | if (!VG_IS_PAGE_ALIGNED(new_addr)) |
| 393 | goto eINVAL; |
| 394 | if (new_addr+new_len-1 < old_addr || new_addr > old_addr+old_len-1) { |
| 395 | /* no overlap */ |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 396 | } else { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 397 | goto eINVAL; |
| 398 | } |
| 399 | if (new_addr == 0) |
| 400 | goto eINVAL; |
| 401 | /* VG_(am_get_advisory_client_simple) interprets zero to mean |
| 402 | non-fixed, which is not what we want */ |
| 403 | advised = VG_(am_get_advisory_client_simple)(new_addr, new_len, &ok); |
| 404 | if (!ok || advised != new_addr) |
| 405 | goto eNOMEM; |
| 406 | ok = VG_(am_relocate_nooverlap_client) |
| 407 | ( &d, old_addr, old_len, new_addr, new_len ); |
| 408 | if (ok) { |
| 409 | VG_TRACK( copy_mem_remap, old_addr, new_addr, |
| 410 | MIN_SIZET(old_len,new_len) ); |
| 411 | if (new_len > old_len) |
| 412 | VG_TRACK( new_mem_mmap, new_addr+old_len, new_len-old_len, |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 413 | old_seg->hasR, old_seg->hasW, old_seg->hasX, |
| 414 | 0/*di_handle*/ ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 415 | VG_TRACK(die_mem_munmap, old_addr, old_len); |
| 416 | if (d) { |
| 417 | VG_(discard_translations)( old_addr, old_len, "do_remap(1)" ); |
| 418 | VG_(discard_translations)( new_addr, new_len, "do_remap(2)" ); |
| 419 | } |
| 420 | return VG_(mk_SysRes_Success)( new_addr ); |
| 421 | } |
| 422 | goto eNOMEM; |
| 423 | } |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 424 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 425 | /* end of the 3 cases */ |
| 426 | /*NOTREACHED*/ vg_assert(0); |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 427 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 428 | grow_in_place_or_move_anywhere_or_fail: |
| 429 | { |
| 430 | /* try growing it in-place */ |
| 431 | Addr needA = old_addr + old_len; |
| 432 | SSizeT needL = new_len - old_len; |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 433 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 434 | vg_assert(needL > 0); |
florian | 0b41f32 | 2014-11-24 17:30:01 +0000 | [diff] [blame] | 435 | vg_assert(needA > 0); |
| 436 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 437 | advised = VG_(am_get_advisory_client_simple)( needA, needL, &ok ); |
sewardj | 63a622f | 2006-08-16 14:22:29 +0000 | [diff] [blame] | 438 | if (ok) { |
philippe | 15e301e | 2011-12-22 13:25:58 +0000 | [diff] [blame] | 439 | /* Fixes bug #129866. */ |
| 440 | ok = VG_(am_covered_by_single_free_segment) ( needA, needL ); |
sewardj | 63a622f | 2006-08-16 14:22:29 +0000 | [diff] [blame] | 441 | } |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 442 | if (ok && advised == needA) { |
florian | dd7318b | 2015-02-25 10:06:06 +0000 | [diff] [blame] | 443 | const NSegment *new_seg = VG_(am_extend_map_client)( old_addr, needL ); |
| 444 | if (new_seg) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 445 | VG_TRACK( new_mem_mmap, needA, needL, |
florian | dd7318b | 2015-02-25 10:06:06 +0000 | [diff] [blame] | 446 | new_seg->hasR, |
| 447 | new_seg->hasW, new_seg->hasX, |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 448 | 0/*di_handle*/ ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 449 | return VG_(mk_SysRes_Success)( old_addr ); |
fitzhardinge | 1a30304 | 2003-12-22 08:48:50 +0000 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 453 | /* that failed. Look elsewhere. */ |
| 454 | advised = VG_(am_get_advisory_client_simple)( 0, new_len, &ok ); |
| 455 | if (ok) { |
tom | 5484f38 | 2010-09-27 15:08:34 +0000 | [diff] [blame] | 456 | Bool oldR = old_seg->hasR; |
| 457 | Bool oldW = old_seg->hasW; |
| 458 | Bool oldX = old_seg->hasX; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 459 | /* assert new area does not overlap old */ |
| 460 | vg_assert(advised+new_len-1 < old_addr |
| 461 | || advised > old_addr+old_len-1); |
| 462 | ok = VG_(am_relocate_nooverlap_client) |
| 463 | ( &d, old_addr, old_len, advised, new_len ); |
| 464 | if (ok) { |
| 465 | VG_TRACK( copy_mem_remap, old_addr, advised, |
| 466 | MIN_SIZET(old_len,new_len) ); |
| 467 | if (new_len > old_len) |
| 468 | VG_TRACK( new_mem_mmap, advised+old_len, new_len-old_len, |
tom | 5484f38 | 2010-09-27 15:08:34 +0000 | [diff] [blame] | 469 | oldR, oldW, oldX, 0/*di_handle*/ ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 470 | VG_TRACK(die_mem_munmap, old_addr, old_len); |
| 471 | if (d) { |
| 472 | VG_(discard_translations)( old_addr, old_len, "do_remap(4)" ); |
| 473 | VG_(discard_translations)( advised, new_len, "do_remap(5)" ); |
| 474 | } |
| 475 | return VG_(mk_SysRes_Success)( advised ); |
| 476 | } |
| 477 | } |
| 478 | goto eNOMEM; |
| 479 | } |
| 480 | /*NOTREACHED*/ vg_assert(0); |
| 481 | |
| 482 | grow_in_place_or_fail: |
| 483 | { |
| 484 | Addr needA = old_addr + old_len; |
| 485 | SizeT needL = new_len - old_len; |
florian | 0b41f32 | 2014-11-24 17:30:01 +0000 | [diff] [blame] | 486 | |
| 487 | vg_assert(needA > 0); |
| 488 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 489 | advised = VG_(am_get_advisory_client_simple)( needA, needL, &ok ); |
sewardj | 63a622f | 2006-08-16 14:22:29 +0000 | [diff] [blame] | 490 | if (ok) { |
philippe | 15e301e | 2011-12-22 13:25:58 +0000 | [diff] [blame] | 491 | /* Fixes bug #129866. */ |
| 492 | ok = VG_(am_covered_by_single_free_segment) ( needA, needL ); |
sewardj | 63a622f | 2006-08-16 14:22:29 +0000 | [diff] [blame] | 493 | } |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 494 | if (!ok || advised != needA) |
| 495 | goto eNOMEM; |
florian | dd7318b | 2015-02-25 10:06:06 +0000 | [diff] [blame] | 496 | const NSegment *new_seg = VG_(am_extend_map_client)( old_addr, needL ); |
| 497 | if (!new_seg) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 498 | goto eNOMEM; |
| 499 | VG_TRACK( new_mem_mmap, needA, needL, |
florian | dd7318b | 2015-02-25 10:06:06 +0000 | [diff] [blame] | 500 | new_seg->hasR, new_seg->hasW, new_seg->hasX, |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 501 | 0/*di_handle*/ ); |
florian | dd7318b | 2015-02-25 10:06:06 +0000 | [diff] [blame] | 502 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 503 | return VG_(mk_SysRes_Success)( old_addr ); |
| 504 | } |
| 505 | /*NOTREACHED*/ vg_assert(0); |
| 506 | |
| 507 | shrink_in_place: |
| 508 | { |
| 509 | SysRes sres = VG_(am_munmap_client)( &d, old_addr+new_len, old_len-new_len ); |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 510 | if (sr_isError(sres)) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 511 | return sres; |
| 512 | VG_TRACK( die_mem_munmap, old_addr+new_len, old_len-new_len ); |
| 513 | if (d) |
| 514 | VG_(discard_translations)( old_addr+new_len, old_len-new_len, |
| 515 | "do_remap(7)" ); |
| 516 | return VG_(mk_SysRes_Success)( old_addr ); |
| 517 | } |
| 518 | /*NOTREACHED*/ vg_assert(0); |
| 519 | |
| 520 | same_in_place: |
| 521 | return VG_(mk_SysRes_Success)( old_addr ); |
| 522 | /*NOTREACHED*/ vg_assert(0); |
| 523 | |
| 524 | eINVAL: |
| 525 | return VG_(mk_SysRes_Error)( VKI_EINVAL ); |
| 526 | eNOMEM: |
| 527 | return VG_(mk_SysRes_Error)( VKI_ENOMEM ); |
| 528 | |
| 529 | # undef MIN_SIZET |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 530 | } |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 531 | #endif /* HAVE_MREMAP */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 532 | |
| 533 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 534 | /* --------------------------------------------------------------------- |
| 535 | File-descriptor tracking |
| 536 | ------------------------------------------------------------------ */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 537 | |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 538 | /* One of these is allocated for each open file descriptor. */ |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 539 | typedef struct OpenFd |
| 540 | { |
| 541 | Int fd; /* The file descriptor */ |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 542 | HChar *pathname; /* NULL if not a regular file or unknown */ |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 543 | ExeContext *where; /* NULL if inherited from parent */ |
| 544 | struct OpenFd *next, *prev; |
| 545 | } OpenFd; |
| 546 | |
| 547 | /* List of allocated file descriptors. */ |
sewardj | 1aa4021 | 2006-12-11 01:11:02 +0000 | [diff] [blame] | 548 | static OpenFd *allocated_fds = NULL; |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 549 | |
| 550 | /* Count of open file descriptors. */ |
sewardj | 0a400c7 | 2005-07-23 09:22:46 +0000 | [diff] [blame] | 551 | static Int fd_count = 0; |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 552 | |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 553 | |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 554 | /* Note the fact that a file descriptor was just closed. */ |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 555 | void ML_(record_fd_close)(Int fd) |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 556 | { |
| 557 | OpenFd *i = allocated_fds; |
| 558 | |
thughes | ad1c956 | 2004-06-26 11:27:52 +0000 | [diff] [blame] | 559 | if (fd >= VG_(fd_hard_limit)) |
rjwalsh | 02665ba | 2003-12-18 01:48:06 +0000 | [diff] [blame] | 560 | return; /* Valgrind internal */ |
| 561 | |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 562 | while(i) { |
| 563 | if(i->fd == fd) { |
| 564 | if(i->prev) |
| 565 | i->prev->next = i->next; |
| 566 | else |
| 567 | allocated_fds = i->next; |
| 568 | if(i->next) |
| 569 | i->next->prev = i->prev; |
| 570 | if(i->pathname) |
florian | 77eb20b | 2014-09-11 21:19:17 +0000 | [diff] [blame] | 571 | VG_(free) (i->pathname); |
| 572 | VG_(free) (i); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 573 | fd_count--; |
| 574 | break; |
| 575 | } |
| 576 | i = i->next; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | /* Note the fact that a file descriptor was just opened. If the |
| 581 | tid is -1, this indicates an inherited fd. If the pathname is NULL, |
| 582 | this either indicates a non-standard file (i.e. a pipe or socket or |
| 583 | some such thing) or that we don't know the filename. If the fd is |
| 584 | already open, then we're probably doing a dup2() to an existing fd, |
| 585 | so just overwrite the existing one. */ |
florian | 3130eab | 2014-11-14 19:25:08 +0000 | [diff] [blame] | 586 | void ML_(record_fd_open_with_given_name)(ThreadId tid, Int fd, |
| 587 | const HChar *pathname) |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 588 | { |
| 589 | OpenFd *i; |
| 590 | |
thughes | ad1c956 | 2004-06-26 11:27:52 +0000 | [diff] [blame] | 591 | if (fd >= VG_(fd_hard_limit)) |
fitzhardinge | 0e8bfcf | 2003-12-12 07:46:54 +0000 | [diff] [blame] | 592 | return; /* Valgrind internal */ |
| 593 | |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 594 | /* Check to see if this fd is already open. */ |
| 595 | i = allocated_fds; |
| 596 | while (i) { |
| 597 | if (i->fd == fd) { |
florian | 77eb20b | 2014-09-11 21:19:17 +0000 | [diff] [blame] | 598 | if (i->pathname) VG_(free)(i->pathname); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 599 | break; |
| 600 | } |
| 601 | i = i->next; |
| 602 | } |
| 603 | |
| 604 | /* Not already one: allocate an OpenFd */ |
| 605 | if (i == NULL) { |
florian | 77eb20b | 2014-09-11 21:19:17 +0000 | [diff] [blame] | 606 | i = VG_(malloc)("syswrap.rfdowgn.1", sizeof(OpenFd)); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 607 | |
| 608 | i->prev = NULL; |
| 609 | i->next = allocated_fds; |
| 610 | if(allocated_fds) allocated_fds->prev = i; |
| 611 | allocated_fds = i; |
| 612 | fd_count++; |
| 613 | } |
| 614 | |
| 615 | i->fd = fd; |
florian | 77eb20b | 2014-09-11 21:19:17 +0000 | [diff] [blame] | 616 | i->pathname = VG_(strdup)("syswrap.rfdowgn.2", pathname); |
sewardj | d01b598 | 2007-11-09 23:24:09 +0000 | [diff] [blame] | 617 | i->where = (tid == -1) ? NULL : VG_(record_ExeContext)(tid, 0/*first_ip_delta*/); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 618 | } |
| 619 | |
njn | f845f8f | 2005-06-23 02:26:47 +0000 | [diff] [blame] | 620 | // Record opening of an fd, and find its name. |
njn | 096ccdd | 2009-02-22 23:00:30 +0000 | [diff] [blame] | 621 | void ML_(record_fd_open_named)(ThreadId tid, Int fd) |
njn | f845f8f | 2005-06-23 02:26:47 +0000 | [diff] [blame] | 622 | { |
florian | 3130eab | 2014-11-14 19:25:08 +0000 | [diff] [blame] | 623 | const HChar* buf; |
| 624 | const HChar* name; |
florian | a175ffb | 2014-10-14 21:01:33 +0000 | [diff] [blame] | 625 | if (VG_(resolve_filename)(fd, &buf)) |
njn | f845f8f | 2005-06-23 02:26:47 +0000 | [diff] [blame] | 626 | name = buf; |
| 627 | else |
| 628 | name = NULL; |
| 629 | |
njn | 4279a88 | 2005-08-26 03:43:28 +0000 | [diff] [blame] | 630 | ML_(record_fd_open_with_given_name)(tid, fd, name); |
njn | f845f8f | 2005-06-23 02:26:47 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | // Record opening of a nameless fd. |
| 634 | void ML_(record_fd_open_nameless)(ThreadId tid, Int fd) |
| 635 | { |
njn | 4279a88 | 2005-08-26 03:43:28 +0000 | [diff] [blame] | 636 | ML_(record_fd_open_with_given_name)(tid, fd, NULL); |
njn | f845f8f | 2005-06-23 02:26:47 +0000 | [diff] [blame] | 637 | } |
| 638 | |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 639 | // Return if a given file descriptor is already recorded. |
| 640 | Bool ML_(fd_recorded)(Int fd) |
| 641 | { |
| 642 | OpenFd *i = allocated_fds; |
| 643 | while (i) { |
| 644 | if (i->fd == fd) |
| 645 | return True; |
| 646 | i = i->next; |
| 647 | } |
| 648 | return False; |
| 649 | } |
| 650 | |
| 651 | /* Returned string must not be modified nor free'd. */ |
| 652 | const HChar *ML_(find_fd_recorded_by_fd)(Int fd) |
| 653 | { |
| 654 | OpenFd *i = allocated_fds; |
| 655 | |
| 656 | while (i) { |
| 657 | if (i->fd == fd) |
| 658 | return i->pathname; |
| 659 | i = i->next; |
| 660 | } |
| 661 | |
| 662 | return NULL; |
| 663 | } |
| 664 | |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 665 | static |
tom | 3995526 | 2014-02-24 15:06:06 +0000 | [diff] [blame] | 666 | HChar *unix_to_name(struct vki_sockaddr_un *sa, UInt len, HChar *name) |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 667 | { |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 668 | if (sa == NULL || len == 0 || sa->sun_path[0] == '\0') { |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 669 | VG_(sprintf)(name, "<unknown>"); |
| 670 | } else { |
| 671 | VG_(sprintf)(name, "%s", sa->sun_path); |
| 672 | } |
| 673 | |
| 674 | return name; |
| 675 | } |
| 676 | |
| 677 | static |
tom | 3995526 | 2014-02-24 15:06:06 +0000 | [diff] [blame] | 678 | HChar *inet_to_name(struct vki_sockaddr_in *sa, UInt len, HChar *name) |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 679 | { |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 680 | if (sa == NULL || len == 0) { |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 681 | VG_(sprintf)(name, "<unknown>"); |
tom | 3995526 | 2014-02-24 15:06:06 +0000 | [diff] [blame] | 682 | } else if (sa->sin_port == 0) { |
| 683 | VG_(sprintf)(name, "<unbound>"); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 684 | } else { |
tom | 329af4b | 2005-11-17 12:02:58 +0000 | [diff] [blame] | 685 | UInt addr = VG_(ntohl)(sa->sin_addr.s_addr); |
tom | 3995526 | 2014-02-24 15:06:06 +0000 | [diff] [blame] | 686 | VG_(sprintf)(name, "%u.%u.%u.%u:%u", |
| 687 | (addr>>24) & 0xFF, (addr>>16) & 0xFF, |
| 688 | (addr>>8) & 0xFF, addr & 0xFF, |
| 689 | VG_(ntohs)(sa->sin_port)); |
| 690 | } |
| 691 | |
| 692 | return name; |
| 693 | } |
| 694 | |
| 695 | static |
| 696 | void inet6_format(HChar *s, const UChar ip[16]) |
| 697 | { |
| 698 | static const unsigned char V4mappedprefix[12] = {0,0,0,0,0,0,0,0,0,0,0xff,0xff}; |
| 699 | |
| 700 | if (!VG_(memcmp)(ip, V4mappedprefix, 12)) { |
florian | 8eebf23 | 2014-09-18 18:35:47 +0000 | [diff] [blame] | 701 | const struct vki_in_addr *sin_addr = |
| 702 | (const struct vki_in_addr *)(ip + 12); |
tom | 3995526 | 2014-02-24 15:06:06 +0000 | [diff] [blame] | 703 | UInt addr = VG_(ntohl)(sin_addr->s_addr); |
| 704 | |
| 705 | VG_(sprintf)(s, "::ffff:%u.%u.%u.%u", |
| 706 | (addr>>24) & 0xFF, (addr>>16) & 0xFF, |
| 707 | (addr>>8) & 0xFF, addr & 0xFF); |
| 708 | } else { |
| 709 | Bool compressing = False; |
| 710 | Bool compressed = False; |
| 711 | Int len = 0; |
| 712 | Int i; |
| 713 | |
| 714 | for (i = 0; i < 16; i += 2) { |
| 715 | UInt word = ((UInt)ip[i] << 8) | (UInt)ip[i+1]; |
| 716 | if (word == 0 && !compressed) { |
| 717 | compressing = True; |
| 718 | } else { |
| 719 | if (compressing) { |
| 720 | compressing = False; |
| 721 | compressed = True; |
| 722 | s[len++] = ':'; |
| 723 | } |
| 724 | if (i > 0) { |
| 725 | s[len++] = ':'; |
| 726 | } |
| 727 | len += VG_(sprintf)(s + len, "%x", word); |
| 728 | } |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 729 | } |
tom | 3995526 | 2014-02-24 15:06:06 +0000 | [diff] [blame] | 730 | |
| 731 | if (compressing) { |
| 732 | s[len++] = ':'; |
| 733 | s[len++] = ':'; |
| 734 | } |
| 735 | |
| 736 | s[len++] = 0; |
| 737 | } |
| 738 | |
| 739 | return; |
| 740 | } |
| 741 | |
| 742 | static |
| 743 | HChar *inet6_to_name(struct vki_sockaddr_in6 *sa, UInt len, HChar *name) |
| 744 | { |
| 745 | if (sa == NULL || len == 0) { |
| 746 | VG_(sprintf)(name, "<unknown>"); |
| 747 | } else if (sa->sin6_port == 0) { |
| 748 | VG_(sprintf)(name, "<unbound>"); |
| 749 | } else { |
florian | f44ff62 | 2014-12-20 16:52:08 +0000 | [diff] [blame] | 750 | HChar addr[100]; // large enough |
tom | 3995526 | 2014-02-24 15:06:06 +0000 | [diff] [blame] | 751 | inet6_format(addr, (void *)&(sa->sin6_addr)); |
| 752 | VG_(sprintf)(name, "[%s]:%u", addr, VG_(ntohs)(sa->sin6_port)); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | return name; |
| 756 | } |
| 757 | |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 758 | /* |
| 759 | * Try get some details about a socket. |
| 760 | */ |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 761 | static void |
sewardj | 0a400c7 | 2005-07-23 09:22:46 +0000 | [diff] [blame] | 762 | getsockdetails(Int fd) |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 763 | { |
| 764 | union u { |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 765 | struct vki_sockaddr a; |
| 766 | struct vki_sockaddr_in in; |
tom | 3995526 | 2014-02-24 15:06:06 +0000 | [diff] [blame] | 767 | struct vki_sockaddr_in6 in6; |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 768 | struct vki_sockaddr_un un; |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 769 | } laddr; |
florian | 1636d33 | 2012-11-15 04:27:04 +0000 | [diff] [blame] | 770 | Int llen; |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 771 | |
| 772 | llen = sizeof(laddr); |
| 773 | VG_(memset)(&laddr, 0, llen); |
| 774 | |
| 775 | if(VG_(getsockname)(fd, (struct vki_sockaddr *)&(laddr.a), &llen) != -1) { |
| 776 | switch(laddr.a.sa_family) { |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 777 | case VKI_AF_INET: { |
florian | f44ff62 | 2014-12-20 16:52:08 +0000 | [diff] [blame] | 778 | HChar lname[32]; // large enough |
| 779 | HChar pname[32]; // large enough |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 780 | struct vki_sockaddr_in paddr; |
florian | 1636d33 | 2012-11-15 04:27:04 +0000 | [diff] [blame] | 781 | Int plen = sizeof(struct vki_sockaddr_in); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 782 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 783 | if (VG_(getpeername)(fd, (struct vki_sockaddr *)&paddr, &plen) != -1) { |
| 784 | VG_(message)(Vg_UserMsg, "Open AF_INET socket %d: %s <-> %s\n", fd, |
tom | 3995526 | 2014-02-24 15:06:06 +0000 | [diff] [blame] | 785 | inet_to_name(&(laddr.in), llen, lname), |
| 786 | inet_to_name(&paddr, plen, pname)); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 787 | } else { |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 788 | VG_(message)(Vg_UserMsg, "Open AF_INET socket %d: %s <-> unbound\n", |
tom | 3995526 | 2014-02-24 15:06:06 +0000 | [diff] [blame] | 789 | fd, inet_to_name(&(laddr.in), llen, lname)); |
| 790 | } |
| 791 | return; |
| 792 | } |
| 793 | case VKI_AF_INET6: { |
florian | f44ff62 | 2014-12-20 16:52:08 +0000 | [diff] [blame] | 794 | HChar lname[128]; // large enough |
| 795 | HChar pname[128]; // large enough |
tom | 3995526 | 2014-02-24 15:06:06 +0000 | [diff] [blame] | 796 | struct vki_sockaddr_in6 paddr; |
| 797 | Int plen = sizeof(struct vki_sockaddr_in6); |
| 798 | |
| 799 | if (VG_(getpeername)(fd, (struct vki_sockaddr *)&paddr, &plen) != -1) { |
| 800 | VG_(message)(Vg_UserMsg, "Open AF_INET6 socket %d: %s <-> %s\n", fd, |
| 801 | inet6_to_name(&(laddr.in6), llen, lname), |
| 802 | inet6_to_name(&paddr, plen, pname)); |
| 803 | } else { |
| 804 | VG_(message)(Vg_UserMsg, "Open AF_INET6 socket %d: %s <-> unbound\n", |
| 805 | fd, inet6_to_name(&(laddr.in6), llen, lname)); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 806 | } |
| 807 | return; |
| 808 | } |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 809 | case VKI_AF_UNIX: { |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 810 | static char lname[256]; |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 811 | VG_(message)(Vg_UserMsg, "Open AF_UNIX socket %d: %s\n", fd, |
tom | 3995526 | 2014-02-24 15:06:06 +0000 | [diff] [blame] | 812 | unix_to_name(&(laddr.un), llen, lname)); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 813 | return; |
| 814 | } |
| 815 | default: |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 816 | VG_(message)(Vg_UserMsg, "Open pf-%d socket %d:\n", |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 817 | laddr.a.sa_family, fd); |
| 818 | return; |
| 819 | } |
| 820 | } |
| 821 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 822 | VG_(message)(Vg_UserMsg, "Open socket %d:\n", fd); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 826 | /* Dump out a summary, and a more detailed list, of open file descriptors. */ |
florian | e543f30 | 2012-10-21 19:43:43 +0000 | [diff] [blame] | 827 | void VG_(show_open_fds) (const HChar* when) |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 828 | { |
| 829 | OpenFd *i = allocated_fds; |
| 830 | |
philippe | c336038 | 2012-10-21 14:37:14 +0000 | [diff] [blame] | 831 | VG_(message)(Vg_UserMsg, "FILE DESCRIPTORS: %d open %s.\n", fd_count, when); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 832 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 833 | while (i) { |
| 834 | if (i->pathname) { |
| 835 | VG_(message)(Vg_UserMsg, "Open file descriptor %d: %s\n", i->fd, |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 836 | i->pathname); |
| 837 | } else { |
sewardj | 0a400c7 | 2005-07-23 09:22:46 +0000 | [diff] [blame] | 838 | Int val; |
florian | 1636d33 | 2012-11-15 04:27:04 +0000 | [diff] [blame] | 839 | Int len = sizeof(val); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 840 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 841 | if (VG_(getsockopt)(i->fd, VKI_SOL_SOCKET, VKI_SO_TYPE, &val, &len) |
| 842 | == -1) { |
| 843 | VG_(message)(Vg_UserMsg, "Open file descriptor %d:\n", i->fd); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 844 | } else { |
| 845 | getsockdetails(i->fd); |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | if(i->where) { |
| 850 | VG_(pp_ExeContext)(i->where); |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 851 | VG_(message)(Vg_UserMsg, "\n"); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 852 | } else { |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 853 | VG_(message)(Vg_UserMsg, " <inherited from parent>\n"); |
| 854 | VG_(message)(Vg_UserMsg, "\n"); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | i = i->next; |
| 858 | } |
| 859 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 860 | VG_(message)(Vg_UserMsg, "\n"); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 861 | } |
| 862 | |
njn | 81b975c | 2009-04-28 05:35:53 +0000 | [diff] [blame] | 863 | /* If /proc/self/fd doesn't exist (e.g. you've got a Linux kernel that doesn't |
| 864 | have /proc support compiled in, or a non-Linux kernel), then we need to |
| 865 | find out what file descriptors we inherited from our parent process the |
| 866 | hard way - by checking each fd in turn. */ |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 867 | static |
njn | 81b975c | 2009-04-28 05:35:53 +0000 | [diff] [blame] | 868 | void init_preopened_fds_without_proc_self_fd(void) |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 869 | { |
| 870 | struct vki_rlimit lim; |
sewardj | 0a400c7 | 2005-07-23 09:22:46 +0000 | [diff] [blame] | 871 | UInt count; |
| 872 | Int i; |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 873 | |
nethercote | 620154f | 2004-11-12 21:21:07 +0000 | [diff] [blame] | 874 | if (VG_(getrlimit) (VKI_RLIMIT_NOFILE, &lim) == -1) { |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 875 | /* Hmm. getrlimit() failed. Now we're screwed, so just choose |
| 876 | an arbitrarily high number. 1024 happens to be the limit in |
njn | 81b975c | 2009-04-28 05:35:53 +0000 | [diff] [blame] | 877 | the 2.4 Linux kernels. */ |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 878 | count = 1024; |
| 879 | } else { |
| 880 | count = lim.rlim_cur; |
| 881 | } |
| 882 | |
| 883 | for (i = 0; i < count; i++) |
njn | 81b975c | 2009-04-28 05:35:53 +0000 | [diff] [blame] | 884 | if (VG_(fcntl)(i, VKI_F_GETFL, 0) != -1) |
| 885 | ML_(record_fd_open_named)(-1, i); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | /* Initialize the list of open file descriptors with the file descriptors |
| 889 | we inherited from out parent process. */ |
| 890 | |
sewardj | 1aa4021 | 2006-12-11 01:11:02 +0000 | [diff] [blame] | 891 | void VG_(init_preopened_fds)(void) |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 892 | { |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 893 | // DDD: should probably use HAVE_PROC here or similar, instead. |
njn | 81b975c | 2009-04-28 05:35:53 +0000 | [diff] [blame] | 894 | #if defined(VGO_linux) |
sewardj | 0a400c7 | 2005-07-23 09:22:46 +0000 | [diff] [blame] | 895 | Int ret; |
mjw | 495c656 | 2014-08-29 14:28:30 +0000 | [diff] [blame] | 896 | struct vki_dirent64 d; |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 897 | SysRes f; |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 898 | |
| 899 | f = VG_(open)("/proc/self/fd", VKI_O_RDONLY, 0); |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 900 | if (sr_isError(f)) { |
njn | 81b975c | 2009-04-28 05:35:53 +0000 | [diff] [blame] | 901 | init_preopened_fds_without_proc_self_fd(); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 902 | return; |
| 903 | } |
| 904 | |
mjw | 495c656 | 2014-08-29 14:28:30 +0000 | [diff] [blame] | 905 | while ((ret = VG_(getdents64)(sr_Res(f), &d, sizeof(d))) != 0) { |
njn | f845f8f | 2005-06-23 02:26:47 +0000 | [diff] [blame] | 906 | if (ret == -1) |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 907 | goto out; |
| 908 | |
njn | f845f8f | 2005-06-23 02:26:47 +0000 | [diff] [blame] | 909 | if (VG_(strcmp)(d.d_name, ".") && VG_(strcmp)(d.d_name, "..")) { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 910 | HChar* s; |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 911 | Int fno = VG_(strtoll10)(d.d_name, &s); |
| 912 | if (*s == '\0') { |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 913 | if (fno != sr_Res(f)) |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 914 | if (VG_(clo_track_fds)) |
| 915 | ML_(record_fd_open_named)(-1, fno); |
| 916 | } else { |
| 917 | VG_(message)(Vg_DebugMsg, |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 918 | "Warning: invalid file name in /proc/self/fd: %s\n", |
| 919 | d.d_name); |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 920 | } |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 921 | } |
| 922 | |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 923 | VG_(lseek)(sr_Res(f), d.d_off, VKI_SEEK_SET); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 924 | } |
| 925 | |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 926 | out: |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 927 | VG_(close)(sr_Res(f)); |
njn | 81b975c | 2009-04-28 05:35:53 +0000 | [diff] [blame] | 928 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 929 | #elif defined(VGO_darwin) |
| 930 | init_preopened_fds_without_proc_self_fd(); |
| 931 | |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 932 | #elif defined(VGO_solaris) |
| 933 | Int ret; |
| 934 | Char buf[VKI_MAXGETDENTS_SIZE]; |
| 935 | SysRes f; |
| 936 | |
| 937 | f = VG_(open)("/proc/self/fd", VKI_O_RDONLY, 0); |
| 938 | if (sr_isError(f)) { |
| 939 | init_preopened_fds_without_proc_self_fd(); |
| 940 | return; |
| 941 | } |
| 942 | |
| 943 | while ((ret = VG_(getdents64)(sr_Res(f), (struct vki_dirent64 *) buf, |
| 944 | sizeof(buf))) > 0) { |
| 945 | Int i = 0; |
| 946 | while (i < ret) { |
| 947 | /* Proceed one entry. */ |
| 948 | struct vki_dirent64 *d = (struct vki_dirent64 *) (buf + i); |
| 949 | if (VG_(strcmp)(d->d_name, ".") && VG_(strcmp)(d->d_name, "..")) { |
| 950 | HChar *s; |
| 951 | Int fno = VG_(strtoll10)(d->d_name, &s); |
| 952 | if (*s == '\0') { |
| 953 | if (fno != sr_Res(f)) |
| 954 | if (VG_(clo_track_fds)) |
| 955 | ML_(record_fd_open_named)(-1, fno); |
| 956 | } else { |
| 957 | VG_(message)(Vg_DebugMsg, |
| 958 | "Warning: invalid file name in /proc/self/fd: %s\n", |
| 959 | d->d_name); |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | /* Move on the next entry. */ |
| 964 | i += d->d_reclen; |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | VG_(close)(sr_Res(f)); |
| 969 | |
njn | 81b975c | 2009-04-28 05:35:53 +0000 | [diff] [blame] | 970 | #else |
| 971 | # error Unknown OS |
| 972 | #endif |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 973 | } |
| 974 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 975 | static |
tom | 56e1c84 | 2005-07-18 22:41:33 +0000 | [diff] [blame] | 976 | void pre_mem_read_sendmsg ( ThreadId tid, Bool read, |
florian | e543f30 | 2012-10-21 19:43:43 +0000 | [diff] [blame] | 977 | const HChar *msg, Addr base, SizeT size ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 978 | { |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 979 | HChar outmsg[VG_(strlen)(msg) + 10]; // large enough |
| 980 | VG_(sprintf)(outmsg, "sendmsg%s", msg); |
nethercote | ef0c766 | 2004-11-06 15:38:43 +0000 | [diff] [blame] | 981 | PRE_MEM_READ( outmsg, base, size ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | static |
tom | 56e1c84 | 2005-07-18 22:41:33 +0000 | [diff] [blame] | 985 | void pre_mem_write_recvmsg ( ThreadId tid, Bool read, |
florian | e543f30 | 2012-10-21 19:43:43 +0000 | [diff] [blame] | 986 | const HChar *msg, Addr base, SizeT size ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 987 | { |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 988 | HChar outmsg[VG_(strlen)(msg) + 10]; // large enough |
| 989 | VG_(sprintf)(outmsg, "recvmsg%s", msg); |
tom | 56e1c84 | 2005-07-18 22:41:33 +0000 | [diff] [blame] | 990 | if ( read ) |
| 991 | PRE_MEM_READ( outmsg, base, size ); |
| 992 | else |
| 993 | PRE_MEM_WRITE( outmsg, base, size ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | static |
tom | 56e1c84 | 2005-07-18 22:41:33 +0000 | [diff] [blame] | 997 | void post_mem_write_recvmsg ( ThreadId tid, Bool read, |
florian | e543f30 | 2012-10-21 19:43:43 +0000 | [diff] [blame] | 998 | const HChar *fieldName, Addr base, SizeT size ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 999 | { |
tom | 56e1c84 | 2005-07-18 22:41:33 +0000 | [diff] [blame] | 1000 | if ( !read ) |
| 1001 | POST_MEM_WRITE( base, size ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | static |
sewardj | 8c82451 | 2002-04-14 04:16:48 +0000 | [diff] [blame] | 1005 | void msghdr_foreachfield ( |
tom | a499123 | 2012-02-10 11:30:09 +0000 | [diff] [blame] | 1006 | ThreadId tid, |
florian | e543f30 | 2012-10-21 19:43:43 +0000 | [diff] [blame] | 1007 | const HChar *name, |
tom | 8b3a609 | 2012-02-10 16:45:01 +0000 | [diff] [blame] | 1008 | struct vki_msghdr *msg, |
| 1009 | UInt length, |
tom | fc75e5e | 2013-02-28 12:50:55 +0000 | [diff] [blame] | 1010 | void (*foreach_func)( ThreadId, Bool, const HChar *, Addr, SizeT ), |
sewardj | 6822be8 | 2014-06-20 14:07:38 +0000 | [diff] [blame] | 1011 | Bool rekv /* "recv" apparently shadows some header decl on OSX108 */ |
sewardj | 8c82451 | 2002-04-14 04:16:48 +0000 | [diff] [blame] | 1012 | ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1013 | { |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1014 | HChar fieldName[VG_(strlen)(name) + 32]; // large enough. |
| 1015 | Addr a; |
| 1016 | SizeT s; |
tom | a499123 | 2012-02-10 11:30:09 +0000 | [diff] [blame] | 1017 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1018 | if ( !msg ) |
| 1019 | return; |
| 1020 | |
tom | a499123 | 2012-02-10 11:30:09 +0000 | [diff] [blame] | 1021 | VG_(sprintf) ( fieldName, "(%s)", name ); |
| 1022 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1023 | /* FIELDPAIR helps the compiler do one call to foreach_func |
| 1024 | for consecutive (no holes) fields. */ |
| 1025 | #define FIELDPAIR(f1,f2) \ |
| 1026 | if (offsetof(struct vki_msghdr, f1) + sizeof(msg->f1) \ |
| 1027 | == offsetof(struct vki_msghdr, f2)) \ |
| 1028 | s += sizeof(msg->f2); \ |
| 1029 | else { \ |
| 1030 | foreach_func (tid, True, fieldName, a, s); \ |
| 1031 | a = (Addr)&msg->f2; \ |
| 1032 | s = sizeof(msg->f2); \ |
| 1033 | } |
| 1034 | |
| 1035 | a = (Addr)&msg->msg_name; |
| 1036 | s = sizeof(msg->msg_name); |
| 1037 | FIELDPAIR(msg_name, msg_namelen); |
| 1038 | FIELDPAIR(msg_namelen, msg_iov); |
| 1039 | FIELDPAIR(msg_iov, msg_iovlen); |
| 1040 | FIELDPAIR(msg_iovlen, msg_control); |
| 1041 | FIELDPAIR(msg_control, msg_controllen); |
| 1042 | foreach_func ( tid, True, fieldName, a, s); |
| 1043 | #undef FIELDPAIR |
tom | fc75e5e | 2013-02-28 12:50:55 +0000 | [diff] [blame] | 1044 | |
| 1045 | /* msg_flags is completely ignored for send_mesg, recv_mesg doesn't read |
| 1046 | the field, but does write to it. */ |
sewardj | 6822be8 | 2014-06-20 14:07:38 +0000 | [diff] [blame] | 1047 | if ( rekv ) |
tom | fc75e5e | 2013-02-28 12:50:55 +0000 | [diff] [blame] | 1048 | foreach_func ( tid, False, fieldName, (Addr)&msg->msg_flags, sizeof( msg->msg_flags ) ); |
tom | a499123 | 2012-02-10 11:30:09 +0000 | [diff] [blame] | 1049 | |
mjw | aa02f66 | 2014-05-14 11:35:54 +0000 | [diff] [blame] | 1050 | if ( ML_(safe_to_deref)(&msg->msg_name, sizeof (void *)) |
| 1051 | && msg->msg_name ) { |
tom | a499123 | 2012-02-10 11:30:09 +0000 | [diff] [blame] | 1052 | VG_(sprintf) ( fieldName, "(%s.msg_name)", name ); |
| 1053 | foreach_func ( tid, False, fieldName, |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1054 | (Addr)msg->msg_name, msg->msg_namelen ); |
tom | a499123 | 2012-02-10 11:30:09 +0000 | [diff] [blame] | 1055 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1056 | |
mjw | aa02f66 | 2014-05-14 11:35:54 +0000 | [diff] [blame] | 1057 | if ( ML_(safe_to_deref)(&msg->msg_iov, sizeof (void *)) |
| 1058 | && msg->msg_iov ) { |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 1059 | struct vki_iovec *iov = msg->msg_iov; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1060 | UInt i; |
| 1061 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame] | 1062 | if (ML_(safe_to_deref)(&msg->msg_iovlen, sizeof (UInt))) { |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1063 | VG_(sprintf) ( fieldName, "(%s.msg_iov)", name ); |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame] | 1064 | foreach_func ( tid, True, fieldName, (Addr)iov, |
| 1065 | msg->msg_iovlen * sizeof( struct vki_iovec ) ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1066 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame] | 1067 | for ( i = 0; i < msg->msg_iovlen && length > 0; ++i, ++iov ) { |
| 1068 | if (ML_(safe_to_deref)(&iov->iov_len, sizeof (UInt))) { |
| 1069 | UInt iov_len = iov->iov_len <= length ? iov->iov_len : length; |
| 1070 | VG_(sprintf) ( fieldName, "(%s.msg_iov[%u])", name, i ); |
| 1071 | foreach_func ( tid, False, fieldName, |
| 1072 | (Addr)iov->iov_base, iov_len ); |
| 1073 | length = length - iov_len; |
| 1074 | } |
| 1075 | } |
tom | a499123 | 2012-02-10 11:30:09 +0000 | [diff] [blame] | 1076 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
mjw | aa02f66 | 2014-05-14 11:35:54 +0000 | [diff] [blame] | 1079 | if ( ML_(safe_to_deref) (&msg->msg_control, sizeof (void *)) |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1080 | && msg->msg_control ) { |
tom | a499123 | 2012-02-10 11:30:09 +0000 | [diff] [blame] | 1081 | VG_(sprintf) ( fieldName, "(%s.msg_control)", name ); |
| 1082 | foreach_func ( tid, False, fieldName, |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1083 | (Addr)msg->msg_control, msg->msg_controllen ); |
tom | a499123 | 2012-02-10 11:30:09 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1088 | static void check_cmsg_for_fds(ThreadId tid, struct vki_msghdr *msg) |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 1089 | { |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 1090 | struct vki_cmsghdr *cm = VKI_CMSG_FIRSTHDR(msg); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 1091 | |
| 1092 | while (cm) { |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1093 | if (cm->cmsg_level == VKI_SOL_SOCKET |
| 1094 | && cm->cmsg_type == VKI_SCM_RIGHTS ) { |
sewardj | 0a400c7 | 2005-07-23 09:22:46 +0000 | [diff] [blame] | 1095 | Int *fds = (Int *) VKI_CMSG_DATA(cm); |
| 1096 | Int fdc = (cm->cmsg_len - VKI_CMSG_ALIGN(sizeof(struct vki_cmsghdr))) |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 1097 | / sizeof(int); |
sewardj | 0a400c7 | 2005-07-23 09:22:46 +0000 | [diff] [blame] | 1098 | Int i; |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 1099 | |
| 1100 | for (i = 0; i < fdc; i++) |
| 1101 | if(VG_(clo_track_fds)) |
nethercote | 493dd18 | 2004-02-24 23:57:47 +0000 | [diff] [blame] | 1102 | // XXX: must we check the range on these fds with |
cerion | 7b2c38c | 2005-06-23 07:52:54 +0000 | [diff] [blame] | 1103 | // ML_(fd_allowed)()? |
njn | 096ccdd | 2009-02-22 23:00:30 +0000 | [diff] [blame] | 1104 | ML_(record_fd_open_named)(tid, fds[i]); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 1107 | cm = VKI_CMSG_NXTHDR(msg, cm); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 1108 | } |
| 1109 | } |
| 1110 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1111 | /* GrP kernel ignores sa_len (at least on Darwin); this checks the rest */ |
sewardj | c483e8f | 2002-05-03 21:01:35 +0000 | [diff] [blame] | 1112 | static |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1113 | void pre_mem_read_sockaddr ( ThreadId tid, |
florian | e543f30 | 2012-10-21 19:43:43 +0000 | [diff] [blame] | 1114 | const HChar *description, |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1115 | struct vki_sockaddr *sa, UInt salen ) |
sewardj | c483e8f | 2002-05-03 21:01:35 +0000 | [diff] [blame] | 1116 | { |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1117 | HChar outmsg[VG_(strlen)( description ) + 30]; // large enough |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 1118 | struct vki_sockaddr_un* saun = (struct vki_sockaddr_un *)sa; |
njn | ff42447 | 2009-05-23 01:18:44 +0000 | [diff] [blame] | 1119 | struct vki_sockaddr_in* sin = (struct vki_sockaddr_in *)sa; |
| 1120 | struct vki_sockaddr_in6* sin6 = (struct vki_sockaddr_in6 *)sa; |
sewardj | 19ce5d5 | 2014-08-06 19:52:12 +0000 | [diff] [blame] | 1121 | # ifdef VKI_AF_BLUETOOTH |
mjw | ada5ad7 | 2013-05-22 10:21:10 +0000 | [diff] [blame] | 1122 | struct vki_sockaddr_rc* rc = (struct vki_sockaddr_rc *)sa; |
sewardj | 19ce5d5 | 2014-08-06 19:52:12 +0000 | [diff] [blame] | 1123 | # endif |
| 1124 | # ifdef VKI_AF_NETLINK |
| 1125 | struct vki_sockaddr_nl* nl = (struct vki_sockaddr_nl *)sa; |
| 1126 | # endif |
sewardj | ff7c1ab | 2003-02-24 21:55:34 +0000 | [diff] [blame] | 1127 | |
| 1128 | /* NULL/zero-length sockaddrs are legal */ |
| 1129 | if ( sa == NULL || salen == 0 ) return; |
| 1130 | |
njn | ff42447 | 2009-05-23 01:18:44 +0000 | [diff] [blame] | 1131 | VG_(sprintf) ( outmsg, description, "sa_family" ); |
nethercote | ef0c766 | 2004-11-06 15:38:43 +0000 | [diff] [blame] | 1132 | PRE_MEM_READ( outmsg, (Addr) &sa->sa_family, sizeof(vki_sa_family_t)); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1133 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame] | 1134 | /* Don't do any extra checking if we cannot determine the sa_family. */ |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1135 | if (! ML_(safe_to_deref) (&sa->sa_family, sizeof(vki_sa_family_t))) |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame] | 1136 | return; |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame] | 1137 | |
sewardj | c483e8f | 2002-05-03 21:01:35 +0000 | [diff] [blame] | 1138 | switch (sa->sa_family) { |
| 1139 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 1140 | case VKI_AF_UNIX: |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame] | 1141 | if (ML_(safe_to_deref) (&saun->sun_path, sizeof (Addr))) { |
| 1142 | VG_(sprintf) ( outmsg, description, "sun_path" ); |
| 1143 | PRE_MEM_RASCIIZ( outmsg, (Addr) saun->sun_path ); |
| 1144 | // GrP fixme max of sun_len-2? what about nul char? |
| 1145 | } |
sewardj | c483e8f | 2002-05-03 21:01:35 +0000 | [diff] [blame] | 1146 | break; |
| 1147 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 1148 | case VKI_AF_INET: |
njn | ff42447 | 2009-05-23 01:18:44 +0000 | [diff] [blame] | 1149 | VG_(sprintf) ( outmsg, description, "sin_port" ); |
| 1150 | PRE_MEM_READ( outmsg, (Addr) &sin->sin_port, sizeof (sin->sin_port) ); |
| 1151 | VG_(sprintf) ( outmsg, description, "sin_addr" ); |
| 1152 | PRE_MEM_READ( outmsg, (Addr) &sin->sin_addr, sizeof (sin->sin_addr) ); |
sewardj | c483e8f | 2002-05-03 21:01:35 +0000 | [diff] [blame] | 1153 | break; |
| 1154 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 1155 | case VKI_AF_INET6: |
njn | ff42447 | 2009-05-23 01:18:44 +0000 | [diff] [blame] | 1156 | VG_(sprintf) ( outmsg, description, "sin6_port" ); |
nethercote | ef0c766 | 2004-11-06 15:38:43 +0000 | [diff] [blame] | 1157 | PRE_MEM_READ( outmsg, |
njn | ff42447 | 2009-05-23 01:18:44 +0000 | [diff] [blame] | 1158 | (Addr) &sin6->sin6_port, sizeof (sin6->sin6_port) ); |
| 1159 | VG_(sprintf) ( outmsg, description, "sin6_flowinfo" ); |
nethercote | ef0c766 | 2004-11-06 15:38:43 +0000 | [diff] [blame] | 1160 | PRE_MEM_READ( outmsg, |
njn | ff42447 | 2009-05-23 01:18:44 +0000 | [diff] [blame] | 1161 | (Addr) &sin6->sin6_flowinfo, sizeof (sin6->sin6_flowinfo) ); |
| 1162 | VG_(sprintf) ( outmsg, description, "sin6_addr" ); |
nethercote | ef0c766 | 2004-11-06 15:38:43 +0000 | [diff] [blame] | 1163 | PRE_MEM_READ( outmsg, |
njn | ff42447 | 2009-05-23 01:18:44 +0000 | [diff] [blame] | 1164 | (Addr) &sin6->sin6_addr, sizeof (sin6->sin6_addr) ); |
| 1165 | VG_(sprintf) ( outmsg, description, "sin6_scope_id" ); |
nethercote | ef0c766 | 2004-11-06 15:38:43 +0000 | [diff] [blame] | 1166 | PRE_MEM_READ( outmsg, |
njn | ff42447 | 2009-05-23 01:18:44 +0000 | [diff] [blame] | 1167 | (Addr) &sin6->sin6_scope_id, sizeof (sin6->sin6_scope_id) ); |
sewardj | c483e8f | 2002-05-03 21:01:35 +0000 | [diff] [blame] | 1168 | break; |
mjw | 4cb663f | 2013-05-22 10:37:52 +0000 | [diff] [blame] | 1169 | |
sewardj | 19ce5d5 | 2014-08-06 19:52:12 +0000 | [diff] [blame] | 1170 | # ifdef VKI_AF_BLUETOOTH |
mjw | ada5ad7 | 2013-05-22 10:21:10 +0000 | [diff] [blame] | 1171 | case VKI_AF_BLUETOOTH: |
| 1172 | VG_(sprintf) ( outmsg, description, "rc_bdaddr" ); |
| 1173 | PRE_MEM_READ( outmsg, (Addr) &rc->rc_bdaddr, sizeof (rc->rc_bdaddr) ); |
| 1174 | VG_(sprintf) ( outmsg, description, "rc_channel" ); |
| 1175 | PRE_MEM_READ( outmsg, (Addr) &rc->rc_channel, sizeof (rc->rc_channel) ); |
| 1176 | break; |
sewardj | 19ce5d5 | 2014-08-06 19:52:12 +0000 | [diff] [blame] | 1177 | # endif |
| 1178 | |
| 1179 | # ifdef VKI_AF_NETLINK |
| 1180 | case VKI_AF_NETLINK: |
| 1181 | VG_(sprintf)(outmsg, description, "nl_pid"); |
| 1182 | PRE_MEM_READ(outmsg, (Addr)&nl->nl_pid, sizeof(nl->nl_pid)); |
| 1183 | VG_(sprintf)(outmsg, description, "nl_groups"); |
| 1184 | PRE_MEM_READ(outmsg, (Addr)&nl->nl_groups, sizeof(nl->nl_groups)); |
| 1185 | break; |
| 1186 | # endif |
mjw | ada5ad7 | 2013-05-22 10:21:10 +0000 | [diff] [blame] | 1187 | |
sewardj | aa62d84 | 2014-08-20 17:45:00 +0000 | [diff] [blame] | 1188 | # ifdef VKI_AF_UNSPEC |
| 1189 | case VKI_AF_UNSPEC: |
| 1190 | break; |
| 1191 | # endif |
| 1192 | |
sewardj | c483e8f | 2002-05-03 21:01:35 +0000 | [diff] [blame] | 1193 | default: |
philippe | c06f684 | 2014-07-30 22:20:29 +0000 | [diff] [blame] | 1194 | /* No specific information about this address family. |
| 1195 | Let's just check the full data following the family. |
| 1196 | Note that this can give false positive if this (unknown) |
| 1197 | struct sockaddr_???? has padding bytes between its elements. */ |
| 1198 | VG_(sprintf) ( outmsg, description, "sa_data" ); |
| 1199 | PRE_MEM_READ( outmsg, (Addr)&sa->sa_family + sizeof(sa->sa_family), |
philippe | 90a7073 | 2014-12-29 18:24:37 +0000 | [diff] [blame] | 1200 | salen - sizeof(sa->sa_family)); |
sewardj | c483e8f | 2002-05-03 21:01:35 +0000 | [diff] [blame] | 1201 | break; |
| 1202 | } |
sewardj | c483e8f | 2002-05-03 21:01:35 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1205 | /* Dereference a pointer to a UInt. */ |
florian | e543f30 | 2012-10-21 19:43:43 +0000 | [diff] [blame] | 1206 | static UInt deref_UInt ( ThreadId tid, Addr a, const HChar* s ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1207 | { |
| 1208 | UInt* a_p = (UInt*)a; |
nethercote | ef0c766 | 2004-11-06 15:38:43 +0000 | [diff] [blame] | 1209 | PRE_MEM_READ( s, (Addr)a_p, sizeof(UInt) ); |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame] | 1210 | if (a_p == NULL || ! ML_(safe_to_deref) (a_p, sizeof(UInt))) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1211 | return 0; |
| 1212 | else |
| 1213 | return *a_p; |
| 1214 | } |
| 1215 | |
njn | f37a81b | 2009-02-17 00:23:30 +0000 | [diff] [blame] | 1216 | void ML_(buf_and_len_pre_check) ( ThreadId tid, Addr buf_p, Addr buflen_p, |
florian | e543f30 | 2012-10-21 19:43:43 +0000 | [diff] [blame] | 1217 | const HChar* buf_s, const HChar* buflen_s ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1218 | { |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1219 | if (VG_(tdict).track_pre_mem_write) { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1220 | UInt buflen_in = deref_UInt( tid, buflen_p, buflen_s); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1221 | if (buflen_in > 0) { |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 1222 | VG_(tdict).track_pre_mem_write( |
| 1223 | Vg_CoreSysCall, tid, buf_s, buf_p, buflen_in ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1224 | } |
| 1225 | } |
| 1226 | } |
| 1227 | |
njn | f37a81b | 2009-02-17 00:23:30 +0000 | [diff] [blame] | 1228 | void ML_(buf_and_len_post_check) ( ThreadId tid, SysRes res, |
florian | e543f30 | 2012-10-21 19:43:43 +0000 | [diff] [blame] | 1229 | Addr buf_p, Addr buflen_p, const HChar* s ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1230 | { |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 1231 | if (!sr_isError(res) && VG_(tdict).track_post_mem_write) { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1232 | UInt buflen_out = deref_UInt( tid, buflen_p, s); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1233 | if (buflen_out > 0 && buf_p != (Addr)NULL) { |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1234 | VG_(tdict).track_post_mem_write( Vg_CoreSysCall, tid, buf_p, buflen_out ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1235 | } |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | /* --------------------------------------------------------------------- |
| 1240 | Data seg end, for brk() |
| 1241 | ------------------------------------------------------------------ */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1242 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1243 | /* +--------+------------+ |
| 1244 | | anon | resvn | |
| 1245 | +--------+------------+ |
nethercote | ce47126 | 2004-08-25 13:43:44 +0000 | [diff] [blame] | 1246 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1247 | ^ ^ ^ |
| 1248 | | | boundary is page aligned |
| 1249 | | VG_(brk_limit) -- no alignment constraint |
| 1250 | VG_(brk_base) -- page aligned -- does not move |
| 1251 | |
| 1252 | Both the anon part and the reservation part are always at least |
| 1253 | one page. |
| 1254 | */ |
| 1255 | |
| 1256 | /* Set the new data segment end to NEWBRK. If this succeeds, return |
| 1257 | NEWBRK, else return the current data segment end. */ |
| 1258 | |
florian | 981abb5 | 2015-04-29 12:59:16 +0000 | [diff] [blame] | 1259 | static Addr do_brk ( Addr newbrk, ThreadId tid ) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1260 | { |
sewardj | ef1cf8b | 2006-10-17 01:47:30 +0000 | [diff] [blame] | 1261 | NSegment const* aseg; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1262 | Addr newbrkP; |
| 1263 | SizeT delta; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1264 | Bool debug = False; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1265 | |
| 1266 | if (debug) |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 1267 | VG_(printf)("\ndo_brk: brk_base=%#lx brk_limit=%#lx newbrk=%#lx\n", |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1268 | VG_(brk_base), VG_(brk_limit), newbrk); |
| 1269 | |
florian | 95f1037 | 2015-02-26 21:48:19 +0000 | [diff] [blame] | 1270 | if (0) VG_(am_show_nsegments)(0, "in_brk"); |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 1271 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1272 | if (newbrk < VG_(brk_base)) |
| 1273 | /* Clearly impossible. */ |
| 1274 | goto bad; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1275 | |
florian | 95f1037 | 2015-02-26 21:48:19 +0000 | [diff] [blame] | 1276 | if (newbrk < VG_(brk_limit)) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1277 | /* shrinking the data segment. Be lazy and don't munmap the |
| 1278 | excess area. */ |
sewardj | ef1cf8b | 2006-10-17 01:47:30 +0000 | [diff] [blame] | 1279 | NSegment const * seg = VG_(am_find_nsegment)(newbrk); |
florian | 95f1037 | 2015-02-26 21:48:19 +0000 | [diff] [blame] | 1280 | vg_assert(seg); |
| 1281 | |
| 1282 | if (seg->hasT) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1283 | VG_(discard_translations)( newbrk, VG_(brk_limit) - newbrk, |
| 1284 | "do_brk(shrink)" ); |
sewardj | d2d241b | 2006-02-14 11:37:41 +0000 | [diff] [blame] | 1285 | /* Since we're being lazy and not unmapping pages, we have to |
| 1286 | zero out the area, so that if the area later comes back into |
| 1287 | circulation, it will be filled with zeroes, as if it really |
| 1288 | had been unmapped and later remapped. Be a bit paranoid and |
| 1289 | try hard to ensure we're not going to segfault by doing the |
| 1290 | write - check both ends of the range are in the same segment |
| 1291 | and that segment is writable. */ |
florian | 95f1037 | 2015-02-26 21:48:19 +0000 | [diff] [blame] | 1292 | NSegment const * seg2; |
| 1293 | |
florian | e787fca | 2015-05-05 06:14:10 +0000 | [diff] [blame] | 1294 | seg2 = VG_(am_find_nsegment)( VG_(brk_limit) - 1 ); |
florian | 95f1037 | 2015-02-26 21:48:19 +0000 | [diff] [blame] | 1295 | vg_assert(seg2); |
| 1296 | |
| 1297 | if (seg == seg2 && seg->hasW) |
| 1298 | VG_(memset)( (void*)newbrk, 0, VG_(brk_limit) - newbrk ); |
sewardj | d2d241b | 2006-02-14 11:37:41 +0000 | [diff] [blame] | 1299 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1300 | VG_(brk_limit) = newbrk; |
| 1301 | return newbrk; |
| 1302 | } |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1303 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1304 | /* otherwise we're expanding the brk segment. */ |
tom | 8e5963d | 2005-10-06 14:49:21 +0000 | [diff] [blame] | 1305 | if (VG_(brk_limit) > VG_(brk_base)) |
| 1306 | aseg = VG_(am_find_nsegment)( VG_(brk_limit)-1 ); |
| 1307 | else |
| 1308 | aseg = VG_(am_find_nsegment)( VG_(brk_limit) ); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1309 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1310 | /* These should be assured by setup_client_dataseg in m_main. */ |
| 1311 | vg_assert(aseg); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1312 | vg_assert(aseg->kind == SkAnonC); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1313 | |
florian | 95f1037 | 2015-02-26 21:48:19 +0000 | [diff] [blame] | 1314 | if (newbrk <= aseg->end + 1) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1315 | /* still fits within the anon segment. */ |
| 1316 | VG_(brk_limit) = newbrk; |
| 1317 | return newbrk; |
| 1318 | } |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1319 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1320 | newbrkP = VG_PGROUNDUP(newbrk); |
florian | 95f1037 | 2015-02-26 21:48:19 +0000 | [diff] [blame] | 1321 | delta = newbrkP - (aseg->end + 1); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1322 | vg_assert(delta > 0); |
| 1323 | vg_assert(VG_IS_PAGE_ALIGNED(delta)); |
| 1324 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1325 | Bool overflow = False; |
florian | 15fa8a2 | 2015-03-03 14:56:17 +0000 | [diff] [blame] | 1326 | if (! VG_(am_extend_into_adjacent_reservation_client)( aseg->start, delta, |
florian | 981abb5 | 2015-04-29 12:59:16 +0000 | [diff] [blame] | 1327 | &overflow)) { |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1328 | if (overflow) { |
| 1329 | static Bool alreadyComplained = False; |
| 1330 | if (!alreadyComplained) { |
| 1331 | alreadyComplained = True; |
| 1332 | if (VG_(clo_verbosity) > 0) { |
| 1333 | VG_(umsg)("brk segment overflow in thread #%u: " |
| 1334 | "can't grow to %#lx\n", |
| 1335 | tid, newbrkP); |
| 1336 | VG_(umsg)("(see section Limitations in user manual)\n"); |
| 1337 | VG_(umsg)("NOTE: further instances of this message " |
| 1338 | "will not be shown\n"); |
| 1339 | } |
| 1340 | } |
| 1341 | } else { |
| 1342 | if (VG_(clo_verbosity) > 0) { |
| 1343 | VG_(umsg)("Cannot map memory to grow brk segment in thread #%u " |
| 1344 | "to %#lx\n", tid, newbrkP); |
| 1345 | VG_(umsg)("(see section Limitations in user manual)\n"); |
| 1346 | } |
| 1347 | } |
florian | 888b815 | 2015-02-26 16:07:12 +0000 | [diff] [blame] | 1348 | goto bad; |
florian | 981abb5 | 2015-04-29 12:59:16 +0000 | [diff] [blame] | 1349 | } |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1350 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1351 | VG_(brk_limit) = newbrk; |
| 1352 | return newbrk; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1353 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1354 | bad: |
| 1355 | return VG_(brk_limit); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1359 | /* --------------------------------------------------------------------- |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1360 | Vet file descriptors for sanity |
| 1361 | ------------------------------------------------------------------ */ |
sewardj | 0423690 | 2005-10-04 22:27:22 +0000 | [diff] [blame] | 1362 | /* |
| 1363 | > - what does the "Bool soft" parameter mean? |
| 1364 | |
| 1365 | (Tom Hughes, 3 Oct 05): |
| 1366 | |
| 1367 | Whether or not to consider a file descriptor invalid if it is above |
| 1368 | the current soft limit. |
| 1369 | |
| 1370 | Basically if we are testing whether a newly created file descriptor is |
| 1371 | valid (in a post handler) then we set soft to true, and if we are |
| 1372 | testing whether a file descriptor that is about to be used (in a pre |
| 1373 | handler) is valid [viz, an already-existing fd] then we set it to false. |
| 1374 | |
| 1375 | The point is that if the (virtual) soft limit is lowered then any |
| 1376 | existing descriptors can still be read/written/closed etc (so long as |
| 1377 | they are below the valgrind reserved descriptors) but no new |
| 1378 | descriptors can be created above the new soft limit. |
| 1379 | |
| 1380 | (jrs 4 Oct 05: in which case, I've renamed it "isNewFd") |
| 1381 | */ |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1382 | |
| 1383 | /* Return true if we're allowed to use or create this fd */ |
florian | e543f30 | 2012-10-21 19:43:43 +0000 | [diff] [blame] | 1384 | Bool ML_(fd_allowed)(Int fd, const HChar *syscallname, ThreadId tid, |
| 1385 | Bool isNewFd) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1386 | { |
sewardj | 0423690 | 2005-10-04 22:27:22 +0000 | [diff] [blame] | 1387 | Bool allowed = True; |
| 1388 | |
| 1389 | /* hard limits always apply */ |
| 1390 | if (fd < 0 || fd >= VG_(fd_hard_limit)) |
| 1391 | allowed = False; |
| 1392 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1393 | /* hijacking the output fds is never allowed */ |
| 1394 | if (fd == VG_(log_output_sink).fd || fd == VG_(xml_output_sink).fd) |
sewardj | 0423690 | 2005-10-04 22:27:22 +0000 | [diff] [blame] | 1395 | allowed = False; |
| 1396 | |
| 1397 | /* if creating a new fd (rather than using an existing one), the |
| 1398 | soft limit must also be observed */ |
| 1399 | if (isNewFd && fd >= VG_(fd_soft_limit)) |
| 1400 | allowed = False; |
| 1401 | |
| 1402 | /* this looks like it ought to be included, but causes problems: */ |
| 1403 | /* |
| 1404 | if (fd == 2 && VG_(debugLog_getLevel)() > 0) |
| 1405 | allowed = False; |
| 1406 | */ |
| 1407 | /* The difficulty is as follows: consider a program P which expects |
| 1408 | to be able to mess with (redirect) its own stderr (fd 2). |
| 1409 | Usually to deal with P we would issue command line flags to send |
| 1410 | logging somewhere other than stderr, so as not to disrupt P. |
| 1411 | The problem is that -d unilaterally hijacks stderr with no |
| 1412 | consultation with P. And so, if this check is enabled, P will |
| 1413 | work OK normally but fail if -d is issued. |
| 1414 | |
| 1415 | Basically -d is a hack and you take your chances when using it. |
| 1416 | It's very useful for low level debugging -- particularly at |
| 1417 | startup -- and having its presence change the behaviour of the |
| 1418 | client is exactly what we don't want. */ |
| 1419 | |
| 1420 | /* croak? */ |
| 1421 | if ((!allowed) && VG_(showing_core_errors)() ) { |
jseward | d9320a4 | 2003-12-12 06:40:05 +0000 | [diff] [blame] | 1422 | VG_(message)(Vg_UserMsg, |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1423 | "Warning: invalid file descriptor %d in syscall %s()\n", |
nethercote | 1543adf | 2004-10-25 15:43:21 +0000 | [diff] [blame] | 1424 | fd, syscallname); |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1425 | if (fd == VG_(log_output_sink).fd && VG_(log_output_sink).fd >= 0) |
jseward | d9320a4 | 2003-12-12 06:40:05 +0000 | [diff] [blame] | 1426 | VG_(message)(Vg_UserMsg, |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1427 | " Use --log-fd=<number> to select an alternative log fd.\n"); |
| 1428 | if (fd == VG_(xml_output_sink).fd && VG_(xml_output_sink).fd >= 0) |
| 1429 | VG_(message)(Vg_UserMsg, |
| 1430 | " Use --xml-fd=<number> to select an alternative XML " |
| 1431 | "output fd.\n"); |
njn | eb04c89 | 2009-05-22 07:09:03 +0000 | [diff] [blame] | 1432 | // DDD: consider always printing this stack trace, it's useful. |
| 1433 | // Also consider also making this a proper core error, ie. |
| 1434 | // suppressible and all that. |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1435 | if (VG_(clo_verbosity) > 1) { |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 1436 | VG_(get_and_pp_StackTrace)(tid, VG_(clo_backtrace_size)); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1437 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1438 | } |
sewardj | 0423690 | 2005-10-04 22:27:22 +0000 | [diff] [blame] | 1439 | |
| 1440 | return allowed; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | |
| 1444 | /* --------------------------------------------------------------------- |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1445 | Deal with a bunch of socket-related syscalls |
| 1446 | ------------------------------------------------------------------ */ |
| 1447 | |
| 1448 | /* ------ */ |
| 1449 | |
sewardj | 987a8eb | 2005-03-01 19:00:30 +0000 | [diff] [blame] | 1450 | void |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 1451 | ML_(generic_PRE_sys_socketpair) ( ThreadId tid, |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1452 | UWord arg0, UWord arg1, |
| 1453 | UWord arg2, UWord arg3 ) |
| 1454 | { |
| 1455 | /* int socketpair(int d, int type, int protocol, int sv[2]); */ |
| 1456 | PRE_MEM_WRITE( "socketcall.socketpair(sv)", |
| 1457 | arg3, 2*sizeof(int) ); |
| 1458 | } |
| 1459 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1460 | SysRes |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 1461 | ML_(generic_POST_sys_socketpair) ( ThreadId tid, |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1462 | SysRes res, |
sewardj | 987a8eb | 2005-03-01 19:00:30 +0000 | [diff] [blame] | 1463 | UWord arg0, UWord arg1, |
| 1464 | UWord arg2, UWord arg3 ) |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1465 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1466 | SysRes r = res; |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1467 | Int fd1 = ((Int*)arg3)[0]; |
| 1468 | Int fd2 = ((Int*)arg3)[1]; |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 1469 | vg_assert(!sr_isError(res)); /* guaranteed by caller */ |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1470 | POST_MEM_WRITE( arg3, 2*sizeof(int) ); |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 1471 | if (!ML_(fd_allowed)(fd1, "socketcall.socketpair", tid, True) || |
| 1472 | !ML_(fd_allowed)(fd2, "socketcall.socketpair", tid, True)) { |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1473 | VG_(close)(fd1); |
| 1474 | VG_(close)(fd2); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1475 | r = VG_(mk_SysRes_Error)( VKI_EMFILE ); |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1476 | } else { |
| 1477 | POST_MEM_WRITE( arg3, 2*sizeof(int) ); |
| 1478 | if (VG_(clo_track_fds)) { |
njn | f845f8f | 2005-06-23 02:26:47 +0000 | [diff] [blame] | 1479 | ML_(record_fd_open_nameless)(tid, fd1); |
| 1480 | ML_(record_fd_open_nameless)(tid, fd2); |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1481 | } |
| 1482 | } |
| 1483 | return r; |
| 1484 | } |
| 1485 | |
| 1486 | /* ------ */ |
| 1487 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1488 | SysRes |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 1489 | ML_(generic_POST_sys_socket) ( ThreadId tid, SysRes res ) |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1490 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1491 | SysRes r = res; |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 1492 | vg_assert(!sr_isError(res)); /* guaranteed by caller */ |
| 1493 | if (!ML_(fd_allowed)(sr_Res(res), "socket", tid, True)) { |
| 1494 | VG_(close)(sr_Res(res)); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1495 | r = VG_(mk_SysRes_Error)( VKI_EMFILE ); |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1496 | } else { |
| 1497 | if (VG_(clo_track_fds)) |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 1498 | ML_(record_fd_open_nameless)(tid, sr_Res(res)); |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1499 | } |
| 1500 | return r; |
| 1501 | } |
| 1502 | |
| 1503 | /* ------ */ |
| 1504 | |
sewardj | 987a8eb | 2005-03-01 19:00:30 +0000 | [diff] [blame] | 1505 | void |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 1506 | ML_(generic_PRE_sys_bind) ( ThreadId tid, |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1507 | UWord arg0, UWord arg1, UWord arg2 ) |
| 1508 | { |
| 1509 | /* int bind(int sockfd, struct sockaddr *my_addr, |
| 1510 | int addrlen); */ |
| 1511 | pre_mem_read_sockaddr( |
| 1512 | tid, "socketcall.bind(my_addr.%s)", |
| 1513 | (struct vki_sockaddr *) arg1, arg2 |
| 1514 | ); |
| 1515 | } |
| 1516 | |
| 1517 | /* ------ */ |
| 1518 | |
sewardj | 987a8eb | 2005-03-01 19:00:30 +0000 | [diff] [blame] | 1519 | void |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 1520 | ML_(generic_PRE_sys_accept) ( ThreadId tid, |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1521 | UWord arg0, UWord arg1, UWord arg2 ) |
| 1522 | { |
| 1523 | /* int accept(int s, struct sockaddr *addr, int *addrlen); */ |
| 1524 | Addr addr_p = arg1; |
| 1525 | Addr addrlen_p = arg2; |
| 1526 | if (addr_p != (Addr)NULL) |
njn | f37a81b | 2009-02-17 00:23:30 +0000 | [diff] [blame] | 1527 | ML_(buf_and_len_pre_check) ( tid, addr_p, addrlen_p, |
| 1528 | "socketcall.accept(addr)", |
| 1529 | "socketcall.accept(addrlen_in)" ); |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1532 | SysRes |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 1533 | ML_(generic_POST_sys_accept) ( ThreadId tid, |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1534 | SysRes res, |
sewardj | 987a8eb | 2005-03-01 19:00:30 +0000 | [diff] [blame] | 1535 | UWord arg0, UWord arg1, UWord arg2 ) |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1536 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1537 | SysRes r = res; |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 1538 | vg_assert(!sr_isError(res)); /* guaranteed by caller */ |
| 1539 | if (!ML_(fd_allowed)(sr_Res(res), "accept", tid, True)) { |
| 1540 | VG_(close)(sr_Res(res)); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1541 | r = VG_(mk_SysRes_Error)( VKI_EMFILE ); |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1542 | } else { |
| 1543 | Addr addr_p = arg1; |
| 1544 | Addr addrlen_p = arg2; |
| 1545 | if (addr_p != (Addr)NULL) |
njn | f37a81b | 2009-02-17 00:23:30 +0000 | [diff] [blame] | 1546 | ML_(buf_and_len_post_check) ( tid, res, addr_p, addrlen_p, |
| 1547 | "socketcall.accept(addrlen_out)" ); |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1548 | if (VG_(clo_track_fds)) |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 1549 | ML_(record_fd_open_nameless)(tid, sr_Res(res)); |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1550 | } |
| 1551 | return r; |
| 1552 | } |
| 1553 | |
| 1554 | /* ------ */ |
| 1555 | |
sewardj | 987a8eb | 2005-03-01 19:00:30 +0000 | [diff] [blame] | 1556 | void |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 1557 | ML_(generic_PRE_sys_sendto) ( ThreadId tid, |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1558 | UWord arg0, UWord arg1, UWord arg2, |
| 1559 | UWord arg3, UWord arg4, UWord arg5 ) |
| 1560 | { |
| 1561 | /* int sendto(int s, const void *msg, int len, |
| 1562 | unsigned int flags, |
| 1563 | const struct sockaddr *to, int tolen); */ |
| 1564 | PRE_MEM_READ( "socketcall.sendto(msg)", |
| 1565 | arg1, /* msg */ |
| 1566 | arg2 /* len */ ); |
| 1567 | pre_mem_read_sockaddr( |
| 1568 | tid, "socketcall.sendto(to.%s)", |
| 1569 | (struct vki_sockaddr *) arg4, arg5 |
| 1570 | ); |
| 1571 | } |
| 1572 | |
| 1573 | /* ------ */ |
| 1574 | |
sewardj | 987a8eb | 2005-03-01 19:00:30 +0000 | [diff] [blame] | 1575 | void |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 1576 | ML_(generic_PRE_sys_send) ( ThreadId tid, |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1577 | UWord arg0, UWord arg1, UWord arg2 ) |
| 1578 | { |
| 1579 | /* int send(int s, const void *msg, size_t len, int flags); */ |
| 1580 | PRE_MEM_READ( "socketcall.send(msg)", |
| 1581 | arg1, /* msg */ |
| 1582 | arg2 /* len */ ); |
| 1583 | |
| 1584 | } |
| 1585 | |
| 1586 | /* ------ */ |
| 1587 | |
sewardj | 987a8eb | 2005-03-01 19:00:30 +0000 | [diff] [blame] | 1588 | void |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 1589 | ML_(generic_PRE_sys_recvfrom) ( ThreadId tid, |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1590 | UWord arg0, UWord arg1, UWord arg2, |
| 1591 | UWord arg3, UWord arg4, UWord arg5 ) |
| 1592 | { |
| 1593 | /* int recvfrom(int s, void *buf, int len, unsigned int flags, |
| 1594 | struct sockaddr *from, int *fromlen); */ |
| 1595 | Addr buf_p = arg1; |
| 1596 | Int len = arg2; |
| 1597 | Addr from_p = arg4; |
| 1598 | Addr fromlen_p = arg5; |
| 1599 | PRE_MEM_WRITE( "socketcall.recvfrom(buf)", buf_p, len ); |
| 1600 | if (from_p != (Addr)NULL) |
njn | f37a81b | 2009-02-17 00:23:30 +0000 | [diff] [blame] | 1601 | ML_(buf_and_len_pre_check) ( tid, from_p, fromlen_p, |
| 1602 | "socketcall.recvfrom(from)", |
| 1603 | "socketcall.recvfrom(fromlen_in)" ); |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
sewardj | 987a8eb | 2005-03-01 19:00:30 +0000 | [diff] [blame] | 1606 | void |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 1607 | ML_(generic_POST_sys_recvfrom) ( ThreadId tid, |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1608 | SysRes res, |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1609 | UWord arg0, UWord arg1, UWord arg2, |
| 1610 | UWord arg3, UWord arg4, UWord arg5 ) |
| 1611 | { |
| 1612 | Addr buf_p = arg1; |
| 1613 | Int len = arg2; |
| 1614 | Addr from_p = arg4; |
| 1615 | Addr fromlen_p = arg5; |
| 1616 | |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 1617 | vg_assert(!sr_isError(res)); /* guaranteed by caller */ |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1618 | if (from_p != (Addr)NULL) |
njn | f37a81b | 2009-02-17 00:23:30 +0000 | [diff] [blame] | 1619 | ML_(buf_and_len_post_check) ( tid, res, from_p, fromlen_p, |
| 1620 | "socketcall.recvfrom(fromlen_out)" ); |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1621 | POST_MEM_WRITE( buf_p, len ); |
| 1622 | } |
| 1623 | |
| 1624 | /* ------ */ |
| 1625 | |
sewardj | 987a8eb | 2005-03-01 19:00:30 +0000 | [diff] [blame] | 1626 | void |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 1627 | ML_(generic_PRE_sys_recv) ( ThreadId tid, |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1628 | UWord arg0, UWord arg1, UWord arg2 ) |
| 1629 | { |
| 1630 | /* int recv(int s, void *buf, int len, unsigned int flags); */ |
| 1631 | /* man 2 recv says: |
| 1632 | The recv call is normally used only on a connected socket |
| 1633 | (see connect(2)) and is identical to recvfrom with a NULL |
| 1634 | from parameter. |
| 1635 | */ |
| 1636 | PRE_MEM_WRITE( "socketcall.recv(buf)", |
| 1637 | arg1, /* buf */ |
| 1638 | arg2 /* len */ ); |
| 1639 | } |
| 1640 | |
sewardj | 987a8eb | 2005-03-01 19:00:30 +0000 | [diff] [blame] | 1641 | void |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 1642 | ML_(generic_POST_sys_recv) ( ThreadId tid, |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1643 | UWord res, |
| 1644 | UWord arg0, UWord arg1, UWord arg2 ) |
| 1645 | { |
| 1646 | if (res >= 0 && arg1 != 0) { |
| 1647 | POST_MEM_WRITE( arg1, /* buf */ |
| 1648 | arg2 /* len */ ); |
| 1649 | } |
| 1650 | } |
| 1651 | |
| 1652 | /* ------ */ |
| 1653 | |
sewardj | 987a8eb | 2005-03-01 19:00:30 +0000 | [diff] [blame] | 1654 | void |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 1655 | ML_(generic_PRE_sys_connect) ( ThreadId tid, |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1656 | UWord arg0, UWord arg1, UWord arg2 ) |
| 1657 | { |
| 1658 | /* int connect(int sockfd, |
| 1659 | struct sockaddr *serv_addr, int addrlen ); */ |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1660 | pre_mem_read_sockaddr( tid, |
| 1661 | "socketcall.connect(serv_addr.%s)", |
| 1662 | (struct vki_sockaddr *) arg1, arg2); |
| 1663 | } |
| 1664 | |
| 1665 | /* ------ */ |
| 1666 | |
sewardj | 987a8eb | 2005-03-01 19:00:30 +0000 | [diff] [blame] | 1667 | void |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 1668 | ML_(generic_PRE_sys_setsockopt) ( ThreadId tid, |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1669 | UWord arg0, UWord arg1, UWord arg2, |
| 1670 | UWord arg3, UWord arg4 ) |
| 1671 | { |
| 1672 | /* int setsockopt(int s, int level, int optname, |
| 1673 | const void *optval, int optlen); */ |
| 1674 | PRE_MEM_READ( "socketcall.setsockopt(optval)", |
| 1675 | arg3, /* optval */ |
| 1676 | arg4 /* optlen */ ); |
| 1677 | } |
| 1678 | |
| 1679 | /* ------ */ |
| 1680 | |
sewardj | 987a8eb | 2005-03-01 19:00:30 +0000 | [diff] [blame] | 1681 | void |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 1682 | ML_(generic_PRE_sys_getsockname) ( ThreadId tid, |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1683 | UWord arg0, UWord arg1, UWord arg2 ) |
| 1684 | { |
| 1685 | /* int getsockname(int s, struct sockaddr* name, int* namelen) */ |
| 1686 | Addr name_p = arg1; |
| 1687 | Addr namelen_p = arg2; |
| 1688 | /* Nb: name_p cannot be NULL */ |
njn | f37a81b | 2009-02-17 00:23:30 +0000 | [diff] [blame] | 1689 | ML_(buf_and_len_pre_check) ( tid, name_p, namelen_p, |
| 1690 | "socketcall.getsockname(name)", |
| 1691 | "socketcall.getsockname(namelen_in)" ); |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
sewardj | 987a8eb | 2005-03-01 19:00:30 +0000 | [diff] [blame] | 1694 | void |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 1695 | ML_(generic_POST_sys_getsockname) ( ThreadId tid, |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1696 | SysRes res, |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1697 | UWord arg0, UWord arg1, UWord arg2 ) |
| 1698 | { |
| 1699 | Addr name_p = arg1; |
| 1700 | Addr namelen_p = arg2; |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 1701 | vg_assert(!sr_isError(res)); /* guaranteed by caller */ |
njn | f37a81b | 2009-02-17 00:23:30 +0000 | [diff] [blame] | 1702 | ML_(buf_and_len_post_check) ( tid, res, name_p, namelen_p, |
| 1703 | "socketcall.getsockname(namelen_out)" ); |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | /* ------ */ |
| 1707 | |
sewardj | 987a8eb | 2005-03-01 19:00:30 +0000 | [diff] [blame] | 1708 | void |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 1709 | ML_(generic_PRE_sys_getpeername) ( ThreadId tid, |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1710 | UWord arg0, UWord arg1, UWord arg2 ) |
| 1711 | { |
| 1712 | /* int getpeername(int s, struct sockaddr* name, int* namelen) */ |
| 1713 | Addr name_p = arg1; |
| 1714 | Addr namelen_p = arg2; |
| 1715 | /* Nb: name_p cannot be NULL */ |
njn | f37a81b | 2009-02-17 00:23:30 +0000 | [diff] [blame] | 1716 | ML_(buf_and_len_pre_check) ( tid, name_p, namelen_p, |
| 1717 | "socketcall.getpeername(name)", |
| 1718 | "socketcall.getpeername(namelen_in)" ); |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
sewardj | 987a8eb | 2005-03-01 19:00:30 +0000 | [diff] [blame] | 1721 | void |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 1722 | ML_(generic_POST_sys_getpeername) ( ThreadId tid, |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1723 | SysRes res, |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1724 | UWord arg0, UWord arg1, UWord arg2 ) |
| 1725 | { |
| 1726 | Addr name_p = arg1; |
| 1727 | Addr namelen_p = arg2; |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 1728 | vg_assert(!sr_isError(res)); /* guaranteed by caller */ |
njn | f37a81b | 2009-02-17 00:23:30 +0000 | [diff] [blame] | 1729 | ML_(buf_and_len_post_check) ( tid, res, name_p, namelen_p, |
| 1730 | "socketcall.getpeername(namelen_out)" ); |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
| 1733 | /* ------ */ |
| 1734 | |
sewardj | 987a8eb | 2005-03-01 19:00:30 +0000 | [diff] [blame] | 1735 | void |
florian | e543f30 | 2012-10-21 19:43:43 +0000 | [diff] [blame] | 1736 | ML_(generic_PRE_sys_sendmsg) ( ThreadId tid, const HChar *name, |
| 1737 | struct vki_msghdr *msg ) |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1738 | { |
tom | fc75e5e | 2013-02-28 12:50:55 +0000 | [diff] [blame] | 1739 | msghdr_foreachfield ( tid, name, msg, ~0, pre_mem_read_sendmsg, False ); |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | /* ------ */ |
| 1743 | |
sewardj | 987a8eb | 2005-03-01 19:00:30 +0000 | [diff] [blame] | 1744 | void |
florian | e543f30 | 2012-10-21 19:43:43 +0000 | [diff] [blame] | 1745 | ML_(generic_PRE_sys_recvmsg) ( ThreadId tid, const HChar *name, |
| 1746 | struct vki_msghdr *msg ) |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1747 | { |
tom | fc75e5e | 2013-02-28 12:50:55 +0000 | [diff] [blame] | 1748 | msghdr_foreachfield ( tid, name, msg, ~0, pre_mem_write_recvmsg, True ); |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
sewardj | 987a8eb | 2005-03-01 19:00:30 +0000 | [diff] [blame] | 1751 | void |
florian | e543f30 | 2012-10-21 19:43:43 +0000 | [diff] [blame] | 1752 | ML_(generic_POST_sys_recvmsg) ( ThreadId tid, const HChar *name, |
| 1753 | struct vki_msghdr *msg, UInt length ) |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1754 | { |
tom | fc75e5e | 2013-02-28 12:50:55 +0000 | [diff] [blame] | 1755 | msghdr_foreachfield( tid, name, msg, length, post_mem_write_recvmsg, True ); |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 1756 | check_cmsg_for_fds( tid, msg ); |
| 1757 | } |
| 1758 | |
| 1759 | |
| 1760 | /* --------------------------------------------------------------------- |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1761 | Deal with a bunch of IPC related syscalls |
| 1762 | ------------------------------------------------------------------ */ |
| 1763 | |
| 1764 | /* ------ */ |
| 1765 | |
| 1766 | void |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 1767 | ML_(generic_PRE_sys_semop) ( ThreadId tid, |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1768 | UWord arg0, UWord arg1, UWord arg2 ) |
| 1769 | { |
| 1770 | /* int semop(int semid, struct sembuf *sops, unsigned nsops); */ |
| 1771 | PRE_MEM_READ( "semop(sops)", arg1, arg2 * sizeof(struct vki_sembuf) ); |
| 1772 | } |
| 1773 | |
| 1774 | /* ------ */ |
| 1775 | |
| 1776 | void |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 1777 | ML_(generic_PRE_sys_semtimedop) ( ThreadId tid, |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1778 | UWord arg0, UWord arg1, |
| 1779 | UWord arg2, UWord arg3 ) |
| 1780 | { |
| 1781 | /* int semtimedop(int semid, struct sembuf *sops, unsigned nsops, |
| 1782 | struct timespec *timeout); */ |
| 1783 | PRE_MEM_READ( "semtimedop(sops)", arg1, arg2 * sizeof(struct vki_sembuf) ); |
| 1784 | if (arg3 != 0) |
| 1785 | PRE_MEM_READ( "semtimedop(timeout)", arg3, sizeof(struct vki_timespec) ); |
| 1786 | } |
| 1787 | |
| 1788 | /* ------ */ |
| 1789 | |
| 1790 | static |
| 1791 | UInt get_sem_count( Int semid ) |
| 1792 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1793 | struct vki_semid_ds buf; |
| 1794 | union vki_semun arg; |
| 1795 | SysRes res; |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1796 | |
sewardj | 27f96b3 | 2008-10-23 09:46:59 +0000 | [diff] [blame] | 1797 | /* Doesn't actually seem to be necessary, but gcc-4.4.0 20081017 |
| 1798 | (experimental) otherwise complains that the use in the return |
| 1799 | statement below is uninitialised. */ |
| 1800 | buf.sem_nsems = 0; |
| 1801 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1802 | arg.buf = &buf; |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1803 | |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 1804 | # if defined(__NR_semctl) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1805 | res = VG_(do_syscall4)(__NR_semctl, semid, 0, VKI_IPC_STAT, *(UWord *)&arg); |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 1806 | # elif defined(__NR_semsys) /* Solaris */ |
| 1807 | res = VG_(do_syscall5)(__NR_semsys, VKI_SEMCTL, semid, 0, VKI_IPC_STAT, |
| 1808 | *(UWord *)&arg); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1809 | # else |
| 1810 | res = VG_(do_syscall5)(__NR_ipc, 3 /* IPCOP_semctl */, semid, 0, |
| 1811 | VKI_IPC_STAT, (UWord)&arg); |
sewardj | 27f96b3 | 2008-10-23 09:46:59 +0000 | [diff] [blame] | 1812 | # endif |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 1813 | if (sr_isError(res)) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1814 | return 0; |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1815 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1816 | return buf.sem_nsems; |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | void |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 1820 | ML_(generic_PRE_sys_semctl) ( ThreadId tid, |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1821 | UWord arg0, UWord arg1, |
| 1822 | UWord arg2, UWord arg3 ) |
| 1823 | { |
| 1824 | /* int semctl(int semid, int semnum, int cmd, ...); */ |
| 1825 | union vki_semun arg = *(union vki_semun *)&arg3; |
| 1826 | UInt nsems; |
| 1827 | switch (arg2 /* cmd */) { |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1828 | #if defined(VKI_IPC_INFO) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1829 | case VKI_IPC_INFO: |
| 1830 | case VKI_SEM_INFO: |
| 1831 | case VKI_IPC_INFO|VKI_IPC_64: |
| 1832 | case VKI_SEM_INFO|VKI_IPC_64: |
| 1833 | PRE_MEM_WRITE( "semctl(IPC_INFO, arg.buf)", |
| 1834 | (Addr)arg.buf, sizeof(struct vki_seminfo) ); |
| 1835 | break; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1836 | #endif |
| 1837 | |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1838 | case VKI_IPC_STAT: |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1839 | #if defined(VKI_SEM_STAT) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1840 | case VKI_SEM_STAT: |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1841 | #endif |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1842 | PRE_MEM_WRITE( "semctl(IPC_STAT, arg.buf)", |
| 1843 | (Addr)arg.buf, sizeof(struct vki_semid_ds) ); |
| 1844 | break; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1845 | |
| 1846 | #if defined(VKI_IPC_64) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1847 | case VKI_IPC_STAT|VKI_IPC_64: |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1848 | #if defined(VKI_SEM_STAT) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1849 | case VKI_SEM_STAT|VKI_IPC_64: |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1850 | #endif |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 1851 | #endif |
| 1852 | #if defined(VKI_IPC_STAT64) |
| 1853 | case VKI_IPC_STAT64: |
| 1854 | #endif |
| 1855 | #if defined(VKI_IPC_64) || defined(VKI_IPC_STAT64) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1856 | PRE_MEM_WRITE( "semctl(IPC_STAT, arg.buf)", |
| 1857 | (Addr)arg.buf, sizeof(struct vki_semid64_ds) ); |
| 1858 | break; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1859 | #endif |
| 1860 | |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1861 | case VKI_IPC_SET: |
| 1862 | PRE_MEM_READ( "semctl(IPC_SET, arg.buf)", |
| 1863 | (Addr)arg.buf, sizeof(struct vki_semid_ds) ); |
| 1864 | break; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1865 | |
| 1866 | #if defined(VKI_IPC_64) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1867 | case VKI_IPC_SET|VKI_IPC_64: |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 1868 | #endif |
| 1869 | #if defined(VKI_IPC_SET64) |
| 1870 | case VKI_IPC_SET64: |
| 1871 | #endif |
| 1872 | #if defined(VKI_IPC64) || defined(VKI_IPC_SET64) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1873 | PRE_MEM_READ( "semctl(IPC_SET, arg.buf)", |
| 1874 | (Addr)arg.buf, sizeof(struct vki_semid64_ds) ); |
| 1875 | break; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1876 | #endif |
| 1877 | |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1878 | case VKI_GETALL: |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1879 | #if defined(VKI_IPC_64) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1880 | case VKI_GETALL|VKI_IPC_64: |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1881 | #endif |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1882 | nsems = get_sem_count( arg0 ); |
| 1883 | PRE_MEM_WRITE( "semctl(IPC_GETALL, arg.array)", |
| 1884 | (Addr)arg.array, sizeof(unsigned short) * nsems ); |
| 1885 | break; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1886 | |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1887 | case VKI_SETALL: |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1888 | #if defined(VKI_IPC_64) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1889 | case VKI_SETALL|VKI_IPC_64: |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1890 | #endif |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1891 | nsems = get_sem_count( arg0 ); |
| 1892 | PRE_MEM_READ( "semctl(IPC_SETALL, arg.array)", |
| 1893 | (Addr)arg.array, sizeof(unsigned short) * nsems ); |
| 1894 | break; |
| 1895 | } |
| 1896 | } |
| 1897 | |
| 1898 | void |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 1899 | ML_(generic_POST_sys_semctl) ( ThreadId tid, |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1900 | UWord res, |
| 1901 | UWord arg0, UWord arg1, |
| 1902 | UWord arg2, UWord arg3 ) |
| 1903 | { |
| 1904 | union vki_semun arg = *(union vki_semun *)&arg3; |
| 1905 | UInt nsems; |
| 1906 | switch (arg2 /* cmd */) { |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1907 | #if defined(VKI_IPC_INFO) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1908 | case VKI_IPC_INFO: |
| 1909 | case VKI_SEM_INFO: |
| 1910 | case VKI_IPC_INFO|VKI_IPC_64: |
| 1911 | case VKI_SEM_INFO|VKI_IPC_64: |
| 1912 | POST_MEM_WRITE( (Addr)arg.buf, sizeof(struct vki_seminfo) ); |
| 1913 | break; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1914 | #endif |
| 1915 | |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1916 | case VKI_IPC_STAT: |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1917 | #if defined(VKI_SEM_STAT) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1918 | case VKI_SEM_STAT: |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1919 | #endif |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1920 | POST_MEM_WRITE( (Addr)arg.buf, sizeof(struct vki_semid_ds) ); |
| 1921 | break; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1922 | |
| 1923 | #if defined(VKI_IPC_64) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1924 | case VKI_IPC_STAT|VKI_IPC_64: |
| 1925 | case VKI_SEM_STAT|VKI_IPC_64: |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 1926 | #endif |
| 1927 | #if defined(VKI_IPC_STAT64) |
| 1928 | case VKI_IPC_STAT64: |
| 1929 | #endif |
| 1930 | #if defined(VKI_IPC_64) || defined(VKI_IPC_STAT64) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1931 | POST_MEM_WRITE( (Addr)arg.buf, sizeof(struct vki_semid64_ds) ); |
| 1932 | break; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1933 | #endif |
| 1934 | |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1935 | case VKI_GETALL: |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1936 | #if defined(VKI_IPC_64) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1937 | case VKI_GETALL|VKI_IPC_64: |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1938 | #endif |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1939 | nsems = get_sem_count( arg0 ); |
| 1940 | POST_MEM_WRITE( (Addr)arg.array, sizeof(unsigned short) * nsems ); |
| 1941 | break; |
| 1942 | } |
| 1943 | } |
| 1944 | |
| 1945 | /* ------ */ |
| 1946 | |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1947 | /* ------ */ |
| 1948 | |
| 1949 | static |
philippe | 2d75ea2 | 2012-08-16 19:40:52 +0000 | [diff] [blame] | 1950 | SizeT get_shm_size ( Int shmid ) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1951 | { |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 1952 | #if defined(__NR_shmctl) |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 1953 | # ifdef VKI_IPC_64 |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1954 | struct vki_shmid64_ds buf; |
philippe | c8d064a | 2014-12-17 20:39:55 +0000 | [diff] [blame] | 1955 | # if defined(VGP_amd64_linux) || defined(VGP_arm64_linux) |
sewardj | 90741b2 | 2010-10-06 12:59:44 +0000 | [diff] [blame] | 1956 | /* See bug 222545 comment 7 */ |
| 1957 | SysRes __res = VG_(do_syscall3)(__NR_shmctl, shmid, |
| 1958 | VKI_IPC_STAT, (UWord)&buf); |
| 1959 | # else |
| 1960 | SysRes __res = VG_(do_syscall3)(__NR_shmctl, shmid, |
| 1961 | VKI_IPC_STAT|VKI_IPC_64, (UWord)&buf); |
| 1962 | # endif |
| 1963 | # else /* !def VKI_IPC_64 */ |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1964 | struct vki_shmid_ds buf; |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 1965 | SysRes __res = VG_(do_syscall3)(__NR_shmctl, shmid, VKI_IPC_STAT, (UWord)&buf); |
sewardj | 90741b2 | 2010-10-06 12:59:44 +0000 | [diff] [blame] | 1966 | # endif /* def VKI_IPC_64 */ |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 1967 | #elif defined(__NR_shmsys) /* Solaris */ |
| 1968 | struct vki_shmid_ds buf; |
| 1969 | SysRes __res = VG_(do_syscall4)(__NR_shmsys, VKI_SHMCTL, shmid, VKI_IPC_STAT, |
| 1970 | (UWord)&buf); |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 1971 | #else |
| 1972 | struct vki_shmid_ds buf; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 1973 | SysRes __res = VG_(do_syscall5)(__NR_ipc, 24 /* IPCOP_shmctl */, shmid, |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1974 | VKI_IPC_STAT, 0, (UWord)&buf); |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 1975 | #endif |
| 1976 | if (sr_isError(__res)) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1977 | return 0; |
| 1978 | |
philippe | 2d75ea2 | 2012-08-16 19:40:52 +0000 | [diff] [blame] | 1979 | return (SizeT) buf.shm_segsz; |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
| 1982 | UWord |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 1983 | ML_(generic_PRE_sys_shmat) ( ThreadId tid, |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 1984 | UWord arg0, UWord arg1, UWord arg2 ) |
| 1985 | { |
| 1986 | /* void *shmat(int shmid, const void *shmaddr, int shmflg); */ |
philippe | 2d75ea2 | 2012-08-16 19:40:52 +0000 | [diff] [blame] | 1987 | SizeT segmentSize = get_shm_size ( arg0 ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1988 | UWord tmp; |
| 1989 | Bool ok; |
| 1990 | if (arg1 == 0) { |
sewardj | 6045709 | 2010-10-06 15:24:39 +0000 | [diff] [blame] | 1991 | /* arm-linux only: work around the fact that |
| 1992 | VG_(am_get_advisory_client_simple) produces something that is |
| 1993 | VKI_PAGE_SIZE aligned, whereas what we want is something |
| 1994 | VKI_SHMLBA aligned, and VKI_SHMLBA >= VKI_PAGE_SIZE. Hence |
| 1995 | increase the request size by VKI_SHMLBA - VKI_PAGE_SIZE and |
| 1996 | then round the result up to the next VKI_SHMLBA boundary. |
| 1997 | See bug 222545 comment 15. So far, arm-linux is the only |
| 1998 | platform where this is known to be necessary. */ |
| 1999 | vg_assert(VKI_SHMLBA >= VKI_PAGE_SIZE); |
| 2000 | if (VKI_SHMLBA > VKI_PAGE_SIZE) { |
| 2001 | segmentSize += VKI_SHMLBA - VKI_PAGE_SIZE; |
| 2002 | } |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2003 | tmp = VG_(am_get_advisory_client_simple)(0, segmentSize, &ok); |
sewardj | 6045709 | 2010-10-06 15:24:39 +0000 | [diff] [blame] | 2004 | if (ok) { |
| 2005 | if (VKI_SHMLBA > VKI_PAGE_SIZE) { |
| 2006 | arg1 = VG_ROUNDUP(tmp, VKI_SHMLBA); |
| 2007 | } else { |
| 2008 | arg1 = tmp; |
| 2009 | } |
| 2010 | } |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2011 | } |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 2012 | else if (!ML_(valid_client_addr)(arg1, segmentSize, tid, "shmat")) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2013 | arg1 = 0; |
| 2014 | return arg1; |
| 2015 | } |
| 2016 | |
| 2017 | void |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 2018 | ML_(generic_POST_sys_shmat) ( ThreadId tid, |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2019 | UWord res, |
| 2020 | UWord arg0, UWord arg1, UWord arg2 ) |
| 2021 | { |
philippe | 2d75ea2 | 2012-08-16 19:40:52 +0000 | [diff] [blame] | 2022 | SizeT segmentSize = VG_PGROUNDUP(get_shm_size(arg0)); |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2023 | if ( segmentSize > 0 ) { |
| 2024 | UInt prot = VKI_PROT_READ|VKI_PROT_WRITE; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2025 | Bool d; |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2026 | |
tom | f61fea0 | 2005-10-04 12:04:06 +0000 | [diff] [blame] | 2027 | if (arg2 & VKI_SHM_RDONLY) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2028 | prot &= ~VKI_PROT_WRITE; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2029 | /* It isn't exactly correct to pass 0 for the fd and offset |
| 2030 | here. The kernel seems to think the corresponding section |
| 2031 | does have dev/ino numbers: |
| 2032 | |
| 2033 | 04e52000-04ec8000 rw-s 00000000 00:06 1966090 /SYSV00000000 (deleted) |
| 2034 | |
| 2035 | However there is no obvious way to find them. In order to |
| 2036 | cope with the discrepancy, aspacem's sync checker omits the |
| 2037 | dev/ino correspondence check in cases where V does not know |
| 2038 | the dev/ino. */ |
njn | c6fe8a2 | 2010-12-08 02:51:43 +0000 | [diff] [blame] | 2039 | d = VG_(am_notify_client_shmat)( res, segmentSize, prot ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2040 | |
| 2041 | /* we don't distinguish whether it's read-only or |
| 2042 | * read-write -- it doesn't matter really. */ |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 2043 | VG_TRACK( new_mem_mmap, res, segmentSize, True, True, False, |
| 2044 | 0/*di_handle*/ ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2045 | if (d) |
florian | ddd61ff | 2015-01-04 17:20:45 +0000 | [diff] [blame] | 2046 | VG_(discard_translations)( (Addr)res, |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2047 | (ULong)VG_PGROUNDUP(segmentSize), |
| 2048 | "ML_(generic_POST_sys_shmat)" ); |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2049 | } |
| 2050 | } |
| 2051 | |
| 2052 | /* ------ */ |
| 2053 | |
| 2054 | Bool |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 2055 | ML_(generic_PRE_sys_shmdt) ( ThreadId tid, UWord arg0 ) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2056 | { |
| 2057 | /* int shmdt(const void *shmaddr); */ |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 2058 | return ML_(valid_client_addr)(arg0, 1, tid, "shmdt"); |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
| 2061 | void |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 2062 | ML_(generic_POST_sys_shmdt) ( ThreadId tid, UWord res, UWord arg0 ) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2063 | { |
sewardj | ef1cf8b | 2006-10-17 01:47:30 +0000 | [diff] [blame] | 2064 | NSegment const* s = VG_(am_find_nsegment)(arg0); |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2065 | |
sewardj | 0e682c6 | 2005-09-28 20:05:31 +0000 | [diff] [blame] | 2066 | if (s != NULL) { |
| 2067 | Addr s_start = s->start; |
| 2068 | SizeT s_len = s->end+1 - s->start; |
tom | 1340c35 | 2005-10-04 15:59:54 +0000 | [diff] [blame] | 2069 | Bool d; |
| 2070 | |
dirk | 7bc57fc | 2006-04-27 11:05:13 +0000 | [diff] [blame] | 2071 | vg_assert(s->kind == SkShmC); |
| 2072 | vg_assert(s->start == arg0); |
tom | 1340c35 | 2005-10-04 15:59:54 +0000 | [diff] [blame] | 2073 | |
| 2074 | d = VG_(am_notify_munmap)(s_start, s_len); |
sewardj | 0e682c6 | 2005-09-28 20:05:31 +0000 | [diff] [blame] | 2075 | s = NULL; /* s is now invalid */ |
| 2076 | VG_TRACK( die_mem_munmap, s_start, s_len ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2077 | if (d) |
florian | ddd61ff | 2015-01-04 17:20:45 +0000 | [diff] [blame] | 2078 | VG_(discard_translations)( s_start, |
sewardj | 0e682c6 | 2005-09-28 20:05:31 +0000 | [diff] [blame] | 2079 | (ULong)s_len, |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2080 | "ML_(generic_POST_sys_shmdt)" ); |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2081 | } |
| 2082 | } |
| 2083 | /* ------ */ |
| 2084 | |
| 2085 | void |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 2086 | ML_(generic_PRE_sys_shmctl) ( ThreadId tid, |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2087 | UWord arg0, UWord arg1, UWord arg2 ) |
| 2088 | { |
| 2089 | /* int shmctl(int shmid, int cmd, struct shmid_ds *buf); */ |
| 2090 | switch (arg1 /* cmd */) { |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2091 | #if defined(VKI_IPC_INFO) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2092 | case VKI_IPC_INFO: |
| 2093 | PRE_MEM_WRITE( "shmctl(IPC_INFO, buf)", |
| 2094 | arg2, sizeof(struct vki_shminfo) ); |
| 2095 | break; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2096 | #if defined(VKI_IPC_64) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2097 | case VKI_IPC_INFO|VKI_IPC_64: |
| 2098 | PRE_MEM_WRITE( "shmctl(IPC_INFO, buf)", |
| 2099 | arg2, sizeof(struct vki_shminfo64) ); |
| 2100 | break; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2101 | #endif |
| 2102 | #endif |
| 2103 | |
| 2104 | #if defined(VKI_SHM_INFO) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2105 | case VKI_SHM_INFO: |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2106 | #if defined(VKI_IPC_64) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2107 | case VKI_SHM_INFO|VKI_IPC_64: |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2108 | #endif |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2109 | PRE_MEM_WRITE( "shmctl(SHM_INFO, buf)", |
| 2110 | arg2, sizeof(struct vki_shm_info) ); |
| 2111 | break; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2112 | #endif |
| 2113 | |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2114 | case VKI_IPC_STAT: |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2115 | #if defined(VKI_SHM_STAT) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2116 | case VKI_SHM_STAT: |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2117 | #endif |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2118 | PRE_MEM_WRITE( "shmctl(IPC_STAT, buf)", |
| 2119 | arg2, sizeof(struct vki_shmid_ds) ); |
| 2120 | break; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2121 | |
| 2122 | #if defined(VKI_IPC_64) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2123 | case VKI_IPC_STAT|VKI_IPC_64: |
| 2124 | case VKI_SHM_STAT|VKI_IPC_64: |
| 2125 | PRE_MEM_WRITE( "shmctl(IPC_STAT, arg.buf)", |
| 2126 | arg2, sizeof(struct vki_shmid64_ds) ); |
| 2127 | break; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2128 | #endif |
| 2129 | |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2130 | case VKI_IPC_SET: |
| 2131 | PRE_MEM_READ( "shmctl(IPC_SET, arg.buf)", |
| 2132 | arg2, sizeof(struct vki_shmid_ds) ); |
| 2133 | break; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2134 | |
| 2135 | #if defined(VKI_IPC_64) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2136 | case VKI_IPC_SET|VKI_IPC_64: |
| 2137 | PRE_MEM_READ( "shmctl(IPC_SET, arg.buf)", |
| 2138 | arg2, sizeof(struct vki_shmid64_ds) ); |
| 2139 | break; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2140 | #endif |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2141 | } |
| 2142 | } |
| 2143 | |
| 2144 | void |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 2145 | ML_(generic_POST_sys_shmctl) ( ThreadId tid, |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2146 | UWord res, |
| 2147 | UWord arg0, UWord arg1, UWord arg2 ) |
| 2148 | { |
| 2149 | switch (arg1 /* cmd */) { |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2150 | #if defined(VKI_IPC_INFO) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2151 | case VKI_IPC_INFO: |
| 2152 | POST_MEM_WRITE( arg2, sizeof(struct vki_shminfo) ); |
| 2153 | break; |
| 2154 | case VKI_IPC_INFO|VKI_IPC_64: |
| 2155 | POST_MEM_WRITE( arg2, sizeof(struct vki_shminfo64) ); |
| 2156 | break; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2157 | #endif |
| 2158 | |
| 2159 | #if defined(VKI_SHM_INFO) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2160 | case VKI_SHM_INFO: |
| 2161 | case VKI_SHM_INFO|VKI_IPC_64: |
| 2162 | POST_MEM_WRITE( arg2, sizeof(struct vki_shm_info) ); |
| 2163 | break; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2164 | #endif |
| 2165 | |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2166 | case VKI_IPC_STAT: |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2167 | #if defined(VKI_SHM_STAT) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2168 | case VKI_SHM_STAT: |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2169 | #endif |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2170 | POST_MEM_WRITE( arg2, sizeof(struct vki_shmid_ds) ); |
| 2171 | break; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2172 | |
| 2173 | #if defined(VKI_IPC_64) |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2174 | case VKI_IPC_STAT|VKI_IPC_64: |
| 2175 | case VKI_SHM_STAT|VKI_IPC_64: |
| 2176 | POST_MEM_WRITE( arg2, sizeof(struct vki_shmid64_ds) ); |
| 2177 | break; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2178 | #endif |
| 2179 | |
| 2180 | |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2181 | } |
| 2182 | } |
| 2183 | |
sewardj | b369c5e | 2005-03-24 17:52:02 +0000 | [diff] [blame] | 2184 | /* --------------------------------------------------------------------- |
tom | 9548a16 | 2005-09-30 08:07:53 +0000 | [diff] [blame] | 2185 | Generic handler for mmap |
| 2186 | ------------------------------------------------------------------ */ |
| 2187 | |
tom | 6d85b04 | 2005-09-30 16:09:12 +0000 | [diff] [blame] | 2188 | /* |
| 2189 | * Although mmap is specified by POSIX and the argument are generally |
| 2190 | * consistent across platforms the precise details of the low level |
| 2191 | * argument passing conventions differ. For example: |
| 2192 | * |
| 2193 | * - On x86-linux there is mmap (aka old_mmap) which takes the |
| 2194 | * arguments in a memory block and the offset in bytes; and |
| 2195 | * mmap2 (aka sys_mmap2) which takes the arguments in the normal |
| 2196 | * way and the offset in pages. |
| 2197 | * |
| 2198 | * - On ppc32-linux there is mmap (aka sys_mmap) which takes the |
| 2199 | * arguments in the normal way and the offset in bytes; and |
| 2200 | * mmap2 (aka sys_mmap2) which takes the arguments in the normal |
| 2201 | * way and the offset in pages. |
| 2202 | * |
| 2203 | * - On amd64-linux everything is simple and there is just the one |
| 2204 | * call, mmap (aka sys_mmap) which takes the arguments in the |
| 2205 | * normal way and the offset in bytes. |
| 2206 | * |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 2207 | * - On s390x-linux there is mmap (aka old_mmap) which takes the |
| 2208 | * arguments in a memory block and the offset in bytes. mmap2 |
| 2209 | * is also available (but not exported via unistd.h) with |
| 2210 | * arguments in a memory block and the offset in pages. |
| 2211 | * |
tom | 6d85b04 | 2005-09-30 16:09:12 +0000 | [diff] [blame] | 2212 | * To cope with all this we provide a generic handler function here |
| 2213 | * and then each platform implements one or more system call handlers |
| 2214 | * which call this generic routine after extracting and normalising |
| 2215 | * the arguments. |
| 2216 | */ |
| 2217 | |
tom | 9548a16 | 2005-09-30 08:07:53 +0000 | [diff] [blame] | 2218 | SysRes |
| 2219 | ML_(generic_PRE_sys_mmap) ( ThreadId tid, |
| 2220 | UWord arg1, UWord arg2, UWord arg3, |
sewardj | 274461d | 2005-10-02 17:01:41 +0000 | [diff] [blame] | 2221 | UWord arg4, UWord arg5, Off64T arg6 ) |
tom | 9548a16 | 2005-09-30 08:07:53 +0000 | [diff] [blame] | 2222 | { |
| 2223 | Addr advised; |
| 2224 | SysRes sres; |
| 2225 | MapRequest mreq; |
| 2226 | Bool mreq_ok; |
| 2227 | |
sewardj | 80fe549 | 2013-10-14 14:50:57 +0000 | [diff] [blame] | 2228 | # if defined(VGO_darwin) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2229 | // Nb: we can't use this on Darwin, it has races: |
| 2230 | // * needs to RETRY if advisory succeeds but map fails |
| 2231 | // (could have been some other thread in a nonblocking call) |
| 2232 | // * needs to not use fixed-position mmap() on Darwin |
| 2233 | // (mmap will cheerfully smash whatever's already there, which might |
| 2234 | // be a new mapping from some other thread in a nonblocking call) |
| 2235 | VG_(core_panic)("can't use ML_(generic_PRE_sys_mmap) on Darwin"); |
sewardj | 80fe549 | 2013-10-14 14:50:57 +0000 | [diff] [blame] | 2236 | # endif |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2237 | |
tom | 9548a16 | 2005-09-30 08:07:53 +0000 | [diff] [blame] | 2238 | if (arg2 == 0) { |
| 2239 | /* SuSV3 says: If len is zero, mmap() shall fail and no mapping |
| 2240 | shall be established. */ |
| 2241 | return VG_(mk_SysRes_Error)( VKI_EINVAL ); |
| 2242 | } |
| 2243 | |
| 2244 | if (!VG_IS_PAGE_ALIGNED(arg1)) { |
| 2245 | /* zap any misaligned addresses. */ |
| 2246 | /* SuSV3 says misaligned addresses only cause the MAP_FIXED case |
| 2247 | to fail. Here, we catch them all. */ |
| 2248 | return VG_(mk_SysRes_Error)( VKI_EINVAL ); |
| 2249 | } |
| 2250 | |
sewardj | 7e21df8 | 2005-09-30 10:48:27 +0000 | [diff] [blame] | 2251 | if (!VG_IS_PAGE_ALIGNED(arg6)) { |
| 2252 | /* zap any misaligned offsets. */ |
| 2253 | /* SuSV3 says: The off argument is constrained to be aligned and |
| 2254 | sized according to the value returned by sysconf() when |
| 2255 | passed _SC_PAGESIZE or _SC_PAGE_SIZE. */ |
| 2256 | return VG_(mk_SysRes_Error)( VKI_EINVAL ); |
| 2257 | } |
| 2258 | |
tom | 9548a16 | 2005-09-30 08:07:53 +0000 | [diff] [blame] | 2259 | /* Figure out what kind of allocation constraints there are |
| 2260 | (fixed/hint/any), and ask aspacem what we should do. */ |
| 2261 | mreq.start = arg1; |
| 2262 | mreq.len = arg2; |
| 2263 | if (arg4 & VKI_MAP_FIXED) { |
| 2264 | mreq.rkind = MFixed; |
| 2265 | } else |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 2266 | #if defined(VKI_MAP_ALIGN) /* Solaris specific */ |
| 2267 | if (arg4 & VKI_MAP_ALIGN) { |
| 2268 | mreq.rkind = MAlign; |
| 2269 | if (mreq.start == 0) { |
| 2270 | mreq.start = VKI_PAGE_SIZE; |
| 2271 | } |
| 2272 | /* VKI_MAP_FIXED and VKI_MAP_ALIGN don't like each other. */ |
| 2273 | arg4 &= ~VKI_MAP_ALIGN; |
| 2274 | } else |
| 2275 | #endif |
tom | 9548a16 | 2005-09-30 08:07:53 +0000 | [diff] [blame] | 2276 | if (arg1 != 0) { |
| 2277 | mreq.rkind = MHint; |
| 2278 | } else { |
| 2279 | mreq.rkind = MAny; |
| 2280 | } |
| 2281 | |
| 2282 | /* Enquire ... */ |
| 2283 | advised = VG_(am_get_advisory)( &mreq, True/*client*/, &mreq_ok ); |
| 2284 | if (!mreq_ok) { |
| 2285 | /* Our request was bounced, so we'd better fail. */ |
| 2286 | return VG_(mk_SysRes_Error)( VKI_EINVAL ); |
| 2287 | } |
| 2288 | |
philippe | da0a8cb | 2015-06-17 19:57:09 +0000 | [diff] [blame] | 2289 | # if defined(VKI_MAP_32BIT) |
| 2290 | /* MAP_32BIT is royally unportable, so if the client asks for it, try our |
| 2291 | best to make it work (but without complexifying aspacemgr). |
| 2292 | If the user requested MAP_32BIT, the mmap-ed space must be in the |
| 2293 | first 2GB of the address space. So, return ENOMEM if aspacemgr |
| 2294 | advisory is above the first 2GB. If MAP_FIXED is also requested, |
| 2295 | MAP_32BIT has to be ignored. |
| 2296 | Assumption about aspacemgr behaviour: aspacemgr scans the address space |
| 2297 | from low addresses to find a free segment. No special effort is done |
| 2298 | to keep the first 2GB 'free' for this MAP_32BIT. So, this will often |
| 2299 | fail once the program has already allocated significant memory. */ |
| 2300 | if ((arg4 & VKI_MAP_32BIT) && !(arg4 & VKI_MAP_FIXED)) { |
| 2301 | if (advised + arg2 >= 0x80000000) |
| 2302 | return VG_(mk_SysRes_Error)( VKI_ENOMEM ); |
| 2303 | } |
| 2304 | # endif |
| 2305 | |
tom | 9548a16 | 2005-09-30 08:07:53 +0000 | [diff] [blame] | 2306 | /* Otherwise we're OK (so far). Install aspacem's choice of |
| 2307 | address, and let the mmap go through. */ |
| 2308 | sres = VG_(am_do_mmap_NO_NOTIFY)(advised, arg2, arg3, |
| 2309 | arg4 | VKI_MAP_FIXED, |
| 2310 | arg5, arg6); |
| 2311 | |
philippe | da0a8cb | 2015-06-17 19:57:09 +0000 | [diff] [blame] | 2312 | # if defined(VKI_MAP_32BIT) |
| 2313 | /* No recovery trial if the advisory was not accepted. */ |
| 2314 | if ((arg4 & VKI_MAP_32BIT) && !(arg4 & VKI_MAP_FIXED) |
| 2315 | && sr_isError(sres)) { |
| 2316 | return VG_(mk_SysRes_Error)( VKI_ENOMEM ); |
| 2317 | } |
| 2318 | # endif |
| 2319 | |
sewardj | 427d016 | 2007-01-09 16:47:20 +0000 | [diff] [blame] | 2320 | /* A refinement: it may be that the kernel refused aspacem's choice |
| 2321 | of address. If we were originally asked for a hinted mapping, |
| 2322 | there is still a last chance: try again at any address. |
| 2323 | Hence: */ |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 2324 | if (mreq.rkind == MHint && sr_isError(sres)) { |
sewardj | 427d016 | 2007-01-09 16:47:20 +0000 | [diff] [blame] | 2325 | mreq.start = 0; |
| 2326 | mreq.len = arg2; |
| 2327 | mreq.rkind = MAny; |
| 2328 | advised = VG_(am_get_advisory)( &mreq, True/*client*/, &mreq_ok ); |
| 2329 | if (!mreq_ok) { |
| 2330 | /* Our request was bounced, so we'd better fail. */ |
| 2331 | return VG_(mk_SysRes_Error)( VKI_EINVAL ); |
| 2332 | } |
| 2333 | /* and try again with the kernel */ |
| 2334 | sres = VG_(am_do_mmap_NO_NOTIFY)(advised, arg2, arg3, |
| 2335 | arg4 | VKI_MAP_FIXED, |
| 2336 | arg5, arg6); |
| 2337 | } |
| 2338 | |
philippe | 98d6b25 | 2014-11-01 21:02:13 +0000 | [diff] [blame] | 2339 | /* Yet another refinement : sometimes valgrind chooses an address |
| 2340 | which is not acceptable by the kernel. This at least happens |
| 2341 | when mmap-ing huge pages, using the flag MAP_HUGETLB. |
| 2342 | valgrind aspacem does not know about huge pages, and modifying |
| 2343 | it to handle huge pages is not straightforward (e.g. need |
| 2344 | to understand special file system mount options). |
| 2345 | So, let's just redo an mmap, without giving any constraint to |
| 2346 | the kernel. If that succeeds, check with aspacem that the returned |
florian | 8f8811e | 2015-08-13 20:35:03 +0000 | [diff] [blame] | 2347 | address is acceptable. |
philippe | 98d6b25 | 2014-11-01 21:02:13 +0000 | [diff] [blame] | 2348 | This will give a similar effect as if the user would have |
florian | 8f8811e | 2015-08-13 20:35:03 +0000 | [diff] [blame] | 2349 | hinted that address. |
philippe | 98d6b25 | 2014-11-01 21:02:13 +0000 | [diff] [blame] | 2350 | The aspacem state will be correctly updated afterwards. |
| 2351 | We however cannot do this last refinement when the user asked |
| 2352 | for a fixed mapping, as the user asked a specific address. */ |
| 2353 | if (sr_isError(sres) && !(arg4 & VKI_MAP_FIXED)) { |
| 2354 | advised = 0; |
| 2355 | /* try mmap with NULL address and without VKI_MAP_FIXED |
| 2356 | to let the kernel decide. */ |
| 2357 | sres = VG_(am_do_mmap_NO_NOTIFY)(advised, arg2, arg3, |
| 2358 | arg4, |
| 2359 | arg5, arg6); |
| 2360 | if (!sr_isError(sres)) { |
florian | 8f8811e | 2015-08-13 20:35:03 +0000 | [diff] [blame] | 2361 | /* The kernel is supposed to know what it is doing, but let's |
| 2362 | do a last sanity check anyway, as if the chosen address had |
| 2363 | been initially hinted by the client. The whole point of this |
| 2364 | last try was to allow mmap of huge pages to succeed without |
| 2365 | making aspacem understand them, on the other hand the kernel |
| 2366 | does not know about valgrind reservations, so this mapping |
| 2367 | can end up in free space and reservations. */ |
| 2368 | mreq.start = (Addr)sr_Res(sres); |
| 2369 | mreq.len = arg2; |
| 2370 | mreq.rkind = MHint; |
| 2371 | advised = VG_(am_get_advisory)( &mreq, True/*client*/, &mreq_ok ); |
| 2372 | vg_assert(mreq_ok && advised == mreq.start); |
philippe | 98d6b25 | 2014-11-01 21:02:13 +0000 | [diff] [blame] | 2373 | } |
| 2374 | } |
| 2375 | |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 2376 | if (!sr_isError(sres)) { |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 2377 | ULong di_handle; |
bart | 510521e | 2008-05-29 08:52:44 +0000 | [diff] [blame] | 2378 | /* Notify aspacem. */ |
njn | dd37258 | 2009-05-10 22:42:19 +0000 | [diff] [blame] | 2379 | notify_core_of_mmap( |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 2380 | (Addr)sr_Res(sres), /* addr kernel actually assigned */ |
bart | 510521e | 2008-05-29 08:52:44 +0000 | [diff] [blame] | 2381 | arg2, /* length */ |
| 2382 | arg3, /* prot */ |
tom | 9548a16 | 2005-09-30 08:07:53 +0000 | [diff] [blame] | 2383 | arg4, /* the original flags value */ |
bart | 510521e | 2008-05-29 08:52:44 +0000 | [diff] [blame] | 2384 | arg5, /* fd */ |
| 2385 | arg6 /* offset */ |
tom | 9548a16 | 2005-09-30 08:07:53 +0000 | [diff] [blame] | 2386 | ); |
| 2387 | /* Load symbols? */ |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 2388 | di_handle = VG_(di_notify_mmap)( (Addr)sr_Res(sres), |
sewardj | 5f2dcad | 2011-10-24 08:53:03 +0000 | [diff] [blame] | 2389 | False/*allow_SkFileV*/, (Int)arg5 ); |
bart | 510521e | 2008-05-29 08:52:44 +0000 | [diff] [blame] | 2390 | /* Notify the tool. */ |
| 2391 | notify_tool_of_mmap( |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 2392 | (Addr)sr_Res(sres), /* addr kernel actually assigned */ |
bart | 510521e | 2008-05-29 08:52:44 +0000 | [diff] [blame] | 2393 | arg2, /* length */ |
| 2394 | arg3, /* prot */ |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 2395 | di_handle /* so the tool can refer to the read debuginfo later, |
| 2396 | if it wants. */ |
bart | 510521e | 2008-05-29 08:52:44 +0000 | [diff] [blame] | 2397 | ); |
tom | 9548a16 | 2005-09-30 08:07:53 +0000 | [diff] [blame] | 2398 | } |
| 2399 | |
| 2400 | /* Stay sane */ |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 2401 | if (!sr_isError(sres) && (arg4 & VKI_MAP_FIXED)) |
| 2402 | vg_assert(sr_Res(sres) == arg1); |
tom | 9548a16 | 2005-09-30 08:07:53 +0000 | [diff] [blame] | 2403 | |
| 2404 | return sres; |
| 2405 | } |
| 2406 | |
| 2407 | |
| 2408 | /* --------------------------------------------------------------------- |
nethercote | 4fa681f | 2004-11-08 17:51:39 +0000 | [diff] [blame] | 2409 | The Main Entertainment ... syscall wrappers |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2410 | ------------------------------------------------------------------ */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2411 | |
nethercote | 4fa681f | 2004-11-08 17:51:39 +0000 | [diff] [blame] | 2412 | /* Note: the PRE() and POST() wrappers are for the actual functions |
nethercote | 8ff888f | 2004-11-17 17:11:45 +0000 | [diff] [blame] | 2413 | implementing the system calls in the OS kernel. These mostly have |
nethercote | 4fa681f | 2004-11-08 17:51:39 +0000 | [diff] [blame] | 2414 | names like sys_write(); a few have names like old_mmap(). See the |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 2415 | comment for ML_(syscall_table)[] for important info about the __NR_foo |
nethercote | 8ff888f | 2004-11-17 17:11:45 +0000 | [diff] [blame] | 2416 | constants and their relationship to the sys_foo() functions. |
nethercote | 4fa681f | 2004-11-08 17:51:39 +0000 | [diff] [blame] | 2417 | |
nethercote | 92b2fd5 | 2004-11-16 16:15:41 +0000 | [diff] [blame] | 2418 | Some notes about names used for syscalls and args: |
| 2419 | - For the --trace-syscalls=yes output, we use the sys_foo() name to avoid |
| 2420 | ambiguity. |
| 2421 | |
| 2422 | - For error messages, we generally use a somewhat generic name |
| 2423 | for the syscall (eg. "write" rather than "sys_write"). This should be |
| 2424 | good enough for the average user to understand what is happening, |
| 2425 | without confusing them with names like "sys_write". |
| 2426 | |
| 2427 | - Also, for error messages the arg names are mostly taken from the man |
| 2428 | pages (even though many of those man pages are really for glibc |
nethercote | 8ff888f | 2004-11-17 17:11:45 +0000 | [diff] [blame] | 2429 | functions of the same name), rather than from the OS kernel source, |
nethercote | 92b2fd5 | 2004-11-16 16:15:41 +0000 | [diff] [blame] | 2430 | for the same reason -- a user presented with a "bogus foo(bar)" arg |
| 2431 | will most likely look at the "foo" man page to see which is the "bar" |
| 2432 | arg. |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 2433 | |
nethercote | 9c311eb | 2004-11-12 18:20:12 +0000 | [diff] [blame] | 2434 | Note that we use our own vki_* types. The one exception is in |
| 2435 | PRE_REG_READn calls, where pointer types haven't been changed, because |
| 2436 | they don't need to be -- eg. for "foo*" to be used, the type foo need not |
| 2437 | be visible. |
| 2438 | |
nethercote | 4fa681f | 2004-11-08 17:51:39 +0000 | [diff] [blame] | 2439 | XXX: some of these are arch-specific, and should be factored out. |
| 2440 | */ |
| 2441 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 2442 | #define PRE(name) DEFN_PRE_TEMPLATE(generic, name) |
| 2443 | #define POST(name) DEFN_POST_TEMPLATE(generic, name) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2444 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 2445 | PRE(sys_exit) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2446 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 2447 | ThreadState* tst; |
sewardj | a922b61 | 2005-03-11 02:47:32 +0000 | [diff] [blame] | 2448 | /* simple; just make this thread exit */ |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 2449 | PRINT("exit( %ld )", SARG1); |
njn | aab814e | 2009-03-03 05:39:23 +0000 | [diff] [blame] | 2450 | PRE_REG_READ1(void, "exit", int, status); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 2451 | tst = VG_(get_ThreadState)(tid); |
| 2452 | /* Set the thread's status to be exiting, then claim that the |
| 2453 | syscall succeeded. */ |
sewardj | ef1cf8b | 2006-10-17 01:47:30 +0000 | [diff] [blame] | 2454 | tst->exitreason = VgSrc_ExitThread; |
sewardj | a922b61 | 2005-03-11 02:47:32 +0000 | [diff] [blame] | 2455 | tst->os_state.exitcode = ARG1; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 2456 | SET_STATUS_Success(0); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2457 | } |
| 2458 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 2459 | PRE(sys_ni_syscall) |
nethercote | eb1c7b7 | 2004-11-11 19:43:50 +0000 | [diff] [blame] | 2460 | { |
njn | 1a1e95c | 2009-06-03 06:50:06 +0000 | [diff] [blame] | 2461 | PRINT("unimplemented (by the kernel) syscall: %s! (ni_syscall)\n", |
| 2462 | VG_SYSNUM_STRING(SYSNO)); |
nethercote | eb1c7b7 | 2004-11-11 19:43:50 +0000 | [diff] [blame] | 2463 | PRE_REG_READ0(long, "ni_syscall"); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 2464 | SET_STATUS_Failure( VKI_ENOSYS ); |
nethercote | eb1c7b7 | 2004-11-11 19:43:50 +0000 | [diff] [blame] | 2465 | } |
| 2466 | |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2467 | PRE(sys_iopl) |
| 2468 | { |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 2469 | PRINT("sys_iopl ( %lu )", ARG1); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2470 | PRE_REG_READ1(long, "iopl", unsigned long, level); |
| 2471 | } |
| 2472 | |
sewardj | 78b50e4 | 2005-06-08 01:47:28 +0000 | [diff] [blame] | 2473 | PRE(sys_fsync) |
| 2474 | { |
| 2475 | *flags |= SfMayBlock; |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 2476 | PRINT("sys_fsync ( %lu )", ARG1); |
sewardj | 78b50e4 | 2005-06-08 01:47:28 +0000 | [diff] [blame] | 2477 | PRE_REG_READ1(long, "fsync", unsigned int, fd); |
| 2478 | } |
| 2479 | |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2480 | PRE(sys_fdatasync) |
| 2481 | { |
| 2482 | *flags |= SfMayBlock; |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 2483 | PRINT("sys_fdatasync ( %lu )", ARG1); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2484 | PRE_REG_READ1(long, "fdatasync", unsigned int, fd); |
| 2485 | } |
| 2486 | |
| 2487 | PRE(sys_msync) |
| 2488 | { |
| 2489 | *flags |= SfMayBlock; |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 2490 | PRINT("sys_msync ( %#lx, %lu, %#lx )", ARG1, ARG2, ARG3); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2491 | PRE_REG_READ3(long, "msync", |
| 2492 | unsigned long, start, vki_size_t, length, int, flags); |
| 2493 | PRE_MEM_READ( "msync(start)", ARG1, ARG2 ); |
| 2494 | } |
| 2495 | |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 2496 | // Nb: getpmsg() and putpmsg() are special additional syscalls used in early |
| 2497 | // versions of LiS (Linux Streams). They are not part of the kernel. |
| 2498 | // Therefore, we have to provide this type ourself, rather than getting it |
| 2499 | // from the kernel sources. |
| 2500 | struct vki_pmsg_strbuf { |
| 2501 | int maxlen; /* no. of bytes in buffer */ |
| 2502 | int len; /* no. of bytes returned */ |
| 2503 | vki_caddr_t buf; /* pointer to data */ |
| 2504 | }; |
| 2505 | PRE(sys_getpmsg) |
| 2506 | { |
| 2507 | /* LiS getpmsg from http://www.gcom.com/home/linux/lis/ */ |
| 2508 | struct vki_pmsg_strbuf *ctrl; |
| 2509 | struct vki_pmsg_strbuf *data; |
| 2510 | *flags |= SfMayBlock; |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 2511 | PRINT("sys_getpmsg ( %ld, %#lx, %#lx, %#lx, %#lx )", SARG1, ARG2, ARG3, |
| 2512 | ARG4, ARG5); |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 2513 | PRE_REG_READ5(int, "getpmsg", |
| 2514 | int, fd, struct strbuf *, ctrl, struct strbuf *, data, |
| 2515 | int *, bandp, int *, flagsp); |
| 2516 | ctrl = (struct vki_pmsg_strbuf *)ARG2; |
| 2517 | data = (struct vki_pmsg_strbuf *)ARG3; |
| 2518 | if (ctrl && ctrl->maxlen > 0) |
| 2519 | PRE_MEM_WRITE( "getpmsg(ctrl)", (Addr)ctrl->buf, ctrl->maxlen); |
| 2520 | if (data && data->maxlen > 0) |
| 2521 | PRE_MEM_WRITE( "getpmsg(data)", (Addr)data->buf, data->maxlen); |
| 2522 | if (ARG4) |
| 2523 | PRE_MEM_WRITE( "getpmsg(bandp)", (Addr)ARG4, sizeof(int)); |
| 2524 | if (ARG5) |
| 2525 | PRE_MEM_WRITE( "getpmsg(flagsp)", (Addr)ARG5, sizeof(int)); |
| 2526 | } |
| 2527 | POST(sys_getpmsg) |
| 2528 | { |
| 2529 | struct vki_pmsg_strbuf *ctrl; |
| 2530 | struct vki_pmsg_strbuf *data; |
| 2531 | vg_assert(SUCCESS); |
| 2532 | ctrl = (struct vki_pmsg_strbuf *)ARG2; |
| 2533 | data = (struct vki_pmsg_strbuf *)ARG3; |
| 2534 | if (RES == 0 && ctrl && ctrl->len > 0) { |
| 2535 | POST_MEM_WRITE( (Addr)ctrl->buf, ctrl->len); |
| 2536 | } |
| 2537 | if (RES == 0 && data && data->len > 0) { |
| 2538 | POST_MEM_WRITE( (Addr)data->buf, data->len); |
| 2539 | } |
| 2540 | } |
| 2541 | |
| 2542 | PRE(sys_putpmsg) |
| 2543 | { |
| 2544 | /* LiS putpmsg from http://www.gcom.com/home/linux/lis/ */ |
| 2545 | struct vki_pmsg_strbuf *ctrl; |
| 2546 | struct vki_pmsg_strbuf *data; |
| 2547 | *flags |= SfMayBlock; |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 2548 | PRINT("sys_putpmsg ( %ld, %#lx, %#lx, %ld, %ld )", SARG1, ARG2, ARG3, |
| 2549 | SARG4, SARG5); |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 2550 | PRE_REG_READ5(int, "putpmsg", |
| 2551 | int, fd, struct strbuf *, ctrl, struct strbuf *, data, |
| 2552 | int, band, int, flags); |
| 2553 | ctrl = (struct vki_pmsg_strbuf *)ARG2; |
| 2554 | data = (struct vki_pmsg_strbuf *)ARG3; |
| 2555 | if (ctrl && ctrl->len > 0) |
| 2556 | PRE_MEM_READ( "putpmsg(ctrl)", (Addr)ctrl->buf, ctrl->len); |
| 2557 | if (data && data->len > 0) |
| 2558 | PRE_MEM_READ( "putpmsg(data)", (Addr)data->buf, data->len); |
| 2559 | } |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2560 | |
| 2561 | PRE(sys_getitimer) |
| 2562 | { |
njn | 72bbd8d | 2009-05-22 08:12:46 +0000 | [diff] [blame] | 2563 | struct vki_itimerval *value = (struct vki_itimerval*)ARG2; |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 2564 | PRINT("sys_getitimer ( %ld, %#lx )", SARG1, ARG2); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2565 | PRE_REG_READ2(long, "getitimer", int, which, struct itimerval *, value); |
njn | 72bbd8d | 2009-05-22 08:12:46 +0000 | [diff] [blame] | 2566 | |
| 2567 | PRE_timeval_WRITE( "getitimer(&value->it_interval)", &(value->it_interval)); |
| 2568 | PRE_timeval_WRITE( "getitimer(&value->it_value)", &(value->it_value)); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2569 | } |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2570 | |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2571 | POST(sys_getitimer) |
| 2572 | { |
| 2573 | if (ARG2 != (Addr)NULL) { |
njn | 72bbd8d | 2009-05-22 08:12:46 +0000 | [diff] [blame] | 2574 | struct vki_itimerval *value = (struct vki_itimerval*)ARG2; |
| 2575 | POST_timeval_WRITE( &(value->it_interval) ); |
| 2576 | POST_timeval_WRITE( &(value->it_value) ); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2577 | } |
| 2578 | } |
| 2579 | |
| 2580 | PRE(sys_setitimer) |
| 2581 | { |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 2582 | PRINT("sys_setitimer ( %ld, %#lx, %#lx )", SARG1, ARG2, ARG3); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2583 | PRE_REG_READ3(long, "setitimer", |
| 2584 | int, which, |
| 2585 | struct itimerval *, value, struct itimerval *, ovalue); |
njn | 72bbd8d | 2009-05-22 08:12:46 +0000 | [diff] [blame] | 2586 | if (ARG2 != (Addr)NULL) { |
| 2587 | struct vki_itimerval *value = (struct vki_itimerval*)ARG2; |
| 2588 | PRE_timeval_READ( "setitimer(&value->it_interval)", |
| 2589 | &(value->it_interval)); |
| 2590 | PRE_timeval_READ( "setitimer(&value->it_value)", |
| 2591 | &(value->it_value)); |
| 2592 | } |
| 2593 | if (ARG3 != (Addr)NULL) { |
| 2594 | struct vki_itimerval *ovalue = (struct vki_itimerval*)ARG3; |
| 2595 | PRE_timeval_WRITE( "setitimer(&ovalue->it_interval)", |
| 2596 | &(ovalue->it_interval)); |
| 2597 | PRE_timeval_WRITE( "setitimer(&ovalue->it_value)", |
| 2598 | &(ovalue->it_value)); |
| 2599 | } |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2600 | } |
| 2601 | |
| 2602 | POST(sys_setitimer) |
| 2603 | { |
| 2604 | if (ARG3 != (Addr)NULL) { |
njn | 72bbd8d | 2009-05-22 08:12:46 +0000 | [diff] [blame] | 2605 | struct vki_itimerval *ovalue = (struct vki_itimerval*)ARG3; |
| 2606 | POST_timeval_WRITE( &(ovalue->it_interval) ); |
| 2607 | POST_timeval_WRITE( &(ovalue->it_value) ); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2608 | } |
| 2609 | } |
| 2610 | |
| 2611 | PRE(sys_chroot) |
| 2612 | { |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 2613 | PRINT("sys_chroot ( %#lx )", ARG1); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2614 | PRE_REG_READ1(long, "chroot", const char *, path); |
| 2615 | PRE_MEM_RASCIIZ( "chroot(path)", ARG1 ); |
| 2616 | } |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 2617 | |
| 2618 | PRE(sys_madvise) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2619 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 2620 | *flags |= SfMayBlock; |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 2621 | PRINT("sys_madvise ( %#lx, %lu, %ld )", ARG1, ARG2, SARG3); |
nethercote | ac866b9 | 2004-11-15 20:23:15 +0000 | [diff] [blame] | 2622 | PRE_REG_READ3(long, "madvise", |
| 2623 | unsigned long, start, vki_size_t, length, int, advice); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2624 | } |
| 2625 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2626 | #if HAVE_MREMAP |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 2627 | PRE(sys_mremap) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2628 | { |
nethercote | 27ea8bc | 2004-07-10 17:21:14 +0000 | [diff] [blame] | 2629 | // Nb: this is different to the glibc version described in the man pages, |
| 2630 | // which lacks the fifth 'new_address' argument. |
tom | c00fcc7 | 2006-05-08 11:58:06 +0000 | [diff] [blame] | 2631 | if (ARG4 & VKI_MREMAP_FIXED) { |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 2632 | PRINT("sys_mremap ( %#lx, %lu, %lu, %#lx, %#lx )", |
| 2633 | ARG1, ARG2, ARG3, ARG4, ARG5); |
tom | c00fcc7 | 2006-05-08 11:58:06 +0000 | [diff] [blame] | 2634 | PRE_REG_READ5(unsigned long, "mremap", |
| 2635 | unsigned long, old_addr, unsigned long, old_size, |
| 2636 | unsigned long, new_size, unsigned long, flags, |
| 2637 | unsigned long, new_addr); |
| 2638 | } else { |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 2639 | PRINT("sys_mremap ( %#lx, %lu, %lu, 0x%lx )", |
| 2640 | ARG1, ARG2, ARG3, ARG4); |
tom | c00fcc7 | 2006-05-08 11:58:06 +0000 | [diff] [blame] | 2641 | PRE_REG_READ4(unsigned long, "mremap", |
| 2642 | unsigned long, old_addr, unsigned long, old_size, |
| 2643 | unsigned long, new_size, unsigned long, flags); |
| 2644 | } |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 2645 | SET_STATUS_from_SysRes( |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2646 | do_mremap((Addr)ARG1, ARG2, (Addr)ARG5, ARG3, ARG4, tid) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 2647 | ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2648 | } |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2649 | #endif /* HAVE_MREMAP */ |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2650 | |
sewardj | 8c9ea4e | 2005-06-08 10:46:56 +0000 | [diff] [blame] | 2651 | PRE(sys_nice) |
| 2652 | { |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 2653 | PRINT("sys_nice ( %ld )", SARG1); |
sewardj | 8c9ea4e | 2005-06-08 10:46:56 +0000 | [diff] [blame] | 2654 | PRE_REG_READ1(long, "nice", int, inc); |
| 2655 | } |
sewardj | 78b50e4 | 2005-06-08 01:47:28 +0000 | [diff] [blame] | 2656 | |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2657 | PRE(sys_mlock) |
| 2658 | { |
| 2659 | *flags |= SfMayBlock; |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 2660 | PRINT("sys_mlock ( %#lx, %lu )", ARG1, ARG2); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2661 | PRE_REG_READ2(long, "mlock", unsigned long, addr, vki_size_t, len); |
| 2662 | } |
| 2663 | |
| 2664 | PRE(sys_munlock) |
| 2665 | { |
| 2666 | *flags |= SfMayBlock; |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 2667 | PRINT("sys_munlock ( %#lx, %lu )", ARG1, ARG2); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2668 | PRE_REG_READ2(long, "munlock", unsigned long, addr, vki_size_t, len); |
| 2669 | } |
| 2670 | |
| 2671 | PRE(sys_mlockall) |
| 2672 | { |
| 2673 | *flags |= SfMayBlock; |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 2674 | PRINT("sys_mlockall ( %lx )", ARG1); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2675 | PRE_REG_READ1(long, "mlockall", int, flags); |
| 2676 | } |
| 2677 | |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2678 | PRE(sys_setpriority) |
| 2679 | { |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 2680 | PRINT("sys_setpriority ( %ld, %ld, %ld )", SARG1, SARG2, SARG3); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2681 | PRE_REG_READ3(long, "setpriority", int, which, int, who, int, prio); |
| 2682 | } |
| 2683 | |
| 2684 | PRE(sys_getpriority) |
| 2685 | { |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 2686 | PRINT("sys_getpriority ( %ld, %ld )", SARG1, SARG2); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2687 | PRE_REG_READ2(long, "getpriority", int, which, int, who); |
| 2688 | } |
| 2689 | |
njn | 7b1edbd | 2009-05-19 06:50:37 +0000 | [diff] [blame] | 2690 | PRE(sys_pwrite64) |
sewardj | edc9547 | 2008-08-07 20:29:55 +0000 | [diff] [blame] | 2691 | { |
sewardj | edc9547 | 2008-08-07 20:29:55 +0000 | [diff] [blame] | 2692 | *flags |= SfMayBlock; |
njn | 7b1edbd | 2009-05-19 06:50:37 +0000 | [diff] [blame] | 2693 | #if VG_WORDSIZE == 4 |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 2694 | PRINT("sys_pwrite64 ( %lu, %#lx, %lu, %lld )", |
| 2695 | ARG1, ARG2, ARG3, (Long)MERGE64(ARG4,ARG5)); |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 2696 | PRE_REG_READ5(ssize_t, "pwrite64", |
| 2697 | unsigned int, fd, const char *, buf, vki_size_t, count, |
sewardj | 0ca89e0 | 2009-11-25 13:57:47 +0000 | [diff] [blame] | 2698 | vki_u32, MERGE64_FIRST(offset), vki_u32, MERGE64_SECOND(offset)); |
njn | 7b1edbd | 2009-05-19 06:50:37 +0000 | [diff] [blame] | 2699 | #elif VG_WORDSIZE == 8 |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 2700 | PRINT("sys_pwrite64 ( %lu, %#lx, %lu, %ld )", |
| 2701 | ARG1, ARG2, ARG3, SARG4); |
njn | 7b1edbd | 2009-05-19 06:50:37 +0000 | [diff] [blame] | 2702 | PRE_REG_READ4(ssize_t, "pwrite64", |
| 2703 | unsigned int, fd, const char *, buf, vki_size_t, count, |
| 2704 | Word, offset); |
| 2705 | #else |
| 2706 | # error Unexpected word size |
| 2707 | #endif |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 2708 | PRE_MEM_READ( "pwrite64(buf)", ARG2, ARG3 ); |
| 2709 | } |
sewardj | 8c9ea4e | 2005-06-08 10:46:56 +0000 | [diff] [blame] | 2710 | |
| 2711 | PRE(sys_sync) |
| 2712 | { |
| 2713 | *flags |= SfMayBlock; |
| 2714 | PRINT("sys_sync ( )"); |
| 2715 | PRE_REG_READ0(long, "sync"); |
| 2716 | } |
| 2717 | |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2718 | PRE(sys_fstatfs) |
| 2719 | { |
sewardj | cc3de2d | 2011-08-18 15:08:20 +0000 | [diff] [blame] | 2720 | FUSE_COMPATIBLE_MAY_BLOCK(); |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 2721 | PRINT("sys_fstatfs ( %lu, %#lx )", ARG1, ARG2); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2722 | PRE_REG_READ2(long, "fstatfs", |
| 2723 | unsigned int, fd, struct statfs *, buf); |
| 2724 | PRE_MEM_WRITE( "fstatfs(buf)", ARG2, sizeof(struct vki_statfs) ); |
| 2725 | } |
| 2726 | |
| 2727 | POST(sys_fstatfs) |
| 2728 | { |
| 2729 | POST_MEM_WRITE( ARG2, sizeof(struct vki_statfs) ); |
| 2730 | } |
| 2731 | |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 2732 | PRE(sys_fstatfs64) |
| 2733 | { |
sewardj | cc3de2d | 2011-08-18 15:08:20 +0000 | [diff] [blame] | 2734 | FUSE_COMPATIBLE_MAY_BLOCK(); |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 2735 | PRINT("sys_fstatfs64 ( %lu, %lu, %#lx )", ARG1, ARG2, ARG3); |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 2736 | PRE_REG_READ3(long, "fstatfs64", |
| 2737 | unsigned int, fd, vki_size_t, size, struct statfs64 *, buf); |
| 2738 | PRE_MEM_WRITE( "fstatfs64(buf)", ARG3, ARG2 ); |
| 2739 | } |
| 2740 | POST(sys_fstatfs64) |
| 2741 | { |
| 2742 | POST_MEM_WRITE( ARG3, ARG2 ); |
| 2743 | } |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2744 | |
| 2745 | PRE(sys_getsid) |
| 2746 | { |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 2747 | PRINT("sys_getsid ( %ld )", SARG1); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2748 | PRE_REG_READ1(long, "getsid", vki_pid_t, pid); |
| 2749 | } |
| 2750 | |
njn | 7b1edbd | 2009-05-19 06:50:37 +0000 | [diff] [blame] | 2751 | PRE(sys_pread64) |
sewardj | edc9547 | 2008-08-07 20:29:55 +0000 | [diff] [blame] | 2752 | { |
sewardj | edc9547 | 2008-08-07 20:29:55 +0000 | [diff] [blame] | 2753 | *flags |= SfMayBlock; |
njn | 7b1edbd | 2009-05-19 06:50:37 +0000 | [diff] [blame] | 2754 | #if VG_WORDSIZE == 4 |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 2755 | PRINT("sys_pread64 ( %lu, %#lx, %lu, %lld )", |
| 2756 | ARG1, ARG2, ARG3, (Long)MERGE64(ARG4,ARG5)); |
sewardj | e7aa4ae | 2005-06-09 12:43:42 +0000 | [diff] [blame] | 2757 | PRE_REG_READ5(ssize_t, "pread64", |
| 2758 | unsigned int, fd, char *, buf, vki_size_t, count, |
tom | ca78724 | 2009-11-25 11:24:00 +0000 | [diff] [blame] | 2759 | vki_u32, MERGE64_FIRST(offset), vki_u32, MERGE64_SECOND(offset)); |
njn | 7b1edbd | 2009-05-19 06:50:37 +0000 | [diff] [blame] | 2760 | #elif VG_WORDSIZE == 8 |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 2761 | PRINT("sys_pread64 ( %lu, %#lx, %lu, %ld )", |
| 2762 | ARG1, ARG2, ARG3, SARG4); |
njn | 7b1edbd | 2009-05-19 06:50:37 +0000 | [diff] [blame] | 2763 | PRE_REG_READ4(ssize_t, "pread64", |
| 2764 | unsigned int, fd, char *, buf, vki_size_t, count, |
| 2765 | Word, offset); |
| 2766 | #else |
| 2767 | # error Unexpected word size |
| 2768 | #endif |
sewardj | e7aa4ae | 2005-06-09 12:43:42 +0000 | [diff] [blame] | 2769 | PRE_MEM_WRITE( "pread64(buf)", ARG2, ARG3 ); |
| 2770 | } |
njn | 7b1edbd | 2009-05-19 06:50:37 +0000 | [diff] [blame] | 2771 | POST(sys_pread64) |
sewardj | e7aa4ae | 2005-06-09 12:43:42 +0000 | [diff] [blame] | 2772 | { |
| 2773 | vg_assert(SUCCESS); |
| 2774 | if (RES > 0) { |
| 2775 | POST_MEM_WRITE( ARG2, RES ); |
| 2776 | } |
| 2777 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2778 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 2779 | PRE(sys_mknod) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2780 | { |
sewardj | cc3de2d | 2011-08-18 15:08:20 +0000 | [diff] [blame] | 2781 | FUSE_COMPATIBLE_MAY_BLOCK(); |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 2782 | PRINT("sys_mknod ( %#lx(%s), %#lx, %#lx )", ARG1, (HChar*)ARG1, ARG2, ARG3 ); |
nethercote | c6851dd | 2004-11-11 18:00:47 +0000 | [diff] [blame] | 2783 | PRE_REG_READ3(long, "mknod", |
| 2784 | const char *, pathname, int, mode, unsigned, dev); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 2785 | PRE_MEM_RASCIIZ( "mknod(pathname)", ARG1 ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2788 | PRE(sys_flock) |
| 2789 | { |
| 2790 | *flags |= SfMayBlock; |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 2791 | PRINT("sys_flock ( %lu, %lu )", ARG1, ARG2 ); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 2792 | PRE_REG_READ2(long, "flock", unsigned int, fd, unsigned int, operation); |
| 2793 | } |
| 2794 | |
nethercote | a81e916 | 2004-02-12 14:34:14 +0000 | [diff] [blame] | 2795 | // Pre_read a char** argument. |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 2796 | void ML_(pre_argv_envp)(Addr a, ThreadId tid, const HChar *s1, const HChar *s2) |
nethercote | a81e916 | 2004-02-12 14:34:14 +0000 | [diff] [blame] | 2797 | { |
| 2798 | while (True) { |
njn | b249fd7 | 2004-11-29 14:24:57 +0000 | [diff] [blame] | 2799 | Addr a_deref; |
| 2800 | Addr* a_p = (Addr*)a; |
| 2801 | PRE_MEM_READ( s1, (Addr)a_p, sizeof(Addr) ); |
| 2802 | a_deref = *a_p; |
nethercote | a81e916 | 2004-02-12 14:34:14 +0000 | [diff] [blame] | 2803 | if (0 == a_deref) |
| 2804 | break; |
nethercote | ef0c766 | 2004-11-06 15:38:43 +0000 | [diff] [blame] | 2805 | PRE_MEM_RASCIIZ( s2, a_deref ); |
nethercote | a81e916 | 2004-02-12 14:34:14 +0000 | [diff] [blame] | 2806 | a += sizeof(char*); |
| 2807 | } |
| 2808 | } |
| 2809 | |
njn | 7b85dd5 | 2005-06-12 17:26:29 +0000 | [diff] [blame] | 2810 | static Bool i_am_the_only_thread ( void ) |
| 2811 | { |
| 2812 | Int c = VG_(count_living_threads)(); |
| 2813 | vg_assert(c >= 1); /* stay sane */ |
| 2814 | return c == 1; |
| 2815 | } |
| 2816 | |
| 2817 | /* Wait until all other threads disappear. */ |
| 2818 | void VG_(reap_threads)(ThreadId self) |
| 2819 | { |
| 2820 | while (!i_am_the_only_thread()) { |
| 2821 | /* Let other thread(s) run */ |
| 2822 | VG_(vg_yield)(); |
| 2823 | VG_(poll_signals)(self); |
| 2824 | } |
| 2825 | vg_assert(i_am_the_only_thread()); |
| 2826 | } |
| 2827 | |
nethercote | 7310afb | 2004-11-12 15:41:06 +0000 | [diff] [blame] | 2828 | // XXX: prototype here seemingly doesn't match the prototype for i386-linux, |
| 2829 | // but it seems to work nonetheless... |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 2830 | PRE(sys_execve) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2831 | { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 2832 | HChar* path = NULL; /* path to executable */ |
| 2833 | HChar** envp = NULL; |
| 2834 | HChar** argv = NULL; |
| 2835 | HChar** arg2copy; |
| 2836 | HChar* launcher_basename = NULL; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 2837 | ThreadState* tst; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2838 | Int i, j, tot_args; |
njn | 7375061 | 2005-10-14 03:11:30 +0000 | [diff] [blame] | 2839 | SysRes res; |
sewardj | 0642127 | 2009-11-05 08:55:13 +0000 | [diff] [blame] | 2840 | Bool setuid_allowed, trace_this_child; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2841 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 2842 | PRINT("sys_execve ( %#lx(%s), %#lx, %#lx )", ARG1, (HChar*)ARG1, ARG2, ARG3); |
nethercote | 7310afb | 2004-11-12 15:41:06 +0000 | [diff] [blame] | 2843 | PRE_REG_READ3(vki_off_t, "execve", |
| 2844 | char *, filename, char **, argv, char **, envp); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 2845 | PRE_MEM_RASCIIZ( "execve(filename)", ARG1 ); |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 2846 | if (ARG2 != 0) { |
| 2847 | /* At least the terminating NULL must be addressable. */ |
| 2848 | if (!ML_(safe_to_deref)((HChar **) ARG2, sizeof(HChar *))) { |
| 2849 | SET_STATUS_Failure(VKI_EFAULT); |
| 2850 | return; |
| 2851 | } |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 2852 | ML_(pre_argv_envp)( ARG2, tid, "execve(argv)", "execve(argv[i])" ); |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 2853 | } |
| 2854 | if (ARG3 != 0) { |
| 2855 | /* At least the terminating NULL must be addressable. */ |
| 2856 | if (!ML_(safe_to_deref)((HChar **) ARG3, sizeof(HChar *))) { |
| 2857 | SET_STATUS_Failure(VKI_EFAULT); |
| 2858 | return; |
| 2859 | } |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 2860 | ML_(pre_argv_envp)( ARG3, tid, "execve(envp)", "execve(envp[i])" ); |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 2861 | } |
fitzhardinge | e1c06d8 | 2003-10-30 07:21:44 +0000 | [diff] [blame] | 2862 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 2863 | vg_assert(VG_(is_valid_tid)(tid)); |
| 2864 | tst = VG_(get_ThreadState)(tid); |
| 2865 | |
fitzhardinge | e1c06d8 | 2003-10-30 07:21:44 +0000 | [diff] [blame] | 2866 | /* Erk. If the exec fails, then the following will have made a |
| 2867 | mess of things which makes it hard for us to continue. The |
| 2868 | right thing to do is piece everything together again in |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2869 | POST(execve), but that's close to impossible. Instead, we make |
| 2870 | an effort to check that the execve will work before actually |
| 2871 | doing it. */ |
fitzhardinge | e1c06d8 | 2003-10-30 07:21:44 +0000 | [diff] [blame] | 2872 | |
njn | 7375061 | 2005-10-14 03:11:30 +0000 | [diff] [blame] | 2873 | /* Check that the name at least begins in client-accessible storage. */ |
sewardj | 0642127 | 2009-11-05 08:55:13 +0000 | [diff] [blame] | 2874 | if (ARG1 == 0 /* obviously bogus */ |
| 2875 | || !VG_(am_is_valid_for_client)( ARG1, 1, VKI_PROT_READ )) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2876 | SET_STATUS_Failure( VKI_EFAULT ); |
| 2877 | return; |
| 2878 | } |
| 2879 | |
sewardj | 9ab64a4 | 2010-12-06 11:40:04 +0000 | [diff] [blame] | 2880 | // debug-only printing |
| 2881 | if (0) { |
| 2882 | VG_(printf)("ARG1 = %p(%s)\n", (void*)ARG1, (HChar*)ARG1); |
| 2883 | if (ARG2) { |
| 2884 | VG_(printf)("ARG2 = "); |
| 2885 | Int q; |
| 2886 | HChar** vec = (HChar**)ARG2; |
| 2887 | for (q = 0; vec[q]; q++) |
| 2888 | VG_(printf)("%p(%s) ", vec[q], vec[q]); |
| 2889 | VG_(printf)("\n"); |
| 2890 | } else { |
| 2891 | VG_(printf)("ARG2 = null\n"); |
| 2892 | } |
| 2893 | } |
| 2894 | |
sewardj | 0642127 | 2009-11-05 08:55:13 +0000 | [diff] [blame] | 2895 | // Decide whether or not we want to follow along |
sewardj | 9ab64a4 | 2010-12-06 11:40:04 +0000 | [diff] [blame] | 2896 | { // Make 'child_argv' be a pointer to the child's arg vector |
| 2897 | // (skipping the exe name) |
florian | 518850b | 2014-10-22 22:25:30 +0000 | [diff] [blame] | 2898 | const HChar** child_argv = (const HChar**)ARG2; |
sewardj | 9ab64a4 | 2010-12-06 11:40:04 +0000 | [diff] [blame] | 2899 | if (child_argv && child_argv[0] == NULL) |
| 2900 | child_argv = NULL; |
| 2901 | trace_this_child = VG_(should_we_trace_this_child)( (HChar*)ARG1, child_argv ); |
| 2902 | } |
sewardj | 0642127 | 2009-11-05 08:55:13 +0000 | [diff] [blame] | 2903 | |
njn | 7375061 | 2005-10-14 03:11:30 +0000 | [diff] [blame] | 2904 | // Do the important checks: it is a file, is executable, permissions are |
sewardj | c74b3ba | 2007-11-17 21:11:57 +0000 | [diff] [blame] | 2905 | // ok, etc. We allow setuid executables to run only in the case when |
| 2906 | // we are not simulating them, that is, they to be run natively. |
sewardj | 0642127 | 2009-11-05 08:55:13 +0000 | [diff] [blame] | 2907 | setuid_allowed = trace_this_child ? False : True; |
florian | e543f30 | 2012-10-21 19:43:43 +0000 | [diff] [blame] | 2908 | res = VG_(pre_exec_check)((const HChar *)ARG1, NULL, setuid_allowed); |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 2909 | if (sr_isError(res)) { |
| 2910 | SET_STATUS_Failure( sr_Err(res) ); |
njn | 7375061 | 2005-10-14 03:11:30 +0000 | [diff] [blame] | 2911 | return; |
| 2912 | } |
| 2913 | |
sewardj | 98e68a4 | 2005-10-04 23:07:33 +0000 | [diff] [blame] | 2914 | /* If we're tracing the child, and the launcher name looks bogus |
| 2915 | (possibly because launcher.c couldn't figure it out, see |
| 2916 | comments therein) then we have no option but to fail. */ |
sewardj | 0642127 | 2009-11-05 08:55:13 +0000 | [diff] [blame] | 2917 | if (trace_this_child |
sewardj | 98e68a4 | 2005-10-04 23:07:33 +0000 | [diff] [blame] | 2918 | && (VG_(name_of_launcher) == NULL |
| 2919 | || VG_(name_of_launcher)[0] != '/')) { |
| 2920 | SET_STATUS_Failure( VKI_ECHILD ); /* "No child processes" */ |
| 2921 | return; |
| 2922 | } |
| 2923 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2924 | /* After this point, we can't recover if the execve fails. */ |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 2925 | VG_(debugLog)(1, "syswrap", "Exec of %s\n", (HChar*)ARG1); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2926 | |
sewardj | 997546c | 2011-05-17 18:14:53 +0000 | [diff] [blame] | 2927 | |
| 2928 | // Terminate gdbserver if it is active. |
| 2929 | if (VG_(clo_vgdb) != Vg_VgdbNo) { |
| 2930 | // If the child will not be traced, we need to terminate gdbserver |
| 2931 | // to cleanup the gdbserver resources (e.g. the FIFO files). |
| 2932 | // If child will be traced, we also terminate gdbserver: the new |
| 2933 | // Valgrind will start a fresh gdbserver after exec. |
| 2934 | VG_(gdbserver) (0); |
| 2935 | } |
| 2936 | |
fitzhardinge | e1c06d8 | 2003-10-30 07:21:44 +0000 | [diff] [blame] | 2937 | /* Resistance is futile. Nuke all other threads. POSIX mandates |
| 2938 | this. (Really, nuke them all, since the new process will make |
| 2939 | its own new thread.) */ |
sewardj | ef1cf8b | 2006-10-17 01:47:30 +0000 | [diff] [blame] | 2940 | VG_(nuke_all_threads_except)( tid, VgSrc_ExitThread ); |
njn | 7b85dd5 | 2005-06-12 17:26:29 +0000 | [diff] [blame] | 2941 | VG_(reap_threads)(tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2942 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2943 | // Set up the child's exe path. |
| 2944 | // |
sewardj | 0642127 | 2009-11-05 08:55:13 +0000 | [diff] [blame] | 2945 | if (trace_this_child) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2946 | |
| 2947 | // We want to exec the launcher. Get its pre-remembered path. |
| 2948 | path = VG_(name_of_launcher); |
| 2949 | // VG_(name_of_launcher) should have been acquired by m_main at |
| 2950 | // startup. |
| 2951 | vg_assert(path); |
| 2952 | |
| 2953 | launcher_basename = VG_(strrchr)(path, '/'); |
| 2954 | if (launcher_basename == NULL || launcher_basename[1] == 0) { |
| 2955 | launcher_basename = path; // hmm, tres dubious |
| 2956 | } else { |
| 2957 | launcher_basename++; |
| 2958 | } |
| 2959 | |
| 2960 | } else { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 2961 | path = (HChar*)ARG1; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2962 | } |
| 2963 | |
| 2964 | // Set up the child's environment. |
| 2965 | // |
tom | 4e13695 | 2005-07-18 14:02:11 +0000 | [diff] [blame] | 2966 | // Remove the valgrind-specific stuff from the environment so the |
njn | 7b4e5ba | 2005-08-25 22:53:57 +0000 | [diff] [blame] | 2967 | // child doesn't get vgpreload_core.so, vgpreload_<tool>.so, etc. |
tom | 4e13695 | 2005-07-18 14:02:11 +0000 | [diff] [blame] | 2968 | // This is done unconditionally, since if we are tracing the child, |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2969 | // the child valgrind will set up the appropriate client environment. |
njn | 1110699 | 2005-07-18 14:10:12 +0000 | [diff] [blame] | 2970 | // Nb: we make a copy of the environment before trying to mangle it |
| 2971 | // as it might be in read-only memory (this was bug #101881). |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2972 | // |
| 2973 | // Then, if tracing the child, set VALGRIND_LIB for it. |
| 2974 | // |
| 2975 | if (ARG3 == 0) { |
| 2976 | envp = NULL; |
| 2977 | } else { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 2978 | envp = VG_(env_clone)( (HChar**)ARG3 ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2979 | if (envp == NULL) goto hosed; |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 2980 | VG_(env_remove_valgrind_env_stuff)( envp, True /*ro_strings*/, NULL ); |
fitzhardinge | 5408c06 | 2004-01-04 23:52:59 +0000 | [diff] [blame] | 2981 | } |
| 2982 | |
sewardj | 0642127 | 2009-11-05 08:55:13 +0000 | [diff] [blame] | 2983 | if (trace_this_child) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2984 | // Set VALGRIND_LIB in ARG3 (the environment) |
| 2985 | VG_(env_setenv)( &envp, VALGRIND_LIB, VG_(libdir)); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2986 | } |
| 2987 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2988 | // Set up the child's args. If not tracing it, they are |
| 2989 | // simply ARG2. Otherwise, they are |
| 2990 | // |
| 2991 | // [launcher_basename] ++ VG_(args_for_valgrind) ++ [ARG1] ++ ARG2[1..] |
| 2992 | // |
| 2993 | // except that the first VG_(args_for_valgrind_noexecpass) args |
| 2994 | // are omitted. |
| 2995 | // |
sewardj | 0642127 | 2009-11-05 08:55:13 +0000 | [diff] [blame] | 2996 | if (!trace_this_child) { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 2997 | argv = (HChar**)ARG2; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2998 | } else { |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 2999 | vg_assert( VG_(args_for_valgrind) ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3000 | vg_assert( VG_(args_for_valgrind_noexecpass) >= 0 ); |
| 3001 | vg_assert( VG_(args_for_valgrind_noexecpass) |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 3002 | <= VG_(sizeXA)( VG_(args_for_valgrind) ) ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3003 | /* how many args in total will there be? */ |
| 3004 | // launcher basename |
| 3005 | tot_args = 1; |
| 3006 | // V's args |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 3007 | tot_args += VG_(sizeXA)( VG_(args_for_valgrind) ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3008 | tot_args -= VG_(args_for_valgrind_noexecpass); |
| 3009 | // name of client exe |
| 3010 | tot_args++; |
| 3011 | // args for client exe, skipping [0] |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 3012 | arg2copy = (HChar**)ARG2; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3013 | if (arg2copy && arg2copy[0]) { |
| 3014 | for (i = 1; arg2copy[i]; i++) |
| 3015 | tot_args++; |
| 3016 | } |
| 3017 | // allocate |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 3018 | argv = VG_(malloc)( "di.syswrap.pre_sys_execve.1", |
| 3019 | (tot_args+1) * sizeof(HChar*) ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3020 | // copy |
| 3021 | j = 0; |
| 3022 | argv[j++] = launcher_basename; |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 3023 | for (i = 0; i < VG_(sizeXA)( VG_(args_for_valgrind) ); i++) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3024 | if (i < VG_(args_for_valgrind_noexecpass)) |
| 3025 | continue; |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 3026 | argv[j++] = * (HChar**) VG_(indexXA)( VG_(args_for_valgrind), i ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3027 | } |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 3028 | argv[j++] = (HChar*)ARG1; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3029 | if (arg2copy && arg2copy[0]) |
| 3030 | for (i = 1; arg2copy[i]; i++) |
| 3031 | argv[j++] = arg2copy[i]; |
| 3032 | argv[j++] = NULL; |
| 3033 | // check |
| 3034 | vg_assert(j == tot_args+1); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3035 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3036 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 3037 | /* |
| 3038 | Set the signal state up for exec. |
| 3039 | |
| 3040 | We need to set the real signal state to make sure the exec'd |
| 3041 | process gets SIG_IGN properly. |
| 3042 | |
| 3043 | Also set our real sigmask to match the client's sigmask so that |
| 3044 | the exec'd child will get the right mask. First we need to |
| 3045 | clear out any pending signals so they they don't get delivered, |
| 3046 | which would confuse things. |
fitzhardinge | f0dd7e1 | 2004-01-16 02:17:30 +0000 | [diff] [blame] | 3047 | |
| 3048 | XXX This is a bug - the signals should remain pending, and be |
| 3049 | delivered to the new process after exec. There's also a |
| 3050 | race-condition, since if someone delivers us a signal between |
| 3051 | the sigprocmask and the execve, we'll still get the signal. Oh |
| 3052 | well. |
| 3053 | */ |
| 3054 | { |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 3055 | vki_sigset_t allsigs; |
| 3056 | vki_siginfo_t info; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 3057 | |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 3058 | /* What this loop does: it queries SCSS (the signal state that |
| 3059 | the client _thinks_ the kernel is in) by calling |
| 3060 | VG_(do_sys_sigaction), and modifies the real kernel signal |
| 3061 | state accordingly. */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3062 | for (i = 1; i < VG_(max_signal); i++) { |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 3063 | vki_sigaction_fromK_t sa_f; |
| 3064 | vki_sigaction_toK_t sa_t; |
| 3065 | VG_(do_sys_sigaction)(i, NULL, &sa_f); |
| 3066 | VG_(convert_sigaction_fromK_to_toK)(&sa_f, &sa_t); |
| 3067 | if (sa_t.ksa_handler == VKI_SIG_IGN) |
| 3068 | VG_(sigaction)(i, &sa_t, NULL); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 3069 | else { |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 3070 | sa_t.ksa_handler = VKI_SIG_DFL; |
| 3071 | VG_(sigaction)(i, &sa_t, NULL); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 3072 | } |
| 3073 | } |
| 3074 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 3075 | VG_(sigfillset)(&allsigs); |
sewardj | ef1cf8b | 2006-10-17 01:47:30 +0000 | [diff] [blame] | 3076 | while(VG_(sigtimedwait_zero)(&allsigs, &info) > 0) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 3077 | ; |
fitzhardinge | f0dd7e1 | 2004-01-16 02:17:30 +0000 | [diff] [blame] | 3078 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 3079 | VG_(sigprocmask)(VKI_SIG_SETMASK, &tst->sig_mask, NULL); |
fitzhardinge | f0dd7e1 | 2004-01-16 02:17:30 +0000 | [diff] [blame] | 3080 | } |
| 3081 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3082 | if (0) { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 3083 | HChar **cpp; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3084 | VG_(printf)("exec: %s\n", path); |
| 3085 | for (cpp = argv; cpp && *cpp; cpp++) |
| 3086 | VG_(printf)("argv: %s\n", *cpp); |
| 3087 | if (0) |
| 3088 | for (cpp = envp; cpp && *cpp; cpp++) |
| 3089 | VG_(printf)("env: %s\n", *cpp); |
| 3090 | } |
| 3091 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3092 | SET_STATUS_from_SysRes( |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3093 | VG_(do_syscall3)(__NR_execve, (UWord)path, (UWord)argv, (UWord)envp) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3094 | ); |
fitzhardinge | b50068f | 2004-02-24 23:42:55 +0000 | [diff] [blame] | 3095 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3096 | /* If we got here, then the execve failed. We've already made way |
| 3097 | too much of a mess to continue, so we have to abort. */ |
| 3098 | hosed: |
tom | dc294c3 | 2005-08-16 10:19:58 +0000 | [diff] [blame] | 3099 | vg_assert(FAILURE); |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3100 | VG_(message)(Vg_UserMsg, "execve(%#lx(%s), %#lx, %#lx) failed, errno %lu\n", |
| 3101 | ARG1, (HChar*)ARG1, ARG2, ARG3, ERR); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 3102 | VG_(message)(Vg_UserMsg, "EXEC FAILED: I can't recover from " |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 3103 | "execve() failing, so I'm dying.\n"); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3104 | VG_(message)(Vg_UserMsg, "Add more stringent tests in PRE(sys_execve), " |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 3105 | "or work out how to recover.\n"); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 3106 | VG_(exit)(101); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3107 | } |
| 3108 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3109 | PRE(sys_access) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3110 | { |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3111 | PRINT("sys_access ( %#lx(%s), %ld )", ARG1, (HChar*)ARG1, SARG2); |
nethercote | 9a3beb9 | 2004-11-12 17:07:26 +0000 | [diff] [blame] | 3112 | PRE_REG_READ2(long, "access", const char *, pathname, int, mode); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 3113 | PRE_MEM_RASCIIZ( "access(pathname)", ARG1 ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3114 | } |
| 3115 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3116 | PRE(sys_alarm) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3117 | { |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3118 | PRINT("sys_alarm ( %lu )", ARG1); |
nethercote | 9a3beb9 | 2004-11-12 17:07:26 +0000 | [diff] [blame] | 3119 | PRE_REG_READ1(unsigned long, "alarm", unsigned int, seconds); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3120 | } |
| 3121 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3122 | PRE(sys_brk) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3123 | { |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 3124 | Addr brk_limit = VG_(brk_limit); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3125 | Addr brk_new; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 3126 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3127 | /* libc says: int brk(void *end_data_segment); |
| 3128 | kernel says: void* brk(void* end_data_segment); (more or less) |
| 3129 | |
| 3130 | libc returns 0 on success, and -1 (and sets errno) on failure. |
| 3131 | Nb: if you ask to shrink the dataseg end below what it |
| 3132 | currently is, that always succeeds, even if the dataseg end |
| 3133 | doesn't actually change (eg. brk(0)). Unless it seg faults. |
| 3134 | |
| 3135 | Kernel returns the new dataseg end. If the brk() failed, this |
| 3136 | will be unchanged from the old one. That's why calling (kernel) |
| 3137 | brk(0) gives the current dataseg end (libc brk() just returns |
| 3138 | zero in that case). |
| 3139 | |
| 3140 | Both will seg fault if you shrink it back into a text segment. |
| 3141 | */ |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 3142 | PRINT("sys_brk ( %#lx )", ARG1); |
nethercote | 9c311eb | 2004-11-12 18:20:12 +0000 | [diff] [blame] | 3143 | PRE_REG_READ1(unsigned long, "brk", unsigned long, end_data_segment); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3144 | |
florian | 981abb5 | 2015-04-29 12:59:16 +0000 | [diff] [blame] | 3145 | brk_new = do_brk(ARG1, tid); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3146 | SET_STATUS_Success( brk_new ); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 3147 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3148 | if (brk_new == ARG1) { |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3149 | /* brk() succeeded */ |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3150 | if (brk_new < brk_limit) { |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3151 | /* successfully shrunk the data segment. */ |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 3152 | VG_TRACK( die_mem_brk, (Addr)ARG1, |
| 3153 | brk_limit-ARG1 ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3154 | } else |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3155 | if (brk_new > brk_limit) { |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3156 | /* successfully grew the data segment */ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 3157 | VG_TRACK( new_mem_brk, brk_limit, |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3158 | ARG1-brk_limit, tid ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3159 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3160 | } else { |
| 3161 | /* brk() failed */ |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3162 | vg_assert(brk_limit == brk_new); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3163 | } |
| 3164 | } |
| 3165 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3166 | PRE(sys_chdir) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3167 | { |
sewardj | cc3de2d | 2011-08-18 15:08:20 +0000 | [diff] [blame] | 3168 | FUSE_COMPATIBLE_MAY_BLOCK(); |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 3169 | PRINT("sys_chdir ( %#lx(%s) )", ARG1,(char*)ARG1); |
nethercote | c6851dd | 2004-11-11 18:00:47 +0000 | [diff] [blame] | 3170 | PRE_REG_READ1(long, "chdir", const char *, path); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 3171 | PRE_MEM_RASCIIZ( "chdir(path)", ARG1 ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3172 | } |
| 3173 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3174 | PRE(sys_chmod) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3175 | { |
sewardj | cc3de2d | 2011-08-18 15:08:20 +0000 | [diff] [blame] | 3176 | FUSE_COMPATIBLE_MAY_BLOCK(); |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3177 | PRINT("sys_chmod ( %#lx(%s), %lu )", ARG1, (HChar*)ARG1, ARG2); |
nethercote | c6851dd | 2004-11-11 18:00:47 +0000 | [diff] [blame] | 3178 | PRE_REG_READ2(long, "chmod", const char *, path, vki_mode_t, mode); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 3179 | PRE_MEM_RASCIIZ( "chmod(path)", ARG1 ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 3182 | PRE(sys_chown) |
| 3183 | { |
sewardj | cc3de2d | 2011-08-18 15:08:20 +0000 | [diff] [blame] | 3184 | FUSE_COMPATIBLE_MAY_BLOCK(); |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 3185 | PRINT("sys_chown ( %#lx(%s), 0x%lx, 0x%lx )", ARG1,(char*)ARG1,ARG2,ARG3); |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 3186 | PRE_REG_READ3(long, "chown", |
| 3187 | const char *, path, vki_uid_t, owner, vki_gid_t, group); |
| 3188 | PRE_MEM_RASCIIZ( "chown(path)", ARG1 ); |
| 3189 | } |
| 3190 | |
| 3191 | PRE(sys_lchown) |
| 3192 | { |
sewardj | cc3de2d | 2011-08-18 15:08:20 +0000 | [diff] [blame] | 3193 | FUSE_COMPATIBLE_MAY_BLOCK(); |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 3194 | PRINT("sys_lchown ( %#lx(%s), 0x%lx, 0x%lx )", ARG1,(char*)ARG1,ARG2,ARG3); |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 3195 | PRE_REG_READ3(long, "lchown", |
| 3196 | const char *, path, vki_uid_t, owner, vki_gid_t, group); |
| 3197 | PRE_MEM_RASCIIZ( "lchown(path)", ARG1 ); |
| 3198 | } |
nethercote | 2e1c37d | 2004-11-13 13:57:12 +0000 | [diff] [blame] | 3199 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3200 | PRE(sys_close) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3201 | { |
sewardj | cc3de2d | 2011-08-18 15:08:20 +0000 | [diff] [blame] | 3202 | FUSE_COMPATIBLE_MAY_BLOCK(); |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3203 | PRINT("sys_close ( %lu )", ARG1); |
nethercote | c6851dd | 2004-11-11 18:00:47 +0000 | [diff] [blame] | 3204 | PRE_REG_READ1(long, "close", unsigned int, fd); |
| 3205 | |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 3206 | /* Detect and negate attempts by the client to close Valgrind's log fd */ |
sewardj | 9da79d1 | 2007-01-13 22:27:51 +0000 | [diff] [blame] | 3207 | if ( (!ML_(fd_allowed)(ARG1, "close", tid, False)) |
| 3208 | /* If doing -d style logging (which is to fd=2), don't |
| 3209 | allow that to be closed either. */ |
| 3210 | || (ARG1 == 2/*stderr*/ && VG_(debugLog_getLevel)() > 0) ) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3211 | SET_STATUS_Failure( VKI_EBADF ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3212 | } |
| 3213 | |
nethercote | 85a456f | 2004-11-16 17:31:56 +0000 | [diff] [blame] | 3214 | POST(sys_close) |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 3215 | { |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 3216 | if (VG_(clo_track_fds)) ML_(record_fd_close)(ARG1); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 3217 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3218 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3219 | PRE(sys_dup) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3220 | { |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3221 | PRINT("sys_dup ( %lu )", ARG1); |
nethercote | 9a3beb9 | 2004-11-12 17:07:26 +0000 | [diff] [blame] | 3222 | PRE_REG_READ1(long, "dup", unsigned int, oldfd); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3223 | } |
| 3224 | |
nethercote | 85a456f | 2004-11-16 17:31:56 +0000 | [diff] [blame] | 3225 | POST(sys_dup) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3226 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3227 | vg_assert(SUCCESS); |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 3228 | if (!ML_(fd_allowed)(RES, "dup", tid, True)) { |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 3229 | VG_(close)(RES); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3230 | SET_STATUS_Failure( VKI_EMFILE ); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 3231 | } else { |
nethercote | 9a3beb9 | 2004-11-12 17:07:26 +0000 | [diff] [blame] | 3232 | if (VG_(clo_track_fds)) |
njn | 096ccdd | 2009-02-22 23:00:30 +0000 | [diff] [blame] | 3233 | ML_(record_fd_open_named)(tid, RES); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3234 | } |
| 3235 | } |
| 3236 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3237 | PRE(sys_dup2) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3238 | { |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3239 | PRINT("sys_dup2 ( %lu, %lu )", ARG1, ARG2); |
nethercote | 71f05f3 | 2004-11-12 18:49:27 +0000 | [diff] [blame] | 3240 | PRE_REG_READ2(long, "dup2", unsigned int, oldfd, unsigned int, newfd); |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 3241 | if (!ML_(fd_allowed)(ARG2, "dup2", tid, True)) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3242 | SET_STATUS_Failure( VKI_EBADF ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3243 | } |
| 3244 | |
nethercote | 85a456f | 2004-11-16 17:31:56 +0000 | [diff] [blame] | 3245 | POST(sys_dup2) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3246 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3247 | vg_assert(SUCCESS); |
nethercote | 71f05f3 | 2004-11-12 18:49:27 +0000 | [diff] [blame] | 3248 | if (VG_(clo_track_fds)) |
njn | 096ccdd | 2009-02-22 23:00:30 +0000 | [diff] [blame] | 3249 | ML_(record_fd_open_named)(tid, RES); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 3252 | PRE(sys_fchdir) |
| 3253 | { |
sewardj | cc3de2d | 2011-08-18 15:08:20 +0000 | [diff] [blame] | 3254 | FUSE_COMPATIBLE_MAY_BLOCK(); |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3255 | PRINT("sys_fchdir ( %lu )", ARG1); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 3256 | PRE_REG_READ1(long, "fchdir", unsigned int, fd); |
| 3257 | } |
| 3258 | |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 3259 | PRE(sys_fchown) |
| 3260 | { |
sewardj | cc3de2d | 2011-08-18 15:08:20 +0000 | [diff] [blame] | 3261 | FUSE_COMPATIBLE_MAY_BLOCK(); |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3262 | PRINT("sys_fchown ( %lu, %lu, %lu )", ARG1, ARG2, ARG3); |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 3263 | PRE_REG_READ3(long, "fchown", |
| 3264 | unsigned int, fd, vki_uid_t, owner, vki_gid_t, group); |
| 3265 | } |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 3266 | |
| 3267 | PRE(sys_fchmod) |
| 3268 | { |
sewardj | cc3de2d | 2011-08-18 15:08:20 +0000 | [diff] [blame] | 3269 | FUSE_COMPATIBLE_MAY_BLOCK(); |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3270 | PRINT("sys_fchmod ( %lu, %lu )", ARG1, ARG2); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 3271 | PRE_REG_READ2(long, "fchmod", unsigned int, fildes, vki_mode_t, mode); |
| 3272 | } |
| 3273 | |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 3274 | PRE(sys_newfstat) |
| 3275 | { |
sewardj | cc3de2d | 2011-08-18 15:08:20 +0000 | [diff] [blame] | 3276 | FUSE_COMPATIBLE_MAY_BLOCK(); |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3277 | PRINT("sys_newfstat ( %lu, %#lx )", ARG1, ARG2); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 3278 | PRE_REG_READ2(long, "fstat", unsigned int, fd, struct stat *, buf); |
| 3279 | PRE_MEM_WRITE( "fstat(buf)", ARG2, sizeof(struct vki_stat) ); |
| 3280 | } |
| 3281 | |
| 3282 | POST(sys_newfstat) |
| 3283 | { |
| 3284 | POST_MEM_WRITE( ARG2, sizeof(struct vki_stat) ); |
| 3285 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3286 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame] | 3287 | #if !defined(VGO_solaris) && !defined(VGP_arm64_linux) |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 3288 | static vki_sigset_t fork_saved_mask; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3289 | |
nethercote | 75a8c98 | 2004-11-11 19:03:34 +0000 | [diff] [blame] | 3290 | // In Linux, the sys_fork() function varies across architectures, but we |
| 3291 | // ignore the various args it gets, and so it looks arch-neutral. Hmm. |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3292 | PRE(sys_fork) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3293 | { |
njn | 58bbd94 | 2009-05-21 23:59:34 +0000 | [diff] [blame] | 3294 | Bool is_child; |
| 3295 | Int child_pid; |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 3296 | vki_sigset_t mask; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3297 | |
nethercote | 75a8c98 | 2004-11-11 19:03:34 +0000 | [diff] [blame] | 3298 | PRINT("sys_fork ( )"); |
| 3299 | PRE_REG_READ0(long, "fork"); |
| 3300 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3301 | /* Block all signals during fork, so that we can fix things up in |
| 3302 | the child without being interrupted. */ |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 3303 | VG_(sigfillset)(&mask); |
| 3304 | VG_(sigprocmask)(VKI_SIG_SETMASK, &mask, &fork_saved_mask); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3305 | |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 3306 | VG_(do_atfork_pre)(tid); |
| 3307 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3308 | SET_STATUS_from_SysRes( VG_(do_syscall0)(__NR_fork) ); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 3309 | |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 3310 | if (!SUCCESS) return; |
| 3311 | |
sewardj | 6e9de46 | 2011-06-28 07:25:29 +0000 | [diff] [blame] | 3312 | #if defined(VGO_linux) |
njn | 58bbd94 | 2009-05-21 23:59:34 +0000 | [diff] [blame] | 3313 | // RES is 0 for child, non-0 (the child's PID) for parent. |
| 3314 | is_child = ( RES == 0 ? True : False ); |
| 3315 | child_pid = ( is_child ? -1 : RES ); |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 3316 | #elif defined(VGO_darwin) |
| 3317 | // RES is the child's pid. RESHI is 1 for child, 0 for parent. |
| 3318 | is_child = RESHI; |
| 3319 | child_pid = RES; |
| 3320 | #else |
| 3321 | # error Unknown OS |
| 3322 | #endif |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 3323 | |
njn | 58bbd94 | 2009-05-21 23:59:34 +0000 | [diff] [blame] | 3324 | if (is_child) { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 3325 | VG_(do_atfork_child)(tid); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3326 | |
| 3327 | /* restore signal mask */ |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 3328 | VG_(sigprocmask)(VKI_SIG_SETMASK, &fork_saved_mask, NULL); |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 3329 | } else { |
njn | e9ba34a | 2008-10-13 04:19:15 +0000 | [diff] [blame] | 3330 | VG_(do_atfork_parent)(tid); |
| 3331 | |
njn | 58bbd94 | 2009-05-21 23:59:34 +0000 | [diff] [blame] | 3332 | PRINT(" fork: process %d created child %d\n", VG_(getpid)(), child_pid); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3333 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3334 | /* restore signal mask */ |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 3335 | VG_(sigprocmask)(VKI_SIG_SETMASK, &fork_saved_mask, NULL); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3336 | } |
| 3337 | } |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame] | 3338 | #endif // !defined(VGO_solaris) && !defined(VGP_arm64_linux) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3339 | |
sewardj | 8c25732 | 2005-06-08 01:01:48 +0000 | [diff] [blame] | 3340 | PRE(sys_ftruncate) |
| 3341 | { |
| 3342 | *flags |= SfMayBlock; |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3343 | PRINT("sys_ftruncate ( %lu, %lu )", ARG1, ARG2); |
sewardj | 8c25732 | 2005-06-08 01:01:48 +0000 | [diff] [blame] | 3344 | PRE_REG_READ2(long, "ftruncate", unsigned int, fd, unsigned long, length); |
| 3345 | } |
| 3346 | |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 3347 | PRE(sys_truncate) |
| 3348 | { |
| 3349 | *flags |= SfMayBlock; |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3350 | PRINT("sys_truncate ( %#lx(%s), %lu )", ARG1, (HChar*)ARG1, ARG2); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 3351 | PRE_REG_READ2(long, "truncate", |
| 3352 | const char *, path, unsigned long, length); |
| 3353 | PRE_MEM_RASCIIZ( "truncate(path)", ARG1 ); |
| 3354 | } |
| 3355 | |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 3356 | PRE(sys_ftruncate64) |
| 3357 | { |
| 3358 | *flags |= SfMayBlock; |
njn | 89d43f7 | 2009-05-24 23:02:55 +0000 | [diff] [blame] | 3359 | #if VG_WORDSIZE == 4 |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3360 | PRINT("sys_ftruncate64 ( %lu, %llu )", ARG1, MERGE64(ARG2,ARG3)); |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 3361 | PRE_REG_READ3(long, "ftruncate64", |
| 3362 | unsigned int, fd, |
tom | ca78724 | 2009-11-25 11:24:00 +0000 | [diff] [blame] | 3363 | UWord, MERGE64_FIRST(length), UWord, MERGE64_SECOND(length)); |
njn | 89d43f7 | 2009-05-24 23:02:55 +0000 | [diff] [blame] | 3364 | #else |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3365 | PRINT("sys_ftruncate64 ( %lu, %lu )", ARG1, ARG2); |
njn | 89d43f7 | 2009-05-24 23:02:55 +0000 | [diff] [blame] | 3366 | PRE_REG_READ2(long, "ftruncate64", |
| 3367 | unsigned int,fd, UWord,length); |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 3368 | #endif |
njn | 89d43f7 | 2009-05-24 23:02:55 +0000 | [diff] [blame] | 3369 | } |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 3370 | |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 3371 | PRE(sys_truncate64) |
| 3372 | { |
| 3373 | *flags |= SfMayBlock; |
njn | 89d43f7 | 2009-05-24 23:02:55 +0000 | [diff] [blame] | 3374 | #if VG_WORDSIZE == 4 |
tom | ca78724 | 2009-11-25 11:24:00 +0000 | [diff] [blame] | 3375 | PRINT("sys_truncate64 ( %#lx, %lld )", ARG1, (Long)MERGE64(ARG2, ARG3)); |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 3376 | PRE_REG_READ3(long, "truncate64", |
| 3377 | const char *, path, |
tom | ca78724 | 2009-11-25 11:24:00 +0000 | [diff] [blame] | 3378 | UWord, MERGE64_FIRST(length), UWord, MERGE64_SECOND(length)); |
njn | 89d43f7 | 2009-05-24 23:02:55 +0000 | [diff] [blame] | 3379 | #else |
| 3380 | PRINT("sys_truncate64 ( %#lx, %lld )", ARG1, (Long)ARG2); |
| 3381 | PRE_REG_READ2(long, "truncate64", |
| 3382 | const char *,path, UWord,length); |
| 3383 | #endif |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 3384 | PRE_MEM_RASCIIZ( "truncate64(path)", ARG1 ); |
| 3385 | } |
sewardj | 78b50e4 | 2005-06-08 01:47:28 +0000 | [diff] [blame] | 3386 | |
| 3387 | PRE(sys_getdents) |
| 3388 | { |
| 3389 | *flags |= SfMayBlock; |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3390 | PRINT("sys_getdents ( %lu, %#lx, %lu )", ARG1, ARG2, ARG3); |
sewardj | 78b50e4 | 2005-06-08 01:47:28 +0000 | [diff] [blame] | 3391 | PRE_REG_READ3(long, "getdents", |
florian | 99e644a | 2014-10-04 21:32:06 +0000 | [diff] [blame] | 3392 | unsigned int, fd, struct vki_dirent *, dirp, |
sewardj | 78b50e4 | 2005-06-08 01:47:28 +0000 | [diff] [blame] | 3393 | unsigned int, count); |
| 3394 | PRE_MEM_WRITE( "getdents(dirp)", ARG2, ARG3 ); |
| 3395 | } |
| 3396 | |
| 3397 | POST(sys_getdents) |
| 3398 | { |
| 3399 | vg_assert(SUCCESS); |
| 3400 | if (RES > 0) |
| 3401 | POST_MEM_WRITE( ARG2, RES ); |
| 3402 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3403 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3404 | PRE(sys_getdents64) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3405 | { |
sewardj | 78b50e4 | 2005-06-08 01:47:28 +0000 | [diff] [blame] | 3406 | *flags |= SfMayBlock; |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3407 | PRINT("sys_getdents64 ( %lu, %#lx, %lu )",ARG1, ARG2, ARG3); |
nethercote | 06c7bd7 | 2004-11-14 19:11:56 +0000 | [diff] [blame] | 3408 | PRE_REG_READ3(long, "getdents64", |
florian | 99e644a | 2014-10-04 21:32:06 +0000 | [diff] [blame] | 3409 | unsigned int, fd, struct vki_dirent64 *, dirp, |
nethercote | 06c7bd7 | 2004-11-14 19:11:56 +0000 | [diff] [blame] | 3410 | unsigned int, count); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 3411 | PRE_MEM_WRITE( "getdents64(dirp)", ARG2, ARG3 ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3412 | } |
| 3413 | |
nethercote | 85a456f | 2004-11-16 17:31:56 +0000 | [diff] [blame] | 3414 | POST(sys_getdents64) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3415 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3416 | vg_assert(SUCCESS); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 3417 | if (RES > 0) |
| 3418 | POST_MEM_WRITE( ARG2, RES ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3419 | } |
| 3420 | |
sewardj | 78b50e4 | 2005-06-08 01:47:28 +0000 | [diff] [blame] | 3421 | PRE(sys_getgroups) |
| 3422 | { |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3423 | PRINT("sys_getgroups ( %ld, %#lx )", SARG1, ARG2); |
sewardj | 78b50e4 | 2005-06-08 01:47:28 +0000 | [diff] [blame] | 3424 | PRE_REG_READ2(long, "getgroups", int, size, vki_gid_t *, list); |
| 3425 | if (ARG1 > 0) |
| 3426 | PRE_MEM_WRITE( "getgroups(list)", ARG2, ARG1 * sizeof(vki_gid_t) ); |
| 3427 | } |
| 3428 | |
| 3429 | POST(sys_getgroups) |
| 3430 | { |
| 3431 | vg_assert(SUCCESS); |
| 3432 | if (ARG1 > 0 && RES > 0) |
| 3433 | POST_MEM_WRITE( ARG2, RES * sizeof(vki_gid_t) ); |
| 3434 | } |
nethercote | 686b5db | 2004-11-14 13:42:51 +0000 | [diff] [blame] | 3435 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3436 | PRE(sys_getcwd) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3437 | { |
njn | 4b70e8c | 2005-08-14 17:36:07 +0000 | [diff] [blame] | 3438 | // Comment from linux/fs/dcache.c: |
| 3439 | // NOTE! The user-level library version returns a character pointer. |
| 3440 | // The kernel system call just returns the length of the buffer filled |
| 3441 | // (which includes the ending '\0' character), or a negative error |
| 3442 | // value. |
| 3443 | // Is this Linux-specific? If so it should be moved to syswrap-linux.c. |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 3444 | PRINT("sys_getcwd ( %#lx, %llu )", ARG1,(ULong)ARG2); |
nethercote | ac866b9 | 2004-11-15 20:23:15 +0000 | [diff] [blame] | 3445 | PRE_REG_READ2(long, "getcwd", char *, buf, unsigned long, size); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 3446 | PRE_MEM_WRITE( "getcwd(buf)", ARG1, ARG2 ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3447 | } |
| 3448 | |
nethercote | 85a456f | 2004-11-16 17:31:56 +0000 | [diff] [blame] | 3449 | POST(sys_getcwd) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3450 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3451 | vg_assert(SUCCESS); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 3452 | if (RES != (Addr)NULL) |
| 3453 | POST_MEM_WRITE( ARG1, RES ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3454 | } |
| 3455 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3456 | PRE(sys_geteuid) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3457 | { |
nethercote | 0df495a | 2004-11-11 16:38:21 +0000 | [diff] [blame] | 3458 | PRINT("sys_geteuid ( )"); |
| 3459 | PRE_REG_READ0(long, "geteuid"); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3460 | } |
| 3461 | |
sewardj | 78b50e4 | 2005-06-08 01:47:28 +0000 | [diff] [blame] | 3462 | PRE(sys_getegid) |
| 3463 | { |
| 3464 | PRINT("sys_getegid ( )"); |
| 3465 | PRE_REG_READ0(long, "getegid"); |
| 3466 | } |
| 3467 | |
sewardj | 78b50e4 | 2005-06-08 01:47:28 +0000 | [diff] [blame] | 3468 | PRE(sys_getgid) |
| 3469 | { |
| 3470 | PRINT("sys_getgid ( )"); |
| 3471 | PRE_REG_READ0(long, "getgid"); |
| 3472 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3473 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3474 | PRE(sys_getpid) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3475 | { |
nethercote | 4e632c2 | 2004-11-09 16:45:33 +0000 | [diff] [blame] | 3476 | PRINT("sys_getpid ()"); |
| 3477 | PRE_REG_READ0(long, "getpid"); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3478 | } |
| 3479 | |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 3480 | PRE(sys_getpgid) |
| 3481 | { |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3482 | PRINT("sys_getpgid ( %ld )", SARG1); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 3483 | PRE_REG_READ1(long, "getpgid", vki_pid_t, pid); |
| 3484 | } |
sewardj | 78b50e4 | 2005-06-08 01:47:28 +0000 | [diff] [blame] | 3485 | |
| 3486 | PRE(sys_getpgrp) |
| 3487 | { |
| 3488 | PRINT("sys_getpgrp ()"); |
| 3489 | PRE_REG_READ0(long, "getpgrp"); |
| 3490 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3491 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3492 | PRE(sys_getppid) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3493 | { |
nethercote | 4e632c2 | 2004-11-09 16:45:33 +0000 | [diff] [blame] | 3494 | PRINT("sys_getppid ()"); |
| 3495 | PRE_REG_READ0(long, "getppid"); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3496 | } |
| 3497 | |
njn | cf45fd4 | 2004-11-24 16:30:22 +0000 | [diff] [blame] | 3498 | static void common_post_getrlimit(ThreadId tid, UWord a1, UWord a2) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3499 | { |
nethercote | 620154f | 2004-11-12 21:21:07 +0000 | [diff] [blame] | 3500 | POST_MEM_WRITE( a2, sizeof(struct vki_rlimit) ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3501 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 3502 | #ifdef _RLIMIT_POSIX_FLAG |
| 3503 | // Darwin will sometimes set _RLIMIT_POSIX_FLAG on getrlimit calls. |
| 3504 | // Unset it here to make the switch case below work correctly. |
| 3505 | a1 &= ~_RLIMIT_POSIX_FLAG; |
| 3506 | #endif |
| 3507 | |
nethercote | 620154f | 2004-11-12 21:21:07 +0000 | [diff] [blame] | 3508 | switch (a1) { |
| 3509 | case VKI_RLIMIT_NOFILE: |
| 3510 | ((struct vki_rlimit *)a2)->rlim_cur = VG_(fd_soft_limit); |
| 3511 | ((struct vki_rlimit *)a2)->rlim_max = VG_(fd_hard_limit); |
| 3512 | break; |
nethercote | 535f03b | 2004-02-15 15:32:51 +0000 | [diff] [blame] | 3513 | |
nethercote | 620154f | 2004-11-12 21:21:07 +0000 | [diff] [blame] | 3514 | case VKI_RLIMIT_DATA: |
| 3515 | *((struct vki_rlimit *)a2) = VG_(client_rlimit_data); |
| 3516 | break; |
fitzhardinge | b50068f | 2004-02-24 23:42:55 +0000 | [diff] [blame] | 3517 | |
nethercote | 620154f | 2004-11-12 21:21:07 +0000 | [diff] [blame] | 3518 | case VKI_RLIMIT_STACK: |
| 3519 | *((struct vki_rlimit *)a2) = VG_(client_rlimit_stack); |
| 3520 | break; |
njn | 89d43f7 | 2009-05-24 23:02:55 +0000 | [diff] [blame] | 3521 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3522 | } |
| 3523 | |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 3524 | PRE(sys_old_getrlimit) |
| 3525 | { |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3526 | PRINT("sys_old_getrlimit ( %lu, %#lx )", ARG1, ARG2); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 3527 | PRE_REG_READ2(long, "old_getrlimit", |
| 3528 | unsigned int, resource, struct rlimit *, rlim); |
| 3529 | PRE_MEM_WRITE( "old_getrlimit(rlim)", ARG2, sizeof(struct vki_rlimit) ); |
| 3530 | } |
| 3531 | |
| 3532 | POST(sys_old_getrlimit) |
| 3533 | { |
| 3534 | common_post_getrlimit(tid, ARG1, ARG2); |
| 3535 | } |
nethercote | 620154f | 2004-11-12 21:21:07 +0000 | [diff] [blame] | 3536 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3537 | PRE(sys_getrlimit) |
nethercote | 620154f | 2004-11-12 21:21:07 +0000 | [diff] [blame] | 3538 | { |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3539 | PRINT("sys_getrlimit ( %lu, %#lx )", ARG1, ARG2); |
nethercote | 620154f | 2004-11-12 21:21:07 +0000 | [diff] [blame] | 3540 | PRE_REG_READ2(long, "getrlimit", |
| 3541 | unsigned int, resource, struct rlimit *, rlim); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 3542 | PRE_MEM_WRITE( "getrlimit(rlim)", ARG2, sizeof(struct vki_rlimit) ); |
nethercote | 620154f | 2004-11-12 21:21:07 +0000 | [diff] [blame] | 3543 | } |
| 3544 | |
nethercote | 85a456f | 2004-11-16 17:31:56 +0000 | [diff] [blame] | 3545 | POST(sys_getrlimit) |
nethercote | 620154f | 2004-11-12 21:21:07 +0000 | [diff] [blame] | 3546 | { |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 3547 | common_post_getrlimit(tid, ARG1, ARG2); |
nethercote | 620154f | 2004-11-12 21:21:07 +0000 | [diff] [blame] | 3548 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3549 | |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 3550 | PRE(sys_getrusage) |
| 3551 | { |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3552 | PRINT("sys_getrusage ( %ld, %#lx )", SARG1, ARG2); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 3553 | PRE_REG_READ2(long, "getrusage", int, who, struct rusage *, usage); |
| 3554 | PRE_MEM_WRITE( "getrusage(usage)", ARG2, sizeof(struct vki_rusage) ); |
| 3555 | } |
| 3556 | |
| 3557 | POST(sys_getrusage) |
| 3558 | { |
| 3559 | vg_assert(SUCCESS); |
| 3560 | if (RES == 0) |
| 3561 | POST_MEM_WRITE( ARG2, sizeof(struct vki_rusage) ); |
| 3562 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3563 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3564 | PRE(sys_gettimeofday) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3565 | { |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 3566 | PRINT("sys_gettimeofday ( %#lx, %#lx )", ARG1,ARG2); |
nethercote | 686b5db | 2004-11-14 13:42:51 +0000 | [diff] [blame] | 3567 | PRE_REG_READ2(long, "gettimeofday", |
| 3568 | struct timeval *, tv, struct timezone *, tz); |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 3569 | // GrP fixme does darwin write to *tz anymore? |
njn | 72bbd8d | 2009-05-22 08:12:46 +0000 | [diff] [blame] | 3570 | if (ARG1 != 0) |
| 3571 | PRE_timeval_WRITE( "gettimeofday(tv)", ARG1 ); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 3572 | if (ARG2 != 0) |
| 3573 | PRE_MEM_WRITE( "gettimeofday(tz)", ARG2, sizeof(struct vki_timezone) ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3574 | } |
| 3575 | |
nethercote | 85a456f | 2004-11-16 17:31:56 +0000 | [diff] [blame] | 3576 | POST(sys_gettimeofday) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3577 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3578 | vg_assert(SUCCESS); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 3579 | if (RES == 0) { |
njn | 72bbd8d | 2009-05-22 08:12:46 +0000 | [diff] [blame] | 3580 | if (ARG1 != 0) |
| 3581 | POST_timeval_WRITE( ARG1 ); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 3582 | if (ARG2 != 0) |
| 3583 | POST_MEM_WRITE( ARG2, sizeof(struct vki_timezone) ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3584 | } |
| 3585 | } |
| 3586 | |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 3587 | PRE(sys_settimeofday) |
| 3588 | { |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 3589 | PRINT("sys_settimeofday ( %#lx, %#lx )", ARG1,ARG2); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 3590 | PRE_REG_READ2(long, "settimeofday", |
| 3591 | struct timeval *, tv, struct timezone *, tz); |
njn | 72bbd8d | 2009-05-22 08:12:46 +0000 | [diff] [blame] | 3592 | if (ARG1 != 0) |
njn | da8549d | 2009-05-25 01:48:59 +0000 | [diff] [blame] | 3593 | PRE_timeval_READ( "settimeofday(tv)", ARG1 ); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 3594 | if (ARG2 != 0) { |
| 3595 | PRE_MEM_READ( "settimeofday(tz)", ARG2, sizeof(struct vki_timezone) ); |
| 3596 | /* maybe should warn if tz->tz_dsttime is non-zero? */ |
| 3597 | } |
| 3598 | } |
nethercote | 686b5db | 2004-11-14 13:42:51 +0000 | [diff] [blame] | 3599 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3600 | PRE(sys_getuid) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3601 | { |
nethercote | 0df495a | 2004-11-11 16:38:21 +0000 | [diff] [blame] | 3602 | PRINT("sys_getuid ( )"); |
| 3603 | PRE_REG_READ0(long, "getuid"); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3604 | } |
| 3605 | |
njn | 1be9cf6 | 2009-05-22 00:15:06 +0000 | [diff] [blame] | 3606 | void ML_(PRE_unknown_ioctl)(ThreadId tid, UWord request, UWord arg) |
| 3607 | { |
| 3608 | /* We don't have any specific information on it, so |
| 3609 | try to do something reasonable based on direction and |
| 3610 | size bits. The encoding scheme is described in |
| 3611 | /usr/include/asm/ioctl.h or /usr/include/sys/ioccom.h . |
| 3612 | |
| 3613 | According to Simon Hausmann, _IOC_READ means the kernel |
| 3614 | writes a value to the ioctl value passed from the user |
| 3615 | space and the other way around with _IOC_WRITE. */ |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 3616 | |
| 3617 | #if defined(VGO_solaris) |
| 3618 | /* Majority of Solaris ioctl requests does not honour direction hints. */ |
| 3619 | UInt dir = _VKI_IOC_NONE; |
| 3620 | #else |
njn | 1be9cf6 | 2009-05-22 00:15:06 +0000 | [diff] [blame] | 3621 | UInt dir = _VKI_IOC_DIR(request); |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 3622 | #endif |
njn | 1be9cf6 | 2009-05-22 00:15:06 +0000 | [diff] [blame] | 3623 | UInt size = _VKI_IOC_SIZE(request); |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 3624 | |
philippe | ec905f7 | 2014-08-17 20:03:51 +0000 | [diff] [blame] | 3625 | if (SimHintiS(SimHint_lax_ioctls, VG_(clo_sim_hints))) { |
njn | 1be9cf6 | 2009-05-22 00:15:06 +0000 | [diff] [blame] | 3626 | /* |
| 3627 | * Be very lax about ioctl handling; the only |
| 3628 | * assumption is that the size is correct. Doesn't |
| 3629 | * require the full buffer to be initialized when |
| 3630 | * writing. Without this, using some device |
| 3631 | * drivers with a large number of strange ioctl |
| 3632 | * commands becomes very tiresome. |
| 3633 | */ |
| 3634 | } else if (/* size == 0 || */ dir == _VKI_IOC_NONE) { |
bart | d77a4ca | 2014-06-28 07:18:33 +0000 | [diff] [blame] | 3635 | static UWord unknown_ioctl[10]; |
| 3636 | static Int moans = sizeof(unknown_ioctl) / sizeof(unknown_ioctl[0]); |
| 3637 | |
njn | 1be9cf6 | 2009-05-22 00:15:06 +0000 | [diff] [blame] | 3638 | if (moans > 0 && !VG_(clo_xml)) { |
bart | d77a4ca | 2014-06-28 07:18:33 +0000 | [diff] [blame] | 3639 | /* Check if have not already moaned for this request. */ |
| 3640 | UInt i; |
| 3641 | for (i = 0; i < sizeof(unknown_ioctl)/sizeof(unknown_ioctl[0]); i++) { |
| 3642 | if (unknown_ioctl[i] == request) |
| 3643 | break; |
| 3644 | if (unknown_ioctl[i] == 0) { |
| 3645 | unknown_ioctl[i] = request; |
| 3646 | moans--; |
| 3647 | VG_(umsg)("Warning: noted but unhandled ioctl 0x%lx" |
| 3648 | " with no size/direction hints.\n", request); |
| 3649 | VG_(umsg)(" This could cause spurious value errors to appear.\n"); |
| 3650 | VG_(umsg)(" See README_MISSING_SYSCALL_OR_IOCTL for " |
| 3651 | "guidance on writing a proper wrapper.\n" ); |
| 3652 | //VG_(get_and_pp_StackTrace)(tid, VG_(clo_backtrace_size)); |
| 3653 | return; |
| 3654 | } |
| 3655 | } |
njn | 1be9cf6 | 2009-05-22 00:15:06 +0000 | [diff] [blame] | 3656 | } |
| 3657 | } else { |
| 3658 | //VG_(message)(Vg_UserMsg, "UNKNOWN ioctl %#lx\n", request); |
| 3659 | //VG_(get_and_pp_StackTrace)(tid, VG_(clo_backtrace_size)); |
| 3660 | if ((dir & _VKI_IOC_WRITE) && size > 0) |
| 3661 | PRE_MEM_READ( "ioctl(generic)", arg, size); |
| 3662 | if ((dir & _VKI_IOC_READ) && size > 0) |
| 3663 | PRE_MEM_WRITE( "ioctl(generic)", arg, size); |
| 3664 | } |
| 3665 | } |
| 3666 | |
| 3667 | void ML_(POST_unknown_ioctl)(ThreadId tid, UInt res, UWord request, UWord arg) |
| 3668 | { |
| 3669 | /* We don't have any specific information on it, so |
| 3670 | try to do something reasonable based on direction and |
| 3671 | size bits. The encoding scheme is described in |
| 3672 | /usr/include/asm/ioctl.h or /usr/include/sys/ioccom.h . |
| 3673 | |
| 3674 | According to Simon Hausmann, _IOC_READ means the kernel |
| 3675 | writes a value to the ioctl value passed from the user |
| 3676 | space and the other way around with _IOC_WRITE. */ |
| 3677 | |
| 3678 | UInt dir = _VKI_IOC_DIR(request); |
| 3679 | UInt size = _VKI_IOC_SIZE(request); |
| 3680 | if (size > 0 && (dir & _VKI_IOC_READ) |
| 3681 | && res == 0 |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 3682 | && arg != (Addr)NULL) { |
njn | 1be9cf6 | 2009-05-22 00:15:06 +0000 | [diff] [blame] | 3683 | POST_MEM_WRITE(arg, size); |
| 3684 | } |
| 3685 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3686 | |
njn | 03f1e58 | 2005-03-26 20:08:06 +0000 | [diff] [blame] | 3687 | /* |
| 3688 | If we're sending a SIGKILL to one of our own threads, then simulate |
| 3689 | it rather than really sending the signal, so that the target thread |
| 3690 | gets a chance to clean up. Returns True if we did the killing (or |
| 3691 | no killing is necessary), and False if the caller should use the |
| 3692 | normal kill syscall. |
| 3693 | |
| 3694 | "pid" is any pid argument which can be passed to kill; group kills |
| 3695 | (< -1, 0), and owner kills (-1) are ignored, on the grounds that |
| 3696 | they'll most likely hit all the threads and we won't need to worry |
| 3697 | about cleanup. In truth, we can't fully emulate these multicast |
| 3698 | kills. |
| 3699 | |
| 3700 | "tgid" is a thread group id. If it is not -1, then the target |
| 3701 | thread must be in that thread group. |
| 3702 | */ |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 3703 | Bool ML_(do_sigkill)(Int pid, Int tgid) |
njn | 03f1e58 | 2005-03-26 20:08:06 +0000 | [diff] [blame] | 3704 | { |
| 3705 | ThreadState *tst; |
| 3706 | ThreadId tid; |
| 3707 | |
| 3708 | if (pid <= 0) |
| 3709 | return False; |
| 3710 | |
sewardj | 4278172 | 2006-12-17 19:36:06 +0000 | [diff] [blame] | 3711 | tid = VG_(lwpid_to_vgtid)(pid); |
njn | 03f1e58 | 2005-03-26 20:08:06 +0000 | [diff] [blame] | 3712 | if (tid == VG_INVALID_THREADID) |
| 3713 | return False; /* none of our threads */ |
| 3714 | |
| 3715 | tst = VG_(get_ThreadState)(tid); |
| 3716 | if (tst == NULL || tst->status == VgTs_Empty) |
| 3717 | return False; /* hm, shouldn't happen */ |
| 3718 | |
| 3719 | if (tgid != -1 && tst->os_state.threadgroup != tgid) |
| 3720 | return False; /* not the right thread group */ |
| 3721 | |
| 3722 | /* Check to see that the target isn't already exiting. */ |
| 3723 | if (!VG_(is_exiting)(tid)) { |
| 3724 | if (VG_(clo_trace_signals)) |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 3725 | VG_(message)(Vg_DebugMsg, |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3726 | "Thread %u being killed with SIGKILL\n", |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 3727 | tst->tid); |
njn | 03f1e58 | 2005-03-26 20:08:06 +0000 | [diff] [blame] | 3728 | |
| 3729 | tst->exitreason = VgSrc_FatalSig; |
| 3730 | tst->os_state.fatalsig = VKI_SIGKILL; |
| 3731 | |
| 3732 | if (!VG_(is_running_thread)(tid)) |
sewardj | ef1cf8b | 2006-10-17 01:47:30 +0000 | [diff] [blame] | 3733 | VG_(get_thread_out_of_syscall)(tid); |
njn | 03f1e58 | 2005-03-26 20:08:06 +0000 | [diff] [blame] | 3734 | } |
| 3735 | |
| 3736 | return True; |
| 3737 | } |
| 3738 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3739 | PRE(sys_kill) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3740 | { |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3741 | PRINT("sys_kill ( %ld, %ld )", SARG1, SARG2); |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 3742 | PRE_REG_READ2(long, "kill", int, pid, int, signal); |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 3743 | if (!ML_(client_signal_OK)(ARG2)) { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3744 | SET_STATUS_Failure( VKI_EINVAL ); |
njn | 03f1e58 | 2005-03-26 20:08:06 +0000 | [diff] [blame] | 3745 | return; |
| 3746 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3747 | |
njn | 03f1e58 | 2005-03-26 20:08:06 +0000 | [diff] [blame] | 3748 | /* If we're sending SIGKILL, check to see if the target is one of |
| 3749 | our threads and handle it specially. */ |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 3750 | if (ARG2 == VKI_SIGKILL && ML_(do_sigkill)(ARG1, -1)) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3751 | SET_STATUS_Success(0); |
njn | 03f1e58 | 2005-03-26 20:08:06 +0000 | [diff] [blame] | 3752 | else |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 3753 | /* re syscall3: Darwin has a 3rd arg, which is a flag (boolean) |
| 3754 | affecting how posix-compliant the call is. I guess it is |
| 3755 | harmless to pass the 3rd arg on other platforms; hence pass |
| 3756 | it on all. */ |
| 3757 | SET_STATUS_from_SysRes( VG_(do_syscall3)(SYSNO, ARG1, ARG2, ARG3) ); |
njn | 03f1e58 | 2005-03-26 20:08:06 +0000 | [diff] [blame] | 3758 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 3759 | if (VG_(clo_trace_signals)) |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 3760 | VG_(message)(Vg_DebugMsg, "kill: sent signal %ld to pid %ld\n", |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3761 | SARG2, SARG1); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3762 | |
| 3763 | /* This kill might have given us a pending signal. Ask for a check once |
| 3764 | the syscall is done. */ |
| 3765 | *flags |= SfPollAfter; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3766 | } |
| 3767 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3768 | PRE(sys_link) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3769 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3770 | *flags |= SfMayBlock; |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 3771 | PRINT("sys_link ( %#lx(%s), %#lx(%s) )", ARG1,(char*)ARG1,ARG2,(char*)ARG2); |
nethercote | c6851dd | 2004-11-11 18:00:47 +0000 | [diff] [blame] | 3772 | PRE_REG_READ2(long, "link", const char *, oldpath, const char *, newpath); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 3773 | PRE_MEM_RASCIIZ( "link(oldpath)", ARG1); |
| 3774 | PRE_MEM_RASCIIZ( "link(newpath)", ARG2); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3775 | } |
| 3776 | |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 3777 | PRE(sys_newlstat) |
| 3778 | { |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 3779 | PRINT("sys_newlstat ( %#lx(%s), %#lx )", ARG1,(char*)ARG1,ARG2); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 3780 | PRE_REG_READ2(long, "lstat", char *, file_name, struct stat *, buf); |
| 3781 | PRE_MEM_RASCIIZ( "lstat(file_name)", ARG1 ); |
| 3782 | PRE_MEM_WRITE( "lstat(buf)", ARG2, sizeof(struct vki_stat) ); |
| 3783 | } |
| 3784 | |
| 3785 | POST(sys_newlstat) |
| 3786 | { |
| 3787 | vg_assert(SUCCESS); |
njn | 9331c7b | 2009-06-15 07:43:50 +0000 | [diff] [blame] | 3788 | POST_MEM_WRITE( ARG2, sizeof(struct vki_stat) ); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 3789 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3790 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3791 | PRE(sys_mkdir) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3792 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3793 | *flags |= SfMayBlock; |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3794 | PRINT("sys_mkdir ( %#lx(%s), %ld )", ARG1, (HChar*)ARG1, SARG2); |
nethercote | 9a3beb9 | 2004-11-12 17:07:26 +0000 | [diff] [blame] | 3795 | PRE_REG_READ2(long, "mkdir", const char *, pathname, int, mode); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 3796 | PRE_MEM_RASCIIZ( "mkdir(pathname)", ARG1 ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3797 | } |
| 3798 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3799 | PRE(sys_mprotect) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3800 | { |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3801 | PRINT("sys_mprotect ( %#lx, %lu, %lu )", ARG1, ARG2, ARG3); |
nethercote | 06c7bd7 | 2004-11-14 19:11:56 +0000 | [diff] [blame] | 3802 | PRE_REG_READ3(long, "mprotect", |
| 3803 | unsigned long, addr, vki_size_t, len, unsigned long, prot); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 3804 | |
tom | 6550519 | 2005-11-08 16:51:55 +0000 | [diff] [blame] | 3805 | if (!ML_(valid_client_addr)(ARG1, ARG2, tid, "mprotect")) { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3806 | SET_STATUS_Failure( VKI_ENOMEM ); |
sewardj | 337556e | 2005-11-09 14:51:59 +0000 | [diff] [blame] | 3807 | } |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 3808 | #if defined(VKI_PROT_GROWSDOWN) |
sewardj | 337556e | 2005-11-09 14:51:59 +0000 | [diff] [blame] | 3809 | else |
| 3810 | if (ARG3 & (VKI_PROT_GROWSDOWN|VKI_PROT_GROWSUP)) { |
| 3811 | /* Deal with mprotects on growable stack areas. |
| 3812 | |
| 3813 | The critical files to understand all this are mm/mprotect.c |
| 3814 | in the kernel and sysdeps/unix/sysv/linux/dl-execstack.c in |
| 3815 | glibc. |
| 3816 | |
| 3817 | The kernel provides PROT_GROWSDOWN and PROT_GROWSUP which |
| 3818 | round the start/end address of mprotect to the start/end of |
| 3819 | the underlying vma and glibc uses that as an easy way to |
| 3820 | change the protection of the stack by calling mprotect on the |
| 3821 | last page of the stack with PROT_GROWSDOWN set. |
| 3822 | |
| 3823 | The sanity check provided by the kernel is that the vma must |
| 3824 | have the VM_GROWSDOWN/VM_GROWSUP flag set as appropriate. */ |
tom | 6550519 | 2005-11-08 16:51:55 +0000 | [diff] [blame] | 3825 | UInt grows = ARG3 & (VKI_PROT_GROWSDOWN|VKI_PROT_GROWSUP); |
sewardj | ef1cf8b | 2006-10-17 01:47:30 +0000 | [diff] [blame] | 3826 | NSegment const *aseg = VG_(am_find_nsegment)(ARG1); |
| 3827 | NSegment const *rseg; |
tom | 6550519 | 2005-11-08 16:51:55 +0000 | [diff] [blame] | 3828 | |
| 3829 | vg_assert(aseg); |
| 3830 | |
| 3831 | if (grows == VKI_PROT_GROWSDOWN) { |
florian | 3e79863 | 2012-11-24 19:41:54 +0000 | [diff] [blame] | 3832 | rseg = VG_(am_next_nsegment)( aseg, False/*backwards*/ ); |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 3833 | if (rseg |
| 3834 | && rseg->kind == SkResvn |
| 3835 | && rseg->smode == SmUpper |
| 3836 | && rseg->end+1 == aseg->start) { |
tom | 6550519 | 2005-11-08 16:51:55 +0000 | [diff] [blame] | 3837 | Addr end = ARG1 + ARG2; |
| 3838 | ARG1 = aseg->start; |
| 3839 | ARG2 = end - aseg->start; |
| 3840 | ARG3 &= ~VKI_PROT_GROWSDOWN; |
| 3841 | } else { |
| 3842 | SET_STATUS_Failure( VKI_EINVAL ); |
| 3843 | } |
| 3844 | } else if (grows == VKI_PROT_GROWSUP) { |
florian | 3e79863 | 2012-11-24 19:41:54 +0000 | [diff] [blame] | 3845 | rseg = VG_(am_next_nsegment)( aseg, True/*forwards*/ ); |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 3846 | if (rseg |
| 3847 | && rseg->kind == SkResvn |
| 3848 | && rseg->smode == SmLower |
| 3849 | && aseg->end+1 == rseg->start) { |
tom | 6550519 | 2005-11-08 16:51:55 +0000 | [diff] [blame] | 3850 | ARG2 = aseg->end - ARG1 + 1; |
| 3851 | ARG3 &= ~VKI_PROT_GROWSUP; |
| 3852 | } else { |
| 3853 | SET_STATUS_Failure( VKI_EINVAL ); |
| 3854 | } |
| 3855 | } else { |
sewardj | 337556e | 2005-11-09 14:51:59 +0000 | [diff] [blame] | 3856 | /* both GROWSUP and GROWSDOWN */ |
tom | 6550519 | 2005-11-08 16:51:55 +0000 | [diff] [blame] | 3857 | SET_STATUS_Failure( VKI_EINVAL ); |
| 3858 | } |
| 3859 | } |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 3860 | #endif // defined(VKI_PROT_GROWSDOWN) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3861 | } |
| 3862 | |
nethercote | 85a456f | 2004-11-16 17:31:56 +0000 | [diff] [blame] | 3863 | POST(sys_mprotect) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3864 | { |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 3865 | Addr a = ARG1; |
| 3866 | SizeT len = ARG2; |
| 3867 | Int prot = ARG3; |
nethercote | 27ea8bc | 2004-07-10 17:21:14 +0000 | [diff] [blame] | 3868 | |
njn | dd37258 | 2009-05-10 22:42:19 +0000 | [diff] [blame] | 3869 | ML_(notify_core_and_tool_of_mprotect)(a, len, prot); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3870 | } |
| 3871 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3872 | PRE(sys_munmap) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3873 | { |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 3874 | if (0) VG_(printf)(" munmap( %#lx )\n", ARG1); |
| 3875 | PRINT("sys_munmap ( %#lx, %llu )", ARG1,(ULong)ARG2); |
nethercote | 06c7bd7 | 2004-11-14 19:11:56 +0000 | [diff] [blame] | 3876 | PRE_REG_READ2(long, "munmap", unsigned long, start, vki_size_t, length); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 3877 | |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 3878 | if (!ML_(valid_client_addr)(ARG1, ARG2, tid, "munmap")) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3879 | SET_STATUS_Failure( VKI_EINVAL ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3880 | } |
| 3881 | |
nethercote | 85a456f | 2004-11-16 17:31:56 +0000 | [diff] [blame] | 3882 | POST(sys_munmap) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3883 | { |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 3884 | Addr a = ARG1; |
| 3885 | SizeT len = ARG2; |
nethercote | 27ea8bc | 2004-07-10 17:21:14 +0000 | [diff] [blame] | 3886 | |
florian | ddd61ff | 2015-01-04 17:20:45 +0000 | [diff] [blame] | 3887 | ML_(notify_core_and_tool_of_munmap)( a, len ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3888 | } |
| 3889 | |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 3890 | PRE(sys_mincore) |
| 3891 | { |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 3892 | PRINT("sys_mincore ( %#lx, %llu, %#lx )", ARG1,(ULong)ARG2,ARG3); |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 3893 | PRE_REG_READ3(long, "mincore", |
| 3894 | unsigned long, start, vki_size_t, length, |
| 3895 | unsigned char *, vec); |
njn | cdaec51 | 2005-06-30 00:16:02 +0000 | [diff] [blame] | 3896 | PRE_MEM_WRITE( "mincore(vec)", ARG3, VG_PGROUNDUP(ARG2) / VKI_PAGE_SIZE ); |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 3897 | } |
| 3898 | POST(sys_mincore) |
| 3899 | { |
njn | cdaec51 | 2005-06-30 00:16:02 +0000 | [diff] [blame] | 3900 | POST_MEM_WRITE( ARG3, VG_PGROUNDUP(ARG2) / VKI_PAGE_SIZE ); |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 3901 | } |
mueller | 6ceb231 | 2004-01-02 22:52:34 +0000 | [diff] [blame] | 3902 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3903 | PRE(sys_nanosleep) |
mueller | 6ceb231 | 2004-01-02 22:52:34 +0000 | [diff] [blame] | 3904 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3905 | *flags |= SfMayBlock|SfPostOnFail; |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 3906 | PRINT("sys_nanosleep ( %#lx, %#lx )", ARG1,ARG2); |
nethercote | 5b653bc | 2004-11-15 14:32:12 +0000 | [diff] [blame] | 3907 | PRE_REG_READ2(long, "nanosleep", |
| 3908 | struct timespec *, req, struct timespec *, rem); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 3909 | PRE_MEM_READ( "nanosleep(req)", ARG1, sizeof(struct vki_timespec) ); |
| 3910 | if (ARG2 != 0) |
| 3911 | PRE_MEM_WRITE( "nanosleep(rem)", ARG2, sizeof(struct vki_timespec) ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3912 | } |
| 3913 | |
nethercote | 85a456f | 2004-11-16 17:31:56 +0000 | [diff] [blame] | 3914 | POST(sys_nanosleep) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3915 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3916 | vg_assert(SUCCESS || FAILURE); |
sewardj | ef1cf8b | 2006-10-17 01:47:30 +0000 | [diff] [blame] | 3917 | if (ARG2 != 0 && FAILURE && ERR == VKI_EINTR) |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 3918 | POST_MEM_WRITE( ARG2, sizeof(struct vki_timespec) ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3919 | } |
| 3920 | |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 3921 | #if defined(VGO_linux) || defined(VGO_solaris) |
| 3922 | /* Handles the case where the open is of /proc/self/auxv or |
| 3923 | /proc/<pid>/auxv, and just gives out a copy of the fd for the |
| 3924 | fake file we cooked up at startup (in m_main). Also, seeks the |
| 3925 | cloned fd back to the start. |
| 3926 | Returns True if auxv open was handled (status is set). */ |
| 3927 | Bool ML_(handle_auxv_open)(SyscallStatus *status, const HChar *filename, |
| 3928 | int flags) |
| 3929 | { |
| 3930 | HChar name[30]; // large enough |
| 3931 | |
| 3932 | if (!ML_(safe_to_deref)((const void *) filename, 1)) |
| 3933 | return False; |
| 3934 | |
| 3935 | /* Opening /proc/<pid>/auxv or /proc/self/auxv? */ |
| 3936 | VG_(sprintf)(name, "/proc/%d/auxv", VG_(getpid)()); |
| 3937 | if (!VG_STREQ(filename, name) && !VG_STREQ(filename, "/proc/self/auxv")) |
| 3938 | return False; |
| 3939 | |
| 3940 | /* Allow to open the file only for reading. */ |
| 3941 | if (flags & (VKI_O_WRONLY | VKI_O_RDWR)) { |
| 3942 | SET_STATUS_Failure(VKI_EACCES); |
| 3943 | return True; |
| 3944 | } |
| 3945 | |
| 3946 | # if defined(VGO_solaris) |
| 3947 | VG_(sprintf)(name, "/proc/self/fd/%d", VG_(cl_auxv_fd)); |
| 3948 | SysRes sres = VG_(open)(name, flags, 0); |
| 3949 | SET_STATUS_from_SysRes(sres); |
| 3950 | # else |
| 3951 | SysRes sres = VG_(dup)(VG_(cl_auxv_fd)); |
| 3952 | SET_STATUS_from_SysRes(sres); |
| 3953 | if (!sr_isError(sres)) { |
| 3954 | OffT off = VG_(lseek)(sr_Res(sres), 0, VKI_SEEK_SET); |
| 3955 | if (off < 0) |
| 3956 | SET_STATUS_Failure(VKI_EMFILE); |
| 3957 | } |
| 3958 | # endif |
| 3959 | |
| 3960 | return True; |
| 3961 | } |
| 3962 | #endif // defined(VGO_linux) || defined(VGO_solaris) |
| 3963 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 3964 | PRE(sys_open) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 3965 | { |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 3966 | if (ARG2 & VKI_O_CREAT) { |
nethercote | e824cc4 | 2004-11-09 16:20:46 +0000 | [diff] [blame] | 3967 | // 3-arg version |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3968 | PRINT("sys_open ( %#lx(%s), %ld, %ld )",ARG1, (HChar*)ARG1, SARG2, SARG3); |
nethercote | e824cc4 | 2004-11-09 16:20:46 +0000 | [diff] [blame] | 3969 | PRE_REG_READ3(long, "open", |
nethercote | 0df495a | 2004-11-11 16:38:21 +0000 | [diff] [blame] | 3970 | const char *, filename, int, flags, int, mode); |
nethercote | e70bd7d | 2004-08-18 14:37:17 +0000 | [diff] [blame] | 3971 | } else { |
nethercote | e824cc4 | 2004-11-09 16:20:46 +0000 | [diff] [blame] | 3972 | // 2-arg version |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 3973 | PRINT("sys_open ( %#lx(%s), %ld )",ARG1, (HChar*)ARG1, SARG2); |
nethercote | e824cc4 | 2004-11-09 16:20:46 +0000 | [diff] [blame] | 3974 | PRE_REG_READ2(long, "open", |
nethercote | 0df495a | 2004-11-11 16:38:21 +0000 | [diff] [blame] | 3975 | const char *, filename, int, flags); |
nethercote | e70bd7d | 2004-08-18 14:37:17 +0000 | [diff] [blame] | 3976 | } |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 3977 | PRE_MEM_RASCIIZ( "open(filename)", ARG1 ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3978 | |
bart | 9b533f8 | 2009-08-25 20:15:41 +0000 | [diff] [blame] | 3979 | #if defined(VGO_linux) |
| 3980 | /* Handle the case where the open is of /proc/self/cmdline or |
| 3981 | /proc/<pid>/cmdline, and just give it a copy of the fd for the |
| 3982 | fake file we cooked up at startup (in m_main). Also, seek the |
| 3983 | cloned fd back to the start. */ |
njn | 70fa47d | 2009-05-24 22:32:33 +0000 | [diff] [blame] | 3984 | { |
florian | f44ff62 | 2014-12-20 16:52:08 +0000 | [diff] [blame] | 3985 | HChar name[30]; // large enough |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 3986 | HChar* arg1s = (HChar*) ARG1; |
njn | 70fa47d | 2009-05-24 22:32:33 +0000 | [diff] [blame] | 3987 | SysRes sres; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3988 | |
njn | 70fa47d | 2009-05-24 22:32:33 +0000 | [diff] [blame] | 3989 | VG_(sprintf)(name, "/proc/%d/cmdline", VG_(getpid)()); |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 3990 | if (ML_(safe_to_deref)( arg1s, 1 ) |
| 3991 | && (VG_STREQ(arg1s, name) || VG_STREQ(arg1s, "/proc/self/cmdline"))) { |
njn | 70fa47d | 2009-05-24 22:32:33 +0000 | [diff] [blame] | 3992 | sres = VG_(dup)( VG_(cl_cmdline_fd) ); |
| 3993 | SET_STATUS_from_SysRes( sres ); |
| 3994 | if (!sr_isError(sres)) { |
| 3995 | OffT off = VG_(lseek)( sr_Res(sres), 0, VKI_SEEK_SET ); |
| 3996 | if (off < 0) |
| 3997 | SET_STATUS_Failure( VKI_EMFILE ); |
| 3998 | } |
| 3999 | return; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 4000 | } |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 4001 | } |
tom | 41ad7e7 | 2012-10-04 20:27:38 +0000 | [diff] [blame] | 4002 | |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 4003 | /* Handle also the case of /proc/self/auxv or /proc/<pid>/auxv. */ |
| 4004 | if (ML_(handle_auxv_open)(status, (const HChar *)ARG1, ARG2)) |
| 4005 | return; |
bart | 9b533f8 | 2009-08-25 20:15:41 +0000 | [diff] [blame] | 4006 | #endif // defined(VGO_linux) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 4007 | |
| 4008 | /* Otherwise handle normally */ |
| 4009 | *flags |= SfMayBlock; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4010 | } |
| 4011 | |
nethercote | 85a456f | 2004-11-16 17:31:56 +0000 | [diff] [blame] | 4012 | POST(sys_open) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4013 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4014 | vg_assert(SUCCESS); |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 4015 | if (!ML_(fd_allowed)(RES, "open", tid, True)) { |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4016 | VG_(close)(RES); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4017 | SET_STATUS_Failure( VKI_EMFILE ); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 4018 | } else { |
nethercote | 493dd18 | 2004-02-24 23:57:47 +0000 | [diff] [blame] | 4019 | if (VG_(clo_track_fds)) |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 4020 | ML_(record_fd_open_with_given_name)(tid, RES, (HChar*)ARG1); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4021 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4022 | } |
| 4023 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4024 | PRE(sys_read) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4025 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4026 | *flags |= SfMayBlock; |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 4027 | PRINT("sys_read ( %lu, %#lx, %lu )", ARG1, ARG2, ARG3); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4028 | PRE_REG_READ3(ssize_t, "read", |
njn | ca0518d | 2004-11-26 19:34:36 +0000 | [diff] [blame] | 4029 | unsigned int, fd, char *, buf, vki_size_t, count); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4030 | |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 4031 | if (!ML_(fd_allowed)(ARG1, "read", tid, False)) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4032 | SET_STATUS_Failure( VKI_EBADF ); |
thughes | 26ab77b | 2004-08-14 12:10:49 +0000 | [diff] [blame] | 4033 | else |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4034 | PRE_MEM_WRITE( "read(buf)", ARG2, ARG3 ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4035 | } |
| 4036 | |
nethercote | 85a456f | 2004-11-16 17:31:56 +0000 | [diff] [blame] | 4037 | POST(sys_read) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4038 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4039 | vg_assert(SUCCESS); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4040 | POST_MEM_WRITE( ARG2, RES ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4041 | } |
| 4042 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4043 | PRE(sys_write) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4044 | { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 4045 | Bool ok; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4046 | *flags |= SfMayBlock; |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 4047 | PRINT("sys_write ( %lu, %#lx, %lu )", ARG1, ARG2, ARG3); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 4048 | PRE_REG_READ3(ssize_t, "write", |
njn | ca0518d | 2004-11-26 19:34:36 +0000 | [diff] [blame] | 4049 | unsigned int, fd, const char *, buf, vki_size_t, count); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 4050 | /* check to see if it is allowed. If not, try for an exemption from |
njn | 628add6 | 2005-11-12 18:21:40 +0000 | [diff] [blame] | 4051 | --sim-hints=enable-outer (used for self hosting). */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 4052 | ok = ML_(fd_allowed)(ARG1, "write", tid, False); |
| 4053 | if (!ok && ARG1 == 2/*stderr*/ |
philippe | ec905f7 | 2014-08-17 20:03:51 +0000 | [diff] [blame] | 4054 | && SimHintiS(SimHint_enable_outer, VG_(clo_sim_hints))) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 4055 | ok = True; |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 4056 | #if defined(VGO_solaris) |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 4057 | if (!ok && VG_(vfork_fildes_addr) != NULL |
| 4058 | && *VG_(vfork_fildes_addr) >= 0 && *VG_(vfork_fildes_addr) == ARG1) |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 4059 | ok = True; |
| 4060 | #endif |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 4061 | if (!ok) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4062 | SET_STATUS_Failure( VKI_EBADF ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4063 | else |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4064 | PRE_MEM_READ( "write(buf)", ARG2, ARG3 ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4065 | } |
| 4066 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4067 | PRE(sys_creat) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4068 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4069 | *flags |= SfMayBlock; |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 4070 | PRINT("sys_creat ( %#lx(%s), %ld )", ARG1, (HChar*)ARG1, SARG2); |
nethercote | c6851dd | 2004-11-11 18:00:47 +0000 | [diff] [blame] | 4071 | PRE_REG_READ2(long, "creat", const char *, pathname, int, mode); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4072 | PRE_MEM_RASCIIZ( "creat(pathname)", ARG1 ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4073 | } |
| 4074 | |
nethercote | 85a456f | 2004-11-16 17:31:56 +0000 | [diff] [blame] | 4075 | POST(sys_creat) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4076 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4077 | vg_assert(SUCCESS); |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 4078 | if (!ML_(fd_allowed)(RES, "creat", tid, True)) { |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4079 | VG_(close)(RES); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4080 | SET_STATUS_Failure( VKI_EMFILE ); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 4081 | } else { |
nethercote | 493dd18 | 2004-02-24 23:57:47 +0000 | [diff] [blame] | 4082 | if (VG_(clo_track_fds)) |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 4083 | ML_(record_fd_open_with_given_name)(tid, RES, (HChar*)ARG1); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4084 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4085 | } |
| 4086 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4087 | PRE(sys_poll) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4088 | { |
| 4089 | /* struct pollfd { |
nethercote | ef0c766 | 2004-11-06 15:38:43 +0000 | [diff] [blame] | 4090 | int fd; -- file descriptor |
| 4091 | short events; -- requested events |
| 4092 | short revents; -- returned events |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4093 | }; |
nethercote | eb0592d | 2004-11-05 12:02:27 +0000 | [diff] [blame] | 4094 | int poll(struct pollfd *ufds, unsigned int nfds, int timeout) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4095 | */ |
nethercote | eb0592d | 2004-11-05 12:02:27 +0000 | [diff] [blame] | 4096 | UInt i; |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4097 | struct vki_pollfd* ufds = (struct vki_pollfd *)ARG1; |
tom | 60a4b0b | 2005-10-12 10:45:27 +0000 | [diff] [blame] | 4098 | *flags |= SfMayBlock; |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 4099 | PRINT("sys_poll ( %#lx, %lu, %ld )\n", ARG1, ARG2, SARG3); |
nethercote | f90953e | 2004-11-15 14:50:02 +0000 | [diff] [blame] | 4100 | PRE_REG_READ3(long, "poll", |
tom | 363ec76 | 2006-03-21 10:58:35 +0000 | [diff] [blame] | 4101 | struct vki_pollfd *, ufds, unsigned int, nfds, long, timeout); |
| 4102 | |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4103 | for (i = 0; i < ARG2; i++) { |
tom | 363ec76 | 2006-03-21 10:58:35 +0000 | [diff] [blame] | 4104 | PRE_MEM_READ( "poll(ufds.fd)", |
| 4105 | (Addr)(&ufds[i].fd), sizeof(ufds[i].fd) ); |
| 4106 | PRE_MEM_READ( "poll(ufds.events)", |
| 4107 | (Addr)(&ufds[i].events), sizeof(ufds[i].events) ); |
tom | 68f338f | 2014-02-09 11:10:08 +0000 | [diff] [blame] | 4108 | PRE_MEM_WRITE( "poll(ufds.revents)", |
tom | 363ec76 | 2006-03-21 10:58:35 +0000 | [diff] [blame] | 4109 | (Addr)(&ufds[i].revents), sizeof(ufds[i].revents) ); |
| 4110 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4111 | } |
| 4112 | |
nethercote | 85a456f | 2004-11-16 17:31:56 +0000 | [diff] [blame] | 4113 | POST(sys_poll) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4114 | { |
sewardj | 416b206 | 2008-05-12 15:46:34 +0000 | [diff] [blame] | 4115 | if (RES >= 0) { |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4116 | UInt i; |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4117 | struct vki_pollfd* ufds = (struct vki_pollfd *)ARG1; |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4118 | for (i = 0; i < ARG2; i++) |
tom | 363ec76 | 2006-03-21 10:58:35 +0000 | [diff] [blame] | 4119 | POST_MEM_WRITE( (Addr)(&ufds[i].revents), sizeof(ufds[i].revents) ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4120 | } |
| 4121 | } |
| 4122 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4123 | PRE(sys_readlink) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4124 | { |
sewardj | cc3de2d | 2011-08-18 15:08:20 +0000 | [diff] [blame] | 4125 | FUSE_COMPATIBLE_MAY_BLOCK(); |
njn | cc1284f | 2009-05-23 01:51:54 +0000 | [diff] [blame] | 4126 | Word saved = SYSNO; |
sewardj | 144c56d | 2005-09-30 01:09:50 +0000 | [diff] [blame] | 4127 | |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 4128 | PRINT("sys_readlink ( %#lx(%s), %#lx, %llu )", ARG1,(char*)ARG1,ARG2,(ULong)ARG3); |
nethercote | 5a945af | 2004-11-14 18:37:07 +0000 | [diff] [blame] | 4129 | PRE_REG_READ3(long, "readlink", |
| 4130 | const char *, path, char *, buf, int, bufsiz); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4131 | PRE_MEM_RASCIIZ( "readlink(path)", ARG1 ); |
| 4132 | PRE_MEM_WRITE( "readlink(buf)", ARG2,ARG3 ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4133 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 4134 | |
njn | cc1284f | 2009-05-23 01:51:54 +0000 | [diff] [blame] | 4135 | { |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 4136 | #if defined(VGO_linux) || defined(VGO_solaris) |
bart | 9b533f8 | 2009-08-25 20:15:41 +0000 | [diff] [blame] | 4137 | #if defined(VGO_linux) |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 4138 | #define PID_EXEPATH "/proc/%d/exe" |
| 4139 | #define SELF_EXEPATH "/proc/self/exe" |
| 4140 | #define SELF_EXEFD "/proc/self/fd/%d" |
| 4141 | #elif defined(VGO_solaris) |
| 4142 | #define PID_EXEPATH "/proc/%d/path/a.out" |
| 4143 | #define SELF_EXEPATH "/proc/self/path/a.out" |
| 4144 | #define SELF_EXEFD "/proc/self/path/%d" |
| 4145 | #endif |
njn | cc1284f | 2009-05-23 01:51:54 +0000 | [diff] [blame] | 4146 | /* |
| 4147 | * Handle the case where readlink is looking at /proc/self/exe or |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 4148 | * /proc/<pid>/exe, or equivalent on Solaris. |
njn | cc1284f | 2009-05-23 01:51:54 +0000 | [diff] [blame] | 4149 | */ |
florian | f44ff62 | 2014-12-20 16:52:08 +0000 | [diff] [blame] | 4150 | HChar name[30]; // large enough |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 4151 | HChar* arg1s = (HChar*) ARG1; |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 4152 | VG_(sprintf)(name, PID_EXEPATH, VG_(getpid)()); |
| 4153 | if (ML_(safe_to_deref)(arg1s, 1) |
| 4154 | && (VG_STREQ(arg1s, name) || VG_STREQ(arg1s, SELF_EXEPATH))) { |
| 4155 | VG_(sprintf)(name, SELF_EXEFD, VG_(cl_exec_fd)); |
njn | cc1284f | 2009-05-23 01:51:54 +0000 | [diff] [blame] | 4156 | SET_STATUS_from_SysRes( VG_(do_syscall3)(saved, (UWord)name, |
| 4157 | ARG2, ARG3)); |
| 4158 | } else |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 4159 | #endif |
njn | cc1284f | 2009-05-23 01:51:54 +0000 | [diff] [blame] | 4160 | { |
| 4161 | /* Normal case */ |
| 4162 | SET_STATUS_from_SysRes( VG_(do_syscall3)(saved, ARG1, ARG2, ARG3)); |
| 4163 | } |
rjwalsh | 17d8530 | 2004-11-18 22:56:09 +0000 | [diff] [blame] | 4164 | } |
| 4165 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4166 | if (SUCCESS && RES > 0) |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4167 | POST_MEM_WRITE( ARG2, RES ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4168 | } |
| 4169 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4170 | PRE(sys_readv) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4171 | { |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4172 | Int i; |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 4173 | struct vki_iovec * vec; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4174 | *flags |= SfMayBlock; |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 4175 | PRINT("sys_readv ( %lu, %#lx, %lu )", ARG1, ARG2, ARG3); |
nethercote | d6b5a21 | 2004-11-15 17:04:14 +0000 | [diff] [blame] | 4176 | PRE_REG_READ3(ssize_t, "readv", |
| 4177 | unsigned long, fd, const struct iovec *, vector, |
| 4178 | unsigned long, count); |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 4179 | if (!ML_(fd_allowed)(ARG1, "readv", tid, False)) { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4180 | SET_STATUS_Failure( VKI_EBADF ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4181 | } else { |
florian | 7cbe068 | 2015-04-25 18:14:17 +0000 | [diff] [blame] | 4182 | if ((Int)ARG3 >= 0) |
| 4183 | PRE_MEM_READ( "readv(vector)", ARG2, ARG3 * sizeof(struct vki_iovec) ); |
nethercote | d6b5a21 | 2004-11-15 17:04:14 +0000 | [diff] [blame] | 4184 | |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4185 | if (ARG2 != 0) { |
nethercote | d6b5a21 | 2004-11-15 17:04:14 +0000 | [diff] [blame] | 4186 | /* ToDo: don't do any of the following if the vector is invalid */ |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4187 | vec = (struct vki_iovec *)ARG2; |
| 4188 | for (i = 0; i < (Int)ARG3; i++) |
nethercote | d6b5a21 | 2004-11-15 17:04:14 +0000 | [diff] [blame] | 4189 | PRE_MEM_WRITE( "readv(vector[...])", |
| 4190 | (Addr)vec[i].iov_base, vec[i].iov_len ); |
| 4191 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4192 | } |
| 4193 | } |
| 4194 | |
nethercote | 85a456f | 2004-11-16 17:31:56 +0000 | [diff] [blame] | 4195 | POST(sys_readv) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4196 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4197 | vg_assert(SUCCESS); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4198 | if (RES > 0) { |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4199 | Int i; |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4200 | struct vki_iovec * vec = (struct vki_iovec *)ARG2; |
| 4201 | Int remains = RES; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4202 | |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4203 | /* RES holds the number of bytes read. */ |
| 4204 | for (i = 0; i < (Int)ARG3; i++) { |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4205 | Int nReadThisBuf = vec[i].iov_len; |
| 4206 | if (nReadThisBuf > remains) nReadThisBuf = remains; |
nethercote | ef0c766 | 2004-11-06 15:38:43 +0000 | [diff] [blame] | 4207 | POST_MEM_WRITE( (Addr)vec[i].iov_base, nReadThisBuf ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4208 | remains -= nReadThisBuf; |
| 4209 | if (remains < 0) VG_(core_panic)("readv: remains < 0"); |
| 4210 | } |
| 4211 | } |
| 4212 | } |
| 4213 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4214 | PRE(sys_rename) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4215 | { |
sewardj | cc3de2d | 2011-08-18 15:08:20 +0000 | [diff] [blame] | 4216 | FUSE_COMPATIBLE_MAY_BLOCK(); |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 4217 | PRINT("sys_rename ( %#lx(%s), %#lx(%s) )", ARG1,(char*)ARG1,ARG2,(char*)ARG2); |
nethercote | 9a3beb9 | 2004-11-12 17:07:26 +0000 | [diff] [blame] | 4218 | PRE_REG_READ2(long, "rename", const char *, oldpath, const char *, newpath); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4219 | PRE_MEM_RASCIIZ( "rename(oldpath)", ARG1 ); |
| 4220 | PRE_MEM_RASCIIZ( "rename(newpath)", ARG2 ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4221 | } |
| 4222 | |
sewardj | 78b50e4 | 2005-06-08 01:47:28 +0000 | [diff] [blame] | 4223 | PRE(sys_rmdir) |
| 4224 | { |
| 4225 | *flags |= SfMayBlock; |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 4226 | PRINT("sys_rmdir ( %#lx(%s) )", ARG1,(char*)ARG1); |
sewardj | 78b50e4 | 2005-06-08 01:47:28 +0000 | [diff] [blame] | 4227 | PRE_REG_READ1(long, "rmdir", const char *, pathname); |
| 4228 | PRE_MEM_RASCIIZ( "rmdir(pathname)", ARG1 ); |
| 4229 | } |
| 4230 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4231 | PRE(sys_select) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4232 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4233 | *flags |= SfMayBlock; |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 4234 | PRINT("sys_select ( %ld, %#lx, %#lx, %#lx, %#lx )", SARG1, ARG2, ARG3, |
| 4235 | ARG4, ARG5); |
nethercote | f1049bf | 2004-11-14 17:03:47 +0000 | [diff] [blame] | 4236 | PRE_REG_READ5(long, "select", |
| 4237 | int, n, vki_fd_set *, readfds, vki_fd_set *, writefds, |
tom | 363ec76 | 2006-03-21 10:58:35 +0000 | [diff] [blame] | 4238 | vki_fd_set *, exceptfds, struct vki_timeval *, timeout); |
nethercote | f1049bf | 2004-11-14 17:03:47 +0000 | [diff] [blame] | 4239 | // XXX: this possibly understates how much memory is read. |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4240 | if (ARG2 != 0) |
nethercote | f1049bf | 2004-11-14 17:03:47 +0000 | [diff] [blame] | 4241 | PRE_MEM_READ( "select(readfds)", |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4242 | ARG2, ARG1/8 /* __FD_SETSIZE/8 */ ); |
| 4243 | if (ARG3 != 0) |
nethercote | f1049bf | 2004-11-14 17:03:47 +0000 | [diff] [blame] | 4244 | PRE_MEM_READ( "select(writefds)", |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4245 | ARG3, ARG1/8 /* __FD_SETSIZE/8 */ ); |
| 4246 | if (ARG4 != 0) |
nethercote | f1049bf | 2004-11-14 17:03:47 +0000 | [diff] [blame] | 4247 | PRE_MEM_READ( "select(exceptfds)", |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4248 | ARG4, ARG1/8 /* __FD_SETSIZE/8 */ ); |
| 4249 | if (ARG5 != 0) |
njn | 72bbd8d | 2009-05-22 08:12:46 +0000 | [diff] [blame] | 4250 | PRE_timeval_READ( "select(timeout)", ARG5 ); |
nethercote | f1049bf | 2004-11-14 17:03:47 +0000 | [diff] [blame] | 4251 | } |
| 4252 | |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 4253 | PRE(sys_setgid) |
| 4254 | { |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 4255 | PRINT("sys_setgid ( %lu )", ARG1); |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 4256 | PRE_REG_READ1(long, "setgid", vki_gid_t, gid); |
| 4257 | } |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 4258 | |
| 4259 | PRE(sys_setsid) |
| 4260 | { |
| 4261 | PRINT("sys_setsid ( )"); |
| 4262 | PRE_REG_READ0(long, "setsid"); |
| 4263 | } |
| 4264 | |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 4265 | PRE(sys_setgroups) |
| 4266 | { |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 4267 | PRINT("setgroups ( %llu, %#lx )", (ULong)ARG1, ARG2); |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 4268 | PRE_REG_READ2(long, "setgroups", int, size, vki_gid_t *, list); |
| 4269 | if (ARG1 > 0) |
| 4270 | PRE_MEM_READ( "setgroups(list)", ARG2, ARG1 * sizeof(vki_gid_t) ); |
| 4271 | } |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 4272 | |
| 4273 | PRE(sys_setpgid) |
| 4274 | { |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 4275 | PRINT("setpgid ( %ld, %ld )", SARG1, SARG2); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 4276 | PRE_REG_READ2(long, "setpgid", vki_pid_t, pid, vki_pid_t, pgid); |
| 4277 | } |
| 4278 | |
| 4279 | PRE(sys_setregid) |
| 4280 | { |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 4281 | PRINT("sys_setregid ( %lu, %lu )", ARG1, ARG2); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 4282 | PRE_REG_READ2(long, "setregid", vki_gid_t, rgid, vki_gid_t, egid); |
| 4283 | } |
| 4284 | |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 4285 | PRE(sys_setreuid) |
| 4286 | { |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 4287 | PRINT("sys_setreuid ( 0x%lx, 0x%lx )", ARG1, ARG2); |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 4288 | PRE_REG_READ2(long, "setreuid", vki_uid_t, ruid, vki_uid_t, euid); |
| 4289 | } |
nethercote | 9c311eb | 2004-11-12 18:20:12 +0000 | [diff] [blame] | 4290 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4291 | PRE(sys_setrlimit) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4292 | { |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 4293 | UWord arg1 = ARG1; |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 4294 | PRINT("sys_setrlimit ( %lu, %#lx )", ARG1, ARG2); |
nethercote | 17258dc | 2004-11-12 19:55:08 +0000 | [diff] [blame] | 4295 | PRE_REG_READ2(long, "setrlimit", |
| 4296 | unsigned int, resource, struct rlimit *, rlim); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4297 | PRE_MEM_READ( "setrlimit(rlim)", ARG2, sizeof(struct vki_rlimit) ); |
fitzhardinge | b50068f | 2004-02-24 23:42:55 +0000 | [diff] [blame] | 4298 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 4299 | #ifdef _RLIMIT_POSIX_FLAG |
| 4300 | // Darwin will sometimes set _RLIMIT_POSIX_FLAG on setrlimit calls. |
| 4301 | // Unset it here to make the if statements below work correctly. |
| 4302 | arg1 &= ~_RLIMIT_POSIX_FLAG; |
| 4303 | #endif |
| 4304 | |
philippe | db16f83 | 2014-09-19 19:35:24 +0000 | [diff] [blame] | 4305 | if (!VG_(am_is_valid_for_client)(ARG2, sizeof(struct vki_rlimit), |
| 4306 | VKI_PROT_READ)) { |
| 4307 | SET_STATUS_Failure( VKI_EFAULT ); |
| 4308 | } |
| 4309 | else if (((struct vki_rlimit *)ARG2)->rlim_cur |
| 4310 | > ((struct vki_rlimit *)ARG2)->rlim_max) { |
tom | aba260b | 2012-03-01 13:42:18 +0000 | [diff] [blame] | 4311 | SET_STATUS_Failure( VKI_EINVAL ); |
| 4312 | } |
| 4313 | else if (arg1 == VKI_RLIMIT_NOFILE) { |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4314 | if (((struct vki_rlimit *)ARG2)->rlim_cur > VG_(fd_hard_limit) || |
| 4315 | ((struct vki_rlimit *)ARG2)->rlim_max != VG_(fd_hard_limit)) { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4316 | SET_STATUS_Failure( VKI_EPERM ); |
thughes | ad1c956 | 2004-06-26 11:27:52 +0000 | [diff] [blame] | 4317 | } |
| 4318 | else { |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4319 | VG_(fd_soft_limit) = ((struct vki_rlimit *)ARG2)->rlim_cur; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4320 | SET_STATUS_Success( 0 ); |
thughes | ad1c956 | 2004-06-26 11:27:52 +0000 | [diff] [blame] | 4321 | } |
| 4322 | } |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 4323 | else if (arg1 == VKI_RLIMIT_DATA) { |
tom | ae0739d | 2006-03-20 15:36:28 +0000 | [diff] [blame] | 4324 | if (((struct vki_rlimit *)ARG2)->rlim_cur > VG_(client_rlimit_data).rlim_max || |
| 4325 | ((struct vki_rlimit *)ARG2)->rlim_max > VG_(client_rlimit_data).rlim_max) { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4326 | SET_STATUS_Failure( VKI_EPERM ); |
thughes | aa4fb11 | 2004-09-11 14:19:25 +0000 | [diff] [blame] | 4327 | } |
| 4328 | else { |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4329 | VG_(client_rlimit_data) = *(struct vki_rlimit *)ARG2; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4330 | SET_STATUS_Success( 0 ); |
thughes | aa4fb11 | 2004-09-11 14:19:25 +0000 | [diff] [blame] | 4331 | } |
fitzhardinge | b50068f | 2004-02-24 23:42:55 +0000 | [diff] [blame] | 4332 | } |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 4333 | else if (arg1 == VKI_RLIMIT_STACK && tid == 1) { |
tom | ae0739d | 2006-03-20 15:36:28 +0000 | [diff] [blame] | 4334 | if (((struct vki_rlimit *)ARG2)->rlim_cur > VG_(client_rlimit_stack).rlim_max || |
| 4335 | ((struct vki_rlimit *)ARG2)->rlim_max > VG_(client_rlimit_stack).rlim_max) { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4336 | SET_STATUS_Failure( VKI_EPERM ); |
thughes | c37184f | 2004-09-11 14:16:57 +0000 | [diff] [blame] | 4337 | } |
| 4338 | else { |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 4339 | /* Change the value of client_stack_szB to the rlim_cur value but |
| 4340 | only if it is smaller than the size of the allocated stack for the |
| 4341 | client. |
| 4342 | TODO: All platforms should set VG_(clstk_max_size) as part of their |
| 4343 | setup_client_stack(). */ |
| 4344 | if ((VG_(clstk_max_size) == 0) |
| 4345 | || (((struct vki_rlimit *) ARG2)->rlim_cur <= VG_(clstk_max_size))) |
| 4346 | VG_(threads)[tid].client_stack_szB = ((struct vki_rlimit *)ARG2)->rlim_cur; |
| 4347 | |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4348 | VG_(client_rlimit_stack) = *(struct vki_rlimit *)ARG2; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4349 | SET_STATUS_Success( 0 ); |
thughes | c37184f | 2004-09-11 14:16:57 +0000 | [diff] [blame] | 4350 | } |
| 4351 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4352 | } |
| 4353 | |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 4354 | PRE(sys_setuid) |
| 4355 | { |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 4356 | PRINT("sys_setuid ( %lu )", ARG1); |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 4357 | PRE_REG_READ1(long, "setuid", vki_uid_t, uid); |
| 4358 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4359 | |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 4360 | PRE(sys_newstat) |
| 4361 | { |
philippe | d7aa40d | 2015-04-19 12:39:33 +0000 | [diff] [blame] | 4362 | FUSE_COMPATIBLE_MAY_BLOCK(); |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 4363 | PRINT("sys_newstat ( %#lx(%s), %#lx )", ARG1,(char*)ARG1,ARG2); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 4364 | PRE_REG_READ2(long, "stat", char *, file_name, struct stat *, buf); |
| 4365 | PRE_MEM_RASCIIZ( "stat(file_name)", ARG1 ); |
| 4366 | PRE_MEM_WRITE( "stat(buf)", ARG2, sizeof(struct vki_stat) ); |
| 4367 | } |
| 4368 | |
| 4369 | POST(sys_newstat) |
| 4370 | { |
| 4371 | POST_MEM_WRITE( ARG2, sizeof(struct vki_stat) ); |
| 4372 | } |
| 4373 | |
| 4374 | PRE(sys_statfs) |
| 4375 | { |
philippe | d7aa40d | 2015-04-19 12:39:33 +0000 | [diff] [blame] | 4376 | FUSE_COMPATIBLE_MAY_BLOCK(); |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 4377 | PRINT("sys_statfs ( %#lx(%s), %#lx )",ARG1,(char*)ARG1,ARG2); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 4378 | PRE_REG_READ2(long, "statfs", const char *, path, struct statfs *, buf); |
| 4379 | PRE_MEM_RASCIIZ( "statfs(path)", ARG1 ); |
| 4380 | PRE_MEM_WRITE( "statfs(buf)", ARG2, sizeof(struct vki_statfs) ); |
| 4381 | } |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 4382 | POST(sys_statfs) |
| 4383 | { |
| 4384 | POST_MEM_WRITE( ARG2, sizeof(struct vki_statfs) ); |
| 4385 | } |
| 4386 | |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 4387 | PRE(sys_statfs64) |
| 4388 | { |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 4389 | PRINT("sys_statfs64 ( %#lx(%s), %llu, %#lx )",ARG1,(char*)ARG1,(ULong)ARG2,ARG3); |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 4390 | PRE_REG_READ3(long, "statfs64", |
| 4391 | const char *, path, vki_size_t, size, struct statfs64 *, buf); |
| 4392 | PRE_MEM_RASCIIZ( "statfs64(path)", ARG1 ); |
| 4393 | PRE_MEM_WRITE( "statfs64(buf)", ARG3, ARG2 ); |
| 4394 | } |
| 4395 | POST(sys_statfs64) |
| 4396 | { |
| 4397 | POST_MEM_WRITE( ARG3, ARG2 ); |
| 4398 | } |
sewardj | 78b50e4 | 2005-06-08 01:47:28 +0000 | [diff] [blame] | 4399 | |
| 4400 | PRE(sys_symlink) |
| 4401 | { |
| 4402 | *flags |= SfMayBlock; |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 4403 | PRINT("sys_symlink ( %#lx(%s), %#lx(%s) )",ARG1,(char*)ARG1,ARG2,(char*)ARG2); |
sewardj | 78b50e4 | 2005-06-08 01:47:28 +0000 | [diff] [blame] | 4404 | PRE_REG_READ2(long, "symlink", const char *, oldpath, const char *, newpath); |
| 4405 | PRE_MEM_RASCIIZ( "symlink(oldpath)", ARG1 ); |
| 4406 | PRE_MEM_RASCIIZ( "symlink(newpath)", ARG2 ); |
| 4407 | } |
sewardj | 9efbbef | 2005-03-01 16:45:23 +0000 | [diff] [blame] | 4408 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4409 | PRE(sys_time) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4410 | { |
| 4411 | /* time_t time(time_t *t); */ |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 4412 | PRINT("sys_time ( %#lx )",ARG1); |
nethercote | c6851dd | 2004-11-11 18:00:47 +0000 | [diff] [blame] | 4413 | PRE_REG_READ1(long, "time", int *, t); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4414 | if (ARG1 != 0) { |
| 4415 | PRE_MEM_WRITE( "time(t)", ARG1, sizeof(vki_time_t) ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4416 | } |
| 4417 | } |
| 4418 | |
nethercote | 85a456f | 2004-11-16 17:31:56 +0000 | [diff] [blame] | 4419 | POST(sys_time) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4420 | { |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4421 | if (ARG1 != 0) { |
| 4422 | POST_MEM_WRITE( ARG1, sizeof(vki_time_t) ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4423 | } |
| 4424 | } |
| 4425 | |
sewardj | 78b50e4 | 2005-06-08 01:47:28 +0000 | [diff] [blame] | 4426 | PRE(sys_times) |
| 4427 | { |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 4428 | PRINT("sys_times ( %#lx )", ARG1); |
sewardj | 78b50e4 | 2005-06-08 01:47:28 +0000 | [diff] [blame] | 4429 | PRE_REG_READ1(long, "times", struct tms *, buf); |
tom | 18f1927 | 2005-07-18 11:03:35 +0000 | [diff] [blame] | 4430 | if (ARG1 != 0) { |
| 4431 | PRE_MEM_WRITE( "times(buf)", ARG1, sizeof(struct vki_tms) ); |
| 4432 | } |
sewardj | 78b50e4 | 2005-06-08 01:47:28 +0000 | [diff] [blame] | 4433 | } |
| 4434 | |
| 4435 | POST(sys_times) |
| 4436 | { |
| 4437 | if (ARG1 != 0) { |
| 4438 | POST_MEM_WRITE( ARG1, sizeof(struct vki_tms) ); |
| 4439 | } |
| 4440 | } |
| 4441 | |
| 4442 | PRE(sys_umask) |
| 4443 | { |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 4444 | PRINT("sys_umask ( %ld )", SARG1); |
sewardj | 78b50e4 | 2005-06-08 01:47:28 +0000 | [diff] [blame] | 4445 | PRE_REG_READ1(long, "umask", int, mask); |
| 4446 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4447 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4448 | PRE(sys_unlink) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4449 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4450 | *flags |= SfMayBlock; |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 4451 | PRINT("sys_unlink ( %#lx(%s) )", ARG1,(char*)ARG1); |
nethercote | c6851dd | 2004-11-11 18:00:47 +0000 | [diff] [blame] | 4452 | PRE_REG_READ1(long, "unlink", const char *, pathname); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4453 | PRE_MEM_RASCIIZ( "unlink(pathname)", ARG1 ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4454 | } |
| 4455 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4456 | PRE(sys_newuname) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4457 | { |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 4458 | PRINT("sys_newuname ( %#lx )", ARG1); |
nethercote | 1a1b9b7 | 2004-11-12 11:19:36 +0000 | [diff] [blame] | 4459 | PRE_REG_READ1(long, "uname", struct new_utsname *, buf); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4460 | PRE_MEM_WRITE( "uname(buf)", ARG1, sizeof(struct vki_new_utsname) ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4461 | } |
| 4462 | |
nethercote | 85a456f | 2004-11-16 17:31:56 +0000 | [diff] [blame] | 4463 | POST(sys_newuname) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4464 | { |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4465 | if (ARG1 != 0) { |
| 4466 | POST_MEM_WRITE( ARG1, sizeof(struct vki_new_utsname) ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4467 | } |
| 4468 | } |
| 4469 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4470 | PRE(sys_waitpid) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4471 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4472 | *flags |= SfMayBlock; |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 4473 | PRINT("sys_waitpid ( %ld, %#lx, %ld )", SARG1, ARG2, SARG3); |
nethercote | c6851dd | 2004-11-11 18:00:47 +0000 | [diff] [blame] | 4474 | PRE_REG_READ3(long, "waitpid", |
| 4475 | vki_pid_t, pid, unsigned int *, status, int, options); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4476 | |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4477 | if (ARG2 != (Addr)NULL) |
| 4478 | PRE_MEM_WRITE( "waitpid(status)", ARG2, sizeof(int) ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4479 | } |
| 4480 | |
nethercote | 85a456f | 2004-11-16 17:31:56 +0000 | [diff] [blame] | 4481 | POST(sys_waitpid) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4482 | { |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4483 | if (ARG2 != (Addr)NULL) |
| 4484 | POST_MEM_WRITE( ARG2, sizeof(int) ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4485 | } |
| 4486 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4487 | PRE(sys_wait4) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4488 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4489 | *flags |= SfMayBlock; |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 4490 | PRINT("sys_wait4 ( %ld, %#lx, %ld, %#lx )", SARG1, ARG2, SARG3, ARG4); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4491 | |
nethercote | 7f7e4d1 | 2004-11-15 12:28:58 +0000 | [diff] [blame] | 4492 | PRE_REG_READ4(long, "wait4", |
| 4493 | vki_pid_t, pid, unsigned int *, status, int, options, |
| 4494 | struct rusage *, rusage); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4495 | if (ARG2 != (Addr)NULL) |
| 4496 | PRE_MEM_WRITE( "wait4(status)", ARG2, sizeof(int) ); |
| 4497 | if (ARG4 != (Addr)NULL) |
| 4498 | PRE_MEM_WRITE( "wait4(rusage)", ARG4, sizeof(struct vki_rusage) ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4499 | } |
| 4500 | |
nethercote | 85a456f | 2004-11-16 17:31:56 +0000 | [diff] [blame] | 4501 | POST(sys_wait4) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4502 | { |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4503 | if (ARG2 != (Addr)NULL) |
| 4504 | POST_MEM_WRITE( ARG2, sizeof(int) ); |
| 4505 | if (ARG4 != (Addr)NULL) |
| 4506 | POST_MEM_WRITE( ARG4, sizeof(struct vki_rusage) ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4507 | } |
| 4508 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4509 | PRE(sys_writev) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4510 | { |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4511 | Int i; |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 4512 | struct vki_iovec * vec; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4513 | *flags |= SfMayBlock; |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 4514 | PRINT("sys_writev ( %lu, %#lx, %lu )", ARG1, ARG2, ARG3); |
nethercote | d6b5a21 | 2004-11-15 17:04:14 +0000 | [diff] [blame] | 4515 | PRE_REG_READ3(ssize_t, "writev", |
| 4516 | unsigned long, fd, const struct iovec *, vector, |
| 4517 | unsigned long, count); |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 4518 | if (!ML_(fd_allowed)(ARG1, "writev", tid, False)) { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4519 | SET_STATUS_Failure( VKI_EBADF ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4520 | } else { |
florian | 7cbe068 | 2015-04-25 18:14:17 +0000 | [diff] [blame] | 4521 | if ((Int)ARG3 >= 0) |
| 4522 | PRE_MEM_READ( "writev(vector)", |
| 4523 | ARG2, ARG3 * sizeof(struct vki_iovec) ); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4524 | if (ARG2 != 0) { |
nethercote | d6b5a21 | 2004-11-15 17:04:14 +0000 | [diff] [blame] | 4525 | /* ToDo: don't do any of the following if the vector is invalid */ |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4526 | vec = (struct vki_iovec *)ARG2; |
| 4527 | for (i = 0; i < (Int)ARG3; i++) |
nethercote | d6b5a21 | 2004-11-15 17:04:14 +0000 | [diff] [blame] | 4528 | PRE_MEM_READ( "writev(vector[...])", |
| 4529 | (Addr)vec[i].iov_base, vec[i].iov_len ); |
| 4530 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4531 | } |
| 4532 | } |
| 4533 | |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 4534 | PRE(sys_utimes) |
| 4535 | { |
sewardj | cc3de2d | 2011-08-18 15:08:20 +0000 | [diff] [blame] | 4536 | FUSE_COMPATIBLE_MAY_BLOCK(); |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 4537 | PRINT("sys_utimes ( %#lx(%s), %#lx )", ARG1,(char*)ARG1,ARG2); |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 4538 | PRE_REG_READ2(long, "utimes", char *, filename, struct timeval *, tvp); |
| 4539 | PRE_MEM_RASCIIZ( "utimes(filename)", ARG1 ); |
njn | 72bbd8d | 2009-05-22 08:12:46 +0000 | [diff] [blame] | 4540 | if (ARG2 != 0) { |
| 4541 | PRE_timeval_READ( "utimes(tvp[0])", ARG2 ); |
| 4542 | PRE_timeval_READ( "utimes(tvp[1])", ARG2+sizeof(struct vki_timeval) ); |
| 4543 | } |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 4544 | } |
| 4545 | |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 4546 | PRE(sys_acct) |
| 4547 | { |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 4548 | PRINT("sys_acct ( %#lx(%s) )", ARG1,(char*)ARG1); |
sewardj | 696c551 | 2005-06-08 23:38:32 +0000 | [diff] [blame] | 4549 | PRE_REG_READ1(long, "acct", const char *, filename); |
| 4550 | PRE_MEM_RASCIIZ( "acct(filename)", ARG1 ); |
| 4551 | } |
mueller | d3502b6 | 2003-11-19 00:43:57 +0000 | [diff] [blame] | 4552 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4553 | PRE(sys_pause) |
thughes | 2f8d5f8 | 2004-09-11 14:29:19 +0000 | [diff] [blame] | 4554 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4555 | *flags |= SfMayBlock; |
nethercote | 0df495a | 2004-11-11 16:38:21 +0000 | [diff] [blame] | 4556 | PRINT("sys_pause ( )"); |
| 4557 | PRE_REG_READ0(long, "pause"); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4558 | } |
| 4559 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4560 | PRE(sys_sigaltstack) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4561 | { |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 4562 | PRINT("sigaltstack ( %#lx, %#lx )",ARG1,ARG2); |
nethercote | b77dee6 | 2004-11-16 17:13:24 +0000 | [diff] [blame] | 4563 | PRE_REG_READ2(int, "sigaltstack", |
| 4564 | const vki_stack_t *, ss, vki_stack_t *, oss); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4565 | if (ARG1 != 0) { |
tom | 92098db | 2005-07-18 23:18:10 +0000 | [diff] [blame] | 4566 | const vki_stack_t *ss = (vki_stack_t *)ARG1; |
| 4567 | PRE_MEM_READ( "sigaltstack(ss)", (Addr)&ss->ss_sp, sizeof(ss->ss_sp) ); |
| 4568 | PRE_MEM_READ( "sigaltstack(ss)", (Addr)&ss->ss_flags, sizeof(ss->ss_flags) ); |
| 4569 | PRE_MEM_READ( "sigaltstack(ss)", (Addr)&ss->ss_size, sizeof(ss->ss_size) ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4570 | } |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4571 | if (ARG2 != 0) { |
| 4572 | PRE_MEM_WRITE( "sigaltstack(oss)", ARG2, sizeof(vki_stack_t) ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4573 | } |
| 4574 | |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 4575 | /* Be safe. */ |
| 4576 | if (ARG1 && !ML_(safe_to_deref((void*)ARG1, sizeof(vki_stack_t)))) { |
| 4577 | SET_STATUS_Failure(VKI_EFAULT); |
| 4578 | return; |
| 4579 | } |
| 4580 | if (ARG2 && !ML_(safe_to_deref((void*)ARG2, sizeof(vki_stack_t)))) { |
| 4581 | SET_STATUS_Failure(VKI_EFAULT); |
| 4582 | return; |
| 4583 | } |
| 4584 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4585 | SET_STATUS_from_SysRes( |
| 4586 | VG_(do_sys_sigaltstack) (tid, (vki_stack_t*)ARG1, |
| 4587 | (vki_stack_t*)ARG2) |
| 4588 | ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4589 | } |
nethercote | 85a456f | 2004-11-16 17:31:56 +0000 | [diff] [blame] | 4590 | POST(sys_sigaltstack) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4591 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 4592 | vg_assert(SUCCESS); |
njn | 22cfccb | 2004-11-27 16:10:23 +0000 | [diff] [blame] | 4593 | if (RES == 0 && ARG2 != 0) |
| 4594 | POST_MEM_WRITE( ARG2, sizeof(vki_stack_t)); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 4595 | } |
| 4596 | |
tom | f93bbd9 | 2013-07-17 12:43:30 +0000 | [diff] [blame] | 4597 | PRE(sys_sethostname) |
| 4598 | { |
florian | b26101c | 2015-08-08 21:45:33 +0000 | [diff] [blame] | 4599 | PRINT("sys_sethostname ( %#lx, %ld )", ARG1, SARG2); |
tom | f93bbd9 | 2013-07-17 12:43:30 +0000 | [diff] [blame] | 4600 | PRE_REG_READ2(long, "sethostname", char *, name, int, len); |
| 4601 | PRE_MEM_READ( "sethostname(name)", ARG1, ARG2 ); |
| 4602 | } |
| 4603 | |
sewardj | e6d5e72 | 2005-06-10 10:27:55 +0000 | [diff] [blame] | 4604 | #undef PRE |
| 4605 | #undef POST |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 4606 | |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 4607 | #endif // defined(VGO_linux) || defined(VGO_darwin) || defined(VGO_solaris) |
njn | 8b68b64 | 2009-06-24 00:37:09 +0000 | [diff] [blame] | 4608 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 4609 | /*--------------------------------------------------------------------*/ |
njn | c1b0181 | 2005-06-17 22:19:06 +0000 | [diff] [blame] | 4610 | /*--- end ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 4611 | /*--------------------------------------------------------------------*/ |