more usz overflow checking

This commit is contained in:
dzaima 2024-08-23 03:59:51 +03:00
parent 06689960a5
commit 09bdeb7fd8
7 changed files with 21 additions and 18 deletions

View File

@ -73,7 +73,7 @@ extern u8 const arrTypeBitsLog[];
#define arrTypeBitsLog(X) arrTypeBitsLog[X] #define arrTypeBitsLog(X) arrTypeBitsLog[X]
#define arrNewType(X) el2t(TIi(X,elType)) #define arrNewType(X) el2t(TIi(X,elType))
SHOULD_INLINE void arr_check_size(u64 sz, u8 type, usz ia) { SHOULD_INLINE void arr_check_size(u64 sz, u8 type, u64 ia) {
#if DEBUG #if DEBUG
assert(IS_ANY_ARR(type) || type==t_harrPartial); assert(IS_ANY_ARR(type) || type==t_harrPartial);
if (!IS_SLICE(type)) { if (!IS_SLICE(type)) {

View File

@ -69,7 +69,7 @@ static inline u64 bit_find(u64* arr, u64 ia, bool v) {
#endif #endif
#define BITARR_SZ(IA) fsizeof(BitArr, a, u64, BIT_N(IA)) #define BITARR_SZ(IA) fsizeof(BitArr, a, u64, BIT_N(IA))
static B m_bitarrv(u64** p, usz ia) { static B m_bitarrv(u64** p, u64 ia) {
CHECK_BITARR_IA(ia); CHECK_BITARR_IA(ia);
BitArr* r = m_arr(BITARR_SZ(ia), t_bitarr, ia); BitArr* r = m_arr(BITARR_SZ(ia), t_bitarr, ia);
arr_shVec((Arr*)r); arr_shVec((Arr*)r);
@ -83,7 +83,7 @@ static B m_bitarrc(u64** p, B x) { assert(isArr(x));
arr_shCopy((Arr*)r, x); arr_shCopy((Arr*)r, x);
return taga(r); return taga(r);
} }
static Arr* m_bitarrp(u64** p, usz ia) { static Arr* m_bitarrp(u64** p, u64 ia) {
CHECK_BITARR_IA(ia); CHECK_BITARR_IA(ia);
BitArr* r = m_arr(BITARR_SZ(ia), t_bitarr, ia); BitArr* r = m_arr(BITARR_SZ(ia), t_bitarr, ia);
*p = (u64*)r->a; *p = (u64*)r->a;

View File

@ -69,13 +69,13 @@ extern INIT_GLOBAL M_FillF fillFns[el_MAX];
static void tyarrv_freeP(Arr* x) { assert(PRNK(x)<=1 && IS_DIRECT_TYARR(PTY(x))); mm_free((Value*)x); } static void tyarrv_freeP(Arr* x) { assert(PRNK(x)<=1 && IS_DIRECT_TYARR(PTY(x))); mm_free((Value*)x); }
static void tyarrv_free(B x) { tyarrv_freeP(a(x)); } static void tyarrv_free(B x) { tyarrv_freeP(a(x)); }
static inline void* m_arrUnchecked(u64 sz, u8 type, usz ia) { static inline void* m_arrUnchecked(u64 sz, u8 type, u64 ia) {
Arr* r = mm_alloc(sz, type); Arr* r = mm_alloc(sz, type);
r->ia = ia; r->ia = ia;
return r; return r;
} }
SHOULD_INLINE void arr_check_size(u64 sz, u8 type, usz ia); SHOULD_INLINE void arr_check_size(u64 sz, u8 type, u64 ia);
SHOULD_INLINE void* m_arr(u64 sz, u8 type, usz ia) { SHOULD_INLINE void* m_arr(u64 sz, u8 type, u64 ia) {
arr_check_size(sz, type, ia); arr_check_size(sz, type, ia);
return m_arrUnchecked(sz, type, ia); return m_arrUnchecked(sz, type, ia);
} }

View File

@ -4,7 +4,7 @@
typedef TyArr JOIN(TU,Arr); typedef TyArr JOIN(TU,Arr);
typedef TP(,) JOIN(TU,Atom); typedef TP(,) JOIN(TU,Atom);
SHOULD_INLINE B TP(m_,arrv) (TEl** p, usz ia) { SHOULD_INLINE B TP(m_,arrv) (TEl** p, u64 ia) {
CHECK_IA(ia, sizeof(TEl)); CHECK_IA(ia, sizeof(TEl));
TyArr* r = m_arr(TYARR_SZ2(TU,ia), T_ARR, ia); TyArr* r = m_arr(TYARR_SZ2(TU,ia), T_ARR, ia);
arr_shVec((Arr*)r); arr_shVec((Arr*)r);
@ -19,7 +19,7 @@ SHOULD_INLINE B TP(m_,arrc) (TEl** p, B x) { assert(isArr(x));
arr_shCopy((Arr*)r, x); arr_shCopy((Arr*)r, x);
return taga(r); return taga(r);
} }
SHOULD_INLINE Arr* TP(m_,arrp) (TEl** p, usz ia) { SHOULD_INLINE Arr* TP(m_,arrp) (TEl** p, u64 ia) {
CHECK_IA(ia, sizeof(TEl)); CHECK_IA(ia, sizeof(TEl));
TyArr* r = m_arr(TYARR_SZ2(TU,ia), T_ARR, ia); TyArr* r = m_arr(TYARR_SZ2(TU,ia), T_ARR, ia);
*p = (TEl*)r->a; *p = (TEl*)r->a;

View File

@ -2,10 +2,10 @@
#include "mut.h" #include "mut.h"
typedef struct { Arr* arr; void* els; } MadeArr; typedef struct { Arr* arr; void* els; } MadeArr;
FORCE_INLINE MadeArr mut_make_arr(usz ia, u8 type, u8 el) { FORCE_INLINE MadeArr mut_make_arr(u64 ia, u8 type, u8 el) {
u64 sz; u64 sz;
switch(el) { default: UD; switch(el) { default: UD;
case el_bit: sz = BITARR_SZ( ia); break; case el_bit: sz = BITARR_SZ( ia); CHECK_BITARR_IA(ia); break;
case el_i8: case el_c8: sz = TYARR_SZ(I8, ia); break; case el_i8: case el_c8: sz = TYARR_SZ(I8, ia); break;
case el_i16: case el_c16: sz = TYARR_SZ(I16,ia); break; case el_i16: case el_c16: sz = TYARR_SZ(I16,ia); break;
case el_i32: case el_c32: sz = TYARR_SZ(I32,ia); break; case el_i32: case el_c32: sz = TYARR_SZ(I32,ia); break;
@ -26,13 +26,13 @@ FORCE_INLINE void mut_init(Mut* m, u8 el) {
} }
#if __clang__ #if __clang__
NOINLINE void make_mut_init(Mut* rp, ux ia, u8 el) { NOINLINE void make_mut_init(Mut* rp, u64 ia, u8 el) {
MAKE_MUT(r, ia) MAKE_MUT(r, ia)
mut_init(r, el); mut_init(r, el);
*rp = r_val; *rp = r_val;
} }
#else #else
NOINLINE Mut make_mut_init(ux ia, u8 el) { NOINLINE Mut make_mut_init(u64 ia, u8 el) {
MAKE_MUT(r, ia) MAKE_MUT(r, ia)
mut_init(r, el); mut_init(r, el);
return r_val; return r_val;
@ -653,7 +653,7 @@ NOINLINE Arr* apd_fill_end(ApdMut* m, u32 ty) {
return a(withFill(taga(m->obj), m->fill)); return a(withFill(taga(m->obj), m->fill));
} }
SHOULD_INLINE Arr* apd_setArr(ApdMut* m, usz ia, u8 xe) { SHOULD_INLINE Arr* apd_setArr(ApdMut* m, u64 ia, u8 xe) {
MadeArr a = mut_make_arr(ia, el2t(xe), xe); MadeArr a = mut_make_arr(ia, el2t(xe), xe);
m->obj = a.arr; m->obj = a.arr;
m->a = a.els; m->a = a.els;

View File

@ -29,17 +29,17 @@ extern INIT_GLOBAL MutFns mutFns[el_MAX+1];
struct Mut { struct Mut {
MutFns* fns; MutFns* fns;
usz ia; u64 ia;
Arr* val; Arr* val;
void* a; void* a;
}; };
void mut_to(Mut* m, u8 n); void mut_to(Mut* m, u8 n);
#if __clang__ && __has_attribute(noescape) // workaround for clang not realizing that stack-returned structs aren't captured #if __clang__ && __has_attribute(noescape) // workaround for clang not realizing that stack-returned structs aren't captured
void make_mut_init(__attribute__((noescape)) Mut* rp, ux ia, u8 el); void make_mut_init(__attribute__((noescape)) Mut* rp, u64 ia, u8 el);
#define MAKE_MUT_INIT(N, IA, EL) Mut N##_val; Mut* N = &N##_val; make_mut_init(N, IA, EL); #define MAKE_MUT_INIT(N, IA, EL) Mut N##_val; Mut* N = &N##_val; make_mut_init(N, IA, EL);
#else #else
Mut make_mut_init(ux ia, u8 el); Mut make_mut_init(u64 ia, u8 el);
#define MAKE_MUT_INIT(N, IA, EL) Mut N##_val = make_mut_init(IA, EL); Mut* N = &N##_val; #define MAKE_MUT_INIT(N, IA, EL) Mut N##_val = make_mut_init(IA, EL); Mut* N = &N##_val;
#endif #endif
#define MAKE_MUT(N, IA) Mut N##_val; N##_val.fns = &mutFns[el_MAX]; N##_val.ia = (IA); Mut* N = &N##_val; #define MAKE_MUT(N, IA) Mut N##_val; N##_val.fns = &mutFns[el_MAX]; N##_val.ia = (IA); Mut* N = &N##_val;
@ -165,7 +165,7 @@ FORCE_INLINE B arr_join_inline(B w, B x, bool consume, bool* usedW) {
assert(isArr(w) && isArr(x)); assert(isArr(w) && isArr(x));
usz wia = IA(w); usz wia = IA(w);
usz xia = IA(x); usz xia = IA(x);
u64 ria = wia+xia; u64 ria = wia + (u64)xia;
if (!reusable(w)) goto no; if (!reusable(w)) goto no;
u64 wsz = mm_sizeUsable(v(w)); u64 wsz = mm_sizeUsable(v(w));
u8 wt = TY(w); u8 wt = TY(w);
@ -259,7 +259,7 @@ struct ApdMut {
}; };
union { union {
struct { ux ia0; }; // tot init struct { u64 ia0; }; // tot init
struct { usz* rsh0; ur rr0; }; // sh init struct { usz* rsh0; ur rr0; }; // sh init
struct { usz* csh; usz cia; ur cr; }; // sh2 & shE (& cia also for sh1) struct { usz* csh; usz cia; ur cr; }; // sh2 & shE (& cia also for sh1)
}; };

View File

@ -1,3 +1,6 @@
!"Out of memory" % a(220) 1e9 {𝕊: a}˘ a !"Out of memory" % a(220) 1e9 {𝕊: a}˘ a
!"⥊: 𝕨 too large" % a(220) 1e9 a˘ a !"⥊: 𝕨 too large" % a(220) 1e9 a˘ a
!"Out of memory" % (225)/(210)10 !"Out of memory" % (225)/(210)10
!"Out of memory" % k16384 nk÷˜232 (nk) / n1
!"Out of memory" % k64 nk÷˜232 (nk) / n1 # %SLOW
!"Out of memory" % ˜31 2 # %SLOW