From 2dc91900558ac004455485e0134ccab8f9de31b3 Mon Sep 17 00:00:00 2001 From: Megvii Engine Team Date: Wed, 29 Jul 2020 19:12:05 +0800 Subject: [PATCH] fix(fastrun/persistent_cache): fix fastrun crash GitOrigin-RevId: b3f7bdf7dc1942a9b012f6b1575cb0a4759cb1b9 --- src/core/impl/utils/persistent_cache.cpp | 6 +++--- src/core/include/megbrain/utils/persistent_cache.h | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/core/impl/utils/persistent_cache.cpp b/src/core/impl/utils/persistent_cache.cpp index 75996a24..ec6651e8 100644 --- a/src/core/impl/utils/persistent_cache.cpp +++ b/src/core/impl/utils/persistent_cache.cpp @@ -176,11 +176,9 @@ AlgoChooserProfileCache::get(const Key &key) { auto entry_len = read_uint32(); mgb_assert(buf + entry_len <= buf_end); - int rep; auto nr = sscanf(reinterpret_cast(buf), ENTRY_FMT, - &rep, &i.time, &i.workspace); + &i.reproducible, &i.time, &i.workspace); mgb_assert(nr == 3); - i.reproducible = rep; buf += entry_len; } mgb_assert(buf == buf_end); @@ -228,6 +226,8 @@ void AlgoChooserProfileCache::put(const Key &key, Result &result) { val.resize(pos + SPR_SIZE); uint32_t nr = snprintf(&val[pos], SPR_SIZE, ENTRY_FMT, i.reproducible, i.time, i.workspace); + //! for memory boundary failed, snprintf ret do not contain \0 + nr += 1; mgb_assert(nr < SPR_SIZE); memcpy(&val[pos - sizeof(uint32_t)], &nr, sizeof(nr)); val.resize(pos + nr); diff --git a/src/core/include/megbrain/utils/persistent_cache.h b/src/core/include/megbrain/utils/persistent_cache.h index bd6ce5a2..5ad1f80b 100644 --- a/src/core/include/megbrain/utils/persistent_cache.h +++ b/src/core/include/megbrain/utils/persistent_cache.h @@ -100,7 +100,8 @@ namespace mgb { struct ResultEntry { std::string algo; //! identifier of the algorithm - bool reproducible; //! whether algorithm is reproducible + //! sscanf will up bool as int + int reproducible; //! whether algorithm is reproducible double time; //! execution time in seconds size_t workspace; //! workspace in bytes };