Get address sanitizer mostly working

This commit is contained in:
Justine Tunney 2020-09-03 05:44:37 -07:00
parent 1f1f3cd477
commit 7327c345f9
149 changed files with 3777 additions and 3457 deletions

View file

@ -63,7 +63,9 @@ $(TOOL_BUILD_LIB_A).pkg: \
$(TOOL_BUILD_LIB_A_OBJS) \
$(foreach x,$(TOOL_BUILD_LIB_A_DIRECTDEPS),$($(x)_A).pkg)
o/$(MODE)/tool/build/lib/fpu.o: OVERRIDE_CFLAGS += -ffast-math
# $(TOOL_BUILD_LIB_A_OBJS): \
# OVERRIDE_CFLAGS += \
# -fsanitize=address
TOOL_BUILD_LIB_LIBS = $(foreach x,$(TOOL_BUILD_LIB_ARTIFACTS),$($(x)))
TOOL_BUILD_LIB_SRCS = $(foreach x,$(TOOL_BUILD_LIB_ARTIFACTS),$($(x)_SRCS))

View file

@ -29,7 +29,7 @@
static double SseRoundDouble(struct Machine *m, double x) {
switch (m->sse.rc) {
case 0:
return nearbyint(x);
return rint(x);
case 1:
return floor(x);
case 2:
@ -134,7 +134,7 @@ static void OpPpiWpsqCvtps2pi(struct Machine *m, uint32_t rde) {
memcpy(f, GetModrmRegisterXmmPointerRead8(m, rde), 8);
switch (m->sse.rc) {
case 0:
for (i = 0; i < 2; ++i) n[i] = nearbyintf(f[i]);
for (i = 0; i < 2; ++i) n[i] = rintf(f[i]);
break;
case 1:
for (i = 0; i < 2; ++i) n[i] = floorf(f[i]);
@ -250,7 +250,7 @@ static void OpVdqWpsCvtps2dq(struct Machine *m, uint32_t rde) {
memcpy(f, GetModrmRegisterXmmPointerRead16(m, rde), 16);
switch (m->sse.rc) {
case 0:
for (i = 0; i < 4; ++i) n[i] = nearbyintf(f[i]);
for (i = 0; i < 4; ++i) n[i] = rintf(f[i]);
break;
case 1:
for (i = 0; i < 4; ++i) n[i] = floorf(f[i]);

View file

@ -173,7 +173,7 @@ void Dis(struct Dis *d, struct Machine *m, int64_t addr) {
struct DisOp op;
long i, j, n, si, max, toto, symbol;
unique = 0;
max = 99999;
max = 999999;
DisFreeOps(&d->ops);
for (i = 0; i < max; ++i) {
xed_decoded_inst_zero_set_mode(d->xedd, XED_MACHINE_MODE_LONG_64);
@ -224,10 +224,12 @@ void Dis(struct Dis *d, struct Machine *m, int64_t addr) {
xed_instruction_length_decode(d->xedd, d->raw, n);
DCHECK_GT(n, 0);
p = DisLineCode((struct DisBuilder){d, d->xedd, addr}, d->buf);
CHECK_LT(p - d->buf, sizeof(d->buf));
n = d->xedd->op.error ? 1 : d->xedd->length;
DCHECK_GT(n, 0);
} else {
p = DisLineData((struct DisBuilder){d, d->xedd, addr}, d->buf, d->raw, n);
CHECK_LT(p - d->buf, sizeof(d->buf));
}
DCHECK_LT(p, d->buf + sizeof(d->buf));
DCHECK_LT(strlen(d->buf), sizeof(d->buf));

View file

@ -175,8 +175,8 @@ static char *DisName(struct DisBuilder b, char *bp, const char *name,
*/
char *DisInst(struct DisBuilder b, char *p, const char *spec) {
long i, n;
char sbuf[128];
char args[4][64];
char sbuf[256];
char args[4][128];
char *s, *name, *state;
bool hasarg, hasmodrm, hasregister, hasmemory;
CHECK_EQ(0, (int)b.xedd->op.error);
@ -190,7 +190,7 @@ char *DisInst(struct DisBuilder b, char *p, const char *spec) {
hasarg = true;
hasregister |= *s == '%';
hasmemory |= *s == 'O';
DisArg(b, args[n], s);
CHECK_LT(DisArg(b, args[n], s) - args[n], sizeof(args[n]));
}
if (g_dis_high) p = DisHigh(p, g_dis_high->keyword);
p = DisName(b, p, name, hasarg && !hasregister && hasmemory);

View file

@ -164,7 +164,7 @@ struct ElfWriter *elfwriter_open(const char *path, int mode) {
CHECK_NE(-1, (elf->fd = open(elf->tmppath,
O_CREAT | O_TRUNC | O_RDWR | O_EXCL, mode)));
CHECK_NE(-1, ftruncate(elf->fd, (elf->mapsize = FRAMESIZE)));
CHECK_NE(MAP_FAILED, (elf->map = mmap((void *)(intptr_t)kFixedMappingsStart,
CHECK_NE(MAP_FAILED, (elf->map = mmap((void *)(intptr_t)kFixedmapStart,
elf->mapsize, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_FIXED, elf->fd, 0)));
elf->ehdr = memcpy(elf->map, &kObjHeader, (elf->wrote = sizeof(kObjHeader)));

View file

@ -235,7 +235,7 @@ static long double FpuDiv(struct Machine *m, long double x, long double y) {
static long double FpuRound(struct Machine *m, long double x) {
switch (m->fpu.rc) {
case 0:
return nearbyintl(x);
return rintl(x);
case 1:
return floorl(x);
case 2:

View file

@ -59,7 +59,7 @@
#define MUTATING true
#define READONLY false
#define UNCONDITIONAL true
#define BITS (8 << RegLog2(rde))
#define BITS (8u << RegLog2(rde))
#define SIGN (1ull << (BITS - 1))
#define MASK (SIGN | (SIGN - 1))
#define SHIFTMASK (BITS - 1)
@ -1330,8 +1330,8 @@ static void OpSqrtpsd(struct Machine *m, uint32_t rde) {
}
static void OpRsqrtps(struct Machine *m, uint32_t rde) {
unsigned i;
float_v x;
unsigned i;
if (Rep(rde) != 3) {
memcpy(&x, GetModrmRegisterXmmPointerRead16(m, rde), 16);
for (i = 0; i < 4; ++i) x[i] = 1.f / sqrtf(x[i]);
@ -1344,8 +1344,8 @@ static void OpRsqrtps(struct Machine *m, uint32_t rde) {
}
static void OpRcpps(struct Machine *m, uint32_t rde) {
int i;
float_v x;
unsigned i;
if (Rep(rde) != 3) {
memcpy(&x, GetModrmRegisterXmmPointerRead16(m, rde), 16);
for (i = 0; i < 4; ++i) x[i] = 1.f / x[i];
@ -1551,7 +1551,7 @@ static float_v OpAddsubps(struct Machine *m, float_v x, float_v y) {
}
static float_v OpMinps(struct Machine *m, float_v x, float_v y) {
int i;
unsigned i;
for (i = 0; i < 4; ++i) {
x[i] = MIN(x[i], y[i]);
}
@ -1559,7 +1559,7 @@ static float_v OpMinps(struct Machine *m, float_v x, float_v y) {
}
static double_v OpMinpd(struct Machine *m, double_v x, double_v y) {
int i;
unsigned i;
for (i = 0; i < 4; ++i) {
x[i] = MIN(x[i], y[i]);
}
@ -1567,7 +1567,7 @@ static double_v OpMinpd(struct Machine *m, double_v x, double_v y) {
}
static float_v OpMaxps(struct Machine *m, float_v x, float_v y) {
int i;
unsigned i;
for (i = 0; i < 4; ++i) {
x[i] = MAX(x[i], y[i]);
}
@ -1575,7 +1575,7 @@ static float_v OpMaxps(struct Machine *m, float_v x, float_v y) {
}
static double_v OpMaxpd(struct Machine *m, double_v x, double_v y) {
int i;
unsigned i;
for (i = 0; i < 4; ++i) {
x[i] = MAX(x[i], y[i]);
}
@ -1721,9 +1721,10 @@ static void OpAlubFlipRo(struct Machine *m, uint32_t rde, int h) {
}
static void OpAlubi(struct Machine *m, uint32_t rde, int h) {
uint8_t *a;
uint8_t *a, x;
a = GetModrmRegisterBytePointerWrite(m, rde);
Write8(a, Alu(0, h, Read8(a), m->xedd->op.uimm0, &m->flags));
x = Alu(0, h, Read8(a), m->xedd->op.uimm0, &m->flags);
if (h != ALU_CMP) Write8(a, x);
}
static void OpAlubiRo(struct Machine *m, uint32_t rde, int h) {
@ -1761,10 +1762,10 @@ static void OpAluwFlipRo(struct Machine *m, uint32_t rde, int h) {
static void OpAluwi(struct Machine *m, uint32_t rde, int h) {
uint8_t *a;
uint64_t x;
a = GetModrmRegisterWordPointerWriteOszRexw(m, rde);
WriteRegisterOrMemory(
rde, a,
Alu(RegLog2(rde), h, ReadMemory(rde, a), m->xedd->op.uimm0, &m->flags));
x = Alu(RegLog2(rde), h, ReadMemory(rde, a), m->xedd->op.uimm0, &m->flags);
if (h != ALU_CMP) WriteRegisterOrMemory(rde, a, x);
}
static void OpAluwiRo(struct Machine *m, uint32_t rde, int h) {
@ -1823,11 +1824,32 @@ static void OpTestRaxIvds(struct Machine *m, uint32_t rde) {
&m->flags);
}
long opcount[256 * 4];
void ExecuteInstruction(struct Machine *m) {
uint32_t rde;
m->ip += m->xedd->length;
rde = m->xedd->op.rde;
opcount[m->xedd->op.map << 8 | m->xedd->op.opcode]++;
switch (m->xedd->op.map << 8 | m->xedd->op.opcode) {
CASE(0x089, OpMovEvqpGvqp(m, rde));
CASE(0x083, OpAluwi(m, rde, ModrmReg(rde)));
CASR(0x070, if (GetCond(m, 0x0)) OpJmp(m));
CASR(0x071, if (GetCond(m, 0x1)) OpJmp(m));
CASR(0x072, if (GetCond(m, 0x2)) OpJmp(m));
CASR(0x073, if (GetCond(m, 0x3)) OpJmp(m));
CASR(0x074, if (GetCond(m, 0x4)) OpJmp(m));
CASR(0x075, if (GetCond(m, 0x5)) OpJmp(m));
CASR(0x076, if (GetCond(m, 0x6)) OpJmp(m));
CASR(0x077, if (GetCond(m, 0x7)) OpJmp(m));
CASR(0x078, if (GetCond(m, 0x8)) OpJmp(m));
CASR(0x079, if (GetCond(m, 0x9)) OpJmp(m));
CASR(0x07A, if (GetCond(m, 0xa)) OpJmp(m));
CASR(0x07B, if (GetCond(m, 0xb)) OpJmp(m));
CASR(0x07C, if (GetCond(m, 0xc)) OpJmp(m));
CASR(0x07D, if (GetCond(m, 0xd)) OpJmp(m));
CASR(0x07E, if (GetCond(m, 0xe)) OpJmp(m));
CASR(0x07F, if (GetCond(m, 0xf)) OpJmp(m));
CASR(0x0B0 ... 0x0B7, OpMovZbIb(m, rde));
CASR(0x0B8 ... 0x0BF, OpMovZvqpIvqp(m, rde));
CASR(0x050 ... 0x057, OpPushZvq(m, rde));
@ -1891,32 +1913,14 @@ void ExecuteInstruction(struct Machine *m) {
CASE(0x06D, OpString(m, rde, STRING_INS));
CASE(0x06E, OpString(m, rde, STRING_OUTS));
CASE(0x06F, OpString(m, rde, STRING_OUTS));
CASR(0x070, if (GetCond(m, 0x0)) OpJmp(m));
CASR(0x071, if (GetCond(m, 0x1)) OpJmp(m));
CASR(0x072, if (GetCond(m, 0x2)) OpJmp(m));
CASR(0x073, if (GetCond(m, 0x3)) OpJmp(m));
CASR(0x074, if (GetCond(m, 0x4)) OpJmp(m));
CASR(0x075, if (GetCond(m, 0x5)) OpJmp(m));
CASR(0x076, if (GetCond(m, 0x6)) OpJmp(m));
CASR(0x077, if (GetCond(m, 0x7)) OpJmp(m));
CASR(0x078, if (GetCond(m, 0x8)) OpJmp(m));
CASR(0x079, if (GetCond(m, 0x9)) OpJmp(m));
CASR(0x07A, if (GetCond(m, 0xa)) OpJmp(m));
CASR(0x07B, if (GetCond(m, 0xb)) OpJmp(m));
CASR(0x07C, if (GetCond(m, 0xc)) OpJmp(m));
CASR(0x07D, if (GetCond(m, 0xd)) OpJmp(m));
CASR(0x07E, if (GetCond(m, 0xe)) OpJmp(m));
CASR(0x07F, if (GetCond(m, 0xf)) OpJmp(m));
CASR(0x080, OpAlubi(m, rde, ModrmReg(rde)));
CASE(0x081, OpAluwi(m, rde, ModrmReg(rde)));
CASR(0x082, OpAlubi(m, rde, ModrmReg(rde)));
CASE(0x083, OpAluwi(m, rde, ModrmReg(rde)));
CASR(0x084, OpAlubRo(m, rde, TEST));
CASE(0x085, OpAluwRo(m, rde, TEST));
CASE(0x086, OpXchgGbEb(m, rde));
CASE(0x087, OpXchgGvqpEvqp(m, rde));
CASE(0x088, OpMovEbGb(m, rde));
CASE(0x089, OpMovEvqpGvqp(m, rde));
CASE(0x08A, OpMovGbEb(m, rde));
CASE(0x08B, OpMovGvqpEvqp(m, rde));
CASE(0x08C, OpMovEvqpSw(m));

View file

@ -136,7 +136,7 @@ struct Machine {
int64_t faultaddr;
uint8_t stash[4096];
uint8_t xmmtype[2][8];
struct XedDecodedInst icache[512];
struct XedDecodedInst icache[1024];
struct MachineFds fds;
};

View file

@ -117,14 +117,13 @@ union MachineVector {
void OpSse(struct Machine *m, uint32_t rde, enum OpSseKernel kernel) {
int i;
uint8_t *p;
union MachineVector x, y, t;
union MachineVector x, y;
p = GetModrmRegisterXmmPointerRead16(m, rde);
if (Osz(rde)) {
memcpy(&y, p, 16);
} else {
memset(&t, 0, 16);
memcpy(&t, p, 8);
memcpy(&y, &t, 16);
memset(&y, 0, 16);
memcpy(&y, p, 8);
}
memcpy(&x, XmmRexrReg(m, rde), 16);
switch (kernel) {