|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 |
|
17 | | -import { transformSync } from "@babel/core"; |
| 17 | +import { PluginItem, transformSync } from "@babel/core"; |
18 | 18 |
|
| 19 | +import { compareHooks } from "./compareHooks"; |
19 | 20 | import { esmCodeCoverage } from "./esmCodeCoverage"; |
20 | 21 | import { removeIndentation } from "./testhelpers"; |
21 | 22 |
|
22 | | -function transform(code: string): { code: string; edgeCount: number } { |
| 23 | +function transform( |
| 24 | + code: string, |
| 25 | + extraPlugins: PluginItem[] = [], |
| 26 | +): { code: string; edgeCount: number } { |
23 | 27 | const coverage = esmCodeCoverage(); |
24 | 28 | const result = transformSync(removeIndentation(code), { |
25 | 29 | filename: "test-module.mjs", |
26 | | - plugins: [coverage.plugin], |
| 30 | + plugins: [coverage.plugin, ...extraPlugins], |
27 | 31 | }); |
28 | 32 | return { |
29 | 33 | code: removeIndentation(result?.code), |
@@ -109,4 +113,110 @@ describe("ESM code coverage instrumentation", () => { |
109 | 113 | const { edgeCount } = transform(`|const x = 42;`); |
110 | 114 | expect(edgeCount).toBe(0); |
111 | 115 | }); |
| 116 | + |
| 117 | + describe("combined with compareHooks", () => { |
| 118 | + it("should replace string-literal === with traceStrCmp", () => { |
| 119 | + const { code } = transform( |
| 120 | + ` |
| 121 | + |export function check(s) { |
| 122 | + | return s === "secret"; |
| 123 | + |}`, |
| 124 | + [compareHooks], |
| 125 | + ); |
| 126 | + |
| 127 | + // The === against a string literal must be replaced. |
| 128 | + expect(code).toContain("Fuzzer.tracer.traceStrCmp"); |
| 129 | + expect(code).toContain('"secret"'); |
| 130 | + expect(code).toContain('"==="'); |
| 131 | + // The raw === should be gone from the check expression. |
| 132 | + expect(code).not.toMatch(/s\s*===\s*"secret"/); |
| 133 | + }); |
| 134 | + |
| 135 | + it("should replace number-literal === with traceNumberCmp", () => { |
| 136 | + const { code } = transform( |
| 137 | + ` |
| 138 | + |export function classify(n) { |
| 139 | + | if (n > 10) return "big"; |
| 140 | + | if (n === 0) return "zero"; |
| 141 | + | return "small"; |
| 142 | + |}`, |
| 143 | + [compareHooks], |
| 144 | + ); |
| 145 | + |
| 146 | + expect(code).toContain("Fuzzer.tracer.traceNumberCmp"); |
| 147 | + }); |
| 148 | + |
| 149 | + it("should NOT hook variable-to-variable comparisons", () => { |
| 150 | + // compareHooks only fires when one operand is a literal. |
| 151 | + // Comparing two identifiers is not hooked (same as CJS). |
| 152 | + const { code } = transform( |
| 153 | + ` |
| 154 | + |const target = "something"; |
| 155 | + |export function check(s) { |
| 156 | + | return s === target; |
| 157 | + |}`, |
| 158 | + [compareHooks], |
| 159 | + ); |
| 160 | + |
| 161 | + expect(code).not.toContain("Fuzzer.tracer.traceStrCmp"); |
| 162 | + }); |
| 163 | + |
| 164 | + it("should hook slice-then-compare patterns", () => { |
| 165 | + // This is the pattern used in the integration tests. |
| 166 | + const { code } = transform( |
| 167 | + ` |
| 168 | + |export function verify(s) { |
| 169 | + | if (s.slice(0, 16) === "a]3;d*F!pk29&bAc") { |
| 170 | + | throw new Error("found it"); |
| 171 | + | } |
| 172 | + |}`, |
| 173 | + [compareHooks], |
| 174 | + ); |
| 175 | + |
| 176 | + expect(code).toContain("Fuzzer.tracer.traceStrCmp"); |
| 177 | + expect(code).toContain("a]3;d*F!pk29&bAc"); |
| 178 | + }); |
| 179 | + |
| 180 | + it("should produce both coverage and hooks together", () => { |
| 181 | + const { code, edgeCount } = transform( |
| 182 | + ` |
| 183 | + |export function check(s) { |
| 184 | + | if (s === "secret") { |
| 185 | + | return true; |
| 186 | + | } |
| 187 | + | return false; |
| 188 | + |}`, |
| 189 | + [compareHooks], |
| 190 | + ); |
| 191 | + |
| 192 | + // Coverage counters from esmCodeCoverage |
| 193 | + expect(code).toContain("__jazzer_cov["); |
| 194 | + expect(edgeCount).toBeGreaterThan(0); |
| 195 | + // Compare hooks |
| 196 | + expect(code).toContain("Fuzzer.tracer.traceStrCmp"); |
| 197 | + }); |
| 198 | + }); |
| 199 | + |
| 200 | + describe("logical expression handling", () => { |
| 201 | + it("should instrument nested logical expressions", () => { |
| 202 | + const { code, edgeCount } = transform(` |
| 203 | + |const x = a || b && c;`); |
| 204 | + |
| 205 | + // Should have instrumented the leaves of the logical tree. |
| 206 | + expect(edgeCount).toBeGreaterThanOrEqual(2); |
| 207 | + expect(code).toContain("__jazzer_cov["); |
| 208 | + }); |
| 209 | + |
| 210 | + it("should not infinite-loop on complex logical chains", () => { |
| 211 | + // This would cause infinite recursion if the counter |
| 212 | + // expression contained || or &&. |
| 213 | + const { code, edgeCount } = transform(` |
| 214 | + |function f() { |
| 215 | + | return a || b || c || d || e; |
| 216 | + |}`); |
| 217 | + |
| 218 | + expect(edgeCount).toBeGreaterThan(0); |
| 219 | + expect(code).toContain("__jazzer_cov["); |
| 220 | + }); |
| 221 | + }); |
112 | 222 | }); |
0 commit comments