/* Max length of a text fragment used to construct error messages. */
#define VG_ERRTXT_LEN 4096
-// 'a' -- the alignment -- must be a power of 2
-#define ROUNDDN(p, a) ((Addr)(p) & ~((Addr)(a)-1))
-#define ROUNDUP(p, a) ROUNDDN((p)+(a)-1, (a))
-#define PGROUNDDN(p) ROUNDDN(p, VKI_PAGE_SIZE)
-#define PGROUNDUP(p) ROUNDUP(p, VKI_PAGE_SIZE)
-
/* ---------------------------------------------------------------------
Environment variables
------------------------------------------------------------------ */
if (len == 0)
return;
- len = PGROUNDUP(len);
+ len = VG_PGROUNDUP(len);
if (debug)
VG_(printf)("unmap_range(%p, %lu)\n", addr, len);
/* Everything must be page-aligned */
vg_assert(VG_IS_PAGE_ALIGNED(addr));
- len = PGROUNDUP(len);
+ len = VG_PGROUNDUP(len);
/* Nuke/truncate any existing segment(s) covering [addr,addr+len) */
VG_(unmap_range)(addr, len);
/* Everything must be page-aligned */
vg_assert(VG_IS_PAGE_ALIGNED(a));
- len = PGROUNDUP(len);
+ len = VG_PGROUNDUP(len);
split_segment(a);
split_segment(a+len);
/* Everything must be page-aligned */
vg_assert((addr & (VKI_PAGE_SIZE-1)) == 0);
- len = PGROUNDUP(len);
+ len = VG_PGROUNDUP(len);
len += VKI_PAGE_SIZE * 2; /* leave redzone gaps before and after mapping */
if (size == 0)
return True; /* isn't this a bit of a strange case? */
- p = PGROUNDDN(p);
- size = PGROUNDUP(size);
+ p = VG_PGROUNDDN(p);
+ size = VG_PGROUNDUP(size);
vg_assert(VG_IS_PAGE_ALIGNED(p));
vg_assert(VG_IS_PAGE_ALIGNED(size));
Addr VG_(get_memory_from_mmap_for_client)
(Addr addr, SizeT len, UInt prot, UInt sf_flags)
{
- len = PGROUNDUP(len);
+ len = VG_PGROUNDUP(len);
tl_assert(!(sf_flags & SF_FIXED));
tl_assert(0 == addr);
vg_assert(VG_(needs).shadow_memory);
vg_assert(VG_(tdict).track_init_shadow_page);
- sz = PGROUNDUP(p+sz) - PGROUNDDN(p);
- p = PGROUNDDN(p);
+ sz = VG_PGROUNDUP(p+sz) - VG_PGROUNDDN(p);
+ p = VG_PGROUNDDN(p);
VG_(mprotect)((void *)p, sz, VKI_PROT_READ|VKI_PROT_WRITE);
if (call_init)
while(sz) {
/* ask the tool to initialize each page */
- VG_TRACK( init_shadow_page, PGROUNDDN(p) );
+ VG_TRACK( init_shadow_page, VG_PGROUNDDN(p) );
p += VKI_PAGE_SIZE;
sz -= VKI_PAGE_SIZE;
vg_assert(VG_(needs).shadow_memory);
vg_assert(!VG_(tdict).track_init_shadow_page);
- size = PGROUNDUP(size);
+ size = VG_PGROUNDUP(size);
if (shadow_alloc == 0)
shadow_alloc = VG_(shadow_base);
/* Did we find a debuglink section? */
if (debuglink != NULL) {
- UInt crc_offset = ROUNDUP(VG_(strlen)(debuglink)+1, 4);
+ UInt crc_offset = VG_ROUNDUP(VG_(strlen)(debuglink)+1, 4);
UInt crc;
vg_assert(crc_offset + sizeof(UInt) <= debuglink_sz);
break;
case AT_PHDR:
- VG_(valgrind_base) = PGROUNDDN(auxv->u.a_val);
+ VG_(valgrind_base) = VG_PGROUNDDN(auxv->u.a_val);
break;
}
// VG_(valgrind_base) should have been set by scan_auxv, but if not,
// this is a workable approximation
if (VG_(valgrind_base) == 0) {
- VG_(valgrind_base) = PGROUNDDN(&_start);
+ VG_(valgrind_base) = VG_PGROUNDDN(&_start);
}
- VG_(valgrind_last) = ROUNDUP(argc_addr, 0x10000) - 1; // stack
+ VG_(valgrind_last) = VG_ROUNDUP(argc_addr, 0x10000) - 1; // stack
// This gives the client the largest possible address space while
// taking into account the tool's shadow needs.
- client_size = ROUNDDN((VG_(valgrind_base)-REDZONE_SIZE) / (1.+ratio),
+ client_size = VG_ROUNDDN((VG_(valgrind_base)-REDZONE_SIZE) / (1.+ratio),
CLIENT_SIZE_MULTIPLE);
VG_(client_base) = 0;
VG_(client_end) = VG_(client_base) + client_size;
/* where !FIXED mmap goes */
VG_(client_mapbase) = VG_(client_base) +
- PGROUNDDN((Addr)(client_size * CLIENT_HEAP_PROPORTION));
+ VG_PGROUNDDN((Addr)(client_size * CLIENT_HEAP_PROPORTION));
VG_(shadow_base) = VG_(client_end) + REDZONE_SIZE;
VG_(shadow_end) = VG_(valgrind_base);
sizeof(char **)*envc + /* envp */
sizeof(char **) + /* terminal NULL */
auxsize + /* auxv */
- ROUNDUP(stringsize, sizeof(Word)) +/* strings (aligned) */
+ VG_ROUNDUP(stringsize, sizeof(Word)) +/* strings (aligned) */
VKI_PAGE_SIZE; /* page for trampoline code */
if (0) VG_(printf)("stacksize = %d\n", stacksize);
/* cl_esp is the client's stack pointer */
cl_esp = VG_(clstk_end) - stacksize;
- cl_esp = ROUNDDN(cl_esp, 16); /* make stack 16 byte aligned */
+ cl_esp = VG_ROUNDDN(cl_esp, 16); /* make stack 16 byte aligned */
/* base of the string table (aligned) */
stringbase = strtab = (char *)(VG_(client_trampoline_code)
- - ROUNDUP(stringsize, sizeof(int)));
+ - VG_ROUNDUP(stringsize, sizeof(int)));
- VG_(clstk_base) = PGROUNDDN(cl_esp);
+ VG_(clstk_base) = VG_PGROUNDDN(cl_esp);
if (0)
printf("stringsize=%d auxsize=%d stacksize=%d\n"
/* ==================== allocate space ==================== */
/* allocate a stack - mmap enough space for the stack */
- res = mmap((void *)PGROUNDDN(cl_esp), VG_(clstk_end) - PGROUNDDN(cl_esp),
+ res = mmap((void *)VG_PGROUNDDN(cl_esp),
+ VG_(clstk_end) - VG_PGROUNDDN(cl_esp),
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0);
vg_assert((void*)-1 != res);
Int sigNo = siginfo->si_signo;
rsp -= sizeof(*frame);
- rsp = ROUNDDN(rsp, 16);
+ rsp = VG_ROUNDDN(rsp, 16);
frame = (struct rt_sigframe *)rsp;
if (!extend(tst, rsp, sizeof(*frame)))
vg_assert((flags & VKI_SA_SIGINFO) == 0);
esp -= sizeof(*frame);
- esp = ROUNDDN(esp, 16);
+ esp = VG_ROUNDDN(esp, 16);
frame = (struct sigframe *)esp;
if (!extend(tst, esp, sizeof(*frame)))
vg_assert((flags & VKI_SA_SIGINFO) != 0);
esp -= sizeof(*frame);
- esp = ROUNDDN(esp, 16);
+ esp = VG_ROUNDDN(esp, 16);
frame = (struct rt_sigframe *)esp;
if (!extend(tst, esp, sizeof(*frame)))
static UInt note_size(const struct note *n)
{
- return sizeof(Elf32_Nhdr) + ROUNDUP(VG_(strlen)(n->name)+1, 4) + ROUNDUP(n->note.n_descsz, 4);
+ return sizeof(Elf32_Nhdr) + VG_ROUNDUP(VG_(strlen)(n->name)+1, 4) + VG_ROUNDUP(n->note.n_descsz, 4);
}
static void add_note(struct note **list, const Char *name, UInt type, const void *data, UInt datasz)
{
Int namelen = VG_(strlen)(name)+1;
Int notelen = sizeof(struct note) +
- ROUNDUP(namelen, 4) +
- ROUNDUP(datasz, 4);
+ VG_ROUNDUP(namelen, 4) +
+ VG_ROUNDUP(datasz, 4);
struct note *n = VG_(arena_malloc)(VG_AR_CORE, notelen);
VG_(memset)(n, 0, notelen);
n->note.n_descsz = datasz;
VG_(memcpy)(n->name, name, namelen);
- VG_(memcpy)(n->name+ROUNDUP(namelen,4), data, datasz);
+ VG_(memcpy)(n->name+VG_ROUNDUP(namelen,4), data, datasz);
}
static void write_note(Int fd, const struct note *n)
off += notesz;
- off = PGROUNDUP(off);
+ off = VG_PGROUNDUP(off);
for(seg = VG_(first_segment)(), idx = 1;
seg != NULL;
vg_assert(seg->addr > addr);
/* Create the mapping */
- base = PGROUNDDN(addr);
+ base = VG_PGROUNDDN(addr);
newsize = seg->addr - base;
if (seg->len + newsize >= maxsize)
nothing mapped there (as opposed to a permissions fault),
then extend the stack segment.
*/
- Addr base = PGROUNDDN(esp - VGA_STACK_REDZONE_SZB);
+ Addr base = VG_PGROUNDDN(esp - VGA_STACK_REDZONE_SZB);
if (VG_(extend_stack)(base, VG_(threads)[tid].client_stack_szB)) {
if (VG_(clo_trace_signals))
VG_(message)(Vg_DebugMsg,
- " -> extended stack base to %p", PGROUNDDN(fault));
+ " -> extended stack base to %p", VG_PGROUNDDN(fault));
return; // extension succeeded, restart instruction
} else
VG_(message)(Vg_UserMsg, "Stack overflow in thread %d: can't grow stack to %p",
static Int recursion = 0;
if (recursion++ == 0) {
- VG_(init_shadow_range)(PGROUNDDN(fault), VKI_PAGE_SIZE, True);
+ VG_(init_shadow_range)(VG_PGROUNDDN(fault), VKI_PAGE_SIZE, True);
recursion--;
return;
} else {
only go down to the start of the mmaped segment. */
seg = VG_(find_segment)((Addr)rsp);
if (seg) {
- ctst->client_stack_highest_word = (Addr)PGROUNDUP(rsp);
+ ctst->client_stack_highest_word = (Addr)VG_PGROUNDUP(rsp);
ctst->client_stack_szB = ctst->client_stack_highest_word - seg->addr;
if (debug)
VG_(printf)("tid %d: guessed client stack range %p-%p\n",
- ctid, seg->addr, PGROUNDUP(rsp));
+ ctid, seg->addr, VG_PGROUNDUP(rsp));
} else {
VG_(message)(Vg_UserMsg, "!? New thread %d starts with RSP(%p) unmapped\n",
ctid, rsp);
unsigned, nr_events, vki_aio_context_t *, ctxp);
PRE_MEM_WRITE( "io_setup(ctxp)", ARG2, sizeof(vki_aio_context_t) );
- size = PGROUNDUP(sizeof(struct vki_aio_ring) +
- ARG1*sizeof(struct vki_io_event));
+ size = VG_PGROUNDUP(sizeof(struct vki_aio_ring) +
+ ARG1*sizeof(struct vki_io_event));
addr = VG_(find_map_space)(0, size, True);
if (addr == 0) {
// If we are going to seg fault (due to a bogus ARG1) do it as late as
// possible...
r = *(struct vki_aio_ring **)ARG1;
- size = PGROUNDUP(sizeof(struct vki_aio_ring) +
- r->nr*sizeof(struct vki_io_event));
+ size = VG_PGROUNDUP(sizeof(struct vki_aio_ring) +
+ r->nr*sizeof(struct vki_io_event));
SET_RESULT( VG_(do_syscall1)(SYSNO, ARG1) );
only go down to the start of the mmaped segment. */
seg = VG_(find_segment)((Addr)esp);
if (seg) {
- ctst->client_stack_highest_word = (Addr)PGROUNDUP(esp);
+ ctst->client_stack_highest_word = (Addr)VG_PGROUNDUP(esp);
ctst->client_stack_szB = ctst->client_stack_highest_word - seg->addr;
if (debug)
VG_(printf)("tid %d: guessed client stack range %p-%p\n",
- ctid, seg->addr, PGROUNDUP(esp));
+ ctid, seg->addr, VG_PGROUNDUP(esp));
} else {
VG_(message)(Vg_UserMsg, "!? New thread %d starts with ESP(%p) unmapped\n",
ctid, esp);
{
Addr ra;
- ra = PGROUNDDN(*a);
- *len = PGROUNDUP(*a + *len) - ra;
+ ra = VG_PGROUNDDN(*a);
+ *len = VG_PGROUNDUP(*a + *len) - ra;
*a = ra;
}
Addr ret;
Segment *seg, *next;
- old_size = PGROUNDUP(old_size);
- new_size = PGROUNDUP(new_size);
+ old_size = VG_PGROUNDUP(old_size);
+ new_size = VG_PGROUNDUP(new_size);
- if (PGROUNDDN(old_addr) != old_addr)
+ if (VG_PGROUNDDN(old_addr) != old_addr)
return -VKI_EINVAL;
if (!VG_(valid_client_addr)(old_addr, old_size, tid, "mremap(old_addr)"))
flags &= ~(VKI_MREMAP_FIXED|VKI_MREMAP_MAYMOVE);
if (flags & VKI_MREMAP_FIXED) {
- if (PGROUNDDN(new_addr) != new_addr)
+ if (VG_PGROUNDDN(new_addr) != new_addr)
return -VKI_EINVAL;
if (!VG_(valid_client_addr)(new_addr, new_size, tid, "mremap(new_addr)"))
if (seg != NULL && newbrk > seg->addr)
return VG_(brk_limit);
- current = PGROUNDUP(VG_(brk_limit));
- newaddr = PGROUNDUP(newbrk);
+ current = VG_PGROUNDUP(VG_(brk_limit));
+ newaddr = VG_PGROUNDUP(newbrk);
if (newaddr != current) {
/* new brk in a new page - fix the mappings */
extern char _end;
int *esp;
char buf[strlen(valgrind_lib) + sizeof(stage2) + 16];
- info.exe_end = PGROUNDDN(init_sp);
+ info.exe_end = VG_PGROUNDDN(init_sp);
#ifdef HAVE_PIE
- info.exe_base = ROUNDDN(info.exe_end - 0x02000000, 0x10000000);
- assert(info.exe_base >= PGROUNDUP(&_end));
+ info.exe_base = VG_ROUNDDN(info.exe_end - 0x02000000, 0x10000000);
+ assert(info.exe_base >= VG_PGROUNDUP(&_end));
info.map_base = info.exe_base + 0x01000000;
#else
// If this system doesn't have PIE (position-independent executables),
// we have to choose a hardwired location for stage2.
- info.exe_base = PGROUNDUP(&_end);
+ info.exe_base = VG_PGROUNDUP(&_end);
info.map_base = KICKSTART_BASE + 0x01000000;
#endif
// use ph->p_align -- part of stage2's memory gets trashed somehow.)
//
// The condition handles the case of a zero-length segment.
- if (PGROUNDUP(bss)-PGROUNDDN(addr) > 0) {
- res = mmap((char *)PGROUNDDN(addr), PGROUNDUP(bss)-PGROUNDDN(addr),
- prot, MAP_FIXED|MAP_PRIVATE, e->fd, PGROUNDDN(off));
- check_mmap(res, (char*)PGROUNDDN(addr),
- PGROUNDUP(bss)-PGROUNDDN(addr));
+ if (VG_PGROUNDUP(bss)-VG_PGROUNDDN(addr) > 0) {
+ res = mmap((char *)VG_PGROUNDDN(addr),
+ VG_PGROUNDUP(bss)-VG_PGROUNDDN(addr),
+ prot, MAP_FIXED|MAP_PRIVATE, e->fd, VG_PGROUNDDN(off));
+ check_mmap(res, (char*)VG_PGROUNDDN(addr),
+ VG_PGROUNDUP(bss)-VG_PGROUNDDN(addr));
}
// if memsz > filesz, fill the remainder with zeroed pages
if (memsz > filesz) {
UInt bytes;
- bytes = PGROUNDUP(brkaddr)-PGROUNDUP(bss);
+ bytes = VG_PGROUNDUP(brkaddr)-VG_PGROUNDUP(bss);
if (bytes > 0) {
- res = mmap((char *)PGROUNDUP(bss), bytes,
+ res = mmap((char *)VG_PGROUNDUP(bss), bytes,
prot, MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
- check_mmap(res, (char*)PGROUNDUP(bss), bytes);
+ check_mmap(res, (char*)VG_PGROUNDUP(bss), bytes);
}
bytes = bss & (VKI_PAGE_SIZE - 1);
int flags = MAP_PRIVATE|MAP_ANONYMOUS;
if (info->map_base != 0) {
- base = (char *)ROUNDUP(info->map_base, interp_align);
+ base = (char *)VG_ROUNDUP(info->map_base, interp_align);
flags |= MAP_FIXED;
}
/* Rotate an unsigned quantity left */
#define ROTL(x, n) (((x) << (n)) | ((x) >> ((sizeof(x)*8)-(n))))
-/* round a up to the next multiple of N. N must be a power of 2 */
-#define ROUNDUP(a, N) ((a + N - 1) & ~(N-1))
-
-/* Round a down to the next multiple of N. N must be a power of 2 */
-#define ROUNDDN(a, N) ((a) & ~(N-1))
-
/*------------------------------------------------------------*/
/*--- Command line options ---*/
/*------------------------------------------------------------*/
* len/4+1 words. This works out which it is by aligning the block and
* seeing if the end byte is in the same word as it is for the unaligned
* block; if not, it's the awkward case. */
- end = ROUNDUP(a + len, 4);
- a = ROUNDDN(a, 4);
+ end = VG_ROUNDUP(a + len, 4);
+ a = VG_ROUNDDN(a, 4);
/* Do it ... */
switch (status) {
{
Addr end;
- end = ROUNDUP(a+size, 4);
- a = ROUNDDN(a, 4);
+ end = VG_ROUNDUP(a+size, 4);
+ a = VG_ROUNDDN(a, 4);
for ( ; a < end; a += 4)
hg_mem_read_word(a, tid);
{
Addr end;
- end = ROUNDUP(a+size, 4);
- a = ROUNDDN(a, 4);
+ end = VG_ROUNDUP(a+size, 4);
+ a = VG_ROUNDDN(a, 4);
for ( ; a < end; a += 4)
hg_mem_write_word(a, tid);
/*=== Useful macros ===*/
/*====================================================================*/
+// 'a' -- the alignment -- must be a power of 2
+#define VG_ROUNDDN(p, a) ((Addr)(p) & ~((Addr)(a)-1))
+#define VG_ROUNDUP(p, a) VG_ROUNDDN((p)+(a)-1, (a))
+#define VG_PGROUNDDN(p) VG_ROUNDDN(p, VKI_PAGE_SIZE)
+#define VG_PGROUNDUP(p) VG_ROUNDUP(p, VKI_PAGE_SIZE)
+
/* No, really. I _am_ that strange. */
#define OINK(nnn) VG_(message)(Vg_DebugMsg, "OINK %d",nnn)
#define VG_DEBUG_LEAKCHECK 0
#define VG_DEBUG_CLIQUE 0
-#define ROUNDDN(p, a) ((Addr)(p) & ~((a)-1))
-#define ROUNDUP(p, a) ROUNDDN((p)+(a)-1, (a))
-#define PGROUNDDN(p) ROUNDDN(p, VKI_PAGE_SIZE)
-#define PGROUNDUP(p) ROUNDUP(p, VKI_PAGE_SIZE)
-
/*------------------------------------------------------------*/
/*--- Low-level address-space scanning, for the leak ---*/
/*--- detector. ---*/
cliques, and clique is the index of the current clique leader. */
static void _lc_scan_memory(Addr start, SizeT len, Int clique)
{
- Addr ptr = ROUNDUP(start, sizeof(Addr));
- Addr end = ROUNDDN(start+len, sizeof(Addr));
+ Addr ptr = VG_ROUNDUP(start, sizeof(Addr));
+ Addr end = VG_ROUNDDN(start+len, sizeof(Addr));
vki_sigset_t sigmask;
if (VG_DEBUG_LEAKCHECK)
if (!VG_(is_client_addr)(ptr) ||
!VG_(is_addressable)(ptr, sizeof(Addr), VKI_PROT_READ))
- ptr = PGROUNDUP(ptr+1); /* first page bad */
+ ptr = VG_PGROUNDUP(ptr+1); /* first page bad */
while (ptr < end) {
Addr addr;
/* Skip invalid chunks */
if (!(*lc_is_within_valid_secondary)(ptr)) {
- ptr = ROUNDUP(ptr+1, SECONDARY_SIZE);
+ ptr = VG_ROUNDUP(ptr+1, SECONDARY_SIZE);
continue;
}
longjmped out of a signal handler. */
VG_(sigprocmask)(VKI_SIG_SETMASK, &sigmask, NULL);
- ptr = PGROUNDUP(ptr+1); /* bad page - skip it */
+ ptr = VG_PGROUNDUP(ptr+1); /* bad page - skip it */
}
}