Cleanup for clangd, mostly FreeBSD

Consistently use braces. Make some bitwise expressions use unsigned.
Use some named variables for magic numbers.
This commit is contained in:
Paul Floyd 2022-12-25 18:36:20 +01:00
parent d8cc70f6d5
commit 227fa1d53b
8 changed files with 378 additions and 274 deletions

View File

@ -1545,8 +1545,9 @@ static ULong DEBUG_SnarfLinetab(
Bool debug = di->trace_symtab;
ULong n_lines_read = 0;
if (debug)
if (debug) {
VG_(umsg)("BEGIN SnarfLineTab linetab=%p size=%d\n", linetab, size);
}
/*
* Now get the important bits.

View File

@ -86,8 +86,9 @@ static void load_client ( /*OUT*/ExeInfo* info,
/* Get hold of a file descriptor which refers to the client
executable. This is needed for attaching to GDB. */
res = VG_(open)(exe_name, VKI_O_RDONLY, VKI_S_IRUSR);
if (!sr_isError(res))
if (!sr_isError(res)) {
VG_(cl_exec_fd) = sr_Res(res);
}
/* Copy necessary bits of 'info' that were filled in */
*client_ip = info->init_ip;
@ -137,7 +138,8 @@ static HChar** setup_client_env ( HChar** origenv, const HChar* toolname)
HChar** cpp;
HChar** ret;
HChar* preload_tool_path;
Int envc, i;
Int envc;
Int i;
/* Alloc space for the vgpreload_core.so path and vgpreload_<tool>.so
paths. We might not need the space for vgpreload_<tool>.so, but it
@ -168,11 +170,15 @@ static HChar** setup_client_env ( HChar** origenv, const HChar* toolname)
VG_(debugLog)(2, "initimg", " \"%s\"\n", preload_string);
/* Count the original size of the env */
if (debug) VG_(printf)("\n\n");
if (debug) {
VG_(printf)("\n\n");
}
envc = 0;
for (cpp = origenv; cpp && *cpp; cpp++) {
envc++;
if (debug) VG_(printf)("XXXXXXXXX: BEFORE %s\n", *cpp);
if (debug) {
VG_(printf)("XXXXXXXXX: BEFORE %s\n", *cpp);
}
}
/* Allocate a new space */
@ -181,7 +187,9 @@ static HChar** setup_client_env ( HChar** origenv, const HChar* toolname)
/* copy it over */
for (cpp = ret; *origenv; ) {
if (debug) VG_(printf)("XXXXXXXXX: COPY %s\n", *origenv);
if (debug) {
VG_(printf)("XXXXXXXXX: COPY %s\n", *origenv);
}
*cpp++ = *origenv++;
}
*cpp = NULL;
@ -202,7 +210,9 @@ static HChar** setup_client_env ( HChar** origenv, const HChar* toolname)
ld_preload_done = True;
}
if (debug) VG_(printf)("XXXXXXXXX: MASH %s\n", *cpp);
if (debug) {
VG_(printf)("XXXXXXXXX: MASH %s\n", *cpp);
}
}
/* Add the missing bits */
@ -213,7 +223,9 @@ static HChar** setup_client_env ( HChar** origenv, const HChar* toolname)
VG_(snprintf)(cp, len, "%s%s", ld_preload, preload_string);
ret[envc++] = cp;
if (debug) VG_(printf)("XXXXXXXXX: ADD %s\n", cp);
if (debug) {
VG_(printf)("XXXXXXXXX: ADD %s\n", cp);
}
}
#if defined(VGP_x86_freebsd)
@ -249,13 +261,16 @@ static HChar** setup_client_env ( HChar** origenv, const HChar* toolname)
/* ret[0 .. envc-1] is live now. */
/* Find and remove a binding for VALGRIND_LAUNCHER. */
for (i = 0; i < envc; i++)
if (0 == VG_(memcmp)(ret[i], v_launcher, v_launcher_len))
for (i = 0; i < envc; i++) {
if (0 == VG_(memcmp)(ret[i], v_launcher, v_launcher_len)) {
break;
}
}
if (i < envc) {
for (; i < envc-1; i++)
for (; i < envc-1; i++) {
ret[i] = ret[i+1];
}
envc--;
}
@ -263,7 +278,9 @@ static HChar** setup_client_env ( HChar** origenv, const HChar* toolname)
ret[envc] = NULL;
for (i = 0; i < envc; i++) {
if (debug) VG_(printf)("XXXXXXXXX: FINAL %s\n", ret[i]);
if (debug) {
VG_(printf)("XXXXXXXXX: FINAL %s\n", ret[i]);
}
}
return ret;
@ -280,12 +297,14 @@ static HChar *copy_str(HChar **tab, const HChar *str)
HChar *cp = *tab;
HChar *orig = cp;
while(*str)
while(*str) {
*cp++ = *str++;
}
*cp++ = '\0';
if (0)
if (0) {
VG_(printf)("copied %p \"%s\" len %lld\n", (void*)orig, orig, (Long)(cp-orig));
}
*tab = cp;
@ -348,12 +367,14 @@ struct auxv *find_auxv(UWord* sp)
{
sp++; // skip argc (Nb: is word-sized, not int-sized!)
while (*sp != 0) // skip argv
while (*sp != 0) { // skip argv
sp++;
}
sp++;
while (*sp != 0) // skip env
while (*sp != 0) { // skip env
sp++;
}
sp++;
return (struct auxv *)sp;
@ -411,8 +432,9 @@ Addr setup_client_stack( void* init_sp,
}
/* now scan the args we're given... */
if (have_exename)
if (have_exename) {
stringsize += VG_(strlen)( VG_(args_the_exename) ) + 1;
}
for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) {
argc++;
@ -480,7 +502,9 @@ Addr setup_client_stack( void* init_sp,
auxsize + /* auxv */
VG_ROUNDUP(stringsize, sizeof(Word)); /* strings (aligned) */
if (0) VG_(printf)("stacksize = %u\n", stacksize);
if (0) {
VG_(printf)("stacksize = %u\n", stacksize);
}
/* client_SP is the client's stack pointer */
client_SP = clstack_end - stacksize;
@ -495,12 +519,13 @@ Addr setup_client_stack( void* init_sp,
/* The max stack size */
clstack_max_size = VG_PGROUNDUP(clstack_max_size);
if (0)
if (0) {
VG_(printf)("stringsize=%u auxsize=%u stacksize=%u maxsize=0x%lx\n"
"clstack_start %p\n"
"clstack_end %p\n",
stringsize, auxsize, stacksize, clstack_max_size,
(void*)clstack_start, (void*)clstack_end);
}
/* ==================== allocate space ==================== */
@ -540,9 +565,10 @@ Addr setup_client_stack( void* init_sp,
inner_HACK = 1024*1024; // create 1M non-fault-extending stack
# endif
if (0)
if (0) {
VG_(printf)("%#lx 0x%lx %#lx 0x%lx\n",
resvn_start, resvn_size, anon_start, anon_size);
}
/* Create a shrinkable reservation followed by an anonymous
segment. Together these constitute a growdown stack. */
@ -589,13 +615,16 @@ Addr setup_client_stack( void* init_sp,
/* --- client argv --- */
client_argv = (Word)ptr;
if (info->interp_name)
if (info->interp_name) {
*ptr++ = (Addr)copy_str(&strtab, info->interp_name);
if (info->interp_args)
}
if (info->interp_args) {
*ptr++ = (Addr)copy_str(&strtab, info->interp_args);
}
if (have_exename)
if (have_exename) {
*ptr++ = (Addr)copy_str(&strtab, VG_(args_the_exename));
}
for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) {
*ptr++ = (Addr)copy_str(
@ -607,8 +636,9 @@ Addr setup_client_stack( void* init_sp,
/* --- envp --- */
VG_(client_envp) = (HChar **)ptr;
for (cpp = orig_envp; cpp && *cpp; ptr++, cpp++)
for (cpp = orig_envp; cpp && *cpp; ptr++, cpp++) {
*ptr = (Addr)copy_str(&strtab, *cpp);
}
*ptr++ = 0;
/* --- auxv --- */
@ -658,16 +688,18 @@ Addr setup_client_stack( void* init_sp,
auxv->u.a_ptr = copy_str(&strtab, orig_auxv->u.a_ptr);
break;
case VKI_AT_CANARY:
if (canarylen >= 1)
if (canarylen >= 1) {
auxv->u.a_ptr = copy_bytes(&strtab, orig_auxv->u.a_ptr, canarylen);
else
} else {
auxv->a_type = VKI_AT_IGNORE;
}
break;
case VKI_AT_PAGESIZES:
if (pagesizeslen >= 1)
if (pagesizeslen >= 1) {
auxv->u.a_ptr = copy_bytes(&strtab, orig_auxv->u.a_ptr, pagesizeslen);
else
} else {
auxv->a_type = VKI_AT_IGNORE;
}
break;
#if 0
/*
@ -717,17 +749,19 @@ Addr setup_client_stack( void* init_sp,
#endif
case VKI_AT_PHDR:
if (info->phdr == 0)
if (info->phdr == 0) {
auxv->a_type = VKI_AT_IGNORE;
else
} else {
auxv->u.a_val = info->phdr;
}
break;
case VKI_AT_PHNUM:
if (info->phdr == 0)
if (info->phdr == 0) {
auxv->a_type = VKI_AT_IGNORE;
else
} else {
auxv->u.a_val = info->phnum;
}
break;
case VKI_AT_BASE:
@ -754,7 +788,9 @@ Addr setup_client_stack( void* init_sp,
/* client_SP is pointing at client's argc/argv */
if (0) VG_(printf)("startup SP = %#lx\n", client_SP);
if (0) {
VG_(printf)("startup SP = %#lx\n", client_SP);
}
return client_SP;
}
@ -803,8 +839,9 @@ static void setup_client_dataseg ( SizeT max_size )
SmLower,
anon_size
);
if (ok)
if (ok) {
VG_(brk_base) = VG_(brk_limit) = anon_start;
}
}
/* that too might have failed, but if it has, we're hosed: there
is no Plan C. */
@ -848,8 +885,9 @@ IIFinaliseImageInfo VG_(ii_create_image)( IICreateImageInfo iicii,
//--------------------------------------------------------------
VG_(debugLog)(1, "initimg", "Loading client\n");
if (VG_(args_the_exename) == NULL)
if (VG_(args_the_exename) == NULL) {
VG_(err_missing_prog)();
}
VG_(memset)(&info, 0, sizeof(info));
@ -883,10 +921,18 @@ IIFinaliseImageInfo VG_(ii_create_image)( IICreateImageInfo iicii,
SizeT m1 = 1024 * 1024;
SizeT m16 = 16 * m1;
SizeT szB = (SizeT)VG_(client_rlimit_stack).rlim_cur;
if (szB < m1) szB = m1;
if (szB > m16) szB = m16;
if (VG_(clo_main_stacksize) > 0) szB = VG_(clo_main_stacksize);
if (szB < m1) szB = m1;
if (szB < m1) {
szB = m1;
}
if (szB > m16) {
szB = m16;
}
if (VG_(clo_main_stacksize) > 0) {
szB = VG_(clo_main_stacksize);
}
if (szB < m1) {
szB = m1;
}
szB = VG_PGROUNDUP(szB);
VG_(debugLog)(1, "initimg",
"Setup client stack: size will be %lu\n", szB);
@ -923,8 +969,12 @@ IIFinaliseImageInfo VG_(ii_create_image)( IICreateImageInfo iicii,
SizeT m8 = 8 * m1;
SizeT dseg_max_size = (SizeT)VG_(client_rlimit_data).rlim_cur;
VG_(debugLog)(1, "initimg", "Setup client data (brk) segment\n");
if (dseg_max_size < m1) dseg_max_size = m1;
if (dseg_max_size > m8) dseg_max_size = m8;
if (dseg_max_size < m1) {
dseg_max_size = m1;
}
if (dseg_max_size > m8) {
dseg_max_size = m8;
}
dseg_max_size = VG_PGROUNDUP(dseg_max_size);
setup_client_dataseg( dseg_max_size );
@ -988,7 +1038,7 @@ void VG_(ii_finalise_image)( IIFinaliseImageInfo iifii )
VG_(memset)(&arch->vex_shadow2, 0, sizeof(VexGuestAMD64State));
/* Put essential stuff into the new state. */
arch->vex.guest_RSP = ((iifii.initial_client_SP - 8) & ~0xFul) + 8;
arch->vex.guest_RSP = ((iifii.initial_client_SP - 8) & ~0xFUL) + 8;
arch->vex.guest_RDI = iifii.initial_client_SP;
arch->vex.guest_RIP = iifii.initial_client_IP;

View File

@ -49,6 +49,8 @@
on amd64-freebsd.
*/
const UInt MAGIC_PI = 0x31415927U;
const UInt MAGIC_E = 0x27182818U;
/*------------------------------------------------------------*/
/*--- Signal frame layouts ---*/
@ -153,12 +155,12 @@ void synth_ucontext(ThreadId tid, const vki_siginfo_t *si,
XXX
*/
SC2(rip,RIP);
sc->addr = (UWord)si->si_addr;
sc->err = err;
sc->addr = (vki_register_t)si->si_addr;
sc->err = (vki_register_t)err;
sc->fpformat = VKI_FPFMT_NODEV;
sc->ownedfp = VKI_FPOWNED_NONE;
sc->len = sizeof(*sc);
sc->rflags = LibVEX_GuestAMD64_get_rflags(&tst->arch.vex);
sc->rflags = (vki_register_t)LibVEX_GuestAMD64_get_rflags(&tst->arch.vex);
sc->trapno = trapno;
# undef SC2
}
@ -219,7 +221,7 @@ static void build_vg_sigframe(struct vg_sigframe *frame,
Int sigNo)
{
frame->sigNo_private = sigNo;
frame->magicPI = 0x31415927;
frame->magicPI = MAGIC_PI;
frame->vex_shadow1 = tst->arch.vex_shadow1;
frame->vex_shadow2 = tst->arch.vex_shadow2;
/* HACK ALERT */
@ -227,7 +229,7 @@ static void build_vg_sigframe(struct vg_sigframe *frame,
/* end HACK ALERT */
frame->mask = tst->sig_mask;
frame->handlerflags = flags;
frame->magicE = 0x27182818;
frame->magicE = MAGIC_E;
}
static Addr build_sigframe(ThreadState *tst,
@ -268,8 +270,9 @@ static Addr build_sigframe(ThreadState *tst,
VG_(memcpy)(&frame->sigInfo, siginfo, sizeof(vki_siginfo_t));
if (sigNo == VKI_SIGILL && siginfo->si_code > 0)
if (sigNo == VKI_SIGILL && siginfo->si_code > 0) {
frame->sigInfo.si_addr = (void*)tst->arch.vex.guest_RIP;
}
synth_ucontext(tst->tid, siginfo, trapno, err, mask,
&frame->uContext, &frame->fpstate);
@ -336,8 +339,8 @@ static
Bool restore_vg_sigframe ( ThreadState *tst,
struct vg_sigframe *frame, Int *sigNo )
{
if (frame->magicPI != 0x31415927 ||
frame->magicE != 0x27182818) {
if (frame->magicPI != MAGIC_PI ||
frame->magicE != MAGIC_E) {
VG_(message)(Vg_UserMsg, "Thread %u return signal frame "
"corrupted. Killing process.\n",
tst->tid);
@ -419,11 +422,12 @@ void VG_(sigframe_destroy)( ThreadId tid )
VG_TRACK( die_mem_stack_signal, rsp - VG_STACK_REDZONE_SZB,
size + VG_STACK_REDZONE_SZB );
if (VG_(clo_trace_signals))
if (VG_(clo_trace_signals)) {
VG_(message)(
Vg_DebugMsg,
"VG_(signal_return) (thread %u): valid magic; RIP=%#llx\n",
tid, tst->arch.vex.guest_RIP);
}
/* tell the tools */
VG_TRACK( post_deliver_signal, tid, sigNo );

View File

@ -180,12 +180,12 @@ typedef
SyscallArgLayout;
/* Flags describing syscall wrappers */
#define SfMayBlock (1 << 1) /* may block */
#define SfPostOnFail (1 << 2) /* call POST() function on failure */
#define SfPollAfter (1 << 3) /* poll for signals on completion */
#define SfYieldAfter (1 << 4) /* yield on completion */
#define SfNoWriteResult (1 << 5) /* don't write result to guest state */
#define SfKernelRestart (1 << 6) /* needs a manual restart */
#define SfMayBlock (1U << 1U) /* may block */
#define SfPostOnFail (1U << 2U) /* call POST() function on failure */
#define SfPollAfter (1U << 3U) /* poll for signals on completion */
#define SfYieldAfter (1U << 4U) /* yield on completion */
#define SfNoWriteResult (1U << 5U) /* don't write result to guest state */
#define SfKernelRestart (1U << 6U) /* needs a manual restart */
/* ---------------------------------------------------------------------

View File

@ -761,10 +761,11 @@ PRE(sys_pread)
unsigned int, fd, char *, buf, vki_size_t, count,
unsigned long, off);
if (!ML_(fd_allowed)(ARG1, "read", tid, False))
if (!ML_(fd_allowed)(ARG1, "read", tid, False)) {
SET_STATUS_Failure( VKI_EBADF );
else
} else {
PRE_MEM_WRITE( "pread(buf)", ARG2, ARG3 );
}
}
POST(sys_pread)
@ -789,10 +790,11 @@ PRE(sys_pwrite)
if (!ok && ARG1 == 2/*stderr*/
&& SimHintiS(SimHint_enable_outer, VG_(clo_sim_hints)))
ok = True;
if (!ok)
if (!ok) {
SET_STATUS_Failure( VKI_EBADF );
else
} else {
PRE_MEM_READ( "pwrite(buf)", ARG2, ARG3 );
}
}
// SYS_mmap 477

File diff suppressed because it is too large Load Diff

View File

@ -27,7 +27,7 @@
#define VKI_AMD64_FREEBSD_H
/* PAGE_SHIFT determines the page size. */
#define VKI_PAGE_SHIFT 12
#define VKI_PAGE_SHIFT 12UL
#define VKI_PAGE_SIZE (1UL << VKI_PAGE_SHIFT)
#define VKI_MAX_PAGE_SHIFT VKI_PAGE_SHIFT
#define VKI_MAX_PAGE_SIZE VKI_PAGE_SIZE

View File

@ -959,12 +959,12 @@ struct vki_termios {
* We actually have a 16 bit "base" ioctl, which may or may not be decoded
* into number/group
*/
#define _VKI_IOC_BASEBITS 16
#define _VKI_IOC_NRBITS 8 /* "num" on freebsd */
#define _VKI_IOC_TYPEBITS 8 /* "group" on freebsd */
#define _VKI_IOC_BASEBITS 16U
#define _VKI_IOC_NRBITS 8U /* "num" on freebsd */
#define _VKI_IOC_TYPEBITS 8U /* "group" on freebsd */
#define _VKI_IOC_SIZEBITS 13
#define _VKI_IOC_DIRBITS 3
#define _VKI_IOC_SIZEBITS 13U
#define _VKI_IOC_DIRBITS 3U
#define _VKI_IOC_BASEMASK ((1ul << _VKI_IOC_BASEBITS)-1)
#define _VKI_IOC_NRMASK ((1ul << _VKI_IOC_NRBITS)-1)
@ -972,8 +972,8 @@ struct vki_termios {
#define _VKI_IOC_SIZEMASK ((1ul << _VKI_IOC_SIZEBITS)-1)
#define _VKI_IOC_DIRMASK ((1ul << _VKI_IOC_DIRBITS)-1)
#define _VKI_IOC_BASESHIFT 0
#define _VKI_IOC_NRSHIFT 0
#define _VKI_IOC_BASESHIFT 0U
#define _VKI_IOC_NRSHIFT 0U
#define _VKI_IOC_TYPESHIFT (_VKI_IOC_NRSHIFT+_VKI_IOC_NRBITS)
#define _VKI_IOC_SIZESHIFT (_VKI_IOC_TYPESHIFT+_VKI_IOC_TYPEBITS)
#define _VKI_IOC_DIRSHIFT (_VKI_IOC_SIZESHIFT+_VKI_IOC_SIZEBITS)
@ -1013,7 +1013,7 @@ extern unsigned int __vki_invalid_size_argument_for_IOC;
// From sys/random.h
//----------------------------------------------------------------------
#define VKI_GRND_NONBLOCK 0x1
#define VKI_GRND_NONBLOCK 0x1U
//----------------------------------------------------------------------
// From sys/termios.h