diff --git a/src/fdr/fdr_compile.cpp b/src/fdr/fdr_compile.cpp index 36bcda69..c4ea50f2 100644 --- a/src/fdr/fdr_compile.cpp +++ b/src/fdr/fdr_compile.cpp @@ -162,7 +162,7 @@ bytecode_ptr FDRCompiler::setupFDR() { headerSize, tabSize, confirmTmp.size(), floodControlTmp.size(), size); - auto fdr = make_bytecode_ptr(size, 64); + auto fdr = make_zeroed_bytecode_ptr(size, 64); assert(fdr); // otherwise would have thrown std::bad_alloc fdr->size = size; diff --git a/src/fdr/fdr_confirm_compile.cpp b/src/fdr/fdr_confirm_compile.cpp index 5e1a540e..d6eb6640 100644 --- a/src/fdr/fdr_confirm_compile.cpp +++ b/src/fdr/fdr_confirm_compile.cpp @@ -284,7 +284,7 @@ bytecode_ptr getFDRConfirm(const vector &lits, sizeof(LitInfo) * lits.size() + totalLitSize; size = ROUNDUP_N(size, alignof(FDRConfirm)); - auto fdrc = make_bytecode_ptr(size); + auto fdrc = make_zeroed_bytecode_ptr(size); assert(fdrc); // otherwise would have thrown std::bad_alloc fdrc->andmsk = andmsk; @@ -373,7 +373,7 @@ setupFullConfs(const vector &lits, u32 totalConfSwitchSize = nBuckets * sizeof(u32); u32 totalSize = ROUNDUP_16(totalConfSwitchSize + totalConfirmSize); - auto buf = make_bytecode_ptr(totalSize, 16); + auto buf = make_zeroed_bytecode_ptr(totalSize, 16); assert(buf); // otherwise would have thrown std::bad_alloc u32 *confBase = (u32 *)buf.get(); diff --git a/src/fdr/flood_compile.cpp b/src/fdr/flood_compile.cpp index 9b948419..7dcc17d1 100644 --- a/src/fdr/flood_compile.cpp +++ b/src/fdr/flood_compile.cpp @@ -207,7 +207,7 @@ bytecode_ptr setupFDRFloodControl(const vector &lits, size_t floodStructSize = sizeof(FDRFlood) * nDistinctFloods; size_t totalSize = ROUNDUP_16(floodHeaderSize + floodStructSize); - auto buf = make_bytecode_ptr(totalSize, 16); + auto buf = make_zeroed_bytecode_ptr(totalSize, 16); assert(buf); // otherwise would have thrown std::bad_alloc u32 *floodHeader = (u32 *)buf.get(); diff --git a/src/fdr/teddy_compile.cpp b/src/fdr/teddy_compile.cpp index 80d3316a..6f956e8c 100644 --- a/src/fdr/teddy_compile.cpp +++ b/src/fdr/teddy_compile.cpp @@ -324,7 +324,7 @@ bytecode_ptr TeddyCompiler::build() { floodControlTmp.size(), 16 * maskWidth); - auto fdr = make_bytecode_ptr(size, 64); + auto fdr = make_zeroed_bytecode_ptr(size, 64); assert(fdr); // otherwise would have thrown std::bad_alloc Teddy *teddy = (Teddy *)fdr.get(); // ugly u8 *teddy_base = (u8 *)teddy; diff --git a/src/hwlm/hwlm_build.cpp b/src/hwlm/hwlm_build.cpp index 824ac3fd..2f61ea6d 100644 --- a/src/hwlm/hwlm_build.cpp +++ b/src/hwlm/hwlm_build.cpp @@ -165,7 +165,8 @@ bytecode_ptr hwlmBuild(const vector &lits, bool make_small, throw ResourceLimitError(); } - auto h = make_bytecode_ptr(ROUNDUP_CL(sizeof(HWLM)) + engSize, 64); + const size_t hwlm_len = ROUNDUP_CL(sizeof(HWLM)) + engSize; + auto h = make_zeroed_bytecode_ptr(hwlm_len, 64); h->type = engType; memcpy(HWLM_DATA(h.get()), eng.get(), engSize); diff --git a/src/hwlm/noodle_build.cpp b/src/hwlm/noodle_build.cpp index 6412d3f5..63fdf072 100644 --- a/src/hwlm/noodle_build.cpp +++ b/src/hwlm/noodle_build.cpp @@ -74,7 +74,7 @@ bytecode_ptr noodBuildTable(const hwlmLiteral &lit) { const auto &s = lit.s; size_t noodle_len = sizeof(noodTable) + s.length(); - auto n = make_bytecode_ptr(noodle_len); + auto n = make_zeroed_bytecode_ptr(noodle_len); assert(n); size_t key_offset = findNoodFragOffset(lit); diff --git a/src/nfa/castlecompile.cpp b/src/nfa/castlecompile.cpp index 00f75159..40fbc18c 100644 --- a/src/nfa/castlecompile.cpp +++ b/src/nfa/castlecompile.cpp @@ -579,7 +579,7 @@ buildCastle(const CastleProto &proto, total_size = ROUNDUP_N(total_size, alignof(mmbit_sparse_iter)); total_size += byte_length(stale_iter); // stale sparse iter - auto nfa = make_bytecode_ptr(total_size); + auto nfa = make_zeroed_bytecode_ptr(total_size); nfa->type = verify_u8(CASTLE_NFA); nfa->length = verify_u32(total_size); nfa->nPositions = verify_u32(subs.size()); diff --git a/src/nfa/goughcompile.cpp b/src/nfa/goughcompile.cpp index 62360561..d92f285f 100644 --- a/src/nfa/goughcompile.cpp +++ b/src/nfa/goughcompile.cpp @@ -1116,7 +1116,7 @@ bytecode_ptr goughCompile(raw_som_dfa &raw, u8 somPrecision, gi.stream_som_loc_width = somPrecision; u32 gough_size = ROUNDUP_N(curr_offset, 16); - auto gough_dfa = make_bytecode_ptr(gough_size); + auto gough_dfa = make_zeroed_bytecode_ptr(gough_size); memcpy(gough_dfa.get(), basic_dfa.get(), basic_dfa->length); memcpy((char *)gough_dfa.get() + haig_offset, &gi, sizeof(gi)); diff --git a/src/nfa/limex_compile.cpp b/src/nfa/limex_compile.cpp index 235d7168..3cdf5de1 100644 --- a/src/nfa/limex_compile.cpp +++ b/src/nfa/limex_compile.cpp @@ -1797,7 +1797,7 @@ struct Factory { u32 tableOffset, tugMaskOffset; size_t len = repeatAllocSize(br, &tableOffset, &tugMaskOffset); - auto info = make_bytecode_ptr(len); + auto info = make_zeroed_bytecode_ptr(len); char *info_ptr = (char *)info.get(); // Collect state space info. @@ -2297,7 +2297,7 @@ struct Factory { size_t nfaSize = sizeof(NFA) + offset; DEBUG_PRINTF("nfa size %zu\n", nfaSize); - auto nfa = make_bytecode_ptr(nfaSize); + auto nfa = make_zeroed_bytecode_ptr(nfaSize); assert(nfa); // otherwise we would have thrown std::bad_alloc implNFA_t *limex = (implNFA_t *)getMutableImplNfa(nfa.get()); diff --git a/src/nfa/mcclellancompile.cpp b/src/nfa/mcclellancompile.cpp index e07d7a2a..206f468a 100644 --- a/src/nfa/mcclellancompile.cpp +++ b/src/nfa/mcclellancompile.cpp @@ -496,7 +496,7 @@ bytecode_ptr mcclellanCompile16(dfa_info &info, const CompileContext &cc, accel_offset -= sizeof(NFA); /* adj accel offset to be relative to m */ assert(ISALIGNED_N(accel_offset, alignof(union AccelAux))); - auto nfa = make_bytecode_ptr(total_size); + auto nfa = make_zeroed_bytecode_ptr(total_size); char *nfa_base = (char *)nfa.get(); populateBasicInfo(sizeof(u16), info, total_size, aux_offset, accel_offset, @@ -715,7 +715,7 @@ bytecode_ptr mcclellanCompile8(dfa_info &info, const CompileContext &cc, accel_offset -= sizeof(NFA); /* adj accel offset to be relative to m */ assert(ISALIGNED_N(accel_offset, alignof(union AccelAux))); - auto nfa = bytecode_ptr(total_size); + auto nfa = make_zeroed_bytecode_ptr(total_size); char *nfa_base = (char *)nfa.get(); mcclellan *m = (mcclellan *)getMutableImplNfa(nfa.get()); diff --git a/src/nfa/mcsheng_compile.cpp b/src/nfa/mcsheng_compile.cpp index c7133d08..e4e4173a 100644 --- a/src/nfa/mcsheng_compile.cpp +++ b/src/nfa/mcsheng_compile.cpp @@ -872,7 +872,7 @@ bytecode_ptr mcshengCompile16(dfa_info &info, dstate_id_t sheng_end, accel_offset -= sizeof(NFA); /* adj accel offset to be relative to m */ assert(ISALIGNED_N(accel_offset, alignof(union AccelAux))); - auto nfa = make_bytecode_ptr(total_size); + auto nfa = make_zeroed_bytecode_ptr(total_size); mcsheng *m = (mcsheng *)getMutableImplNfa(nfa.get()); populateBasicInfo(sizeof(u16), info, total_size, aux_offset, accel_offset, @@ -998,7 +998,7 @@ bytecode_ptr mcshengCompile8(dfa_info &info, dstate_id_t sheng_end, accel_offset -= sizeof(NFA); /* adj accel offset to be relative to m */ assert(ISALIGNED_N(accel_offset, alignof(union AccelAux))); - auto nfa = make_bytecode_ptr(total_size); + auto nfa = make_zeroed_bytecode_ptr(total_size); mcsheng *m = (mcsheng *)getMutableImplNfa(nfa.get()); allocateImplId8(info, sheng_end, accel_escape_info, &m->accel_limit_8, diff --git a/src/nfa/mpvcompile.cpp b/src/nfa/mpvcompile.cpp index 51787512..8497c648 100644 --- a/src/nfa/mpvcompile.cpp +++ b/src/nfa/mpvcompile.cpp @@ -343,7 +343,7 @@ bytecode_ptr mpvCompile(const vector &puffs_in, DEBUG_PRINTF("%u puffs, len = %u\n", puffette_count, len); - auto nfa = make_bytecode_ptr(len); + auto nfa = make_zeroed_bytecode_ptr(len); mpv_puffette *pa_base = (mpv_puffette *) ((char *)nfa.get() + sizeof(NFA) + sizeof(mpv) diff --git a/src/nfa/shengcompile.cpp b/src/nfa/shengcompile.cpp index b2996aa5..8c061913 100644 --- a/src/nfa/shengcompile.cpp +++ b/src/nfa/shengcompile.cpp @@ -507,7 +507,7 @@ bytecode_ptr shengCompile(raw_dfa &raw, const CompileContext &cc, DEBUG_PRINTF("NFA: %u, aux: %u, reports: %u, accel: %u, total: %u\n", nfa_size, total_aux, total_reports, total_accel, total_size); - auto nfa = make_bytecode_ptr(total_size); + auto nfa = make_zeroed_bytecode_ptr(total_size); populateBasicInfo(nfa.get(), info, accelInfo, nfa_size, reports_offset, accel_offset, total_size, total_size - sizeof(NFA)); diff --git a/src/nfa/tamaramacompile.cpp b/src/nfa/tamaramacompile.cpp index 6d253411..1a6e8bef 100644 --- a/src/nfa/tamaramacompile.cpp +++ b/src/nfa/tamaramacompile.cpp @@ -134,7 +134,7 @@ buildTamarama(const TamaInfo &tamaInfo, const u32 queue, // use subSize as a sentinel value for no active subengines, // so add one to subSize here u32 activeIdxSize = calcPackedBytes(subSize + 1); - auto nfa = make_bytecode_ptr(total_size); + auto nfa = make_zeroed_bytecode_ptr(total_size); nfa->type = verify_u8(TAMARAMA_NFA); nfa->length = verify_u32(total_size); nfa->queueIndex = queue; diff --git a/src/nfagraph/ng_lbr.cpp b/src/nfagraph/ng_lbr.cpp index 11262ae1..9bf16efe 100644 --- a/src/nfagraph/ng_lbr.cpp +++ b/src/nfagraph/ng_lbr.cpp @@ -137,7 +137,7 @@ bytecode_ptr makeLbrNfa(NFAEngineType nfa_type, enum RepeatType rtype, } size_t len = sizeof(NFA) + sizeof(LbrStruct) + sizeof(RepeatInfo) + tableLen + sizeof(u64a); - auto nfa = make_bytecode_ptr(len); + auto nfa = make_zeroed_bytecode_ptr(len); nfa->type = verify_u8(nfa_type); nfa->length = verify_u32(len); return nfa; diff --git a/src/rose/rose_build_anchored.cpp b/src/rose/rose_build_anchored.cpp index 12b62128..6d56ee00 100644 --- a/src/rose/rose_build_anchored.cpp +++ b/src/rose/rose_build_anchored.cpp @@ -891,7 +891,8 @@ buildAnchoredMatcher(RoseBuildImpl &build, const vector &fragments, throw ResourceLimitError(); } - auto atable = make_bytecode_ptr(total_size, 64); + auto atable = + make_zeroed_bytecode_ptr(total_size, 64); char *curr = (char *)atable.get(); u32 state_offset = 0; diff --git a/src/rose/rose_build_bytecode.cpp b/src/rose/rose_build_bytecode.cpp index b48a80c2..baf13446 100644 --- a/src/rose/rose_build_bytecode.cpp +++ b/src/rose/rose_build_bytecode.cpp @@ -5612,7 +5612,7 @@ bytecode_ptr addSmallWriteEngine(const RoseBuildImpl &build, const size_t smwrOffset = ROUNDUP_CL(mainSize); const size_t newSize = smwrOffset + smallWriteSize; - auto rose2 = make_bytecode_ptr(newSize, 64); + auto rose2 = make_zeroed_bytecode_ptr(newSize, 64); char *ptr = (char *)rose2.get(); memcpy(ptr, rose.get(), mainSize); memcpy(ptr + smwrOffset, smwr_engine.get(), smallWriteSize); @@ -5958,7 +5958,7 @@ bytecode_ptr RoseBuildImpl::buildFinalEngine(u32 minWidth) { proto.size = currOffset; // Time to allocate the real RoseEngine structure, at cacheline alignment. - auto engine = make_bytecode_ptr(currOffset, 64); + auto engine = make_zeroed_bytecode_ptr(currOffset, 64); assert(engine); // will have thrown bad_alloc otherwise. // Copy in our prototype engine data. diff --git a/src/smallwrite/smallwrite_build.cpp b/src/smallwrite/smallwrite_build.cpp index dc2a4466..d5c1ccb7 100644 --- a/src/smallwrite/smallwrite_build.cpp +++ b/src/smallwrite/smallwrite_build.cpp @@ -504,7 +504,7 @@ bytecode_ptr SmallWriteBuildImpl::build(u32 roseQuality) { } u32 size = sizeof(SmallWriteEngine) + nfa->length; - auto smwr = make_bytecode_ptr(size); + auto smwr = make_zeroed_bytecode_ptr(size); smwr->size = size; smwr->start_offset = start_offset; diff --git a/src/util/bytecode_ptr.h b/src/util/bytecode_ptr.h index 713649ab..d3f5215a 100644 --- a/src/util/bytecode_ptr.h +++ b/src/util/bytecode_ptr.h @@ -63,7 +63,6 @@ public: if (!ptr) { throw std::bad_alloc(); } - std::memset(ptr.get(), 0, bytes); } bytecode_ptr(std::nullptr_t) {} @@ -122,12 +121,27 @@ private: size_t alignment = 0; //!< Alignment of memory region in bytes. }; +/** + * \brief Constructs a bytecode_ptr with the given size and alignment. + */ template inline bytecode_ptr make_bytecode_ptr(size_t size, size_t align = alignof(T)) { return bytecode_ptr(size, align); } +/** + * \brief Constructs a bytecode_ptr with the given size and alignment and + * fills the memory region with zeroes. + */ +template +inline bytecode_ptr make_zeroed_bytecode_ptr(size_t size, + size_t align = alignof(T)) { + auto ptr = make_bytecode_ptr(size, align); + std::memset(ptr.get(), 0, size); + return ptr; +} + } // namespace ue2 #endif // UTIL_BYTECODE_PTR_H