react-nitro-hermes 0.0.1-alpha.8 → 0.0.1-alpha.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (181) hide show
  1. package/hermes/CMakeLists.txt +874 -0
  2. package/hermes/android/HostObjectJniTest.cpp +178 -0
  3. package/hermes/android/build-logic/build.gradle.kts +31 -0
  4. package/hermes/android/build-logic/settings.gradle.kts +17 -0
  5. package/hermes/android/build-logic/src/main/kotlin/com/facebook/hermes/helpers/internal/SerializeHermesCompilerPackageJson.kt +33 -0
  6. package/hermes/android/build-logic/src/main/kotlin/com/facebook/hermes/plugins/internal/TestUtilsPlugin.kt +80 -0
  7. package/hermes/android/build-logic/src/main/kotlin/com/facebook/hermes/tasks/internal/CustomExecTask.kt +46 -0
  8. package/hermes/android/build-logic/src/main/kotlin/publish.gradle.kts +82 -0
  9. package/hermes/android/build.gradle.kts +332 -0
  10. package/hermes/android/cppruntime/build.gradle +72 -0
  11. package/hermes/android/cppruntime/src/main/AndroidManifest.xml +2 -0
  12. package/hermes/android/cppruntime/src/main/cpp/CMakeLists.txt +8 -0
  13. package/hermes/android/cppruntime/src/main/cpp/stub.cpp +12 -0
  14. package/hermes/android/cross-compile/swift-libicu.patch +13 -0
  15. package/hermes/android/cross-compile/toolchain-android-arm-api13-gnustl.cmake +13 -0
  16. package/hermes/android/cross-compile/toolchain-android-arm-api15-gnustl.cmake +14 -0
  17. package/hermes/android/gradle/libs.versions.toml +30 -0
  18. package/hermes/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  19. package/hermes/android/gradle/wrapper/gradle-wrapper.properties +7 -0
  20. package/hermes/android/gradle.properties +3 -0
  21. package/hermes/android/gradlew +248 -0
  22. package/hermes/android/gradlew.bat +93 -0
  23. package/hermes/android/intltest/build.gradle +147 -0
  24. package/hermes/android/intltest/java/com/facebook/hermes/test/CMakeLists.txt +45 -0
  25. package/hermes/android/intltest/java/com/facebook/hermes/test/HermesEpilogue.cpp +68 -0
  26. package/hermes/android/intltest/java/com/facebook/hermes/test/HermesEpilogue.h +44 -0
  27. package/hermes/android/intltest/java/com/facebook/hermes/test/HermesEpilogue.java +45 -0
  28. package/hermes/android/intltest/java/com/facebook/hermes/test/HermesEpilogueTestData.java +32 -0
  29. package/hermes/android/intltest/java/com/facebook/hermes/test/HermesInstrumentationTest.java +150 -0
  30. package/hermes/android/intltest/java/com/facebook/hermes/test/HermesIntlAndroidTest.java +147 -0
  31. package/hermes/android/intltest/java/com/facebook/hermes/test/HermesIntlTest262.java +470 -0
  32. package/hermes/android/intltest/java/com/facebook/hermes/test/HermesRuntimeFactory.cpp +73 -0
  33. package/hermes/android/intltest/java/com/facebook/hermes/test/JSRuntime.cpp +90 -0
  34. package/hermes/android/intltest/java/com/facebook/hermes/test/JSRuntime.h +62 -0
  35. package/hermes/android/intltest/java/com/facebook/hermes/test/JSRuntime.java +155 -0
  36. package/hermes/android/intltest/java/com/facebook/hermes/test/MappedFileBuffer.cpp +48 -0
  37. package/hermes/android/intltest/java/com/facebook/hermes/test/MappedFileBuffer.h +36 -0
  38. package/hermes/android/intltest/java/com/facebook/hermes/test/OnLoad.cpp +13 -0
  39. package/hermes/android/intltest/java/com/facebook/hermes/test/OnLoadEpilogue.cpp +13 -0
  40. package/hermes/android/intltest/src/main/AndroidManifest.xml +2 -0
  41. package/hermes/android/ios-artifacts/build.gradle.kts +65 -0
  42. package/hermes/android/settings.gradle.kts +27 -0
  43. package/hermes/cmake/modules/Hermes.cmake +481 -0
  44. package/hermes/cmake/modules/Lit.cmake +96 -0
  45. package/hermes/doc/Branches.md +38 -0
  46. package/hermes/doc/BuildingAndRunning.md +121 -0
  47. package/hermes/doc/CodingStandards.md +112 -0
  48. package/hermes/doc/CrossCompilation.md +79 -0
  49. package/hermes/doc/Design.md +217 -0
  50. package/hermes/doc/Emscripten.md +156 -0
  51. package/hermes/doc/Features.md +143 -0
  52. package/hermes/doc/GCSafeCoding.md +563 -0
  53. package/hermes/doc/GenGC.md +300 -0
  54. package/hermes/doc/Hades.md +344 -0
  55. package/hermes/doc/HighLevelOptimizations.md +37 -0
  56. package/hermes/doc/IR.md +1306 -0
  57. package/hermes/doc/IntlAPIs.md +196 -0
  58. package/hermes/doc/JIT.md +999 -0
  59. package/hermes/doc/LazyEvalCompilation.md +84 -0
  60. package/hermes/doc/MemoryProfilers.md +316 -0
  61. package/hermes/doc/Modules.md +144 -0
  62. package/hermes/doc/Optimizer.md +83 -0
  63. package/hermes/doc/PerfProfiling.md +29 -0
  64. package/hermes/doc/ProhibitInvoke.md +329 -0
  65. package/hermes/doc/ReactNative.md +40 -0
  66. package/hermes/doc/ReactNativeIntegration.md +94 -0
  67. package/hermes/doc/RegExp.md +24 -0
  68. package/hermes/doc/SpecIncompat.md +85 -0
  69. package/hermes/doc/Strings.md +140 -0
  70. package/hermes/doc/TypedLanguage.md +432 -0
  71. package/hermes/doc/VM.md +174 -0
  72. package/hermes/doc/blog/2024-11-09-octane-benchmark-jit-progress.md +17 -0
  73. package/hermes/doc/blog/2024-12-19-static-hermes-update-dec-2024.md +47 -0
  74. package/hermes/doc/blog/2024-12-23-compiling-javascript-to-wasm.md +267 -0
  75. package/hermes/doc/blog/2025-06-09-jsi-runtime-data-apis.md +23 -0
  76. package/hermes/doc/blog/2025-07-15-static-h-performance-june-2025.md +18 -0
  77. package/hermes/doc/blog/2025-11-02-hermes-compilation-runtime-modes.md +23 -0
  78. package/hermes/doc/blog/2025-11-06-hermes-memory-modes.md +35 -0
  79. package/hermes/doc/blog/2025-11-24-json-parsing-performance.md +17 -0
  80. package/hermes/doc/blog/2025-12-02-iserialization.md +11 -0
  81. package/hermes/doc/blog/2025-12-12-new-way-to-contribute.md +68 -0
  82. package/hermes/doc/blog/2026-06-05-new-hermes-stable-release.md +51 -0
  83. package/hermes/doc/blog/README.md +25 -0
  84. package/hermes/doc/blog/images/2024-11-09-octane-pi3.jpeg +0 -0
  85. package/hermes/doc/blog/images/2024-11-09-octane-pi5.jpeg +0 -0
  86. package/hermes/doc/blog/images/2025-07-15-perf-hv32-interpreter.jpeg +0 -0
  87. package/hermes/doc/blog/images/2025-07-15-perf-hv32-jit.jpeg +0 -0
  88. package/hermes/doc/icu_license.txt +420 -0
  89. package/hermes/doc/img/heap_snapshot_retainers.png +0 -0
  90. package/hermes/doc/img/logo.svg +1 -0
  91. package/hermes/doc/img/object_captured_by_environment.png +0 -0
  92. package/hermes/doc/img/root_categories.png +0 -0
  93. package/hermes/doc/plans/ir-type/flow-type-system.md +324 -0
  94. package/hermes/doc/plans/ir-type/ir-type-system-v2-design.md +652 -0
  95. package/hermes/doc/plans/ir-type/ir-type-system.md +220 -0
  96. package/hermes/doc/plans/ir-type/ir-type-v2-implementation.md +1187 -0
  97. package/hermes/doc/plans/ir-type/memory.md +48 -0
  98. package/hermes/doc/plans/ir-type/phase1-steps.md +512 -0
  99. package/hermes/doc/plans/ir-type/progress.md +239 -0
  100. package/hermes/doc/plans/ir-type/typeinference-primitive-fastpath-design.md +203 -0
  101. package/hermes/doc/plans/ir-type/typeinference-primitive-fastpath-results.md +71 -0
  102. package/hermes/doc/superpowers/plans/2026-07-28-worker-arraybuffer-input.md +597 -0
  103. package/hermes/doc/superpowers/plans/2026-08-18-jitemitter-split.md +1149 -0
  104. package/hermes/doc/superpowers/plans/2026-08-20-jit-type-asserts.md +969 -0
  105. package/hermes/doc/superpowers/plans/2026-08-23-jit-shared-compiler-driver.md +478 -0
  106. package/hermes/doc/superpowers/plans/2026-08-23-x86-64-jit-skeleton.md +797 -0
  107. package/hermes/doc/superpowers/plans/2026-08-24-x86-64-jit-arith.md +444 -0
  108. package/hermes/doc/superpowers/plans/2026-08-24-x86-64-jit-calls.md +261 -0
  109. package/hermes/doc/superpowers/plans/2026-08-24-x86-64-jit-props.md +240 -0
  110. package/hermes/doc/superpowers/plans/2026-10-26-regexp-bytecode-sharing.md +2197 -0
  111. package/hermes/doc/superpowers/plans/2026-10-28-regexp-start-filter-and-exec-fast-paths.md +3981 -0
  112. package/hermes/doc/superpowers/specs/2026-07-28-worker-arraybuffer-input-design.md +206 -0
  113. package/hermes/doc/superpowers/specs/2026-08-18-jitemitter-split-design.md +530 -0
  114. package/hermes/doc/superpowers/specs/2026-08-20-jit-type-asserts-design.md +360 -0
  115. package/hermes/doc/superpowers/specs/2026-08-23-x86-64-jit-design.md +246 -0
  116. package/hermes/doc/superpowers/specs/2026-08-26-icu-free-normalize-design.md +355 -0
  117. package/hermes/doc/superpowers/specs/2026-08-27-icu-free-convert-to-case-design.md +305 -0
  118. package/hermes/doc/superpowers/specs/2026-08-27-icu-free-locale-compare-design.md +429 -0
  119. package/hermes/doc/superpowers/specs/2026-08-29-icu-free-date-format-design.md +297 -0
  120. package/hermes/doc/superpowers/specs/2026-10-26-regexp-bytecode-sharing-design.md +605 -0
  121. package/hermes/doc/superpowers/specs/2026-10-28-regexp-start-filter-and-exec-fast-paths-design.md +551 -0
  122. package/hermes/doc/typescript-stripping.md +90 -0
  123. package/hermes/external/CMakeLists.txt +20 -0
  124. package/hermes/utils/IntlGen/IntlGen.java +531 -0
  125. package/hermes/utils/bench_with_stats.py +827 -0
  126. package/hermes/utils/build-apple-framework.sh +323 -0
  127. package/hermes/utils/chrome_snapshot_parser.py +210 -0
  128. package/hermes/utils/cmake-dev-build.sh +18 -0
  129. package/hermes/utils/commons.sh +55 -0
  130. package/hermes/utils/dump-cpp-funcs.sh +3 -0
  131. package/hermes/utils/es5objects.js +198 -0
  132. package/hermes/utils/format.sh +145 -0
  133. package/hermes/utils/gen-promise-internal-bc.sh +66 -0
  134. package/hermes/utils/genUnicodeTable.py +2503 -0
  135. package/hermes/utils/hermes-lit/CMakeLists.txt +22 -0
  136. package/hermes/utils/hermes-lit/hermes-lit.in +15 -0
  137. package/hermes/utils/jit/README.md +134 -0
  138. package/hermes/utils/jit/jit-diff.sh +160 -0
  139. package/hermes/utils/jit/jit-dump.sh +193 -0
  140. package/hermes/utils/lit-expect-gen/generate.py +217 -0
  141. package/hermes/utils/promise/index.js +15 -0
  142. package/hermes/utils/promise/package.json +13 -0
  143. package/hermes/utils/promise/rollup.config.js +19 -0
  144. package/hermes/utils/promise/yarn.lock +247 -0
  145. package/hermes/utils/scripts/hermes/consts.js +26 -0
  146. package/hermes/utils/scripts/hermes/create-tarball.js +75 -0
  147. package/hermes/utils/scripts/hermes/get-hermes-version.js +57 -0
  148. package/hermes/utils/scripts/hermes/hermes-utils.js +122 -0
  149. package/hermes/utils/scripts/hermes/package.json +6 -0
  150. package/hermes/utils/scripts/hermes/prepare-artifacts.js +113 -0
  151. package/hermes/utils/scripts/hermes/publish-artifacts.js +109 -0
  152. package/hermes/utils/scripts/hermes/publish-npm.js +96 -0
  153. package/hermes/utils/scripts/hermes/scm-utils.js +41 -0
  154. package/hermes/utils/scripts/hermes/set-artifacts-version.js +64 -0
  155. package/hermes/utils/scripts/hermes/version-utils.js +98 -0
  156. package/hermes/utils/scripts/hermes/yarn.lock +240 -0
  157. package/hermes/utils/summarize_sizes.py +184 -0
  158. package/hermes/utils/symbolicate-nativecall-stats.py +112 -0
  159. package/hermes/utils/test_runner.py +13 -0
  160. package/hermes/utils/testsuite/README.facebook +3 -0
  161. package/hermes/utils/testsuite/README.md +69 -0
  162. package/hermes/utils/testsuite/cli.py +547 -0
  163. package/hermes/utils/testsuite/es_ast.py +337 -0
  164. package/hermes/utils/testsuite/external/LICENSE +28 -0
  165. package/hermes/utils/testsuite/external/README.facebook +8 -0
  166. package/hermes/utils/testsuite/external/__init__.py +0 -0
  167. package/hermes/utils/testsuite/external/_monkeyYaml.py +164 -0
  168. package/hermes/utils/testsuite/external/parse_test262.py +141 -0
  169. package/hermes/utils/testsuite/hermes.py +522 -0
  170. package/hermes/utils/testsuite/preprocess.py +365 -0
  171. package/hermes/utils/testsuite/progress.py +377 -0
  172. package/hermes/utils/testsuite/skiplist.json +4356 -0
  173. package/hermes/utils/testsuite/skiplist.py +270 -0
  174. package/hermes/utils/testsuite/test_run_defs.py +435 -0
  175. package/hermes/utils/testsuite/typing_defs.py +52 -0
  176. package/hermes/utils/testsuite/utils.py +214 -0
  177. package/hermes/utils/tidy.sh +22 -0
  178. package/hermes/utils/update-fbcode-hermes.sh +53 -0
  179. package/hermes/utils/updateErrorTest.py +193 -0
  180. package/hermes/utils/wasm-compile.sh +65 -0
  181. package/package.json +1 -1
@@ -0,0 +1,605 @@
1
+ # Shared RegExp bytecode for literals
2
+
3
+ Status: implemented (see Delivered). Written against x86-jit `58f7ba649`,
4
+ then rebased onto `static_h` `393a539e6`; nothing here depends on the JIT.
5
+
6
+ ## Problem
7
+
8
+ Every evaluation of a regex literal produces a new `JSRegExp` (required by
9
+ the spec) and gives it a private copy of the regex bytecode:
10
+
11
+ - Interpreter and both JITs: `CreateRegExp` →
12
+ `Interpreter::createRegExp` (`lib/VM/Interpreter-slowpaths.cpp:597-621`;
13
+ the JIT reaches it through `lib/VM/JIT/JitHandlers.cpp:245`) fetches the
14
+ bytes from the module (`RuntimeModule::getRegExpBytecodeFromRegExpID`,
15
+ `lib/VM/RuntimeModule.cpp:290-295`) and `JSRegExp::initializeBytecode`
16
+ mallocs and memcpys them (`lib/VM/JSRegExp.cpp:264-275`). The finalizer
17
+ frees them (`lib/VM/JSRegExp.cpp:377-387`).
18
+ - Static Hermes: `_sh_ljs_create_regexp` (`lib/VM/StaticH.cpp:1789-1810`)
19
+ calls `JSRegExp::initialize(re, runtime, pattern, flags)`, which
20
+ **re-parses and re-compiles the pattern on every evaluation**
21
+ (`lib/VM/JSRegExp.cpp:169-204`). SH has no ahead-of-time regex bytecode
22
+ (`lib/BCGen/SH/SH.cpp:1805-1825`, TODO T132343328).
23
+
24
+ The interpreter cost is a malloc + memcpy + free per evaluation; the SH
25
+ cost is a full parse and compile per evaluation. Either way, nothing
26
+ per-pattern survives between evaluations, so a future regex JIT would have
27
+ nowhere to keep compiled code.
28
+
29
+ ## Goals
30
+
31
+ 1. Each regex literal's bytecode is materialized at most once per
32
+ `RuntimeModule` (interpreter/JIT) or per `SHUnit` (SH), and shared by
33
+ every `JSRegExp` created from it.
34
+ 2. SH compiles regex literals ahead of time, as HBC already does, so it
35
+ never parses a literal at runtime.
36
+ 3. A single shared, immutable, reference-counted object that a later regex
37
+ JIT can hang compiled code off.
38
+
39
+ ## Non-goals
40
+
41
+ - Caching `new RegExp(str)`. Recreating a dynamic regex repeatedly is the
42
+ program's choice; dynamic regexes keep exclusively owned bytecode, as
43
+ today. (A regex JIT would compile only after a per-entry use count, which
44
+ a regex rebuilt per call never reaches.)
45
+ - Weak references, eviction, LRU. Literal caches are bounded by the number
46
+ of distinct literals and hold strong references.
47
+ - Borrowing the module's storage instead of copying once. The module is not
48
+ guaranteed to outlive the regex (see Lifetime below), and with lazy
49
+ compilation the storage is replaced as the module grows
50
+ (`lib/BCGen/HBC/BytecodeGenerator.cpp:848-851` →
51
+ `lib/BCGen/HBC/BCProviderFromSrc.cpp:72-73`).
52
+ - Matcher, glue, and per-search allocation costs (`std::vector` in
53
+ `performSearch`, `lib/VM/JSRegExp.cpp:291`).
54
+
55
+ ## Design
56
+
57
+ ### `vm::RegExpBytecode`
58
+
59
+ New native class (`include/hermes/VM/RegExpBytecode.h`; the name
60
+ `CompiledRegExp` is taken by the compiler-side class in
61
+ `include/hermes/Regex/RegexSerialization.h:39`):
62
+
63
+ ```cpp
64
+ class RegExpBytecode final
65
+ : public llvh::ThreadSafeRefCountedBase<RegExpBytecode> {
66
+ public:
67
+ /// Who is responsible for reporting this object's memory.
68
+ enum class Ownership : uint8_t {
69
+ /// Owned by exactly one JSRegExp; that JSRegExp reports it.
70
+ Exclusive,
71
+ /// Held by a literal cache (RuntimeModule or SHUnitExt) and shared by
72
+ /// any number of JSRegExps; the cache reports it.
73
+ Cached,
74
+ };
75
+
76
+ static llvh::IntrusiveRefCntPtr<RegExpBytecode> create(
77
+ llvh::ArrayRef<uint8_t> bytecode, Ownership ownership);
78
+
79
+ llvh::ArrayRef<uint8_t> bytecode() const;
80
+ const regex::RegexBytecodeHeader &header() const;
81
+ Ownership ownership() const;
82
+ /// Bytes of malloc'd memory, including this header.
83
+ size_t mallocSize() const;
84
+
85
+ static void operator delete(void *p) { free(p); }
86
+ private:
87
+ uint32_t size_;
88
+ Ownership ownership_;
89
+ // uint8_t bytes_[size_] follow, suitably aligned.
90
+ };
91
+ ```
92
+
93
+ - One `checkedMalloc` holds the object and the trailing bytes; a
94
+ class-specific `operator delete` frees it, which is what
95
+ `ThreadSafeRefCountedBase::Release` calls
96
+ (`external/llvh/include/llvh/ADT/IntrusiveRefCntPtr.h:97-102`).
97
+ - Immutable after `create`. Nothing in the VM writes to bytecode after
98
+ initialization today; the one per-object flag mutation (split clearing
99
+ `sticky`, `lib/VM/JSLib/RegExp.cpp:2436-2439`) writes the `JSRegExp`'s
100
+ own `syntaxFlags_`, which stays per object (see below).
101
+ - The count is atomic because the last release can happen on the Hades
102
+ background thread: old-generation sweep runs finalizers
103
+ (`lib/VM/gcs/HadesGC.cpp:1063`, `:1112`, `:1168`), and `~JSRegExp` is
104
+ called from `JSRegExp::_finalizeImpl` (`lib/VM/JSRegExp.cpp:381-387`).
105
+ Deletion is a plain `free`, safe on any thread. Nothing ever looks up an
106
+ entry whose count may be dropping to zero, because every lookup path
107
+ goes through a cache that itself holds a strong reference.
108
+
109
+ ### `JSRegExp`
110
+
111
+ Replace `uint8_t *bytecode_` and `uint32_t bytecodeSize_`
112
+ (`include/hermes/VM/JSRegExp.h:151-152`) with
113
+ `llvh::IntrusiveRefCntPtr<RegExpBytecode> bytecode_`, keeping it right
114
+ after `pattern_` for the alignment reason in the comment at
115
+ `include/hermes/VM/JSRegExp.h:145-148`. A one-pointer intrusive handle is
116
+ safe to relocate when the GC moves the cell.
117
+
118
+ - `syntaxFlags_` stays a per-object field, still initialized from the
119
+ header (`lib/VM/JSRegExp.cpp:269-271`). `JSRegExp::search` reads it for
120
+ `sticky` (`lib/VM/JSRegExp.cpp:342`), and split's `setSyntaxFlags` must
121
+ keep affecting only the splitter.
122
+ - `initialize(..., ArrayRef<uint8_t>)` (`include/hermes/VM/JSRegExp.h:53-58`)
123
+ becomes `initialize(..., IntrusiveRefCntPtr<RegExpBytecode>)`. It asserts
124
+ `!bytecode_`: every caller initializes a freshly created object (the
125
+ callers are `Interpreter-slowpaths.cpp:613-619`, `StaticH.cpp:1795-1801`,
126
+ `JSLib/RegExp.cpp:484`, `:667-669`, and the two internal ones in
127
+ `JSRegExp.cpp`; Hermes has no `RegExp.prototype.compile`).
128
+ - `initialize(self, pattern, flags)` (`lib/VM/JSRegExp.cpp:169-204`, the
129
+ dynamic path) wraps `regex.compile()` in an `Exclusive` entry.
130
+ - The fast copy `initialize(self, other, flags)`
131
+ (`lib/VM/JSRegExp.cpp:130-166`) is used only by
132
+ `regExpConstructorFastCopy` (`lib/VM/JSLib/RegExp.cpp:661-675`), whose
133
+ callers are matchAll (`:1679`) and split (`:2428`). The public
134
+ constructor `new RegExp(re)` does not use it: it goes through
135
+ `regExpConstructorInternal` → `regExpInitialize` and recompiles
136
+ (`lib/VM/JSLib/RegExp.cpp:566-630`, `:484`). That path is a dynamic
137
+ regex and stays out of scope. With matching flags, the fast copy does:
138
+ - `Cached` → share the pointer;
139
+ - `Exclusive` → copy into a new `Exclusive` entry, exactly as today.
140
+ This keeps the invariant "an `Exclusive` entry has exactly one
141
+ `JSRegExp` holder", which the memory accounting below relies on.
142
+ - New accessor `llvh::ArrayRef<uint8_t> getBytecode() const` for the two
143
+ `search` call sites (`lib/VM/JSRegExp.cpp:351`, `:359`) and for tests.
144
+ - `~JSRegExp` drops the reference instead of calling `free`.
145
+
146
+ ### Interpreter/JIT literal cache: per `RuntimeModule`
147
+
148
+ - New member `std::vector<llvh::IntrusiveRefCntPtr<RegExpBytecode>>
149
+ regExpCache_` and accessor `getRegExpBytecode(uint32_t regExpID)`, which
150
+ replaces the `ArrayRef` accessor in `Interpreter::createRegExp`.
151
+ - Lazily sized. On a miss, grow to
152
+ `bcProvider_->getRegExpTable().size()` if needed, then create a `Cached`
153
+ entry from `getRegExpBytecodeFromRegExpID`. The table grows under lazy
154
+ compilation (`BytecodeGenerator.cpp:848-851`), so the size is never
155
+ fixed at module creation.
156
+ - Mutator-thread only: `Interpreter::createRegExp` runs on the mutator.
157
+ - Literal IDs are already deduplicated by pattern+flags at compile time
158
+ (`UniquingRegExpTable`, `include/hermes/Regex/RegexSerialization.h:90`),
159
+ so two literals with the same text in one module share one entry.
160
+ - Named groups are unaffected. For literals, the compiler emits a
161
+ `HermesBuiltin.initRegexNamedGroups` call after `CreateRegExp`
162
+ (`lib/IRGen/ESTreeIRGen-expr.cpp:2911-2946`) that sets the per-object
163
+ mapping (`lib/VM/JSLib/HermesBuiltin.cpp:983-991`).
164
+
165
+ ### SH: ahead-of-time regex bytecode and a per-unit cache
166
+
167
+ Today SH emits `_sh_ljs_create_regexp(shr, pattern_sym, flags_sym)`
168
+ (`lib/BCGen/SH/SH.cpp:1798-1825`), and the runtime parses and compiles on
169
+ every call. The comment there says threading a compiled regex through to
170
+ SH codegen was "too awkward". That no longer holds: the compiler
171
+ `Context` already caches every literal's `CompiledRegExp` by pattern and
172
+ flags (`include/hermes/AST/Context.h:351-357`). HBC's ISel uses it
173
+ (`lib/BCGen/HBC/ISel.cpp:1514-1527`), and so does IRGen for every backend
174
+ (`lib/IRGen/ESTreeIRGen-expr.cpp:2920-2921`).
175
+
176
+ **Compiler (`lib/BCGen/SH/SH.cpp`):**
177
+ - `ModuleGen` (`:429-446`) gains a `UniquingRegExpTable`
178
+ (`include/hermes/Regex/RegexSerialization.h:90`), the same deduplicating
179
+ table HBC uses (`include/hermes/BCGen/HBC/Bytecode.h:219`, `:388-398`).
180
+ - `generateCreateRegExpInst` looks up
181
+ `ctx.getCompiledRegExp(pattern, flags)`, adds it to the table, and emits
182
+ `_sh_ljs_create_regexp(shr, shUnit, pattern_sym, flags_sym, id)`. It
183
+ deletes the commented-out code and the TODO.
184
+ - Unit emission (`:3175-3217`) writes `s_regexp_storage`, built from the
185
+ table's bytecode buffer, and `s_regexp_table`, an array of
186
+ `{offset, length}`. It fills the new `SHUnit` fields. The buffer writer
187
+ `generateBuffer` is currently a private member of `SHLiteralBuffers`
188
+ (`:235`), so it moves to a free function.
189
+ - Every `CreateRegExpInst` that reaches SH codegen has a compiled entry
190
+ in the `Context`:
191
+ - `-O0` and typed mode both run semantic resolution, which compiles
192
+ regex literals into the `Context` (`tools/shermes/shermes.cpp:886`,
193
+ `lib/Sema/SemResolve.cpp:174`, `lib/Sema/SemanticResolver.cpp:821`).
194
+ - Runtime `eval` in SH compiles to HBC, not SH
195
+ (`lib/VM/StaticH.cpp:2441`, `lib/VM/JSLib/eval.cpp:101`), so its
196
+ literals take the `RuntimeModule` path.
197
+
198
+ **ABI (`include/hermes/VM/static_h.h`):**
199
+ - New `SHRegExpTableEntry { uint32_t offset; uint32_t length; }`.
200
+ - New `SHUnit` fields: `regexp_storage`, `regexp_table`, `num_regexps`.
201
+ - `_sh_ljs_create_regexp` gains `SHUnit *unit` and `uint32_t regexp_id`
202
+ parameters.
203
+ - This is an incompatible change to the unit layout and to the function's
204
+ signature. Generated C is always compiled against the `static_h.h` of
205
+ the runtime that loads it. The repository has no other caller: the two
206
+ hits in `lib/VM/JIT/JitHandlers.{h,cpp}` are comments.
207
+
208
+ **Runtime:**
209
+ - `SHUnitExt` (`lib/VM/SHUnitExt.h:19-32`) gains
210
+ `std::vector<IntrusiveRefCntPtr<RegExpBytecode>> regExpCache`, sized to
211
+ `num_regexps` when the unit is attached (`lib/VM/StaticHUnit.cpp:117`).
212
+ It is freed with the `SHUnitExt` in `sh_unit_done`
213
+ (`lib/VM/StaticHUnit.cpp:201-204`, called from `~Runtime`,
214
+ `lib/VM/Runtime.cpp:540-542`). SH has no lazy compilation, so the size
215
+ is fixed.
216
+ - `_sh_ljs_create_regexp` creates the `JSRegExp` and takes the bytecode
217
+ from the cache, creating a `Cached` entry on first use. It never parses.
218
+ This mirrors `Interpreter::createRegExp`, and both call the same
219
+ `JSRegExp::initialize(…, IntrusiveRefCntPtr)`.
220
+ - Named groups: SH literals get the `initRegexNamedGroups` call from the
221
+ shared IRGen, as HBC does (`lib/IRGen/ESTreeIRGen-expr.cpp:2923-2944`).
222
+ The runtime group-mapping build in `initialize(pattern, flags)` was
223
+ redundant for SH literals and goes away with the parse. T7c checks that
224
+ named-group behaviour is preserved.
225
+ - No symbol-keyed runtime map is needed. The unit's index identifies the
226
+ literal, as `regExpID` does for `RuntimeModule`.
227
+
228
+ ### Lifetime
229
+
230
+ - A `RuntimeModule` can be destroyed while regexes created from it are
231
+ still alive. `Runtime::markDomainRefInRuntimeModules` deletes modules
232
+ whose domain is dead during weak-root marking
233
+ (`lib/VM/Runtime.cpp:803-826`), and a `JSRegExp` holds no reference to
234
+ its domain. The module's cache then drops its references; surviving
235
+ `JSRegExp`s keep the entries alive.
236
+ - `RuntimeModule::~RuntimeModule` may therefore run on whatever thread
237
+ runs weak-root marking. The atomic count covers this; no other shared
238
+ state is touched.
239
+
240
+ ### Memory accounting and heap snapshots
241
+
242
+ Today each `JSRegExp` reports its bytecode as malloc size and as a native
243
+ `RegExpBytecode` snapshot node identified by
244
+ `gc.getNativeID(bytecode_)`, untracked in the finalizer
245
+ (`lib/VM/JSRegExp.cpp:381-425`). Sharing would emit duplicate native
246
+ nodes and multiply-counted sizes, so responsibility follows `Ownership`:
247
+
248
+ - `Exclusive`: unchanged. The `JSRegExp` reports `mallocSize()`, emits
249
+ the native node and edge, and untracks the entry pointer in its
250
+ finalizer.
251
+ - `Cached` in a `RuntimeModule`: this follows the `CodeBlock` precedent.
252
+ Code blocks get their own snapshot nodes and are not part of
253
+ `additionalMemorySize()` (`lib/VM/RuntimeModule.cpp:412-417`,
254
+ `:455-498`).
255
+ - **Snapshot.** `snapshotAddNodes` emits:
256
+ - one native `RegExpBytecode` node per materialized entry, sized
257
+ `mallocSize()`;
258
+ - a node for the cache vector, identified by `&regExpCache_` (as
259
+ `&functionMap_` is) and sized `capacity * sizeof(pointer)`, with
260
+ indexed edges to the entry nodes.
261
+
262
+ `snapshotAddEdges` adds a `regExpCache` edge from the module node.
263
+ `additionalMemorySize()` is unchanged, so the `RuntimeModule` node's
264
+ size, which is `sizeof(RuntimeModule) + additionalMemorySize()`
265
+ (`lib/VM/Domain.cpp:85-94`), does not double-count.
266
+ - **Malloc estimate.** A new `RuntimeModule::regExpCacheMemorySize()`
267
+ returns the vector capacity in bytes plus the sum of the entry sizes.
268
+ It is added in `Domain::_mallocSizeImpl` (`lib/VM/Domain.cpp:62-70`)
269
+ and nowhere in the snapshot.
270
+ - **ID tracking.** `~RuntimeModule` untracks the entry pointers and
271
+ `&regExpCache_`, as it does for code blocks and `&functionMap_`
272
+ (`lib/VM/RuntimeModule.cpp:72-75`). Otherwise a stale native ID
273
+ survives and runtime teardown can assert (`lib/VM/Runtime.cpp:525`).
274
+ - **`JSRegExp`.** It reports 0 and emits nothing for a `Cached` entry.
275
+ - `Cached` in an `SHUnitExt`: `sh_unit_additional_memory_size`
276
+ (`lib/VM/StaticHUnit.cpp:206-210`) adds the cache vector's capacity
277
+ plus the sizes of the materialized entries. SH units have no native
278
+ snapshot node today, so these entries get no node either (follow-up dz
279
+ issue).
280
+ - Known gap: after a module is destroyed, `Cached` entries still held by
281
+ surviving `JSRegExp`s are reported by nobody. This is rare (dead domain,
282
+ live regex) and preferable to dangling snapshot edges.
283
+
284
+ ### Future regex JIT hook (not in this change)
285
+
286
+ Compiled code would live on `Cached` entries only, behind a use counter,
287
+ and be freed when the entry's last reference drops.
288
+
289
+ - **Release thread.** That release may run on the Hades background
290
+ thread. asmjit permits this: `JitRuntime::_release` calls
291
+ `JitAllocator::release`, which takes the allocator lock
292
+ (`external/asmjit/asmjit/src/asmjit/core/jitruntime.cpp:73-75`,
293
+ `jitallocator.cpp:904-912`).
294
+ - **Rooting invariant the JIT must keep.** Code must not be freed while
295
+ it runs. Holding a raw pointer to the code does not guarantee that.
296
+ Every invocation must keep either the owning `JSRegExp` rooted or an
297
+ explicit entry reference for its whole duration, including reentry.
298
+ Today's matcher receives a `Handle<JSRegExp>`
299
+ (`lib/VM/JSRegExp.cpp:324`).
300
+ - **Allocator lifetime.** The allocator must outlive every entry that
301
+ releases code.
302
+
303
+ This change only has to keep `RegExpBytecode` as the single per-pattern
304
+ object.
305
+
306
+ ## Tests
307
+
308
+ Each test states what fails before the change, or labels itself a
309
+ regression guard. They are VM unit tests in a new
310
+ `unittests/VMRuntime/RegExpBytecodeTest.cpp` (running JS with
311
+ `runtime.run`, as in `unittests/VMRuntime/StackTracesTreeTest.cpp:43-57`)
312
+ unless noted. "Same bytes" means equal `getBytecode().data()` on two
313
+ simultaneously live objects. Before the change, two live objects always
314
+ have distinct malloc'd buffers, so that equality cannot hold by accident.
315
+
316
+ - **T1, literal sharing.** `function f(){ return /a(b)c/g; } [f(), f()]`:
317
+ both elements have the same bytes. Fails before: distinct copies.
318
+ - **T2, lazy compilation growth.** Run with lazy compilation forced,
319
+ where a lazily compiled function contains a literal whose ID is past
320
+ the table size at module load. The literal is shared across two calls
321
+ and matches correctly. The test first asserts that precondition (the
322
+ table size before and after the lazy compile, read from the
323
+ `BCProvider`), so it cannot pass vacuously. Fails before: distinct
324
+ copies. An implementation
325
+ that sized the cache at module load would index out of bounds (ASan
326
+ build).
327
+ - **T3, dynamic not shared (regression guard).** `new RegExp("abc")`
328
+ twice gives different bytes. Passes before and after; pins the
329
+ non-goal.
330
+ - **T4, fast copy (C++).** The test calls
331
+ `JSRegExp::initialize(self, other, flags)` directly, keeping both
332
+ objects rooted and alive during the comparison:
333
+ - `other` comes from a literal (`Cached`): equal flags share the bytes;
334
+ different flags give distinct, recompiled bytes.
335
+ - `other` comes from `new RegExp(str)` (`Exclusive`): equal flags give
336
+ distinct bytes.
337
+
338
+ The first assertion fails before. The public `new RegExp(re)` is not
339
+ tested for sharing because it is out of scope.
340
+ - **T5, per-object sticky (regression guard).**
341
+ `var r = /a/y; "bab".split(r); r.lastIndex = 0; r.exec("ba")` is
342
+ `null`, and `r.sticky` is `true`. Guards against `split`'s flag change
343
+ leaking through shared state.
344
+ - **T6, module dies first (regression guard).** The default, non-lazy
345
+ `Runtime::run` is unsuitable here. It loads source-compiled modules as
346
+ persistent whenever lazy compilation is off
347
+ (`include/hermes/BCGen/HBC/BCProviderFromSrc.h:164-168`), and
348
+ `runBytecode` then keeps the provider in `persistentBCProviders_`
349
+ (`lib/VM/Runtime.cpp:1123-1125`, `:1156`), so the storage would
350
+ outlive the module. Instead:
351
+ - Compile with `bytecodeForSource`.
352
+ - Copy the bytes into an **owning** buffer (`OwnedMemoryBuffer` around
353
+ `getMemBufferCopy`), because a plain `Buffer` does not free its data
354
+ (`include/hermes/Support/Buffer.h:22-24`). Then drop the vector.
355
+ - Build a `BCProviderFromBuffer` held only by the `shared_ptr` passed to
356
+ `runBytecode` with `persistent = false`. `runBytecode` creates the
357
+ fresh `Domain` itself (`lib/VM/Runtime.cpp:1193`).
358
+ - The script returns a literal regex. The test keeps only that
359
+ `JSRegExp` and a `std::weak_ptr<BCProvider>`.
360
+ - Collect, and assert the precondition that the weak pointer has
361
+ expired. Then `exec` must still match, with no ASan report.
362
+
363
+ Passes before (private copy). Fails under ASan for any variant that
364
+ borrows module storage.
365
+ - **T7, SH.**
366
+ - **T7a, emission:** `test/shermes/regexp-aot.js` with
367
+ `%shermes -emit-c -o - %s | %FileCheck %s`, following
368
+ `test/shermes/strict-equal-inline-fastpath.js:8`. It checks:
369
+ - `s_regexp_storage` and `s_regexp_table` are emitted;
370
+ - two textually equal literals produce one table entry;
371
+ - the call passes `shUnit` and an index.
372
+
373
+ Fails before: none of these is emitted.
374
+ - **T7b, sharing (C++):** a unit test builds a zeroed `SHUnit` with
375
+ only the regexp fields set, attaches the `SHUnitExt`, and calls
376
+ `_sh_ljs_create_regexp` twice with the same index. Both results have
377
+ the same bytes. Fails before: the old function re-compiles on every
378
+ call.
379
+ - **T7c, behaviour (regression guard):** the same file run with
380
+ `%shermes -exec`. It evaluates a named-group literal in a loop and
381
+ checks `groups`, `flags`, `lastIndex` and `sticky` on each result.
382
+ - **Not covered by tests.** No test proves that the runtime parse is
383
+ gone. A cache miss that compiled from the symbols and then shared the
384
+ result would pass T7a–c. Code review verifies instead that cache
385
+ misses read the emitted bytecode and never call runtime compilation.
386
+ That avoids adding a production counter.
387
+ - **T8, memory accounting.** First assert that the cache is empty. Then,
388
+ for T1's objects:
389
+ - each `JSRegExp` has malloc size 0;
390
+ - `regExpCacheMemorySize()` grows by the vector capacity in bytes plus
391
+ the entry's `mallocSize()` after the first evaluation, and by 0
392
+ after the second;
393
+ - a heap snapshot contains exactly one `RegExpBytecode` node for the
394
+ literal, sized `mallocSize()`, plus the cache-vector node with its
395
+ edges;
396
+ - the `RuntimeModule` node's size is unchanged by the evaluations.
397
+
398
+ Fails before: each `JSRegExp` reports `bytecodeSize_` and emits its
399
+ own node.
400
+ - **Existing coverage:** `test/hermes/regexp*.js`, the `check-hermes`
401
+ suite, and `-Xjit=force` runs of the JIT lit tests (the JIT calls the
402
+ same `Interpreter::createRegExp`).
403
+ - **TSan.** Regexes that die young are finalized on the mutator
404
+ (`lib/VM/gcs/HadesGC.cpp:2738-2739`), so the scenario must promote them
405
+ first. It uses the async-collection pattern in
406
+ `unittests/VMRuntime/GCBasicsTest.cpp:722`.
407
+ - **Case 1:** keep literal-created regexes in an array across enough
408
+ young-gen collections to promote them, asserting `!inYoungGen` as a
409
+ precondition. Then drop them and start
410
+ `collectWithOptions({Major, Async})`. A plain `collect()` blocks and
411
+ runs on the mutator (`HadesGC.cpp:1462`, `:1484`). Meanwhile the
412
+ mutator keeps evaluating the same literal, acquiring the cached
413
+ entry.
414
+ - **Case 2:** promote the regexes the same way. Then stop evaluating
415
+ the module's literal and release its factory function, since a
416
+ callable function keeps its `Domain` alive. Collect until module
417
+ deletion is verified, then drop the regexes and drive an async major
418
+ collection with unrelated work. The last references are released by
419
+ old-gen-swept finalizers after the module destructor.
420
+
421
+ ## Risks
422
+
423
+ - **SH ABI:** the `SHUnit` layout and `_sh_ljs_create_regexp`'s signature
424
+ change. Already-generated C must be regenerated.
425
+ - **`JSRegExp` cell size:** shrinks by the 4-byte `bytecodeSize_` (modulo
426
+ padding).
427
+ - **Heap snapshot tooling** that expects a `RegExpBytecode` node per
428
+ regex will see one per module entry instead.
429
+ - **SH output embeds regex bytecode:** generated SH C now carries the
430
+ regex bytecode, so a change to the regex bytecode format requires
431
+ regenerating SH output, as literal buffers already do. Nothing checks
432
+ it.
433
+ - **Sandbox:** the generated `API/hermes_sandbox` sources are not
434
+ regenerated by this change.
435
+
436
+ ## Delivered (2026-10-26)
437
+
438
+ ### Commits
439
+
440
+ - Task 1, `6a5bba9a2`: VM: hold JSRegExp bytecode in a refcounted
441
+ RegExpBytecode
442
+ - Task 2, `abb995b2f`: VM: share regexp literal bytecode per RuntimeModule
443
+ - Task 3, `02f2e6495`: VM: report cached regexp bytecode from its
444
+ RuntimeModule; `7a9243940`: VM: test the Domain's report of cached
445
+ regexp bytecode
446
+ - Task 4, `cb32dd9cb`: SH: compile regexp literals ahead of time
447
+ - Task 5, `6d4aae072`: VM: TSan guards for cached regexp bytecode release
448
+
449
+ ### Suites
450
+
451
+ Each tree was rebuilt (`hermes hermesc shermes HermesVMRuntimeTests`)
452
+ before its run. Counts are expected passes / expected failures /
453
+ unsupported / unexpected failures from `check-hermes`.
454
+
455
+ | Tree | Result |
456
+ |---|---|
457
+ | `cmake-build-asan` (instrumented) | 4527 / 6 / 162 / 2 |
458
+ | `cmake-build-hsan` (instrumented) | 4231 / 6 / 329 / 3 |
459
+ | `cmake-build-hv32` (not instrumented) | 4317 / 6 / 328 / 1 |
460
+ | `cmake-build-asan`, `LIT_FILTER='jit/'` | 167 / 0 / 8 / 0 (plus the 2 known unit failures, which the filter does not exclude) |
461
+ | `cmake-build-tsan`, `TSAN_OPTIONS=halt_on_error=1`, `RegExpBytecode*:HeapSnapshot*` | 25 of 25 passed, no report |
462
+
463
+ Unexpected failures, none caused by this branch:
464
+
465
+ - `APITests` `WithOrWithoutAllocationTracker/HeapSnapshotAPITest.HeapTimeline/0`
466
+ and `/1` fail on every instrumented tree (asan, hsan). They fail
467
+ identically at `fb22f7c1f`, before this branch, once
468
+ `HERMES_MEMORY_INSTRUMENTATION=ON`. Filed as a dz issue (below).
469
+ - `jit/loose-eq-null.js` fails on hsan and hv32 with "JIT is not enabled
470
+ in this build". Neither tree enables the JIT, the test carries no
471
+ `REQUIRES` for it, and the file is not touched by this branch. The test
472
+ exists only on x86-jit.
473
+
474
+ `cmake-build-asan`, `hsan` and `tsan` were reconfigured with
475
+ `HERMES_MEMORY_INSTRUMENTATION=ON` (the default follows
476
+ `HERMES_ENABLE_DEBUGGER`, so `HeapSnapshotTest.cpp` was never compiled
477
+ there). `hv32` stays uninstrumented, so heap-snapshot coverage runs on
478
+ the other three trees only. The pass counts above are therefore not
479
+ comparable to the 4473 baseline.
480
+
481
+ ### Suites after rebasing onto `static_h` (2026-10-27)
482
+
483
+ On `static_h` the JIT exists only for arm64, so `cmake-build-asan` was
484
+ reconfigured with `HERMESVM_ALLOW_JIT=0` and the JIT was exercised in an
485
+ arm64 cross build (`HERMESVM_ALLOW_JIT=2`, Debug -O1, no ASan) under
486
+ qemu-user, following `aarch64/README.md` from x86-jit.
487
+
488
+ | Tree | Result |
489
+ |---|---|
490
+ | `cmake-build-asan` (instrumented) | 4422 / 6 / 202 / 2 |
491
+ | `cmake-build-hsan` (instrumented) | 4296 / 6 / 203 / 2 |
492
+ | `cmake-build-hv32` (not instrumented) | 4382 / 6 / 202 / 0 |
493
+ | `cmake-build-arm64`, `LIT_FILTER='jit/'` | 47 / 0 / 1 / 0 |
494
+ | `cmake-build-tsan`, `TSAN_OPTIONS=halt_on_error=1`, `RegExpBytecode*:HeapSnapshot*` | 26 of 26 passed, no report |
495
+
496
+ The unexpected failures are the two `HeapTimeline` tests, which fail
497
+ identically on `static_h` `393a539e6` with instrumentation on. On arm64,
498
+ `LIT_FILTER='jit/|regexp'` also fails the seven tests that run
499
+ `shermes -exec`, all because the host linker cannot link the arm64
500
+ `libshermes_console`; that is a limit of the cross setup.
501
+
502
+ ### Mutations and the failure each produced
503
+
504
+ Task 1:
505
+
506
+ - `mallocSize()` returns `size_`: `CreateCopiesBytes` (19 vs 31).
507
+ - Dynamic-path entry created `Cached`: `DynamicRegExpsAreExclusive`.
508
+ - `search` reads sticky from the shared header, `setSyntaxFlags` writes
509
+ it, fast copy always shares: `SplitDoesNotClearStickyOfItsArgument`.
510
+
511
+ Task 2:
512
+
513
+ - Return a fresh `Cached` entry without storing it:
514
+ `LiteralIsSharedAcrossEvaluations` and
515
+ `LazilyCompiledLiteralGrowsTheCache`.
516
+ - Delete the resize branch: ASan SEGV in `getRegExpBytecode`. A variant
517
+ that sized the cache once on first miss (resize only when the cache is
518
+ empty) passed `LazilyCompiledLiteralGrowsTheCache`, because that test
519
+ grows the table only once. `LazyCompilesKeepGrowingTheCache` now pins
520
+ growth across lazy compiles: it compiles a second function after the
521
+ first (table size 1 -> 2) and fails under that mutation (see the final
522
+ review fix).
523
+ - Fast copy always creates: `FastCopySharesOnlyCachedBytecode`.
524
+ - Function-local static cache: `LiteralCachesArePerModule` and
525
+ `SplitDoesNotClearStickyOfItsArgument`.
526
+ - Borrowed variant of `RegExpBytecode`: ASan heap-use-after-free in
527
+ `searchWithBytecodeImpl` from `LiteralOutlivesItsModule`.
528
+ - Sticky read from the shared header: `regexp-literal-shared.js` fails
529
+ under `-O`, `-O0`, `-lazy`, `-gc-sanitize-handles=1` and the JIT lit
530
+ test.
531
+
532
+ Task 3 (instrumented tree):
533
+
534
+ - `JSRegExp::_mallocSizeImpl` uses the entry: `getMallocSize` expected 0,
535
+ got 35.
536
+ - Per-entry node loop disabled: assertion in
537
+ `HeapSnapshot::addIndexedEdge`.
538
+ - Per-entry `untrackNative` removed: assertion in `Runtime::~Runtime`.
539
+ - `regExpCache` edge removed: the edge expectation in `HeapSnapshotTest`.
540
+ - `Domain.cpp` drops `regExpCacheMemorySize()`: Domain size expected 379,
541
+ got 336.
542
+
543
+ Task 4:
544
+
545
+ - `sh_unit_get_regexp_bytecode` returns without storing:
546
+ `SHUnitSharesPrecompiledBytecode`.
547
+ - Emit ID 0 for every literal: `regexp-aot.js` CHECK failure under `-O0`
548
+ and `-O`.
549
+ - `addRegExp` early return disabled: `regexp-aot.js` fails under `-O0`.
550
+ Under `-O` the optimizer merges the first two literals, so only `-O0`
551
+ pins uniquing.
552
+ - `regexp_id ^ 1` in `_sh_ljs_create_regexp`: `regexp-aot.js` EXEC
553
+ failure.
554
+
555
+ Task 5:
556
+
557
+ - `ThreadSafeRefCountedBase` replaced by `RefCountedBase`: TSan data race
558
+ in `RefCountedBase<RegExpBytecode>::Release`, from a Hades finalizer
559
+ against `Retain` in `getRegExpBytecode`. It fired on run 1 of 10, so
560
+ detection is probabilistic.
561
+ - Extra `Release()` in `~RuntimeModule`: ASan heap-use-after-free in
562
+ `RegExpBytecode::ownership()` from `JSRegExp::_finalizeImpl`.
563
+
564
+ Every TSan guard was shown to fire.
565
+
566
+ ### Spec resolutions taken
567
+
568
+ 1. `JSRegExp::getBytecodeEntry()` is added beside `getBytecode()`; tests
569
+ need the entry's ownership and `mallocSize()` (Task 1).
570
+ 2. An `Exclusive` `JSRegExp` reports `mallocSize()`, which includes the
571
+ entry header, where it reported the bare bytecode size (Task 1).
572
+ 3. Trailing bytes are aligned to `alignof(std::max_align_t)` (Task 1).
573
+ 4. The SH cache is sized by `SHUnitExt(uint32_t numRegExps)` and read
574
+ through `sh_unit_get_regexp_bytecode` (Task 4).
575
+ 5. The new `SHUnit` fields follow `object_literal_class_cache` (Task 4).
576
+ 6. T8 lives in `HeapSnapshotTest.cpp` to reuse its snapshot parsers
577
+ (Task 3).
578
+ 7. T6's mutation is a temporary borrowing variant of `RegExpBytecode`;
579
+ it fails under ASan after the provider has expired (Task 2).
580
+ 8. T8 reads the cache vector's capacity from its snapshot node and
581
+ checks the malloc estimate against it (Task 3).
582
+ 9. Task 5's case 1 races the mutator's increments against concurrent
583
+ finalizer decrements; case 2 guards the last release after the module
584
+ destructor (Task 5).
585
+
586
+ Made during implementation:
587
+
588
+ - T8 counts regexp nodes against a recorded baseline plus one, because
589
+ the internal JS modules already cache four entries (Task 3).
590
+ - `RegExpBytecodeTest.cpp` has no `GTEST_SKIP` (not in the vendored
591
+ gtest); the TSan tests return early on non-Hades trees, so they pass
592
+ vacuously there (Task 5).
593
+ - Under `-O` the `regexp-aot.js` check accepts either order for the two
594
+ merged literals (`OPT-DAG`) (Task 4).
595
+ - Controller rulings: Task 1 expects 3 tests, not 4; the review base is
596
+ `fb22f7c1f`; `HERMES_MEMORY_INSTRUMENTATION=ON` in asan/hsan/tsan.
597
+
598
+ ### Follow-ups (dz)
599
+
600
+ - `01a17ba7-e5f4-73fd-a894-4fe229246c62`: SH units have no heap-snapshot
601
+ node for their regexp cache.
602
+ - `01a17ba7-e648-707f-887a-26f2dd9e4f04`: HeapSnapshotAPITest.HeapTimeline
603
+ fails with HERMES_MEMORY_INSTRUMENTATION=ON.
604
+ - test/jit/loose-eq-null.js lacks REQUIRES: jit. It belongs to x86-jit,
605
+ where the test lives, and is not filed on this branch.