From 179e0a37dca3eed1de939a7e490d2e2e9de4ec6a Mon Sep 17 00:00:00 2001 From: dzaima Date: Tue, 15 Nov 2022 16:52:52 +0200 Subject: [PATCH] more tail verify work --- src/builtins/sysfn.c | 13 +++++++++++-- src/opt/mm_buddy.c | 24 ++++++++++++------------ 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/builtins/sysfn.c b/src/builtins/sysfn.c index 02de9688..89717314 100644 --- a/src/builtins/sysfn.c +++ b/src/builtins/sysfn.c @@ -347,8 +347,10 @@ B rand_range_c2(B t, B w, B x) { } else if (max!=2) { u64am = (am+ 7)>>3; t=t_i8arr; } else { u64am = (am+63)>>6; t=t_bitarr; } - assert((u64am<<3) >= (t==t_bitarr? BIT_N(am)<<3 : am<= aExact); + r = m_arr(offsetof(TyArr,a) + aFilled, t, am); void* rp = ((TyArr*)r)->a; if (max & (max-1)) { // not power of two if (t==t_i32arr) PLAINLOOP for (usz i = 0; i < am; i++) ((i32*)rp)[i] = wy2u0k(wyrand(&seed), max); @@ -366,6 +368,7 @@ B rand_range_c2(B t, B w, B x) { end:; PLAINLOOP for (usz i = 0; i < u64am; i++) ((u64*)rp)[i] = wyrand(&seed) & mask; } + REINIT_TAIL(r, offsetof(TyArr,a) + aExact, offsetof(TyArr,a) + aFilled); } RAND_END; @@ -1167,6 +1170,12 @@ B bitcast_impl(B el0, B el1, B x) { B pr = r; r = taga(TI(r,slice)(r, 0, IA(r))); arr_shSetI(a(r), xr, shObj(pr)); // safe to use pr because r has refcount>1 and slice only consumes one, leaving some behind + } else { + #if VERIFY_TAIL + if (xct.s==1 && rct.s!=1) { + REINIT_TAIL(a(r), offsetof(TyArr,a)+IA(r)/8, offsetof(TyArr,a) + (BIT_N(IA(r))<<3)); + } + #endif } return set_bit_result(r, rt, xr, rl, sh); } diff --git a/src/opt/mm_buddy.c b/src/opt/mm_buddy.c index ee51029a..d01088f1 100644 --- a/src/opt/mm_buddy.c +++ b/src/opt/mm_buddy.c @@ -63,7 +63,7 @@ void verifyEnd(void* ptr, u64 sz, u64 start, u64 end) { if (end+64>sz) { printf("Bad used range: "N64u".."N64u", allocation size "N64u"\n", start, end, sz); exit(1); } } void tailVerifyReinit(void* ptr, u64 filled, u64 end) { - if(filled<=8) { printf("Bad reinit start: "N64u".."N64u"\n", filled, end); exit(1); } + if(filled>end || filled<=8) { printf("Bad reinit arguments: "N64u".."N64u"\n", filled, end); exit(1); } verifyEnd(ptr, mm_size(ptr), filled, end); tailVerifyInit(ptr, filled, end, mm_size(ptr)); } @@ -78,9 +78,9 @@ NOINLINE void dumpByte(bool exp, bool has, void* ptr, u64 o) { printf("%02x", v); if (col) printf("\x1b[0m"); } -NOINLINE NORETURN void tailFail(u64 got, u64 exp, void* ptr, u64 off, int len, u64 allocFilled, u64 allocTotal) { +NOINLINE NORETURN void tailFail(u64 got, u64 exp, void* ptr, u64 off, int len, u64 allocFilled, u64 allocTotal, u64 ia) { printf("Corrupted tail @ %p + "N64d", checked length %d\n", ptr, off, len); - printf("Allocation filled with "N64d" bytes, total space "N64d"\n", allocFilled, allocTotal); + printf("Allocation filled with "N64d" bytes, total allocation "N64d", IA=="N64d"\n", allocFilled, allocTotal, ia); printf("Expected: x=%016"SCNx64" / u="N64u"\nGot: x=%016"SCNx64" / u="N64u"\n\n", exp, exp, got, got); fflush(stdout); fflush(stderr); @@ -98,19 +98,19 @@ NOINLINE NORETURN void tailFail(u64 got, u64 exp, void* ptr, u64 off, int len, u __builtin_trap(); } void tailVerifyFree(void* ptr) { - u64 filled; Arr* xa = ptr; + u64 filled; u64 ia; Arr* xa = ptr; switch(PTY(xa)) { default: return; - case t_bitarr: filled = BITARR_SZ(PIA(xa)); break; - case t_i8arr: filled = TYARR_SZ(I8, PIA(xa)); break; - case t_i16arr: filled = TYARR_SZ(I16, PIA(xa)); break; - case t_i32arr: filled = TYARR_SZ(I32, PIA(xa)); break; - case t_f64arr: filled = TYARR_SZ(F64, PIA(xa)); break; - case t_harr: filled = fsizeof(HArr,a,B,PIA(xa)); break; - case t_fillarr: filled = fsizeof(FillArr,a,B,PIA(xa)); break; + case t_bitarr: filled = BITARR_SZ(ia=PIA(xa)); break; + case t_i8arr: filled = TYARR_SZ(I8, ia=PIA(xa)); break; + case t_i16arr: filled = TYARR_SZ(I16, ia=PIA(xa)); break; + case t_i32arr: filled = TYARR_SZ(I32, ia=PIA(xa)); break; + case t_f64arr: filled = TYARR_SZ(F64, ia=PIA(xa)); break; + case t_harr: filled = fsizeof(HArr,a,B,ia=PIA(xa)); break; + case t_fillarr: filled = fsizeof(FillArr,a,B,ia=PIA(xa)); break; } u64 end = mm_size(ptr); verifyEnd(ptr, end, 8, filled); - #define F(G, X, O, L) if ((G) != (X)) tailFail(G, X, ptr, O, L, filled, end) + #define F(G, X, O, L) if ((G) != (X)) tailFail(G, X, ptr, O, L, filled, end, ia) ITER_TAIL(F) #undef F }