From fb02f4a8975655b405ac4bc3f9982383f33ea6dc Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 8 Jul 2023 12:29:18 -0600 Subject: [PATCH] [sanitize] Micro-optimize ChainRule::sanitize --- src/hb-ot-layout-gsubgpos.hh | 7 ++++--- src/hb-sanitize.hh | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/hb-ot-layout-gsubgpos.hh b/src/hb-ot-layout-gsubgpos.hh index 90010e5a8..d37c47841 100644 --- a/src/hb-ot-layout-gsubgpos.hh +++ b/src/hb-ot-layout-gsubgpos.hh @@ -3086,11 +3086,12 @@ struct ChainRule bool sanitize (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); - if (!backtrack.len.sanitize (c)) return_trace (false); + /* Hyper-optimized sanitized because this is really hot. */ + if (!c->check_point (&StructAfter (backtrack.len))) return_trace (false); const auto &input = StructAfter (backtrack); - if (!input.lenP1.sanitize (c)) return_trace (false); + if (!c->check_point (&StructAfter (input.lenP1))) return_trace (false); const auto &lookahead = StructAfter (input); - if (!lookahead.len.sanitize (c)) return_trace (false); + if (!c->check_point (&StructAfter (lookahead.len))) return_trace (false); const auto &lookup = StructAfter (lookahead); return_trace (lookup.sanitize (c)); } diff --git a/src/hb-sanitize.hh b/src/hb-sanitize.hh index d10ebdf74..42945996e 100644 --- a/src/hb-sanitize.hh +++ b/src/hb-sanitize.hh @@ -301,6 +301,24 @@ struct hb_sanitize_context_t : return likely (ok); } +#ifndef HB_OPTIMIZE_SIZE + HB_ALWAYS_INLINE +#endif + bool check_point (const void *base) const + { + const char *p = (const char *) base; + bool ok = (uintptr_t) (p - this->start) <= this->length; + + DEBUG_MSG_LEVEL (SANITIZE, p, this->debug_depth+1, 0, + "check_point [%p]" + " in [%p..%p] -> %s", + p, + this->start, this->end, + ok ? "OK" : "OUT-OF-RANGE"); + + return likely (ok); + } + template bool check_range (const T *base, unsigned int a,