From a0cdc65ae22aa4c58b321543cc265adac00afb3e Mon Sep 17 00:00:00 2001 From: Chris Speciale <26172437+dimensionscape@users.noreply.github.com> Date: Tue, 1 Dec 2026 15:02:40 -0500 Subject: [PATCH] Bound the text the fuzz comparison builds for a value The scheduled fuzz run of 2026-11-30 stopped in the serializer target with libFuzzer's out-of-memory error, 4185 MB against its 4096 MB limit. Loading the 3,881 cached inputs had already taken it from 115 MB to 3472 MB at 9 inputs a second. ASan counted 88 MB live, and the large stacks were all canon() concatenating strings. The input it stopped on, "Moy0:r0gr0h...", is an ObjectMap whose key, an object, and value both hold the map. canon() stops after 100,000 steps, but in such a map the text also grows about 1.6 times a level, and each level copies the texts below it. That input built 20 million characters of text before it ran out of steps; with a 3,000 character string beside the key's reference, 720 million, which took 850 MB and 8 seconds under ASan, and mutants of it up to 1.3 GB and 23 seconds. None of them was compared in the end, since each ran out of steps. canon() now also gives up after 4,194,304 characters, counting each level's text as it is built, so it allocates at most a few times that for a value. Without references or runs of nulls, 4096 bytes of input build under a third of it. A value past it is not compared, as one past the step limit already was; 99,000 nulls 32 levels down, which builds 16 million characters, was compared before and is not now. Tests: the input is in test/fuzz/regressions/serializer. With clang 18, ASan and UBSan in WSL, every target replays its seeds and regressions, and the inputs above take 167 to 189 MB and under 0.3 seconds each. Loading 3,392 serializer inputs that local runs gathered took main 620 seconds and 1.4 GB before a 20-second timeout, and this change 158 seconds and 750 MB. From there a 30-minute run of 78,263 inputs stayed at 760 MB throughout. --- test/fuzz/Fuzz.hx | 28 ++++++++++++++++-- .../map-holds-itself-in-key-and-value | Bin 0 -> 32 bytes 2 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 test/fuzz/regressions/serializer/map-holds-itself-in-key-and-value diff --git a/test/fuzz/Fuzz.hx b/test/fuzz/Fuzz.hx index ba67c1ef..fabda1f8 100644 --- a/test/fuzz/Fuzz.hx +++ b/test/fuzz/Fuzz.hx @@ -569,15 +569,20 @@ class Fuzz { /** Whether two values have different texts from canon(). A value that - takes more than CANON_WORK steps is not compared: the serializer's - references can share a value at every level, as "ay1:aoy0:r0gr0h" - does, and the depth cut-off alone leaves 2^32 steps. + takes more than CANON_WORK steps or CANON_TEXT characters is not + compared: the serializer's references can share a value at every + level, as "ay1:aoy0:r0gr0h" does, and the depth cut-off alone leaves + 2^32 steps. In a map whose key and value both hold the map, as + "Moy0:r0gr0h", the text also grows at each level, so 100,000 steps + could build hundreds of millions of characters. **/ static function differ(a:Dynamic, b:Dynamic):Bool { try { canonLeft = CANON_WORK; + canonTextLeft = CANON_TEXT; var ta = canon(a); canonLeft = CANON_WORK; + canonTextLeft = CANON_TEXT; return ta != canon(b); } catch (e:CanonTooBig) { return false; @@ -587,6 +592,16 @@ class Fuzz { static inline var CANON_WORK = 100000; static var canonLeft = 0; + /** + The characters of text canon() builds for a value at most. Each level + copies the texts below it into its own, so a text counts once for + each level that holds it, as it is allocated. Without references or + runs of nulls, 4096 bytes of input stay under a third of this: a byte + gives at most ten characters ("m" gives "-Infinity,"), at 33 levels. + **/ + static inline var CANON_TEXT = 1 << 22; + static var canonTextLeft = 0; + /** A text for a value that does not depend on the order fields and map keys come out in. Shared and cyclic values from the serializer's @@ -597,6 +612,13 @@ class Fuzz { throw new CanonTooBig(); if (depth > 32) return "..."; + var text = canonText(v, depth); + if ((canonTextLeft -= text.length) < 0) + throw new CanonTooBig(); + return text; + } + + static function canonText(v:Dynamic, depth:Int):String { function sorted(items:Array) { items.sort(Reflect.compare); return items.join(","); diff --git a/test/fuzz/regressions/serializer/map-holds-itself-in-key-and-value b/test/fuzz/regressions/serializer/map-holds-itself-in-key-and-value new file mode 100644 index 0000000000000000000000000000000000000000..7c51092b4d7262b8ffa8fe00691b82f69ef2b9b0 GIT binary patch literal 32 ncmeaxuQaeKGDt5n$naETuwuwG%rx{j%BZk(EGkN@bV>vOoW=>q literal 0 HcmV?d00001