Skip to content

Commit

Permalink
Modify the value returned by the kern.usrstack sysctl to reflect the
Browse files Browse the repository at this point in the history
user stack that Valgrind synthesizes for the guest. Without this change
the sysctl will return the stack of the Valgrind host. This manifested itself
as a problem on rust compiled binaries, which were trying to add an extra
guard page but were failing since Valgrind refused guest mmaps into what it
considered to be its own memory space.
  • Loading branch information
paulfloyd committed Apr 7, 2021
1 parent ae95a5a commit 5923237
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions coregrind/m_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3843,6 +3843,13 @@ UWord voucher_mach_msg_set ( UWord arg1 )
#endif


Word VG_(get_usrstack)(void)
{
return VG_PGROUNDDN(the_iicii.clstack_end - the_iifii.clstack_max_size);
}



/*--------------------------------------------------------------------*/
/*--- end ---*/
/*--------------------------------------------------------------------*/
26 changes: 26 additions & 0 deletions coregrind/m_syswrap/syswrap-freebsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1983,6 +1983,32 @@ PRE(sys___sysctl)
}
}

/*
* Special handling cases
*
* 1. kern.userstack
* This sysctl returns the address of the bottom of the user stack
* (that is the highest user stack address, since the stack grows
* downwards). Without any special handling this would return the
* address of the host userstack. We have created a stack for the
* guest (in aspacemgr) and that is the one that we want the guest
* to see. Aspacemgr is setup in m_main.c with the adresses and sizes
* saved to file static variables in that file, so we call
* VG_(get_usrstack)() to retrieve them from there.
*/
if (SARG2 >= 2 && ML_(safe_to_deref)(name, 2*sizeof(int))) {
if (name[0] == 1 && name[1] == 33) {
// kern.userstack
Word tmp = VG_(get_usrstack)();
size_t* out = (size_t*)ARG3;
size_t* outlen = (size_t*)ARG4;
*out = tmp;
*outlen = sizeof(size_t);
SET_STATUS_Success(0);
}
}


PRE_REG_READ6(int, "__sysctl", int *, name, vki_u_int32_t, namelen, void *, oldp,
vki_size_t *, oldlenp, void *, newp, vki_size_t, newlen);

Expand Down
4 changes: 4 additions & 0 deletions coregrind/pub_core_aspacemgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ extern Bool VG_(am_search_for_new_segment)(Addr *start, SizeT *size,
UInt *prot);
#endif

/* For kern.usrstack syscall on FreeBSD */
extern Word VG_(get_usrstack)(void);


#endif // __PUB_CORE_ASPACEMGR_H

/*--------------------------------------------------------------------*/
Expand Down

0 comments on commit 5923237

Please sign in to comment.