Make %p consistent with glibc and musl

See #61
This commit is contained in:
Justine Tunney 2021-03-06 14:26:36 -08:00
parent 937d921018
commit c3ed8d6c7f
11 changed files with 141 additions and 73 deletions

View file

@ -259,10 +259,9 @@ hidden int __fmt(void *fn, void *arg, const char *format, va_list va) {
alphabet = "0123456789abcdef";
switch ((d = *format++)) {
case 'p':
flags |= FLAGS_ZEROPAD;
width = POINTER_XDIGITS;
flags |= FLAGS_HASH;
log2base = 4;
signbit = 47;
signbit = 63;
goto FormatNumber;
case 'X':
alphabet = "0123456789ABCDEF";

View file

@ -17,14 +17,56 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/fmt.h"
#include "libc/limits.h"
#include "libc/math.h"
#include "libc/runtime/gc.h"
#include "libc/str/str.h"
#include "libc/testlib/testlib.h"
#include "libc/x/x.h"
TEST(RealFormatting, g) {
TEST(fmt, d) {
EXPECT_STREQ("-123", gc(xasprintf("%d", -123)));
EXPECT_STREQ("-1", gc(xasprintf("%d", -1)));
EXPECT_STREQ("1", gc(xasprintf("%d", 1)));
EXPECT_STREQ("16", gc(xasprintf("%d", 16)));
EXPECT_STREQ("2147483647", gc(xasprintf("%d", INT_MAX)));
EXPECT_STREQ("-2147483648", gc(xasprintf("%d", INT_MIN)));
EXPECT_STREQ(" -123", gc(xasprintf("%10d", -123)));
EXPECT_STREQ(" -1", gc(xasprintf("%10d", -1)));
EXPECT_STREQ(" 1", gc(xasprintf("%10d", 1)));
EXPECT_STREQ(" 16", gc(xasprintf("%10d", 16)));
EXPECT_STREQ("2147483647", gc(xasprintf("%10d", INT_MAX)));
EXPECT_STREQ("-2147483648", gc(xasprintf("%10d", INT_MIN)));
EXPECT_STREQ("-000000123", gc(xasprintf("%010d", -123)));
EXPECT_STREQ("-000000001", gc(xasprintf("%010d", -1)));
EXPECT_STREQ("0000000001", gc(xasprintf("%010d", 1)));
EXPECT_STREQ("0000000016", gc(xasprintf("%010d", 16)));
EXPECT_STREQ("2147483647", gc(xasprintf("%010d", INT_MAX)));
EXPECT_STREQ("-2147483648", gc(xasprintf("%010d", INT_MIN)));
EXPECT_STREQ("-123 ", gc(xasprintf("%-10d", -123)));
EXPECT_STREQ("-1 ", gc(xasprintf("%-10d", -1)));
EXPECT_STREQ("1 ", gc(xasprintf("%-10d", 1)));
EXPECT_STREQ("16 ", gc(xasprintf("%-10d", 16)));
EXPECT_STREQ("2147483647", gc(xasprintf("%-10d", INT_MAX)));
EXPECT_STREQ("-2147483648", gc(xasprintf("%-10d", INT_MIN)));
EXPECT_STREQ(" -123", gc(xasprintf("%+10d", -123)));
EXPECT_STREQ(" -1", gc(xasprintf("%+10d", -1)));
EXPECT_STREQ(" +1", gc(xasprintf("%+10d", 1)));
EXPECT_STREQ(" +16", gc(xasprintf("%+10d", 16)));
EXPECT_STREQ("+2147483647", gc(xasprintf("%+10d", INT_MAX)));
EXPECT_STREQ("-2147483648", gc(xasprintf("%+10d", INT_MIN)));
EXPECT_STREQ("-123", gc(xasprintf("% d", -123)));
EXPECT_STREQ("-1", gc(xasprintf("% d", -1)));
EXPECT_STREQ(" 1", gc(xasprintf("% d", 1)));
EXPECT_STREQ(" 16", gc(xasprintf("% d", 16)));
EXPECT_STREQ(" 2147483647", gc(xasprintf("% d", INT_MAX)));
EXPECT_STREQ("-2147483648", gc(xasprintf("% d", INT_MIN)));
}
TEST(fmt, g) {
EXPECT_STREQ("1", gc(xasprintf("%g", 1.)));
EXPECT_STREQ(" 1", gc(xasprintf("% g", 1.)));
EXPECT_STREQ("+1", gc(xasprintf("%+g", 1.)));
EXPECT_STREQ("-1", gc(xasprintf("%g", -1.)));
EXPECT_STREQ("10", gc(xasprintf("%g", 10.)));
EXPECT_STREQ("10", gc(xasprintf("%.0g", 10.)));
@ -68,7 +110,7 @@ TEST(RealFormatting, g) {
EXPECT_STREQ(" 1.79769E+308", gc(xasprintf("%13G", __DBL_MAX__)));
}
TEST(RealFormatting, f) {
TEST(fmt, f) {
EXPECT_STREQ("3.141593", gc(xasprintf("%f", 0x1.921fb54442d1846ap+1)));
EXPECT_STREQ("3.1415926535897931",
gc(xasprintf("%.16f", 0x1.921fb54442d1846ap+1)));
@ -133,7 +175,7 @@ TEST(RealFormatting, f) {
gc(xasprintf("%10F", __DBL_MAX__)));
}
TEST(RealFormatting, e) {
TEST(fmt, e) {
EXPECT_STREQ("3.14159", gc(xasprintf("%g", 0x1.921fb54442d1846ap+1)));
EXPECT_STREQ("3.141592653589793",
gc(xasprintf("%.16g", 0x1.921fb54442d1846ap+1)));
@ -173,7 +215,7 @@ TEST(RealFormatting, e) {
EXPECT_STREQ(" +1.797693E+308", gc(xasprintf("%+24E", __DBL_MAX__)));
}
TEST(RealFormatting, a) {
TEST(fmt, a) {
EXPECT_STREQ("0x1.921fb54442d18p+1",
gc(xasprintf("%a", 0x1.921fb54442d1846ap+1)));
EXPECT_STREQ("0X1.921FB54442D18P+1",
@ -197,3 +239,40 @@ TEST(RealFormatting, a) {
EXPECT_STREQ(" 0X1.E9A488E8A71DEP+14", gc(xasprintf("%24A", 31337.1337)));
EXPECT_STREQ(" -0X1.E9A488E8A71DEP+14", gc(xasprintf("%24A", -31337.1337)));
}
TEST(fmt, p) {
EXPECT_STREQ("0x1", gc(xasprintf("%p", 1)));
EXPECT_STREQ("0x10", gc(xasprintf("%p", 16)));
EXPECT_STREQ("0x31337", gc(xasprintf("%p", 0x31337)));
EXPECT_STREQ("0xffffffff", gc(xasprintf("%p", 0xffffffff)));
EXPECT_STREQ("0xffff800000031337", gc(xasprintf("%p", 0xffff800000031337)));
EXPECT_STREQ(" 0x1", gc(xasprintf("%10p", 1)));
EXPECT_STREQ(" 0x10", gc(xasprintf("%10p", 16)));
EXPECT_STREQ(" 0x31337", gc(xasprintf("%10p", 0x31337)));
EXPECT_STREQ("0xffffffff", gc(xasprintf("%10p", 0xffffffff)));
EXPECT_STREQ("0xffff800000031337", gc(xasprintf("%10p", 0xffff800000031337)));
EXPECT_STREQ("0x00000001", gc(xasprintf("%010p", 1)));
EXPECT_STREQ("0x00000010", gc(xasprintf("%010p", 16)));
EXPECT_STREQ("0x00031337", gc(xasprintf("%010p", 0x31337)));
EXPECT_STREQ("0xffffffff", gc(xasprintf("%010p", 0xffffffff)));
EXPECT_STREQ("0xffff800000031337",
gc(xasprintf("%010p", 0xffff800000031337)));
EXPECT_STREQ("0x1 ", gc(xasprintf("%-10p", 1)));
EXPECT_STREQ("0x10 ", gc(xasprintf("%-10p", 16)));
EXPECT_STREQ("0x31337 ", gc(xasprintf("%-10p", 0x31337)));
EXPECT_STREQ("0xffffffff", gc(xasprintf("%-10p", 0xffffffff)));
EXPECT_STREQ("0xffff800000031337",
gc(xasprintf("%-10p", 0xffff800000031337)));
EXPECT_STREQ(" 0x1", gc(xasprintf("%+10p", 1)));
EXPECT_STREQ(" 0x10", gc(xasprintf("%+10p", 16)));
EXPECT_STREQ(" 0x31337", gc(xasprintf("%+10p", 0x31337)));
EXPECT_STREQ("0xffffffff", gc(xasprintf("%+10p", 0xffffffff)));
EXPECT_STREQ("0xffff800000031337",
gc(xasprintf("%+10p", 0xffff800000031337)));
EXPECT_STREQ(" 0x1", gc(xasprintf("% 10p", 1)));
EXPECT_STREQ(" 0x10", gc(xasprintf("% 10p", 16)));
EXPECT_STREQ(" 0x31337", gc(xasprintf("% 10p", 0x31337)));
EXPECT_STREQ("0xffffffff", gc(xasprintf("% 10p", 0xffffffff)));
EXPECT_STREQ("0xffff800000031337",
gc(xasprintf("% 10p", 0xffff800000031337)));
}

View file

@ -482,34 +482,6 @@ TEST(sprintf, testOverflow_truncationNotSaturation) {
EXPECT_STREQ("Test16 65535", Format("%s%hhi %hu", "Test", 10000, 0xFFFFFFFF));
}
TEST(sprintf, test_pointer) {
sprintf(buffer, "%p", (void *)0x1234U);
if (sizeof(void *) == 4U) {
EXPECT_STREQ("00001234", buffer);
} else {
EXPECT_STREQ("000000001234", buffer);
}
sprintf(buffer, "%p", (void *)0x12345678U);
if (sizeof(void *) == 4U) {
EXPECT_STREQ("12345678", buffer);
} else {
EXPECT_STREQ("000012345678", buffer);
}
sprintf(buffer, "%p-%p", (void *)0x12345678U, (void *)0x7EDCBA98U);
if (sizeof(void *) == 4U) {
EXPECT_STREQ("12345678-7edcba98", buffer);
} else {
EXPECT_STREQ("000012345678-00007edcba98", buffer);
}
if (sizeof(uintptr_t) == sizeof(uint64_t)) {
sprintf(buffer, "%p", (void *)(uintptr_t)0xFFFFFFFFU);
EXPECT_STREQ("0000ffffffff", buffer);
} else {
sprintf(buffer, "%p", (void *)(uintptr_t)0xFFFFFFFFU);
EXPECT_STREQ("ffffffff", buffer);
}
}
TEST(sprintf, test_unknown_flag) {
EXPECT_STREQ("kmarco", Format("%kmarco", 42, 37));
}
@ -595,22 +567,6 @@ TEST(xasprintf, test) {
free(pp);
}
TEST(xasprintf, nullPointer) {
ASSERT_STREQ("000000000000", gc(xasprintf("%p", NULL)));
}
TEST(xasprintf, pointer_doesntShowNonCanonicalZeroes) {
ASSERT_STREQ("100000000010", gc(xasprintf("%p", 0x0000100000000010)));
ASSERT_STREQ("0x100000000010", gc(xasprintf("%#p", 0x0000100000000010)));
}
TEST(xasprintf, nonCanonicalPointer_discardsHighBits_ratherThanSaturate) {
ASSERT_STREQ("100000000010", gc(xasprintf("%p", 0x1000100000000010)));
ASSERT_STREQ("0x100000000010", gc(xasprintf("%#p", 0x1000100000000010)));
ASSERT_STREQ("7fffffffffff", gc(xasprintf("%p", 0x7fffffffffff)));
ASSERT_STREQ("0x7fffffffffff", gc(xasprintf("%#p", 0x7fffffffffff)));
}
TEST(xasprintf, hugeNtoa) {
ASSERT_STREQ(
"0b1111111111111111111111111111111111111111111111111111111111111111111111"

View file

@ -0,0 +1,32 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2021 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/mem/mem.h"
#include "libc/stdio/stdio.h"
#include "libc/testlib/hyperion.h"
#include "libc/testlib/testlib.h"
TEST(fgets, test) {
FILE *f;
char buf[29];
f = fmemopen(strdup(kHyperion), kHyperionSize, "r+");
ASSERT_STREQ("The fall of Hyperion - a Dre", fgets(buf, sizeof(buf), f));
ASSERT_STREQ("am\n", fgets(buf, sizeof(buf), f));
ASSERT_STREQ("John Keats\n", fgets(buf, sizeof(buf), f));
fclose(f);
}

View file

@ -1295,7 +1295,7 @@ static void DrawMemoryZoomed(struct Panel *p, struct MemoryView *view,
free(ranges.p);
high = false;
for (c = i = 0; i < p->bottom - p->top; ++i) {
AppendFmt(&p->lines[i], "%p ",
AppendFmt(&p->lines[i], "%012lx ",
(view->start + i) * DUMPWIDTH * (1ull << view->zoom));
for (j = 0; j < DUMPWIDTH; ++j, ++c) {
a = ((view->start + i) * DUMPWIDTH + j + 0) * (1ull << view->zoom);
@ -1331,7 +1331,7 @@ static void DrawMemoryUnzoomed(struct Panel *p, struct MemoryView *view,
bool high, changed;
high = false;
for (i = 0; i < p->bottom - p->top; ++i) {
AppendFmt(&p->lines[i], "%p ", (view->start + i) * DUMPWIDTH);
AppendFmt(&p->lines[i], "%012lx ", (view->start + i) * DUMPWIDTH);
for (j = 0; j < DUMPWIDTH; ++j) {
k = (view->start + i) * DUMPWIDTH + j;
c = VirtualBing(k);
@ -1421,7 +1421,7 @@ static void DrawBreakpoints(struct Panel *p) {
sym = DisFindSym(dis, addr);
name = sym != -1 ? dis->syms.stab + dis->syms.p[sym].name : "UNKNOWN";
s = buf;
s += sprintf(s, "%p ", addr);
s += sprintf(s, "%012lx ", addr);
CHECK_LT(Demangle(s, name, DIS_MAX_SYMBOL_LENGTH), buf + ARRAYLEN(buf));
AppendPanel(p, line - breakpointsstart, buf);
if (sym != -1 && addr != dis->syms.p[sym].addr) {
@ -1461,7 +1461,7 @@ static void DrawFrames(struct Panel *p) {
sym = DisFindSym(dis, rp);
name = sym != -1 ? dis->syms.stab + dis->syms.p[sym].name : "UNKNOWN";
s = line;
s += sprintf(s, "%p %p ", Read64(m->ss) + bp, rp);
s += sprintf(s, "%012lx %012lx ", Read64(m->ss) + bp, rp);
s = Demangle(s, name, DIS_MAX_SYMBOL_LENGTH);
AppendPanel(p, i - framesstart, line);
if (sym != -1 && rp != dis->syms.p[sym].addr) {
@ -1501,14 +1501,15 @@ static void CheckFramePointerImpl(void) {
sp = Read64(m->sp);
while (bp) {
if (!(r = FindReal(m, Read64(m->ss) + bp))) {
LOGF("corrupt frame: %p", bp);
LOGF("corrupt frame: %012lx", bp);
ThrowProtectionFault(m);
}
sp = bp;
bp = Read64(r + 0) - 0;
rp = Read64(r + 8) - 1;
if (!bp && !(m->bofram[0] <= rp && rp <= m->bofram[1])) {
LOGF("bad frame !(%p <= %p <= %p)", m->bofram[0], rp, m->bofram[1]);
LOGF("bad frame !(%012lx <= %012lx <= %012lx)", m->bofram[0], rp,
m->bofram[1]);
ThrowProtectionFault(m);
}
}
@ -1831,7 +1832,7 @@ static void OnDebug(void) {
}
static void OnSegmentationFault(void) {
snprintf(systemfailure, sizeof(systemfailure), "SEGMENTATION FAULT %p",
snprintf(systemfailure, sizeof(systemfailure), "SEGMENTATION FAULT %012lx",
m->faultaddr);
LaunchDebuggerReactively();
}
@ -2189,7 +2190,7 @@ static void OnBinbase(struct Machine *m) {
unsigned i;
int64_t skew;
skew = m->xedd->op.disp * 512;
LOGF("skew binbase %,ld @ %p", skew, GetIp());
LOGF("skew binbase %,ld @ %012lx", skew, GetIp());
for (i = 0; i < dis->syms.i; ++i) dis->syms.p[i].addr += skew;
for (i = 0; i < dis->loads.i; ++i) dis->loads.p[i].addr += skew;
for (i = 0; i < breakpoints.i; ++i) breakpoints.p[i].addr += skew;
@ -2562,7 +2563,7 @@ static void Exec(void) {
if (!(interrupt = setjmp(m->onhalt))) {
if (!(action & CONTINUE) &&
(bp = IsAtBreakpoint(&breakpoints, GetIp())) != -1) {
LOGF("BREAK1 %p", breakpoints.p[bp].addr);
LOGF("BREAK1 %012lx", breakpoints.p[bp].addr);
tuimode = true;
LoadInstruction(m);
ExecuteInstruction(m);
@ -2573,7 +2574,7 @@ static void Exec(void) {
for (;;) {
LoadInstruction(m);
if ((bp = IsAtBreakpoint(&breakpoints, GetIp())) != -1) {
LOGF("BREAK2 %p", breakpoints.p[bp].addr);
LOGF("BREAK2 %012lx", breakpoints.p[bp].addr);
action &= ~(FINISH | NEXT | CONTINUE);
tuimode = true;
break;
@ -2625,7 +2626,7 @@ static void Tui(void) {
if ((action & (FINISH | NEXT | CONTINUE)) &&
(bp = IsAtBreakpoint(&breakpoints, GetIp())) != -1) {
action &= ~(FINISH | NEXT | CONTINUE);
LOGF("BREAK %p", breakpoints.p[bp].addr);
LOGF("BREAK %012lx", breakpoints.p[bp].addr);
}
} else {
m->xedd = (struct XedDecodedInst *)m->icache[0];

View file

@ -144,7 +144,7 @@ static void DisCanonizeSyms(struct Dis *d) {
d->syms.n = i;
}
for (i = 0; i < d->syms.i; ++i) {
DEBUGF("%p-%p %s", d->syms.p[i].addr,
DEBUGF("%012lx-%012lx %s", d->syms.p[i].addr,
d->syms.p[i].addr + (d->syms.p[i].size ? d->syms.p[i].size - 1 : 0),
d->syms.stab + d->syms.p[i].name);
}

View file

@ -78,7 +78,7 @@ static void LoadElf(struct Machine *m, struct Elf *elf) {
unsigned i;
Elf64_Phdr *phdr;
m->ip = elf->base = elf->ehdr->e_entry;
VERBOSEF("LOADELF ENTRY %p", m->ip);
VERBOSEF("LOADELF ENTRY %012lx", m->ip);
for (i = 0; i < elf->ehdr->e_phnum; ++i) {
phdr = GetElfSegmentHeaderAddress(elf->ehdr, elf->size, i);
switch (phdr->p_type) {

View file

@ -49,14 +49,14 @@ static void FormatStartPage(struct Pml4tFormater *pp, int64_t start) {
pp->t = true;
pp->start = start;
if (pp->lines++) AppendChar(&pp->b, '\n');
AppendFmt(&pp->b, "%p-", start);
AppendFmt(&pp->b, "%012lx-", start);
}
static void FormatEndPage(struct Pml4tFormater *pp, int64_t end) {
int64_t size;
pp->t = false;
size = end - pp->start;
AppendFmt(&pp->b, "%p %p %,ld bytes", end - 1, size, size);
AppendFmt(&pp->b, "%012lx %012lx %,ld bytes", end - 1, size, size);
}
static void *GetPt(struct Machine *m, uint64_t r) {

View file

@ -530,7 +530,7 @@ static int64_t OpBrk(struct Machine *m, int64_t addr) {
}
static int OpMunmap(struct Machine *m, int64_t virt, uint64_t size) {
VERBOSEF("MUNMAP%s %p %,ld", GetSimulated(), virt, size);
VERBOSEF("MUNMAP%s %012lx %,ld", GetSimulated(), virt, size);
return FreeVirtual(m, virt, size);
}
@ -538,8 +538,8 @@ static int64_t OpMmap(struct Machine *m, int64_t virt, size_t size, int prot,
int flags, int fd, int64_t offset) {
void *tmp;
uint64_t key;
VERBOSEF("MMAP%s %p %,ld %#x %#x %d %#lx", GetSimulated(), virt, size, prot,
flags, fd, offset);
VERBOSEF("MMAP%s %012lx %,ld %#x %#x %d %#lx", GetSimulated(), virt, size,
prot, flags, fd, offset);
if (prot & PROT_READ) {
key = 0x0205;
if (prot & PROT_WRITE) key |= 2;
@ -821,12 +821,12 @@ static ssize_t OpWrite(struct Machine *m, int fd, int64_t addr, size_t size) {
if ((rc = m->fds.p[fd].cb->writev(m->fds.p[fd].fd, iv.p, iv.i)) != -1) {
SetReadAddr(m, addr, rc);
} else {
VERBOSEF("write(%d [%d], %p, %zu) failed: %s", fd, m->fds.p[fd].fd,
VERBOSEF("write(%d [%d], %012lx, %zu) failed: %s", fd, m->fds.p[fd].fd,
addr, size, strerror(errno));
}
}
} else {
VERBOSEF("write(%d, %p, %zu) bad fd", fd, addr, size);
VERBOSEF("write(%d, %012lx, %zu) bad fd", fd, addr, size);
rc = ebadf();
}
FreeIovs(&iv);

View file

@ -44,7 +44,7 @@ void ThrowDivideError(struct Machine *m) {
void ThrowSegmentationFault(struct Machine *m, int64_t va) {
m->faultaddr = va;
if (m->xedd) m->ip -= m->xedd->length;
WARNF("%s%s ADDR %p IP %p AX %lx CX %lx DX %lx BX %lx SP %lx "
WARNF("%s%s ADDR %012lx IP %012lx AX %lx CX %lx DX %lx BX %lx SP %lx "
"BP %lx SI %lx DI %lx R8 %lx R9 %lx R10 %lx R11 %lx R12 %lx R13 %lx "
"R14 %lx R15 %lx",
"SEGMENTATION FAULT", IsGenuineCosmo() ? " SIMULATED" : "", va, m->ip,

View file

@ -185,7 +185,8 @@ struct Package *LoadPackage(const char *path) {
pkg->objects.p = (struct Object *)((intptr_t)pkg->objects.p + (intptr_t)pkg);
pkg->symbols.p = (struct Symbol *)((intptr_t)pkg->symbols.p + (intptr_t)pkg);
CHECK(strcmp(path, &pkg->strings.p[pkg->path]) == 0,
"corrupt package: %`'s pkg=%p strings=%p", path, pkg, pkg->strings.p);
"corrupt package: %`'s pkg=%012lx strings=%012lx", path, pkg,
pkg->strings.p);
pkg->addr = pkg;
pkg->size = st.st_size;
return pkg;