1 /* -*- mode: C; c-basic-offset: 3; -*- */
3 /*--------------------------------------------------------------------*/
4 /*--- Wrappers for generic Unix system calls ---*/
5 /*--- syswrap-generic.c ---*/
6 /*--------------------------------------------------------------------*/
9 This file is part of Valgrind, a dynamic binary instrumentation
12 Copyright (C) 2000-2017 Julian Seward
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"
68 void ML_(guess_and_register_stack
) (Addr sp
, ThreadState
* tst
)
73 /* We don't really know where the client stack is, because its
74 allocated by the client. The best we can do is look at the
75 memory mappings and try to derive some useful information. We
76 assume that sp starts near its highest possible value, and can
77 only go down to the start of the mmaped segment. */
78 seg
= VG_(am_find_nsegment
)(sp
);
80 && VG_(am_is_valid_for_client
)(sp
, 1, VKI_PROT_READ
| VKI_PROT_WRITE
)) {
81 tst
->client_stack_highest_byte
= (Addr
)VG_PGROUNDUP(sp
)-1;
82 tst
->client_stack_szB
= tst
->client_stack_highest_byte
- seg
->start
+ 1;
85 = VG_(register_stack
)(seg
->start
, tst
->client_stack_highest_byte
);
88 VG_(printf
)("tid %u: guessed client stack range [%#lx-%#lx]"
90 tst
->tid
, seg
->start
, tst
->client_stack_highest_byte
,
91 tst
->os_state
.stk_id
);
93 VG_(message
)(Vg_UserMsg
,
94 "!? New thread %u starts with SP(%#lx) unmapped\n",
96 tst
->client_stack_highest_byte
= 0;
97 tst
->client_stack_szB
= 0;
101 /* Returns True iff address range is something the client can
102 plausibly mess with: all of it is either already belongs to the
103 client or is free or a reservation. */
105 Bool
ML_(valid_client_addr
)(Addr start
, SizeT size
, ThreadId tid
,
106 const HChar
*syscallname
)
113 ret
= VG_(am_is_valid_for_client_or_free_or_resvn
)
114 (start
,size
,VKI_PROT_NONE
);
117 VG_(printf
)("%s: test=%#lx-%#lx ret=%d\n",
118 syscallname
, start
, start
+size
-1, (Int
)ret
);
120 if (!ret
&& syscallname
!= NULL
) {
121 VG_(message
)(Vg_UserMsg
, "Warning: client syscall %s tried "
122 "to modify addresses %#lx-%#lx\n",
123 syscallname
, start
, start
+size
-1);
124 if (VG_(clo_verbosity
) > 1) {
125 VG_(get_and_pp_StackTrace
)(tid
, VG_(clo_backtrace_size
));
133 Bool
ML_(client_signal_OK
)(Int sigNo
)
135 /* signal 0 is OK for kill */
136 Bool ret
= sigNo
>= 0 && sigNo
<= VG_SIGVGRTUSERMAX
;
138 //VG_(printf)("client_signal_OK(%d) -> %d\n", sigNo, ret);
144 /* Handy small function to help stop wrappers from segfaulting when
145 presented with bogus client addresses. Is not used for generating
146 user-visible errors. */
148 Bool
ML_(safe_to_deref
) ( const void *start
, SizeT size
)
150 return VG_(am_is_valid_for_client
)( (Addr
)start
, size
, VKI_PROT_READ
);
154 /* ---------------------------------------------------------------------
156 ------------------------------------------------------------------ */
158 /* AFAICT from kernel sources (mm/mprotect.c) and general experimentation,
159 munmap, mprotect (and mremap??) work at the page level. So addresses
160 and lengths must be adjusted for this. */
162 /* Mash around start and length so that the area exactly covers
163 an integral number of pages. If we don't do that, memcheck's
164 idea of addressible memory diverges from that of the
165 kernel's, which causes the leak detector to crash. */
167 void page_align_addr_and_len( Addr
* a
, SizeT
* len
)
171 ra
= VG_PGROUNDDN(*a
);
172 *len
= VG_PGROUNDUP(*a
+ *len
) - ra
;
176 static void notify_core_of_mmap(Addr a
, SizeT len
, UInt prot
,
177 UInt flags
, Int fd
, Off64T offset
)
181 /* 'a' is the return value from a real kernel mmap, hence: */
182 vg_assert(VG_IS_PAGE_ALIGNED(a
));
183 /* whereas len is whatever the syscall supplied. So: */
184 len
= VG_PGROUNDUP(len
);
186 d
= VG_(am_notify_client_mmap
)( a
, len
, prot
, flags
, fd
, offset
);
189 VG_(discard_translations
)( a
, (ULong
)len
,
190 "notify_core_of_mmap" );
193 static void notify_tool_of_mmap(Addr a
, SizeT len
, UInt prot
, ULong di_handle
)
197 /* 'a' is the return value from a real kernel mmap, hence: */
198 vg_assert(VG_IS_PAGE_ALIGNED(a
));
199 /* whereas len is whatever the syscall supplied. So: */
200 len
= VG_PGROUNDUP(len
);
202 rr
= toBool(prot
& VKI_PROT_READ
);
203 ww
= toBool(prot
& VKI_PROT_WRITE
);
204 xx
= toBool(prot
& VKI_PROT_EXEC
);
206 VG_TRACK( new_mem_mmap
, a
, len
, rr
, ww
, xx
, di_handle
);
210 /* When a client mmap has been successfully done, this function must
211 be called. It notifies both aspacem and the tool of the new
214 JRS 2008-Aug-14: But notice this is *very* obscure. The only place
215 it is called from is POST(sys_io_setup). In particular,
216 ML_(generic_PRE_sys_mmap), in m_syswrap, is the "normal case" handler for
217 client mmap. But it doesn't call this function; instead it does the
218 relevant notifications itself. Here, we just pass di_handle=0 to
219 notify_tool_of_mmap as we have no better information. But really this
220 function should be done away with; problem is I don't understand what
221 POST(sys_io_setup) does or how it works.
223 [However, this function is used lots for Darwin, because
224 ML_(generic_PRE_sys_mmap) cannot be used for Darwin.]
227 ML_(notify_core_and_tool_of_mmap
) ( Addr a
, SizeT len
, UInt prot
,
228 UInt flags
, Int fd
, Off64T offset
)
230 // XXX: unlike the other notify_core_and_tool* functions, this one doesn't
231 // do anything with debug info (ie. it doesn't call VG_(di_notify_mmap)).
233 notify_core_of_mmap(a
, len
, prot
, flags
, fd
, offset
);
234 notify_tool_of_mmap(a
, len
, prot
, 0/*di_handle*/);
238 ML_(notify_core_and_tool_of_munmap
) ( Addr a
, SizeT len
)
242 page_align_addr_and_len(&a
, &len
);
243 d
= VG_(am_notify_munmap
)(a
, len
);
244 VG_TRACK( die_mem_munmap
, a
, len
);
245 VG_(di_notify_munmap
)( a
, len
);
247 VG_(discard_translations
)( a
, (ULong
)len
,
248 "ML_(notify_core_and_tool_of_munmap)" );
252 ML_(notify_core_and_tool_of_mprotect
) ( Addr a
, SizeT len
, Int prot
)
254 Bool rr
= toBool(prot
& VKI_PROT_READ
);
255 Bool ww
= toBool(prot
& VKI_PROT_WRITE
);
256 Bool xx
= toBool(prot
& VKI_PROT_EXEC
);
259 page_align_addr_and_len(&a
, &len
);
260 d
= VG_(am_notify_mprotect
)(a
, len
, prot
);
261 VG_TRACK( change_mem_mprotect
, a
, len
, rr
, ww
, xx
);
262 VG_(di_notify_mprotect
)( a
, len
, prot
);
264 VG_(discard_translations
)( a
, (ULong
)len
,
265 "ML_(notify_core_and_tool_of_mprotect)" );
271 /* Expand (or shrink) an existing mapping, potentially moving it at
272 the same time (controlled by the MREMAP_MAYMOVE flag). Nightmare.
275 SysRes
do_mremap( Addr old_addr
, SizeT old_len
,
276 Addr new_addr
, SizeT new_len
,
277 UWord flags
, ThreadId tid
)
279 # define MIN_SIZET(_aa,_bb) (_aa) < (_bb) ? (_aa) : (_bb)
282 NSegment
const* old_seg
;
284 Bool f_fixed
= toBool(flags
& VKI_MREMAP_FIXED
);
285 Bool f_maymove
= toBool(flags
& VKI_MREMAP_MAYMOVE
);
288 VG_(printf
)("do_remap (old %#lx %lu) (new %#lx %lu) %s %s\n",
289 old_addr
,old_len
,new_addr
,new_len
,
290 flags
& VKI_MREMAP_MAYMOVE
? "MAYMOVE" : "",
291 flags
& VKI_MREMAP_FIXED
? "FIXED" : "");
293 VG_(am_show_nsegments
)(0, "do_remap: before");
295 if (flags
& ~(VKI_MREMAP_FIXED
| VKI_MREMAP_MAYMOVE
))
298 if (!VG_IS_PAGE_ALIGNED(old_addr
))
301 old_len
= VG_PGROUNDUP(old_len
);
302 new_len
= VG_PGROUNDUP(new_len
);
307 /* kernel doesn't reject this, but we do. */
311 /* reject wraparounds */
312 if (old_addr
+ old_len
< old_addr
)
314 if (f_fixed
== True
&& new_addr
+ new_len
< new_len
)
317 /* kernel rejects all fixed, no-move requests (which are
319 if (f_fixed
== True
&& f_maymove
== False
)
322 /* Stay away from non-client areas. */
323 if (!ML_(valid_client_addr
)(old_addr
, old_len
, tid
, "mremap(old_addr)"))
326 /* In all remaining cases, if the old range does not fall within a
327 single segment, fail. */
328 old_seg
= VG_(am_find_nsegment
)( old_addr
);
329 if (old_addr
< old_seg
->start
|| old_addr
+old_len
-1 > old_seg
->end
)
331 if (old_seg
->kind
!= SkAnonC
&& old_seg
->kind
!= SkFileC
332 && old_seg
->kind
!= SkShmC
)
335 vg_assert(old_len
> 0);
336 vg_assert(new_len
> 0);
337 vg_assert(VG_IS_PAGE_ALIGNED(old_len
));
338 vg_assert(VG_IS_PAGE_ALIGNED(new_len
));
339 vg_assert(VG_IS_PAGE_ALIGNED(old_addr
));
341 /* There are 3 remaining cases:
345 new space has to be at old address, so:
346 - shrink -> unmap end
347 - same size -> do nothing
348 - grow -> if can grow in-place, do so, else fail
350 * maymove == True, fixed == False
352 new space can be anywhere, so:
353 - shrink -> unmap end
354 - same size -> do nothing
355 - grow -> if can grow in-place, do so, else
356 move to anywhere large enough, else fail
358 * maymove == True, fixed == True
360 new space must be at new address, so:
362 - if new address is not page aligned, fail
363 - if new address range overlaps old one, fail
364 - if new address range cannot be allocated, fail
365 - else move to new address range with new size
369 if (f_maymove
== False
) {
370 /* new space has to be at old address */
371 if (new_len
< old_len
)
372 goto shrink_in_place
;
373 if (new_len
> old_len
)
374 goto grow_in_place_or_fail
;
378 if (f_maymove
== True
&& f_fixed
== False
) {
379 /* new space can be anywhere */
380 if (new_len
< old_len
)
381 goto shrink_in_place
;
382 if (new_len
> old_len
)
383 goto grow_in_place_or_move_anywhere_or_fail
;
387 if (f_maymove
== True
&& f_fixed
== True
) {
388 /* new space can only be at the new address */
389 if (!VG_IS_PAGE_ALIGNED(new_addr
))
391 if (new_addr
+new_len
-1 < old_addr
|| new_addr
> old_addr
+old_len
-1) {
398 /* VG_(am_get_advisory_client_simple) interprets zero to mean
399 non-fixed, which is not what we want */
400 advised
= VG_(am_get_advisory_client_simple
)(new_addr
, new_len
, &ok
);
401 if (!ok
|| advised
!= new_addr
)
403 ok
= VG_(am_relocate_nooverlap_client
)
404 ( &d
, old_addr
, old_len
, new_addr
, new_len
);
406 VG_TRACK( copy_mem_remap
, old_addr
, new_addr
,
407 MIN_SIZET(old_len
,new_len
) );
408 if (new_len
> old_len
)
409 VG_TRACK( new_mem_mmap
, new_addr
+old_len
, new_len
-old_len
,
410 old_seg
->hasR
, old_seg
->hasW
, old_seg
->hasX
,
412 VG_TRACK(die_mem_munmap
, old_addr
, old_len
);
414 VG_(discard_translations
)( old_addr
, old_len
, "do_remap(1)" );
415 VG_(discard_translations
)( new_addr
, new_len
, "do_remap(2)" );
417 return VG_(mk_SysRes_Success
)( new_addr
);
422 /* end of the 3 cases */
423 /*NOTREACHED*/ vg_assert(0);
425 grow_in_place_or_move_anywhere_or_fail
:
427 /* try growing it in-place */
428 Addr needA
= old_addr
+ old_len
;
429 SSizeT needL
= new_len
- old_len
;
431 vg_assert(needL
> 0);
432 vg_assert(needA
> 0);
434 advised
= VG_(am_get_advisory_client_simple
)( needA
, needL
, &ok
);
436 /* Fixes bug #129866. */
437 ok
= VG_(am_covered_by_single_free_segment
) ( needA
, needL
);
439 if (ok
&& advised
== needA
) {
440 const NSegment
*new_seg
= VG_(am_extend_map_client
)( old_addr
, needL
);
442 VG_TRACK( new_mem_mmap
, needA
, needL
,
444 new_seg
->hasW
, new_seg
->hasX
,
446 return VG_(mk_SysRes_Success
)( old_addr
);
450 /* that failed. Look elsewhere. */
451 advised
= VG_(am_get_advisory_client_simple
)( 0, new_len
, &ok
);
453 Bool oldR
= old_seg
->hasR
;
454 Bool oldW
= old_seg
->hasW
;
455 Bool oldX
= old_seg
->hasX
;
456 /* assert new area does not overlap old */
457 vg_assert(advised
+new_len
-1 < old_addr
458 || advised
> old_addr
+old_len
-1);
459 ok
= VG_(am_relocate_nooverlap_client
)
460 ( &d
, old_addr
, old_len
, advised
, new_len
);
462 VG_TRACK( copy_mem_remap
, old_addr
, advised
,
463 MIN_SIZET(old_len
,new_len
) );
464 if (new_len
> old_len
)
465 VG_TRACK( new_mem_mmap
, advised
+old_len
, new_len
-old_len
,
466 oldR
, oldW
, oldX
, 0/*di_handle*/ );
467 VG_TRACK(die_mem_munmap
, old_addr
, old_len
);
469 VG_(discard_translations
)( old_addr
, old_len
, "do_remap(4)" );
470 VG_(discard_translations
)( advised
, new_len
, "do_remap(5)" );
472 return VG_(mk_SysRes_Success
)( advised
);
477 /*NOTREACHED*/ vg_assert(0);
479 grow_in_place_or_fail
:
481 Addr needA
= old_addr
+ old_len
;
482 SizeT needL
= new_len
- old_len
;
484 vg_assert(needA
> 0);
486 advised
= VG_(am_get_advisory_client_simple
)( needA
, needL
, &ok
);
488 /* Fixes bug #129866. */
489 ok
= VG_(am_covered_by_single_free_segment
) ( needA
, needL
);
491 if (!ok
|| advised
!= needA
)
493 const NSegment
*new_seg
= VG_(am_extend_map_client
)( old_addr
, needL
);
496 VG_TRACK( new_mem_mmap
, needA
, needL
,
497 new_seg
->hasR
, new_seg
->hasW
, new_seg
->hasX
,
500 return VG_(mk_SysRes_Success
)( old_addr
);
502 /*NOTREACHED*/ vg_assert(0);
506 SysRes sres
= VG_(am_munmap_client
)( &d
, old_addr
+new_len
, old_len
-new_len
);
507 if (sr_isError(sres
))
509 VG_TRACK( die_mem_munmap
, old_addr
+new_len
, old_len
-new_len
);
511 VG_(discard_translations
)( old_addr
+new_len
, old_len
-new_len
,
513 return VG_(mk_SysRes_Success
)( old_addr
);
515 /*NOTREACHED*/ vg_assert(0);
518 return VG_(mk_SysRes_Success
)( old_addr
);
519 /*NOTREACHED*/ vg_assert(0);
522 return VG_(mk_SysRes_Error
)( VKI_EINVAL
);
524 return VG_(mk_SysRes_Error
)( VKI_ENOMEM
);
528 #endif /* HAVE_MREMAP */
531 /* ---------------------------------------------------------------------
532 File-descriptor tracking
533 ------------------------------------------------------------------ */
535 /* One of these is allocated for each open file descriptor. */
536 typedef struct OpenFd
538 Int fd
; /* The file descriptor */
539 HChar
*pathname
; /* NULL if not a regular file or unknown */
540 ExeContext
*where
; /* NULL if inherited from parent */
541 struct OpenFd
*next
, *prev
;
544 /* List of allocated file descriptors. */
545 static OpenFd
*allocated_fds
= NULL
;
547 /* Count of open file descriptors. */
548 static Int fd_count
= 0;
551 /* Note the fact that a file descriptor was just closed. */
552 void ML_(record_fd_close
)(Int fd
)
554 OpenFd
*i
= allocated_fds
;
556 if (fd
>= VG_(fd_hard_limit
))
557 return; /* Valgrind internal */
562 i
->prev
->next
= i
->next
;
564 allocated_fds
= i
->next
;
566 i
->next
->prev
= i
->prev
;
568 VG_(free
) (i
->pathname
);
577 /* Note the fact that a file descriptor was just opened. If the
578 tid is -1, this indicates an inherited fd. If the pathname is NULL,
579 this either indicates a non-standard file (i.e. a pipe or socket or
580 some such thing) or that we don't know the filename. If the fd is
581 already open, then we're probably doing a dup2() to an existing fd,
582 so just overwrite the existing one. */
583 void ML_(record_fd_open_with_given_name
)(ThreadId tid
, Int fd
,
584 const HChar
*pathname
)
588 if (fd
>= VG_(fd_hard_limit
))
589 return; /* Valgrind internal */
591 /* Check to see if this fd is already open. */
595 if (i
->pathname
) VG_(free
)(i
->pathname
);
601 /* Not already one: allocate an OpenFd */
603 i
= VG_(malloc
)("syswrap.rfdowgn.1", sizeof(OpenFd
));
606 i
->next
= allocated_fds
;
607 if(allocated_fds
) allocated_fds
->prev
= i
;
613 i
->pathname
= VG_(strdup
)("syswrap.rfdowgn.2", pathname
);
614 i
->where
= (tid
== -1) ? NULL
: VG_(record_ExeContext
)(tid
, 0/*first_ip_delta*/);
617 // Record opening of an fd, and find its name.
618 void ML_(record_fd_open_named
)(ThreadId tid
, Int fd
)
622 if (VG_(resolve_filename
)(fd
, &buf
))
627 ML_(record_fd_open_with_given_name
)(tid
, fd
, name
);
630 // Record opening of a nameless fd.
631 void ML_(record_fd_open_nameless
)(ThreadId tid
, Int fd
)
633 ML_(record_fd_open_with_given_name
)(tid
, fd
, NULL
);
636 // Return if a given file descriptor is already recorded.
637 Bool
ML_(fd_recorded
)(Int fd
)
639 OpenFd
*i
= allocated_fds
;
648 /* Returned string must not be modified nor free'd. */
649 const HChar
*ML_(find_fd_recorded_by_fd
)(Int fd
)
651 OpenFd
*i
= allocated_fds
;
663 HChar
*unix_to_name(struct vki_sockaddr_un
*sa
, UInt len
, HChar
*name
)
665 if (sa
== NULL
|| len
== 0 || sa
->sun_path
[0] == '\0') {
666 VG_(sprintf
)(name
, "<unknown>");
668 VG_(sprintf
)(name
, "%s", sa
->sun_path
);
675 HChar
*inet_to_name(struct vki_sockaddr_in
*sa
, UInt len
, HChar
*name
)
677 if (sa
== NULL
|| len
== 0) {
678 VG_(sprintf
)(name
, "<unknown>");
679 } else if (sa
->sin_port
== 0) {
680 VG_(sprintf
)(name
, "<unbound>");
682 UInt addr
= VG_(ntohl
)(sa
->sin_addr
.s_addr
);
683 VG_(sprintf
)(name
, "%u.%u.%u.%u:%u",
684 (addr
>>24) & 0xFF, (addr
>>16) & 0xFF,
685 (addr
>>8) & 0xFF, addr
& 0xFF,
686 VG_(ntohs
)(sa
->sin_port
));
693 void inet6_format(HChar
*s
, const UChar ip
[16])
695 static const unsigned char V4mappedprefix
[12] = {0,0,0,0,0,0,0,0,0,0,0xff,0xff};
697 if (!VG_(memcmp
)(ip
, V4mappedprefix
, 12)) {
698 const struct vki_in_addr
*sin_addr
=
699 (const struct vki_in_addr
*)(ip
+ 12);
700 UInt addr
= VG_(ntohl
)(sin_addr
->s_addr
);
702 VG_(sprintf
)(s
, "::ffff:%u.%u.%u.%u",
703 (addr
>>24) & 0xFF, (addr
>>16) & 0xFF,
704 (addr
>>8) & 0xFF, addr
& 0xFF);
706 Bool compressing
= False
;
707 Bool compressed
= False
;
711 for (i
= 0; i
< 16; i
+= 2) {
712 UInt word
= ((UInt
)ip
[i
] << 8) | (UInt
)ip
[i
+1];
713 if (word
== 0 && !compressed
) {
724 len
+= VG_(sprintf
)(s
+ len
, "%x", word
);
740 HChar
*inet6_to_name(struct vki_sockaddr_in6
*sa
, UInt len
, HChar
*name
)
742 if (sa
== NULL
|| len
== 0) {
743 VG_(sprintf
)(name
, "<unknown>");
744 } else if (sa
->sin6_port
== 0) {
745 VG_(sprintf
)(name
, "<unbound>");
747 HChar addr
[100]; // large enough
748 inet6_format(addr
, (void *)&(sa
->sin6_addr
));
749 VG_(sprintf
)(name
, "[%s]:%u", addr
, VG_(ntohs
)(sa
->sin6_port
));
756 * Try get some details about a socket.
759 getsockdetails(Int fd
)
762 struct vki_sockaddr a
;
763 struct vki_sockaddr_in in
;
764 struct vki_sockaddr_in6 in6
;
765 struct vki_sockaddr_un un
;
769 llen
= sizeof(laddr
);
770 VG_(memset
)(&laddr
, 0, llen
);
772 if(VG_(getsockname
)(fd
, (struct vki_sockaddr
*)&(laddr
.a
), &llen
) != -1) {
773 switch(laddr
.a
.sa_family
) {
775 HChar lname
[32]; // large enough
776 HChar pname
[32]; // large enough
777 struct vki_sockaddr_in paddr
;
778 Int plen
= sizeof(struct vki_sockaddr_in
);
780 if (VG_(getpeername
)(fd
, (struct vki_sockaddr
*)&paddr
, &plen
) != -1) {
781 VG_(message
)(Vg_UserMsg
, "Open AF_INET socket %d: %s <-> %s\n", fd
,
782 inet_to_name(&(laddr
.in
), llen
, lname
),
783 inet_to_name(&paddr
, plen
, pname
));
785 VG_(message
)(Vg_UserMsg
, "Open AF_INET socket %d: %s <-> unbound\n",
786 fd
, inet_to_name(&(laddr
.in
), llen
, lname
));
791 HChar lname
[128]; // large enough
792 HChar pname
[128]; // large enough
793 struct vki_sockaddr_in6 paddr
;
794 Int plen
= sizeof(struct vki_sockaddr_in6
);
796 if (VG_(getpeername
)(fd
, (struct vki_sockaddr
*)&paddr
, &plen
) != -1) {
797 VG_(message
)(Vg_UserMsg
, "Open AF_INET6 socket %d: %s <-> %s\n", fd
,
798 inet6_to_name(&(laddr
.in6
), llen
, lname
),
799 inet6_to_name(&paddr
, plen
, pname
));
801 VG_(message
)(Vg_UserMsg
, "Open AF_INET6 socket %d: %s <-> unbound\n",
802 fd
, inet6_to_name(&(laddr
.in6
), llen
, lname
));
807 static char lname
[256];
808 VG_(message
)(Vg_UserMsg
, "Open AF_UNIX socket %d: %s\n", fd
,
809 unix_to_name(&(laddr
.un
), llen
, lname
));
813 VG_(message
)(Vg_UserMsg
, "Open pf-%d socket %d:\n",
814 laddr
.a
.sa_family
, fd
);
819 VG_(message
)(Vg_UserMsg
, "Open socket %d:\n", fd
);
823 /* Dump out a summary, and a more detailed list, of open file descriptors. */
824 void VG_(show_open_fds
) (const HChar
* when
)
829 for (i
= allocated_fds
; i
; i
= i
->next
) {
834 /* If we are running quiet and there are either no open file descriptors
835 or not tracking all fds, then don't report anything. */
837 || ((non_std
== 0) && (VG_(clo_track_fds
) < 2)))
838 && (VG_(clo_verbosity
) == 0))
841 VG_(message
)(Vg_UserMsg
, "FILE DESCRIPTORS: %d open (%d std) %s.\n",
842 fd_count
, fd_count
- non_std
, when
);
844 for (i
= allocated_fds
; i
; i
= i
->next
) {
845 if (i
->fd
<= 2 && VG_(clo_track_fds
) < 2)
849 VG_(message
)(Vg_UserMsg
, "Open file descriptor %d: %s\n", i
->fd
,
853 Int len
= sizeof(val
);
855 if (VG_(getsockopt
)(i
->fd
, VKI_SOL_SOCKET
, VKI_SO_TYPE
, &val
, &len
)
857 VG_(message
)(Vg_UserMsg
, "Open file descriptor %d:\n", i
->fd
);
859 getsockdetails(i
->fd
);
864 VG_(pp_ExeContext
)(i
->where
);
865 VG_(message
)(Vg_UserMsg
, "\n");
867 VG_(message
)(Vg_UserMsg
, " <inherited from parent>\n");
868 VG_(message
)(Vg_UserMsg
, "\n");
872 VG_(message
)(Vg_UserMsg
, "\n");
875 /* If /proc/self/fd doesn't exist (e.g. you've got a Linux kernel that doesn't
876 have /proc support compiled in, or a non-Linux kernel), then we need to
877 find out what file descriptors we inherited from our parent process the
878 hard way - by checking each fd in turn. */
880 void init_preopened_fds_without_proc_self_fd(void)
882 struct vki_rlimit lim
;
886 if (VG_(getrlimit
) (VKI_RLIMIT_NOFILE
, &lim
) == -1) {
887 /* Hmm. getrlimit() failed. Now we're screwed, so just choose
888 an arbitrarily high number. 1024 happens to be the limit in
889 the 2.4 Linux kernels. */
892 count
= lim
.rlim_cur
;
895 for (i
= 0; i
< count
; i
++)
896 if (VG_(fcntl
)(i
, VKI_F_GETFL
, 0) != -1)
897 ML_(record_fd_open_named
)(-1, i
);
900 /* Initialize the list of open file descriptors with the file descriptors
901 we inherited from out parent process. */
903 void VG_(init_preopened_fds
)(void)
905 // DDD: should probably use HAVE_PROC here or similar, instead.
906 #if defined(VGO_linux)
908 struct vki_dirent64 d
;
911 f
= VG_(open
)("/proc/self/fd", VKI_O_RDONLY
, 0);
913 init_preopened_fds_without_proc_self_fd();
917 while ((ret
= VG_(getdents64
)(sr_Res(f
), &d
, sizeof(d
))) != 0) {
921 if (VG_(strcmp
)(d
.d_name
, ".") && VG_(strcmp
)(d
.d_name
, "..")) {
923 Int fno
= VG_(strtoll10
)(d
.d_name
, &s
);
925 if (fno
!= sr_Res(f
))
926 if (VG_(clo_track_fds
))
927 ML_(record_fd_open_named
)(-1, fno
);
929 VG_(message
)(Vg_DebugMsg
,
930 "Warning: invalid file name in /proc/self/fd: %s\n",
935 VG_(lseek
)(sr_Res(f
), d
.d_off
, VKI_SEEK_SET
);
939 VG_(close
)(sr_Res(f
));
941 #elif defined(VGO_darwin)
942 init_preopened_fds_without_proc_self_fd();
944 #elif defined(VGO_solaris)
946 Char buf
[VKI_MAXGETDENTS_SIZE
];
949 f
= VG_(open
)("/proc/self/fd", VKI_O_RDONLY
, 0);
951 init_preopened_fds_without_proc_self_fd();
955 while ((ret
= VG_(getdents64
)(sr_Res(f
), (struct vki_dirent64
*) buf
,
959 /* Proceed one entry. */
960 struct vki_dirent64
*d
= (struct vki_dirent64
*) (buf
+ i
);
961 if (VG_(strcmp
)(d
->d_name
, ".") && VG_(strcmp
)(d
->d_name
, "..")) {
963 Int fno
= VG_(strtoll10
)(d
->d_name
, &s
);
965 if (fno
!= sr_Res(f
))
966 if (VG_(clo_track_fds
))
967 ML_(record_fd_open_named
)(-1, fno
);
969 VG_(message
)(Vg_DebugMsg
,
970 "Warning: invalid file name in /proc/self/fd: %s\n",
975 /* Move on the next entry. */
980 VG_(close
)(sr_Res(f
));
988 void pre_mem_read_sendmsg ( ThreadId tid
, Bool read
,
989 const HChar
*msg
, Addr base
, SizeT size
)
991 HChar outmsg
[VG_(strlen
)(msg
) + 10]; // large enough
992 VG_(sprintf
)(outmsg
, "sendmsg%s", msg
);
993 PRE_MEM_READ( outmsg
, base
, size
);
997 void pre_mem_write_recvmsg ( ThreadId tid
, Bool read
,
998 const HChar
*msg
, Addr base
, SizeT size
)
1000 HChar outmsg
[VG_(strlen
)(msg
) + 10]; // large enough
1001 VG_(sprintf
)(outmsg
, "recvmsg%s", msg
);
1003 PRE_MEM_READ( outmsg
, base
, size
);
1005 PRE_MEM_WRITE( outmsg
, base
, size
);
1009 void post_mem_write_recvmsg ( ThreadId tid
, Bool read
,
1010 const HChar
*fieldName
, Addr base
, SizeT size
)
1013 POST_MEM_WRITE( base
, size
);
1017 void msghdr_foreachfield (
1020 struct vki_msghdr
*msg
,
1022 void (*foreach_func
)( ThreadId
, Bool
, const HChar
*, Addr
, SizeT
),
1023 Bool rekv
/* "recv" apparently shadows some header decl on OSX108 */
1026 HChar fieldName
[VG_(strlen
)(name
) + 32]; // large enough.
1033 VG_(sprintf
) ( fieldName
, "(%s)", name
);
1035 /* FIELDPAIR helps the compiler do one call to foreach_func
1036 for consecutive (no holes) fields. */
1037 #define FIELDPAIR(f1,f2) \
1038 if (offsetof(struct vki_msghdr, f1) + sizeof(msg->f1) \
1039 == offsetof(struct vki_msghdr, f2)) \
1040 s += sizeof(msg->f2); \
1042 foreach_func (tid, True, fieldName, a, s); \
1043 a = (Addr)&msg->f2; \
1044 s = sizeof(msg->f2); \
1047 a
= (Addr
)&msg
->msg_name
;
1048 s
= sizeof(msg
->msg_name
);
1049 FIELDPAIR(msg_name
, msg_namelen
);
1050 FIELDPAIR(msg_namelen
, msg_iov
);
1051 FIELDPAIR(msg_iov
, msg_iovlen
);
1052 FIELDPAIR(msg_iovlen
, msg_control
);
1053 FIELDPAIR(msg_control
, msg_controllen
);
1054 foreach_func ( tid
, True
, fieldName
, a
, s
);
1057 /* msg_flags is completely ignored for send_mesg, recv_mesg doesn't read
1058 the field, but does write to it. */
1060 foreach_func ( tid
, False
, fieldName
, (Addr
)&msg
->msg_flags
, sizeof( msg
->msg_flags
) );
1062 if ( ML_(safe_to_deref
)(&msg
->msg_name
, sizeof (void *))
1063 && msg
->msg_name
) {
1064 VG_(sprintf
) ( fieldName
, "(%s.msg_name)", name
);
1065 foreach_func ( tid
, False
, fieldName
,
1066 (Addr
)msg
->msg_name
, msg
->msg_namelen
);
1069 if ( ML_(safe_to_deref
)(&msg
->msg_iov
, sizeof (void *))
1071 struct vki_iovec
*iov
= msg
->msg_iov
;
1074 if (ML_(safe_to_deref
)(&msg
->msg_iovlen
, sizeof (UInt
))) {
1075 VG_(sprintf
) ( fieldName
, "(%s.msg_iov)", name
);
1076 foreach_func ( tid
, True
, fieldName
, (Addr
)iov
,
1077 msg
->msg_iovlen
* sizeof( struct vki_iovec
) );
1079 for ( i
= 0; i
< msg
->msg_iovlen
&& length
> 0; ++i
, ++iov
) {
1080 if (ML_(safe_to_deref
)(&iov
->iov_len
, sizeof (UInt
))) {
1081 UInt iov_len
= iov
->iov_len
<= length
? iov
->iov_len
: length
;
1082 VG_(sprintf
) ( fieldName
, "(%s.msg_iov[%u])", name
, i
);
1083 foreach_func ( tid
, False
, fieldName
,
1084 (Addr
)iov
->iov_base
, iov_len
);
1085 length
= length
- iov_len
;
1091 if ( ML_(safe_to_deref
) (&msg
->msg_control
, sizeof (void *))
1092 && msg
->msg_control
) {
1093 VG_(sprintf
) ( fieldName
, "(%s.msg_control)", name
);
1094 foreach_func ( tid
, False
, fieldName
,
1095 (Addr
)msg
->msg_control
, msg
->msg_controllen
);
1100 static void check_cmsg_for_fds(ThreadId tid
, struct vki_msghdr
*msg
)
1102 struct vki_cmsghdr
*cm
= VKI_CMSG_FIRSTHDR(msg
);
1105 if (cm
->cmsg_level
== VKI_SOL_SOCKET
1106 && cm
->cmsg_type
== VKI_SCM_RIGHTS
) {
1107 Int
*fds
= (Int
*) VKI_CMSG_DATA(cm
);
1108 Int fdc
= (cm
->cmsg_len
- VKI_CMSG_ALIGN(sizeof(struct vki_cmsghdr
)))
1112 for (i
= 0; i
< fdc
; i
++)
1113 if(VG_(clo_track_fds
))
1114 // XXX: must we check the range on these fds with
1115 // ML_(fd_allowed)()?
1116 ML_(record_fd_open_named
)(tid
, fds
[i
]);
1119 cm
= VKI_CMSG_NXTHDR(msg
, cm
);
1123 /* GrP kernel ignores sa_len (at least on Darwin); this checks the rest */
1125 void pre_mem_read_sockaddr ( ThreadId tid
,
1126 const HChar
*description
,
1127 struct vki_sockaddr
*sa
, UInt salen
)
1129 HChar outmsg
[VG_(strlen
)( description
) + 30]; // large enough
1130 struct vki_sockaddr_un
* saun
= (struct vki_sockaddr_un
*)sa
;
1131 struct vki_sockaddr_in
* sin
= (struct vki_sockaddr_in
*)sa
;
1132 struct vki_sockaddr_in6
* sin6
= (struct vki_sockaddr_in6
*)sa
;
1133 # ifdef VKI_AF_BLUETOOTH
1134 struct vki_sockaddr_rc
* rc
= (struct vki_sockaddr_rc
*)sa
;
1136 # ifdef VKI_AF_NETLINK
1137 struct vki_sockaddr_nl
* nl
= (struct vki_sockaddr_nl
*)sa
;
1140 /* NULL/zero-length sockaddrs are legal */
1141 if ( sa
== NULL
|| salen
== 0 ) return;
1143 VG_(sprintf
) ( outmsg
, description
, "sa_family" );
1144 PRE_MEM_READ( outmsg
, (Addr
) &sa
->sa_family
, sizeof(vki_sa_family_t
));
1146 /* Don't do any extra checking if we cannot determine the sa_family. */
1147 if (! ML_(safe_to_deref
) (&sa
->sa_family
, sizeof(vki_sa_family_t
)))
1150 switch (sa
->sa_family
) {
1153 if (ML_(safe_to_deref
) (&saun
->sun_path
, sizeof (Addr
))) {
1154 VG_(sprintf
) ( outmsg
, description
, "sun_path" );
1155 PRE_MEM_RASCIIZ( outmsg
, (Addr
) saun
->sun_path
);
1156 // GrP fixme max of sun_len-2? what about nul char?
1161 VG_(sprintf
) ( outmsg
, description
, "sin_port" );
1162 PRE_MEM_READ( outmsg
, (Addr
) &sin
->sin_port
, sizeof (sin
->sin_port
) );
1163 VG_(sprintf
) ( outmsg
, description
, "sin_addr" );
1164 PRE_MEM_READ( outmsg
, (Addr
) &sin
->sin_addr
, sizeof (sin
->sin_addr
) );
1168 VG_(sprintf
) ( outmsg
, description
, "sin6_port" );
1169 PRE_MEM_READ( outmsg
,
1170 (Addr
) &sin6
->sin6_port
, sizeof (sin6
->sin6_port
) );
1171 VG_(sprintf
) ( outmsg
, description
, "sin6_flowinfo" );
1172 PRE_MEM_READ( outmsg
,
1173 (Addr
) &sin6
->sin6_flowinfo
, sizeof (sin6
->sin6_flowinfo
) );
1174 VG_(sprintf
) ( outmsg
, description
, "sin6_addr" );
1175 PRE_MEM_READ( outmsg
,
1176 (Addr
) &sin6
->sin6_addr
, sizeof (sin6
->sin6_addr
) );
1177 VG_(sprintf
) ( outmsg
, description
, "sin6_scope_id" );
1178 PRE_MEM_READ( outmsg
,
1179 (Addr
) &sin6
->sin6_scope_id
, sizeof (sin6
->sin6_scope_id
) );
1182 # ifdef VKI_AF_BLUETOOTH
1183 case VKI_AF_BLUETOOTH
:
1184 VG_(sprintf
) ( outmsg
, description
, "rc_bdaddr" );
1185 PRE_MEM_READ( outmsg
, (Addr
) &rc
->rc_bdaddr
, sizeof (rc
->rc_bdaddr
) );
1186 VG_(sprintf
) ( outmsg
, description
, "rc_channel" );
1187 PRE_MEM_READ( outmsg
, (Addr
) &rc
->rc_channel
, sizeof (rc
->rc_channel
) );
1191 # ifdef VKI_AF_NETLINK
1192 case VKI_AF_NETLINK
:
1193 VG_(sprintf
)(outmsg
, description
, "nl_pid");
1194 PRE_MEM_READ(outmsg
, (Addr
)&nl
->nl_pid
, sizeof(nl
->nl_pid
));
1195 VG_(sprintf
)(outmsg
, description
, "nl_groups");
1196 PRE_MEM_READ(outmsg
, (Addr
)&nl
->nl_groups
, sizeof(nl
->nl_groups
));
1200 # ifdef VKI_AF_UNSPEC
1206 /* No specific information about this address family.
1207 Let's just check the full data following the family.
1208 Note that this can give false positive if this (unknown)
1209 struct sockaddr_???? has padding bytes between its elements. */
1210 VG_(sprintf
) ( outmsg
, description
, "sa_data" );
1211 PRE_MEM_READ( outmsg
, (Addr
)&sa
->sa_family
+ sizeof(sa
->sa_family
),
1212 salen
- sizeof(sa
->sa_family
));
1217 /* Dereference a pointer to a UInt. */
1218 static UInt
deref_UInt ( ThreadId tid
, Addr a
, const HChar
* s
)
1220 UInt
* a_p
= (UInt
*)a
;
1221 PRE_MEM_READ( s
, (Addr
)a_p
, sizeof(UInt
) );
1222 if (a_p
== NULL
|| ! ML_(safe_to_deref
) (a_p
, sizeof(UInt
)))
1228 void ML_(buf_and_len_pre_check
) ( ThreadId tid
, Addr buf_p
, Addr buflen_p
,
1229 const HChar
* buf_s
, const HChar
* buflen_s
)
1231 if (VG_(tdict
).track_pre_mem_write
) {
1232 UInt buflen_in
= deref_UInt( tid
, buflen_p
, buflen_s
);
1233 if (buflen_in
> 0) {
1234 VG_(tdict
).track_pre_mem_write(
1235 Vg_CoreSysCall
, tid
, buf_s
, buf_p
, buflen_in
);
1240 void ML_(buf_and_len_post_check
) ( ThreadId tid
, SysRes res
,
1241 Addr buf_p
, Addr buflen_p
, const HChar
* s
)
1243 if (!sr_isError(res
) && VG_(tdict
).track_post_mem_write
) {
1244 UInt buflen_out
= deref_UInt( tid
, buflen_p
, s
);
1245 if (buflen_out
> 0 && buf_p
!= (Addr
)NULL
) {
1246 VG_(tdict
).track_post_mem_write( Vg_CoreSysCall
, tid
, buf_p
, buflen_out
);
1251 /* ---------------------------------------------------------------------
1252 Data seg end, for brk()
1253 ------------------------------------------------------------------ */
1255 /* +--------+------------+
1257 +--------+------------+
1260 | | boundary is page aligned
1261 | VG_(brk_limit) -- no alignment constraint
1262 VG_(brk_base) -- page aligned -- does not move
1264 Both the anon part and the reservation part are always at least
1268 /* Set the new data segment end to NEWBRK. If this succeeds, return
1269 NEWBRK, else return the current data segment end. */
1271 static Addr
do_brk ( Addr newbrk
, ThreadId tid
)
1273 NSegment
const* aseg
;
1279 VG_(printf
)("\ndo_brk: brk_base=%#lx brk_limit=%#lx newbrk=%#lx\n",
1280 VG_(brk_base
), VG_(brk_limit
), newbrk
);
1282 if (0) VG_(am_show_nsegments
)(0, "in_brk");
1284 if (newbrk
< VG_(brk_base
))
1285 /* Clearly impossible. */
1288 if (newbrk
< VG_(brk_limit
)) {
1289 /* shrinking the data segment. Be lazy and don't munmap the
1291 NSegment
const * seg
= VG_(am_find_nsegment
)(newbrk
);
1295 VG_(discard_translations
)( newbrk
, VG_(brk_limit
) - newbrk
,
1297 /* Since we're being lazy and not unmapping pages, we have to
1298 zero out the area, so that if the area later comes back into
1299 circulation, it will be filled with zeroes, as if it really
1300 had been unmapped and later remapped. Be a bit paranoid and
1301 try hard to ensure we're not going to segfault by doing the
1302 write - check both ends of the range are in the same segment
1303 and that segment is writable. */
1304 NSegment
const * seg2
;
1306 seg2
= VG_(am_find_nsegment
)( VG_(brk_limit
) - 1 );
1309 if (seg
== seg2
&& seg
->hasW
)
1310 VG_(memset
)( (void*)newbrk
, 0, VG_(brk_limit
) - newbrk
);
1312 VG_(brk_limit
) = newbrk
;
1316 /* otherwise we're expanding the brk segment. */
1317 if (VG_(brk_limit
) > VG_(brk_base
))
1318 aseg
= VG_(am_find_nsegment
)( VG_(brk_limit
)-1 );
1320 aseg
= VG_(am_find_nsegment
)( VG_(brk_limit
) );
1322 /* These should be assured by setup_client_dataseg in m_main. */
1324 vg_assert(aseg
->kind
== SkAnonC
);
1326 if (newbrk
<= aseg
->end
+ 1) {
1327 /* still fits within the anon segment. */
1328 VG_(brk_limit
) = newbrk
;
1332 newbrkP
= VG_PGROUNDUP(newbrk
);
1333 delta
= newbrkP
- (aseg
->end
+ 1);
1334 vg_assert(delta
> 0);
1335 vg_assert(VG_IS_PAGE_ALIGNED(delta
));
1337 Bool overflow
= False
;
1338 if (! VG_(am_extend_into_adjacent_reservation_client
)( aseg
->start
, delta
,
1341 static Bool alreadyComplained
= False
;
1342 if (!alreadyComplained
) {
1343 alreadyComplained
= True
;
1344 if (VG_(clo_verbosity
) > 0) {
1345 VG_(umsg
)("brk segment overflow in thread #%u: "
1346 "can't grow to %#lx\n",
1348 VG_(umsg
)("(see section Limitations in user manual)\n");
1349 VG_(umsg
)("NOTE: further instances of this message "
1350 "will not be shown\n");
1354 if (VG_(clo_verbosity
) > 0) {
1355 VG_(umsg
)("Cannot map memory to grow brk segment in thread #%u "
1356 "to %#lx\n", tid
, newbrkP
);
1357 VG_(umsg
)("(see section Limitations in user manual)\n");
1363 VG_(brk_limit
) = newbrk
;
1367 return VG_(brk_limit
);
1371 /* ---------------------------------------------------------------------
1372 Vet file descriptors for sanity
1373 ------------------------------------------------------------------ */
1375 > - what does the "Bool soft" parameter mean?
1377 (Tom Hughes, 3 Oct 05):
1379 Whether or not to consider a file descriptor invalid if it is above
1380 the current soft limit.
1382 Basically if we are testing whether a newly created file descriptor is
1383 valid (in a post handler) then we set soft to true, and if we are
1384 testing whether a file descriptor that is about to be used (in a pre
1385 handler) is valid [viz, an already-existing fd] then we set it to false.
1387 The point is that if the (virtual) soft limit is lowered then any
1388 existing descriptors can still be read/written/closed etc (so long as
1389 they are below the valgrind reserved descriptors) but no new
1390 descriptors can be created above the new soft limit.
1392 (jrs 4 Oct 05: in which case, I've renamed it "isNewFd")
1395 /* Return true if we're allowed to use or create this fd */
1396 Bool
ML_(fd_allowed
)(Int fd
, const HChar
*syscallname
, ThreadId tid
,
1399 Bool allowed
= True
;
1401 /* hard limits always apply */
1402 if (fd
< 0 || fd
>= VG_(fd_hard_limit
))
1405 /* hijacking the output fds is never allowed */
1406 if (fd
== VG_(log_output_sink
).fd
|| fd
== VG_(xml_output_sink
).fd
)
1409 /* if creating a new fd (rather than using an existing one), the
1410 soft limit must also be observed */
1411 if (isNewFd
&& fd
>= VG_(fd_soft_limit
))
1414 /* this looks like it ought to be included, but causes problems: */
1416 if (fd == 2 && VG_(debugLog_getLevel)() > 0)
1419 /* The difficulty is as follows: consider a program P which expects
1420 to be able to mess with (redirect) its own stderr (fd 2).
1421 Usually to deal with P we would issue command line flags to send
1422 logging somewhere other than stderr, so as not to disrupt P.
1423 The problem is that -d unilaterally hijacks stderr with no
1424 consultation with P. And so, if this check is enabled, P will
1425 work OK normally but fail if -d is issued.
1427 Basically -d is a hack and you take your chances when using it.
1428 It's very useful for low level debugging -- particularly at
1429 startup -- and having its presence change the behaviour of the
1430 client is exactly what we don't want. */
1433 if ((!allowed
) && VG_(showing_core_errors
)() ) {
1434 VG_(message
)(Vg_UserMsg
,
1435 "Warning: invalid file descriptor %d in syscall %s()\n",
1437 if (fd
== VG_(log_output_sink
).fd
&& VG_(log_output_sink
).fd
>= 0)
1438 VG_(message
)(Vg_UserMsg
,
1439 " Use --log-fd=<number> to select an alternative log fd.\n");
1440 if (fd
== VG_(xml_output_sink
).fd
&& VG_(xml_output_sink
).fd
>= 0)
1441 VG_(message
)(Vg_UserMsg
,
1442 " Use --xml-fd=<number> to select an alternative XML "
1444 // DDD: consider always printing this stack trace, it's useful.
1445 // Also consider also making this a proper core error, ie.
1446 // suppressible and all that.
1447 if (VG_(clo_verbosity
) > 1) {
1448 VG_(get_and_pp_StackTrace
)(tid
, VG_(clo_backtrace_size
));
1456 /* ---------------------------------------------------------------------
1457 Deal with a bunch of socket-related syscalls
1458 ------------------------------------------------------------------ */
1463 ML_(generic_PRE_sys_socketpair
) ( ThreadId tid
,
1464 UWord arg0
, UWord arg1
,
1465 UWord arg2
, UWord arg3
)
1467 /* int socketpair(int d, int type, int protocol, int sv[2]); */
1468 PRE_MEM_WRITE( "socketcall.socketpair(sv)",
1469 arg3
, 2*sizeof(int) );
1473 ML_(generic_POST_sys_socketpair
) ( ThreadId tid
,
1475 UWord arg0
, UWord arg1
,
1476 UWord arg2
, UWord arg3
)
1479 Int fd1
= ((Int
*)arg3
)[0];
1480 Int fd2
= ((Int
*)arg3
)[1];
1481 vg_assert(!sr_isError(res
)); /* guaranteed by caller */
1482 POST_MEM_WRITE( arg3
, 2*sizeof(int) );
1483 if (!ML_(fd_allowed
)(fd1
, "socketcall.socketpair", tid
, True
) ||
1484 !ML_(fd_allowed
)(fd2
, "socketcall.socketpair", tid
, True
)) {
1487 r
= VG_(mk_SysRes_Error
)( VKI_EMFILE
);
1489 POST_MEM_WRITE( arg3
, 2*sizeof(int) );
1490 if (VG_(clo_track_fds
)) {
1491 ML_(record_fd_open_nameless
)(tid
, fd1
);
1492 ML_(record_fd_open_nameless
)(tid
, fd2
);
1501 ML_(generic_POST_sys_socket
) ( ThreadId tid
, SysRes res
)
1504 vg_assert(!sr_isError(res
)); /* guaranteed by caller */
1505 if (!ML_(fd_allowed
)(sr_Res(res
), "socket", tid
, True
)) {
1506 VG_(close
)(sr_Res(res
));
1507 r
= VG_(mk_SysRes_Error
)( VKI_EMFILE
);
1509 if (VG_(clo_track_fds
))
1510 ML_(record_fd_open_nameless
)(tid
, sr_Res(res
));
1518 ML_(generic_PRE_sys_bind
) ( ThreadId tid
,
1519 UWord arg0
, UWord arg1
, UWord arg2
)
1521 /* int bind(int sockfd, struct sockaddr *my_addr,
1523 pre_mem_read_sockaddr(
1524 tid
, "socketcall.bind(my_addr.%s)",
1525 (struct vki_sockaddr
*) arg1
, arg2
1532 ML_(generic_PRE_sys_accept
) ( ThreadId tid
,
1533 UWord arg0
, UWord arg1
, UWord arg2
)
1535 /* int accept(int s, struct sockaddr *addr, int *addrlen); */
1537 Addr addrlen_p
= arg2
;
1538 if (addr_p
!= (Addr
)NULL
)
1539 ML_(buf_and_len_pre_check
) ( tid
, addr_p
, addrlen_p
,
1540 "socketcall.accept(addr)",
1541 "socketcall.accept(addrlen_in)" );
1545 ML_(generic_POST_sys_accept
) ( ThreadId tid
,
1547 UWord arg0
, UWord arg1
, UWord arg2
)
1550 vg_assert(!sr_isError(res
)); /* guaranteed by caller */
1551 if (!ML_(fd_allowed
)(sr_Res(res
), "accept", tid
, True
)) {
1552 VG_(close
)(sr_Res(res
));
1553 r
= VG_(mk_SysRes_Error
)( VKI_EMFILE
);
1556 Addr addrlen_p
= arg2
;
1557 if (addr_p
!= (Addr
)NULL
)
1558 ML_(buf_and_len_post_check
) ( tid
, res
, addr_p
, addrlen_p
,
1559 "socketcall.accept(addrlen_out)" );
1560 if (VG_(clo_track_fds
))
1561 ML_(record_fd_open_nameless
)(tid
, sr_Res(res
));
1569 ML_(generic_PRE_sys_sendto
) ( ThreadId tid
,
1570 UWord arg0
, UWord arg1
, UWord arg2
,
1571 UWord arg3
, UWord arg4
, UWord arg5
)
1573 /* int sendto(int s, const void *msg, int len,
1575 const struct sockaddr *to, int tolen); */
1576 PRE_MEM_READ( "socketcall.sendto(msg)",
1579 pre_mem_read_sockaddr(
1580 tid
, "socketcall.sendto(to.%s)",
1581 (struct vki_sockaddr
*) arg4
, arg5
1588 ML_(generic_PRE_sys_send
) ( ThreadId tid
,
1589 UWord arg0
, UWord arg1
, UWord arg2
)
1591 /* int send(int s, const void *msg, size_t len, int flags); */
1592 PRE_MEM_READ( "socketcall.send(msg)",
1601 ML_(generic_PRE_sys_recvfrom
) ( ThreadId tid
,
1602 UWord arg0
, UWord arg1
, UWord arg2
,
1603 UWord arg3
, UWord arg4
, UWord arg5
)
1605 /* int recvfrom(int s, void *buf, int len, unsigned int flags,
1606 struct sockaddr *from, int *fromlen); */
1610 Addr fromlen_p
= arg5
;
1611 PRE_MEM_WRITE( "socketcall.recvfrom(buf)", buf_p
, len
);
1612 if (from_p
!= (Addr
)NULL
)
1613 ML_(buf_and_len_pre_check
) ( tid
, from_p
, fromlen_p
,
1614 "socketcall.recvfrom(from)",
1615 "socketcall.recvfrom(fromlen_in)" );
1619 ML_(generic_POST_sys_recvfrom
) ( ThreadId tid
,
1621 UWord arg0
, UWord arg1
, UWord arg2
,
1622 UWord arg3
, UWord arg4
, UWord arg5
)
1627 Addr fromlen_p
= arg5
;
1629 vg_assert(!sr_isError(res
)); /* guaranteed by caller */
1630 if (from_p
!= (Addr
)NULL
)
1631 ML_(buf_and_len_post_check
) ( tid
, res
, from_p
, fromlen_p
,
1632 "socketcall.recvfrom(fromlen_out)" );
1633 POST_MEM_WRITE( buf_p
, len
);
1639 ML_(generic_PRE_sys_recv
) ( ThreadId tid
,
1640 UWord arg0
, UWord arg1
, UWord arg2
)
1642 /* int recv(int s, void *buf, int len, unsigned int flags); */
1644 The recv call is normally used only on a connected socket
1645 (see connect(2)) and is identical to recvfrom with a NULL
1648 PRE_MEM_WRITE( "socketcall.recv(buf)",
1654 ML_(generic_POST_sys_recv
) ( ThreadId tid
,
1656 UWord arg0
, UWord arg1
, UWord arg2
)
1658 if (res
>= 0 && arg1
!= 0) {
1659 POST_MEM_WRITE( arg1
, /* buf */
1667 ML_(generic_PRE_sys_connect
) ( ThreadId tid
,
1668 UWord arg0
, UWord arg1
, UWord arg2
)
1670 /* int connect(int sockfd,
1671 struct sockaddr *serv_addr, int addrlen ); */
1672 pre_mem_read_sockaddr( tid
,
1673 "socketcall.connect(serv_addr.%s)",
1674 (struct vki_sockaddr
*) arg1
, arg2
);
1680 ML_(generic_PRE_sys_setsockopt
) ( ThreadId tid
,
1681 UWord arg0
, UWord arg1
, UWord arg2
,
1682 UWord arg3
, UWord arg4
)
1684 /* int setsockopt(int s, int level, int optname,
1685 const void *optval, int optlen); */
1686 PRE_MEM_READ( "socketcall.setsockopt(optval)",
1688 arg4
/* optlen */ );
1694 ML_(generic_PRE_sys_getsockname
) ( ThreadId tid
,
1695 UWord arg0
, UWord arg1
, UWord arg2
)
1697 /* int getsockname(int s, struct sockaddr* name, int* namelen) */
1699 Addr namelen_p
= arg2
;
1700 /* Nb: name_p cannot be NULL */
1701 ML_(buf_and_len_pre_check
) ( tid
, name_p
, namelen_p
,
1702 "socketcall.getsockname(name)",
1703 "socketcall.getsockname(namelen_in)" );
1707 ML_(generic_POST_sys_getsockname
) ( ThreadId tid
,
1709 UWord arg0
, UWord arg1
, UWord arg2
)
1712 Addr namelen_p
= arg2
;
1713 vg_assert(!sr_isError(res
)); /* guaranteed by caller */
1714 ML_(buf_and_len_post_check
) ( tid
, res
, name_p
, namelen_p
,
1715 "socketcall.getsockname(namelen_out)" );
1721 ML_(generic_PRE_sys_getpeername
) ( ThreadId tid
,
1722 UWord arg0
, UWord arg1
, UWord arg2
)
1724 /* int getpeername(int s, struct sockaddr* name, int* namelen) */
1726 Addr namelen_p
= arg2
;
1727 /* Nb: name_p cannot be NULL */
1728 ML_(buf_and_len_pre_check
) ( tid
, name_p
, namelen_p
,
1729 "socketcall.getpeername(name)",
1730 "socketcall.getpeername(namelen_in)" );
1734 ML_(generic_POST_sys_getpeername
) ( ThreadId tid
,
1736 UWord arg0
, UWord arg1
, UWord arg2
)
1739 Addr namelen_p
= arg2
;
1740 vg_assert(!sr_isError(res
)); /* guaranteed by caller */
1741 ML_(buf_and_len_post_check
) ( tid
, res
, name_p
, namelen_p
,
1742 "socketcall.getpeername(namelen_out)" );
1748 ML_(generic_PRE_sys_sendmsg
) ( ThreadId tid
, const HChar
*name
,
1749 struct vki_msghdr
*msg
)
1751 msghdr_foreachfield ( tid
, name
, msg
, ~0, pre_mem_read_sendmsg
, False
);
1757 ML_(generic_PRE_sys_recvmsg
) ( ThreadId tid
, const HChar
*name
,
1758 struct vki_msghdr
*msg
)
1760 msghdr_foreachfield ( tid
, name
, msg
, ~0, pre_mem_write_recvmsg
, True
);
1764 ML_(generic_POST_sys_recvmsg
) ( ThreadId tid
, const HChar
*name
,
1765 struct vki_msghdr
*msg
, UInt length
)
1767 msghdr_foreachfield( tid
, name
, msg
, length
, post_mem_write_recvmsg
, True
);
1768 check_cmsg_for_fds( tid
, msg
);
1772 /* ---------------------------------------------------------------------
1773 Deal with a bunch of IPC related syscalls
1774 ------------------------------------------------------------------ */
1779 ML_(generic_PRE_sys_semop
) ( ThreadId tid
,
1780 UWord arg0
, UWord arg1
, UWord arg2
)
1782 /* int semop(int semid, struct sembuf *sops, unsigned nsops); */
1783 PRE_MEM_READ( "semop(sops)", arg1
, arg2
* sizeof(struct vki_sembuf
) );
1789 ML_(generic_PRE_sys_semtimedop
) ( ThreadId tid
,
1790 UWord arg0
, UWord arg1
,
1791 UWord arg2
, UWord arg3
)
1793 /* int semtimedop(int semid, struct sembuf *sops, unsigned nsops,
1794 struct timespec *timeout); */
1795 PRE_MEM_READ( "semtimedop(sops)", arg1
, arg2
* sizeof(struct vki_sembuf
) );
1797 PRE_MEM_READ( "semtimedop(timeout)", arg3
, sizeof(struct vki_timespec
) );
1803 UInt
get_sem_count( Int semid
)
1805 union vki_semun arg
;
1808 # if defined(__NR_semctl)
1809 # if defined(VGO_darwin)
1810 /* Darwin has no specific 64 bit semid_ds, but has __NR_semctl. */
1811 struct vki_semid_ds buf
;
1814 struct vki_semid64_ds buf
;
1817 res
= VG_(do_syscall4
)(__NR_semctl
, semid
, 0, VKI_IPC_STAT
, *(UWord
*)&arg
);
1818 if (sr_isError(res
))
1821 return buf
.sem_nsems
;
1823 # elif defined(__NR_semsys) /* Solaris */
1824 struct vki_semid_ds buf
;
1826 res
= VG_(do_syscall5
)(__NR_semsys
, VKI_SEMCTL
, semid
, 0, VKI_IPC_STAT
,
1828 if (sr_isError(res
))
1831 return buf
.sem_nsems
;
1834 struct vki_semid_ds buf
;
1836 res
= VG_(do_syscall5
)(__NR_ipc
, 3 /* IPCOP_semctl */, semid
, 0,
1837 VKI_IPC_STAT
, (UWord
)&arg
);
1838 if (sr_isError(res
))
1841 return buf
.sem_nsems
;
1846 ML_(generic_PRE_sys_semctl
) ( ThreadId tid
,
1847 UWord arg0
, UWord arg1
,
1848 UWord arg2
, UWord arg3
)
1850 /* int semctl(int semid, int semnum, int cmd, ...); */
1851 union vki_semun arg
= *(union vki_semun
*)&arg3
;
1853 switch (arg2
/* cmd */) {
1854 #if defined(VKI_IPC_INFO)
1857 case VKI_IPC_INFO
|VKI_IPC_64
:
1858 case VKI_SEM_INFO
|VKI_IPC_64
:
1859 PRE_MEM_WRITE( "semctl(IPC_INFO, arg.buf)",
1860 (Addr
)arg
.buf
, sizeof(struct vki_seminfo
) );
1865 #if defined(VKI_SEM_STAT)
1868 PRE_MEM_WRITE( "semctl(IPC_STAT, arg.buf)",
1869 (Addr
)arg
.buf
, sizeof(struct vki_semid_ds
) );
1872 #if defined(VKI_IPC_64)
1873 case VKI_IPC_STAT
|VKI_IPC_64
:
1874 #if defined(VKI_SEM_STAT)
1875 case VKI_SEM_STAT
|VKI_IPC_64
:
1878 #if defined(VKI_IPC_STAT64)
1879 case VKI_IPC_STAT64
:
1881 #if defined(VKI_IPC_64) || defined(VKI_IPC_STAT64)
1882 PRE_MEM_WRITE( "semctl(IPC_STAT, arg.buf)",
1883 (Addr
)arg
.buf
, sizeof(struct vki_semid64_ds
) );
1888 PRE_MEM_READ( "semctl(IPC_SET, arg.buf)",
1889 (Addr
)arg
.buf
, sizeof(struct vki_semid_ds
) );
1892 #if defined(VKI_IPC_64)
1893 case VKI_IPC_SET
|VKI_IPC_64
:
1895 #if defined(VKI_IPC_SET64)
1898 #if defined(VKI_IPC64) || defined(VKI_IPC_SET64)
1899 PRE_MEM_READ( "semctl(IPC_SET, arg.buf)",
1900 (Addr
)arg
.buf
, sizeof(struct vki_semid64_ds
) );
1905 #if defined(VKI_IPC_64)
1906 case VKI_GETALL
|VKI_IPC_64
:
1908 nsems
= get_sem_count( arg0
);
1909 PRE_MEM_WRITE( "semctl(IPC_GETALL, arg.array)",
1910 (Addr
)arg
.array
, sizeof(unsigned short) * nsems
);
1914 #if defined(VKI_IPC_64)
1915 case VKI_SETALL
|VKI_IPC_64
:
1917 nsems
= get_sem_count( arg0
);
1918 PRE_MEM_READ( "semctl(IPC_SETALL, arg.array)",
1919 (Addr
)arg
.array
, sizeof(unsigned short) * nsems
);
1925 ML_(generic_POST_sys_semctl
) ( ThreadId tid
,
1927 UWord arg0
, UWord arg1
,
1928 UWord arg2
, UWord arg3
)
1930 union vki_semun arg
= *(union vki_semun
*)&arg3
;
1932 switch (arg2
/* cmd */) {
1933 #if defined(VKI_IPC_INFO)
1936 case VKI_IPC_INFO
|VKI_IPC_64
:
1937 case VKI_SEM_INFO
|VKI_IPC_64
:
1938 POST_MEM_WRITE( (Addr
)arg
.buf
, sizeof(struct vki_seminfo
) );
1943 #if defined(VKI_SEM_STAT)
1946 POST_MEM_WRITE( (Addr
)arg
.buf
, sizeof(struct vki_semid_ds
) );
1949 #if defined(VKI_IPC_64)
1950 case VKI_IPC_STAT
|VKI_IPC_64
:
1951 case VKI_SEM_STAT
|VKI_IPC_64
:
1953 #if defined(VKI_IPC_STAT64)
1954 case VKI_IPC_STAT64
:
1956 #if defined(VKI_IPC_64) || defined(VKI_IPC_STAT64)
1957 POST_MEM_WRITE( (Addr
)arg
.buf
, sizeof(struct vki_semid64_ds
) );
1962 #if defined(VKI_IPC_64)
1963 case VKI_GETALL
|VKI_IPC_64
:
1965 nsems
= get_sem_count( arg0
);
1966 POST_MEM_WRITE( (Addr
)arg
.array
, sizeof(unsigned short) * nsems
);
1976 SizeT
get_shm_size ( Int shmid
)
1979 * The excluded platforms below gained direct shmctl in Linux 5.1. Keep
1980 * using ipc-multiplexed shmctl to keep compatibility with older kernel
1983 #if defined(__NR_shmctl) && \
1984 !defined(VGP_x86_linux) && !defined(VGP_mips32_linux) && \
1985 !defined(VGP_ppc32_linux) && !defined(VGP_ppc64be_linux) && \
1986 !defined(VGP_ppc64le_linux) && !defined(VGP_s390x_linux)
1988 struct vki_shmid64_ds buf
;
1990 * On Linux, the following ABIs use old shmid_ds by default with direct
1991 * shmctl and require IPC_64 for shmid64_ds (i.e. the direct syscall is
1992 * mapped to sys_old_shmctl):
1993 * alpha, arm, microblaze, mips n32/n64, xtensa
1994 * Other Linux ABIs use shmid64_ds by default and do not recognize IPC_64
1995 * with the direct shmctl syscall (but still recognize it for the
1996 * ipc-multiplexed version if that exists for the ABI).
1998 # if defined(VGO_linux) && !defined(VGP_arm_linux) && !defined(VGP_mips64_linux)
1999 SysRes __res
= VG_(do_syscall3
)(__NR_shmctl
, shmid
,
2000 VKI_IPC_STAT
, (UWord
)&buf
);
2002 SysRes __res
= VG_(do_syscall3
)(__NR_shmctl
, shmid
,
2003 VKI_IPC_STAT
|VKI_IPC_64
, (UWord
)&buf
);
2005 # else /* !def VKI_IPC_64 */
2006 struct vki_shmid_ds buf
;
2007 SysRes __res
= VG_(do_syscall3
)(__NR_shmctl
, shmid
, VKI_IPC_STAT
, (UWord
)&buf
);
2008 # endif /* def VKI_IPC_64 */
2009 #elif defined(__NR_shmsys) /* Solaris */
2010 struct vki_shmid_ds buf
;
2011 SysRes __res
= VG_(do_syscall4
)(__NR_shmsys
, VKI_SHMCTL
, shmid
, VKI_IPC_STAT
,
2014 struct vki_shmid_ds buf
;
2015 SysRes __res
= VG_(do_syscall5
)(__NR_ipc
, 24 /* IPCOP_shmctl */, shmid
,
2016 VKI_IPC_STAT
, 0, (UWord
)&buf
);
2018 if (sr_isError(__res
))
2021 return (SizeT
) buf
.shm_segsz
;
2025 ML_(generic_PRE_sys_shmat
) ( ThreadId tid
,
2026 UWord arg0
, UWord arg1
, UWord arg2
)
2028 /* void *shmat(int shmid, const void *shmaddr, int shmflg); */
2029 SizeT segmentSize
= get_shm_size ( arg0
);
2033 /* arm-linux only: work around the fact that
2034 VG_(am_get_advisory_client_simple) produces something that is
2035 VKI_PAGE_SIZE aligned, whereas what we want is something
2036 VKI_SHMLBA aligned, and VKI_SHMLBA >= VKI_PAGE_SIZE. Hence
2037 increase the request size by VKI_SHMLBA - VKI_PAGE_SIZE and
2038 then round the result up to the next VKI_SHMLBA boundary.
2039 See bug 222545 comment 15. So far, arm-linux is the only
2040 platform where this is known to be necessary. */
2041 vg_assert(VKI_SHMLBA
>= VKI_PAGE_SIZE
);
2042 if (VKI_SHMLBA
> VKI_PAGE_SIZE
) {
2043 segmentSize
+= VKI_SHMLBA
- VKI_PAGE_SIZE
;
2045 tmp
= VG_(am_get_advisory_client_simple
)(0, segmentSize
, &ok
);
2047 if (VKI_SHMLBA
> VKI_PAGE_SIZE
) {
2048 arg1
= VG_ROUNDUP(tmp
, VKI_SHMLBA
);
2054 else if (!ML_(valid_client_addr
)(arg1
, segmentSize
, tid
, "shmat"))
2060 ML_(generic_POST_sys_shmat
) ( ThreadId tid
,
2062 UWord arg0
, UWord arg1
, UWord arg2
)
2064 SizeT segmentSize
= VG_PGROUNDUP(get_shm_size(arg0
));
2065 if ( segmentSize
> 0 ) {
2066 UInt prot
= VKI_PROT_READ
|VKI_PROT_WRITE
;
2069 if (arg2
& VKI_SHM_RDONLY
)
2070 prot
&= ~VKI_PROT_WRITE
;
2071 /* It isn't exactly correct to pass 0 for the fd and offset
2072 here. The kernel seems to think the corresponding section
2073 does have dev/ino numbers:
2075 04e52000-04ec8000 rw-s 00000000 00:06 1966090 /SYSV00000000 (deleted)
2077 However there is no obvious way to find them. In order to
2078 cope with the discrepancy, aspacem's sync checker omits the
2079 dev/ino correspondence check in cases where V does not know
2081 d
= VG_(am_notify_client_shmat
)( res
, segmentSize
, prot
);
2083 /* we don't distinguish whether it's read-only or
2084 * read-write -- it doesn't matter really. */
2085 VG_TRACK( new_mem_mmap
, res
, segmentSize
, True
, True
, False
,
2088 VG_(discard_translations
)( (Addr
)res
,
2089 (ULong
)VG_PGROUNDUP(segmentSize
),
2090 "ML_(generic_POST_sys_shmat)" );
2097 ML_(generic_PRE_sys_shmdt
) ( ThreadId tid
, UWord arg0
)
2099 /* int shmdt(const void *shmaddr); */
2100 return ML_(valid_client_addr
)(arg0
, 1, tid
, "shmdt");
2104 ML_(generic_POST_sys_shmdt
) ( ThreadId tid
, UWord res
, UWord arg0
)
2106 NSegment
const* s
= VG_(am_find_nsegment
)(arg0
);
2109 Addr s_start
= s
->start
;
2110 SizeT s_len
= s
->end
+1 - s
->start
;
2113 vg_assert(s
->kind
== SkShmC
);
2114 vg_assert(s
->start
== arg0
);
2116 d
= VG_(am_notify_munmap
)(s_start
, s_len
);
2117 s
= NULL
; /* s is now invalid */
2118 VG_TRACK( die_mem_munmap
, s_start
, s_len
);
2120 VG_(discard_translations
)( s_start
,
2122 "ML_(generic_POST_sys_shmdt)" );
2128 ML_(generic_PRE_sys_shmctl
) ( ThreadId tid
,
2129 UWord arg0
, UWord arg1
, UWord arg2
)
2131 /* int shmctl(int shmid, int cmd, struct shmid_ds *buf); */
2132 switch (arg1
/* cmd */) {
2133 #if defined(VKI_IPC_INFO)
2135 PRE_MEM_WRITE( "shmctl(IPC_INFO, buf)",
2136 arg2
, sizeof(struct vki_shminfo
) );
2138 #if defined(VKI_IPC_64)
2139 case VKI_IPC_INFO
|VKI_IPC_64
:
2140 PRE_MEM_WRITE( "shmctl(IPC_INFO, buf)",
2141 arg2
, sizeof(struct vki_shminfo64
) );
2146 #if defined(VKI_SHM_INFO)
2148 #if defined(VKI_IPC_64)
2149 case VKI_SHM_INFO
|VKI_IPC_64
:
2151 PRE_MEM_WRITE( "shmctl(SHM_INFO, buf)",
2152 arg2
, sizeof(struct vki_shm_info
) );
2157 #if defined(VKI_SHM_STAT)
2160 PRE_MEM_WRITE( "shmctl(IPC_STAT, buf)",
2161 arg2
, sizeof(struct vki_shmid_ds
) );
2164 #if defined(VKI_IPC_64)
2165 case VKI_IPC_STAT
|VKI_IPC_64
:
2166 case VKI_SHM_STAT
|VKI_IPC_64
:
2167 PRE_MEM_WRITE( "shmctl(IPC_STAT, arg.buf)",
2168 arg2
, sizeof(struct vki_shmid64_ds
) );
2173 PRE_MEM_READ( "shmctl(IPC_SET, arg.buf)",
2174 arg2
, sizeof(struct vki_shmid_ds
) );
2177 #if defined(VKI_IPC_64)
2178 case VKI_IPC_SET
|VKI_IPC_64
:
2179 PRE_MEM_READ( "shmctl(IPC_SET, arg.buf)",
2180 arg2
, sizeof(struct vki_shmid64_ds
) );
2187 ML_(generic_POST_sys_shmctl
) ( ThreadId tid
,
2189 UWord arg0
, UWord arg1
, UWord arg2
)
2191 switch (arg1
/* cmd */) {
2192 #if defined(VKI_IPC_INFO)
2194 POST_MEM_WRITE( arg2
, sizeof(struct vki_shminfo
) );
2196 case VKI_IPC_INFO
|VKI_IPC_64
:
2197 POST_MEM_WRITE( arg2
, sizeof(struct vki_shminfo64
) );
2201 #if defined(VKI_SHM_INFO)
2203 case VKI_SHM_INFO
|VKI_IPC_64
:
2204 POST_MEM_WRITE( arg2
, sizeof(struct vki_shm_info
) );
2209 #if defined(VKI_SHM_STAT)
2212 POST_MEM_WRITE( arg2
, sizeof(struct vki_shmid_ds
) );
2215 #if defined(VKI_IPC_64)
2216 case VKI_IPC_STAT
|VKI_IPC_64
:
2217 case VKI_SHM_STAT
|VKI_IPC_64
:
2218 POST_MEM_WRITE( arg2
, sizeof(struct vki_shmid64_ds
) );
2226 /* ---------------------------------------------------------------------
2227 Generic handler for mmap
2228 ------------------------------------------------------------------ */
2231 * Although mmap is specified by POSIX and the argument are generally
2232 * consistent across platforms the precise details of the low level
2233 * argument passing conventions differ. For example:
2235 * - On x86-linux there is mmap (aka old_mmap) which takes the
2236 * arguments in a memory block and the offset in bytes; and
2237 * mmap2 (aka sys_mmap2) which takes the arguments in the normal
2238 * way and the offset in pages.
2240 * - On ppc32-linux there is mmap (aka sys_mmap) which takes the
2241 * arguments in the normal way and the offset in bytes; and
2242 * mmap2 (aka sys_mmap2) which takes the arguments in the normal
2243 * way and the offset in pages.
2245 * - On amd64-linux everything is simple and there is just the one
2246 * call, mmap (aka sys_mmap) which takes the arguments in the
2247 * normal way and the offset in bytes.
2249 * - On s390x-linux there is mmap (aka old_mmap) which takes the
2250 * arguments in a memory block and the offset in bytes. mmap2
2251 * is also available (but not exported via unistd.h) with
2252 * arguments in a memory block and the offset in pages.
2254 * To cope with all this we provide a generic handler function here
2255 * and then each platform implements one or more system call handlers
2256 * which call this generic routine after extracting and normalising
2261 ML_(generic_PRE_sys_mmap
) ( ThreadId tid
,
2262 UWord arg1
, UWord arg2
, UWord arg3
,
2263 UWord arg4
, UWord arg5
, Off64T arg6
)
2270 # if defined(VGO_darwin)
2271 // Nb: we can't use this on Darwin, it has races:
2272 // * needs to RETRY if advisory succeeds but map fails
2273 // (could have been some other thread in a nonblocking call)
2274 // * needs to not use fixed-position mmap() on Darwin
2275 // (mmap will cheerfully smash whatever's already there, which might
2276 // be a new mapping from some other thread in a nonblocking call)
2277 VG_(core_panic
)("can't use ML_(generic_PRE_sys_mmap) on Darwin");
2281 /* SuSV3 says: If len is zero, mmap() shall fail and no mapping
2282 shall be established. */
2283 return VG_(mk_SysRes_Error
)( VKI_EINVAL
);
2286 if (!VG_IS_PAGE_ALIGNED(arg1
)) {
2287 /* zap any misaligned addresses. */
2288 /* SuSV3 says misaligned addresses only cause the MAP_FIXED case
2289 to fail. Here, we catch them all. */
2290 return VG_(mk_SysRes_Error
)( VKI_EINVAL
);
2293 if (!VG_IS_PAGE_ALIGNED(arg6
)) {
2294 /* zap any misaligned offsets. */
2295 /* SuSV3 says: The off argument is constrained to be aligned and
2296 sized according to the value returned by sysconf() when
2297 passed _SC_PAGESIZE or _SC_PAGE_SIZE. */
2298 return VG_(mk_SysRes_Error
)( VKI_EINVAL
);
2301 /* Figure out what kind of allocation constraints there are
2302 (fixed/hint/any), and ask aspacem what we should do. */
2305 if (arg4
& VKI_MAP_FIXED
) {
2306 mreq
.rkind
= MFixed
;
2308 #if defined(VKI_MAP_ALIGN) /* Solaris specific */
2309 if (arg4
& VKI_MAP_ALIGN
) {
2310 mreq
.rkind
= MAlign
;
2311 if (mreq
.start
== 0) {
2312 mreq
.start
= VKI_PAGE_SIZE
;
2314 /* VKI_MAP_FIXED and VKI_MAP_ALIGN don't like each other. */
2315 arg4
&= ~VKI_MAP_ALIGN
;
2325 advised
= VG_(am_get_advisory
)( &mreq
, True
/*client*/, &mreq_ok
);
2327 /* Our request was bounced, so we'd better fail. */
2328 return VG_(mk_SysRes_Error
)( VKI_EINVAL
);
2331 # if defined(VKI_MAP_32BIT)
2332 /* MAP_32BIT is royally unportable, so if the client asks for it, try our
2333 best to make it work (but without complexifying aspacemgr).
2334 If the user requested MAP_32BIT, the mmap-ed space must be in the
2335 first 2GB of the address space. So, return ENOMEM if aspacemgr
2336 advisory is above the first 2GB. If MAP_FIXED is also requested,
2337 MAP_32BIT has to be ignored.
2338 Assumption about aspacemgr behaviour: aspacemgr scans the address space
2339 from low addresses to find a free segment. No special effort is done
2340 to keep the first 2GB 'free' for this MAP_32BIT. So, this will often
2341 fail once the program has already allocated significant memory. */
2342 if ((arg4
& VKI_MAP_32BIT
) && !(arg4
& VKI_MAP_FIXED
)) {
2343 if (advised
+ arg2
>= 0x80000000)
2344 return VG_(mk_SysRes_Error
)( VKI_ENOMEM
);
2348 /* Otherwise we're OK (so far). Install aspacem's choice of
2349 address, and let the mmap go through. */
2350 sres
= VG_(am_do_mmap_NO_NOTIFY
)(advised
, arg2
, arg3
,
2351 arg4
| VKI_MAP_FIXED
,
2354 # if defined(VKI_MAP_32BIT)
2355 /* No recovery trial if the advisory was not accepted. */
2356 if ((arg4
& VKI_MAP_32BIT
) && !(arg4
& VKI_MAP_FIXED
)
2357 && sr_isError(sres
)) {
2358 return VG_(mk_SysRes_Error
)( VKI_ENOMEM
);
2362 /* A refinement: it may be that the kernel refused aspacem's choice
2363 of address. If we were originally asked for a hinted mapping,
2364 there is still a last chance: try again at any address.
2366 if (mreq
.rkind
== MHint
&& sr_isError(sres
)) {
2370 advised
= VG_(am_get_advisory
)( &mreq
, True
/*client*/, &mreq_ok
);
2372 /* Our request was bounced, so we'd better fail. */
2373 return VG_(mk_SysRes_Error
)( VKI_EINVAL
);
2375 /* and try again with the kernel */
2376 sres
= VG_(am_do_mmap_NO_NOTIFY
)(advised
, arg2
, arg3
,
2377 arg4
| VKI_MAP_FIXED
,
2381 /* Yet another refinement : sometimes valgrind chooses an address
2382 which is not acceptable by the kernel. This at least happens
2383 when mmap-ing huge pages, using the flag MAP_HUGETLB.
2384 valgrind aspacem does not know about huge pages, and modifying
2385 it to handle huge pages is not straightforward (e.g. need
2386 to understand special file system mount options).
2387 So, let's just redo an mmap, without giving any constraint to
2388 the kernel. If that succeeds, check with aspacem that the returned
2389 address is acceptable.
2390 This will give a similar effect as if the user would have
2391 hinted that address.
2392 The aspacem state will be correctly updated afterwards.
2393 We however cannot do this last refinement when the user asked
2394 for a fixed mapping, as the user asked a specific address. */
2395 if (sr_isError(sres
) && !(arg4
& VKI_MAP_FIXED
)) {
2397 /* try mmap with NULL address and without VKI_MAP_FIXED
2398 to let the kernel decide. */
2399 sres
= VG_(am_do_mmap_NO_NOTIFY
)(advised
, arg2
, arg3
,
2402 if (!sr_isError(sres
)) {
2403 /* The kernel is supposed to know what it is doing, but let's
2404 do a last sanity check anyway, as if the chosen address had
2405 been initially hinted by the client. The whole point of this
2406 last try was to allow mmap of huge pages to succeed without
2407 making aspacem understand them, on the other hand the kernel
2408 does not know about valgrind reservations, so this mapping
2409 can end up in free space and reservations. */
2410 mreq
.start
= (Addr
)sr_Res(sres
);
2413 advised
= VG_(am_get_advisory
)( &mreq
, True
/*client*/, &mreq_ok
);
2414 vg_assert(mreq_ok
&& advised
== mreq
.start
);
2418 if (!sr_isError(sres
)) {
2420 /* Notify aspacem. */
2421 notify_core_of_mmap(
2422 (Addr
)sr_Res(sres
), /* addr kernel actually assigned */
2425 arg4
, /* the original flags value */
2430 di_handle
= VG_(di_notify_mmap
)( (Addr
)sr_Res(sres
),
2431 False
/*allow_SkFileV*/, (Int
)arg5
);
2432 /* Notify the tool. */
2433 notify_tool_of_mmap(
2434 (Addr
)sr_Res(sres
), /* addr kernel actually assigned */
2437 di_handle
/* so the tool can refer to the read debuginfo later,
2443 if (!sr_isError(sres
) && (arg4
& VKI_MAP_FIXED
))
2444 vg_assert(sr_Res(sres
) == arg1
);
2450 /* ---------------------------------------------------------------------
2451 The Main Entertainment ... syscall wrappers
2452 ------------------------------------------------------------------ */
2454 /* Note: the PRE() and POST() wrappers are for the actual functions
2455 implementing the system calls in the OS kernel. These mostly have
2456 names like sys_write(); a few have names like old_mmap(). See the
2457 comment for ML_(syscall_table)[] for important info about the __NR_foo
2458 constants and their relationship to the sys_foo() functions.
2460 Some notes about names used for syscalls and args:
2461 - For the --trace-syscalls=yes output, we use the sys_foo() name to avoid
2464 - For error messages, we generally use a somewhat generic name
2465 for the syscall (eg. "write" rather than "sys_write"). This should be
2466 good enough for the average user to understand what is happening,
2467 without confusing them with names like "sys_write".
2469 - Also, for error messages the arg names are mostly taken from the man
2470 pages (even though many of those man pages are really for glibc
2471 functions of the same name), rather than from the OS kernel source,
2472 for the same reason -- a user presented with a "bogus foo(bar)" arg
2473 will most likely look at the "foo" man page to see which is the "bar"
2476 Note that we use our own vki_* types. The one exception is in
2477 PRE_REG_READn calls, where pointer types haven't been changed, because
2478 they don't need to be -- eg. for "foo*" to be used, the type foo need not
2481 XXX: some of these are arch-specific, and should be factored out.
2484 #define PRE(name) DEFN_PRE_TEMPLATE(generic, name)
2485 #define POST(name) DEFN_POST_TEMPLATE(generic, name)
2490 /* simple; just make this thread exit */
2491 PRINT("exit( %ld )", SARG1
);
2492 PRE_REG_READ1(void, "exit", int, status
);
2493 tst
= VG_(get_ThreadState
)(tid
);
2494 /* Set the thread's status to be exiting, then claim that the
2495 syscall succeeded. */
2496 tst
->exitreason
= VgSrc_ExitThread
;
2497 tst
->os_state
.exitcode
= ARG1
;
2498 SET_STATUS_Success(0);
2503 PRINT("unimplemented (by the kernel) syscall: %s! (ni_syscall)\n",
2504 VG_SYSNUM_STRING(SYSNO
));
2505 PRE_REG_READ0(long, "ni_syscall");
2506 SET_STATUS_Failure( VKI_ENOSYS
);
2511 PRINT("sys_iopl ( %" FMT_REGWORD
"u )", ARG1
);
2512 PRE_REG_READ1(long, "iopl", unsigned long, level
);
2517 *flags
|= SfMayBlock
;
2518 PRINT("sys_fsync ( %" FMT_REGWORD
"u )", ARG1
);
2519 PRE_REG_READ1(long, "fsync", unsigned int, fd
);
2524 *flags
|= SfMayBlock
;
2525 PRINT("sys_fdatasync ( %" FMT_REGWORD
"u )", ARG1
);
2526 PRE_REG_READ1(long, "fdatasync", unsigned int, fd
);
2531 *flags
|= SfMayBlock
;
2532 PRINT("sys_msync ( %#" FMT_REGWORD
"x, %" FMT_REGWORD
"u, %#"
2533 FMT_REGWORD
"x )", ARG1
, ARG2
, ARG3
);
2534 PRE_REG_READ3(long, "msync",
2535 unsigned long, start
, vki_size_t
, length
, int, flags
);
2536 PRE_MEM_READ( "msync(start)", ARG1
, ARG2
);
2539 // Nb: getpmsg() and putpmsg() are special additional syscalls used in early
2540 // versions of LiS (Linux Streams). They are not part of the kernel.
2541 // Therefore, we have to provide this type ourself, rather than getting it
2542 // from the kernel sources.
2543 struct vki_pmsg_strbuf
{
2544 int maxlen
; /* no. of bytes in buffer */
2545 int len
; /* no. of bytes returned */
2546 vki_caddr_t buf
; /* pointer to data */
2550 /* LiS getpmsg from http://www.gcom.com/home/linux/lis/ */
2551 struct vki_pmsg_strbuf
*ctrl
;
2552 struct vki_pmsg_strbuf
*data
;
2553 *flags
|= SfMayBlock
;
2554 PRINT("sys_getpmsg ( %ld, %#" FMT_REGWORD
"x, %#" FMT_REGWORD
"x, %#"
2555 FMT_REGWORD
"x, %#" FMT_REGWORD
"x )", SARG1
,
2556 ARG2
, ARG3
, ARG4
, ARG5
);
2557 PRE_REG_READ5(int, "getpmsg",
2558 int, fd
, struct strbuf
*, ctrl
, struct strbuf
*, data
,
2559 int *, bandp
, int *, flagsp
);
2560 ctrl
= (struct vki_pmsg_strbuf
*)(Addr
)ARG2
;
2561 data
= (struct vki_pmsg_strbuf
*)(Addr
)ARG3
;
2562 if (ctrl
&& ctrl
->maxlen
> 0)
2563 PRE_MEM_WRITE( "getpmsg(ctrl)", (Addr
)ctrl
->buf
, ctrl
->maxlen
);
2564 if (data
&& data
->maxlen
> 0)
2565 PRE_MEM_WRITE( "getpmsg(data)", (Addr
)data
->buf
, data
->maxlen
);
2567 PRE_MEM_WRITE( "getpmsg(bandp)", (Addr
)ARG4
, sizeof(int));
2569 PRE_MEM_WRITE( "getpmsg(flagsp)", (Addr
)ARG5
, sizeof(int));
2573 struct vki_pmsg_strbuf
*ctrl
;
2574 struct vki_pmsg_strbuf
*data
;
2576 ctrl
= (struct vki_pmsg_strbuf
*)(Addr
)ARG2
;
2577 data
= (struct vki_pmsg_strbuf
*)(Addr
)ARG3
;
2578 if (RES
== 0 && ctrl
&& ctrl
->len
> 0) {
2579 POST_MEM_WRITE( (Addr
)ctrl
->buf
, ctrl
->len
);
2581 if (RES
== 0 && data
&& data
->len
> 0) {
2582 POST_MEM_WRITE( (Addr
)data
->buf
, data
->len
);
2588 /* LiS putpmsg from http://www.gcom.com/home/linux/lis/ */
2589 struct vki_pmsg_strbuf
*ctrl
;
2590 struct vki_pmsg_strbuf
*data
;
2591 *flags
|= SfMayBlock
;
2592 PRINT("sys_putpmsg ( %ld, %#" FMT_REGWORD
"x, %#" FMT_REGWORD
2593 "x, %ld, %ld )", SARG1
, ARG2
, ARG3
, SARG4
, SARG5
);
2594 PRE_REG_READ5(int, "putpmsg",
2595 int, fd
, struct strbuf
*, ctrl
, struct strbuf
*, data
,
2596 int, band
, int, flags
);
2597 ctrl
= (struct vki_pmsg_strbuf
*)(Addr
)ARG2
;
2598 data
= (struct vki_pmsg_strbuf
*)(Addr
)ARG3
;
2599 if (ctrl
&& ctrl
->len
> 0)
2600 PRE_MEM_READ( "putpmsg(ctrl)", (Addr
)ctrl
->buf
, ctrl
->len
);
2601 if (data
&& data
->len
> 0)
2602 PRE_MEM_READ( "putpmsg(data)", (Addr
)data
->buf
, data
->len
);
2607 struct vki_itimerval
*value
= (struct vki_itimerval
*)(Addr
)ARG2
;
2608 PRINT("sys_getitimer ( %ld, %#" FMT_REGWORD
"x )", SARG1
, ARG2
);
2609 PRE_REG_READ2(long, "getitimer", int, which
, struct itimerval
*, value
);
2611 PRE_timeval_WRITE( "getitimer(&value->it_interval)", &(value
->it_interval
));
2612 PRE_timeval_WRITE( "getitimer(&value->it_value)", &(value
->it_value
));
2617 if (ARG2
!= (Addr
)NULL
) {
2618 struct vki_itimerval
*value
= (struct vki_itimerval
*)(Addr
)ARG2
;
2619 POST_timeval_WRITE( &(value
->it_interval
) );
2620 POST_timeval_WRITE( &(value
->it_value
) );
2626 PRINT("sys_setitimer ( %ld, %#" FMT_REGWORD
"x, %#" FMT_REGWORD
"x )",
2628 PRE_REG_READ3(long, "setitimer",
2630 struct itimerval
*, value
, struct itimerval
*, ovalue
);
2631 if (ARG2
!= (Addr
)NULL
) {
2632 struct vki_itimerval
*value
= (struct vki_itimerval
*)(Addr
)ARG2
;
2633 PRE_timeval_READ( "setitimer(&value->it_interval)",
2634 &(value
->it_interval
));
2635 PRE_timeval_READ( "setitimer(&value->it_value)",
2636 &(value
->it_value
));
2638 if (ARG3
!= (Addr
)NULL
) {
2639 struct vki_itimerval
*ovalue
= (struct vki_itimerval
*)(Addr
)ARG3
;
2640 PRE_timeval_WRITE( "setitimer(&ovalue->it_interval)",
2641 &(ovalue
->it_interval
));
2642 PRE_timeval_WRITE( "setitimer(&ovalue->it_value)",
2643 &(ovalue
->it_value
));
2649 if (ARG3
!= (Addr
)NULL
) {
2650 struct vki_itimerval
*ovalue
= (struct vki_itimerval
*)(Addr
)ARG3
;
2651 POST_timeval_WRITE( &(ovalue
->it_interval
) );
2652 POST_timeval_WRITE( &(ovalue
->it_value
) );
2658 PRINT("sys_chroot ( %#" FMT_REGWORD
"x )", ARG1
);
2659 PRE_REG_READ1(long, "chroot", const char *, path
);
2660 PRE_MEM_RASCIIZ( "chroot(path)", ARG1
);
2665 *flags
|= SfMayBlock
;
2666 PRINT("sys_madvise ( %#" FMT_REGWORD
"x, %" FMT_REGWORD
"u, %ld )",
2668 PRE_REG_READ3(long, "madvise",
2669 unsigned long, start
, vki_size_t
, length
, int, advice
);
2675 // Nb: this is different to the glibc version described in the man pages,
2676 // which lacks the fifth 'new_address' argument.
2677 if (ARG4
& VKI_MREMAP_FIXED
) {
2678 PRINT("sys_mremap ( %#" FMT_REGWORD
"x, %" FMT_REGWORD
"u, %"
2679 FMT_REGWORD
"u, %#" FMT_REGWORD
"x, %#" FMT_REGWORD
"x )",
2680 ARG1
, ARG2
, ARG3
, ARG4
, ARG5
);
2681 PRE_REG_READ5(unsigned long, "mremap",
2682 unsigned long, old_addr
, unsigned long, old_size
,
2683 unsigned long, new_size
, unsigned long, flags
,
2684 unsigned long, new_addr
);
2686 PRINT("sys_mremap ( %#" FMT_REGWORD
"x, %" FMT_REGWORD
"u, %"
2687 FMT_REGWORD
"u, 0x%" FMT_REGWORD
"x )",
2688 ARG1
, ARG2
, ARG3
, ARG4
);
2689 PRE_REG_READ4(unsigned long, "mremap",
2690 unsigned long, old_addr
, unsigned long, old_size
,
2691 unsigned long, new_size
, unsigned long, flags
);
2693 SET_STATUS_from_SysRes(
2694 do_mremap((Addr
)ARG1
, ARG2
, (Addr
)ARG5
, ARG3
, ARG4
, tid
)
2697 #endif /* HAVE_MREMAP */
2701 PRINT("sys_nice ( %ld )", SARG1
);
2702 PRE_REG_READ1(long, "nice", int, inc
);
2707 *flags
|= SfMayBlock
;
2708 PRINT("sys_mlock ( %#" FMT_REGWORD
"x, %" FMT_REGWORD
"u )", ARG1
, ARG2
);
2709 PRE_REG_READ2(long, "mlock", unsigned long, addr
, vki_size_t
, len
);
2714 *flags
|= SfMayBlock
;
2715 PRINT("sys_munlock ( %#" FMT_REGWORD
"x, %" FMT_REGWORD
"u )", ARG1
, ARG2
);
2716 PRE_REG_READ2(long, "munlock", unsigned long, addr
, vki_size_t
, len
);
2721 *flags
|= SfMayBlock
;
2722 PRINT("sys_mlockall ( %" FMT_REGWORD
"x )", ARG1
);
2723 PRE_REG_READ1(long, "mlockall", int, flags
);
2726 PRE(sys_setpriority
)
2728 PRINT("sys_setpriority ( %ld, %ld, %ld )", SARG1
, SARG2
, SARG3
);
2729 PRE_REG_READ3(long, "setpriority", int, which
, int, who
, int, prio
);
2732 PRE(sys_getpriority
)
2734 PRINT("sys_getpriority ( %ld, %ld )", SARG1
, SARG2
);
2735 PRE_REG_READ2(long, "getpriority", int, which
, int, who
);
2740 *flags
|= SfMayBlock
;
2741 #if VG_WORDSIZE == 4
2742 PRINT("sys_pwrite64 ( %" FMT_REGWORD
"u, %#" FMT_REGWORD
"x, %"
2743 FMT_REGWORD
"u, %lld )", ARG1
, ARG2
, ARG3
, (Long
)MERGE64(ARG4
,ARG5
));
2744 PRE_REG_READ5(ssize_t
, "pwrite64",
2745 unsigned int, fd
, const char *, buf
, vki_size_t
, count
,
2746 vki_u32
, MERGE64_FIRST(offset
), vki_u32
, MERGE64_SECOND(offset
));
2747 #elif VG_WORDSIZE == 8
2748 PRINT("sys_pwrite64 ( %lu, %#lx, %lu, %ld )",
2749 ARG1
, ARG2
, ARG3
, SARG4
);
2750 PRE_REG_READ4(ssize_t
, "pwrite64",
2751 unsigned int, fd
, const char *, buf
, vki_size_t
, count
,
2754 # error Unexpected word size
2756 PRE_MEM_READ( "pwrite64(buf)", ARG2
, ARG3
);
2761 *flags
|= SfMayBlock
;
2762 PRINT("sys_sync ( )");
2763 PRE_REG_READ0(long, "sync");
2766 #if !defined(VGP_nanomips_linux)
2769 FUSE_COMPATIBLE_MAY_BLOCK();
2770 PRINT("sys_fstatfs ( %" FMT_REGWORD
"u, %#" FMT_REGWORD
"x )", ARG1
, ARG2
);
2771 PRE_REG_READ2(long, "fstatfs",
2772 unsigned int, fd
, struct statfs
*, buf
);
2773 PRE_MEM_WRITE( "fstatfs(buf)", ARG2
, sizeof(struct vki_statfs
) );
2778 POST_MEM_WRITE( ARG2
, sizeof(struct vki_statfs
) );
2783 FUSE_COMPATIBLE_MAY_BLOCK();
2784 PRINT("sys_fstatfs64 ( %" FMT_REGWORD
"u, %" FMT_REGWORD
"u, %#"
2785 FMT_REGWORD
"x )", ARG1
, ARG2
, ARG3
);
2786 PRE_REG_READ3(long, "fstatfs64",
2787 unsigned int, fd
, vki_size_t
, size
, struct statfs64
*, buf
);
2788 PRE_MEM_WRITE( "fstatfs64(buf)", ARG3
, ARG2
);
2792 POST_MEM_WRITE( ARG3
, ARG2
);
2798 PRINT("sys_getsid ( %ld )", SARG1
);
2799 PRE_REG_READ1(long, "getsid", vki_pid_t
, pid
);
2804 *flags
|= SfMayBlock
;
2805 #if VG_WORDSIZE == 4
2806 PRINT("sys_pread64 ( %" FMT_REGWORD
"u, %#" FMT_REGWORD
"x, %"
2807 FMT_REGWORD
"u, %lld )", ARG1
, ARG2
, ARG3
, (Long
)MERGE64(ARG4
,ARG5
));
2808 PRE_REG_READ5(ssize_t
, "pread64",
2809 unsigned int, fd
, char *, buf
, vki_size_t
, count
,
2810 vki_u32
, MERGE64_FIRST(offset
), vki_u32
, MERGE64_SECOND(offset
));
2811 #elif VG_WORDSIZE == 8
2812 PRINT("sys_pread64 ( %lu, %#lx, %lu, %ld )",
2813 ARG1
, ARG2
, ARG3
, SARG4
);
2814 PRE_REG_READ4(ssize_t
, "pread64",
2815 unsigned int, fd
, char *, buf
, vki_size_t
, count
,
2818 # error Unexpected word size
2820 PRE_MEM_WRITE( "pread64(buf)", ARG2
, ARG3
);
2826 POST_MEM_WRITE( ARG2
, RES
);
2832 FUSE_COMPATIBLE_MAY_BLOCK();
2833 PRINT("sys_mknod ( %#" FMT_REGWORD
"x(%s), %#" FMT_REGWORD
"x, %#"
2834 FMT_REGWORD
"x )", ARG1
, (HChar
*)(Addr
)ARG1
, ARG2
, ARG3
);
2835 PRE_REG_READ3(long, "mknod",
2836 const char *, pathname
, int, mode
, unsigned, dev
);
2837 PRE_MEM_RASCIIZ( "mknod(pathname)", ARG1
);
2842 *flags
|= SfMayBlock
;
2843 PRINT("sys_flock ( %" FMT_REGWORD
"u, %" FMT_REGWORD
"u )", ARG1
, ARG2
);
2844 PRE_REG_READ2(long, "flock", unsigned int, fd
, unsigned int, operation
);
2847 // Pre_read a char** argument.
2848 void ML_(pre_argv_envp
)(Addr a
, ThreadId tid
, const HChar
*s1
, const HChar
*s2
)
2852 Addr
* a_p
= (Addr
*)a
;
2853 PRE_MEM_READ( s1
, (Addr
)a_p
, sizeof(Addr
) );
2857 PRE_MEM_RASCIIZ( s2
, a_deref
);
2862 static Bool
i_am_the_only_thread ( void )
2864 Int c
= VG_(count_living_threads
)();
2865 vg_assert(c
>= 1); /* stay sane */
2869 /* Wait until all other threads disappear. */
2870 void VG_(reap_threads
)(ThreadId self
)
2872 while (!i_am_the_only_thread()) {
2873 /* Let other thread(s) run */
2875 VG_(poll_signals
)(self
);
2877 vg_assert(i_am_the_only_thread());
2880 /* This handles the common part of the PRE macro for execve and execveat. */
2881 void handle_pre_sys_execve(ThreadId tid
, SyscallStatus
*status
, Addr pathname
,
2882 Addr arg_2
, Addr arg_3
, Bool is_execveat
,
2885 HChar
* path
= NULL
; /* path to executable */
2886 HChar
** envp
= NULL
;
2887 HChar
** argv
= NULL
;
2889 HChar
* launcher_basename
= NULL
;
2893 Bool setuid_allowed
, trace_this_child
;
2895 char str2
[30], str3
[30];
2902 VG_(strcpy
)(str2
, str
);
2903 VG_(strcpy
)(str3
, str
);
2906 /* At least the terminating NULL must be addressable. */
2907 if (!ML_(safe_to_deref
)((HChar
**) (Addr
)arg_2
, sizeof(HChar
*))) {
2908 SET_STATUS_Failure(VKI_EFAULT
);
2911 VG_(strcat
)(str2
, "(argv)");
2912 VG_(strcat
)(str3
, "(argv[i])");
2913 ML_(pre_argv_envp
)( arg_2
, tid
, str2
, str3
);
2915 // Reset helper strings to syscall name.
2916 str2
[VG_(strlen
)(str
)] = '\0';
2917 str3
[VG_(strlen
)(str
)] = '\0';
2919 /* At least the terminating NULL must be addressable. */
2920 if (!ML_(safe_to_deref
)((HChar
**) (Addr
)arg_3
, sizeof(HChar
*))) {
2921 SET_STATUS_Failure(VKI_EFAULT
);
2924 VG_(strcat
)(str2
, "(envp)");
2925 VG_(strcat
)(str3
, "(envp[i])");
2926 ML_(pre_argv_envp
)( arg_3
, tid
, str2
, str3
);
2929 vg_assert(VG_(is_valid_tid
)(tid
));
2930 tst
= VG_(get_ThreadState
)(tid
);
2932 /* Erk. If the exec fails, then the following will have made a
2933 mess of things which makes it hard for us to continue. The
2934 right thing to do is piece everything together again in
2935 POST(execve), but that's close to impossible. Instead, we make
2936 an effort to check that the execve will work before actually
2939 /* Check that the name at least begins in client-accessible storage.
2940 If we didn't create it ourselves in execveat. */
2942 && !VG_(am_is_valid_for_client
)( pathname
, 1, VKI_PROT_READ
)) {
2943 SET_STATUS_Failure( VKI_EFAULT
);
2947 // debug-only printing
2949 VG_(printf
)("pathname = %p(%s)\n", (void*)(Addr
)pathname
, (HChar
*)(Addr
)pathname
);
2951 VG_(printf
)("arg_2 = ");
2953 HChar
** vec
= (HChar
**)(Addr
)arg_2
;
2954 for (q
= 0; vec
[q
]; q
++)
2955 VG_(printf
)("%p(%s) ", vec
[q
], vec
[q
]);
2958 VG_(printf
)("arg_2 = null\n");
2962 // Decide whether or not we want to follow along
2963 { // Make 'child_argv' be a pointer to the child's arg vector
2964 // (skipping the exe name)
2965 const HChar
** child_argv
= (const HChar
**)(Addr
)arg_2
;
2966 if (child_argv
&& child_argv
[0] == NULL
)
2968 trace_this_child
= VG_(should_we_trace_this_child
)( (HChar
*)(Addr
)pathname
,
2972 // Do the important checks: it is a file, is executable, permissions are
2973 // ok, etc. We allow setuid executables to run only in the case when
2974 // we are not simulating them, that is, they to be run natively.
2975 setuid_allowed
= trace_this_child
? False
: True
;
2976 res
= VG_(pre_exec_check
)((const HChar
*)(Addr
)pathname
, NULL
, setuid_allowed
);
2977 if (sr_isError(res
)) {
2978 SET_STATUS_Failure( sr_Err(res
) );
2982 /* If we're tracing the child, and the launcher name looks bogus
2983 (possibly because launcher.c couldn't figure it out, see
2984 comments therein) then we have no option but to fail. */
2985 if (trace_this_child
2986 && (VG_(name_of_launcher
) == NULL
2987 || VG_(name_of_launcher
)[0] != '/')) {
2988 SET_STATUS_Failure( VKI_ECHILD
); /* "No child processes" */
2992 /* After this point, we can't recover if the execve fails. */
2993 VG_(debugLog
)(1, "syswrap", "Exec of %s\n", (HChar
*)(Addr
)pathname
);
2996 // Terminate gdbserver if it is active.
2997 if (VG_(clo_vgdb
) != Vg_VgdbNo
) {
2998 // If the child will not be traced, we need to terminate gdbserver
2999 // to cleanup the gdbserver resources (e.g. the FIFO files).
3000 // If child will be traced, we also terminate gdbserver: the new
3001 // Valgrind will start a fresh gdbserver after exec.
3005 /* Resistance is futile. Nuke all other threads. POSIX mandates
3006 this. (Really, nuke them all, since the new process will make
3007 its own new thread.) */
3008 VG_(nuke_all_threads_except
)( tid
, VgSrc_ExitThread
);
3009 VG_(reap_threads
)(tid
);
3011 // Set up the child's exe path.
3013 if (trace_this_child
) {
3015 // We want to exec the launcher. Get its pre-remembered path.
3016 path
= VG_(name_of_launcher
);
3017 // VG_(name_of_launcher) should have been acquired by m_main at
3021 launcher_basename
= VG_(strrchr
)(path
, '/');
3022 if (launcher_basename
== NULL
|| launcher_basename
[1] == 0) {
3023 launcher_basename
= path
; // hmm, tres dubious
3025 launcher_basename
++;
3029 path
= (HChar
*)(Addr
)pathname
;
3032 // Set up the child's environment.
3034 // Remove the valgrind-specific stuff from the environment so the
3035 // child doesn't get vgpreload_core.so, vgpreload_<tool>.so, etc.
3036 // This is done unconditionally, since if we are tracing the child,
3037 // the child valgrind will set up the appropriate client environment.
3038 // Nb: we make a copy of the environment before trying to mangle it
3039 // as it might be in read-only memory (this was bug #101881).
3041 // Then, if tracing the child, set VALGRIND_LIB for it.
3046 envp
= VG_(env_clone
)( (HChar
**)(Addr
)arg_3
);
3047 if (envp
== NULL
) goto hosed
;
3048 VG_(env_remove_valgrind_env_stuff
)( envp
, True
/*ro_strings*/, NULL
);
3051 if (trace_this_child
) {
3052 // Set VALGRIND_LIB in arg_3 (the environment)
3053 VG_(env_setenv
)( &envp
, VALGRIND_LIB
, VG_(libdir
));
3056 // Set up the child's args. If not tracing it, they are
3057 // simply arg_2. Otherwise, they are
3059 // [launcher_basename] ++ VG_(args_for_valgrind) ++ [pathname] ++ arg_2[1..]
3061 // except that the first VG_(args_for_valgrind_noexecpass) args
3064 if (!trace_this_child
) {
3065 argv
= (HChar
**)(Addr
)arg_2
;
3067 vg_assert( VG_(args_for_valgrind
) );
3068 vg_assert( VG_(args_for_valgrind_noexecpass
) >= 0 );
3069 vg_assert( VG_(args_for_valgrind_noexecpass
)
3070 <= VG_(sizeXA
)( VG_(args_for_valgrind
) ) );
3071 /* how many args in total will there be? */
3072 // launcher basename
3075 tot_args
+= VG_(sizeXA
)( VG_(args_for_valgrind
) );
3076 tot_args
-= VG_(args_for_valgrind_noexecpass
);
3077 // name of client exe
3079 // args for client exe, skipping [0]
3080 arg2copy
= (HChar
**)(Addr
)arg_2
;
3081 if (arg2copy
&& arg2copy
[0]) {
3082 for (i
= 1; arg2copy
[i
]; i
++)
3086 argv
= VG_(malloc
)( "di.syswrap.pre_sys_execve.1",
3087 (tot_args
+1) * sizeof(HChar
*) );
3090 argv
[j
++] = launcher_basename
;
3091 for (i
= 0; i
< VG_(sizeXA
)( VG_(args_for_valgrind
) ); i
++) {
3092 if (i
< VG_(args_for_valgrind_noexecpass
))
3094 argv
[j
++] = * (HChar
**) VG_(indexXA
)( VG_(args_for_valgrind
), i
);
3096 argv
[j
++] = (HChar
*)(Addr
)pathname
;
3097 if (arg2copy
&& arg2copy
[0])
3098 for (i
= 1; arg2copy
[i
]; i
++)
3099 argv
[j
++] = arg2copy
[i
];
3102 vg_assert(j
== tot_args
+1);
3106 Set the signal state up for exec.
3108 We need to set the real signal state to make sure the exec'd
3109 process gets SIG_IGN properly.
3111 Also set our real sigmask to match the client's sigmask so that
3112 the exec'd child will get the right mask. First we need to
3113 clear out any pending signals so they they don't get delivered,
3114 which would confuse things.
3116 XXX This is a bug - the signals should remain pending, and be
3117 delivered to the new process after exec. There's also a
3118 race-condition, since if someone delivers us a signal between
3119 the sigprocmask and the execve, we'll still get the signal. Oh
3123 vki_sigset_t allsigs
;
3126 /* What this loop does: it queries SCSS (the signal state that
3127 the client _thinks_ the kernel is in) by calling
3128 VG_(do_sys_sigaction), and modifies the real kernel signal
3129 state accordingly. */
3130 for (i
= 1; i
< VG_(max_signal
); i
++) {
3131 vki_sigaction_fromK_t sa_f
;
3132 vki_sigaction_toK_t sa_t
;
3133 VG_(do_sys_sigaction
)(i
, NULL
, &sa_f
);
3134 VG_(convert_sigaction_fromK_to_toK
)(&sa_f
, &sa_t
);
3135 if (sa_t
.ksa_handler
== VKI_SIG_IGN
)
3136 VG_(sigaction
)(i
, &sa_t
, NULL
);
3138 sa_t
.ksa_handler
= VKI_SIG_DFL
;
3139 VG_(sigaction
)(i
, &sa_t
, NULL
);
3143 VG_(sigfillset
)(&allsigs
);
3144 while(VG_(sigtimedwait_zero
)(&allsigs
, &info
) > 0)
3147 VG_(sigprocmask
)(VKI_SIG_SETMASK
, &tst
->sig_mask
, NULL
);
3152 VG_(printf
)("exec: %s\n", path
);
3153 for (cpp
= argv
; cpp
&& *cpp
; cpp
++)
3154 VG_(printf
)("argv: %s\n", *cpp
);
3156 for (cpp
= envp
; cpp
&& *cpp
; cpp
++)
3157 VG_(printf
)("env: %s\n", *cpp
);
3160 // always execute this because it's executing valgrind, not the "target" exe
3161 SET_STATUS_from_SysRes(
3162 VG_(do_syscall3
)(__NR_execve
, (UWord
)path
, (UWord
)argv
, (UWord
)envp
));
3164 /* If we got here, then the execve failed. We've already made way
3165 too much of a mess to continue, so we have to abort. */
3168 VG_(message
)(Vg_UserMsg
, "execve(%#" FMT_REGWORD
"x(%s), %#" FMT_REGWORD
3169 "x, %#" FMT_REGWORD
"x) failed, errno %lu\n",
3170 pathname
, (HChar
*)(Addr
)pathname
, arg_2
, arg_3
, ERR
);
3171 VG_(message
)(Vg_UserMsg
, "EXEC FAILED: I can't recover from "
3172 "execve() failing, so I'm dying.\n");
3173 VG_(message
)(Vg_UserMsg
, "Add more stringent tests in PRE(sys_execve), "
3174 "or work out how to recover.\n");
3179 // XXX: prototype here seemingly doesn't match the prototype for i386-linux,
3180 // but it seems to work nonetheless...
3183 PRINT("sys_execve ( %#" FMT_REGWORD
"x(%s), %#" FMT_REGWORD
"x, %#"
3184 FMT_REGWORD
"x )", ARG1
, (HChar
*)(Addr
)ARG1
, ARG2
, ARG3
);
3185 PRE_REG_READ3(vki_off_t
, "execve",
3186 char *, filename
, char **, argv
, char **, envp
);
3187 PRE_MEM_RASCIIZ( "execve(filename)", ARG1
);
3189 char *pathname
= (char *)ARG1
;
3190 Addr arg_2
= (Addr
)ARG2
;
3191 Addr arg_3
= (Addr
)ARG3
;
3193 handle_pre_sys_execve(tid
, status
, (Addr
)pathname
, arg_2
, arg_3
, 0, True
);
3198 PRINT("sys_access ( %#" FMT_REGWORD
"x(%s), %ld )", ARG1
,
3199 (HChar
*)(Addr
)ARG1
, SARG2
);
3200 PRE_REG_READ2(long, "access", const char *, pathname
, int, mode
);
3201 PRE_MEM_RASCIIZ( "access(pathname)", ARG1
);
3206 PRINT("sys_alarm ( %" FMT_REGWORD
"u )", ARG1
);
3207 PRE_REG_READ1(unsigned long, "alarm", unsigned int, seconds
);
3212 Addr brk_limit
= VG_(brk_limit
);
3215 /* libc says: int brk(void *end_data_segment);
3216 kernel says: void* brk(void* end_data_segment); (more or less)
3218 libc returns 0 on success, and -1 (and sets errno) on failure.
3219 Nb: if you ask to shrink the dataseg end below what it
3220 currently is, that always succeeds, even if the dataseg end
3221 doesn't actually change (eg. brk(0)). Unless it seg faults.
3223 Kernel returns the new dataseg end. If the brk() failed, this
3224 will be unchanged from the old one. That's why calling (kernel)
3225 brk(0) gives the current dataseg end (libc brk() just returns
3228 Both will seg fault if you shrink it back into a text segment.
3230 PRINT("sys_brk ( %#" FMT_REGWORD
"x )", ARG1
);
3231 PRE_REG_READ1(unsigned long, "brk", unsigned long, end_data_segment
);
3233 brk_new
= do_brk(ARG1
, tid
);
3234 SET_STATUS_Success( brk_new
);
3236 if (brk_new
== ARG1
) {
3237 /* brk() succeeded */
3238 if (brk_new
< brk_limit
) {
3239 /* successfully shrunk the data segment. */
3240 VG_TRACK( die_mem_brk
, (Addr
)ARG1
,
3243 if (brk_new
> brk_limit
) {
3244 /* successfully grew the data segment */
3245 VG_TRACK( new_mem_brk
, brk_limit
,
3246 ARG1
-brk_limit
, tid
);
3250 vg_assert(brk_limit
== brk_new
);
3256 FUSE_COMPATIBLE_MAY_BLOCK();
3257 PRINT("sys_chdir ( %#" FMT_REGWORD
"x(%s) )", ARG1
,(char*)(Addr
)ARG1
);
3258 PRE_REG_READ1(long, "chdir", const char *, path
);
3259 PRE_MEM_RASCIIZ( "chdir(path)", ARG1
);
3264 FUSE_COMPATIBLE_MAY_BLOCK();
3265 PRINT("sys_chmod ( %#" FMT_REGWORD
"x(%s), %" FMT_REGWORD
"u )", ARG1
,
3266 (HChar
*)(Addr
)ARG1
, ARG2
);
3267 PRE_REG_READ2(long, "chmod", const char *, path
, vki_mode_t
, mode
);
3268 PRE_MEM_RASCIIZ( "chmod(path)", ARG1
);
3273 FUSE_COMPATIBLE_MAY_BLOCK();
3274 PRINT("sys_chown ( %#" FMT_REGWORD
"x(%s), 0x%" FMT_REGWORD
"x, 0x%"
3275 FMT_REGWORD
"x )", ARG1
,(char*)(Addr
)ARG1
,ARG2
,ARG3
);
3276 PRE_REG_READ3(long, "chown",
3277 const char *, path
, vki_uid_t
, owner
, vki_gid_t
, group
);
3278 PRE_MEM_RASCIIZ( "chown(path)", ARG1
);
3283 FUSE_COMPATIBLE_MAY_BLOCK();
3284 PRINT("sys_lchown ( %#" FMT_REGWORD
"x(%s), 0x%" FMT_REGWORD
"x, 0x%"
3285 FMT_REGWORD
"x )", ARG1
,(char*)(Addr
)ARG1
,ARG2
,ARG3
);
3286 PRE_REG_READ3(long, "lchown",
3287 const char *, path
, vki_uid_t
, owner
, vki_gid_t
, group
);
3288 PRE_MEM_RASCIIZ( "lchown(path)", ARG1
);
3293 FUSE_COMPATIBLE_MAY_BLOCK();
3294 PRINT("sys_close ( %" FMT_REGWORD
"u )", ARG1
);
3295 PRE_REG_READ1(long, "close", unsigned int, fd
);
3297 /* Detect and negate attempts by the client to close Valgrind's log fd */
3298 if ( (!ML_(fd_allowed
)(ARG1
, "close", tid
, False
))
3299 /* If doing -d style logging (which is to fd=2), don't
3300 allow that to be closed either. */
3301 || (ARG1
== 2/*stderr*/ && VG_(debugLog_getLevel
)() > 0) )
3302 SET_STATUS_Failure( VKI_EBADF
);
3307 if (VG_(clo_track_fds
)) ML_(record_fd_close
)(ARG1
);
3312 PRINT("sys_dup ( %" FMT_REGWORD
"u )", ARG1
);
3313 PRE_REG_READ1(long, "dup", unsigned int, oldfd
);
3319 if (!ML_(fd_allowed
)(RES
, "dup", tid
, True
)) {
3321 SET_STATUS_Failure( VKI_EMFILE
);
3323 if (VG_(clo_track_fds
))
3324 ML_(record_fd_open_named
)(tid
, RES
);
3330 PRINT("sys_dup2 ( %" FMT_REGWORD
"u, %" FMT_REGWORD
"u )", ARG1
, ARG2
);
3331 PRE_REG_READ2(long, "dup2", unsigned int, oldfd
, unsigned int, newfd
);
3332 if (!ML_(fd_allowed
)(ARG2
, "dup2", tid
, True
))
3333 SET_STATUS_Failure( VKI_EBADF
);
3339 if (VG_(clo_track_fds
))
3340 ML_(record_fd_open_named
)(tid
, RES
);
3345 FUSE_COMPATIBLE_MAY_BLOCK();
3346 PRINT("sys_fchdir ( %" FMT_REGWORD
"u )", ARG1
);
3347 PRE_REG_READ1(long, "fchdir", unsigned int, fd
);
3352 FUSE_COMPATIBLE_MAY_BLOCK();
3353 PRINT("sys_fchown ( %" FMT_REGWORD
"u, %" FMT_REGWORD
"u, %"
3354 FMT_REGWORD
"u )", ARG1
, ARG2
, ARG3
);
3355 PRE_REG_READ3(long, "fchown",
3356 unsigned int, fd
, vki_uid_t
, owner
, vki_gid_t
, group
);
3361 FUSE_COMPATIBLE_MAY_BLOCK();
3362 PRINT("sys_fchmod ( %" FMT_REGWORD
"u, %" FMT_REGWORD
"u )", ARG1
, ARG2
);
3363 PRE_REG_READ2(long, "fchmod", unsigned int, fildes
, vki_mode_t
, mode
);
3366 #if !defined(VGP_nanomips_linux)
3369 FUSE_COMPATIBLE_MAY_BLOCK();
3370 PRINT("sys_newfstat ( %" FMT_REGWORD
"u, %#" FMT_REGWORD
"x )", ARG1
, ARG2
);
3371 PRE_REG_READ2(long, "fstat", unsigned int, fd
, struct stat
*, buf
);
3372 PRE_MEM_WRITE( "fstat(buf)", ARG2
, sizeof(struct vki_stat
) );
3377 POST_MEM_WRITE( ARG2
, sizeof(struct vki_stat
) );
3381 #if !defined(VGO_solaris) && !defined(VGP_arm64_linux) && \
3382 !defined(VGP_nanomips_linux)
3383 static vki_sigset_t fork_saved_mask
;
3385 // In Linux, the sys_fork() function varies across architectures, but we
3386 // ignore the various args it gets, and so it looks arch-neutral. Hmm.
3393 PRINT("sys_fork ( )");
3394 PRE_REG_READ0(long, "fork");
3396 /* Block all signals during fork, so that we can fix things up in
3397 the child without being interrupted. */
3398 VG_(sigfillset
)(&mask
);
3399 VG_(sigprocmask
)(VKI_SIG_SETMASK
, &mask
, &fork_saved_mask
);
3401 VG_(do_atfork_pre
)(tid
);
3403 SET_STATUS_from_SysRes( VG_(do_syscall0
)(__NR_fork
) );
3405 if (!SUCCESS
) return;
3407 #if defined(VGO_linux)
3408 // RES is 0 for child, non-0 (the child's PID) for parent.
3409 is_child
= ( RES
== 0 ? True
: False
);
3410 child_pid
= ( is_child
? -1 : RES
);
3411 #elif defined(VGO_darwin)
3412 // RES is the child's pid. RESHI is 1 for child, 0 for parent.
3420 VG_(do_atfork_child
)(tid
);
3422 /* restore signal mask */
3423 VG_(sigprocmask
)(VKI_SIG_SETMASK
, &fork_saved_mask
, NULL
);
3425 VG_(do_atfork_parent
)(tid
);
3427 PRINT(" fork: process %d created child %d\n", VG_(getpid
)(), child_pid
);
3429 /* restore signal mask */
3430 VG_(sigprocmask
)(VKI_SIG_SETMASK
, &fork_saved_mask
, NULL
);
3433 #endif // !defined(VGO_solaris) && !defined(VGP_arm64_linux)
3437 *flags
|= SfMayBlock
;
3438 PRINT("sys_ftruncate ( %" FMT_REGWORD
"u, %" FMT_REGWORD
"u )", ARG1
, ARG2
);
3439 PRE_REG_READ2(long, "ftruncate", unsigned int, fd
, unsigned long, length
);
3444 *flags
|= SfMayBlock
;
3445 PRINT("sys_truncate ( %#" FMT_REGWORD
"x(%s), %" FMT_REGWORD
"u )",
3446 ARG1
, (HChar
*)(Addr
)ARG1
, ARG2
);
3447 PRE_REG_READ2(long, "truncate",
3448 const char *, path
, unsigned long, length
);
3449 PRE_MEM_RASCIIZ( "truncate(path)", ARG1
);
3452 PRE(sys_ftruncate64
)
3454 *flags
|= SfMayBlock
;
3455 #if VG_WORDSIZE == 4
3456 PRINT("sys_ftruncate64 ( %" FMT_REGWORD
"u, %llu )", ARG1
,
3457 MERGE64(ARG2
,ARG3
));
3458 PRE_REG_READ3(long, "ftruncate64",
3460 UWord
, MERGE64_FIRST(length
), UWord
, MERGE64_SECOND(length
));
3462 PRINT("sys_ftruncate64 ( %lu, %lu )", ARG1
, ARG2
);
3463 PRE_REG_READ2(long, "ftruncate64",
3464 unsigned int,fd
, UWord
,length
);
3470 *flags
|= SfMayBlock
;
3471 #if VG_WORDSIZE == 4
3472 PRINT("sys_truncate64 ( %#" FMT_REGWORD
"x, %lld )", ARG1
,
3473 (Long
)MERGE64(ARG2
, ARG3
));
3474 PRE_REG_READ3(long, "truncate64",
3476 UWord
, MERGE64_FIRST(length
), UWord
, MERGE64_SECOND(length
));
3478 PRINT("sys_truncate64 ( %#lx, %lld )", ARG1
, (Long
)ARG2
);
3479 PRE_REG_READ2(long, "truncate64",
3480 const char *,path
, UWord
,length
);
3482 PRE_MEM_RASCIIZ( "truncate64(path)", ARG1
);
3487 *flags
|= SfMayBlock
;
3488 PRINT("sys_getdents ( %" FMT_REGWORD
"u, %#" FMT_REGWORD
"x, %" FMT_REGWORD
3489 "u )", ARG1
, ARG2
, ARG3
);
3490 PRE_REG_READ3(long, "getdents",
3491 unsigned int, fd
, struct vki_dirent
*, dirp
,
3492 unsigned int, count
);
3493 PRE_MEM_WRITE( "getdents(dirp)", ARG2
, ARG3
);
3500 POST_MEM_WRITE( ARG2
, RES
);
3505 *flags
|= SfMayBlock
;
3506 PRINT("sys_getdents64 ( %" FMT_REGWORD
"u, %#" FMT_REGWORD
"x, %"
3507 FMT_REGWORD
"u )",ARG1
, ARG2
, ARG3
);
3508 PRE_REG_READ3(long, "getdents64",
3509 unsigned int, fd
, struct vki_dirent64
*, dirp
,
3510 unsigned int, count
);
3511 PRE_MEM_WRITE( "getdents64(dirp)", ARG2
, ARG3
);
3514 POST(sys_getdents64
)
3518 POST_MEM_WRITE( ARG2
, RES
);
3523 PRINT("sys_getgroups ( %ld, %#" FMT_REGWORD
"x )", SARG1
, ARG2
);
3524 PRE_REG_READ2(long, "getgroups", int, size
, vki_gid_t
*, list
);
3526 PRE_MEM_WRITE( "getgroups(list)", ARG2
, ARG1
* sizeof(vki_gid_t
) );
3532 if (ARG1
> 0 && RES
> 0)
3533 POST_MEM_WRITE( ARG2
, RES
* sizeof(vki_gid_t
) );
3538 // Comment from linux/fs/dcache.c:
3539 // NOTE! The user-level library version returns a character pointer.
3540 // The kernel system call just returns the length of the buffer filled
3541 // (which includes the ending '\0' character), or a negative error
3543 // Is this Linux-specific? If so it should be moved to syswrap-linux.c.
3544 PRINT("sys_getcwd ( %#" FMT_REGWORD
"x, %llu )", ARG1
,(ULong
)ARG2
);
3545 PRE_REG_READ2(long, "getcwd", char *, buf
, unsigned long, size
);
3546 PRE_MEM_WRITE( "getcwd(buf)", ARG1
, ARG2
);
3552 if (RES
!= (Addr
)NULL
)
3553 POST_MEM_WRITE( ARG1
, RES
);
3558 PRINT("sys_geteuid ( )");
3559 PRE_REG_READ0(long, "geteuid");
3564 PRINT("sys_getegid ( )");
3565 PRE_REG_READ0(long, "getegid");
3570 PRINT("sys_getgid ( )");
3571 PRE_REG_READ0(long, "getgid");
3576 PRINT("sys_getpid ()");
3577 PRE_REG_READ0(long, "getpid");
3582 PRINT("sys_getpgid ( %ld )", SARG1
);
3583 PRE_REG_READ1(long, "getpgid", vki_pid_t
, pid
);
3588 PRINT("sys_getpgrp ()");
3589 PRE_REG_READ0(long, "getpgrp");
3594 PRINT("sys_getppid ()");
3595 PRE_REG_READ0(long, "getppid");
3598 static void common_post_getrlimit(ThreadId tid
, UWord a1
, UWord a2
)
3600 POST_MEM_WRITE( a2
, sizeof(struct vki_rlimit
) );
3602 #ifdef _RLIMIT_POSIX_FLAG
3603 // Darwin will sometimes set _RLIMIT_POSIX_FLAG on getrlimit calls.
3604 // Unset it here to make the switch case below work correctly.
3605 a1
&= ~_RLIMIT_POSIX_FLAG
;
3609 case VKI_RLIMIT_NOFILE
:
3610 ((struct vki_rlimit
*)a2
)->rlim_cur
= VG_(fd_soft_limit
);
3611 ((struct vki_rlimit
*)a2
)->rlim_max
= VG_(fd_hard_limit
);
3614 case VKI_RLIMIT_DATA
:
3615 *((struct vki_rlimit
*)a2
) = VG_(client_rlimit_data
);
3618 case VKI_RLIMIT_STACK
:
3619 *((struct vki_rlimit
*)a2
) = VG_(client_rlimit_stack
);
3624 PRE(sys_old_getrlimit
)
3626 PRINT("sys_old_getrlimit ( %" FMT_REGWORD
"u, %#" FMT_REGWORD
"x )",
3628 PRE_REG_READ2(long, "old_getrlimit",
3629 unsigned int, resource
, struct rlimit
*, rlim
);
3630 PRE_MEM_WRITE( "old_getrlimit(rlim)", ARG2
, sizeof(struct vki_rlimit
) );
3633 POST(sys_old_getrlimit
)
3635 common_post_getrlimit(tid
, ARG1
, ARG2
);
3640 PRINT("sys_getrlimit ( %" FMT_REGWORD
"u, %#" FMT_REGWORD
"x )", ARG1
, ARG2
);
3641 PRE_REG_READ2(long, "getrlimit",
3642 unsigned int, resource
, struct rlimit
*, rlim
);
3643 PRE_MEM_WRITE( "getrlimit(rlim)", ARG2
, sizeof(struct vki_rlimit
) );
3648 common_post_getrlimit(tid
, ARG1
, ARG2
);
3653 PRINT("sys_getrusage ( %ld, %#" FMT_REGWORD
"x )", SARG1
, ARG2
);
3654 PRE_REG_READ2(long, "getrusage", int, who
, struct rusage
*, usage
);
3655 PRE_MEM_WRITE( "getrusage(usage)", ARG2
, sizeof(struct vki_rusage
) );
3662 POST_MEM_WRITE( ARG2
, sizeof(struct vki_rusage
) );
3665 PRE(sys_gettimeofday
)
3667 PRINT("sys_gettimeofday ( %#" FMT_REGWORD
"x, %#" FMT_REGWORD
"x )",
3669 PRE_REG_READ2(long, "gettimeofday",
3670 struct timeval
*, tv
, struct timezone
*, tz
);
3671 // GrP fixme does darwin write to *tz anymore?
3673 PRE_timeval_WRITE( "gettimeofday(tv)", (Addr
)ARG1
);
3675 PRE_MEM_WRITE( "gettimeofday(tz)", ARG2
, sizeof(struct vki_timezone
) );
3678 POST(sys_gettimeofday
)
3683 POST_timeval_WRITE( (Addr
)ARG1
);
3685 POST_MEM_WRITE( ARG2
, sizeof(struct vki_timezone
) );
3689 PRE(sys_settimeofday
)
3691 PRINT("sys_settimeofday ( %#" FMT_REGWORD
"x, %#" FMT_REGWORD
"x )",
3693 PRE_REG_READ2(long, "settimeofday",
3694 struct timeval
*, tv
, struct timezone
*, tz
);
3696 PRE_timeval_READ( "settimeofday(tv)", (Addr
)ARG1
);
3698 PRE_MEM_READ( "settimeofday(tz)", ARG2
, sizeof(struct vki_timezone
) );
3699 /* maybe should warn if tz->tz_dsttime is non-zero? */
3705 PRINT("sys_getuid ( )");
3706 PRE_REG_READ0(long, "getuid");
3709 void ML_(PRE_unknown_ioctl
)(ThreadId tid
, UWord request
, UWord arg
)
3711 /* We don't have any specific information on it, so
3712 try to do something reasonable based on direction and
3713 size bits. The encoding scheme is described in
3714 /usr/include/asm/ioctl.h or /usr/include/sys/ioccom.h .
3716 According to Simon Hausmann, _IOC_READ means the kernel
3717 writes a value to the ioctl value passed from the user
3718 space and the other way around with _IOC_WRITE. */
3720 #if defined(VGO_solaris)
3721 /* Majority of Solaris ioctl requests does not honour direction hints. */
3722 UInt dir
= _VKI_IOC_NONE
;
3724 UInt dir
= _VKI_IOC_DIR(request
);
3726 UInt size
= _VKI_IOC_SIZE(request
);
3728 if (SimHintiS(SimHint_lax_ioctls
, VG_(clo_sim_hints
))) {
3730 * Be very lax about ioctl handling; the only
3731 * assumption is that the size is correct. Doesn't
3732 * require the full buffer to be initialized when
3733 * writing. Without this, using some device
3734 * drivers with a large number of strange ioctl
3735 * commands becomes very tiresome.
3737 } else if (/* size == 0 || */ dir
== _VKI_IOC_NONE
) {
3738 static UWord unknown_ioctl
[10];
3739 static Int moans
= sizeof(unknown_ioctl
) / sizeof(unknown_ioctl
[0]);
3741 if (moans
> 0 && !VG_(clo_xml
)) {
3742 /* Check if have not already moaned for this request. */
3744 for (i
= 0; i
< sizeof(unknown_ioctl
)/sizeof(unknown_ioctl
[0]); i
++) {
3745 if (unknown_ioctl
[i
] == request
)
3747 if (unknown_ioctl
[i
] == 0) {
3748 unknown_ioctl
[i
] = request
;
3750 VG_(umsg
)("Warning: noted but unhandled ioctl 0x%lx"
3751 " with no size/direction hints.\n", request
);
3752 VG_(umsg
)(" This could cause spurious value errors to appear.\n");
3753 VG_(umsg
)(" See README_MISSING_SYSCALL_OR_IOCTL for "
3754 "guidance on writing a proper wrapper.\n" );
3755 //VG_(get_and_pp_StackTrace)(tid, VG_(clo_backtrace_size));
3761 //VG_(message)(Vg_UserMsg, "UNKNOWN ioctl %#lx\n", request);
3762 //VG_(get_and_pp_StackTrace)(tid, VG_(clo_backtrace_size));
3763 if ((dir
& _VKI_IOC_WRITE
) && size
> 0)
3764 PRE_MEM_READ( "ioctl(generic)", arg
, size
);
3765 if ((dir
& _VKI_IOC_READ
) && size
> 0)
3766 PRE_MEM_WRITE( "ioctl(generic)", arg
, size
);
3770 void ML_(POST_unknown_ioctl
)(ThreadId tid
, UInt res
, UWord request
, UWord arg
)
3772 /* We don't have any specific information on it, so
3773 try to do something reasonable based on direction and
3774 size bits. The encoding scheme is described in
3775 /usr/include/asm/ioctl.h or /usr/include/sys/ioccom.h .
3777 According to Simon Hausmann, _IOC_READ means the kernel
3778 writes a value to the ioctl value passed from the user
3779 space and the other way around with _IOC_WRITE. */
3781 UInt dir
= _VKI_IOC_DIR(request
);
3782 UInt size
= _VKI_IOC_SIZE(request
);
3783 if (size
> 0 && (dir
& _VKI_IOC_READ
)
3785 && arg
!= (Addr
)NULL
) {
3786 POST_MEM_WRITE(arg
, size
);
3791 If we're sending a SIGKILL to one of our own threads, then simulate
3792 it rather than really sending the signal, so that the target thread
3793 gets a chance to clean up. Returns True if we did the killing (or
3794 no killing is necessary), and False if the caller should use the
3795 normal kill syscall.
3797 "pid" is any pid argument which can be passed to kill; group kills
3798 (< -1, 0), and owner kills (-1) are ignored, on the grounds that
3799 they'll most likely hit all the threads and we won't need to worry
3800 about cleanup. In truth, we can't fully emulate these multicast
3803 "tgid" is a thread group id. If it is not -1, then the target
3804 thread must be in that thread group.
3806 Bool
ML_(do_sigkill
)(Int pid
, Int tgid
)
3814 tid
= VG_(lwpid_to_vgtid
)(pid
);
3815 if (tid
== VG_INVALID_THREADID
)
3816 return False
; /* none of our threads */
3818 tst
= VG_(get_ThreadState
)(tid
);
3819 if (tst
== NULL
|| tst
->status
== VgTs_Empty
)
3820 return False
; /* hm, shouldn't happen */
3822 if (tgid
!= -1 && tst
->os_state
.threadgroup
!= tgid
)
3823 return False
; /* not the right thread group */
3825 /* Fatal SIGKILL sent to one of our threads.
3826 "Handle" the signal ourselves, as trying to have tid
3827 handling the signal causes termination problems (see #409367
3829 Moreover, as a process cannot do anything when receiving SIGKILL,
3830 it is not particularly crucial that "tid" does the work to
3831 terminate the process. */
3833 if (VG_(clo_trace_signals
))
3834 VG_(message
)(Vg_DebugMsg
,
3835 "Thread %u %s being killed with SIGKILL, running tid: %u\n",
3836 tst
->tid
, VG_(name_of_ThreadStatus
) (tst
->status
), VG_(running_tid
));
3838 if (!VG_(is_running_thread
)(tid
))
3839 tst
= VG_(get_ThreadState
)(VG_(running_tid
));
3840 VG_(nuke_all_threads_except
) (VG_(running_tid
), VgSrc_FatalSig
);
3841 VG_(reap_threads
)(VG_(running_tid
));
3842 tst
->exitreason
= VgSrc_FatalSig
;
3843 tst
->os_state
.fatalsig
= VKI_SIGKILL
;
3850 PRINT("sys_kill ( %ld, %ld )", SARG1
, SARG2
);
3851 PRE_REG_READ2(long, "kill", int, pid
, int, signal
);
3852 if (!ML_(client_signal_OK
)(ARG2
)) {
3853 SET_STATUS_Failure( VKI_EINVAL
);
3857 /* If we're sending SIGKILL, check to see if the target is one of
3858 our threads and handle it specially. */
3859 if (ARG2
== VKI_SIGKILL
&& ML_(do_sigkill
)(ARG1
, -1))
3860 SET_STATUS_Success(0);
3862 /* re syscall3: Darwin has a 3rd arg, which is a flag (boolean)
3863 affecting how posix-compliant the call is. I guess it is
3864 harmless to pass the 3rd arg on other platforms; hence pass
3866 SET_STATUS_from_SysRes( VG_(do_syscall3
)(SYSNO
, ARG1
, ARG2
, ARG3
) );
3868 if (VG_(clo_trace_signals
))
3869 VG_(message
)(Vg_DebugMsg
, "kill: sent signal %ld to pid %ld\n",
3872 /* This kill might have given us a pending signal. Ask for a check once
3873 the syscall is done. */
3874 *flags
|= SfPollAfter
;
3879 *flags
|= SfMayBlock
;
3880 PRINT("sys_link ( %#" FMT_REGWORD
"x(%s), %#" FMT_REGWORD
"x(%s) )", ARG1
,
3881 (char*)(Addr
)ARG1
,ARG2
,(char*)(Addr
)ARG2
);
3882 PRE_REG_READ2(long, "link", const char *, oldpath
, const char *, newpath
);
3883 PRE_MEM_RASCIIZ( "link(oldpath)", ARG1
);
3884 PRE_MEM_RASCIIZ( "link(newpath)", ARG2
);
3887 #if !defined(VGP_nanomips_linux)
3890 PRINT("sys_newlstat ( %#" FMT_REGWORD
"x(%s), %#" FMT_REGWORD
"x )", ARG1
,
3891 (char*)(Addr
)ARG1
,ARG2
);
3892 PRE_REG_READ2(long, "lstat", char *, file_name
, struct stat
*, buf
);
3893 PRE_MEM_RASCIIZ( "lstat(file_name)", ARG1
);
3894 PRE_MEM_WRITE( "lstat(buf)", ARG2
, sizeof(struct vki_stat
) );
3900 POST_MEM_WRITE( ARG2
, sizeof(struct vki_stat
) );
3906 *flags
|= SfMayBlock
;
3907 PRINT("sys_mkdir ( %#" FMT_REGWORD
"x(%s), %ld )", ARG1
,
3908 (HChar
*)(Addr
)ARG1
, SARG2
);
3909 PRE_REG_READ2(long, "mkdir", const char *, pathname
, int, mode
);
3910 PRE_MEM_RASCIIZ( "mkdir(pathname)", ARG1
);
3915 PRINT("sys_mprotect ( %#" FMT_REGWORD
"x, %" FMT_REGWORD
"u, %"
3916 FMT_REGWORD
"u )", ARG1
, ARG2
, ARG3
);
3917 PRE_REG_READ3(long, "mprotect",
3918 unsigned long, addr
, vki_size_t
, len
, unsigned long, prot
);
3924 handle_sys_mprotect (tid
, status
, &addr
, &len
, &prot
);
3930 /* This will be called from the generic mprotect, or the linux specific
3931 pkey_mprotect. Pass pointers to ARG1, ARG2 and ARG3 as addr, len and prot,
3932 they might be adjusted and have to assigned back to ARG1, ARG2 and ARG3. */
3933 void handle_sys_mprotect(ThreadId tid
, SyscallStatus
* status
,
3934 Addr
*addr
, SizeT
*len
, Int
*prot
)
3936 if (!ML_(valid_client_addr
)(*addr
, *len
, tid
, "mprotect")) {
3937 SET_STATUS_Failure( VKI_ENOMEM
);
3939 #if defined(VKI_PROT_GROWSDOWN)
3941 if (*prot
& (VKI_PROT_GROWSDOWN
|VKI_PROT_GROWSUP
)) {
3942 /* Deal with mprotects on growable stack areas.
3944 The critical files to understand all this are mm/mprotect.c
3945 in the kernel and sysdeps/unix/sysv/linux/dl-execstack.c in
3948 The kernel provides PROT_GROWSDOWN and PROT_GROWSUP which
3949 round the start/end address of mprotect to the start/end of
3950 the underlying vma and glibc uses that as an easy way to
3951 change the protection of the stack by calling mprotect on the
3952 last page of the stack with PROT_GROWSDOWN set.
3954 The sanity check provided by the kernel is that the vma must
3955 have the VM_GROWSDOWN/VM_GROWSUP flag set as appropriate. */
3956 UInt grows
= *prot
& (VKI_PROT_GROWSDOWN
|VKI_PROT_GROWSUP
);
3957 NSegment
const *aseg
= VG_(am_find_nsegment
)(*addr
);
3958 NSegment
const *rseg
;
3962 if (grows
== VKI_PROT_GROWSDOWN
) {
3963 rseg
= VG_(am_next_nsegment
)( aseg
, False
/*backwards*/ );
3965 && rseg
->kind
== SkResvn
3966 && rseg
->smode
== SmUpper
3967 && rseg
->end
+1 == aseg
->start
) {
3968 Addr end
= *addr
+ *len
;
3969 *addr
= aseg
->start
;
3970 *len
= end
- aseg
->start
;
3971 *prot
&= ~VKI_PROT_GROWSDOWN
;
3973 SET_STATUS_Failure( VKI_EINVAL
);
3975 } else if (grows
== VKI_PROT_GROWSUP
) {
3976 rseg
= VG_(am_next_nsegment
)( aseg
, True
/*forwards*/ );
3978 && rseg
->kind
== SkResvn
3979 && rseg
->smode
== SmLower
3980 && aseg
->end
+1 == rseg
->start
) {
3981 *len
= aseg
->end
- *addr
+ 1;
3982 *prot
&= ~VKI_PROT_GROWSUP
;
3984 SET_STATUS_Failure( VKI_EINVAL
);
3987 /* both GROWSUP and GROWSDOWN */
3988 SET_STATUS_Failure( VKI_EINVAL
);
3991 #endif // defined(VKI_PROT_GROWSDOWN)
4000 ML_(notify_core_and_tool_of_mprotect
)(a
, len
, prot
);
4005 if (0) VG_(printf
)(" munmap( %#" FMT_REGWORD
"x )\n", ARG1
);
4006 PRINT("sys_munmap ( %#" FMT_REGWORD
"x, %llu )", ARG1
,(ULong
)ARG2
);
4007 PRE_REG_READ2(long, "munmap", unsigned long, start
, vki_size_t
, length
);
4009 if (!ML_(valid_client_addr
)(ARG1
, ARG2
, tid
, "munmap"))
4010 SET_STATUS_Failure( VKI_EINVAL
);
4018 ML_(notify_core_and_tool_of_munmap
)( a
, len
);
4023 PRINT("sys_mincore ( %#" FMT_REGWORD
"x, %llu, %#" FMT_REGWORD
"x )",
4024 ARG1
, (ULong
)ARG2
, ARG3
);
4025 PRE_REG_READ3(long, "mincore",
4026 unsigned long, start
, vki_size_t
, length
,
4027 unsigned char *, vec
);
4028 PRE_MEM_WRITE( "mincore(vec)", ARG3
, VG_PGROUNDUP(ARG2
) / VKI_PAGE_SIZE
);
4032 POST_MEM_WRITE( ARG3
, VG_PGROUNDUP(ARG2
) / VKI_PAGE_SIZE
);
4037 *flags
|= SfMayBlock
|SfPostOnFail
;
4038 PRINT("sys_nanosleep ( %#" FMT_REGWORD
"x, %#" FMT_REGWORD
"x )", ARG1
,ARG2
);
4039 PRE_REG_READ2(long, "nanosleep",
4040 struct timespec
*, req
, struct timespec
*, rem
);
4041 PRE_MEM_READ( "nanosleep(req)", ARG1
, sizeof(struct vki_timespec
) );
4043 PRE_MEM_WRITE( "nanosleep(rem)", ARG2
, sizeof(struct vki_timespec
) );
4048 vg_assert(SUCCESS
|| FAILURE
);
4049 if (ARG2
!= 0 && FAILURE
&& ERR
== VKI_EINTR
)
4050 POST_MEM_WRITE( ARG2
, sizeof(struct vki_timespec
) );
4053 #if defined(VGO_linux) || defined(VGO_solaris)
4054 /* Handles the case where the open is of /proc/self/auxv or
4055 /proc/<pid>/auxv, and just gives out a copy of the fd for the
4056 fake file we cooked up at startup (in m_main). Also, seeks the
4057 cloned fd back to the start.
4058 Returns True if auxv open was handled (status is set). */
4059 Bool
ML_(handle_auxv_open
)(SyscallStatus
*status
, const HChar
*filename
,
4062 HChar name
[30]; // large enough
4064 if (!ML_(safe_to_deref
)((const void *) filename
, 1))
4067 /* Opening /proc/<pid>/auxv or /proc/self/auxv? */
4068 VG_(sprintf
)(name
, "/proc/%d/auxv", VG_(getpid
)());
4069 if (!VG_STREQ(filename
, name
) && !VG_STREQ(filename
, "/proc/self/auxv"))
4072 /* Allow to open the file only for reading. */
4073 if (flags
& (VKI_O_WRONLY
| VKI_O_RDWR
)) {
4074 SET_STATUS_Failure(VKI_EACCES
);
4078 # if defined(VGO_solaris)
4079 VG_(sprintf
)(name
, "/proc/self/fd/%d", VG_(cl_auxv_fd
));
4080 SysRes sres
= VG_(open
)(name
, flags
, 0);
4081 SET_STATUS_from_SysRes(sres
);
4083 SysRes sres
= VG_(dup
)(VG_(cl_auxv_fd
));
4084 SET_STATUS_from_SysRes(sres
);
4085 if (!sr_isError(sres
)) {
4086 OffT off
= VG_(lseek
)(sr_Res(sres
), 0, VKI_SEEK_SET
);
4088 SET_STATUS_Failure(VKI_EMFILE
);
4094 #endif // defined(VGO_linux) || defined(VGO_solaris)
4096 #if defined(VGO_linux)
4097 Bool
ML_(handle_self_exe_open
)(SyscallStatus
*status
, const HChar
*filename
,
4100 HChar name
[30]; // large enough for /proc/<int>/exe
4102 if (!ML_(safe_to_deref
)((const void *) filename
, 1))
4105 /* Opening /proc/<pid>/exe or /proc/self/exe? */
4106 VG_(sprintf
)(name
, "/proc/%d/exe", VG_(getpid
)());
4107 if (!VG_STREQ(filename
, name
) && !VG_STREQ(filename
, "/proc/self/exe"))
4110 /* Allow to open the file only for reading. */
4111 if (flags
& (VKI_O_WRONLY
| VKI_O_RDWR
)) {
4112 SET_STATUS_Failure(VKI_EACCES
);
4116 SysRes sres
= VG_(dup
)(VG_(cl_exec_fd
));
4117 SET_STATUS_from_SysRes(sres
);
4118 if (!sr_isError(sres
)) {
4119 OffT off
= VG_(lseek
)(sr_Res(sres
), 0, VKI_SEEK_SET
);
4121 SET_STATUS_Failure(VKI_EMFILE
);
4126 #endif // defined(VGO_linux)
4130 if (ARG2
& VKI_O_CREAT
) {
4132 PRINT("sys_open ( %#" FMT_REGWORD
"x(%s), %ld, %ld )",ARG1
,
4133 (HChar
*)(Addr
)ARG1
, SARG2
, SARG3
);
4134 PRE_REG_READ3(long, "open",
4135 const char *, filename
, int, flags
, int, mode
);
4138 PRINT("sys_open ( %#" FMT_REGWORD
"x(%s), %ld )",ARG1
,
4139 (HChar
*)(Addr
)ARG1
, SARG2
);
4140 PRE_REG_READ2(long, "open",
4141 const char *, filename
, int, flags
);
4143 PRE_MEM_RASCIIZ( "open(filename)", ARG1
);
4145 #if defined(VGO_linux)
4146 /* Handle the case where the open is of /proc/self/cmdline or
4147 /proc/<pid>/cmdline, and just give it a copy of the fd for the
4148 fake file we cooked up at startup (in m_main). Also, seek the
4149 cloned fd back to the start. */
4151 HChar name
[30]; // large enough
4152 HChar
* arg1s
= (HChar
*) (Addr
)ARG1
;
4155 VG_(sprintf
)(name
, "/proc/%d/cmdline", VG_(getpid
)());
4156 if (ML_(safe_to_deref
)( arg1s
, 1 )
4157 && (VG_STREQ(arg1s
, name
) || VG_STREQ(arg1s
, "/proc/self/cmdline"))) {
4158 sres
= VG_(dup
)( VG_(cl_cmdline_fd
) );
4159 SET_STATUS_from_SysRes( sres
);
4160 if (!sr_isError(sres
)) {
4161 OffT off
= VG_(lseek
)( sr_Res(sres
), 0, VKI_SEEK_SET
);
4163 SET_STATUS_Failure( VKI_EMFILE
);
4169 /* Handle also the case of /proc/self/auxv or /proc/<pid>/auxv
4170 or /proc/self/exe or /proc/<pid>/exe. */
4171 if (ML_(handle_auxv_open
)(status
, (const HChar
*)(Addr
)ARG1
, ARG2
)
4172 || ML_(handle_self_exe_open
)(status
, (const HChar
*)(Addr
)ARG1
, ARG2
))
4174 #endif // defined(VGO_linux)
4176 /* Otherwise handle normally */
4177 *flags
|= SfMayBlock
;
4183 if (!ML_(fd_allowed
)(RES
, "open", tid
, True
)) {
4185 SET_STATUS_Failure( VKI_EMFILE
);
4187 if (VG_(clo_track_fds
))
4188 ML_(record_fd_open_with_given_name
)(tid
, RES
, (HChar
*)(Addr
)ARG1
);
4194 *flags
|= SfMayBlock
;
4195 PRINT("sys_read ( %" FMT_REGWORD
"u, %#" FMT_REGWORD
"x, %"
4196 FMT_REGWORD
"u )", ARG1
, ARG2
, ARG3
);
4197 PRE_REG_READ3(ssize_t
, "read",
4198 unsigned int, fd
, char *, buf
, vki_size_t
, count
);
4200 if (!ML_(fd_allowed
)(ARG1
, "read", tid
, False
))
4201 SET_STATUS_Failure( VKI_EBADF
);
4203 PRE_MEM_WRITE( "read(buf)", ARG2
, ARG3
);
4209 POST_MEM_WRITE( ARG2
, RES
);
4215 *flags
|= SfMayBlock
;
4216 PRINT("sys_write ( %" FMT_REGWORD
"u, %#" FMT_REGWORD
"x, %"
4217 FMT_REGWORD
"u )", ARG1
, ARG2
, ARG3
);
4218 PRE_REG_READ3(ssize_t
, "write",
4219 unsigned int, fd
, const char *, buf
, vki_size_t
, count
);
4220 /* check to see if it is allowed. If not, try for an exemption from
4221 --sim-hints=enable-outer (used for self hosting). */
4222 ok
= ML_(fd_allowed
)(ARG1
, "write", tid
, False
);
4223 if (!ok
&& ARG1
== 2/*stderr*/
4224 && SimHintiS(SimHint_enable_outer
, VG_(clo_sim_hints
)))
4226 #if defined(VGO_solaris)
4227 if (!ok
&& VG_(vfork_fildes_addr
) != NULL
4228 && *VG_(vfork_fildes_addr
) >= 0 && *VG_(vfork_fildes_addr
) == ARG1
)
4232 SET_STATUS_Failure( VKI_EBADF
);
4234 PRE_MEM_READ( "write(buf)", ARG2
, ARG3
);
4239 *flags
|= SfMayBlock
;
4240 PRINT("sys_creat ( %#" FMT_REGWORD
"x(%s), %ld )", ARG1
,
4241 (HChar
*)(Addr
)ARG1
, SARG2
);
4242 PRE_REG_READ2(long, "creat", const char *, pathname
, int, mode
);
4243 PRE_MEM_RASCIIZ( "creat(pathname)", ARG1
);
4249 if (!ML_(fd_allowed
)(RES
, "creat", tid
, True
)) {
4251 SET_STATUS_Failure( VKI_EMFILE
);
4253 if (VG_(clo_track_fds
))
4254 ML_(record_fd_open_with_given_name
)(tid
, RES
, (HChar
*)(Addr
)ARG1
);
4261 int fd; -- file descriptor
4262 short events; -- requested events
4263 short revents; -- returned events
4265 int poll(struct pollfd *ufds, unsigned int nfds, int timeout)
4268 struct vki_pollfd
* ufds
= (struct vki_pollfd
*)(Addr
)ARG1
;
4269 *flags
|= SfMayBlock
;
4270 PRINT("sys_poll ( %#" FMT_REGWORD
"x, %" FMT_REGWORD
"u, %ld )\n",
4272 PRE_REG_READ3(long, "poll",
4273 struct vki_pollfd
*, ufds
, unsigned int, nfds
, long, timeout
);
4275 for (i
= 0; i
< ARG2
; i
++) {
4276 PRE_MEM_READ( "poll(ufds.fd)",
4277 (Addr
)(&ufds
[i
].fd
), sizeof(ufds
[i
].fd
) );
4278 PRE_MEM_READ( "poll(ufds.events)",
4279 (Addr
)(&ufds
[i
].events
), sizeof(ufds
[i
].events
) );
4280 PRE_MEM_WRITE( "poll(ufds.revents)",
4281 (Addr
)(&ufds
[i
].revents
), sizeof(ufds
[i
].revents
) );
4289 struct vki_pollfd
* ufds
= (struct vki_pollfd
*)(Addr
)ARG1
;
4290 for (i
= 0; i
< ARG2
; i
++)
4291 POST_MEM_WRITE( (Addr
)(&ufds
[i
].revents
), sizeof(ufds
[i
].revents
) );
4297 FUSE_COMPATIBLE_MAY_BLOCK();
4300 PRINT("sys_readlink ( %#" FMT_REGWORD
"x(%s), %#" FMT_REGWORD
"x, %llu )",
4301 ARG1
, (char*)(Addr
)ARG1
, ARG2
, (ULong
)ARG3
);
4302 PRE_REG_READ3(long, "readlink",
4303 const char *, path
, char *, buf
, int, bufsiz
);
4304 PRE_MEM_RASCIIZ( "readlink(path)", ARG1
);
4305 PRE_MEM_WRITE( "readlink(buf)", ARG2
,ARG3
);
4309 #if defined(VGO_linux) || defined(VGO_solaris)
4310 #if defined(VGO_linux)
4311 #define PID_EXEPATH "/proc/%d/exe"
4312 #define SELF_EXEPATH "/proc/self/exe"
4313 #define SELF_EXEFD "/proc/self/fd/%d"
4314 #elif defined(VGO_solaris)
4315 #define PID_EXEPATH "/proc/%d/path/a.out"
4316 #define SELF_EXEPATH "/proc/self/path/a.out"
4317 #define SELF_EXEFD "/proc/self/path/%d"
4320 * Handle the case where readlink is looking at /proc/self/exe or
4321 * /proc/<pid>/exe, or equivalent on Solaris.
4323 HChar name
[30]; // large enough
4324 HChar
* arg1s
= (HChar
*) (Addr
)ARG1
;
4325 VG_(sprintf
)(name
, PID_EXEPATH
, VG_(getpid
)());
4326 if (ML_(safe_to_deref
)(arg1s
, 1)
4327 && (VG_STREQ(arg1s
, name
) || VG_STREQ(arg1s
, SELF_EXEPATH
))) {
4328 VG_(sprintf
)(name
, SELF_EXEFD
, VG_(cl_exec_fd
));
4329 SET_STATUS_from_SysRes( VG_(do_syscall3
)(saved
, (UWord
)name
,
4335 SET_STATUS_from_SysRes( VG_(do_syscall3
)(saved
, ARG1
, ARG2
, ARG3
));
4339 if (SUCCESS
&& RES
> 0)
4340 POST_MEM_WRITE( ARG2
, RES
);
4346 struct vki_iovec
* vec
;
4347 *flags
|= SfMayBlock
;
4348 PRINT("sys_readv ( %" FMT_REGWORD
"u, %#" FMT_REGWORD
"x, %"
4349 FMT_REGWORD
"u )", ARG1
, ARG2
, ARG3
);
4350 PRE_REG_READ3(ssize_t
, "readv",
4351 unsigned long, fd
, const struct iovec
*, vector
,
4352 unsigned long, count
);
4353 if (!ML_(fd_allowed
)(ARG1
, "readv", tid
, False
)) {
4354 SET_STATUS_Failure( VKI_EBADF
);
4357 PRE_MEM_READ( "readv(vector)", ARG2
, ARG3
* sizeof(struct vki_iovec
) );
4359 if (ML_(safe_to_deref
)((const void*)ARG2
, ARG3
*sizeof(struct vki_iovec
*))) {
4360 vec
= (struct vki_iovec
*)(Addr
)ARG2
;
4361 for (i
= 0; i
< (Int
)ARG3
; i
++)
4362 PRE_MEM_WRITE( "readv(vector[...])",
4363 (Addr
)vec
[i
].iov_base
, vec
[i
].iov_len
);
4373 struct vki_iovec
* vec
= (struct vki_iovec
*)(Addr
)ARG2
;
4376 /* RES holds the number of bytes read. */
4377 for (i
= 0; i
< (Int
)ARG3
; i
++) {
4378 Int nReadThisBuf
= vec
[i
].iov_len
;
4379 if (nReadThisBuf
> remains
) nReadThisBuf
= remains
;
4380 POST_MEM_WRITE( (Addr
)vec
[i
].iov_base
, nReadThisBuf
);
4381 remains
-= nReadThisBuf
;
4382 if (remains
< 0) VG_(core_panic
)("readv: remains < 0");
4389 FUSE_COMPATIBLE_MAY_BLOCK();
4390 PRINT("sys_rename ( %#" FMT_REGWORD
"x(%s), %#" FMT_REGWORD
"x(%s) )", ARG1
,
4391 (char*)(Addr
)ARG1
,ARG2
,(char*)(Addr
)ARG2
);
4392 PRE_REG_READ2(long, "rename", const char *, oldpath
, const char *, newpath
);
4393 PRE_MEM_RASCIIZ( "rename(oldpath)", ARG1
);
4394 PRE_MEM_RASCIIZ( "rename(newpath)", ARG2
);
4399 *flags
|= SfMayBlock
;
4400 PRINT("sys_rmdir ( %#" FMT_REGWORD
"x(%s) )", ARG1
,(char*)(Addr
)ARG1
);
4401 PRE_REG_READ1(long, "rmdir", const char *, pathname
);
4402 PRE_MEM_RASCIIZ( "rmdir(pathname)", ARG1
);
4407 *flags
|= SfMayBlock
;
4408 PRINT("sys_select ( %ld, %#" FMT_REGWORD
"x, %#" FMT_REGWORD
"x, %#"
4409 FMT_REGWORD
"x, %#" FMT_REGWORD
"x )", SARG1
, ARG2
, ARG3
, ARG4
, ARG5
);
4410 PRE_REG_READ5(long, "select",
4411 int, n
, vki_fd_set
*, readfds
, vki_fd_set
*, writefds
,
4412 vki_fd_set
*, exceptfds
, struct vki_timeval
*, timeout
);
4413 // XXX: this possibly understates how much memory is read.
4415 PRE_MEM_READ( "select(readfds)",
4416 ARG2
, ARG1
/8 /* __FD_SETSIZE/8 */ );
4418 PRE_MEM_READ( "select(writefds)",
4419 ARG3
, ARG1
/8 /* __FD_SETSIZE/8 */ );
4421 PRE_MEM_READ( "select(exceptfds)",
4422 ARG4
, ARG1
/8 /* __FD_SETSIZE/8 */ );
4424 PRE_timeval_READ( "select(timeout)", (Addr
)ARG5
);
4429 PRINT("sys_setgid ( %" FMT_REGWORD
"u )", ARG1
);
4430 PRE_REG_READ1(long, "setgid", vki_gid_t
, gid
);
4435 PRINT("sys_setsid ( )");
4436 PRE_REG_READ0(long, "setsid");
4441 PRINT("setgroups ( %llu, %#" FMT_REGWORD
"x )", (ULong
)ARG1
, ARG2
);
4442 PRE_REG_READ2(long, "setgroups", int, size
, vki_gid_t
*, list
);
4444 PRE_MEM_READ( "setgroups(list)", ARG2
, ARG1
* sizeof(vki_gid_t
) );
4449 PRINT("setpgid ( %ld, %ld )", SARG1
, SARG2
);
4450 PRE_REG_READ2(long, "setpgid", vki_pid_t
, pid
, vki_pid_t
, pgid
);
4455 PRINT("sys_setregid ( %" FMT_REGWORD
"u, %" FMT_REGWORD
"u )", ARG1
, ARG2
);
4456 PRE_REG_READ2(long, "setregid", vki_gid_t
, rgid
, vki_gid_t
, egid
);
4461 PRINT("sys_setreuid ( 0x%" FMT_REGWORD
"x, 0x%" FMT_REGWORD
"x )",
4463 PRE_REG_READ2(long, "setreuid", vki_uid_t
, ruid
, vki_uid_t
, euid
);
4469 PRINT("sys_setrlimit ( %" FMT_REGWORD
"u, %#" FMT_REGWORD
"x )", ARG1
, ARG2
);
4470 PRE_REG_READ2(long, "setrlimit",
4471 unsigned int, resource
, struct rlimit
*, rlim
);
4472 PRE_MEM_READ( "setrlimit(rlim)", ARG2
, sizeof(struct vki_rlimit
) );
4474 #ifdef _RLIMIT_POSIX_FLAG
4475 // Darwin will sometimes set _RLIMIT_POSIX_FLAG on setrlimit calls.
4476 // Unset it here to make the if statements below work correctly.
4477 arg1
&= ~_RLIMIT_POSIX_FLAG
;
4480 if (!VG_(am_is_valid_for_client
)(ARG2
, sizeof(struct vki_rlimit
),
4482 SET_STATUS_Failure( VKI_EFAULT
);
4484 else if (((struct vki_rlimit
*)(Addr
)ARG2
)->rlim_cur
4485 > ((struct vki_rlimit
*)(Addr
)ARG2
)->rlim_max
) {
4486 SET_STATUS_Failure( VKI_EINVAL
);
4488 else if (arg1
== VKI_RLIMIT_NOFILE
) {
4489 if (((struct vki_rlimit
*)(Addr
)ARG2
)->rlim_cur
> VG_(fd_hard_limit
) ||
4490 ((struct vki_rlimit
*)(Addr
)ARG2
)->rlim_max
!= VG_(fd_hard_limit
)) {
4491 SET_STATUS_Failure( VKI_EPERM
);
4494 VG_(fd_soft_limit
) = ((struct vki_rlimit
*)(Addr
)ARG2
)->rlim_cur
;
4495 SET_STATUS_Success( 0 );
4498 else if (arg1
== VKI_RLIMIT_DATA
) {
4499 if (((struct vki_rlimit
*)(Addr
)ARG2
)->rlim_cur
4500 > VG_(client_rlimit_data
).rlim_max
||
4501 ((struct vki_rlimit
*)(Addr
)ARG2
)->rlim_max
4502 > VG_(client_rlimit_data
).rlim_max
) {
4503 SET_STATUS_Failure( VKI_EPERM
);
4506 VG_(client_rlimit_data
) = *(struct vki_rlimit
*)(Addr
)ARG2
;
4507 SET_STATUS_Success( 0 );
4510 else if (arg1
== VKI_RLIMIT_STACK
&& tid
== 1) {
4511 if (((struct vki_rlimit
*)(Addr
)ARG2
)->rlim_cur
4512 > VG_(client_rlimit_stack
).rlim_max
||
4513 ((struct vki_rlimit
*)(Addr
)ARG2
)->rlim_max
4514 > VG_(client_rlimit_stack
).rlim_max
) {
4515 SET_STATUS_Failure( VKI_EPERM
);
4518 /* Change the value of client_stack_szB to the rlim_cur value but
4519 only if it is smaller than the size of the allocated stack for the
4521 TODO: All platforms should set VG_(clstk_max_size) as part of their
4522 setup_client_stack(). */
4523 if ((VG_(clstk_max_size
) == 0)
4524 || (((struct vki_rlimit
*) (Addr
)ARG2
)->rlim_cur
<= VG_(clstk_max_size
)))
4525 VG_(threads
)[tid
].client_stack_szB
= ((struct vki_rlimit
*)(Addr
)ARG2
)->rlim_cur
;
4527 VG_(client_rlimit_stack
) = *(struct vki_rlimit
*)(Addr
)ARG2
;
4528 SET_STATUS_Success( 0 );
4535 PRINT("sys_setuid ( %" FMT_REGWORD
"u )", ARG1
);
4536 PRE_REG_READ1(long, "setuid", vki_uid_t
, uid
);
4539 #if !defined(VGP_nanomips_linux)
4542 FUSE_COMPATIBLE_MAY_BLOCK();
4543 PRINT("sys_newstat ( %#" FMT_REGWORD
"x(%s), %#" FMT_REGWORD
"x )",
4544 ARG1
,(char*)(Addr
)ARG1
,ARG2
);
4545 PRE_REG_READ2(long, "stat", char *, file_name
, struct stat
*, buf
);
4546 PRE_MEM_RASCIIZ( "stat(file_name)", ARG1
);
4547 PRE_MEM_WRITE( "stat(buf)", ARG2
, sizeof(struct vki_stat
) );
4552 POST_MEM_WRITE( ARG2
, sizeof(struct vki_stat
) );
4557 FUSE_COMPATIBLE_MAY_BLOCK();
4558 PRINT("sys_statfs ( %#" FMT_REGWORD
"x(%s), %#" FMT_REGWORD
"x )",
4559 ARG1
, (char*)(Addr
)ARG1
, ARG2
);
4560 PRE_REG_READ2(long, "statfs", const char *, path
, struct statfs
*, buf
);
4561 PRE_MEM_RASCIIZ( "statfs(path)", ARG1
);
4562 PRE_MEM_WRITE( "statfs(buf)", ARG2
, sizeof(struct vki_statfs
) );
4566 POST_MEM_WRITE( ARG2
, sizeof(struct vki_statfs
) );
4571 PRINT("sys_statfs64 ( %#" FMT_REGWORD
"x(%s), %llu, %#" FMT_REGWORD
"x )",
4572 ARG1
, (char*)(Addr
)ARG1
, (ULong
)ARG2
, ARG3
);
4573 PRE_REG_READ3(long, "statfs64",
4574 const char *, path
, vki_size_t
, size
, struct statfs64
*, buf
);
4575 PRE_MEM_RASCIIZ( "statfs64(path)", ARG1
);
4576 PRE_MEM_WRITE( "statfs64(buf)", ARG3
, ARG2
);
4580 POST_MEM_WRITE( ARG3
, ARG2
);
4586 *flags
|= SfMayBlock
;
4587 PRINT("sys_symlink ( %#" FMT_REGWORD
"x(%s), %#" FMT_REGWORD
"x(%s) )",
4588 ARG1
, (char*)(Addr
)ARG1
, ARG2
, (char*)(Addr
)ARG2
);
4589 PRE_REG_READ2(long, "symlink", const char *, oldpath
, const char *, newpath
);
4590 PRE_MEM_RASCIIZ( "symlink(oldpath)", ARG1
);
4591 PRE_MEM_RASCIIZ( "symlink(newpath)", ARG2
);
4596 /* time_t time(time_t *t); */
4597 PRINT("sys_time ( %#" FMT_REGWORD
"x )",ARG1
);
4598 PRE_REG_READ1(long, "time", int *, t
);
4600 PRE_MEM_WRITE( "time(t)", ARG1
, sizeof(vki_time_t
) );
4607 POST_MEM_WRITE( ARG1
, sizeof(vki_time_t
) );
4613 PRINT("sys_times ( %#" FMT_REGWORD
"x )", ARG1
);
4614 PRE_REG_READ1(long, "times", struct tms
*, buf
);
4616 PRE_MEM_WRITE( "times(buf)", ARG1
, sizeof(struct vki_tms
) );
4623 POST_MEM_WRITE( ARG1
, sizeof(struct vki_tms
) );
4629 PRINT("sys_umask ( %ld )", SARG1
);
4630 PRE_REG_READ1(long, "umask", int, mask
);
4635 *flags
|= SfMayBlock
;
4636 PRINT("sys_unlink ( %#" FMT_REGWORD
"x(%s) )", ARG1
,(char*)(Addr
)ARG1
);
4637 PRE_REG_READ1(long, "unlink", const char *, pathname
);
4638 PRE_MEM_RASCIIZ( "unlink(pathname)", ARG1
);
4643 PRINT("sys_newuname ( %#" FMT_REGWORD
"x )", ARG1
);
4644 PRE_REG_READ1(long, "uname", struct new_utsname
*, buf
);
4645 PRE_MEM_WRITE( "uname(buf)", ARG1
, sizeof(struct vki_new_utsname
) );
4651 POST_MEM_WRITE( ARG1
, sizeof(struct vki_new_utsname
) );
4657 *flags
|= SfMayBlock
;
4658 PRINT("sys_waitpid ( %ld, %#" FMT_REGWORD
"x, %ld )", SARG1
, ARG2
, SARG3
);
4659 PRE_REG_READ3(long, "waitpid",
4660 vki_pid_t
, pid
, unsigned int *, status
, int, options
);
4662 if (ARG2
!= (Addr
)NULL
)
4663 PRE_MEM_WRITE( "waitpid(status)", ARG2
, sizeof(int) );
4668 if (ARG2
!= (Addr
)NULL
)
4669 POST_MEM_WRITE( ARG2
, sizeof(int) );
4674 *flags
|= SfMayBlock
;
4675 PRINT("sys_wait4 ( %ld, %#" FMT_REGWORD
"x, %ld, %#" FMT_REGWORD
"x )",
4676 SARG1
, ARG2
, SARG3
, ARG4
);
4678 PRE_REG_READ4(long, "wait4",
4679 vki_pid_t
, pid
, unsigned int *, status
, int, options
,
4680 struct rusage
*, rusage
);
4681 if (ARG2
!= (Addr
)NULL
)
4682 PRE_MEM_WRITE( "wait4(status)", ARG2
, sizeof(int) );
4683 if (ARG4
!= (Addr
)NULL
)
4684 PRE_MEM_WRITE( "wait4(rusage)", ARG4
, sizeof(struct vki_rusage
) );
4689 if (ARG2
!= (Addr
)NULL
)
4690 POST_MEM_WRITE( ARG2
, sizeof(int) );
4691 if (ARG4
!= (Addr
)NULL
)
4692 POST_MEM_WRITE( ARG4
, sizeof(struct vki_rusage
) );
4698 struct vki_iovec
* vec
;
4699 *flags
|= SfMayBlock
;
4700 PRINT("sys_writev ( %" FMT_REGWORD
"u, %#" FMT_REGWORD
"x, %"
4701 FMT_REGWORD
"u )", ARG1
, ARG2
, ARG3
);
4702 PRE_REG_READ3(ssize_t
, "writev",
4703 unsigned long, fd
, const struct iovec
*, vector
,
4704 unsigned long, count
);
4705 if (!ML_(fd_allowed
)(ARG1
, "writev", tid
, False
)) {
4706 SET_STATUS_Failure( VKI_EBADF
);
4709 PRE_MEM_READ( "writev(vector)",
4710 ARG2
, ARG3
* sizeof(struct vki_iovec
) );
4712 if (ML_(safe_to_deref
)((const void*)ARG2
, ARG3
*sizeof(struct vki_iovec
*))) {
4713 vec
= (struct vki_iovec
*)(Addr
)ARG2
;
4714 for (i
= 0; i
< (Int
)ARG3
; i
++)
4715 PRE_MEM_READ( "writev(vector[...])",
4716 (Addr
)vec
[i
].iov_base
, vec
[i
].iov_len
);
4723 FUSE_COMPATIBLE_MAY_BLOCK();
4724 PRINT("sys_utimes ( %#" FMT_REGWORD
"x(%s), %#" FMT_REGWORD
"x )",
4725 ARG1
, (char*)(Addr
)ARG1
, ARG2
);
4726 PRE_REG_READ2(long, "utimes", char *, filename
, struct timeval
*, tvp
);
4727 PRE_MEM_RASCIIZ( "utimes(filename)", ARG1
);
4729 PRE_timeval_READ( "utimes(tvp[0])", (Addr
)ARG2
);
4730 PRE_timeval_READ( "utimes(tvp[1])",
4731 (Addr
)ARG2
+sizeof(struct vki_timeval
) );
4737 PRINT("sys_acct ( %#" FMT_REGWORD
"x(%s) )", ARG1
,(char*)(Addr
)ARG1
);
4738 PRE_REG_READ1(long, "acct", const char *, filename
);
4739 PRE_MEM_RASCIIZ( "acct(filename)", ARG1
);
4744 *flags
|= SfMayBlock
;
4745 PRINT("sys_pause ( )");
4746 PRE_REG_READ0(long, "pause");
4749 PRE(sys_sigaltstack
)
4751 PRINT("sigaltstack ( %#" FMT_REGWORD
"x, %#" FMT_REGWORD
"x )",ARG1
,ARG2
);
4752 PRE_REG_READ2(int, "sigaltstack",
4753 const vki_stack_t
*, ss
, vki_stack_t
*, oss
);
4755 const vki_stack_t
*ss
= (vki_stack_t
*)(Addr
)ARG1
;
4756 PRE_MEM_READ( "sigaltstack(ss)", (Addr
)&ss
->ss_sp
, sizeof(ss
->ss_sp
) );
4757 PRE_MEM_READ( "sigaltstack(ss)", (Addr
)&ss
->ss_flags
, sizeof(ss
->ss_flags
) );
4758 PRE_MEM_READ( "sigaltstack(ss)", (Addr
)&ss
->ss_size
, sizeof(ss
->ss_size
) );
4761 PRE_MEM_WRITE( "sigaltstack(oss)", ARG2
, sizeof(vki_stack_t
) );
4765 if (ARG1
&& !ML_(safe_to_deref((void*)(Addr
)ARG1
, sizeof(vki_stack_t
)))) {
4766 SET_STATUS_Failure(VKI_EFAULT
);
4769 if (ARG2
&& !ML_(safe_to_deref((void*)(Addr
)ARG2
, sizeof(vki_stack_t
)))) {
4770 SET_STATUS_Failure(VKI_EFAULT
);
4774 SET_STATUS_from_SysRes(
4775 VG_(do_sys_sigaltstack
) (tid
, (vki_stack_t
*)(Addr
)ARG1
,
4776 (vki_stack_t
*)(Addr
)ARG2
)
4779 POST(sys_sigaltstack
)
4782 if (RES
== 0 && ARG2
!= 0)
4783 POST_MEM_WRITE( ARG2
, sizeof(vki_stack_t
));
4786 PRE(sys_sethostname
)
4788 PRINT("sys_sethostname ( %#" FMT_REGWORD
"x, %ld )", ARG1
, SARG2
);
4789 PRE_REG_READ2(long, "sethostname", char *, name
, int, len
);
4790 PRE_MEM_READ( "sethostname(name)", ARG1
, ARG2
);
4796 #endif // defined(VGO_linux) || defined(VGO_darwin) || defined(VGO_solaris)
4798 /*--------------------------------------------------------------------*/
4800 /*--------------------------------------------------------------------*/