Update bug status.
[valgrind.git] / coregrind / m_syswrap / syswrap-generic.c
blob280c48f1b4a7e91bba028b36d2aee6e123d4491a
1 /* -*- mode: C; c-basic-offset: 3; -*- */
3 /*--------------------------------------------------------------------*/
4 /*--- Wrappers for generic Unix system calls ---*/
5 /*--- syswrap-generic.c ---*/
6 /*--------------------------------------------------------------------*/
8 /*
9 This file is part of Valgrind, a dynamic binary instrumentation
10 framework.
12 Copyright (C) 2000-2017 Julian Seward
13 jseward@acm.org
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.
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.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, see <http://www.gnu.org/licenses/>.
28 The GNU General Public License is contained in the file COPYING.
31 #if defined(VGO_linux) || defined(VGO_darwin) || defined(VGO_solaris)
33 #include "pub_core_basics.h"
34 #include "pub_core_vki.h"
35 #include "pub_core_vkiscnums.h"
36 #include "pub_core_threadstate.h"
37 #include "pub_core_debuginfo.h" // VG_(di_notify_*)
38 #include "pub_core_aspacemgr.h"
39 #include "pub_core_transtab.h" // VG_(discard_translations)
40 #include "pub_core_xarray.h"
41 #include "pub_core_clientstate.h" // VG_(brk_base), VG_(brk_limit)
42 #include "pub_core_debuglog.h"
43 #include "pub_core_errormgr.h"
44 #include "pub_core_gdbserver.h" // VG_(gdbserver)
45 #include "pub_core_libcbase.h"
46 #include "pub_core_libcassert.h"
47 #include "pub_core_libcfile.h"
48 #include "pub_core_libcprint.h"
49 #include "pub_core_libcproc.h"
50 #include "pub_core_libcsignal.h"
51 #include "pub_core_machine.h" // VG_(get_SP)
52 #include "pub_core_mallocfree.h"
53 #include "pub_core_options.h"
54 #include "pub_core_scheduler.h"
55 #include "pub_core_signals.h"
56 #include "pub_core_stacktrace.h" // For VG_(get_and_pp_StackTrace)()
57 #include "pub_core_syscall.h"
58 #include "pub_core_syswrap.h"
59 #include "pub_core_tooliface.h"
60 #include "pub_core_ume.h"
61 #include "pub_core_stacks.h"
63 #include "priv_types_n_macros.h"
64 #include "priv_syswrap-generic.h"
66 #include "config.h"
69 void ML_(guess_and_register_stack) (Addr sp, ThreadState* tst)
71 Bool debug = False;
72 NSegment const* seg;
74 /* We don't really know where the client stack is, because its
75 allocated by the client. The best we can do is look at the
76 memory mappings and try to derive some useful information. We
77 assume that sp starts near its highest possible value, and can
78 only go down to the start of the mmaped segment. */
79 seg = VG_(am_find_nsegment)(sp);
80 if (seg
81 && VG_(am_is_valid_for_client)(sp, 1, VKI_PROT_READ | VKI_PROT_WRITE)) {
82 tst->client_stack_highest_byte = (Addr)VG_PGROUNDUP(sp)-1;
83 tst->client_stack_szB = tst->client_stack_highest_byte - seg->start + 1;
85 tst->os_state.stk_id
86 = VG_(register_stack)(seg->start, tst->client_stack_highest_byte);
88 if (debug)
89 VG_(printf)("tid %u: guessed client stack range [%#lx-%#lx]"
90 " as stk_id %lu\n",
91 tst->tid, seg->start, tst->client_stack_highest_byte,
92 tst->os_state.stk_id);
93 } else {
94 VG_(message)(Vg_UserMsg,
95 "!? New thread %u starts with SP(%#lx) unmapped\n",
96 tst->tid, sp);
97 tst->client_stack_highest_byte = 0;
98 tst->client_stack_szB = 0;
102 /* Returns True iff address range is something the client can
103 plausibly mess with: all of it is either already belongs to the
104 client or is free or a reservation. */
106 Bool ML_(valid_client_addr)(Addr start, SizeT size, ThreadId tid,
107 const HChar *syscallname)
109 Bool ret;
111 if (size == 0)
112 return True;
114 ret = VG_(am_is_valid_for_client_or_free_or_resvn)
115 (start,size,VKI_PROT_NONE);
117 if (0)
118 VG_(printf)("%s: test=%#lx-%#lx ret=%d\n",
119 syscallname, start, start+size-1, (Int)ret);
121 if (!ret && syscallname != NULL) {
122 VG_(message)(Vg_UserMsg, "Warning: client syscall %s tried "
123 "to modify addresses %#lx-%#lx\n",
124 syscallname, start, start+size-1);
125 if (VG_(clo_verbosity) > 1) {
126 VG_(get_and_pp_StackTrace)(tid, VG_(clo_backtrace_size));
130 return ret;
134 Bool ML_(client_signal_OK)(Int sigNo)
136 /* signal 0 is OK for kill */
137 Bool ret = sigNo >= 0 && sigNo <= VG_SIGVGRTUSERMAX;
139 //VG_(printf)("client_signal_OK(%d) -> %d\n", sigNo, ret);
141 return ret;
145 /* Handy small function to help stop wrappers from segfaulting when
146 presented with bogus client addresses. Is not used for generating
147 user-visible errors. */
149 Bool ML_(safe_to_deref) ( const void *start, SizeT size )
151 return VG_(am_is_valid_for_client)( (Addr)start, size, VKI_PROT_READ );
155 /* ---------------------------------------------------------------------
156 Doing mmap, mremap
157 ------------------------------------------------------------------ */
159 /* AFAICT from kernel sources (mm/mprotect.c) and general experimentation,
160 munmap, mprotect (and mremap??) work at the page level. So addresses
161 and lengths must be adjusted for this. */
163 /* Mash around start and length so that the area exactly covers
164 an integral number of pages. If we don't do that, memcheck's
165 idea of addressible memory diverges from that of the
166 kernel's, which causes the leak detector to crash. */
167 static
168 void page_align_addr_and_len( Addr* a, SizeT* len)
170 Addr ra;
172 ra = VG_PGROUNDDN(*a);
173 *len = VG_PGROUNDUP(*a + *len) - ra;
174 *a = ra;
177 static void notify_core_of_mmap(Addr a, SizeT len, UInt prot,
178 UInt flags, Int fd, Off64T offset)
180 Bool d;
182 /* 'a' is the return value from a real kernel mmap, hence: */
183 vg_assert(VG_IS_PAGE_ALIGNED(a));
184 /* whereas len is whatever the syscall supplied. So: */
185 len = VG_PGROUNDUP(len);
187 d = VG_(am_notify_client_mmap)( a, len, prot, flags, fd, offset );
189 if (d)
190 VG_(discard_translations)( a, (ULong)len,
191 "notify_core_of_mmap" );
194 static void notify_tool_of_mmap(Addr a, SizeT len, UInt prot, ULong di_handle)
196 Bool rr, ww, xx;
198 /* 'a' is the return value from a real kernel mmap, hence: */
199 vg_assert(VG_IS_PAGE_ALIGNED(a));
200 /* whereas len is whatever the syscall supplied. So: */
201 len = VG_PGROUNDUP(len);
203 rr = toBool(prot & VKI_PROT_READ);
204 ww = toBool(prot & VKI_PROT_WRITE);
205 xx = toBool(prot & VKI_PROT_EXEC);
207 VG_TRACK( new_mem_mmap, a, len, rr, ww, xx, di_handle );
211 /* When a client mmap has been successfully done, this function must
212 be called. It notifies both aspacem and the tool of the new
213 mapping.
215 JRS 2008-Aug-14: But notice this is *very* obscure. The only place
216 it is called from is POST(sys_io_setup). In particular,
217 ML_(generic_PRE_sys_mmap), in m_syswrap, is the "normal case" handler for
218 client mmap. But it doesn't call this function; instead it does the
219 relevant notifications itself. Here, we just pass di_handle=0 to
220 notify_tool_of_mmap as we have no better information. But really this
221 function should be done away with; problem is I don't understand what
222 POST(sys_io_setup) does or how it works.
224 [However, this function is used lots for Darwin, because
225 ML_(generic_PRE_sys_mmap) cannot be used for Darwin.]
227 void
228 ML_(notify_core_and_tool_of_mmap) ( Addr a, SizeT len, UInt prot,
229 UInt flags, Int fd, Off64T offset )
231 // XXX: unlike the other notify_core_and_tool* functions, this one doesn't
232 // do anything with debug info (ie. it doesn't call VG_(di_notify_mmap)).
233 // Should it? --njn
234 notify_core_of_mmap(a, len, prot, flags, fd, offset);
235 notify_tool_of_mmap(a, len, prot, 0/*di_handle*/);
238 void
239 ML_(notify_core_and_tool_of_munmap) ( Addr a, SizeT len )
241 Bool d;
243 page_align_addr_and_len(&a, &len);
244 d = VG_(am_notify_munmap)(a, len);
245 VG_TRACK( die_mem_munmap, a, len );
246 VG_(di_notify_munmap)( a, len );
247 if (d)
248 VG_(discard_translations)( a, (ULong)len,
249 "ML_(notify_core_and_tool_of_munmap)" );
252 void
253 ML_(notify_core_and_tool_of_mprotect) ( Addr a, SizeT len, Int prot )
255 Bool rr = toBool(prot & VKI_PROT_READ);
256 Bool ww = toBool(prot & VKI_PROT_WRITE);
257 Bool xx = toBool(prot & VKI_PROT_EXEC);
258 Bool d;
260 page_align_addr_and_len(&a, &len);
261 d = VG_(am_notify_mprotect)(a, len, prot);
262 VG_TRACK( change_mem_mprotect, a, len, rr, ww, xx );
263 VG_(di_notify_mprotect)( a, len, prot );
264 if (d)
265 VG_(discard_translations)( a, (ULong)len,
266 "ML_(notify_core_and_tool_of_mprotect)" );
271 #if HAVE_MREMAP
272 /* Expand (or shrink) an existing mapping, potentially moving it at
273 the same time (controlled by the MREMAP_MAYMOVE flag). Nightmare.
275 static
276 SysRes do_mremap( Addr old_addr, SizeT old_len,
277 Addr new_addr, SizeT new_len,
278 UWord flags, ThreadId tid )
280 # define MIN_SIZET(_aa,_bb) (_aa) < (_bb) ? (_aa) : (_bb)
282 Bool ok, d;
283 NSegment const* old_seg;
284 Addr advised;
285 Bool f_fixed = toBool(flags & VKI_MREMAP_FIXED);
286 Bool f_maymove = toBool(flags & VKI_MREMAP_MAYMOVE);
288 if (0)
289 VG_(printf)("do_remap (old %#lx %lu) (new %#lx %lu) %s %s\n",
290 old_addr,old_len,new_addr,new_len,
291 flags & VKI_MREMAP_MAYMOVE ? "MAYMOVE" : "",
292 flags & VKI_MREMAP_FIXED ? "FIXED" : "");
293 if (0)
294 VG_(am_show_nsegments)(0, "do_remap: before");
296 if (flags & ~(VKI_MREMAP_FIXED | VKI_MREMAP_MAYMOVE))
297 goto eINVAL;
299 if (!VG_IS_PAGE_ALIGNED(old_addr))
300 goto eINVAL;
302 old_len = VG_PGROUNDUP(old_len);
303 new_len = VG_PGROUNDUP(new_len);
305 if (new_len == 0)
306 goto eINVAL;
308 /* kernel doesn't reject this, but we do. */
309 if (old_len == 0)
310 goto eINVAL;
312 /* reject wraparounds */
313 if (old_addr + old_len < old_addr)
314 goto eINVAL;
315 if (f_fixed == True && new_addr + new_len < new_len)
316 goto eINVAL;
318 /* kernel rejects all fixed, no-move requests (which are
319 meaningless). */
320 if (f_fixed == True && f_maymove == False)
321 goto eINVAL;
323 /* Stay away from non-client areas. */
324 if (!ML_(valid_client_addr)(old_addr, old_len, tid, "mremap(old_addr)"))
325 goto eINVAL;
327 /* In all remaining cases, if the old range does not fall within a
328 single segment, fail. */
329 old_seg = VG_(am_find_nsegment)( old_addr );
330 if (old_addr < old_seg->start || old_addr+old_len-1 > old_seg->end)
331 goto eINVAL;
332 if (old_seg->kind != SkAnonC && old_seg->kind != SkFileC
333 && old_seg->kind != SkShmC)
334 goto eINVAL;
336 vg_assert(old_len > 0);
337 vg_assert(new_len > 0);
338 vg_assert(VG_IS_PAGE_ALIGNED(old_len));
339 vg_assert(VG_IS_PAGE_ALIGNED(new_len));
340 vg_assert(VG_IS_PAGE_ALIGNED(old_addr));
342 /* There are 3 remaining cases:
344 * maymove == False
346 new space has to be at old address, so:
347 - shrink -> unmap end
348 - same size -> do nothing
349 - grow -> if can grow in-place, do so, else fail
351 * maymove == True, fixed == False
353 new space can be anywhere, so:
354 - shrink -> unmap end
355 - same size -> do nothing
356 - grow -> if can grow in-place, do so, else
357 move to anywhere large enough, else fail
359 * maymove == True, fixed == True
361 new space must be at new address, so:
363 - if new address is not page aligned, fail
364 - if new address range overlaps old one, fail
365 - if new address range cannot be allocated, fail
366 - else move to new address range with new size
367 - else fail
370 if (f_maymove == False) {
371 /* new space has to be at old address */
372 if (new_len < old_len)
373 goto shrink_in_place;
374 if (new_len > old_len)
375 goto grow_in_place_or_fail;
376 goto same_in_place;
379 if (f_maymove == True && f_fixed == False) {
380 /* new space can be anywhere */
381 if (new_len < old_len)
382 goto shrink_in_place;
383 if (new_len > old_len)
384 goto grow_in_place_or_move_anywhere_or_fail;
385 goto same_in_place;
388 if (f_maymove == True && f_fixed == True) {
389 /* new space can only be at the new address */
390 if (!VG_IS_PAGE_ALIGNED(new_addr))
391 goto eINVAL;
392 if (new_addr+new_len-1 < old_addr || new_addr > old_addr+old_len-1) {
393 /* no overlap */
394 } else {
395 goto eINVAL;
397 if (new_addr == 0)
398 goto eINVAL;
399 /* VG_(am_get_advisory_client_simple) interprets zero to mean
400 non-fixed, which is not what we want */
401 advised = VG_(am_get_advisory_client_simple)(new_addr, new_len, &ok);
402 if (!ok || advised != new_addr)
403 goto eNOMEM;
404 ok = VG_(am_relocate_nooverlap_client)
405 ( &d, old_addr, old_len, new_addr, new_len );
406 if (ok) {
407 VG_TRACK( copy_mem_remap, old_addr, new_addr,
408 MIN_SIZET(old_len,new_len) );
409 if (new_len > old_len)
410 VG_TRACK( new_mem_mmap, new_addr+old_len, new_len-old_len,
411 old_seg->hasR, old_seg->hasW, old_seg->hasX,
412 0/*di_handle*/ );
413 VG_TRACK(die_mem_munmap, old_addr, old_len);
414 if (d) {
415 VG_(discard_translations)( old_addr, old_len, "do_remap(1)" );
416 VG_(discard_translations)( new_addr, new_len, "do_remap(2)" );
418 return VG_(mk_SysRes_Success)( new_addr );
420 goto eNOMEM;
423 /* end of the 3 cases */
424 /*NOTREACHED*/ vg_assert(0);
426 grow_in_place_or_move_anywhere_or_fail:
428 /* try growing it in-place */
429 Addr needA = old_addr + old_len;
430 SSizeT needL = new_len - old_len;
432 vg_assert(needL > 0);
433 vg_assert(needA > 0);
435 advised = VG_(am_get_advisory_client_simple)( needA, needL, &ok );
436 if (ok) {
437 /* Fixes bug #129866. */
438 ok = VG_(am_covered_by_single_free_segment) ( needA, needL );
440 if (ok && advised == needA) {
441 const NSegment *new_seg = VG_(am_extend_map_client)( old_addr, needL );
442 if (new_seg) {
443 VG_TRACK( new_mem_mmap, needA, needL,
444 new_seg->hasR,
445 new_seg->hasW, new_seg->hasX,
446 0/*di_handle*/ );
447 return VG_(mk_SysRes_Success)( old_addr );
451 /* that failed. Look elsewhere. */
452 advised = VG_(am_get_advisory_client_simple)( 0, new_len, &ok );
453 if (ok) {
454 Bool oldR = old_seg->hasR;
455 Bool oldW = old_seg->hasW;
456 Bool oldX = old_seg->hasX;
457 /* assert new area does not overlap old */
458 vg_assert(advised+new_len-1 < old_addr
459 || advised > old_addr+old_len-1);
460 ok = VG_(am_relocate_nooverlap_client)
461 ( &d, old_addr, old_len, advised, new_len );
462 if (ok) {
463 VG_TRACK( copy_mem_remap, old_addr, advised,
464 MIN_SIZET(old_len,new_len) );
465 if (new_len > old_len)
466 VG_TRACK( new_mem_mmap, advised+old_len, new_len-old_len,
467 oldR, oldW, oldX, 0/*di_handle*/ );
468 VG_TRACK(die_mem_munmap, old_addr, old_len);
469 if (d) {
470 VG_(discard_translations)( old_addr, old_len, "do_remap(4)" );
471 VG_(discard_translations)( advised, new_len, "do_remap(5)" );
473 return VG_(mk_SysRes_Success)( advised );
476 goto eNOMEM;
478 /*NOTREACHED*/ vg_assert(0);
480 grow_in_place_or_fail:
482 Addr needA = old_addr + old_len;
483 SizeT needL = new_len - old_len;
485 vg_assert(needA > 0);
487 advised = VG_(am_get_advisory_client_simple)( needA, needL, &ok );
488 if (ok) {
489 /* Fixes bug #129866. */
490 ok = VG_(am_covered_by_single_free_segment) ( needA, needL );
492 if (!ok || advised != needA)
493 goto eNOMEM;
494 const NSegment *new_seg = VG_(am_extend_map_client)( old_addr, needL );
495 if (!new_seg)
496 goto eNOMEM;
497 VG_TRACK( new_mem_mmap, needA, needL,
498 new_seg->hasR, new_seg->hasW, new_seg->hasX,
499 0/*di_handle*/ );
501 return VG_(mk_SysRes_Success)( old_addr );
503 /*NOTREACHED*/ vg_assert(0);
505 shrink_in_place:
507 SysRes sres = VG_(am_munmap_client)( &d, old_addr+new_len, old_len-new_len );
508 if (sr_isError(sres))
509 return sres;
510 VG_TRACK( die_mem_munmap, old_addr+new_len, old_len-new_len );
511 if (d)
512 VG_(discard_translations)( old_addr+new_len, old_len-new_len,
513 "do_remap(7)" );
514 return VG_(mk_SysRes_Success)( old_addr );
516 /*NOTREACHED*/ vg_assert(0);
518 same_in_place:
519 return VG_(mk_SysRes_Success)( old_addr );
520 /*NOTREACHED*/ vg_assert(0);
522 eINVAL:
523 return VG_(mk_SysRes_Error)( VKI_EINVAL );
524 eNOMEM:
525 return VG_(mk_SysRes_Error)( VKI_ENOMEM );
527 # undef MIN_SIZET
529 #endif /* HAVE_MREMAP */
532 /* ---------------------------------------------------------------------
533 File-descriptor tracking
534 ------------------------------------------------------------------ */
536 /* One of these is allocated for each open file descriptor. */
537 typedef struct OpenFd
539 Int fd; /* The file descriptor */
540 HChar *pathname; /* NULL if not a regular file or unknown */
541 ExeContext *where; /* NULL if inherited from parent */
542 struct OpenFd *next, *prev;
543 } OpenFd;
545 /* List of allocated file descriptors. */
546 static OpenFd *allocated_fds = NULL;
548 /* Count of open file descriptors. */
549 static Int fd_count = 0;
552 /* Note the fact that a file descriptor was just closed. */
553 void ML_(record_fd_close)(Int fd)
555 OpenFd *i = allocated_fds;
557 if (fd >= VG_(fd_hard_limit))
558 return; /* Valgrind internal */
560 while(i) {
561 if(i->fd == fd) {
562 if(i->prev)
563 i->prev->next = i->next;
564 else
565 allocated_fds = i->next;
566 if(i->next)
567 i->next->prev = i->prev;
568 if(i->pathname)
569 VG_(free) (i->pathname);
570 VG_(free) (i);
571 fd_count--;
572 break;
574 i = i->next;
578 /* Note the fact that a file descriptor was just opened. If the
579 tid is -1, this indicates an inherited fd. If the pathname is NULL,
580 this either indicates a non-standard file (i.e. a pipe or socket or
581 some such thing) or that we don't know the filename. If the fd is
582 already open, then we're probably doing a dup2() to an existing fd,
583 so just overwrite the existing one. */
584 void ML_(record_fd_open_with_given_name)(ThreadId tid, Int fd,
585 const HChar *pathname)
587 OpenFd *i;
589 if (fd >= VG_(fd_hard_limit))
590 return; /* Valgrind internal */
592 /* Check to see if this fd is already open. */
593 i = allocated_fds;
594 while (i) {
595 if (i->fd == fd) {
596 if (i->pathname) VG_(free)(i->pathname);
597 break;
599 i = i->next;
602 /* Not already one: allocate an OpenFd */
603 if (i == NULL) {
604 i = VG_(malloc)("syswrap.rfdowgn.1", sizeof(OpenFd));
606 i->prev = NULL;
607 i->next = allocated_fds;
608 if(allocated_fds) allocated_fds->prev = i;
609 allocated_fds = i;
610 fd_count++;
613 i->fd = fd;
614 i->pathname = VG_(strdup)("syswrap.rfdowgn.2", pathname);
615 i->where = (tid == -1) ? NULL : VG_(record_ExeContext)(tid, 0/*first_ip_delta*/);
618 // Record opening of an fd, and find its name.
619 void ML_(record_fd_open_named)(ThreadId tid, Int fd)
621 const HChar* buf;
622 const HChar* name;
623 if (VG_(resolve_filename)(fd, &buf))
624 name = buf;
625 else
626 name = NULL;
628 ML_(record_fd_open_with_given_name)(tid, fd, name);
631 // Record opening of a nameless fd.
632 void ML_(record_fd_open_nameless)(ThreadId tid, Int fd)
634 ML_(record_fd_open_with_given_name)(tid, fd, NULL);
637 // Return if a given file descriptor is already recorded.
638 Bool ML_(fd_recorded)(Int fd)
640 OpenFd *i = allocated_fds;
641 while (i) {
642 if (i->fd == fd)
643 return True;
644 i = i->next;
646 return False;
649 /* Returned string must not be modified nor free'd. */
650 const HChar *ML_(find_fd_recorded_by_fd)(Int fd)
652 OpenFd *i = allocated_fds;
654 while (i) {
655 if (i->fd == fd)
656 return i->pathname;
657 i = i->next;
660 return NULL;
663 static
664 HChar *unix_to_name(struct vki_sockaddr_un *sa, UInt len, HChar *name)
666 if (sa == NULL || len == 0 || sa->sun_path[0] == '\0') {
667 VG_(sprintf)(name, "<unknown>");
668 } else {
669 VG_(sprintf)(name, "%s", sa->sun_path);
672 return name;
675 static
676 HChar *inet_to_name(struct vki_sockaddr_in *sa, UInt len, HChar *name)
678 if (sa == NULL || len == 0) {
679 VG_(sprintf)(name, "<unknown>");
680 } else if (sa->sin_port == 0) {
681 VG_(sprintf)(name, "<unbound>");
682 } else {
683 UInt addr = VG_(ntohl)(sa->sin_addr.s_addr);
684 VG_(sprintf)(name, "%u.%u.%u.%u:%u",
685 (addr>>24) & 0xFF, (addr>>16) & 0xFF,
686 (addr>>8) & 0xFF, addr & 0xFF,
687 VG_(ntohs)(sa->sin_port));
690 return name;
693 static
694 void inet6_format(HChar *s, const UChar ip[16])
696 static const unsigned char V4mappedprefix[12] = {0,0,0,0,0,0,0,0,0,0,0xff,0xff};
698 if (!VG_(memcmp)(ip, V4mappedprefix, 12)) {
699 const struct vki_in_addr *sin_addr =
700 (const struct vki_in_addr *)(ip + 12);
701 UInt addr = VG_(ntohl)(sin_addr->s_addr);
703 VG_(sprintf)(s, "::ffff:%u.%u.%u.%u",
704 (addr>>24) & 0xFF, (addr>>16) & 0xFF,
705 (addr>>8) & 0xFF, addr & 0xFF);
706 } else {
707 Bool compressing = False;
708 Bool compressed = False;
709 Int len = 0;
710 Int i;
712 for (i = 0; i < 16; i += 2) {
713 UInt word = ((UInt)ip[i] << 8) | (UInt)ip[i+1];
714 if (word == 0 && !compressed) {
715 compressing = True;
716 } else {
717 if (compressing) {
718 compressing = False;
719 compressed = True;
720 s[len++] = ':';
722 if (i > 0) {
723 s[len++] = ':';
725 len += VG_(sprintf)(s + len, "%x", word);
729 if (compressing) {
730 s[len++] = ':';
731 s[len++] = ':';
734 s[len++] = 0;
737 return;
740 static
741 HChar *inet6_to_name(struct vki_sockaddr_in6 *sa, UInt len, HChar *name)
743 if (sa == NULL || len == 0) {
744 VG_(sprintf)(name, "<unknown>");
745 } else if (sa->sin6_port == 0) {
746 VG_(sprintf)(name, "<unbound>");
747 } else {
748 HChar addr[100]; // large enough
749 inet6_format(addr, (void *)&(sa->sin6_addr));
750 VG_(sprintf)(name, "[%s]:%u", addr, VG_(ntohs)(sa->sin6_port));
753 return name;
757 * Try get some details about a socket.
759 static void
760 getsockdetails(Int fd)
762 union u {
763 struct vki_sockaddr a;
764 struct vki_sockaddr_in in;
765 struct vki_sockaddr_in6 in6;
766 struct vki_sockaddr_un un;
767 } laddr;
768 Int llen;
770 llen = sizeof(laddr);
771 VG_(memset)(&laddr, 0, llen);
773 if(VG_(getsockname)(fd, (struct vki_sockaddr *)&(laddr.a), &llen) != -1) {
774 switch(laddr.a.sa_family) {
775 case VKI_AF_INET: {
776 HChar lname[32]; // large enough
777 HChar pname[32]; // large enough
778 struct vki_sockaddr_in paddr;
779 Int plen = sizeof(struct vki_sockaddr_in);
781 if (VG_(getpeername)(fd, (struct vki_sockaddr *)&paddr, &plen) != -1) {
782 VG_(message)(Vg_UserMsg, "Open AF_INET socket %d: %s <-> %s\n", fd,
783 inet_to_name(&(laddr.in), llen, lname),
784 inet_to_name(&paddr, plen, pname));
785 } else {
786 VG_(message)(Vg_UserMsg, "Open AF_INET socket %d: %s <-> unbound\n",
787 fd, inet_to_name(&(laddr.in), llen, lname));
789 return;
791 case VKI_AF_INET6: {
792 HChar lname[128]; // large enough
793 HChar pname[128]; // large enough
794 struct vki_sockaddr_in6 paddr;
795 Int plen = sizeof(struct vki_sockaddr_in6);
797 if (VG_(getpeername)(fd, (struct vki_sockaddr *)&paddr, &plen) != -1) {
798 VG_(message)(Vg_UserMsg, "Open AF_INET6 socket %d: %s <-> %s\n", fd,
799 inet6_to_name(&(laddr.in6), llen, lname),
800 inet6_to_name(&paddr, plen, pname));
801 } else {
802 VG_(message)(Vg_UserMsg, "Open AF_INET6 socket %d: %s <-> unbound\n",
803 fd, inet6_to_name(&(laddr.in6), llen, lname));
805 return;
807 case VKI_AF_UNIX: {
808 static char lname[256];
809 VG_(message)(Vg_UserMsg, "Open AF_UNIX socket %d: %s\n", fd,
810 unix_to_name(&(laddr.un), llen, lname));
811 return;
813 default:
814 VG_(message)(Vg_UserMsg, "Open pf-%d socket %d:\n",
815 laddr.a.sa_family, fd);
816 return;
820 VG_(message)(Vg_UserMsg, "Open socket %d:\n", fd);
824 /* Dump out a summary, and a more detailed list, of open file descriptors. */
825 void VG_(show_open_fds) (const HChar* when)
827 OpenFd *i = allocated_fds;
829 VG_(message)(Vg_UserMsg, "FILE DESCRIPTORS: %d open %s.\n", fd_count, when);
831 while (i) {
832 if (i->pathname) {
833 VG_(message)(Vg_UserMsg, "Open file descriptor %d: %s\n", i->fd,
834 i->pathname);
835 } else {
836 Int val;
837 Int len = sizeof(val);
839 if (VG_(getsockopt)(i->fd, VKI_SOL_SOCKET, VKI_SO_TYPE, &val, &len)
840 == -1) {
841 VG_(message)(Vg_UserMsg, "Open file descriptor %d:\n", i->fd);
842 } else {
843 getsockdetails(i->fd);
847 if(i->where) {
848 VG_(pp_ExeContext)(i->where);
849 VG_(message)(Vg_UserMsg, "\n");
850 } else {
851 VG_(message)(Vg_UserMsg, " <inherited from parent>\n");
852 VG_(message)(Vg_UserMsg, "\n");
855 i = i->next;
858 VG_(message)(Vg_UserMsg, "\n");
861 /* If /proc/self/fd doesn't exist (e.g. you've got a Linux kernel that doesn't
862 have /proc support compiled in, or a non-Linux kernel), then we need to
863 find out what file descriptors we inherited from our parent process the
864 hard way - by checking each fd in turn. */
865 static
866 void init_preopened_fds_without_proc_self_fd(void)
868 struct vki_rlimit lim;
869 UInt count;
870 Int i;
872 if (VG_(getrlimit) (VKI_RLIMIT_NOFILE, &lim) == -1) {
873 /* Hmm. getrlimit() failed. Now we're screwed, so just choose
874 an arbitrarily high number. 1024 happens to be the limit in
875 the 2.4 Linux kernels. */
876 count = 1024;
877 } else {
878 count = lim.rlim_cur;
881 for (i = 0; i < count; i++)
882 if (VG_(fcntl)(i, VKI_F_GETFL, 0) != -1)
883 ML_(record_fd_open_named)(-1, i);
886 /* Initialize the list of open file descriptors with the file descriptors
887 we inherited from out parent process. */
889 void VG_(init_preopened_fds)(void)
891 // DDD: should probably use HAVE_PROC here or similar, instead.
892 #if defined(VGO_linux)
893 Int ret;
894 struct vki_dirent64 d;
895 SysRes f;
897 f = VG_(open)("/proc/self/fd", VKI_O_RDONLY, 0);
898 if (sr_isError(f)) {
899 init_preopened_fds_without_proc_self_fd();
900 return;
903 while ((ret = VG_(getdents64)(sr_Res(f), &d, sizeof(d))) != 0) {
904 if (ret == -1)
905 goto out;
907 if (VG_(strcmp)(d.d_name, ".") && VG_(strcmp)(d.d_name, "..")) {
908 HChar* s;
909 Int fno = VG_(strtoll10)(d.d_name, &s);
910 if (*s == '\0') {
911 if (fno != sr_Res(f))
912 if (VG_(clo_track_fds))
913 ML_(record_fd_open_named)(-1, fno);
914 } else {
915 VG_(message)(Vg_DebugMsg,
916 "Warning: invalid file name in /proc/self/fd: %s\n",
917 d.d_name);
921 VG_(lseek)(sr_Res(f), d.d_off, VKI_SEEK_SET);
924 out:
925 VG_(close)(sr_Res(f));
927 #elif defined(VGO_darwin)
928 init_preopened_fds_without_proc_self_fd();
930 #elif defined(VGO_solaris)
931 Int ret;
932 Char buf[VKI_MAXGETDENTS_SIZE];
933 SysRes f;
935 f = VG_(open)("/proc/self/fd", VKI_O_RDONLY, 0);
936 if (sr_isError(f)) {
937 init_preopened_fds_without_proc_self_fd();
938 return;
941 while ((ret = VG_(getdents64)(sr_Res(f), (struct vki_dirent64 *) buf,
942 sizeof(buf))) > 0) {
943 Int i = 0;
944 while (i < ret) {
945 /* Proceed one entry. */
946 struct vki_dirent64 *d = (struct vki_dirent64 *) (buf + i);
947 if (VG_(strcmp)(d->d_name, ".") && VG_(strcmp)(d->d_name, "..")) {
948 HChar *s;
949 Int fno = VG_(strtoll10)(d->d_name, &s);
950 if (*s == '\0') {
951 if (fno != sr_Res(f))
952 if (VG_(clo_track_fds))
953 ML_(record_fd_open_named)(-1, fno);
954 } else {
955 VG_(message)(Vg_DebugMsg,
956 "Warning: invalid file name in /proc/self/fd: %s\n",
957 d->d_name);
961 /* Move on the next entry. */
962 i += d->d_reclen;
966 VG_(close)(sr_Res(f));
968 #else
969 # error Unknown OS
970 #endif
973 static
974 void pre_mem_read_sendmsg ( ThreadId tid, Bool read,
975 const HChar *msg, Addr base, SizeT size )
977 HChar outmsg[VG_(strlen)(msg) + 10]; // large enough
978 VG_(sprintf)(outmsg, "sendmsg%s", msg);
979 PRE_MEM_READ( outmsg, base, size );
982 static
983 void pre_mem_write_recvmsg ( ThreadId tid, Bool read,
984 const HChar *msg, Addr base, SizeT size )
986 HChar outmsg[VG_(strlen)(msg) + 10]; // large enough
987 VG_(sprintf)(outmsg, "recvmsg%s", msg);
988 if ( read )
989 PRE_MEM_READ( outmsg, base, size );
990 else
991 PRE_MEM_WRITE( outmsg, base, size );
994 static
995 void post_mem_write_recvmsg ( ThreadId tid, Bool read,
996 const HChar *fieldName, Addr base, SizeT size )
998 if ( !read )
999 POST_MEM_WRITE( base, size );
1002 static
1003 void msghdr_foreachfield (
1004 ThreadId tid,
1005 const HChar *name,
1006 struct vki_msghdr *msg,
1007 UInt length,
1008 void (*foreach_func)( ThreadId, Bool, const HChar *, Addr, SizeT ),
1009 Bool rekv /* "recv" apparently shadows some header decl on OSX108 */
1012 HChar fieldName[VG_(strlen)(name) + 32]; // large enough.
1013 Addr a;
1014 SizeT s;
1016 if ( !msg )
1017 return;
1019 VG_(sprintf) ( fieldName, "(%s)", name );
1021 /* FIELDPAIR helps the compiler do one call to foreach_func
1022 for consecutive (no holes) fields. */
1023 #define FIELDPAIR(f1,f2) \
1024 if (offsetof(struct vki_msghdr, f1) + sizeof(msg->f1) \
1025 == offsetof(struct vki_msghdr, f2)) \
1026 s += sizeof(msg->f2); \
1027 else { \
1028 foreach_func (tid, True, fieldName, a, s); \
1029 a = (Addr)&msg->f2; \
1030 s = sizeof(msg->f2); \
1033 a = (Addr)&msg->msg_name;
1034 s = sizeof(msg->msg_name);
1035 FIELDPAIR(msg_name, msg_namelen);
1036 FIELDPAIR(msg_namelen, msg_iov);
1037 FIELDPAIR(msg_iov, msg_iovlen);
1038 FIELDPAIR(msg_iovlen, msg_control);
1039 FIELDPAIR(msg_control, msg_controllen);
1040 foreach_func ( tid, True, fieldName, a, s);
1041 #undef FIELDPAIR
1043 /* msg_flags is completely ignored for send_mesg, recv_mesg doesn't read
1044 the field, but does write to it. */
1045 if ( rekv )
1046 foreach_func ( tid, False, fieldName, (Addr)&msg->msg_flags, sizeof( msg->msg_flags ) );
1048 if ( ML_(safe_to_deref)(&msg->msg_name, sizeof (void *))
1049 && msg->msg_name ) {
1050 VG_(sprintf) ( fieldName, "(%s.msg_name)", name );
1051 foreach_func ( tid, False, fieldName,
1052 (Addr)msg->msg_name, msg->msg_namelen );
1055 if ( ML_(safe_to_deref)(&msg->msg_iov, sizeof (void *))
1056 && msg->msg_iov ) {
1057 struct vki_iovec *iov = msg->msg_iov;
1058 UInt i;
1060 if (ML_(safe_to_deref)(&msg->msg_iovlen, sizeof (UInt))) {
1061 VG_(sprintf) ( fieldName, "(%s.msg_iov)", name );
1062 foreach_func ( tid, True, fieldName, (Addr)iov,
1063 msg->msg_iovlen * sizeof( struct vki_iovec ) );
1065 for ( i = 0; i < msg->msg_iovlen && length > 0; ++i, ++iov ) {
1066 if (ML_(safe_to_deref)(&iov->iov_len, sizeof (UInt))) {
1067 UInt iov_len = iov->iov_len <= length ? iov->iov_len : length;
1068 VG_(sprintf) ( fieldName, "(%s.msg_iov[%u])", name, i );
1069 foreach_func ( tid, False, fieldName,
1070 (Addr)iov->iov_base, iov_len );
1071 length = length - iov_len;
1077 if ( ML_(safe_to_deref) (&msg->msg_control, sizeof (void *))
1078 && msg->msg_control ) {
1079 VG_(sprintf) ( fieldName, "(%s.msg_control)", name );
1080 foreach_func ( tid, False, fieldName,
1081 (Addr)msg->msg_control, msg->msg_controllen );
1086 static void check_cmsg_for_fds(ThreadId tid, struct vki_msghdr *msg)
1088 struct vki_cmsghdr *cm = VKI_CMSG_FIRSTHDR(msg);
1090 while (cm) {
1091 if (cm->cmsg_level == VKI_SOL_SOCKET
1092 && cm->cmsg_type == VKI_SCM_RIGHTS ) {
1093 Int *fds = (Int *) VKI_CMSG_DATA(cm);
1094 Int fdc = (cm->cmsg_len - VKI_CMSG_ALIGN(sizeof(struct vki_cmsghdr)))
1095 / sizeof(int);
1096 Int i;
1098 for (i = 0; i < fdc; i++)
1099 if(VG_(clo_track_fds))
1100 // XXX: must we check the range on these fds with
1101 // ML_(fd_allowed)()?
1102 ML_(record_fd_open_named)(tid, fds[i]);
1105 cm = VKI_CMSG_NXTHDR(msg, cm);
1109 /* GrP kernel ignores sa_len (at least on Darwin); this checks the rest */
1110 static
1111 void pre_mem_read_sockaddr ( ThreadId tid,
1112 const HChar *description,
1113 struct vki_sockaddr *sa, UInt salen )
1115 HChar outmsg[VG_(strlen)( description ) + 30]; // large enough
1116 struct vki_sockaddr_un* saun = (struct vki_sockaddr_un *)sa;
1117 struct vki_sockaddr_in* sin = (struct vki_sockaddr_in *)sa;
1118 struct vki_sockaddr_in6* sin6 = (struct vki_sockaddr_in6 *)sa;
1119 # ifdef VKI_AF_BLUETOOTH
1120 struct vki_sockaddr_rc* rc = (struct vki_sockaddr_rc *)sa;
1121 # endif
1122 # ifdef VKI_AF_NETLINK
1123 struct vki_sockaddr_nl* nl = (struct vki_sockaddr_nl *)sa;
1124 # endif
1126 /* NULL/zero-length sockaddrs are legal */
1127 if ( sa == NULL || salen == 0 ) return;
1129 VG_(sprintf) ( outmsg, description, "sa_family" );
1130 PRE_MEM_READ( outmsg, (Addr) &sa->sa_family, sizeof(vki_sa_family_t));
1132 /* Don't do any extra checking if we cannot determine the sa_family. */
1133 if (! ML_(safe_to_deref) (&sa->sa_family, sizeof(vki_sa_family_t)))
1134 return;
1136 switch (sa->sa_family) {
1138 case VKI_AF_UNIX:
1139 if (ML_(safe_to_deref) (&saun->sun_path, sizeof (Addr))) {
1140 VG_(sprintf) ( outmsg, description, "sun_path" );
1141 PRE_MEM_RASCIIZ( outmsg, (Addr) saun->sun_path );
1142 // GrP fixme max of sun_len-2? what about nul char?
1144 break;
1146 case VKI_AF_INET:
1147 VG_(sprintf) ( outmsg, description, "sin_port" );
1148 PRE_MEM_READ( outmsg, (Addr) &sin->sin_port, sizeof (sin->sin_port) );
1149 VG_(sprintf) ( outmsg, description, "sin_addr" );
1150 PRE_MEM_READ( outmsg, (Addr) &sin->sin_addr, sizeof (sin->sin_addr) );
1151 break;
1153 case VKI_AF_INET6:
1154 VG_(sprintf) ( outmsg, description, "sin6_port" );
1155 PRE_MEM_READ( outmsg,
1156 (Addr) &sin6->sin6_port, sizeof (sin6->sin6_port) );
1157 VG_(sprintf) ( outmsg, description, "sin6_flowinfo" );
1158 PRE_MEM_READ( outmsg,
1159 (Addr) &sin6->sin6_flowinfo, sizeof (sin6->sin6_flowinfo) );
1160 VG_(sprintf) ( outmsg, description, "sin6_addr" );
1161 PRE_MEM_READ( outmsg,
1162 (Addr) &sin6->sin6_addr, sizeof (sin6->sin6_addr) );
1163 VG_(sprintf) ( outmsg, description, "sin6_scope_id" );
1164 PRE_MEM_READ( outmsg,
1165 (Addr) &sin6->sin6_scope_id, sizeof (sin6->sin6_scope_id) );
1166 break;
1168 # ifdef VKI_AF_BLUETOOTH
1169 case VKI_AF_BLUETOOTH:
1170 VG_(sprintf) ( outmsg, description, "rc_bdaddr" );
1171 PRE_MEM_READ( outmsg, (Addr) &rc->rc_bdaddr, sizeof (rc->rc_bdaddr) );
1172 VG_(sprintf) ( outmsg, description, "rc_channel" );
1173 PRE_MEM_READ( outmsg, (Addr) &rc->rc_channel, sizeof (rc->rc_channel) );
1174 break;
1175 # endif
1177 # ifdef VKI_AF_NETLINK
1178 case VKI_AF_NETLINK:
1179 VG_(sprintf)(outmsg, description, "nl_pid");
1180 PRE_MEM_READ(outmsg, (Addr)&nl->nl_pid, sizeof(nl->nl_pid));
1181 VG_(sprintf)(outmsg, description, "nl_groups");
1182 PRE_MEM_READ(outmsg, (Addr)&nl->nl_groups, sizeof(nl->nl_groups));
1183 break;
1184 # endif
1186 # ifdef VKI_AF_UNSPEC
1187 case VKI_AF_UNSPEC:
1188 break;
1189 # endif
1191 default:
1192 /* No specific information about this address family.
1193 Let's just check the full data following the family.
1194 Note that this can give false positive if this (unknown)
1195 struct sockaddr_???? has padding bytes between its elements. */
1196 VG_(sprintf) ( outmsg, description, "sa_data" );
1197 PRE_MEM_READ( outmsg, (Addr)&sa->sa_family + sizeof(sa->sa_family),
1198 salen - sizeof(sa->sa_family));
1199 break;
1203 /* Dereference a pointer to a UInt. */
1204 static UInt deref_UInt ( ThreadId tid, Addr a, const HChar* s )
1206 UInt* a_p = (UInt*)a;
1207 PRE_MEM_READ( s, (Addr)a_p, sizeof(UInt) );
1208 if (a_p == NULL || ! ML_(safe_to_deref) (a_p, sizeof(UInt)))
1209 return 0;
1210 else
1211 return *a_p;
1214 void ML_(buf_and_len_pre_check) ( ThreadId tid, Addr buf_p, Addr buflen_p,
1215 const HChar* buf_s, const HChar* buflen_s )
1217 if (VG_(tdict).track_pre_mem_write) {
1218 UInt buflen_in = deref_UInt( tid, buflen_p, buflen_s);
1219 if (buflen_in > 0) {
1220 VG_(tdict).track_pre_mem_write(
1221 Vg_CoreSysCall, tid, buf_s, buf_p, buflen_in );
1226 void ML_(buf_and_len_post_check) ( ThreadId tid, SysRes res,
1227 Addr buf_p, Addr buflen_p, const HChar* s )
1229 if (!sr_isError(res) && VG_(tdict).track_post_mem_write) {
1230 UInt buflen_out = deref_UInt( tid, buflen_p, s);
1231 if (buflen_out > 0 && buf_p != (Addr)NULL) {
1232 VG_(tdict).track_post_mem_write( Vg_CoreSysCall, tid, buf_p, buflen_out );
1237 /* ---------------------------------------------------------------------
1238 Data seg end, for brk()
1239 ------------------------------------------------------------------ */
1241 /* +--------+------------+
1242 | anon | resvn |
1243 +--------+------------+
1245 ^ ^ ^
1246 | | boundary is page aligned
1247 | VG_(brk_limit) -- no alignment constraint
1248 VG_(brk_base) -- page aligned -- does not move
1250 Both the anon part and the reservation part are always at least
1251 one page.
1254 /* Set the new data segment end to NEWBRK. If this succeeds, return
1255 NEWBRK, else return the current data segment end. */
1257 static Addr do_brk ( Addr newbrk, ThreadId tid )
1259 NSegment const* aseg;
1260 Addr newbrkP;
1261 SizeT delta;
1262 Bool debug = False;
1264 if (debug)
1265 VG_(printf)("\ndo_brk: brk_base=%#lx brk_limit=%#lx newbrk=%#lx\n",
1266 VG_(brk_base), VG_(brk_limit), newbrk);
1268 if (0) VG_(am_show_nsegments)(0, "in_brk");
1270 if (newbrk < VG_(brk_base))
1271 /* Clearly impossible. */
1272 goto bad;
1274 if (newbrk < VG_(brk_limit)) {
1275 /* shrinking the data segment. Be lazy and don't munmap the
1276 excess area. */
1277 NSegment const * seg = VG_(am_find_nsegment)(newbrk);
1278 vg_assert(seg);
1280 if (seg->hasT)
1281 VG_(discard_translations)( newbrk, VG_(brk_limit) - newbrk,
1282 "do_brk(shrink)" );
1283 /* Since we're being lazy and not unmapping pages, we have to
1284 zero out the area, so that if the area later comes back into
1285 circulation, it will be filled with zeroes, as if it really
1286 had been unmapped and later remapped. Be a bit paranoid and
1287 try hard to ensure we're not going to segfault by doing the
1288 write - check both ends of the range are in the same segment
1289 and that segment is writable. */
1290 NSegment const * seg2;
1292 seg2 = VG_(am_find_nsegment)( VG_(brk_limit) - 1 );
1293 vg_assert(seg2);
1295 if (seg == seg2 && seg->hasW)
1296 VG_(memset)( (void*)newbrk, 0, VG_(brk_limit) - newbrk );
1298 VG_(brk_limit) = newbrk;
1299 return newbrk;
1302 /* otherwise we're expanding the brk segment. */
1303 if (VG_(brk_limit) > VG_(brk_base))
1304 aseg = VG_(am_find_nsegment)( VG_(brk_limit)-1 );
1305 else
1306 aseg = VG_(am_find_nsegment)( VG_(brk_limit) );
1308 /* These should be assured by setup_client_dataseg in m_main. */
1309 vg_assert(aseg);
1310 vg_assert(aseg->kind == SkAnonC);
1312 if (newbrk <= aseg->end + 1) {
1313 /* still fits within the anon segment. */
1314 VG_(brk_limit) = newbrk;
1315 return newbrk;
1318 newbrkP = VG_PGROUNDUP(newbrk);
1319 delta = newbrkP - (aseg->end + 1);
1320 vg_assert(delta > 0);
1321 vg_assert(VG_IS_PAGE_ALIGNED(delta));
1323 Bool overflow = False;
1324 if (! VG_(am_extend_into_adjacent_reservation_client)( aseg->start, delta,
1325 &overflow)) {
1326 if (overflow) {
1327 static Bool alreadyComplained = False;
1328 if (!alreadyComplained) {
1329 alreadyComplained = True;
1330 if (VG_(clo_verbosity) > 0) {
1331 VG_(umsg)("brk segment overflow in thread #%u: "
1332 "can't grow to %#lx\n",
1333 tid, newbrkP);
1334 VG_(umsg)("(see section Limitations in user manual)\n");
1335 VG_(umsg)("NOTE: further instances of this message "
1336 "will not be shown\n");
1339 } else {
1340 if (VG_(clo_verbosity) > 0) {
1341 VG_(umsg)("Cannot map memory to grow brk segment in thread #%u "
1342 "to %#lx\n", tid, newbrkP);
1343 VG_(umsg)("(see section Limitations in user manual)\n");
1346 goto bad;
1349 VG_(brk_limit) = newbrk;
1350 return newbrk;
1352 bad:
1353 return VG_(brk_limit);
1357 /* ---------------------------------------------------------------------
1358 Vet file descriptors for sanity
1359 ------------------------------------------------------------------ */
1361 > - what does the "Bool soft" parameter mean?
1363 (Tom Hughes, 3 Oct 05):
1365 Whether or not to consider a file descriptor invalid if it is above
1366 the current soft limit.
1368 Basically if we are testing whether a newly created file descriptor is
1369 valid (in a post handler) then we set soft to true, and if we are
1370 testing whether a file descriptor that is about to be used (in a pre
1371 handler) is valid [viz, an already-existing fd] then we set it to false.
1373 The point is that if the (virtual) soft limit is lowered then any
1374 existing descriptors can still be read/written/closed etc (so long as
1375 they are below the valgrind reserved descriptors) but no new
1376 descriptors can be created above the new soft limit.
1378 (jrs 4 Oct 05: in which case, I've renamed it "isNewFd")
1381 /* Return true if we're allowed to use or create this fd */
1382 Bool ML_(fd_allowed)(Int fd, const HChar *syscallname, ThreadId tid,
1383 Bool isNewFd)
1385 Bool allowed = True;
1387 /* hard limits always apply */
1388 if (fd < 0 || fd >= VG_(fd_hard_limit))
1389 allowed = False;
1391 /* hijacking the output fds is never allowed */
1392 if (fd == VG_(log_output_sink).fd || fd == VG_(xml_output_sink).fd)
1393 allowed = False;
1395 /* if creating a new fd (rather than using an existing one), the
1396 soft limit must also be observed */
1397 if (isNewFd && fd >= VG_(fd_soft_limit))
1398 allowed = False;
1400 /* this looks like it ought to be included, but causes problems: */
1402 if (fd == 2 && VG_(debugLog_getLevel)() > 0)
1403 allowed = False;
1405 /* The difficulty is as follows: consider a program P which expects
1406 to be able to mess with (redirect) its own stderr (fd 2).
1407 Usually to deal with P we would issue command line flags to send
1408 logging somewhere other than stderr, so as not to disrupt P.
1409 The problem is that -d unilaterally hijacks stderr with no
1410 consultation with P. And so, if this check is enabled, P will
1411 work OK normally but fail if -d is issued.
1413 Basically -d is a hack and you take your chances when using it.
1414 It's very useful for low level debugging -- particularly at
1415 startup -- and having its presence change the behaviour of the
1416 client is exactly what we don't want. */
1418 /* croak? */
1419 if ((!allowed) && VG_(showing_core_errors)() ) {
1420 VG_(message)(Vg_UserMsg,
1421 "Warning: invalid file descriptor %d in syscall %s()\n",
1422 fd, syscallname);
1423 if (fd == VG_(log_output_sink).fd && VG_(log_output_sink).fd >= 0)
1424 VG_(message)(Vg_UserMsg,
1425 " Use --log-fd=<number> to select an alternative log fd.\n");
1426 if (fd == VG_(xml_output_sink).fd && VG_(xml_output_sink).fd >= 0)
1427 VG_(message)(Vg_UserMsg,
1428 " Use --xml-fd=<number> to select an alternative XML "
1429 "output fd.\n");
1430 // DDD: consider always printing this stack trace, it's useful.
1431 // Also consider also making this a proper core error, ie.
1432 // suppressible and all that.
1433 if (VG_(clo_verbosity) > 1) {
1434 VG_(get_and_pp_StackTrace)(tid, VG_(clo_backtrace_size));
1438 return allowed;
1442 /* ---------------------------------------------------------------------
1443 Deal with a bunch of socket-related syscalls
1444 ------------------------------------------------------------------ */
1446 /* ------ */
1448 void
1449 ML_(generic_PRE_sys_socketpair) ( ThreadId tid,
1450 UWord arg0, UWord arg1,
1451 UWord arg2, UWord arg3 )
1453 /* int socketpair(int d, int type, int protocol, int sv[2]); */
1454 PRE_MEM_WRITE( "socketcall.socketpair(sv)",
1455 arg3, 2*sizeof(int) );
1458 SysRes
1459 ML_(generic_POST_sys_socketpair) ( ThreadId tid,
1460 SysRes res,
1461 UWord arg0, UWord arg1,
1462 UWord arg2, UWord arg3 )
1464 SysRes r = res;
1465 Int fd1 = ((Int*)arg3)[0];
1466 Int fd2 = ((Int*)arg3)[1];
1467 vg_assert(!sr_isError(res)); /* guaranteed by caller */
1468 POST_MEM_WRITE( arg3, 2*sizeof(int) );
1469 if (!ML_(fd_allowed)(fd1, "socketcall.socketpair", tid, True) ||
1470 !ML_(fd_allowed)(fd2, "socketcall.socketpair", tid, True)) {
1471 VG_(close)(fd1);
1472 VG_(close)(fd2);
1473 r = VG_(mk_SysRes_Error)( VKI_EMFILE );
1474 } else {
1475 POST_MEM_WRITE( arg3, 2*sizeof(int) );
1476 if (VG_(clo_track_fds)) {
1477 ML_(record_fd_open_nameless)(tid, fd1);
1478 ML_(record_fd_open_nameless)(tid, fd2);
1481 return r;
1484 /* ------ */
1486 SysRes
1487 ML_(generic_POST_sys_socket) ( ThreadId tid, SysRes res )
1489 SysRes r = res;
1490 vg_assert(!sr_isError(res)); /* guaranteed by caller */
1491 if (!ML_(fd_allowed)(sr_Res(res), "socket", tid, True)) {
1492 VG_(close)(sr_Res(res));
1493 r = VG_(mk_SysRes_Error)( VKI_EMFILE );
1494 } else {
1495 if (VG_(clo_track_fds))
1496 ML_(record_fd_open_nameless)(tid, sr_Res(res));
1498 return r;
1501 /* ------ */
1503 void
1504 ML_(generic_PRE_sys_bind) ( ThreadId tid,
1505 UWord arg0, UWord arg1, UWord arg2 )
1507 /* int bind(int sockfd, struct sockaddr *my_addr,
1508 int addrlen); */
1509 pre_mem_read_sockaddr(
1510 tid, "socketcall.bind(my_addr.%s)",
1511 (struct vki_sockaddr *) arg1, arg2
1515 /* ------ */
1517 void
1518 ML_(generic_PRE_sys_accept) ( ThreadId tid,
1519 UWord arg0, UWord arg1, UWord arg2 )
1521 /* int accept(int s, struct sockaddr *addr, int *addrlen); */
1522 Addr addr_p = arg1;
1523 Addr addrlen_p = arg2;
1524 if (addr_p != (Addr)NULL)
1525 ML_(buf_and_len_pre_check) ( tid, addr_p, addrlen_p,
1526 "socketcall.accept(addr)",
1527 "socketcall.accept(addrlen_in)" );
1530 SysRes
1531 ML_(generic_POST_sys_accept) ( ThreadId tid,
1532 SysRes res,
1533 UWord arg0, UWord arg1, UWord arg2 )
1535 SysRes r = res;
1536 vg_assert(!sr_isError(res)); /* guaranteed by caller */
1537 if (!ML_(fd_allowed)(sr_Res(res), "accept", tid, True)) {
1538 VG_(close)(sr_Res(res));
1539 r = VG_(mk_SysRes_Error)( VKI_EMFILE );
1540 } else {
1541 Addr addr_p = arg1;
1542 Addr addrlen_p = arg2;
1543 if (addr_p != (Addr)NULL)
1544 ML_(buf_and_len_post_check) ( tid, res, addr_p, addrlen_p,
1545 "socketcall.accept(addrlen_out)" );
1546 if (VG_(clo_track_fds))
1547 ML_(record_fd_open_nameless)(tid, sr_Res(res));
1549 return r;
1552 /* ------ */
1554 void
1555 ML_(generic_PRE_sys_sendto) ( ThreadId tid,
1556 UWord arg0, UWord arg1, UWord arg2,
1557 UWord arg3, UWord arg4, UWord arg5 )
1559 /* int sendto(int s, const void *msg, int len,
1560 unsigned int flags,
1561 const struct sockaddr *to, int tolen); */
1562 PRE_MEM_READ( "socketcall.sendto(msg)",
1563 arg1, /* msg */
1564 arg2 /* len */ );
1565 pre_mem_read_sockaddr(
1566 tid, "socketcall.sendto(to.%s)",
1567 (struct vki_sockaddr *) arg4, arg5
1571 /* ------ */
1573 void
1574 ML_(generic_PRE_sys_send) ( ThreadId tid,
1575 UWord arg0, UWord arg1, UWord arg2 )
1577 /* int send(int s, const void *msg, size_t len, int flags); */
1578 PRE_MEM_READ( "socketcall.send(msg)",
1579 arg1, /* msg */
1580 arg2 /* len */ );
1584 /* ------ */
1586 void
1587 ML_(generic_PRE_sys_recvfrom) ( ThreadId tid,
1588 UWord arg0, UWord arg1, UWord arg2,
1589 UWord arg3, UWord arg4, UWord arg5 )
1591 /* int recvfrom(int s, void *buf, int len, unsigned int flags,
1592 struct sockaddr *from, int *fromlen); */
1593 Addr buf_p = arg1;
1594 Int len = arg2;
1595 Addr from_p = arg4;
1596 Addr fromlen_p = arg5;
1597 PRE_MEM_WRITE( "socketcall.recvfrom(buf)", buf_p, len );
1598 if (from_p != (Addr)NULL)
1599 ML_(buf_and_len_pre_check) ( tid, from_p, fromlen_p,
1600 "socketcall.recvfrom(from)",
1601 "socketcall.recvfrom(fromlen_in)" );
1604 void
1605 ML_(generic_POST_sys_recvfrom) ( ThreadId tid,
1606 SysRes res,
1607 UWord arg0, UWord arg1, UWord arg2,
1608 UWord arg3, UWord arg4, UWord arg5 )
1610 Addr buf_p = arg1;
1611 Int len = arg2;
1612 Addr from_p = arg4;
1613 Addr fromlen_p = arg5;
1615 vg_assert(!sr_isError(res)); /* guaranteed by caller */
1616 if (from_p != (Addr)NULL)
1617 ML_(buf_and_len_post_check) ( tid, res, from_p, fromlen_p,
1618 "socketcall.recvfrom(fromlen_out)" );
1619 POST_MEM_WRITE( buf_p, len );
1622 /* ------ */
1624 void
1625 ML_(generic_PRE_sys_recv) ( ThreadId tid,
1626 UWord arg0, UWord arg1, UWord arg2 )
1628 /* int recv(int s, void *buf, int len, unsigned int flags); */
1629 /* man 2 recv says:
1630 The recv call is normally used only on a connected socket
1631 (see connect(2)) and is identical to recvfrom with a NULL
1632 from parameter.
1634 PRE_MEM_WRITE( "socketcall.recv(buf)",
1635 arg1, /* buf */
1636 arg2 /* len */ );
1639 void
1640 ML_(generic_POST_sys_recv) ( ThreadId tid,
1641 UWord res,
1642 UWord arg0, UWord arg1, UWord arg2 )
1644 if (res >= 0 && arg1 != 0) {
1645 POST_MEM_WRITE( arg1, /* buf */
1646 arg2 /* len */ );
1650 /* ------ */
1652 void
1653 ML_(generic_PRE_sys_connect) ( ThreadId tid,
1654 UWord arg0, UWord arg1, UWord arg2 )
1656 /* int connect(int sockfd,
1657 struct sockaddr *serv_addr, int addrlen ); */
1658 pre_mem_read_sockaddr( tid,
1659 "socketcall.connect(serv_addr.%s)",
1660 (struct vki_sockaddr *) arg1, arg2);
1663 /* ------ */
1665 void
1666 ML_(generic_PRE_sys_setsockopt) ( ThreadId tid,
1667 UWord arg0, UWord arg1, UWord arg2,
1668 UWord arg3, UWord arg4 )
1670 /* int setsockopt(int s, int level, int optname,
1671 const void *optval, int optlen); */
1672 PRE_MEM_READ( "socketcall.setsockopt(optval)",
1673 arg3, /* optval */
1674 arg4 /* optlen */ );
1677 /* ------ */
1679 void
1680 ML_(generic_PRE_sys_getsockname) ( ThreadId tid,
1681 UWord arg0, UWord arg1, UWord arg2 )
1683 /* int getsockname(int s, struct sockaddr* name, int* namelen) */
1684 Addr name_p = arg1;
1685 Addr namelen_p = arg2;
1686 /* Nb: name_p cannot be NULL */
1687 ML_(buf_and_len_pre_check) ( tid, name_p, namelen_p,
1688 "socketcall.getsockname(name)",
1689 "socketcall.getsockname(namelen_in)" );
1692 void
1693 ML_(generic_POST_sys_getsockname) ( ThreadId tid,
1694 SysRes res,
1695 UWord arg0, UWord arg1, UWord arg2 )
1697 Addr name_p = arg1;
1698 Addr namelen_p = arg2;
1699 vg_assert(!sr_isError(res)); /* guaranteed by caller */
1700 ML_(buf_and_len_post_check) ( tid, res, name_p, namelen_p,
1701 "socketcall.getsockname(namelen_out)" );
1704 /* ------ */
1706 void
1707 ML_(generic_PRE_sys_getpeername) ( ThreadId tid,
1708 UWord arg0, UWord arg1, UWord arg2 )
1710 /* int getpeername(int s, struct sockaddr* name, int* namelen) */
1711 Addr name_p = arg1;
1712 Addr namelen_p = arg2;
1713 /* Nb: name_p cannot be NULL */
1714 ML_(buf_and_len_pre_check) ( tid, name_p, namelen_p,
1715 "socketcall.getpeername(name)",
1716 "socketcall.getpeername(namelen_in)" );
1719 void
1720 ML_(generic_POST_sys_getpeername) ( ThreadId tid,
1721 SysRes res,
1722 UWord arg0, UWord arg1, UWord arg2 )
1724 Addr name_p = arg1;
1725 Addr namelen_p = arg2;
1726 vg_assert(!sr_isError(res)); /* guaranteed by caller */
1727 ML_(buf_and_len_post_check) ( tid, res, name_p, namelen_p,
1728 "socketcall.getpeername(namelen_out)" );
1731 /* ------ */
1733 void
1734 ML_(generic_PRE_sys_sendmsg) ( ThreadId tid, const HChar *name,
1735 struct vki_msghdr *msg )
1737 msghdr_foreachfield ( tid, name, msg, ~0, pre_mem_read_sendmsg, False );
1740 /* ------ */
1742 void
1743 ML_(generic_PRE_sys_recvmsg) ( ThreadId tid, const HChar *name,
1744 struct vki_msghdr *msg )
1746 msghdr_foreachfield ( tid, name, msg, ~0, pre_mem_write_recvmsg, True );
1749 void
1750 ML_(generic_POST_sys_recvmsg) ( ThreadId tid, const HChar *name,
1751 struct vki_msghdr *msg, UInt length )
1753 msghdr_foreachfield( tid, name, msg, length, post_mem_write_recvmsg, True );
1754 check_cmsg_for_fds( tid, msg );
1758 /* ---------------------------------------------------------------------
1759 Deal with a bunch of IPC related syscalls
1760 ------------------------------------------------------------------ */
1762 /* ------ */
1764 void
1765 ML_(generic_PRE_sys_semop) ( ThreadId tid,
1766 UWord arg0, UWord arg1, UWord arg2 )
1768 /* int semop(int semid, struct sembuf *sops, unsigned nsops); */
1769 PRE_MEM_READ( "semop(sops)", arg1, arg2 * sizeof(struct vki_sembuf) );
1772 /* ------ */
1774 void
1775 ML_(generic_PRE_sys_semtimedop) ( ThreadId tid,
1776 UWord arg0, UWord arg1,
1777 UWord arg2, UWord arg3 )
1779 /* int semtimedop(int semid, struct sembuf *sops, unsigned nsops,
1780 struct timespec *timeout); */
1781 PRE_MEM_READ( "semtimedop(sops)", arg1, arg2 * sizeof(struct vki_sembuf) );
1782 if (arg3 != 0)
1783 PRE_MEM_READ( "semtimedop(timeout)", arg3, sizeof(struct vki_timespec) );
1786 /* ------ */
1788 static
1789 UInt get_sem_count( Int semid )
1791 union vki_semun arg;
1792 SysRes res;
1794 # if defined(__NR_semctl)
1795 # if defined(VGO_darwin)
1796 /* Darwin has no specific 64 bit semid_ds, but has __NR_semctl. */
1797 struct vki_semid_ds buf;
1798 arg.buf = &buf;
1799 # else
1800 struct vki_semid64_ds buf;
1801 arg.buf64 = &buf;
1802 # endif
1803 res = VG_(do_syscall4)(__NR_semctl, semid, 0, VKI_IPC_STAT, *(UWord *)&arg);
1804 if (sr_isError(res))
1805 return 0;
1807 return buf.sem_nsems;
1809 # elif defined(__NR_semsys) /* Solaris */
1810 struct vki_semid_ds buf;
1811 arg.buf = &buf;
1812 res = VG_(do_syscall5)(__NR_semsys, VKI_SEMCTL, semid, 0, VKI_IPC_STAT,
1813 *(UWord *)&arg);
1814 if (sr_isError(res))
1815 return 0;
1817 return buf.sem_nsems;
1819 # else
1820 struct vki_semid_ds buf;
1821 arg.buf = &buf;
1822 res = VG_(do_syscall5)(__NR_ipc, 3 /* IPCOP_semctl */, semid, 0,
1823 VKI_IPC_STAT, (UWord)&arg);
1824 if (sr_isError(res))
1825 return 0;
1827 return buf.sem_nsems;
1828 # endif
1831 void
1832 ML_(generic_PRE_sys_semctl) ( ThreadId tid,
1833 UWord arg0, UWord arg1,
1834 UWord arg2, UWord arg3 )
1836 /* int semctl(int semid, int semnum, int cmd, ...); */
1837 union vki_semun arg = *(union vki_semun *)&arg3;
1838 UInt nsems;
1839 switch (arg2 /* cmd */) {
1840 #if defined(VKI_IPC_INFO)
1841 case VKI_IPC_INFO:
1842 case VKI_SEM_INFO:
1843 case VKI_IPC_INFO|VKI_IPC_64:
1844 case VKI_SEM_INFO|VKI_IPC_64:
1845 PRE_MEM_WRITE( "semctl(IPC_INFO, arg.buf)",
1846 (Addr)arg.buf, sizeof(struct vki_seminfo) );
1847 break;
1848 #endif
1850 case VKI_IPC_STAT:
1851 #if defined(VKI_SEM_STAT)
1852 case VKI_SEM_STAT:
1853 #endif
1854 PRE_MEM_WRITE( "semctl(IPC_STAT, arg.buf)",
1855 (Addr)arg.buf, sizeof(struct vki_semid_ds) );
1856 break;
1858 #if defined(VKI_IPC_64)
1859 case VKI_IPC_STAT|VKI_IPC_64:
1860 #if defined(VKI_SEM_STAT)
1861 case VKI_SEM_STAT|VKI_IPC_64:
1862 #endif
1863 #endif
1864 #if defined(VKI_IPC_STAT64)
1865 case VKI_IPC_STAT64:
1866 #endif
1867 #if defined(VKI_IPC_64) || defined(VKI_IPC_STAT64)
1868 PRE_MEM_WRITE( "semctl(IPC_STAT, arg.buf)",
1869 (Addr)arg.buf, sizeof(struct vki_semid64_ds) );
1870 break;
1871 #endif
1873 case VKI_IPC_SET:
1874 PRE_MEM_READ( "semctl(IPC_SET, arg.buf)",
1875 (Addr)arg.buf, sizeof(struct vki_semid_ds) );
1876 break;
1878 #if defined(VKI_IPC_64)
1879 case VKI_IPC_SET|VKI_IPC_64:
1880 #endif
1881 #if defined(VKI_IPC_SET64)
1882 case VKI_IPC_SET64:
1883 #endif
1884 #if defined(VKI_IPC64) || defined(VKI_IPC_SET64)
1885 PRE_MEM_READ( "semctl(IPC_SET, arg.buf)",
1886 (Addr)arg.buf, sizeof(struct vki_semid64_ds) );
1887 break;
1888 #endif
1890 case VKI_GETALL:
1891 #if defined(VKI_IPC_64)
1892 case VKI_GETALL|VKI_IPC_64:
1893 #endif
1894 nsems = get_sem_count( arg0 );
1895 PRE_MEM_WRITE( "semctl(IPC_GETALL, arg.array)",
1896 (Addr)arg.array, sizeof(unsigned short) * nsems );
1897 break;
1899 case VKI_SETALL:
1900 #if defined(VKI_IPC_64)
1901 case VKI_SETALL|VKI_IPC_64:
1902 #endif
1903 nsems = get_sem_count( arg0 );
1904 PRE_MEM_READ( "semctl(IPC_SETALL, arg.array)",
1905 (Addr)arg.array, sizeof(unsigned short) * nsems );
1906 break;
1910 void
1911 ML_(generic_POST_sys_semctl) ( ThreadId tid,
1912 UWord res,
1913 UWord arg0, UWord arg1,
1914 UWord arg2, UWord arg3 )
1916 union vki_semun arg = *(union vki_semun *)&arg3;
1917 UInt nsems;
1918 switch (arg2 /* cmd */) {
1919 #if defined(VKI_IPC_INFO)
1920 case VKI_IPC_INFO:
1921 case VKI_SEM_INFO:
1922 case VKI_IPC_INFO|VKI_IPC_64:
1923 case VKI_SEM_INFO|VKI_IPC_64:
1924 POST_MEM_WRITE( (Addr)arg.buf, sizeof(struct vki_seminfo) );
1925 break;
1926 #endif
1928 case VKI_IPC_STAT:
1929 #if defined(VKI_SEM_STAT)
1930 case VKI_SEM_STAT:
1931 #endif
1932 POST_MEM_WRITE( (Addr)arg.buf, sizeof(struct vki_semid_ds) );
1933 break;
1935 #if defined(VKI_IPC_64)
1936 case VKI_IPC_STAT|VKI_IPC_64:
1937 case VKI_SEM_STAT|VKI_IPC_64:
1938 #endif
1939 #if defined(VKI_IPC_STAT64)
1940 case VKI_IPC_STAT64:
1941 #endif
1942 #if defined(VKI_IPC_64) || defined(VKI_IPC_STAT64)
1943 POST_MEM_WRITE( (Addr)arg.buf, sizeof(struct vki_semid64_ds) );
1944 break;
1945 #endif
1947 case VKI_GETALL:
1948 #if defined(VKI_IPC_64)
1949 case VKI_GETALL|VKI_IPC_64:
1950 #endif
1951 nsems = get_sem_count( arg0 );
1952 POST_MEM_WRITE( (Addr)arg.array, sizeof(unsigned short) * nsems );
1953 break;
1957 /* ------ */
1959 /* ------ */
1961 static
1962 SizeT get_shm_size ( Int shmid )
1964 #if defined(__NR_shmctl)
1965 # ifdef VKI_IPC_64
1966 struct vki_shmid64_ds buf;
1967 # if defined(VGP_amd64_linux) || defined(VGP_arm64_linux)
1968 /* See bug 222545 comment 7 */
1969 SysRes __res = VG_(do_syscall3)(__NR_shmctl, shmid,
1970 VKI_IPC_STAT, (UWord)&buf);
1971 # else
1972 SysRes __res = VG_(do_syscall3)(__NR_shmctl, shmid,
1973 VKI_IPC_STAT|VKI_IPC_64, (UWord)&buf);
1974 # endif
1975 # else /* !def VKI_IPC_64 */
1976 struct vki_shmid_ds buf;
1977 SysRes __res = VG_(do_syscall3)(__NR_shmctl, shmid, VKI_IPC_STAT, (UWord)&buf);
1978 # endif /* def VKI_IPC_64 */
1979 #elif defined(__NR_shmsys) /* Solaris */
1980 struct vki_shmid_ds buf;
1981 SysRes __res = VG_(do_syscall4)(__NR_shmsys, VKI_SHMCTL, shmid, VKI_IPC_STAT,
1982 (UWord)&buf);
1983 #else
1984 struct vki_shmid_ds buf;
1985 SysRes __res = VG_(do_syscall5)(__NR_ipc, 24 /* IPCOP_shmctl */, shmid,
1986 VKI_IPC_STAT, 0, (UWord)&buf);
1987 #endif
1988 if (sr_isError(__res))
1989 return 0;
1991 return (SizeT) buf.shm_segsz;
1994 UWord
1995 ML_(generic_PRE_sys_shmat) ( ThreadId tid,
1996 UWord arg0, UWord arg1, UWord arg2 )
1998 /* void *shmat(int shmid, const void *shmaddr, int shmflg); */
1999 SizeT segmentSize = get_shm_size ( arg0 );
2000 UWord tmp;
2001 Bool ok;
2002 if (arg1 == 0) {
2003 /* arm-linux only: work around the fact that
2004 VG_(am_get_advisory_client_simple) produces something that is
2005 VKI_PAGE_SIZE aligned, whereas what we want is something
2006 VKI_SHMLBA aligned, and VKI_SHMLBA >= VKI_PAGE_SIZE. Hence
2007 increase the request size by VKI_SHMLBA - VKI_PAGE_SIZE and
2008 then round the result up to the next VKI_SHMLBA boundary.
2009 See bug 222545 comment 15. So far, arm-linux is the only
2010 platform where this is known to be necessary. */
2011 vg_assert(VKI_SHMLBA >= VKI_PAGE_SIZE);
2012 if (VKI_SHMLBA > VKI_PAGE_SIZE) {
2013 segmentSize += VKI_SHMLBA - VKI_PAGE_SIZE;
2015 tmp = VG_(am_get_advisory_client_simple)(0, segmentSize, &ok);
2016 if (ok) {
2017 if (VKI_SHMLBA > VKI_PAGE_SIZE) {
2018 arg1 = VG_ROUNDUP(tmp, VKI_SHMLBA);
2019 } else {
2020 arg1 = tmp;
2024 else if (!ML_(valid_client_addr)(arg1, segmentSize, tid, "shmat"))
2025 arg1 = 0;
2026 return arg1;
2029 void
2030 ML_(generic_POST_sys_shmat) ( ThreadId tid,
2031 UWord res,
2032 UWord arg0, UWord arg1, UWord arg2 )
2034 SizeT segmentSize = VG_PGROUNDUP(get_shm_size(arg0));
2035 if ( segmentSize > 0 ) {
2036 UInt prot = VKI_PROT_READ|VKI_PROT_WRITE;
2037 Bool d;
2039 if (arg2 & VKI_SHM_RDONLY)
2040 prot &= ~VKI_PROT_WRITE;
2041 /* It isn't exactly correct to pass 0 for the fd and offset
2042 here. The kernel seems to think the corresponding section
2043 does have dev/ino numbers:
2045 04e52000-04ec8000 rw-s 00000000 00:06 1966090 /SYSV00000000 (deleted)
2047 However there is no obvious way to find them. In order to
2048 cope with the discrepancy, aspacem's sync checker omits the
2049 dev/ino correspondence check in cases where V does not know
2050 the dev/ino. */
2051 d = VG_(am_notify_client_shmat)( res, segmentSize, prot );
2053 /* we don't distinguish whether it's read-only or
2054 * read-write -- it doesn't matter really. */
2055 VG_TRACK( new_mem_mmap, res, segmentSize, True, True, False,
2056 0/*di_handle*/ );
2057 if (d)
2058 VG_(discard_translations)( (Addr)res,
2059 (ULong)VG_PGROUNDUP(segmentSize),
2060 "ML_(generic_POST_sys_shmat)" );
2064 /* ------ */
2066 Bool
2067 ML_(generic_PRE_sys_shmdt) ( ThreadId tid, UWord arg0 )
2069 /* int shmdt(const void *shmaddr); */
2070 return ML_(valid_client_addr)(arg0, 1, tid, "shmdt");
2073 void
2074 ML_(generic_POST_sys_shmdt) ( ThreadId tid, UWord res, UWord arg0 )
2076 NSegment const* s = VG_(am_find_nsegment)(arg0);
2078 if (s != NULL) {
2079 Addr s_start = s->start;
2080 SizeT s_len = s->end+1 - s->start;
2081 Bool d;
2083 vg_assert(s->kind == SkShmC);
2084 vg_assert(s->start == arg0);
2086 d = VG_(am_notify_munmap)(s_start, s_len);
2087 s = NULL; /* s is now invalid */
2088 VG_TRACK( die_mem_munmap, s_start, s_len );
2089 if (d)
2090 VG_(discard_translations)( s_start,
2091 (ULong)s_len,
2092 "ML_(generic_POST_sys_shmdt)" );
2095 /* ------ */
2097 void
2098 ML_(generic_PRE_sys_shmctl) ( ThreadId tid,
2099 UWord arg0, UWord arg1, UWord arg2 )
2101 /* int shmctl(int shmid, int cmd, struct shmid_ds *buf); */
2102 switch (arg1 /* cmd */) {
2103 #if defined(VKI_IPC_INFO)
2104 case VKI_IPC_INFO:
2105 PRE_MEM_WRITE( "shmctl(IPC_INFO, buf)",
2106 arg2, sizeof(struct vki_shminfo) );
2107 break;
2108 #if defined(VKI_IPC_64)
2109 case VKI_IPC_INFO|VKI_IPC_64:
2110 PRE_MEM_WRITE( "shmctl(IPC_INFO, buf)",
2111 arg2, sizeof(struct vki_shminfo64) );
2112 break;
2113 #endif
2114 #endif
2116 #if defined(VKI_SHM_INFO)
2117 case VKI_SHM_INFO:
2118 #if defined(VKI_IPC_64)
2119 case VKI_SHM_INFO|VKI_IPC_64:
2120 #endif
2121 PRE_MEM_WRITE( "shmctl(SHM_INFO, buf)",
2122 arg2, sizeof(struct vki_shm_info) );
2123 break;
2124 #endif
2126 case VKI_IPC_STAT:
2127 #if defined(VKI_SHM_STAT)
2128 case VKI_SHM_STAT:
2129 #endif
2130 PRE_MEM_WRITE( "shmctl(IPC_STAT, buf)",
2131 arg2, sizeof(struct vki_shmid_ds) );
2132 break;
2134 #if defined(VKI_IPC_64)
2135 case VKI_IPC_STAT|VKI_IPC_64:
2136 case VKI_SHM_STAT|VKI_IPC_64:
2137 PRE_MEM_WRITE( "shmctl(IPC_STAT, arg.buf)",
2138 arg2, sizeof(struct vki_shmid64_ds) );
2139 break;
2140 #endif
2142 case VKI_IPC_SET:
2143 PRE_MEM_READ( "shmctl(IPC_SET, arg.buf)",
2144 arg2, sizeof(struct vki_shmid_ds) );
2145 break;
2147 #if defined(VKI_IPC_64)
2148 case VKI_IPC_SET|VKI_IPC_64:
2149 PRE_MEM_READ( "shmctl(IPC_SET, arg.buf)",
2150 arg2, sizeof(struct vki_shmid64_ds) );
2151 break;
2152 #endif
2156 void
2157 ML_(generic_POST_sys_shmctl) ( ThreadId tid,
2158 UWord res,
2159 UWord arg0, UWord arg1, UWord arg2 )
2161 switch (arg1 /* cmd */) {
2162 #if defined(VKI_IPC_INFO)
2163 case VKI_IPC_INFO:
2164 POST_MEM_WRITE( arg2, sizeof(struct vki_shminfo) );
2165 break;
2166 case VKI_IPC_INFO|VKI_IPC_64:
2167 POST_MEM_WRITE( arg2, sizeof(struct vki_shminfo64) );
2168 break;
2169 #endif
2171 #if defined(VKI_SHM_INFO)
2172 case VKI_SHM_INFO:
2173 case VKI_SHM_INFO|VKI_IPC_64:
2174 POST_MEM_WRITE( arg2, sizeof(struct vki_shm_info) );
2175 break;
2176 #endif
2178 case VKI_IPC_STAT:
2179 #if defined(VKI_SHM_STAT)
2180 case VKI_SHM_STAT:
2181 #endif
2182 POST_MEM_WRITE( arg2, sizeof(struct vki_shmid_ds) );
2183 break;
2185 #if defined(VKI_IPC_64)
2186 case VKI_IPC_STAT|VKI_IPC_64:
2187 case VKI_SHM_STAT|VKI_IPC_64:
2188 POST_MEM_WRITE( arg2, sizeof(struct vki_shmid64_ds) );
2189 break;
2190 #endif
2196 /* ---------------------------------------------------------------------
2197 Generic handler for mmap
2198 ------------------------------------------------------------------ */
2201 * Although mmap is specified by POSIX and the argument are generally
2202 * consistent across platforms the precise details of the low level
2203 * argument passing conventions differ. For example:
2205 * - On x86-linux there is mmap (aka old_mmap) which takes the
2206 * arguments in a memory block and the offset in bytes; and
2207 * mmap2 (aka sys_mmap2) which takes the arguments in the normal
2208 * way and the offset in pages.
2210 * - On ppc32-linux there is mmap (aka sys_mmap) which takes the
2211 * arguments in the normal way and the offset in bytes; and
2212 * mmap2 (aka sys_mmap2) which takes the arguments in the normal
2213 * way and the offset in pages.
2215 * - On amd64-linux everything is simple and there is just the one
2216 * call, mmap (aka sys_mmap) which takes the arguments in the
2217 * normal way and the offset in bytes.
2219 * - On s390x-linux there is mmap (aka old_mmap) which takes the
2220 * arguments in a memory block and the offset in bytes. mmap2
2221 * is also available (but not exported via unistd.h) with
2222 * arguments in a memory block and the offset in pages.
2224 * To cope with all this we provide a generic handler function here
2225 * and then each platform implements one or more system call handlers
2226 * which call this generic routine after extracting and normalising
2227 * the arguments.
2230 SysRes
2231 ML_(generic_PRE_sys_mmap) ( ThreadId tid,
2232 UWord arg1, UWord arg2, UWord arg3,
2233 UWord arg4, UWord arg5, Off64T arg6 )
2235 Addr advised;
2236 SysRes sres;
2237 MapRequest mreq;
2238 Bool mreq_ok;
2240 # if defined(VGO_darwin)
2241 // Nb: we can't use this on Darwin, it has races:
2242 // * needs to RETRY if advisory succeeds but map fails
2243 // (could have been some other thread in a nonblocking call)
2244 // * needs to not use fixed-position mmap() on Darwin
2245 // (mmap will cheerfully smash whatever's already there, which might
2246 // be a new mapping from some other thread in a nonblocking call)
2247 VG_(core_panic)("can't use ML_(generic_PRE_sys_mmap) on Darwin");
2248 # endif
2250 if (arg2 == 0) {
2251 /* SuSV3 says: If len is zero, mmap() shall fail and no mapping
2252 shall be established. */
2253 return VG_(mk_SysRes_Error)( VKI_EINVAL );
2256 if (!VG_IS_PAGE_ALIGNED(arg1)) {
2257 /* zap any misaligned addresses. */
2258 /* SuSV3 says misaligned addresses only cause the MAP_FIXED case
2259 to fail. Here, we catch them all. */
2260 return VG_(mk_SysRes_Error)( VKI_EINVAL );
2263 if (!VG_IS_PAGE_ALIGNED(arg6)) {
2264 /* zap any misaligned offsets. */
2265 /* SuSV3 says: The off argument is constrained to be aligned and
2266 sized according to the value returned by sysconf() when
2267 passed _SC_PAGESIZE or _SC_PAGE_SIZE. */
2268 return VG_(mk_SysRes_Error)( VKI_EINVAL );
2271 /* Figure out what kind of allocation constraints there are
2272 (fixed/hint/any), and ask aspacem what we should do. */
2273 mreq.start = arg1;
2274 mreq.len = arg2;
2275 if (arg4 & VKI_MAP_FIXED) {
2276 mreq.rkind = MFixed;
2277 } else
2278 #if defined(VKI_MAP_ALIGN) /* Solaris specific */
2279 if (arg4 & VKI_MAP_ALIGN) {
2280 mreq.rkind = MAlign;
2281 if (mreq.start == 0) {
2282 mreq.start = VKI_PAGE_SIZE;
2284 /* VKI_MAP_FIXED and VKI_MAP_ALIGN don't like each other. */
2285 arg4 &= ~VKI_MAP_ALIGN;
2286 } else
2287 #endif
2288 if (arg1 != 0) {
2289 mreq.rkind = MHint;
2290 } else {
2291 mreq.rkind = MAny;
2294 /* Enquire ... */
2295 advised = VG_(am_get_advisory)( &mreq, True/*client*/, &mreq_ok );
2296 if (!mreq_ok) {
2297 /* Our request was bounced, so we'd better fail. */
2298 return VG_(mk_SysRes_Error)( VKI_EINVAL );
2301 # if defined(VKI_MAP_32BIT)
2302 /* MAP_32BIT is royally unportable, so if the client asks for it, try our
2303 best to make it work (but without complexifying aspacemgr).
2304 If the user requested MAP_32BIT, the mmap-ed space must be in the
2305 first 2GB of the address space. So, return ENOMEM if aspacemgr
2306 advisory is above the first 2GB. If MAP_FIXED is also requested,
2307 MAP_32BIT has to be ignored.
2308 Assumption about aspacemgr behaviour: aspacemgr scans the address space
2309 from low addresses to find a free segment. No special effort is done
2310 to keep the first 2GB 'free' for this MAP_32BIT. So, this will often
2311 fail once the program has already allocated significant memory. */
2312 if ((arg4 & VKI_MAP_32BIT) && !(arg4 & VKI_MAP_FIXED)) {
2313 if (advised + arg2 >= 0x80000000)
2314 return VG_(mk_SysRes_Error)( VKI_ENOMEM );
2316 # endif
2318 /* Otherwise we're OK (so far). Install aspacem's choice of
2319 address, and let the mmap go through. */
2320 sres = VG_(am_do_mmap_NO_NOTIFY)(advised, arg2, arg3,
2321 arg4 | VKI_MAP_FIXED,
2322 arg5, arg6);
2324 # if defined(VKI_MAP_32BIT)
2325 /* No recovery trial if the advisory was not accepted. */
2326 if ((arg4 & VKI_MAP_32BIT) && !(arg4 & VKI_MAP_FIXED)
2327 && sr_isError(sres)) {
2328 return VG_(mk_SysRes_Error)( VKI_ENOMEM );
2330 # endif
2332 /* A refinement: it may be that the kernel refused aspacem's choice
2333 of address. If we were originally asked for a hinted mapping,
2334 there is still a last chance: try again at any address.
2335 Hence: */
2336 if (mreq.rkind == MHint && sr_isError(sres)) {
2337 mreq.start = 0;
2338 mreq.len = arg2;
2339 mreq.rkind = MAny;
2340 advised = VG_(am_get_advisory)( &mreq, True/*client*/, &mreq_ok );
2341 if (!mreq_ok) {
2342 /* Our request was bounced, so we'd better fail. */
2343 return VG_(mk_SysRes_Error)( VKI_EINVAL );
2345 /* and try again with the kernel */
2346 sres = VG_(am_do_mmap_NO_NOTIFY)(advised, arg2, arg3,
2347 arg4 | VKI_MAP_FIXED,
2348 arg5, arg6);
2351 /* Yet another refinement : sometimes valgrind chooses an address
2352 which is not acceptable by the kernel. This at least happens
2353 when mmap-ing huge pages, using the flag MAP_HUGETLB.
2354 valgrind aspacem does not know about huge pages, and modifying
2355 it to handle huge pages is not straightforward (e.g. need
2356 to understand special file system mount options).
2357 So, let's just redo an mmap, without giving any constraint to
2358 the kernel. If that succeeds, check with aspacem that the returned
2359 address is acceptable.
2360 This will give a similar effect as if the user would have
2361 hinted that address.
2362 The aspacem state will be correctly updated afterwards.
2363 We however cannot do this last refinement when the user asked
2364 for a fixed mapping, as the user asked a specific address. */
2365 if (sr_isError(sres) && !(arg4 & VKI_MAP_FIXED)) {
2366 advised = 0;
2367 /* try mmap with NULL address and without VKI_MAP_FIXED
2368 to let the kernel decide. */
2369 sres = VG_(am_do_mmap_NO_NOTIFY)(advised, arg2, arg3,
2370 arg4,
2371 arg5, arg6);
2372 if (!sr_isError(sres)) {
2373 /* The kernel is supposed to know what it is doing, but let's
2374 do a last sanity check anyway, as if the chosen address had
2375 been initially hinted by the client. The whole point of this
2376 last try was to allow mmap of huge pages to succeed without
2377 making aspacem understand them, on the other hand the kernel
2378 does not know about valgrind reservations, so this mapping
2379 can end up in free space and reservations. */
2380 mreq.start = (Addr)sr_Res(sres);
2381 mreq.len = arg2;
2382 mreq.rkind = MHint;
2383 advised = VG_(am_get_advisory)( &mreq, True/*client*/, &mreq_ok );
2384 vg_assert(mreq_ok && advised == mreq.start);
2388 if (!sr_isError(sres)) {
2389 ULong di_handle;
2390 /* Notify aspacem. */
2391 notify_core_of_mmap(
2392 (Addr)sr_Res(sres), /* addr kernel actually assigned */
2393 arg2, /* length */
2394 arg3, /* prot */
2395 arg4, /* the original flags value */
2396 arg5, /* fd */
2397 arg6 /* offset */
2399 /* Load symbols? */
2400 di_handle = VG_(di_notify_mmap)( (Addr)sr_Res(sres),
2401 False/*allow_SkFileV*/, (Int)arg5 );
2402 /* Notify the tool. */
2403 notify_tool_of_mmap(
2404 (Addr)sr_Res(sres), /* addr kernel actually assigned */
2405 arg2, /* length */
2406 arg3, /* prot */
2407 di_handle /* so the tool can refer to the read debuginfo later,
2408 if it wants. */
2412 /* Stay sane */
2413 if (!sr_isError(sres) && (arg4 & VKI_MAP_FIXED))
2414 vg_assert(sr_Res(sres) == arg1);
2416 return sres;
2420 /* ---------------------------------------------------------------------
2421 The Main Entertainment ... syscall wrappers
2422 ------------------------------------------------------------------ */
2424 /* Note: the PRE() and POST() wrappers are for the actual functions
2425 implementing the system calls in the OS kernel. These mostly have
2426 names like sys_write(); a few have names like old_mmap(). See the
2427 comment for ML_(syscall_table)[] for important info about the __NR_foo
2428 constants and their relationship to the sys_foo() functions.
2430 Some notes about names used for syscalls and args:
2431 - For the --trace-syscalls=yes output, we use the sys_foo() name to avoid
2432 ambiguity.
2434 - For error messages, we generally use a somewhat generic name
2435 for the syscall (eg. "write" rather than "sys_write"). This should be
2436 good enough for the average user to understand what is happening,
2437 without confusing them with names like "sys_write".
2439 - Also, for error messages the arg names are mostly taken from the man
2440 pages (even though many of those man pages are really for glibc
2441 functions of the same name), rather than from the OS kernel source,
2442 for the same reason -- a user presented with a "bogus foo(bar)" arg
2443 will most likely look at the "foo" man page to see which is the "bar"
2444 arg.
2446 Note that we use our own vki_* types. The one exception is in
2447 PRE_REG_READn calls, where pointer types haven't been changed, because
2448 they don't need to be -- eg. for "foo*" to be used, the type foo need not
2449 be visible.
2451 XXX: some of these are arch-specific, and should be factored out.
2454 #define PRE(name) DEFN_PRE_TEMPLATE(generic, name)
2455 #define POST(name) DEFN_POST_TEMPLATE(generic, name)
2457 PRE(sys_exit)
2459 ThreadState* tst;
2460 /* simple; just make this thread exit */
2461 PRINT("exit( %ld )", SARG1);
2462 PRE_REG_READ1(void, "exit", int, status);
2463 tst = VG_(get_ThreadState)(tid);
2464 /* Set the thread's status to be exiting, then claim that the
2465 syscall succeeded. */
2466 tst->exitreason = VgSrc_ExitThread;
2467 tst->os_state.exitcode = ARG1;
2468 SET_STATUS_Success(0);
2471 PRE(sys_ni_syscall)
2473 PRINT("unimplemented (by the kernel) syscall: %s! (ni_syscall)\n",
2474 VG_SYSNUM_STRING(SYSNO));
2475 PRE_REG_READ0(long, "ni_syscall");
2476 SET_STATUS_Failure( VKI_ENOSYS );
2479 PRE(sys_iopl)
2481 PRINT("sys_iopl ( %" FMT_REGWORD "u )", ARG1);
2482 PRE_REG_READ1(long, "iopl", unsigned long, level);
2485 PRE(sys_fsync)
2487 *flags |= SfMayBlock;
2488 PRINT("sys_fsync ( %" FMT_REGWORD "u )", ARG1);
2489 PRE_REG_READ1(long, "fsync", unsigned int, fd);
2492 PRE(sys_fdatasync)
2494 *flags |= SfMayBlock;
2495 PRINT("sys_fdatasync ( %" FMT_REGWORD "u )", ARG1);
2496 PRE_REG_READ1(long, "fdatasync", unsigned int, fd);
2499 PRE(sys_msync)
2501 *flags |= SfMayBlock;
2502 PRINT("sys_msync ( %#" FMT_REGWORD "x, %" FMT_REGWORD "u, %#"
2503 FMT_REGWORD "x )", ARG1, ARG2, ARG3);
2504 PRE_REG_READ3(long, "msync",
2505 unsigned long, start, vki_size_t, length, int, flags);
2506 PRE_MEM_READ( "msync(start)", ARG1, ARG2 );
2509 // Nb: getpmsg() and putpmsg() are special additional syscalls used in early
2510 // versions of LiS (Linux Streams). They are not part of the kernel.
2511 // Therefore, we have to provide this type ourself, rather than getting it
2512 // from the kernel sources.
2513 struct vki_pmsg_strbuf {
2514 int maxlen; /* no. of bytes in buffer */
2515 int len; /* no. of bytes returned */
2516 vki_caddr_t buf; /* pointer to data */
2518 PRE(sys_getpmsg)
2520 /* LiS getpmsg from http://www.gcom.com/home/linux/lis/ */
2521 struct vki_pmsg_strbuf *ctrl;
2522 struct vki_pmsg_strbuf *data;
2523 *flags |= SfMayBlock;
2524 PRINT("sys_getpmsg ( %ld, %#" FMT_REGWORD "x, %#" FMT_REGWORD "x, %#"
2525 FMT_REGWORD "x, %#" FMT_REGWORD "x )", SARG1,
2526 ARG2, ARG3, ARG4, ARG5);
2527 PRE_REG_READ5(int, "getpmsg",
2528 int, fd, struct strbuf *, ctrl, struct strbuf *, data,
2529 int *, bandp, int *, flagsp);
2530 ctrl = (struct vki_pmsg_strbuf *)(Addr)ARG2;
2531 data = (struct vki_pmsg_strbuf *)(Addr)ARG3;
2532 if (ctrl && ctrl->maxlen > 0)
2533 PRE_MEM_WRITE( "getpmsg(ctrl)", (Addr)ctrl->buf, ctrl->maxlen);
2534 if (data && data->maxlen > 0)
2535 PRE_MEM_WRITE( "getpmsg(data)", (Addr)data->buf, data->maxlen);
2536 if (ARG4)
2537 PRE_MEM_WRITE( "getpmsg(bandp)", (Addr)ARG4, sizeof(int));
2538 if (ARG5)
2539 PRE_MEM_WRITE( "getpmsg(flagsp)", (Addr)ARG5, sizeof(int));
2541 POST(sys_getpmsg)
2543 struct vki_pmsg_strbuf *ctrl;
2544 struct vki_pmsg_strbuf *data;
2545 vg_assert(SUCCESS);
2546 ctrl = (struct vki_pmsg_strbuf *)(Addr)ARG2;
2547 data = (struct vki_pmsg_strbuf *)(Addr)ARG3;
2548 if (RES == 0 && ctrl && ctrl->len > 0) {
2549 POST_MEM_WRITE( (Addr)ctrl->buf, ctrl->len);
2551 if (RES == 0 && data && data->len > 0) {
2552 POST_MEM_WRITE( (Addr)data->buf, data->len);
2556 PRE(sys_putpmsg)
2558 /* LiS putpmsg from http://www.gcom.com/home/linux/lis/ */
2559 struct vki_pmsg_strbuf *ctrl;
2560 struct vki_pmsg_strbuf *data;
2561 *flags |= SfMayBlock;
2562 PRINT("sys_putpmsg ( %ld, %#" FMT_REGWORD "x, %#" FMT_REGWORD
2563 "x, %ld, %ld )", SARG1, ARG2, ARG3, SARG4, SARG5);
2564 PRE_REG_READ5(int, "putpmsg",
2565 int, fd, struct strbuf *, ctrl, struct strbuf *, data,
2566 int, band, int, flags);
2567 ctrl = (struct vki_pmsg_strbuf *)(Addr)ARG2;
2568 data = (struct vki_pmsg_strbuf *)(Addr)ARG3;
2569 if (ctrl && ctrl->len > 0)
2570 PRE_MEM_READ( "putpmsg(ctrl)", (Addr)ctrl->buf, ctrl->len);
2571 if (data && data->len > 0)
2572 PRE_MEM_READ( "putpmsg(data)", (Addr)data->buf, data->len);
2575 PRE(sys_getitimer)
2577 struct vki_itimerval *value = (struct vki_itimerval*)(Addr)ARG2;
2578 PRINT("sys_getitimer ( %ld, %#" FMT_REGWORD "x )", SARG1, ARG2);
2579 PRE_REG_READ2(long, "getitimer", int, which, struct itimerval *, value);
2581 PRE_timeval_WRITE( "getitimer(&value->it_interval)", &(value->it_interval));
2582 PRE_timeval_WRITE( "getitimer(&value->it_value)", &(value->it_value));
2585 POST(sys_getitimer)
2587 if (ARG2 != (Addr)NULL) {
2588 struct vki_itimerval *value = (struct vki_itimerval*)(Addr)ARG2;
2589 POST_timeval_WRITE( &(value->it_interval) );
2590 POST_timeval_WRITE( &(value->it_value) );
2594 PRE(sys_setitimer)
2596 PRINT("sys_setitimer ( %ld, %#" FMT_REGWORD "x, %#" FMT_REGWORD "x )",
2597 SARG1, ARG2, ARG3);
2598 PRE_REG_READ3(long, "setitimer",
2599 int, which,
2600 struct itimerval *, value, struct itimerval *, ovalue);
2601 if (ARG2 != (Addr)NULL) {
2602 struct vki_itimerval *value = (struct vki_itimerval*)(Addr)ARG2;
2603 PRE_timeval_READ( "setitimer(&value->it_interval)",
2604 &(value->it_interval));
2605 PRE_timeval_READ( "setitimer(&value->it_value)",
2606 &(value->it_value));
2608 if (ARG3 != (Addr)NULL) {
2609 struct vki_itimerval *ovalue = (struct vki_itimerval*)(Addr)ARG3;
2610 PRE_timeval_WRITE( "setitimer(&ovalue->it_interval)",
2611 &(ovalue->it_interval));
2612 PRE_timeval_WRITE( "setitimer(&ovalue->it_value)",
2613 &(ovalue->it_value));
2617 POST(sys_setitimer)
2619 if (ARG3 != (Addr)NULL) {
2620 struct vki_itimerval *ovalue = (struct vki_itimerval*)(Addr)ARG3;
2621 POST_timeval_WRITE( &(ovalue->it_interval) );
2622 POST_timeval_WRITE( &(ovalue->it_value) );
2626 PRE(sys_chroot)
2628 PRINT("sys_chroot ( %#" FMT_REGWORD "x )", ARG1);
2629 PRE_REG_READ1(long, "chroot", const char *, path);
2630 PRE_MEM_RASCIIZ( "chroot(path)", ARG1 );
2633 PRE(sys_madvise)
2635 *flags |= SfMayBlock;
2636 PRINT("sys_madvise ( %#" FMT_REGWORD "x, %" FMT_REGWORD "u, %ld )",
2637 ARG1, ARG2, SARG3);
2638 PRE_REG_READ3(long, "madvise",
2639 unsigned long, start, vki_size_t, length, int, advice);
2642 #if HAVE_MREMAP
2643 PRE(sys_mremap)
2645 // Nb: this is different to the glibc version described in the man pages,
2646 // which lacks the fifth 'new_address' argument.
2647 if (ARG4 & VKI_MREMAP_FIXED) {
2648 PRINT("sys_mremap ( %#" FMT_REGWORD "x, %" FMT_REGWORD "u, %"
2649 FMT_REGWORD "u, %#" FMT_REGWORD "x, %#" FMT_REGWORD "x )",
2650 ARG1, ARG2, ARG3, ARG4, ARG5);
2651 PRE_REG_READ5(unsigned long, "mremap",
2652 unsigned long, old_addr, unsigned long, old_size,
2653 unsigned long, new_size, unsigned long, flags,
2654 unsigned long, new_addr);
2655 } else {
2656 PRINT("sys_mremap ( %#" FMT_REGWORD "x, %" FMT_REGWORD "u, %"
2657 FMT_REGWORD "u, 0x%" FMT_REGWORD "x )",
2658 ARG1, ARG2, ARG3, ARG4);
2659 PRE_REG_READ4(unsigned long, "mremap",
2660 unsigned long, old_addr, unsigned long, old_size,
2661 unsigned long, new_size, unsigned long, flags);
2663 SET_STATUS_from_SysRes(
2664 do_mremap((Addr)ARG1, ARG2, (Addr)ARG5, ARG3, ARG4, tid)
2667 #endif /* HAVE_MREMAP */
2669 PRE(sys_nice)
2671 PRINT("sys_nice ( %ld )", SARG1);
2672 PRE_REG_READ1(long, "nice", int, inc);
2675 PRE(sys_mlock)
2677 *flags |= SfMayBlock;
2678 PRINT("sys_mlock ( %#" FMT_REGWORD "x, %" FMT_REGWORD "u )", ARG1, ARG2);
2679 PRE_REG_READ2(long, "mlock", unsigned long, addr, vki_size_t, len);
2682 PRE(sys_munlock)
2684 *flags |= SfMayBlock;
2685 PRINT("sys_munlock ( %#" FMT_REGWORD "x, %" FMT_REGWORD "u )", ARG1, ARG2);
2686 PRE_REG_READ2(long, "munlock", unsigned long, addr, vki_size_t, len);
2689 PRE(sys_mlockall)
2691 *flags |= SfMayBlock;
2692 PRINT("sys_mlockall ( %" FMT_REGWORD "x )", ARG1);
2693 PRE_REG_READ1(long, "mlockall", int, flags);
2696 PRE(sys_setpriority)
2698 PRINT("sys_setpriority ( %ld, %ld, %ld )", SARG1, SARG2, SARG3);
2699 PRE_REG_READ3(long, "setpriority", int, which, int, who, int, prio);
2702 PRE(sys_getpriority)
2704 PRINT("sys_getpriority ( %ld, %ld )", SARG1, SARG2);
2705 PRE_REG_READ2(long, "getpriority", int, which, int, who);
2708 PRE(sys_pwrite64)
2710 *flags |= SfMayBlock;
2711 #if VG_WORDSIZE == 4
2712 PRINT("sys_pwrite64 ( %" FMT_REGWORD "u, %#" FMT_REGWORD "x, %"
2713 FMT_REGWORD "u, %lld )", ARG1, ARG2, ARG3, (Long)MERGE64(ARG4,ARG5));
2714 PRE_REG_READ5(ssize_t, "pwrite64",
2715 unsigned int, fd, const char *, buf, vki_size_t, count,
2716 vki_u32, MERGE64_FIRST(offset), vki_u32, MERGE64_SECOND(offset));
2717 #elif VG_WORDSIZE == 8
2718 PRINT("sys_pwrite64 ( %lu, %#lx, %lu, %ld )",
2719 ARG1, ARG2, ARG3, SARG4);
2720 PRE_REG_READ4(ssize_t, "pwrite64",
2721 unsigned int, fd, const char *, buf, vki_size_t, count,
2722 Word, offset);
2723 #else
2724 # error Unexpected word size
2725 #endif
2726 PRE_MEM_READ( "pwrite64(buf)", ARG2, ARG3 );
2729 PRE(sys_sync)
2731 *flags |= SfMayBlock;
2732 PRINT("sys_sync ( )");
2733 PRE_REG_READ0(long, "sync");
2736 #if !defined(VGP_nanomips_linux)
2737 PRE(sys_fstatfs)
2739 FUSE_COMPATIBLE_MAY_BLOCK();
2740 PRINT("sys_fstatfs ( %" FMT_REGWORD "u, %#" FMT_REGWORD "x )", ARG1, ARG2);
2741 PRE_REG_READ2(long, "fstatfs",
2742 unsigned int, fd, struct statfs *, buf);
2743 PRE_MEM_WRITE( "fstatfs(buf)", ARG2, sizeof(struct vki_statfs) );
2746 POST(sys_fstatfs)
2748 POST_MEM_WRITE( ARG2, sizeof(struct vki_statfs) );
2751 PRE(sys_fstatfs64)
2753 FUSE_COMPATIBLE_MAY_BLOCK();
2754 PRINT("sys_fstatfs64 ( %" FMT_REGWORD "u, %" FMT_REGWORD "u, %#"
2755 FMT_REGWORD "x )", ARG1, ARG2, ARG3);
2756 PRE_REG_READ3(long, "fstatfs64",
2757 unsigned int, fd, vki_size_t, size, struct statfs64 *, buf);
2758 PRE_MEM_WRITE( "fstatfs64(buf)", ARG3, ARG2 );
2760 POST(sys_fstatfs64)
2762 POST_MEM_WRITE( ARG3, ARG2 );
2764 #endif
2766 PRE(sys_getsid)
2768 PRINT("sys_getsid ( %ld )", SARG1);
2769 PRE_REG_READ1(long, "getsid", vki_pid_t, pid);
2772 PRE(sys_pread64)
2774 *flags |= SfMayBlock;
2775 #if VG_WORDSIZE == 4
2776 PRINT("sys_pread64 ( %" FMT_REGWORD "u, %#" FMT_REGWORD "x, %"
2777 FMT_REGWORD "u, %lld )", ARG1, ARG2, ARG3, (Long)MERGE64(ARG4,ARG5));
2778 PRE_REG_READ5(ssize_t, "pread64",
2779 unsigned int, fd, char *, buf, vki_size_t, count,
2780 vki_u32, MERGE64_FIRST(offset), vki_u32, MERGE64_SECOND(offset));
2781 #elif VG_WORDSIZE == 8
2782 PRINT("sys_pread64 ( %lu, %#lx, %lu, %ld )",
2783 ARG1, ARG2, ARG3, SARG4);
2784 PRE_REG_READ4(ssize_t, "pread64",
2785 unsigned int, fd, char *, buf, vki_size_t, count,
2786 Word, offset);
2787 #else
2788 # error Unexpected word size
2789 #endif
2790 PRE_MEM_WRITE( "pread64(buf)", ARG2, ARG3 );
2792 POST(sys_pread64)
2794 vg_assert(SUCCESS);
2795 if (RES > 0) {
2796 POST_MEM_WRITE( ARG2, RES );
2800 PRE(sys_mknod)
2802 FUSE_COMPATIBLE_MAY_BLOCK();
2803 PRINT("sys_mknod ( %#" FMT_REGWORD "x(%s), %#" FMT_REGWORD "x, %#"
2804 FMT_REGWORD "x )", ARG1, (HChar*)(Addr)ARG1, ARG2, ARG3 );
2805 PRE_REG_READ3(long, "mknod",
2806 const char *, pathname, int, mode, unsigned, dev);
2807 PRE_MEM_RASCIIZ( "mknod(pathname)", ARG1 );
2810 PRE(sys_flock)
2812 *flags |= SfMayBlock;
2813 PRINT("sys_flock ( %" FMT_REGWORD "u, %" FMT_REGWORD "u )", ARG1, ARG2 );
2814 PRE_REG_READ2(long, "flock", unsigned int, fd, unsigned int, operation);
2817 // Pre_read a char** argument.
2818 void ML_(pre_argv_envp)(Addr a, ThreadId tid, const HChar *s1, const HChar *s2)
2820 while (True) {
2821 Addr a_deref;
2822 Addr* a_p = (Addr*)a;
2823 PRE_MEM_READ( s1, (Addr)a_p, sizeof(Addr) );
2824 a_deref = *a_p;
2825 if (0 == a_deref)
2826 break;
2827 PRE_MEM_RASCIIZ( s2, a_deref );
2828 a += sizeof(char*);
2832 static Bool i_am_the_only_thread ( void )
2834 Int c = VG_(count_living_threads)();
2835 vg_assert(c >= 1); /* stay sane */
2836 return c == 1;
2839 /* Wait until all other threads disappear. */
2840 void VG_(reap_threads)(ThreadId self)
2842 while (!i_am_the_only_thread()) {
2843 /* Let other thread(s) run */
2844 VG_(vg_yield)();
2845 VG_(poll_signals)(self);
2847 vg_assert(i_am_the_only_thread());
2850 // XXX: prototype here seemingly doesn't match the prototype for i386-linux,
2851 // but it seems to work nonetheless...
2852 PRE(sys_execve)
2854 HChar* path = NULL; /* path to executable */
2855 HChar** envp = NULL;
2856 HChar** argv = NULL;
2857 HChar** arg2copy;
2858 HChar* launcher_basename = NULL;
2859 ThreadState* tst;
2860 Int i, j, tot_args;
2861 SysRes res;
2862 Bool setuid_allowed, trace_this_child;
2864 PRINT("sys_execve ( %#" FMT_REGWORD "x(%s), %#" FMT_REGWORD "x, %#"
2865 FMT_REGWORD "x )", ARG1, (HChar*)(Addr)ARG1, ARG2, ARG3);
2866 PRE_REG_READ3(vki_off_t, "execve",
2867 char *, filename, char **, argv, char **, envp);
2868 PRE_MEM_RASCIIZ( "execve(filename)", ARG1 );
2869 if (ARG2 != 0) {
2870 /* At least the terminating NULL must be addressable. */
2871 if (!ML_(safe_to_deref)((HChar **) (Addr)ARG2, sizeof(HChar *))) {
2872 SET_STATUS_Failure(VKI_EFAULT);
2873 return;
2875 ML_(pre_argv_envp)( ARG2, tid, "execve(argv)", "execve(argv[i])" );
2877 if (ARG3 != 0) {
2878 /* At least the terminating NULL must be addressable. */
2879 if (!ML_(safe_to_deref)((HChar **) (Addr)ARG3, sizeof(HChar *))) {
2880 SET_STATUS_Failure(VKI_EFAULT);
2881 return;
2883 ML_(pre_argv_envp)( ARG3, tid, "execve(envp)", "execve(envp[i])" );
2886 vg_assert(VG_(is_valid_tid)(tid));
2887 tst = VG_(get_ThreadState)(tid);
2889 /* Erk. If the exec fails, then the following will have made a
2890 mess of things which makes it hard for us to continue. The
2891 right thing to do is piece everything together again in
2892 POST(execve), but that's close to impossible. Instead, we make
2893 an effort to check that the execve will work before actually
2894 doing it. */
2896 /* Check that the name at least begins in client-accessible storage. */
2897 if (ARG1 == 0 /* obviously bogus */
2898 || !VG_(am_is_valid_for_client)( ARG1, 1, VKI_PROT_READ )) {
2899 SET_STATUS_Failure( VKI_EFAULT );
2900 return;
2903 // debug-only printing
2904 if (0) {
2905 VG_(printf)("ARG1 = %p(%s)\n", (void*)(Addr)ARG1, (HChar*)(Addr)ARG1);
2906 if (ARG2) {
2907 VG_(printf)("ARG2 = ");
2908 Int q;
2909 HChar** vec = (HChar**)(Addr)ARG2;
2910 for (q = 0; vec[q]; q++)
2911 VG_(printf)("%p(%s) ", vec[q], vec[q]);
2912 VG_(printf)("\n");
2913 } else {
2914 VG_(printf)("ARG2 = null\n");
2918 // Decide whether or not we want to follow along
2919 { // Make 'child_argv' be a pointer to the child's arg vector
2920 // (skipping the exe name)
2921 const HChar** child_argv = (const HChar**)(Addr)ARG2;
2922 if (child_argv && child_argv[0] == NULL)
2923 child_argv = NULL;
2924 trace_this_child = VG_(should_we_trace_this_child)( (HChar*)(Addr)ARG1,
2925 child_argv );
2928 // Do the important checks: it is a file, is executable, permissions are
2929 // ok, etc. We allow setuid executables to run only in the case when
2930 // we are not simulating them, that is, they to be run natively.
2931 setuid_allowed = trace_this_child ? False : True;
2932 res = VG_(pre_exec_check)((const HChar *)(Addr)ARG1, NULL, setuid_allowed);
2933 if (sr_isError(res)) {
2934 SET_STATUS_Failure( sr_Err(res) );
2935 return;
2938 /* If we're tracing the child, and the launcher name looks bogus
2939 (possibly because launcher.c couldn't figure it out, see
2940 comments therein) then we have no option but to fail. */
2941 if (trace_this_child
2942 && (VG_(name_of_launcher) == NULL
2943 || VG_(name_of_launcher)[0] != '/')) {
2944 SET_STATUS_Failure( VKI_ECHILD ); /* "No child processes" */
2945 return;
2948 /* After this point, we can't recover if the execve fails. */
2949 VG_(debugLog)(1, "syswrap", "Exec of %s\n", (HChar*)(Addr)ARG1);
2952 // Terminate gdbserver if it is active.
2953 if (VG_(clo_vgdb) != Vg_VgdbNo) {
2954 // If the child will not be traced, we need to terminate gdbserver
2955 // to cleanup the gdbserver resources (e.g. the FIFO files).
2956 // If child will be traced, we also terminate gdbserver: the new
2957 // Valgrind will start a fresh gdbserver after exec.
2958 VG_(gdbserver) (0);
2961 /* Resistance is futile. Nuke all other threads. POSIX mandates
2962 this. (Really, nuke them all, since the new process will make
2963 its own new thread.) */
2964 VG_(nuke_all_threads_except)( tid, VgSrc_ExitThread );
2965 VG_(reap_threads)(tid);
2967 // Set up the child's exe path.
2969 if (trace_this_child) {
2971 // We want to exec the launcher. Get its pre-remembered path.
2972 path = VG_(name_of_launcher);
2973 // VG_(name_of_launcher) should have been acquired by m_main at
2974 // startup.
2975 vg_assert(path);
2977 launcher_basename = VG_(strrchr)(path, '/');
2978 if (launcher_basename == NULL || launcher_basename[1] == 0) {
2979 launcher_basename = path; // hmm, tres dubious
2980 } else {
2981 launcher_basename++;
2984 } else {
2985 path = (HChar*)(Addr)ARG1;
2988 // Set up the child's environment.
2990 // Remove the valgrind-specific stuff from the environment so the
2991 // child doesn't get vgpreload_core.so, vgpreload_<tool>.so, etc.
2992 // This is done unconditionally, since if we are tracing the child,
2993 // the child valgrind will set up the appropriate client environment.
2994 // Nb: we make a copy of the environment before trying to mangle it
2995 // as it might be in read-only memory (this was bug #101881).
2997 // Then, if tracing the child, set VALGRIND_LIB for it.
2999 if (ARG3 == 0) {
3000 envp = NULL;
3001 } else {
3002 envp = VG_(env_clone)( (HChar**)(Addr)ARG3 );
3003 if (envp == NULL) goto hosed;
3004 VG_(env_remove_valgrind_env_stuff)( envp, True /*ro_strings*/, NULL );
3007 if (trace_this_child) {
3008 // Set VALGRIND_LIB in ARG3 (the environment)
3009 VG_(env_setenv)( &envp, VALGRIND_LIB, VG_(libdir));
3012 // Set up the child's args. If not tracing it, they are
3013 // simply ARG2. Otherwise, they are
3015 // [launcher_basename] ++ VG_(args_for_valgrind) ++ [ARG1] ++ ARG2[1..]
3017 // except that the first VG_(args_for_valgrind_noexecpass) args
3018 // are omitted.
3020 if (!trace_this_child) {
3021 argv = (HChar**)(Addr)ARG2;
3022 } else {
3023 vg_assert( VG_(args_for_valgrind) );
3024 vg_assert( VG_(args_for_valgrind_noexecpass) >= 0 );
3025 vg_assert( VG_(args_for_valgrind_noexecpass)
3026 <= VG_(sizeXA)( VG_(args_for_valgrind) ) );
3027 /* how many args in total will there be? */
3028 // launcher basename
3029 tot_args = 1;
3030 // V's args
3031 tot_args += VG_(sizeXA)( VG_(args_for_valgrind) );
3032 tot_args -= VG_(args_for_valgrind_noexecpass);
3033 // name of client exe
3034 tot_args++;
3035 // args for client exe, skipping [0]
3036 arg2copy = (HChar**)(Addr)ARG2;
3037 if (arg2copy && arg2copy[0]) {
3038 for (i = 1; arg2copy[i]; i++)
3039 tot_args++;
3041 // allocate
3042 argv = VG_(malloc)( "di.syswrap.pre_sys_execve.1",
3043 (tot_args+1) * sizeof(HChar*) );
3044 // copy
3045 j = 0;
3046 argv[j++] = launcher_basename;
3047 for (i = 0; i < VG_(sizeXA)( VG_(args_for_valgrind) ); i++) {
3048 if (i < VG_(args_for_valgrind_noexecpass))
3049 continue;
3050 argv[j++] = * (HChar**) VG_(indexXA)( VG_(args_for_valgrind), i );
3052 argv[j++] = (HChar*)(Addr)ARG1;
3053 if (arg2copy && arg2copy[0])
3054 for (i = 1; arg2copy[i]; i++)
3055 argv[j++] = arg2copy[i];
3056 argv[j++] = NULL;
3057 // check
3058 vg_assert(j == tot_args+1);
3062 Set the signal state up for exec.
3064 We need to set the real signal state to make sure the exec'd
3065 process gets SIG_IGN properly.
3067 Also set our real sigmask to match the client's sigmask so that
3068 the exec'd child will get the right mask. First we need to
3069 clear out any pending signals so they they don't get delivered,
3070 which would confuse things.
3072 XXX This is a bug - the signals should remain pending, and be
3073 delivered to the new process after exec. There's also a
3074 race-condition, since if someone delivers us a signal between
3075 the sigprocmask and the execve, we'll still get the signal. Oh
3076 well.
3079 vki_sigset_t allsigs;
3080 vki_siginfo_t info;
3082 /* What this loop does: it queries SCSS (the signal state that
3083 the client _thinks_ the kernel is in) by calling
3084 VG_(do_sys_sigaction), and modifies the real kernel signal
3085 state accordingly. */
3086 for (i = 1; i < VG_(max_signal); i++) {
3087 vki_sigaction_fromK_t sa_f;
3088 vki_sigaction_toK_t sa_t;
3089 VG_(do_sys_sigaction)(i, NULL, &sa_f);
3090 VG_(convert_sigaction_fromK_to_toK)(&sa_f, &sa_t);
3091 if (sa_t.ksa_handler == VKI_SIG_IGN)
3092 VG_(sigaction)(i, &sa_t, NULL);
3093 else {
3094 sa_t.ksa_handler = VKI_SIG_DFL;
3095 VG_(sigaction)(i, &sa_t, NULL);
3099 VG_(sigfillset)(&allsigs);
3100 while(VG_(sigtimedwait_zero)(&allsigs, &info) > 0)
3103 VG_(sigprocmask)(VKI_SIG_SETMASK, &tst->sig_mask, NULL);
3106 if (0) {
3107 HChar **cpp;
3108 VG_(printf)("exec: %s\n", path);
3109 for (cpp = argv; cpp && *cpp; cpp++)
3110 VG_(printf)("argv: %s\n", *cpp);
3111 if (0)
3112 for (cpp = envp; cpp && *cpp; cpp++)
3113 VG_(printf)("env: %s\n", *cpp);
3116 SET_STATUS_from_SysRes(
3117 VG_(do_syscall3)(__NR_execve, (UWord)path, (UWord)argv, (UWord)envp)
3120 /* If we got here, then the execve failed. We've already made way
3121 too much of a mess to continue, so we have to abort. */
3122 hosed:
3123 vg_assert(FAILURE);
3124 VG_(message)(Vg_UserMsg, "execve(%#" FMT_REGWORD "x(%s), %#" FMT_REGWORD
3125 "x, %#" FMT_REGWORD "x) failed, errno %lu\n",
3126 ARG1, (HChar*)(Addr)ARG1, ARG2, ARG3, ERR);
3127 VG_(message)(Vg_UserMsg, "EXEC FAILED: I can't recover from "
3128 "execve() failing, so I'm dying.\n");
3129 VG_(message)(Vg_UserMsg, "Add more stringent tests in PRE(sys_execve), "
3130 "or work out how to recover.\n");
3131 VG_(exit)(101);
3134 PRE(sys_access)
3136 PRINT("sys_access ( %#" FMT_REGWORD "x(%s), %ld )", ARG1,
3137 (HChar*)(Addr)ARG1, SARG2);
3138 PRE_REG_READ2(long, "access", const char *, pathname, int, mode);
3139 PRE_MEM_RASCIIZ( "access(pathname)", ARG1 );
3142 PRE(sys_alarm)
3144 PRINT("sys_alarm ( %" FMT_REGWORD "u )", ARG1);
3145 PRE_REG_READ1(unsigned long, "alarm", unsigned int, seconds);
3148 PRE(sys_brk)
3150 Addr brk_limit = VG_(brk_limit);
3151 Addr brk_new;
3153 /* libc says: int brk(void *end_data_segment);
3154 kernel says: void* brk(void* end_data_segment); (more or less)
3156 libc returns 0 on success, and -1 (and sets errno) on failure.
3157 Nb: if you ask to shrink the dataseg end below what it
3158 currently is, that always succeeds, even if the dataseg end
3159 doesn't actually change (eg. brk(0)). Unless it seg faults.
3161 Kernel returns the new dataseg end. If the brk() failed, this
3162 will be unchanged from the old one. That's why calling (kernel)
3163 brk(0) gives the current dataseg end (libc brk() just returns
3164 zero in that case).
3166 Both will seg fault if you shrink it back into a text segment.
3168 PRINT("sys_brk ( %#" FMT_REGWORD "x )", ARG1);
3169 PRE_REG_READ1(unsigned long, "brk", unsigned long, end_data_segment);
3171 brk_new = do_brk(ARG1, tid);
3172 SET_STATUS_Success( brk_new );
3174 if (brk_new == ARG1) {
3175 /* brk() succeeded */
3176 if (brk_new < brk_limit) {
3177 /* successfully shrunk the data segment. */
3178 VG_TRACK( die_mem_brk, (Addr)ARG1,
3179 brk_limit-ARG1 );
3180 } else
3181 if (brk_new > brk_limit) {
3182 /* successfully grew the data segment */
3183 VG_TRACK( new_mem_brk, brk_limit,
3184 ARG1-brk_limit, tid );
3186 } else {
3187 /* brk() failed */
3188 vg_assert(brk_limit == brk_new);
3192 PRE(sys_chdir)
3194 FUSE_COMPATIBLE_MAY_BLOCK();
3195 PRINT("sys_chdir ( %#" FMT_REGWORD "x(%s) )", ARG1,(char*)(Addr)ARG1);
3196 PRE_REG_READ1(long, "chdir", const char *, path);
3197 PRE_MEM_RASCIIZ( "chdir(path)", ARG1 );
3200 PRE(sys_chmod)
3202 FUSE_COMPATIBLE_MAY_BLOCK();
3203 PRINT("sys_chmod ( %#" FMT_REGWORD "x(%s), %" FMT_REGWORD "u )", ARG1,
3204 (HChar*)(Addr)ARG1, ARG2);
3205 PRE_REG_READ2(long, "chmod", const char *, path, vki_mode_t, mode);
3206 PRE_MEM_RASCIIZ( "chmod(path)", ARG1 );
3209 PRE(sys_chown)
3211 FUSE_COMPATIBLE_MAY_BLOCK();
3212 PRINT("sys_chown ( %#" FMT_REGWORD "x(%s), 0x%" FMT_REGWORD "x, 0x%"
3213 FMT_REGWORD "x )", ARG1,(char*)(Addr)ARG1,ARG2,ARG3);
3214 PRE_REG_READ3(long, "chown",
3215 const char *, path, vki_uid_t, owner, vki_gid_t, group);
3216 PRE_MEM_RASCIIZ( "chown(path)", ARG1 );
3219 PRE(sys_lchown)
3221 FUSE_COMPATIBLE_MAY_BLOCK();
3222 PRINT("sys_lchown ( %#" FMT_REGWORD "x(%s), 0x%" FMT_REGWORD "x, 0x%"
3223 FMT_REGWORD "x )", ARG1,(char*)(Addr)ARG1,ARG2,ARG3);
3224 PRE_REG_READ3(long, "lchown",
3225 const char *, path, vki_uid_t, owner, vki_gid_t, group);
3226 PRE_MEM_RASCIIZ( "lchown(path)", ARG1 );
3229 PRE(sys_close)
3231 FUSE_COMPATIBLE_MAY_BLOCK();
3232 PRINT("sys_close ( %" FMT_REGWORD "u )", ARG1);
3233 PRE_REG_READ1(long, "close", unsigned int, fd);
3235 /* Detect and negate attempts by the client to close Valgrind's log fd */
3236 if ( (!ML_(fd_allowed)(ARG1, "close", tid, False))
3237 /* If doing -d style logging (which is to fd=2), don't
3238 allow that to be closed either. */
3239 || (ARG1 == 2/*stderr*/ && VG_(debugLog_getLevel)() > 0) )
3240 SET_STATUS_Failure( VKI_EBADF );
3243 POST(sys_close)
3245 if (VG_(clo_track_fds)) ML_(record_fd_close)(ARG1);
3248 PRE(sys_dup)
3250 PRINT("sys_dup ( %" FMT_REGWORD "u )", ARG1);
3251 PRE_REG_READ1(long, "dup", unsigned int, oldfd);
3254 POST(sys_dup)
3256 vg_assert(SUCCESS);
3257 if (!ML_(fd_allowed)(RES, "dup", tid, True)) {
3258 VG_(close)(RES);
3259 SET_STATUS_Failure( VKI_EMFILE );
3260 } else {
3261 if (VG_(clo_track_fds))
3262 ML_(record_fd_open_named)(tid, RES);
3266 PRE(sys_dup2)
3268 PRINT("sys_dup2 ( %" FMT_REGWORD "u, %" FMT_REGWORD "u )", ARG1, ARG2);
3269 PRE_REG_READ2(long, "dup2", unsigned int, oldfd, unsigned int, newfd);
3270 if (!ML_(fd_allowed)(ARG2, "dup2", tid, True))
3271 SET_STATUS_Failure( VKI_EBADF );
3274 POST(sys_dup2)
3276 vg_assert(SUCCESS);
3277 if (VG_(clo_track_fds))
3278 ML_(record_fd_open_named)(tid, RES);
3281 PRE(sys_fchdir)
3283 FUSE_COMPATIBLE_MAY_BLOCK();
3284 PRINT("sys_fchdir ( %" FMT_REGWORD "u )", ARG1);
3285 PRE_REG_READ1(long, "fchdir", unsigned int, fd);
3288 PRE(sys_fchown)
3290 FUSE_COMPATIBLE_MAY_BLOCK();
3291 PRINT("sys_fchown ( %" FMT_REGWORD "u, %" FMT_REGWORD "u, %"
3292 FMT_REGWORD "u )", ARG1, ARG2, ARG3);
3293 PRE_REG_READ3(long, "fchown",
3294 unsigned int, fd, vki_uid_t, owner, vki_gid_t, group);
3297 PRE(sys_fchmod)
3299 FUSE_COMPATIBLE_MAY_BLOCK();
3300 PRINT("sys_fchmod ( %" FMT_REGWORD "u, %" FMT_REGWORD "u )", ARG1, ARG2);
3301 PRE_REG_READ2(long, "fchmod", unsigned int, fildes, vki_mode_t, mode);
3304 #if !defined(VGP_nanomips_linux)
3305 PRE(sys_newfstat)
3307 FUSE_COMPATIBLE_MAY_BLOCK();
3308 PRINT("sys_newfstat ( %" FMT_REGWORD "u, %#" FMT_REGWORD "x )", ARG1, ARG2);
3309 PRE_REG_READ2(long, "fstat", unsigned int, fd, struct stat *, buf);
3310 PRE_MEM_WRITE( "fstat(buf)", ARG2, sizeof(struct vki_stat) );
3313 POST(sys_newfstat)
3315 POST_MEM_WRITE( ARG2, sizeof(struct vki_stat) );
3317 #endif
3319 #if !defined(VGO_solaris) && !defined(VGP_arm64_linux) && \
3320 !defined(VGP_nanomips_linux)
3321 static vki_sigset_t fork_saved_mask;
3323 // In Linux, the sys_fork() function varies across architectures, but we
3324 // ignore the various args it gets, and so it looks arch-neutral. Hmm.
3325 PRE(sys_fork)
3327 Bool is_child;
3328 Int child_pid;
3329 vki_sigset_t mask;
3331 PRINT("sys_fork ( )");
3332 PRE_REG_READ0(long, "fork");
3334 /* Block all signals during fork, so that we can fix things up in
3335 the child without being interrupted. */
3336 VG_(sigfillset)(&mask);
3337 VG_(sigprocmask)(VKI_SIG_SETMASK, &mask, &fork_saved_mask);
3339 VG_(do_atfork_pre)(tid);
3341 SET_STATUS_from_SysRes( VG_(do_syscall0)(__NR_fork) );
3343 if (!SUCCESS) return;
3345 #if defined(VGO_linux)
3346 // RES is 0 for child, non-0 (the child's PID) for parent.
3347 is_child = ( RES == 0 ? True : False );
3348 child_pid = ( is_child ? -1 : RES );
3349 #elif defined(VGO_darwin)
3350 // RES is the child's pid. RESHI is 1 for child, 0 for parent.
3351 is_child = RESHI;
3352 child_pid = RES;
3353 #else
3354 # error Unknown OS
3355 #endif
3357 if (is_child) {
3358 VG_(do_atfork_child)(tid);
3360 /* restore signal mask */
3361 VG_(sigprocmask)(VKI_SIG_SETMASK, &fork_saved_mask, NULL);
3362 } else {
3363 VG_(do_atfork_parent)(tid);
3365 PRINT(" fork: process %d created child %d\n", VG_(getpid)(), child_pid);
3367 /* restore signal mask */
3368 VG_(sigprocmask)(VKI_SIG_SETMASK, &fork_saved_mask, NULL);
3371 #endif // !defined(VGO_solaris) && !defined(VGP_arm64_linux)
3373 PRE(sys_ftruncate)
3375 *flags |= SfMayBlock;
3376 PRINT("sys_ftruncate ( %" FMT_REGWORD "u, %" FMT_REGWORD "u )", ARG1, ARG2);
3377 PRE_REG_READ2(long, "ftruncate", unsigned int, fd, unsigned long, length);
3380 PRE(sys_truncate)
3382 *flags |= SfMayBlock;
3383 PRINT("sys_truncate ( %#" FMT_REGWORD "x(%s), %" FMT_REGWORD "u )",
3384 ARG1, (HChar*)(Addr)ARG1, ARG2);
3385 PRE_REG_READ2(long, "truncate",
3386 const char *, path, unsigned long, length);
3387 PRE_MEM_RASCIIZ( "truncate(path)", ARG1 );
3390 PRE(sys_ftruncate64)
3392 *flags |= SfMayBlock;
3393 #if VG_WORDSIZE == 4
3394 PRINT("sys_ftruncate64 ( %" FMT_REGWORD "u, %llu )", ARG1,
3395 MERGE64(ARG2,ARG3));
3396 PRE_REG_READ3(long, "ftruncate64",
3397 unsigned int, fd,
3398 UWord, MERGE64_FIRST(length), UWord, MERGE64_SECOND(length));
3399 #else
3400 PRINT("sys_ftruncate64 ( %lu, %lu )", ARG1, ARG2);
3401 PRE_REG_READ2(long, "ftruncate64",
3402 unsigned int,fd, UWord,length);
3403 #endif
3406 PRE(sys_truncate64)
3408 *flags |= SfMayBlock;
3409 #if VG_WORDSIZE == 4
3410 PRINT("sys_truncate64 ( %#" FMT_REGWORD "x, %lld )", ARG1,
3411 (Long)MERGE64(ARG2, ARG3));
3412 PRE_REG_READ3(long, "truncate64",
3413 const char *, path,
3414 UWord, MERGE64_FIRST(length), UWord, MERGE64_SECOND(length));
3415 #else
3416 PRINT("sys_truncate64 ( %#lx, %lld )", ARG1, (Long)ARG2);
3417 PRE_REG_READ2(long, "truncate64",
3418 const char *,path, UWord,length);
3419 #endif
3420 PRE_MEM_RASCIIZ( "truncate64(path)", ARG1 );
3423 PRE(sys_getdents)
3425 *flags |= SfMayBlock;
3426 PRINT("sys_getdents ( %" FMT_REGWORD "u, %#" FMT_REGWORD "x, %" FMT_REGWORD
3427 "u )", ARG1, ARG2, ARG3);
3428 PRE_REG_READ3(long, "getdents",
3429 unsigned int, fd, struct vki_dirent *, dirp,
3430 unsigned int, count);
3431 PRE_MEM_WRITE( "getdents(dirp)", ARG2, ARG3 );
3434 POST(sys_getdents)
3436 vg_assert(SUCCESS);
3437 if (RES > 0)
3438 POST_MEM_WRITE( ARG2, RES );
3441 PRE(sys_getdents64)
3443 *flags |= SfMayBlock;
3444 PRINT("sys_getdents64 ( %" FMT_REGWORD "u, %#" FMT_REGWORD "x, %"
3445 FMT_REGWORD "u )",ARG1, ARG2, ARG3);
3446 PRE_REG_READ3(long, "getdents64",
3447 unsigned int, fd, struct vki_dirent64 *, dirp,
3448 unsigned int, count);
3449 PRE_MEM_WRITE( "getdents64(dirp)", ARG2, ARG3 );
3452 POST(sys_getdents64)
3454 vg_assert(SUCCESS);
3455 if (RES > 0)
3456 POST_MEM_WRITE( ARG2, RES );
3459 PRE(sys_getgroups)
3461 PRINT("sys_getgroups ( %ld, %#" FMT_REGWORD "x )", SARG1, ARG2);
3462 PRE_REG_READ2(long, "getgroups", int, size, vki_gid_t *, list);
3463 if (ARG1 > 0)
3464 PRE_MEM_WRITE( "getgroups(list)", ARG2, ARG1 * sizeof(vki_gid_t) );
3467 POST(sys_getgroups)
3469 vg_assert(SUCCESS);
3470 if (ARG1 > 0 && RES > 0)
3471 POST_MEM_WRITE( ARG2, RES * sizeof(vki_gid_t) );
3474 PRE(sys_getcwd)
3476 // Comment from linux/fs/dcache.c:
3477 // NOTE! The user-level library version returns a character pointer.
3478 // The kernel system call just returns the length of the buffer filled
3479 // (which includes the ending '\0' character), or a negative error
3480 // value.
3481 // Is this Linux-specific? If so it should be moved to syswrap-linux.c.
3482 PRINT("sys_getcwd ( %#" FMT_REGWORD "x, %llu )", ARG1,(ULong)ARG2);
3483 PRE_REG_READ2(long, "getcwd", char *, buf, unsigned long, size);
3484 PRE_MEM_WRITE( "getcwd(buf)", ARG1, ARG2 );
3487 POST(sys_getcwd)
3489 vg_assert(SUCCESS);
3490 if (RES != (Addr)NULL)
3491 POST_MEM_WRITE( ARG1, RES );
3494 PRE(sys_geteuid)
3496 PRINT("sys_geteuid ( )");
3497 PRE_REG_READ0(long, "geteuid");
3500 PRE(sys_getegid)
3502 PRINT("sys_getegid ( )");
3503 PRE_REG_READ0(long, "getegid");
3506 PRE(sys_getgid)
3508 PRINT("sys_getgid ( )");
3509 PRE_REG_READ0(long, "getgid");
3512 PRE(sys_getpid)
3514 PRINT("sys_getpid ()");
3515 PRE_REG_READ0(long, "getpid");
3518 PRE(sys_getpgid)
3520 PRINT("sys_getpgid ( %ld )", SARG1);
3521 PRE_REG_READ1(long, "getpgid", vki_pid_t, pid);
3524 PRE(sys_getpgrp)
3526 PRINT("sys_getpgrp ()");
3527 PRE_REG_READ0(long, "getpgrp");
3530 PRE(sys_getppid)
3532 PRINT("sys_getppid ()");
3533 PRE_REG_READ0(long, "getppid");
3536 static void common_post_getrlimit(ThreadId tid, UWord a1, UWord a2)
3538 POST_MEM_WRITE( a2, sizeof(struct vki_rlimit) );
3540 #ifdef _RLIMIT_POSIX_FLAG
3541 // Darwin will sometimes set _RLIMIT_POSIX_FLAG on getrlimit calls.
3542 // Unset it here to make the switch case below work correctly.
3543 a1 &= ~_RLIMIT_POSIX_FLAG;
3544 #endif
3546 switch (a1) {
3547 case VKI_RLIMIT_NOFILE:
3548 ((struct vki_rlimit *)a2)->rlim_cur = VG_(fd_soft_limit);
3549 ((struct vki_rlimit *)a2)->rlim_max = VG_(fd_hard_limit);
3550 break;
3552 case VKI_RLIMIT_DATA:
3553 *((struct vki_rlimit *)a2) = VG_(client_rlimit_data);
3554 break;
3556 case VKI_RLIMIT_STACK:
3557 *((struct vki_rlimit *)a2) = VG_(client_rlimit_stack);
3558 break;
3562 PRE(sys_old_getrlimit)
3564 PRINT("sys_old_getrlimit ( %" FMT_REGWORD "u, %#" FMT_REGWORD "x )",
3565 ARG1, ARG2);
3566 PRE_REG_READ2(long, "old_getrlimit",
3567 unsigned int, resource, struct rlimit *, rlim);
3568 PRE_MEM_WRITE( "old_getrlimit(rlim)", ARG2, sizeof(struct vki_rlimit) );
3571 POST(sys_old_getrlimit)
3573 common_post_getrlimit(tid, ARG1, ARG2);
3576 PRE(sys_getrlimit)
3578 PRINT("sys_getrlimit ( %" FMT_REGWORD "u, %#" FMT_REGWORD "x )", ARG1, ARG2);
3579 PRE_REG_READ2(long, "getrlimit",
3580 unsigned int, resource, struct rlimit *, rlim);
3581 PRE_MEM_WRITE( "getrlimit(rlim)", ARG2, sizeof(struct vki_rlimit) );
3584 POST(sys_getrlimit)
3586 common_post_getrlimit(tid, ARG1, ARG2);
3589 PRE(sys_getrusage)
3591 PRINT("sys_getrusage ( %ld, %#" FMT_REGWORD "x )", SARG1, ARG2);
3592 PRE_REG_READ2(long, "getrusage", int, who, struct rusage *, usage);
3593 PRE_MEM_WRITE( "getrusage(usage)", ARG2, sizeof(struct vki_rusage) );
3596 POST(sys_getrusage)
3598 vg_assert(SUCCESS);
3599 if (RES == 0)
3600 POST_MEM_WRITE( ARG2, sizeof(struct vki_rusage) );
3603 PRE(sys_gettimeofday)
3605 PRINT("sys_gettimeofday ( %#" FMT_REGWORD "x, %#" FMT_REGWORD "x )",
3606 ARG1,ARG2);
3607 PRE_REG_READ2(long, "gettimeofday",
3608 struct timeval *, tv, struct timezone *, tz);
3609 // GrP fixme does darwin write to *tz anymore?
3610 if (ARG1 != 0)
3611 PRE_timeval_WRITE( "gettimeofday(tv)", (Addr)ARG1 );
3612 if (ARG2 != 0)
3613 PRE_MEM_WRITE( "gettimeofday(tz)", ARG2, sizeof(struct vki_timezone) );
3616 POST(sys_gettimeofday)
3618 vg_assert(SUCCESS);
3619 if (RES == 0) {
3620 if (ARG1 != 0)
3621 POST_timeval_WRITE( (Addr)ARG1 );
3622 if (ARG2 != 0)
3623 POST_MEM_WRITE( ARG2, sizeof(struct vki_timezone) );
3627 PRE(sys_settimeofday)
3629 PRINT("sys_settimeofday ( %#" FMT_REGWORD "x, %#" FMT_REGWORD "x )",
3630 ARG1,ARG2);
3631 PRE_REG_READ2(long, "settimeofday",
3632 struct timeval *, tv, struct timezone *, tz);
3633 if (ARG1 != 0)
3634 PRE_timeval_READ( "settimeofday(tv)", (Addr)ARG1 );
3635 if (ARG2 != 0) {
3636 PRE_MEM_READ( "settimeofday(tz)", ARG2, sizeof(struct vki_timezone) );
3637 /* maybe should warn if tz->tz_dsttime is non-zero? */
3641 PRE(sys_getuid)
3643 PRINT("sys_getuid ( )");
3644 PRE_REG_READ0(long, "getuid");
3647 void ML_(PRE_unknown_ioctl)(ThreadId tid, UWord request, UWord arg)
3649 /* We don't have any specific information on it, so
3650 try to do something reasonable based on direction and
3651 size bits. The encoding scheme is described in
3652 /usr/include/asm/ioctl.h or /usr/include/sys/ioccom.h .
3654 According to Simon Hausmann, _IOC_READ means the kernel
3655 writes a value to the ioctl value passed from the user
3656 space and the other way around with _IOC_WRITE. */
3658 #if defined(VGO_solaris)
3659 /* Majority of Solaris ioctl requests does not honour direction hints. */
3660 UInt dir = _VKI_IOC_NONE;
3661 #else
3662 UInt dir = _VKI_IOC_DIR(request);
3663 #endif
3664 UInt size = _VKI_IOC_SIZE(request);
3666 if (SimHintiS(SimHint_lax_ioctls, VG_(clo_sim_hints))) {
3668 * Be very lax about ioctl handling; the only
3669 * assumption is that the size is correct. Doesn't
3670 * require the full buffer to be initialized when
3671 * writing. Without this, using some device
3672 * drivers with a large number of strange ioctl
3673 * commands becomes very tiresome.
3675 } else if (/* size == 0 || */ dir == _VKI_IOC_NONE) {
3676 static UWord unknown_ioctl[10];
3677 static Int moans = sizeof(unknown_ioctl) / sizeof(unknown_ioctl[0]);
3679 if (moans > 0 && !VG_(clo_xml)) {
3680 /* Check if have not already moaned for this request. */
3681 UInt i;
3682 for (i = 0; i < sizeof(unknown_ioctl)/sizeof(unknown_ioctl[0]); i++) {
3683 if (unknown_ioctl[i] == request)
3684 break;
3685 if (unknown_ioctl[i] == 0) {
3686 unknown_ioctl[i] = request;
3687 moans--;
3688 VG_(umsg)("Warning: noted but unhandled ioctl 0x%lx"
3689 " with no size/direction hints.\n", request);
3690 VG_(umsg)(" This could cause spurious value errors to appear.\n");
3691 VG_(umsg)(" See README_MISSING_SYSCALL_OR_IOCTL for "
3692 "guidance on writing a proper wrapper.\n" );
3693 //VG_(get_and_pp_StackTrace)(tid, VG_(clo_backtrace_size));
3694 return;
3698 } else {
3699 //VG_(message)(Vg_UserMsg, "UNKNOWN ioctl %#lx\n", request);
3700 //VG_(get_and_pp_StackTrace)(tid, VG_(clo_backtrace_size));
3701 if ((dir & _VKI_IOC_WRITE) && size > 0)
3702 PRE_MEM_READ( "ioctl(generic)", arg, size);
3703 if ((dir & _VKI_IOC_READ) && size > 0)
3704 PRE_MEM_WRITE( "ioctl(generic)", arg, size);
3708 void ML_(POST_unknown_ioctl)(ThreadId tid, UInt res, UWord request, UWord arg)
3710 /* We don't have any specific information on it, so
3711 try to do something reasonable based on direction and
3712 size bits. The encoding scheme is described in
3713 /usr/include/asm/ioctl.h or /usr/include/sys/ioccom.h .
3715 According to Simon Hausmann, _IOC_READ means the kernel
3716 writes a value to the ioctl value passed from the user
3717 space and the other way around with _IOC_WRITE. */
3719 UInt dir = _VKI_IOC_DIR(request);
3720 UInt size = _VKI_IOC_SIZE(request);
3721 if (size > 0 && (dir & _VKI_IOC_READ)
3722 && res == 0
3723 && arg != (Addr)NULL) {
3724 POST_MEM_WRITE(arg, size);
3729 If we're sending a SIGKILL to one of our own threads, then simulate
3730 it rather than really sending the signal, so that the target thread
3731 gets a chance to clean up. Returns True if we did the killing (or
3732 no killing is necessary), and False if the caller should use the
3733 normal kill syscall.
3735 "pid" is any pid argument which can be passed to kill; group kills
3736 (< -1, 0), and owner kills (-1) are ignored, on the grounds that
3737 they'll most likely hit all the threads and we won't need to worry
3738 about cleanup. In truth, we can't fully emulate these multicast
3739 kills.
3741 "tgid" is a thread group id. If it is not -1, then the target
3742 thread must be in that thread group.
3744 Bool ML_(do_sigkill)(Int pid, Int tgid)
3746 ThreadState *tst;
3747 ThreadId tid;
3749 if (pid <= 0)
3750 return False;
3752 tid = VG_(lwpid_to_vgtid)(pid);
3753 if (tid == VG_INVALID_THREADID)
3754 return False; /* none of our threads */
3756 tst = VG_(get_ThreadState)(tid);
3757 if (tst == NULL || tst->status == VgTs_Empty)
3758 return False; /* hm, shouldn't happen */
3760 if (tgid != -1 && tst->os_state.threadgroup != tgid)
3761 return False; /* not the right thread group */
3763 /* Fatal SIGKILL sent to one of our threads.
3764 "Handle" the signal ourselves, as trying to have tid
3765 handling the signal causes termination problems (see #409367
3766 and #409141).
3767 Moreover, as a process cannot do anything when receiving SIGKILL,
3768 it is not particularly crucial that "tid" does the work to
3769 terminate the process. */
3771 if (VG_(clo_trace_signals))
3772 VG_(message)(Vg_DebugMsg,
3773 "Thread %u %s being killed with SIGKILL, running tid: %u\n",
3774 tst->tid, VG_(name_of_ThreadStatus) (tst->status), VG_(running_tid));
3776 if (!VG_(is_running_thread)(tid))
3777 tst = VG_(get_ThreadState)(VG_(running_tid));
3778 VG_(nuke_all_threads_except) (VG_(running_tid), VgSrc_FatalSig);
3779 VG_(reap_threads)(VG_(running_tid));
3780 tst->exitreason = VgSrc_FatalSig;
3781 tst->os_state.fatalsig = VKI_SIGKILL;
3783 return True;
3786 PRE(sys_kill)
3788 PRINT("sys_kill ( %ld, %ld )", SARG1, SARG2);
3789 PRE_REG_READ2(long, "kill", int, pid, int, signal);
3790 if (!ML_(client_signal_OK)(ARG2)) {
3791 SET_STATUS_Failure( VKI_EINVAL );
3792 return;
3795 /* If we're sending SIGKILL, check to see if the target is one of
3796 our threads and handle it specially. */
3797 if (ARG2 == VKI_SIGKILL && ML_(do_sigkill)(ARG1, -1))
3798 SET_STATUS_Success(0);
3799 else
3800 /* re syscall3: Darwin has a 3rd arg, which is a flag (boolean)
3801 affecting how posix-compliant the call is. I guess it is
3802 harmless to pass the 3rd arg on other platforms; hence pass
3803 it on all. */
3804 SET_STATUS_from_SysRes( VG_(do_syscall3)(SYSNO, ARG1, ARG2, ARG3) );
3806 if (VG_(clo_trace_signals))
3807 VG_(message)(Vg_DebugMsg, "kill: sent signal %ld to pid %ld\n",
3808 SARG2, SARG1);
3810 /* This kill might have given us a pending signal. Ask for a check once
3811 the syscall is done. */
3812 *flags |= SfPollAfter;
3815 PRE(sys_link)
3817 *flags |= SfMayBlock;
3818 PRINT("sys_link ( %#" FMT_REGWORD "x(%s), %#" FMT_REGWORD "x(%s) )", ARG1,
3819 (char*)(Addr)ARG1,ARG2,(char*)(Addr)ARG2);
3820 PRE_REG_READ2(long, "link", const char *, oldpath, const char *, newpath);
3821 PRE_MEM_RASCIIZ( "link(oldpath)", ARG1);
3822 PRE_MEM_RASCIIZ( "link(newpath)", ARG2);
3825 #if !defined(VGP_nanomips_linux)
3826 PRE(sys_newlstat)
3828 PRINT("sys_newlstat ( %#" FMT_REGWORD "x(%s), %#" FMT_REGWORD "x )", ARG1,
3829 (char*)(Addr)ARG1,ARG2);
3830 PRE_REG_READ2(long, "lstat", char *, file_name, struct stat *, buf);
3831 PRE_MEM_RASCIIZ( "lstat(file_name)", ARG1 );
3832 PRE_MEM_WRITE( "lstat(buf)", ARG2, sizeof(struct vki_stat) );
3835 POST(sys_newlstat)
3837 vg_assert(SUCCESS);
3838 POST_MEM_WRITE( ARG2, sizeof(struct vki_stat) );
3840 #endif
3842 PRE(sys_mkdir)
3844 *flags |= SfMayBlock;
3845 PRINT("sys_mkdir ( %#" FMT_REGWORD "x(%s), %ld )", ARG1,
3846 (HChar*)(Addr)ARG1, SARG2);
3847 PRE_REG_READ2(long, "mkdir", const char *, pathname, int, mode);
3848 PRE_MEM_RASCIIZ( "mkdir(pathname)", ARG1 );
3851 PRE(sys_mprotect)
3853 PRINT("sys_mprotect ( %#" FMT_REGWORD "x, %" FMT_REGWORD "u, %"
3854 FMT_REGWORD "u )", ARG1, ARG2, ARG3);
3855 PRE_REG_READ3(long, "mprotect",
3856 unsigned long, addr, vki_size_t, len, unsigned long, prot);
3858 Addr addr = ARG1;
3859 SizeT len = ARG2;
3860 Int prot = ARG3;
3862 handle_sys_mprotect (tid, status, &addr, &len, &prot);
3864 ARG1 = addr;
3865 ARG2 = len;
3866 ARG3 = prot;
3868 /* This will be called from the generic mprotect, or the linux specific
3869 pkey_mprotect. Pass pointers to ARG1, ARG2 and ARG3 as addr, len and prot,
3870 they might be adjusted and have to assigned back to ARG1, ARG2 and ARG3. */
3871 void handle_sys_mprotect(ThreadId tid, SyscallStatus* status,
3872 Addr *addr, SizeT *len, Int *prot)
3874 if (!ML_(valid_client_addr)(*addr, *len, tid, "mprotect")) {
3875 SET_STATUS_Failure( VKI_ENOMEM );
3877 #if defined(VKI_PROT_GROWSDOWN)
3878 else
3879 if (*prot & (VKI_PROT_GROWSDOWN|VKI_PROT_GROWSUP)) {
3880 /* Deal with mprotects on growable stack areas.
3882 The critical files to understand all this are mm/mprotect.c
3883 in the kernel and sysdeps/unix/sysv/linux/dl-execstack.c in
3884 glibc.
3886 The kernel provides PROT_GROWSDOWN and PROT_GROWSUP which
3887 round the start/end address of mprotect to the start/end of
3888 the underlying vma and glibc uses that as an easy way to
3889 change the protection of the stack by calling mprotect on the
3890 last page of the stack with PROT_GROWSDOWN set.
3892 The sanity check provided by the kernel is that the vma must
3893 have the VM_GROWSDOWN/VM_GROWSUP flag set as appropriate. */
3894 UInt grows = *prot & (VKI_PROT_GROWSDOWN|VKI_PROT_GROWSUP);
3895 NSegment const *aseg = VG_(am_find_nsegment)(*addr);
3896 NSegment const *rseg;
3898 vg_assert(aseg);
3900 if (grows == VKI_PROT_GROWSDOWN) {
3901 rseg = VG_(am_next_nsegment)( aseg, False/*backwards*/ );
3902 if (rseg
3903 && rseg->kind == SkResvn
3904 && rseg->smode == SmUpper
3905 && rseg->end+1 == aseg->start) {
3906 Addr end = *addr + *len;
3907 *addr = aseg->start;
3908 *len = end - aseg->start;
3909 *prot &= ~VKI_PROT_GROWSDOWN;
3910 } else {
3911 SET_STATUS_Failure( VKI_EINVAL );
3913 } else if (grows == VKI_PROT_GROWSUP) {
3914 rseg = VG_(am_next_nsegment)( aseg, True/*forwards*/ );
3915 if (rseg
3916 && rseg->kind == SkResvn
3917 && rseg->smode == SmLower
3918 && aseg->end+1 == rseg->start) {
3919 *len = aseg->end - *addr + 1;
3920 *prot &= ~VKI_PROT_GROWSUP;
3921 } else {
3922 SET_STATUS_Failure( VKI_EINVAL );
3924 } else {
3925 /* both GROWSUP and GROWSDOWN */
3926 SET_STATUS_Failure( VKI_EINVAL );
3929 #endif // defined(VKI_PROT_GROWSDOWN)
3932 POST(sys_mprotect)
3934 Addr a = ARG1;
3935 SizeT len = ARG2;
3936 Int prot = ARG3;
3938 ML_(notify_core_and_tool_of_mprotect)(a, len, prot);
3941 PRE(sys_munmap)
3943 if (0) VG_(printf)(" munmap( %#" FMT_REGWORD "x )\n", ARG1);
3944 PRINT("sys_munmap ( %#" FMT_REGWORD "x, %llu )", ARG1,(ULong)ARG2);
3945 PRE_REG_READ2(long, "munmap", unsigned long, start, vki_size_t, length);
3947 if (!ML_(valid_client_addr)(ARG1, ARG2, tid, "munmap"))
3948 SET_STATUS_Failure( VKI_EINVAL );
3951 POST(sys_munmap)
3953 Addr a = ARG1;
3954 SizeT len = ARG2;
3956 ML_(notify_core_and_tool_of_munmap)( a, len );
3959 PRE(sys_mincore)
3961 PRINT("sys_mincore ( %#" FMT_REGWORD "x, %llu, %#" FMT_REGWORD "x )",
3962 ARG1, (ULong)ARG2, ARG3);
3963 PRE_REG_READ3(long, "mincore",
3964 unsigned long, start, vki_size_t, length,
3965 unsigned char *, vec);
3966 PRE_MEM_WRITE( "mincore(vec)", ARG3, VG_PGROUNDUP(ARG2) / VKI_PAGE_SIZE );
3968 POST(sys_mincore)
3970 POST_MEM_WRITE( ARG3, VG_PGROUNDUP(ARG2) / VKI_PAGE_SIZE );
3973 PRE(sys_nanosleep)
3975 *flags |= SfMayBlock|SfPostOnFail;
3976 PRINT("sys_nanosleep ( %#" FMT_REGWORD "x, %#" FMT_REGWORD "x )", ARG1,ARG2);
3977 PRE_REG_READ2(long, "nanosleep",
3978 struct timespec *, req, struct timespec *, rem);
3979 PRE_MEM_READ( "nanosleep(req)", ARG1, sizeof(struct vki_timespec) );
3980 if (ARG2 != 0)
3981 PRE_MEM_WRITE( "nanosleep(rem)", ARG2, sizeof(struct vki_timespec) );
3984 POST(sys_nanosleep)
3986 vg_assert(SUCCESS || FAILURE);
3987 if (ARG2 != 0 && FAILURE && ERR == VKI_EINTR)
3988 POST_MEM_WRITE( ARG2, sizeof(struct vki_timespec) );
3991 #if defined(VGO_linux) || defined(VGO_solaris)
3992 /* Handles the case where the open is of /proc/self/auxv or
3993 /proc/<pid>/auxv, and just gives out a copy of the fd for the
3994 fake file we cooked up at startup (in m_main). Also, seeks the
3995 cloned fd back to the start.
3996 Returns True if auxv open was handled (status is set). */
3997 Bool ML_(handle_auxv_open)(SyscallStatus *status, const HChar *filename,
3998 int flags)
4000 HChar name[30]; // large enough
4002 if (!ML_(safe_to_deref)((const void *) filename, 1))
4003 return False;
4005 /* Opening /proc/<pid>/auxv or /proc/self/auxv? */
4006 VG_(sprintf)(name, "/proc/%d/auxv", VG_(getpid)());
4007 if (!VG_STREQ(filename, name) && !VG_STREQ(filename, "/proc/self/auxv"))
4008 return False;
4010 /* Allow to open the file only for reading. */
4011 if (flags & (VKI_O_WRONLY | VKI_O_RDWR)) {
4012 SET_STATUS_Failure(VKI_EACCES);
4013 return True;
4016 # if defined(VGO_solaris)
4017 VG_(sprintf)(name, "/proc/self/fd/%d", VG_(cl_auxv_fd));
4018 SysRes sres = VG_(open)(name, flags, 0);
4019 SET_STATUS_from_SysRes(sres);
4020 # else
4021 SysRes sres = VG_(dup)(VG_(cl_auxv_fd));
4022 SET_STATUS_from_SysRes(sres);
4023 if (!sr_isError(sres)) {
4024 OffT off = VG_(lseek)(sr_Res(sres), 0, VKI_SEEK_SET);
4025 if (off < 0)
4026 SET_STATUS_Failure(VKI_EMFILE);
4028 # endif
4030 return True;
4032 #endif // defined(VGO_linux) || defined(VGO_solaris)
4034 PRE(sys_open)
4036 if (ARG2 & VKI_O_CREAT) {
4037 // 3-arg version
4038 PRINT("sys_open ( %#" FMT_REGWORD "x(%s), %ld, %ld )",ARG1,
4039 (HChar*)(Addr)ARG1, SARG2, SARG3);
4040 PRE_REG_READ3(long, "open",
4041 const char *, filename, int, flags, int, mode);
4042 } else {
4043 // 2-arg version
4044 PRINT("sys_open ( %#" FMT_REGWORD "x(%s), %ld )",ARG1,
4045 (HChar*)(Addr)ARG1, SARG2);
4046 PRE_REG_READ2(long, "open",
4047 const char *, filename, int, flags);
4049 PRE_MEM_RASCIIZ( "open(filename)", ARG1 );
4051 #if defined(VGO_linux)
4052 /* Handle the case where the open is of /proc/self/cmdline or
4053 /proc/<pid>/cmdline, and just give it a copy of the fd for the
4054 fake file we cooked up at startup (in m_main). Also, seek the
4055 cloned fd back to the start. */
4057 HChar name[30]; // large enough
4058 HChar* arg1s = (HChar*) (Addr)ARG1;
4059 SysRes sres;
4061 VG_(sprintf)(name, "/proc/%d/cmdline", VG_(getpid)());
4062 if (ML_(safe_to_deref)( arg1s, 1 )
4063 && (VG_STREQ(arg1s, name) || VG_STREQ(arg1s, "/proc/self/cmdline"))) {
4064 sres = VG_(dup)( VG_(cl_cmdline_fd) );
4065 SET_STATUS_from_SysRes( sres );
4066 if (!sr_isError(sres)) {
4067 OffT off = VG_(lseek)( sr_Res(sres), 0, VKI_SEEK_SET );
4068 if (off < 0)
4069 SET_STATUS_Failure( VKI_EMFILE );
4071 return;
4075 /* Handle also the case of /proc/self/auxv or /proc/<pid>/auxv. */
4076 if (ML_(handle_auxv_open)(status, (const HChar *)(Addr)ARG1, ARG2))
4077 return;
4078 #endif // defined(VGO_linux)
4080 /* Otherwise handle normally */
4081 *flags |= SfMayBlock;
4084 POST(sys_open)
4086 vg_assert(SUCCESS);
4087 if (!ML_(fd_allowed)(RES, "open", tid, True)) {
4088 VG_(close)(RES);
4089 SET_STATUS_Failure( VKI_EMFILE );
4090 } else {
4091 if (VG_(clo_track_fds))
4092 ML_(record_fd_open_with_given_name)(tid, RES, (HChar*)(Addr)ARG1);
4096 PRE(sys_read)
4098 *flags |= SfMayBlock;
4099 PRINT("sys_read ( %" FMT_REGWORD "u, %#" FMT_REGWORD "x, %"
4100 FMT_REGWORD "u )", ARG1, ARG2, ARG3);
4101 PRE_REG_READ3(ssize_t, "read",
4102 unsigned int, fd, char *, buf, vki_size_t, count);
4104 if (!ML_(fd_allowed)(ARG1, "read", tid, False))
4105 SET_STATUS_Failure( VKI_EBADF );
4106 else
4107 PRE_MEM_WRITE( "read(buf)", ARG2, ARG3 );
4110 POST(sys_read)
4112 vg_assert(SUCCESS);
4113 POST_MEM_WRITE( ARG2, RES );
4116 PRE(sys_write)
4118 Bool ok;
4119 *flags |= SfMayBlock;
4120 PRINT("sys_write ( %" FMT_REGWORD "u, %#" FMT_REGWORD "x, %"
4121 FMT_REGWORD "u )", ARG1, ARG2, ARG3);
4122 PRE_REG_READ3(ssize_t, "write",
4123 unsigned int, fd, const char *, buf, vki_size_t, count);
4124 /* check to see if it is allowed. If not, try for an exemption from
4125 --sim-hints=enable-outer (used for self hosting). */
4126 ok = ML_(fd_allowed)(ARG1, "write", tid, False);
4127 if (!ok && ARG1 == 2/*stderr*/
4128 && SimHintiS(SimHint_enable_outer, VG_(clo_sim_hints)))
4129 ok = True;
4130 #if defined(VGO_solaris)
4131 if (!ok && VG_(vfork_fildes_addr) != NULL
4132 && *VG_(vfork_fildes_addr) >= 0 && *VG_(vfork_fildes_addr) == ARG1)
4133 ok = True;
4134 #endif
4135 if (!ok)
4136 SET_STATUS_Failure( VKI_EBADF );
4137 else
4138 PRE_MEM_READ( "write(buf)", ARG2, ARG3 );
4141 PRE(sys_creat)
4143 *flags |= SfMayBlock;
4144 PRINT("sys_creat ( %#" FMT_REGWORD "x(%s), %ld )", ARG1,
4145 (HChar*)(Addr)ARG1, SARG2);
4146 PRE_REG_READ2(long, "creat", const char *, pathname, int, mode);
4147 PRE_MEM_RASCIIZ( "creat(pathname)", ARG1 );
4150 POST(sys_creat)
4152 vg_assert(SUCCESS);
4153 if (!ML_(fd_allowed)(RES, "creat", tid, True)) {
4154 VG_(close)(RES);
4155 SET_STATUS_Failure( VKI_EMFILE );
4156 } else {
4157 if (VG_(clo_track_fds))
4158 ML_(record_fd_open_with_given_name)(tid, RES, (HChar*)(Addr)ARG1);
4162 PRE(sys_poll)
4164 /* struct pollfd {
4165 int fd; -- file descriptor
4166 short events; -- requested events
4167 short revents; -- returned events
4169 int poll(struct pollfd *ufds, unsigned int nfds, int timeout)
4171 UInt i;
4172 struct vki_pollfd* ufds = (struct vki_pollfd *)(Addr)ARG1;
4173 *flags |= SfMayBlock;
4174 PRINT("sys_poll ( %#" FMT_REGWORD "x, %" FMT_REGWORD "u, %ld )\n",
4175 ARG1, ARG2, SARG3);
4176 PRE_REG_READ3(long, "poll",
4177 struct vki_pollfd *, ufds, unsigned int, nfds, long, timeout);
4179 for (i = 0; i < ARG2; i++) {
4180 PRE_MEM_READ( "poll(ufds.fd)",
4181 (Addr)(&ufds[i].fd), sizeof(ufds[i].fd) );
4182 PRE_MEM_READ( "poll(ufds.events)",
4183 (Addr)(&ufds[i].events), sizeof(ufds[i].events) );
4184 PRE_MEM_WRITE( "poll(ufds.revents)",
4185 (Addr)(&ufds[i].revents), sizeof(ufds[i].revents) );
4189 POST(sys_poll)
4191 if (RES >= 0) {
4192 UInt i;
4193 struct vki_pollfd* ufds = (struct vki_pollfd *)(Addr)ARG1;
4194 for (i = 0; i < ARG2; i++)
4195 POST_MEM_WRITE( (Addr)(&ufds[i].revents), sizeof(ufds[i].revents) );
4199 PRE(sys_readlink)
4201 FUSE_COMPATIBLE_MAY_BLOCK();
4202 Word saved = SYSNO;
4204 PRINT("sys_readlink ( %#" FMT_REGWORD "x(%s), %#" FMT_REGWORD "x, %llu )",
4205 ARG1, (char*)(Addr)ARG1, ARG2, (ULong)ARG3);
4206 PRE_REG_READ3(long, "readlink",
4207 const char *, path, char *, buf, int, bufsiz);
4208 PRE_MEM_RASCIIZ( "readlink(path)", ARG1 );
4209 PRE_MEM_WRITE( "readlink(buf)", ARG2,ARG3 );
4213 #if defined(VGO_linux) || defined(VGO_solaris)
4214 #if defined(VGO_linux)
4215 #define PID_EXEPATH "/proc/%d/exe"
4216 #define SELF_EXEPATH "/proc/self/exe"
4217 #define SELF_EXEFD "/proc/self/fd/%d"
4218 #elif defined(VGO_solaris)
4219 #define PID_EXEPATH "/proc/%d/path/a.out"
4220 #define SELF_EXEPATH "/proc/self/path/a.out"
4221 #define SELF_EXEFD "/proc/self/path/%d"
4222 #endif
4224 * Handle the case where readlink is looking at /proc/self/exe or
4225 * /proc/<pid>/exe, or equivalent on Solaris.
4227 HChar name[30]; // large enough
4228 HChar* arg1s = (HChar*) (Addr)ARG1;
4229 VG_(sprintf)(name, PID_EXEPATH, VG_(getpid)());
4230 if (ML_(safe_to_deref)(arg1s, 1)
4231 && (VG_STREQ(arg1s, name) || VG_STREQ(arg1s, SELF_EXEPATH))) {
4232 VG_(sprintf)(name, SELF_EXEFD, VG_(cl_exec_fd));
4233 SET_STATUS_from_SysRes( VG_(do_syscall3)(saved, (UWord)name,
4234 ARG2, ARG3));
4235 } else
4236 #endif
4238 /* Normal case */
4239 SET_STATUS_from_SysRes( VG_(do_syscall3)(saved, ARG1, ARG2, ARG3));
4243 if (SUCCESS && RES > 0)
4244 POST_MEM_WRITE( ARG2, RES );
4247 PRE(sys_readv)
4249 Int i;
4250 struct vki_iovec * vec;
4251 *flags |= SfMayBlock;
4252 PRINT("sys_readv ( %" FMT_REGWORD "u, %#" FMT_REGWORD "x, %"
4253 FMT_REGWORD "u )", ARG1, ARG2, ARG3);
4254 PRE_REG_READ3(ssize_t, "readv",
4255 unsigned long, fd, const struct iovec *, vector,
4256 unsigned long, count);
4257 if (!ML_(fd_allowed)(ARG1, "readv", tid, False)) {
4258 SET_STATUS_Failure( VKI_EBADF );
4259 } else {
4260 if ((Int)ARG3 >= 0)
4261 PRE_MEM_READ( "readv(vector)", ARG2, ARG3 * sizeof(struct vki_iovec) );
4263 if (ARG2 != 0) {
4264 /* ToDo: don't do any of the following if the vector is invalid */
4265 vec = (struct vki_iovec *)(Addr)ARG2;
4266 for (i = 0; i < (Int)ARG3; i++)
4267 PRE_MEM_WRITE( "readv(vector[...])",
4268 (Addr)vec[i].iov_base, vec[i].iov_len );
4273 POST(sys_readv)
4275 vg_assert(SUCCESS);
4276 if (RES > 0) {
4277 Int i;
4278 struct vki_iovec * vec = (struct vki_iovec *)(Addr)ARG2;
4279 Int remains = RES;
4281 /* RES holds the number of bytes read. */
4282 for (i = 0; i < (Int)ARG3; i++) {
4283 Int nReadThisBuf = vec[i].iov_len;
4284 if (nReadThisBuf > remains) nReadThisBuf = remains;
4285 POST_MEM_WRITE( (Addr)vec[i].iov_base, nReadThisBuf );
4286 remains -= nReadThisBuf;
4287 if (remains < 0) VG_(core_panic)("readv: remains < 0");
4292 PRE(sys_rename)
4294 FUSE_COMPATIBLE_MAY_BLOCK();
4295 PRINT("sys_rename ( %#" FMT_REGWORD "x(%s), %#" FMT_REGWORD "x(%s) )", ARG1,
4296 (char*)(Addr)ARG1,ARG2,(char*)(Addr)ARG2);
4297 PRE_REG_READ2(long, "rename", const char *, oldpath, const char *, newpath);
4298 PRE_MEM_RASCIIZ( "rename(oldpath)", ARG1 );
4299 PRE_MEM_RASCIIZ( "rename(newpath)", ARG2 );
4302 PRE(sys_rmdir)
4304 *flags |= SfMayBlock;
4305 PRINT("sys_rmdir ( %#" FMT_REGWORD "x(%s) )", ARG1,(char*)(Addr)ARG1);
4306 PRE_REG_READ1(long, "rmdir", const char *, pathname);
4307 PRE_MEM_RASCIIZ( "rmdir(pathname)", ARG1 );
4310 PRE(sys_select)
4312 *flags |= SfMayBlock;
4313 PRINT("sys_select ( %ld, %#" FMT_REGWORD "x, %#" FMT_REGWORD "x, %#"
4314 FMT_REGWORD "x, %#" FMT_REGWORD "x )", SARG1, ARG2, ARG3, ARG4, ARG5);
4315 PRE_REG_READ5(long, "select",
4316 int, n, vki_fd_set *, readfds, vki_fd_set *, writefds,
4317 vki_fd_set *, exceptfds, struct vki_timeval *, timeout);
4318 // XXX: this possibly understates how much memory is read.
4319 if (ARG2 != 0)
4320 PRE_MEM_READ( "select(readfds)",
4321 ARG2, ARG1/8 /* __FD_SETSIZE/8 */ );
4322 if (ARG3 != 0)
4323 PRE_MEM_READ( "select(writefds)",
4324 ARG3, ARG1/8 /* __FD_SETSIZE/8 */ );
4325 if (ARG4 != 0)
4326 PRE_MEM_READ( "select(exceptfds)",
4327 ARG4, ARG1/8 /* __FD_SETSIZE/8 */ );
4328 if (ARG5 != 0)
4329 PRE_timeval_READ( "select(timeout)", (Addr)ARG5 );
4332 PRE(sys_setgid)
4334 PRINT("sys_setgid ( %" FMT_REGWORD "u )", ARG1);
4335 PRE_REG_READ1(long, "setgid", vki_gid_t, gid);
4338 PRE(sys_setsid)
4340 PRINT("sys_setsid ( )");
4341 PRE_REG_READ0(long, "setsid");
4344 PRE(sys_setgroups)
4346 PRINT("setgroups ( %llu, %#" FMT_REGWORD "x )", (ULong)ARG1, ARG2);
4347 PRE_REG_READ2(long, "setgroups", int, size, vki_gid_t *, list);
4348 if (ARG1 > 0)
4349 PRE_MEM_READ( "setgroups(list)", ARG2, ARG1 * sizeof(vki_gid_t) );
4352 PRE(sys_setpgid)
4354 PRINT("setpgid ( %ld, %ld )", SARG1, SARG2);
4355 PRE_REG_READ2(long, "setpgid", vki_pid_t, pid, vki_pid_t, pgid);
4358 PRE(sys_setregid)
4360 PRINT("sys_setregid ( %" FMT_REGWORD "u, %" FMT_REGWORD "u )", ARG1, ARG2);
4361 PRE_REG_READ2(long, "setregid", vki_gid_t, rgid, vki_gid_t, egid);
4364 PRE(sys_setreuid)
4366 PRINT("sys_setreuid ( 0x%" FMT_REGWORD "x, 0x%" FMT_REGWORD "x )",
4367 ARG1, ARG2);
4368 PRE_REG_READ2(long, "setreuid", vki_uid_t, ruid, vki_uid_t, euid);
4371 PRE(sys_setrlimit)
4373 UWord arg1 = ARG1;
4374 PRINT("sys_setrlimit ( %" FMT_REGWORD "u, %#" FMT_REGWORD "x )", ARG1, ARG2);
4375 PRE_REG_READ2(long, "setrlimit",
4376 unsigned int, resource, struct rlimit *, rlim);
4377 PRE_MEM_READ( "setrlimit(rlim)", ARG2, sizeof(struct vki_rlimit) );
4379 #ifdef _RLIMIT_POSIX_FLAG
4380 // Darwin will sometimes set _RLIMIT_POSIX_FLAG on setrlimit calls.
4381 // Unset it here to make the if statements below work correctly.
4382 arg1 &= ~_RLIMIT_POSIX_FLAG;
4383 #endif
4385 if (!VG_(am_is_valid_for_client)(ARG2, sizeof(struct vki_rlimit),
4386 VKI_PROT_READ)) {
4387 SET_STATUS_Failure( VKI_EFAULT );
4389 else if (((struct vki_rlimit *)(Addr)ARG2)->rlim_cur
4390 > ((struct vki_rlimit *)(Addr)ARG2)->rlim_max) {
4391 SET_STATUS_Failure( VKI_EINVAL );
4393 else if (arg1 == VKI_RLIMIT_NOFILE) {
4394 if (((struct vki_rlimit *)(Addr)ARG2)->rlim_cur > VG_(fd_hard_limit) ||
4395 ((struct vki_rlimit *)(Addr)ARG2)->rlim_max != VG_(fd_hard_limit)) {
4396 SET_STATUS_Failure( VKI_EPERM );
4398 else {
4399 VG_(fd_soft_limit) = ((struct vki_rlimit *)(Addr)ARG2)->rlim_cur;
4400 SET_STATUS_Success( 0 );
4403 else if (arg1 == VKI_RLIMIT_DATA) {
4404 if (((struct vki_rlimit *)(Addr)ARG2)->rlim_cur
4405 > VG_(client_rlimit_data).rlim_max ||
4406 ((struct vki_rlimit *)(Addr)ARG2)->rlim_max
4407 > VG_(client_rlimit_data).rlim_max) {
4408 SET_STATUS_Failure( VKI_EPERM );
4410 else {
4411 VG_(client_rlimit_data) = *(struct vki_rlimit *)(Addr)ARG2;
4412 SET_STATUS_Success( 0 );
4415 else if (arg1 == VKI_RLIMIT_STACK && tid == 1) {
4416 if (((struct vki_rlimit *)(Addr)ARG2)->rlim_cur
4417 > VG_(client_rlimit_stack).rlim_max ||
4418 ((struct vki_rlimit *)(Addr)ARG2)->rlim_max
4419 > VG_(client_rlimit_stack).rlim_max) {
4420 SET_STATUS_Failure( VKI_EPERM );
4422 else {
4423 /* Change the value of client_stack_szB to the rlim_cur value but
4424 only if it is smaller than the size of the allocated stack for the
4425 client.
4426 TODO: All platforms should set VG_(clstk_max_size) as part of their
4427 setup_client_stack(). */
4428 if ((VG_(clstk_max_size) == 0)
4429 || (((struct vki_rlimit *) (Addr)ARG2)->rlim_cur <= VG_(clstk_max_size)))
4430 VG_(threads)[tid].client_stack_szB = ((struct vki_rlimit *)(Addr)ARG2)->rlim_cur;
4432 VG_(client_rlimit_stack) = *(struct vki_rlimit *)(Addr)ARG2;
4433 SET_STATUS_Success( 0 );
4438 PRE(sys_setuid)
4440 PRINT("sys_setuid ( %" FMT_REGWORD "u )", ARG1);
4441 PRE_REG_READ1(long, "setuid", vki_uid_t, uid);
4444 #if !defined(VGP_nanomips_linux)
4445 PRE(sys_newstat)
4447 FUSE_COMPATIBLE_MAY_BLOCK();
4448 PRINT("sys_newstat ( %#" FMT_REGWORD "x(%s), %#" FMT_REGWORD "x )",
4449 ARG1,(char*)(Addr)ARG1,ARG2);
4450 PRE_REG_READ2(long, "stat", char *, file_name, struct stat *, buf);
4451 PRE_MEM_RASCIIZ( "stat(file_name)", ARG1 );
4452 PRE_MEM_WRITE( "stat(buf)", ARG2, sizeof(struct vki_stat) );
4455 POST(sys_newstat)
4457 POST_MEM_WRITE( ARG2, sizeof(struct vki_stat) );
4460 PRE(sys_statfs)
4462 FUSE_COMPATIBLE_MAY_BLOCK();
4463 PRINT("sys_statfs ( %#" FMT_REGWORD "x(%s), %#" FMT_REGWORD "x )",
4464 ARG1, (char*)(Addr)ARG1, ARG2);
4465 PRE_REG_READ2(long, "statfs", const char *, path, struct statfs *, buf);
4466 PRE_MEM_RASCIIZ( "statfs(path)", ARG1 );
4467 PRE_MEM_WRITE( "statfs(buf)", ARG2, sizeof(struct vki_statfs) );
4469 POST(sys_statfs)
4471 POST_MEM_WRITE( ARG2, sizeof(struct vki_statfs) );
4474 PRE(sys_statfs64)
4476 PRINT("sys_statfs64 ( %#" FMT_REGWORD "x(%s), %llu, %#" FMT_REGWORD "x )",
4477 ARG1, (char*)(Addr)ARG1, (ULong)ARG2, ARG3);
4478 PRE_REG_READ3(long, "statfs64",
4479 const char *, path, vki_size_t, size, struct statfs64 *, buf);
4480 PRE_MEM_RASCIIZ( "statfs64(path)", ARG1 );
4481 PRE_MEM_WRITE( "statfs64(buf)", ARG3, ARG2 );
4483 POST(sys_statfs64)
4485 POST_MEM_WRITE( ARG3, ARG2 );
4487 #endif
4489 PRE(sys_symlink)
4491 *flags |= SfMayBlock;
4492 PRINT("sys_symlink ( %#" FMT_REGWORD "x(%s), %#" FMT_REGWORD "x(%s) )",
4493 ARG1, (char*)(Addr)ARG1, ARG2, (char*)(Addr)ARG2);
4494 PRE_REG_READ2(long, "symlink", const char *, oldpath, const char *, newpath);
4495 PRE_MEM_RASCIIZ( "symlink(oldpath)", ARG1 );
4496 PRE_MEM_RASCIIZ( "symlink(newpath)", ARG2 );
4499 PRE(sys_time)
4501 /* time_t time(time_t *t); */
4502 PRINT("sys_time ( %#" FMT_REGWORD "x )",ARG1);
4503 PRE_REG_READ1(long, "time", int *, t);
4504 if (ARG1 != 0) {
4505 PRE_MEM_WRITE( "time(t)", ARG1, sizeof(vki_time_t) );
4509 POST(sys_time)
4511 if (ARG1 != 0) {
4512 POST_MEM_WRITE( ARG1, sizeof(vki_time_t) );
4516 PRE(sys_times)
4518 PRINT("sys_times ( %#" FMT_REGWORD "x )", ARG1);
4519 PRE_REG_READ1(long, "times", struct tms *, buf);
4520 if (ARG1 != 0) {
4521 PRE_MEM_WRITE( "times(buf)", ARG1, sizeof(struct vki_tms) );
4525 POST(sys_times)
4527 if (ARG1 != 0) {
4528 POST_MEM_WRITE( ARG1, sizeof(struct vki_tms) );
4532 PRE(sys_umask)
4534 PRINT("sys_umask ( %ld )", SARG1);
4535 PRE_REG_READ1(long, "umask", int, mask);
4538 PRE(sys_unlink)
4540 *flags |= SfMayBlock;
4541 PRINT("sys_unlink ( %#" FMT_REGWORD "x(%s) )", ARG1,(char*)(Addr)ARG1);
4542 PRE_REG_READ1(long, "unlink", const char *, pathname);
4543 PRE_MEM_RASCIIZ( "unlink(pathname)", ARG1 );
4546 PRE(sys_newuname)
4548 PRINT("sys_newuname ( %#" FMT_REGWORD "x )", ARG1);
4549 PRE_REG_READ1(long, "uname", struct new_utsname *, buf);
4550 PRE_MEM_WRITE( "uname(buf)", ARG1, sizeof(struct vki_new_utsname) );
4553 POST(sys_newuname)
4555 if (ARG1 != 0) {
4556 POST_MEM_WRITE( ARG1, sizeof(struct vki_new_utsname) );
4560 PRE(sys_waitpid)
4562 *flags |= SfMayBlock;
4563 PRINT("sys_waitpid ( %ld, %#" FMT_REGWORD "x, %ld )", SARG1, ARG2, SARG3);
4564 PRE_REG_READ3(long, "waitpid",
4565 vki_pid_t, pid, unsigned int *, status, int, options);
4567 if (ARG2 != (Addr)NULL)
4568 PRE_MEM_WRITE( "waitpid(status)", ARG2, sizeof(int) );
4571 POST(sys_waitpid)
4573 if (ARG2 != (Addr)NULL)
4574 POST_MEM_WRITE( ARG2, sizeof(int) );
4577 PRE(sys_wait4)
4579 *flags |= SfMayBlock;
4580 PRINT("sys_wait4 ( %ld, %#" FMT_REGWORD "x, %ld, %#" FMT_REGWORD "x )",
4581 SARG1, ARG2, SARG3, ARG4);
4583 PRE_REG_READ4(long, "wait4",
4584 vki_pid_t, pid, unsigned int *, status, int, options,
4585 struct rusage *, rusage);
4586 if (ARG2 != (Addr)NULL)
4587 PRE_MEM_WRITE( "wait4(status)", ARG2, sizeof(int) );
4588 if (ARG4 != (Addr)NULL)
4589 PRE_MEM_WRITE( "wait4(rusage)", ARG4, sizeof(struct vki_rusage) );
4592 POST(sys_wait4)
4594 if (ARG2 != (Addr)NULL)
4595 POST_MEM_WRITE( ARG2, sizeof(int) );
4596 if (ARG4 != (Addr)NULL)
4597 POST_MEM_WRITE( ARG4, sizeof(struct vki_rusage) );
4600 PRE(sys_writev)
4602 Int i;
4603 struct vki_iovec * vec;
4604 *flags |= SfMayBlock;
4605 PRINT("sys_writev ( %" FMT_REGWORD "u, %#" FMT_REGWORD "x, %"
4606 FMT_REGWORD "u )", ARG1, ARG2, ARG3);
4607 PRE_REG_READ3(ssize_t, "writev",
4608 unsigned long, fd, const struct iovec *, vector,
4609 unsigned long, count);
4610 if (!ML_(fd_allowed)(ARG1, "writev", tid, False)) {
4611 SET_STATUS_Failure( VKI_EBADF );
4612 } else {
4613 if ((Int)ARG3 >= 0)
4614 PRE_MEM_READ( "writev(vector)",
4615 ARG2, ARG3 * sizeof(struct vki_iovec) );
4616 if (ARG2 != 0) {
4617 /* ToDo: don't do any of the following if the vector is invalid */
4618 vec = (struct vki_iovec *)(Addr)ARG2;
4619 for (i = 0; i < (Int)ARG3; i++)
4620 PRE_MEM_READ( "writev(vector[...])",
4621 (Addr)vec[i].iov_base, vec[i].iov_len );
4626 PRE(sys_utimes)
4628 FUSE_COMPATIBLE_MAY_BLOCK();
4629 PRINT("sys_utimes ( %#" FMT_REGWORD "x(%s), %#" FMT_REGWORD "x )",
4630 ARG1, (char*)(Addr)ARG1, ARG2);
4631 PRE_REG_READ2(long, "utimes", char *, filename, struct timeval *, tvp);
4632 PRE_MEM_RASCIIZ( "utimes(filename)", ARG1 );
4633 if (ARG2 != 0) {
4634 PRE_timeval_READ( "utimes(tvp[0])", (Addr)ARG2 );
4635 PRE_timeval_READ( "utimes(tvp[1])",
4636 (Addr)ARG2+sizeof(struct vki_timeval) );
4640 PRE(sys_acct)
4642 PRINT("sys_acct ( %#" FMT_REGWORD "x(%s) )", ARG1,(char*)(Addr)ARG1);
4643 PRE_REG_READ1(long, "acct", const char *, filename);
4644 PRE_MEM_RASCIIZ( "acct(filename)", ARG1 );
4647 PRE(sys_pause)
4649 *flags |= SfMayBlock;
4650 PRINT("sys_pause ( )");
4651 PRE_REG_READ0(long, "pause");
4654 PRE(sys_sigaltstack)
4656 PRINT("sigaltstack ( %#" FMT_REGWORD "x, %#" FMT_REGWORD "x )",ARG1,ARG2);
4657 PRE_REG_READ2(int, "sigaltstack",
4658 const vki_stack_t *, ss, vki_stack_t *, oss);
4659 if (ARG1 != 0) {
4660 const vki_stack_t *ss = (vki_stack_t *)(Addr)ARG1;
4661 PRE_MEM_READ( "sigaltstack(ss)", (Addr)&ss->ss_sp, sizeof(ss->ss_sp) );
4662 PRE_MEM_READ( "sigaltstack(ss)", (Addr)&ss->ss_flags, sizeof(ss->ss_flags) );
4663 PRE_MEM_READ( "sigaltstack(ss)", (Addr)&ss->ss_size, sizeof(ss->ss_size) );
4665 if (ARG2 != 0) {
4666 PRE_MEM_WRITE( "sigaltstack(oss)", ARG2, sizeof(vki_stack_t) );
4669 /* Be safe. */
4670 if (ARG1 && !ML_(safe_to_deref((void*)(Addr)ARG1, sizeof(vki_stack_t)))) {
4671 SET_STATUS_Failure(VKI_EFAULT);
4672 return;
4674 if (ARG2 && !ML_(safe_to_deref((void*)(Addr)ARG2, sizeof(vki_stack_t)))) {
4675 SET_STATUS_Failure(VKI_EFAULT);
4676 return;
4679 SET_STATUS_from_SysRes(
4680 VG_(do_sys_sigaltstack) (tid, (vki_stack_t*)(Addr)ARG1,
4681 (vki_stack_t*)(Addr)ARG2)
4684 POST(sys_sigaltstack)
4686 vg_assert(SUCCESS);
4687 if (RES == 0 && ARG2 != 0)
4688 POST_MEM_WRITE( ARG2, sizeof(vki_stack_t));
4691 PRE(sys_sethostname)
4693 PRINT("sys_sethostname ( %#" FMT_REGWORD "x, %ld )", ARG1, SARG2);
4694 PRE_REG_READ2(long, "sethostname", char *, name, int, len);
4695 PRE_MEM_READ( "sethostname(name)", ARG1, ARG2 );
4698 #undef PRE
4699 #undef POST
4701 #endif // defined(VGO_linux) || defined(VGO_darwin) || defined(VGO_solaris)
4703 /*--------------------------------------------------------------------*/
4704 /*--- end ---*/
4705 /*--------------------------------------------------------------------*/