Repository navigation
fix: close issue #249 — Emscripten OOM in CKKS unit tests - #1215
Merged
Merged
Conversation
pascoec
force-pushed
the
249-fix-emscripten-oom
branch
from
July 8, 2026 20:07
c1e92af to
92b3837
Compare
Collaborator
|
-Rebased and addressed conflicts introduced by #1213 Did not verify the Emscripten changes. Will wait for @dsuponitskiy to review and approve/deny the proposed ci changes. |
pascoec
requested review from
dsuponitskiy and
pascoec
and removed request for
pascoec
July 8, 2026 20:30
dsuponitskiy
force-pushed
the
249-fix-emscripten-oom
branch
from
September 4, 2026 04:17
3bcacde to
2c43d08
Compare
pascoec
approved these changes
Sep 8, 2026
PR #1179 added ClearBootstrapPrecom and ClearSchemeSwitchPrecom helpers but did not wire them into ReleaseAllContexts. This change completes that work, then re-enables the EVAL_FAST_ROTATION and CONTEXT_WITH_SERTYPE tests that PR #985 had to guard under !defined(__EMSCRIPTEN__) for the original std::bad_alloc failure. Library changes (no hot-path touched): - ReleaseAllContexts now walks every live context and calls ClearAllCKKSCaches before clearing the static maps. - Adds malloc_zone_pressure_relief(Apple) and _heapmin(MSVC) to AllocTrim. - Add ClearAllCKKSCaches convenience on CryptoContextImpl. - ClearBootstrapPrecom and ClearSchemeSwitchPrecom bodies made noexcept-safe so the walk does not throw on contexts that have not Enable(FHE)'d or Enable(SCHEMESWITCH)'d. Test changes: - Mid-test ClearAllCKKSCaches between JSON and BINARY rounds in UnitTestCKKSrnsSerialize. - UTCKKSCacheClear extends its WITH_TCM skip to __EMSCRIPTEN__ (the heap-probe helper has no emmalloc backend). - Remove the !defined(__EMSCRIPTEN__) guards around the affected rows. CI: - Add .github/workflows/emscripten.yml: builds with emsdk 3.1.59 and runs pke_tests/core_tests under node in two slices. Validated on macOS + WASM: - Native: pke_tests 1892/1892 pass, core_tests 158/158 pass. - WASM: CKKS slice 939/941 pass (2 heap-probe tests skipped), BFV+BGV+binfhe slice 324/324 pass, peak 1.6 GB. Closes #249.
Make the process-wide allocator tuning introduced in #1213 opt-out via a new build flag, since it changes malloc policy for the whole host application (freed memory is retained in-process, raising resident set size for workloads with large transient peaks). - Gate the glibc mallopt(M_MMAP_MAX=0, M_TRIM_THRESHOLD=-1) startup tuning behind WITH_MALLOC_TUNING and propagate the flag through config_core.h; build with -DWITH_MALLOC_TUNING=OFF to disable. - Document per-platform behavior in explicit branches: macOS libmalloc already retains freed memory (madvise-reusable pages plus the large cache), and the Windows process heap exposes no retention knob. - Rewrite the __APPLE__ branch of AllocTrim() to use the supported malloc_zone_pressure_relief(NULL, 0) all-zones call instead of the malloc_get_all_zones()/mach introspection API, dropping <mach/mach.h>. - Docs: add the new flag and the previously undocumented options (GIT_SUBMOD_AUTO, WITH_COVTEST, WITH_NOISE_DEBUG, WITH_REDUCED_NOISE, USE_MACPORTS) to the CMake flags table, and fix stale defaults (WITH_BE2 is OFF; the default math backend is 4, not 2).
The scheme validators added to Clear{Bootstrap,SchemeSwitch}Precom throw, but
those functions are noexcept and ReleaseAllContexts() now calls them for every
registered context, so any BFV or BGV context -- or any CKKS context that never
enabled SCHEMESWITCH -- terminated the process during teardown.
Split the two concerns instead. The CryptoContextImpl entry points keep
VerifyCKKSScheme() and drop noexcept: calling them on a non-CKKS context is a
user error and should report as one. SchemeBase goes back to null-guarded
forwarders, matching FHEBase's noexcept no-op defaults -- a scheme that never
enabled the feature has nothing cached, which is not an error. ClearAllCKKSCaches()
stays noexcept for the ReleaseAllContexts() sweep and tests the same isCKKS()
predicate directly rather than reaching it through a throwing wrapper.
exhaustion tracked by #249 and no longer reproduce: drop them from the InteractiveBootstrap and BGVrns automorphism suites, from SCHEME_SWITCH_ARGMIN 01-02 and SCHEME_SWITCH_SERIALIZE 05-06, and from the 24 EVALSQUARE rows in UnitTestSHE. The two guards in UnitTestTrapdoor stay: they document a wasm correctness problem and also cover Cygwin. UTCKKSRNSCSvsFA aborted the wasm run outright, which is what kept the rest of the suite from being exercised at all. Each of its tests takes two measurements, and the factory's static context vector plus the static eval-key maps held the first bootstrapping context and its key set alive while the second was built. Releasing each measurement's own context before returning drops the peak of FullPackingBootstrap_HybridKeySwitch from 5.55 to 3.82 GB (native, single threaded). ReleaseAllContexts() clears both static key maps and trims on its own, so the fixture's explicit ClearEval*Keys() calls were redundant. That one case still needs ~4.0 GB live at ring 2^14 with composite degree 2, which fits the 4 GB wasm32 address space with about 1.5% to spare, so it is skipped under Emscripten with the measurement recorded; the other seven cases stay under 0.5 GB. AllocTrim() also gains a note on why Emscripten gets no implementation: wasm linear memory never shrinks, dlmalloc's malloc_trim() is a no-op there, and emmalloc's strands the freed region and doubled peak heap in a free-then-reallocate test.
dsuponitskiy
force-pushed
the
249-fix-emscripten-oom
branch
from
September 9, 2026 02:46
4fc9c96 to
7e93f49
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #249. Resubmission of #1208 / #1214 from an org branch as requested.
PR #1179 added the cache-clear helpers but didn't wire them into
ReleaseAllContexts. This wires the walk in, makes the per-context clears noexcept-safe, and re-enables the 16 rows previously guarded under!__EMSCRIPTEN__(EVAL_FAST_ROTATION_41–48,CONTEXT_WITH_SERTYPE_01–08).UTCKKSCacheClearextends its existingWITH_TCMskip to__EMSCRIPTEN__— its heap probes rely onmallinfo2. Newemscripten.ymlruns emsdk 3.1.59 + node slices so this can't go silent again.No hot-path file is touched.
Validated locally
pke_tests1892/1892,core_tests158/158NATIVE_SIZE=64): the 16 re-enabled rows pass; CKKS slice 939/941 (2 heap-probe skips), BFV+BGV+binfhe 324/324; peak ~1.6 GB / 4 GB cap