Make sanitize_depth variable automatic and not passed through function args

This commit is contained in:
Behdad Esfahbod 2010-05-04 23:21:57 -04:00
parent 4a446ac351
commit 20e3dd5d29
1 changed files with 43 additions and 29 deletions

View File

@ -112,6 +112,26 @@ ASSERT_STATIC (sizeof (Type) + 1 <= sizeof (_Null##Type))
#define Null(Type) Null<Type>() #define Null(Type) Null<Type>()
/*
* Debug
*/
/* Helper object to increment debug_depth and decrement
* when returning from the object. */
template <int debug_level>
struct hb_auto_debug_depth_t {
explicit hb_auto_debug_depth_t (unsigned int *p) : p(p) { ++*p; }
~hb_auto_debug_depth_t (void) { --*p; }
private:
unsigned int *p;
};
template <> /* Optimize when debugging is disabled */
struct hb_auto_debug_depth_t<0> {
explicit hb_auto_debug_depth_t (unsigned int *p) {}
};
/* /*
* Sanitize * Sanitize
@ -121,19 +141,17 @@ ASSERT_STATIC (sizeof (Type) + 1 <= sizeof (_Null##Type))
#define HB_DEBUG_SANITIZE HB_DEBUG+0 #define HB_DEBUG_SANITIZE HB_DEBUG+0
#endif #endif
#define TRACE_SANITIZE() \ #define TRACE_SANITIZE() \
HB_STMT_START { \ hb_auto_debug_depth_t<HB_DEBUG_SANITIZE> auto_debug_depth (&context->debug_depth); \
if (HB_DEBUG_SANITIZE) \ if (HB_DEBUG_SANITIZE) \
_hb_trace ("SANITIZE", HB_FUNC, this, sanitize_depth, HB_DEBUG_SANITIZE); \ _hb_trace ("SANITIZE", HB_FUNC, this, context->debug_depth, HB_DEBUG_SANITIZE); \
} HB_STMT_END
#define SANITIZE_ARG_DEF \ #define SANITIZE_ARG_DEF \
hb_sanitize_context_t *context, \ hb_sanitize_context_t *context
unsigned int sanitize_depth HB_UNUSED
#define SANITIZE_ARG \ #define SANITIZE_ARG \
context, \ context
(HB_DEBUG_SANITIZE ? sanitize_depth + 1 : 0)
struct hb_sanitize_context_t struct hb_sanitize_context_t
{ {
@ -144,6 +162,7 @@ struct hb_sanitize_context_t
this->end = this->start + hb_blob_get_length (blob); this->end = this->start + hb_blob_get_length (blob);
this->writable = hb_blob_is_writable (blob); this->writable = hb_blob_is_writable (blob);
this->edit_count = 0; this->edit_count = 0;
this->debug_depth = 0;
if (HB_DEBUG_SANITIZE) if (HB_DEBUG_SANITIZE)
fprintf (stderr, "sanitize %p init [%p..%p] (%u bytes)\n", fprintf (stderr, "sanitize %p init [%p..%p] (%u bytes)\n",
@ -162,18 +181,16 @@ struct hb_sanitize_context_t
this->start = this->end = NULL; this->start = this->end = NULL;
} }
inline bool check (unsigned int sanitize_depth, inline bool check (const char *base, unsigned int len) const
const char *base,
unsigned int len) const
{ {
bool ret = this->start <= base && bool ret = this->start <= base &&
base <= this->end && base <= this->end &&
(unsigned int) (this->end - base) >= len; (unsigned int) (this->end - base) >= len;
if (HB_DEBUG_SANITIZE && (int) sanitize_depth < (int) HB_DEBUG_SANITIZE) \ if (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE) \
fprintf (stderr, "SANITIZE(%p) %-*d-> check [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \ fprintf (stderr, "SANITIZE(%p) %-*d-> check [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
base, base,
sanitize_depth, sanitize_depth, this->debug_depth, this->debug_depth,
base, base+len, len, base, base+len, len,
this->start, this->end, this->start, this->end,
ret ? "pass" : "FAIL"); ret ? "pass" : "FAIL");
@ -181,35 +198,30 @@ struct hb_sanitize_context_t
return ret; return ret;
} }
inline bool check_array (unsigned int sanitize_depth, inline bool check_array (const char *base, unsigned int record_size, unsigned int len) const
const char *base,
unsigned int record_size,
unsigned int len) const
{ {
bool overflows = len >= ((unsigned int) -1) / record_size; bool overflows = len >= ((unsigned int) -1) / record_size;
if (HB_DEBUG_SANITIZE && (int) sanitize_depth < (int) HB_DEBUG_SANITIZE) if (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE)
fprintf (stderr, "SANITIZE(%p) %-*d-> array [%p..%p] (%d*%d=%ld bytes) in [%p..%p] -> %s\n", \ fprintf (stderr, "SANITIZE(%p) %-*d-> array [%p..%p] (%d*%d=%ld bytes) in [%p..%p] -> %s\n", \
base, base,
sanitize_depth, sanitize_depth, this->debug_depth, this->debug_depth,
base, base + (record_size * len), record_size, len, (unsigned long) record_size * len, base, base + (record_size * len), record_size, len, (unsigned long) record_size * len,
this->start, this->end, this->start, this->end,
!overflows ? "does not overflow" : "OVERFLOWS FAIL"); !overflows ? "does not overflow" : "OVERFLOWS FAIL");
return likely (!overflows) && this->check (sanitize_depth, base, record_size * len); return likely (!overflows) && this->check (base, record_size * len);
} }
inline bool can_edit (unsigned int sanitize_depth, inline bool can_edit (const char *base HB_UNUSED, unsigned int len HB_UNUSED)
const char *base HB_UNUSED,
unsigned int len HB_UNUSED)
{ {
this->edit_count++; this->edit_count++;
if (HB_DEBUG_SANITIZE && (int) sanitize_depth < (int) HB_DEBUG_SANITIZE) if (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE)
fprintf (stderr, "SANITIZE(%p) %-*d-> edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \ fprintf (stderr, "SANITIZE(%p) %-*d-> edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
base, base,
sanitize_depth, sanitize_depth, this->debug_depth, this->debug_depth,
this->edit_count, this->edit_count,
base, base+len, len, base, base+len, len,
this->start, this->end, this->start, this->end,
@ -225,6 +237,9 @@ struct hb_sanitize_context_t
inline void reset_edit_count (void) { this->edit_count = 0; } inline void reset_edit_count (void) { this->edit_count = 0; }
public:
unsigned int debug_depth;
private: private:
const char *start, *end; const char *start, *end;
bool writable; bool writable;
@ -239,9 +254,9 @@ struct hb_sanitize_context_t
#define SANITIZE_SELF() SANITIZE_MEM(this, sizeof (*this)) #define SANITIZE_SELF() SANITIZE_MEM(this, sizeof (*this))
#define SANITIZE_MEM(B,L) likely (context->check (sanitize_depth, CharP(B), (L))) #define SANITIZE_MEM(B,L) likely (context->check (CharP(B), (L)))
#define SANITIZE_ARRAY(A,S,L) likely (context->check_array (sanitize_depth, CharP(A), S, L)) #define SANITIZE_ARRAY(A,S,L) likely (context->check_array (CharP(A), S, L))
/* Template to sanitize an object. */ /* Template to sanitize an object. */
@ -250,7 +265,6 @@ struct Sanitizer
{ {
static hb_blob_t *sanitize (hb_blob_t *blob) { static hb_blob_t *sanitize (hb_blob_t *blob) {
hb_sanitize_context_t context[1]; hb_sanitize_context_t context[1];
unsigned int sanitize_depth = 0;
bool sane; bool sane;
/* TODO is_sane() stuff */ /* TODO is_sane() stuff */
@ -464,7 +478,7 @@ struct GenericOffsetTo : OffsetType
private: private:
/* Set the offset to Null */ /* Set the offset to Null */
inline bool neuter (SANITIZE_ARG_DEF) { inline bool neuter (SANITIZE_ARG_DEF) {
if (context->can_edit (sanitize_depth, CharP(this), this->get_size ())) { if (context->can_edit (CharP(this), this->get_size ())) {
this->set (0); /* 0 is Null offset */ this->set (0); /* 0 is Null offset */
return true; return true;
} }