cfsolve 0.1.2 → 0.1.4
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +22 -0
- data/README.md +8 -3
- data/THIRD_PARTY_LICENSES/README.md +71 -254
- data/THIRD_PARTY_LICENSES/{derive_builder_macro-0.20.2/LICENSE-APACHE → bytecount-0.6.9/LICENSE.Apache2} +0 -1
- data/THIRD_PARTY_LICENSES/{darling-0.20.11/LICENSE → bytecount-0.6.9/LICENSE.MIT} +1 -3
- data/THIRD_PARTY_LICENSES/{ident_case-1.0.1/LICENSE → cobs-0.3.0/LICENSE-MIT} +1 -1
- data/THIRD_PARTY_LICENSES/cow-utils-0.1.3/LICENSE +21 -0
- data/THIRD_PARTY_LICENSES/{hashbrown-0.14.5 → embedded-io-0.4.0}/LICENSE-MIT +1 -1
- data/THIRD_PARTY_LICENSES/{ahash-0.8.12 → embedded-io-0.6.1}/LICENSE-MIT +1 -1
- data/THIRD_PARTY_LICENSES/{browserslist-data-0.1.4 → escape-simd-0.1.1}/LICENSE +1 -1
- data/THIRD_PARTY_LICENSES/escape-simd-0.1.1/NOTICE +62 -0
- data/THIRD_PARTY_LICENSES/json-escape-simd-3.1.3/LICENSE +21 -0
- data/THIRD_PARTY_LICENSES/{miette-7.6.0/LICENSE → json-escape-simd-3.1.3/LICENSE-APACHE} +0 -1
- data/THIRD_PARTY_LICENSES/json-escape-simd-3.1.3/NOTICE +62 -0
- data/THIRD_PARTY_LICENSES/memchr-2.8.3/UNLICENSE +24 -0
- data/THIRD_PARTY_LICENSES/{derive_builder_core-0.20.2 → nonmax-0.5.5}/LICENSE-APACHE +1 -2
- data/THIRD_PARTY_LICENSES/{dashmap-5.5.3/LICENSE → nonmax-0.5.5/LICENSE-MIT} +2 -4
- data/THIRD_PARTY_LICENSES/{rand-0.8.5 → num-bigint-0.5.1}/LICENSE-MIT +0 -1
- data/THIRD_PARTY_LICENSES/oxc-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/{browserslist-rs-0.19.0 → oxc-browserslist-5.0.1}/LICENSE +1 -0
- data/THIRD_PARTY_LICENSES/oxc_allocator-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_allocator-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_ast-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_ast-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_ast_macros-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_ast_macros-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_ast_visit-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_ast_visit-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_codegen-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_codegen-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_compat-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_compat-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_data_structures-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_data_structures-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_diagnostics-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_diagnostics-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_ecmascript-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_ecmascript-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_estree-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_estree-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_index-5.0.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_parser-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_parser-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_regular_expression-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_regular_expression-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_semantic-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_semantic-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_span-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_span-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_str-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_str-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_syntax-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_syntax-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/{fixedbitset-0.5.7 → postcard-1.1.3}/LICENSE-MIT +1 -1
- data/THIRD_PARTY_LICENSES/{bytes-str-0.2.7/LICENSE → self_cell-1.3.0/LICENSE-APACHE} +1 -2
- data/THIRD_PARTY_LICENSES/self_cell-1.3.0/LICENSE-GPLv2 +339 -0
- data/THIRD_PARTY_LICENSES/{darling_core-0.20.11 → smawk-0.3.3}/LICENSE +1 -1
- data/UPSTREAM.md +14 -6
- data/ext/cfsolve/Cargo.lock +534 -1727
- data/ext/cfsolve/Cargo.toml +4 -0
- data/ext/cfsolve/src/main.rs +1 -1
- data/lib/cfsolve/profiles/chrome-157-macos/answers.json +4777 -0
- data/lib/cfsolve/profiles/chrome-157-macos.json +613 -0
- data/lib/cfsolve/profiles/safari-26-ios/answers.json +266 -265
- data/lib/cfsolve/profiles/safari-26-ios/challenge-page-answers.json +312 -312
- data/lib/cfsolve/profiles/safari-26-ios.json +106 -152
- data/lib/cfsolve/version.rb +2 -2
- data/lib/cfsolve.rb +1 -1
- data/vendor/cfsolve/Cargo.lock +561 -1729
- data/vendor/cfsolve/Cargo.toml +26 -8
- data/vendor/cfsolve/README.md +57 -5
- data/vendor/cfsolve/examples/check_answers.rs +2 -2
- data/vendor/cfsolve/examples/compare_session.rs +1 -2
- data/vendor/cfsolve/examples/solver_profile.rs +84 -0
- data/vendor/cfsolve/examples/solver_smoke.rs +9 -8
- data/vendor/cfsolve/examples/stage2_snapshot.rs +2 -2
- data/vendor/cfsolve/examples/tls_echo.rs +3 -3
- data/vendor/cfsolve/examples/turnstile_smoke.rs +58 -0
- data/vendor/cfsolve/examples/vm_snapshot.rs +32 -33
- data/vendor/cfsolve/profiles/chrome-157-macos/answers.json +4777 -0
- data/vendor/cfsolve/profiles/chrome-157-macos.json +613 -0
- data/vendor/cfsolve/profiles/safari-26-ios/answers.json +266 -265
- data/vendor/cfsolve/profiles/safari-26-ios/challenge-page-answers.json +312 -312
- data/vendor/cfsolve/profiles/safari-26-ios.json +106 -152
- data/vendor/cfsolve/src/bin/capture-proxy/main.rs +3 -2
- data/vendor/cfsolve/src/bin/challenge/cli.rs +8 -2
- data/vendor/cfsolve/src/bin/challenge/main.rs +7 -2
- data/vendor/cfsolve/src/capture/proxy/tests.rs +1 -1
- data/vendor/cfsolve/src/capture/proxy.rs +44 -13
- data/vendor/cfsolve/src/capture/sessions.rs +4 -3
- data/vendor/cfsolve/src/capture/upstream.rs +1 -1
- data/vendor/cfsolve/src/challenge/codec.rs +4 -2
- data/vendor/cfsolve/src/challenge/expression.rs +161 -117
- data/vendor/cfsolve/src/challenge/installed/audit.rs +11 -10
- data/vendor/cfsolve/src/challenge/installed.rs +13 -6
- data/vendor/cfsolve/src/challenge/merge.rs +120 -80
- data/vendor/cfsolve/src/challenge/orchestrator.rs +57 -44
- data/vendor/cfsolve/src/challenge/payload.rs +69 -31
- data/vendor/cfsolve/src/challenge/program.rs +197 -178
- data/vendor/cfsolve/src/challenge/reduced_timing.rs +55 -40
- data/vendor/cfsolve/src/challenge/source.rs +151 -96
- data/vendor/cfsolve/src/challenge/state.rs +180 -148
- data/vendor/cfsolve/src/challenge/tally.rs +64 -50
- data/vendor/cfsolve/src/challenge.rs +1 -0
- data/vendor/cfsolve/src/deobfuscate/analysis/bindings.rs +187 -185
- data/vendor/cfsolve/src/deobfuscate/analysis/effects.rs +42 -51
- data/vendor/cfsolve/src/deobfuscate/analysis/hoisting.rs +147 -228
- data/vendor/cfsolve/src/deobfuscate/analysis/properties.rs +250 -425
- data/vendor/cfsolve/src/deobfuscate/analysis/unconditional.rs +79 -0
- data/vendor/cfsolve/src/deobfuscate/analysis.rs +259 -41
- data/vendor/cfsolve/src/deobfuscate/argument_arrays.rs +70 -73
- data/vendor/cfsolve/src/deobfuscate/arrow_blocks.rs +91 -104
- data/vendor/cfsolve/src/deobfuscate/ast.rs +383 -0
- data/vendor/cfsolve/src/deobfuscate/bitwise.rs +44 -54
- data/vendor/cfsolve/src/deobfuscate/blocks.rs +36 -29
- data/vendor/cfsolve/src/deobfuscate/call_helpers.rs +80 -131
- data/vendor/cfsolve/src/deobfuscate/control_flow.rs +239 -297
- data/vendor/cfsolve/src/deobfuscate/copy_propagation.rs +114 -135
- data/vendor/cfsolve/src/deobfuscate/dead_branches.rs +62 -61
- data/vendor/cfsolve/src/deobfuscate/dead_properties.rs +197 -214
- data/vendor/cfsolve/src/deobfuscate/dispatch.rs +238 -298
- data/vendor/cfsolve/src/deobfuscate/encoded_strings.rs +198 -203
- data/vendor/cfsolve/src/deobfuscate/function_lengths.rs +32 -38
- data/vendor/cfsolve/src/deobfuscate/helper_objects.rs +338 -513
- data/vendor/cfsolve/src/deobfuscate/hygiene.rs +197 -0
- data/vendor/cfsolve/src/deobfuscate/int32_helpers.rs +121 -155
- data/vendor/cfsolve/src/deobfuscate/literals.rs +116 -67
- data/vendor/cfsolve/src/deobfuscate/loops.rs +43 -43
- data/vendor/cfsolve/src/deobfuscate/memoized_strings.rs +127 -131
- data/vendor/cfsolve/src/deobfuscate/merged_functions.rs +262 -220
- data/vendor/cfsolve/src/deobfuscate/pipeline.rs +199 -244
- data/vendor/cfsolve/src/deobfuscate/register_arrays.rs +353 -327
- data/vendor/cfsolve/src/deobfuscate/removal.rs +83 -52
- data/vendor/cfsolve/src/deobfuscate/simplify.rs +147 -0
- data/vendor/cfsolve/src/deobfuscate/state_machines.rs +88 -110
- data/vendor/cfsolve/src/deobfuscate/statements.rs +228 -228
- data/vendor/cfsolve/src/deobfuscate/static_members.rs +46 -31
- data/vendor/cfsolve/src/deobfuscate/testing.rs +49 -59
- data/vendor/cfsolve/src/deobfuscate/unused_bindings.rs +92 -102
- data/vendor/cfsolve/src/deobfuscate.rs +15 -11
- data/vendor/cfsolve/src/embedding_page/extra_params.rs +132 -24
- data/vendor/cfsolve/src/embedding_page/page.rs +41 -1
- data/vendor/cfsolve/src/embedding_page/render_sample.rs +184 -147
- data/vendor/cfsolve/src/error.rs +40 -5
- data/vendor/cfsolve/src/fingerprint.rs +8 -2
- data/vendor/cfsolve/src/http/client.rs +7 -10
- data/vendor/cfsolve/src/http/requests.rs +2 -2
- data/vendor/cfsolve/src/js/ast.rs +479 -263
- data/vendor/cfsolve/src/lib.rs +1 -0
- data/vendor/cfsolve/src/listing/fold.rs +19 -9
- data/vendor/cfsolve/src/listing/query.rs +1 -47
- data/vendor/cfsolve/src/performance_clock.rs +20 -1
- data/vendor/cfsolve/src/profiling.rs +49 -0
- data/vendor/cfsolve/src/script.rs +35 -24
- data/vendor/cfsolve/src/session/challenge_page.rs +41 -21
- data/vendor/cfsolve/src/session/channel.rs +2 -2
- data/vendor/cfsolve/src/session/initial.rs +12 -11
- data/vendor/cfsolve/src/session/probes.rs +1 -0
- data/vendor/cfsolve/src/session/sink.rs +26 -1
- data/vendor/cfsolve/src/session/tests/challenge_page.rs +36 -1
- data/vendor/cfsolve/src/session.rs +1 -1
- data/vendor/cfsolve/src/solver.rs +3 -1
- data/vendor/cfsolve/src/stage2/analysis/analyze.rs +33 -4
- data/vendor/cfsolve/src/stage2/analysis/audit.rs +4 -2
- data/vendor/cfsolve/src/stage2/analysis/discovery.rs +13 -2
- data/vendor/cfsolve/src/stage2/analysis/envelope.rs +2 -1
- data/vendor/cfsolve/src/stage2/audit_globals.rs +142 -74
- data/vendor/cfsolve/src/stage2/benchmark.rs +113 -41
- data/vendor/cfsolve/src/stage2/call_stack.rs +1564 -1041
- data/vendor/cfsolve/src/stage2/challenge_page.rs +3 -2
- data/vendor/cfsolve/src/stage2/fixtures/encoded_benchmark_worker.js +1 -0
- data/vendor/cfsolve/src/stage2/interaction/checkbox.rs +202 -150
- data/vendor/cfsolve/src/stage2/interaction/click.rs +2 -2
- data/vendor/cfsolve/src/stage2/interaction/plan.rs +5 -4
- data/vendor/cfsolve/src/stage2/payload/recover.rs +2 -2
- data/vendor/cfsolve/src/stage2/probes/clock.rs +1 -3
- data/vendor/cfsolve/src/stage2/probes/dates.rs +44 -0
- data/vendor/cfsolve/src/stage2/probes/parent_info_array.rs +326 -0
- data/vendor/cfsolve/src/stage2/probes/performance.rs +12 -8
- data/vendor/cfsolve/src/stage2/probes/stack.rs +117 -4
- data/vendor/cfsolve/src/stage2/probes/websocket_echo.rs +80 -2
- data/vendor/cfsolve/src/stage2/probes.rs +1 -0
- data/vendor/cfsolve/src/stage2/queries.rs +50 -0
- data/vendor/cfsolve/src/stage2/reference.rs +5 -0
- data/vendor/cfsolve/src/stage2/registry.rs +8 -2
- data/vendor/cfsolve/src/stage2/replay.rs +34 -3
- data/vendor/cfsolve/src/stage2/stylesheets.rs +35 -0
- data/vendor/cfsolve/src/stage2/syntax.rs +88 -0
- data/vendor/cfsolve/src/stage2/trace.rs +134 -108
- data/vendor/cfsolve/src/stage2/value_labels.rs +185 -0
- data/vendor/cfsolve/src/stage2/vocabulary.rs +112 -17
- data/vendor/cfsolve/src/stage2.rs +3 -0
- data/vendor/cfsolve/src/vm/disassemble.rs +75 -7
- data/vendor/cfsolve/src/vm/dispatch.rs +93 -65
- data/vendor/cfsolve/src/vm/encoded_names.rs +635 -0
- data/vendor/cfsolve/src/vm/extract.rs +550 -250
- data/vendor/cfsolve/src/vm/fixtures/free_functions/README.md +9 -0
- data/vendor/cfsolve/src/vm/fixtures/free_functions/machine.js +7438 -0
- data/vendor/cfsolve/src/vm/fixtures/free_functions/program.base64 +1 -0
- data/vendor/cfsolve/src/vm/interpret/access.rs +71 -50
- data/vendor/cfsolve/src/vm/interpret/builtin.rs +10 -10
- data/vendor/cfsolve/src/vm/interpret/call.rs +116 -52
- data/vendor/cfsolve/src/vm/interpret/compile.rs +79 -62
- data/vendor/cfsolve/src/vm/interpret/copies.rs +2 -2
- data/vendor/cfsolve/src/vm/interpret/evaluate/tests.rs +63 -42
- data/vendor/cfsolve/src/vm/interpret/evaluate.rs +65 -575
- data/vendor/cfsolve/src/vm/interpret/explore.rs +7 -10
- data/vendor/cfsolve/src/vm/interpret/formula.rs +131 -0
- data/vendor/cfsolve/src/vm/interpret/frozen.rs +3 -5
- data/vendor/cfsolve/src/vm/interpret/intrinsic.rs +160 -58
- data/vendor/cfsolve/src/vm/interpret/mix32.rs +189 -0
- data/vendor/cfsolve/src/vm/interpret/native_dispatch.rs +220 -0
- data/vendor/cfsolve/src/vm/interpret/pattern.rs +1765 -0
- data/vendor/cfsolve/src/vm/interpret/plan.rs +586 -338
- data/vendor/cfsolve/src/vm/interpret/register_helpers.rs +392 -0
- data/vendor/cfsolve/src/vm/interpret/run.rs +120 -45
- data/vendor/cfsolve/src/vm/interpret/semantics.rs +71 -46
- data/vendor/cfsolve/src/vm/interpret/setup.rs +80 -45
- data/vendor/cfsolve/src/vm/interpret/string_decoder.rs +482 -0
- data/vendor/cfsolve/src/vm/interpret.rs +9 -1
- data/vendor/cfsolve/src/vm/machine.rs +116 -51
- data/vendor/cfsolve/src/vm/output.rs +19 -6
- data/vendor/cfsolve/src/vm/packed_names.rs +11 -7
- data/vendor/cfsolve/src/vm/registers.rs +35 -21
- data/vendor/cfsolve/src/vm/session.rs +189 -121
- data/vendor/cfsolve/src/vm/value.rs +22 -19
- data/vendor/cfsolve/src/vm.rs +3 -1
- data/vendor/cfsolve/vendor/btls-sys/Cargo.toml +3 -0
- data/vendor/cfsolve/vendor/btls-sys/build/main.rs +20 -0
- metadata +106 -178
- data/THIRD_PARTY_LICENSES/Inflector-0.11.4/LICENSE.md +0 -22
- data/THIRD_PARTY_LICENSES/arrayvec-0.7.6/LICENSE-MIT +0 -25
- data/THIRD_PARTY_LICENSES/ascii-1.1.0/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/ascii-1.1.0/LICENSE-MIT +0 -22
- data/THIRD_PARTY_LICENSES/ast_node-5.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/better_scoped_tls-1.0.1/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/bitvec-1.0.1/LICENSE.txt +0 -21
- data/THIRD_PARTY_LICENSES/bstr-1.12.1/COPYING +0 -8
- data/THIRD_PARTY_LICENSES/bstr-1.12.1/LICENSE-MIT +0 -21
- data/THIRD_PARTY_LICENSES/cargo-platform-0.1.9/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/cargo_metadata-0.19.2/LICENSE-MIT +0 -23
- data/THIRD_PARTY_LICENSES/darling_macro-0.20.11/LICENSE +0 -21
- data/THIRD_PARTY_LICENSES/debugid-0.8.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/derive_builder-0.20.2/LICENSE-MIT +0 -21
- data/THIRD_PARTY_LICENSES/derive_builder_core-0.20.2/LICENSE-MIT +0 -21
- data/THIRD_PARTY_LICENSES/derive_builder_macro-0.20.2/LICENSE-MIT +0 -21
- data/THIRD_PARTY_LICENSES/foldhash-0.1.5/LICENSE +0 -19
- data/THIRD_PARTY_LICENSES/from_variant-3.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/funty-2.0.0/LICENSE.txt +0 -21
- data/THIRD_PARTY_LICENSES/hashbrown-0.16.1/LICENSE-MIT +0 -25
- data/THIRD_PARTY_LICENSES/heck-0.5.0/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/hermit-abi-0.5.2/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/hermit-abi-0.5.2/LICENSE-MIT +0 -23
- data/THIRD_PARTY_LICENSES/hstr-3.0.3/LICENSE +0 -202
- data/THIRD_PARTY_LICENSES/if_chain-1.0.3/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/if_chain-1.0.3/LICENSE-MIT +0 -25
- data/THIRD_PARTY_LICENSES/is-macro-0.3.7/LICENSE +0 -202
- data/THIRD_PARTY_LICENSES/itoa-1.0.17/LICENSE-MIT +0 -23
- data/THIRD_PARTY_LICENSES/jsonc-parser-0.26.3/LICENSE +0 -21
- data/THIRD_PARTY_LICENSES/lazy_static-1.5.0/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/lazy_static-1.5.0/LICENSE-MIT +0 -25
- data/THIRD_PARTY_LICENSES/lru-0.16.2/LICENSE +0 -21
- data/THIRD_PARTY_LICENSES/memchr-2.7.6/COPYING +0 -3
- data/THIRD_PARTY_LICENSES/memchr-2.7.6/LICENSE-MIT +0 -21
- data/THIRD_PARTY_LICENSES/miette-derive-7.6.0/LICENSE +0 -229
- data/THIRD_PARTY_LICENSES/normpath-0.2.0/COPYRIGHT +0 -7
- data/THIRD_PARTY_LICENSES/normpath-0.2.0/LICENSE-MIT +0 -21
- data/THIRD_PARTY_LICENSES/normpath-0.2.0/LICENSE-THIRD-PARTY +0 -210
- data/THIRD_PARTY_LICENSES/num_cpus-1.17.0/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/num_cpus-1.17.0/LICENSE-MIT +0 -20
- data/THIRD_PARTY_LICENSES/owo-colors-4.2.3/LICENSE +0 -21
- data/THIRD_PARTY_LICENSES/par-core-2.0.0/LICENSE +0 -202
- data/THIRD_PARTY_LICENSES/par-iter-2.0.0/LICENSE +0 -202
- data/THIRD_PARTY_LICENSES/path-clean-0.1.0/LICENSE-APACHE +0 -13
- data/THIRD_PARTY_LICENSES/path-clean-0.1.0/LICENSE-MIT +0 -18
- data/THIRD_PARTY_LICENSES/path-clean-1.0.1/LICENSE-APACHE +0 -13
- data/THIRD_PARTY_LICENSES/path-clean-1.0.1/LICENSE-MIT +0 -18
- data/THIRD_PARTY_LICENSES/pathdiff-0.2.3/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/pathdiff-0.2.3/LICENSE-MIT +0 -23
- data/THIRD_PARTY_LICENSES/petgraph-0.7.1/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/precomputed-map-0.2.0/LICENSE +0 -21
- data/THIRD_PARTY_LICENSES/preset_env_base-6.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/radium-0.7.0/LICENSE.txt +0 -21
- data/THIRD_PARTY_LICENSES/radix_fmt-1.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/rand-0.8.5/COPYRIGHT +0 -12
- data/THIRD_PARTY_LICENSES/rand-0.8.5/LICENSE-APACHE +0 -176
- data/THIRD_PARTY_LICENSES/rand_core-0.6.4/COPYRIGHT +0 -12
- data/THIRD_PARTY_LICENSES/rand_core-0.6.4/LICENSE-APACHE +0 -187
- data/THIRD_PARTY_LICENSES/rand_core-0.6.4/LICENSE-MIT +0 -26
- data/THIRD_PARTY_LICENSES/regress-0.10.5/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/regress-0.10.5/LICENSE-MIT +0 -21
- data/THIRD_PARTY_LICENSES/ryu-1.0.22/LICENSE-APACHE +0 -176
- data/THIRD_PARTY_LICENSES/ryu-1.0.22/LICENSE-BOOST +0 -23
- data/THIRD_PARTY_LICENSES/scoped-tls-1.0.1/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/scoped-tls-1.0.1/LICENSE-MIT +0 -25
- data/THIRD_PARTY_LICENSES/siphasher-0.3.11/COPYING +0 -7
- data/THIRD_PARTY_LICENSES/smallvec-1.15.1/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/smartstring-1.0.1/LICENCE.md +0 -355
- data/THIRD_PARTY_LICENSES/st-map-0.2.4/LICENSE +0 -202
- data/THIRD_PARTY_LICENSES/static-map-macro-0.3.4/LICENSE +0 -202
- data/THIRD_PARTY_LICENSES/string_enum-1.0.2/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/strsim-0.11.1/LICENSE +0 -23
- data/THIRD_PARTY_LICENSES/swc-52.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_allocator-4.0.1/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_atoms-9.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_common-18.0.1/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_compiler_base-45.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_config-3.1.2/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_config_macro-1.0.1/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_core-54.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_ast-19.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_codegen-21.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_codegen_macros-2.0.2/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_compat_bugfixes-39.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_compat_common-30.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2015-39.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2016-35.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2017-35.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2018-36.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2019-35.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2020-37.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2021-35.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2022-37.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_compat_es3-26.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_ext_transforms-25.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_hooks-0.3.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_loader-18.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_minifier-42.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_parser-32.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_preset_env-45.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_transformer-6.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_transforms-44.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_transforms_base-35.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_transforms_classes-35.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_transforms_compat-40.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_transforms_macros-1.0.1/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_transforms_module-38.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_transforms_optimization-37.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_transforms_proposal-35.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_transforms_react-38.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_transforms_typescript-38.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_usage_analyzer-27.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_utils-25.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_visit-19.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_eq_ignore_macros-1.0.1/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_error_reporters-20.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_macros_common-1.0.1/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_node_comments-18.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_timer-1.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_trace_macro-2.0.2/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_transform_common-12.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_visit-2.0.1/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/tap-1.0.1/LICENSE.txt +0 -21
- data/THIRD_PARTY_LICENSES/tracing-0.1.44/LICENSE +0 -25
- data/THIRD_PARTY_LICENSES/tracing-attributes-0.1.31/LICENSE +0 -25
- data/THIRD_PARTY_LICENSES/tracing-core-0.1.36/LICENSE +0 -25
- data/THIRD_PARTY_LICENSES/triomphe-0.1.15/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/triomphe-0.1.15/LICENSE-MIT +0 -27
- data/THIRD_PARTY_LICENSES/unicode-width-0.1.14/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/unicode-width-0.1.14/LICENSE-MIT +0 -25
- data/THIRD_PARTY_LICENSES/uuid-1.19.0/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/uuid-1.19.0/LICENSE-MIT +0 -26
- data/THIRD_PARTY_LICENSES/vergen-9.0.6/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/vergen-9.0.6/LICENSE-MIT +0 -25
- data/THIRD_PARTY_LICENSES/vergen-lib-0.1.6/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/vergen-lib-0.1.6/LICENSE-MIT +0 -25
- data/THIRD_PARTY_LICENSES/wyz-0.5.1/LICENSE.txt +0 -21
- data/vendor/cfsolve/src/deobfuscate/analysis/globals.rs +0 -33
- data/vendor/cfsolve/src/deobfuscate/analysis/names.rs +0 -52
- /data/THIRD_PARTY_LICENSES/{derive_builder-0.20.2 → cobs-0.3.0}/LICENSE-APACHE +0 -0
- /data/THIRD_PARTY_LICENSES/{compact_str-0.7.1 → compact_str-0.10.0}/LICENSE +0 -0
- /data/THIRD_PARTY_LICENSES/{ahash-0.8.12 → embedded-io-0.4.0}/LICENSE-APACHE +0 -0
- /data/THIRD_PARTY_LICENSES/{arrayvec-0.7.6 → embedded-io-0.6.1}/LICENSE-APACHE +0 -0
- /data/THIRD_PARTY_LICENSES/{normpath-0.2.0 → escape-simd-0.1.1}/LICENSE-APACHE +0 -0
- /data/THIRD_PARTY_LICENSES/{bstr-1.12.1 → itertools-0.15.0}/LICENSE-APACHE +0 -0
- /data/THIRD_PARTY_LICENSES/{petgraph-0.7.1 → itertools-0.15.0}/LICENSE-MIT +0 -0
- /data/THIRD_PARTY_LICENSES/{itoa-1.0.17 → itoa-1.0.19}/LICENSE-APACHE +0 -0
- /data/THIRD_PARTY_LICENSES/{camino-1.2.2 → itoa-1.0.19}/LICENSE-MIT +0 -0
- /data/THIRD_PARTY_LICENSES/{globset-0.4.18 → memchr-2.8.3}/COPYING +0 -0
- /data/THIRD_PARTY_LICENSES/{globset-0.4.18 → memchr-2.8.3}/LICENSE-MIT +0 -0
- /data/THIRD_PARTY_LICENSES/{camino-1.2.2 → num-bigint-0.5.1}/LICENSE-APACHE +0 -0
- /data/THIRD_PARTY_LICENSES/{swc_sourcemap-9.3.4 → oxc_sourcemap-9.0.0}/LICENSE +0 -0
- /data/THIRD_PARTY_LICENSES/{phf-0.11.3 → phf-0.14.0}/LICENSE +0 -0
- /data/THIRD_PARTY_LICENSES/{phf_generator-0.11.3 → phf_generator-0.14.0}/LICENSE +0 -0
- /data/THIRD_PARTY_LICENSES/{phf_macros-0.11.3 → phf_macros-0.14.0}/LICENSE +0 -0
- /data/THIRD_PARTY_LICENSES/{phf_shared-0.11.3 → phf_shared-0.14.0}/LICENSE +0 -0
- /data/THIRD_PARTY_LICENSES/{fixedbitset-0.5.7 → postcard-1.1.3}/LICENSE-APACHE +0 -0
- /data/THIRD_PARTY_LICENSES/{hashbrown-0.14.5 → smallvec-1.16.5}/LICENSE-APACHE +0 -0
- /data/THIRD_PARTY_LICENSES/{smallvec-1.15.1 → smallvec-1.16.5}/LICENSE-MIT +0 -0
- /data/THIRD_PARTY_LICENSES/{unicode-width-0.1.14 → unicode-segmentation-1.13.3}/COPYRIGHT +0 -0
- /data/THIRD_PARTY_LICENSES/{hashbrown-0.16.1 → unicode-segmentation-1.13.3}/LICENSE-APACHE +0 -0
- /data/THIRD_PARTY_LICENSES/{heck-0.5.0 → unicode-segmentation-1.13.3}/LICENSE-MIT +0 -0
- /data/THIRD_PARTY_LICENSES/{cargo-platform-0.1.9 → zmij_ecma-1.0.23}/LICENSE-MIT +0 -0
|
@@ -14,61 +14,68 @@
|
|
|
14
14
|
|
|
15
15
|
use std::collections::{HashMap, HashSet};
|
|
16
16
|
use std::mem;
|
|
17
|
+
use std::sync::Arc;
|
|
17
18
|
|
|
18
19
|
use anyhow::{Context, Result, bail};
|
|
19
|
-
use
|
|
20
|
-
use
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
use oxc::ast::AstKind;
|
|
21
|
+
use oxc::ast::ast::{
|
|
22
|
+
Argument, ArrayExpression, ArrayExpressionElement, AssignmentExpression, AssignmentTarget,
|
|
23
|
+
BindingPattern, CallExpression, Class, ClassElement, Expression, FormalParameter, Function,
|
|
24
|
+
MemberExpression, MethodDefinition, MethodDefinitionKind, NewExpression, ObjectExpression,
|
|
25
|
+
ObjectProperty, ObjectPropertyKind, PropertyKind, Statement, SwitchCase, SwitchStatement,
|
|
26
|
+
ThrowStatement, TryStatement, VariableDeclarator,
|
|
26
27
|
};
|
|
27
|
-
use
|
|
28
|
+
use oxc::ast_visit::{Visit, walk};
|
|
29
|
+
use oxc::semantic::SymbolId;
|
|
30
|
+
use oxc::span::{GetSpan, Span};
|
|
31
|
+
use oxc::syntax::operator::{AssignmentOperator, BinaryOperator, UnaryOperator};
|
|
32
|
+
use oxc::syntax::scope::ScopeFlags;
|
|
33
|
+
use serde::{Deserialize, Serialize};
|
|
28
34
|
|
|
35
|
+
use super::syntax::{
|
|
36
|
+
assigned_member, is_optional_call, member, plain_parameter, walk_method, walk_property,
|
|
37
|
+
};
|
|
29
38
|
use crate::deobfuscate::{self, Deobfuscated};
|
|
30
39
|
use crate::engine::{self, Engine, Lines, StackFrame, V8_STACK_LIMIT};
|
|
31
|
-
use crate::js::ast;
|
|
40
|
+
use crate::js::ast::{self, Binding, ParsedScript};
|
|
32
41
|
use crate::{candidates, js};
|
|
33
42
|
|
|
34
43
|
/// Maps VM dispatch cases to opcodes at their original source positions. Deobfuscation resolves
|
|
35
44
|
/// case labels while preserving spans. Recovery uses the resolved labels to find each opcode's
|
|
36
45
|
/// case in the original script.
|
|
37
|
-
#[derive(Clone,
|
|
38
|
-
pub struct CaseOpcodes
|
|
39
|
-
|
|
40
|
-
///
|
|
41
|
-
|
|
42
|
-
|
|
46
|
+
#[derive(Clone, Default)]
|
|
47
|
+
pub struct CaseOpcodes {
|
|
48
|
+
opcodes: HashMap<Span, u8>,
|
|
49
|
+
/// The decoded program captured during deobfuscation, with the source it was decoded from,
|
|
50
|
+
/// shared so the cases stay cheap to clone.
|
|
51
|
+
decoded: Option<Arc<ParsedScript>>,
|
|
43
52
|
}
|
|
44
53
|
|
|
45
54
|
/// Deobfuscates `source` and maps its dispatch cases' original positions to their opcodes.
|
|
46
55
|
pub fn deobfuscate_with_cases(source: String) -> Result<(Deobfuscated, CaseOpcodes)> {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
impl Visit for Cases {
|
|
52
|
-
fn visit_switch_case(&mut self, case: &SwitchCase) {
|
|
56
|
+
// Spans are stable across parses of the same script.
|
|
57
|
+
struct Cases(HashMap<Span, u8>);
|
|
58
|
+
impl<'a> Visit<'a> for Cases {
|
|
59
|
+
fn visit_switch_case(&mut self, case: &SwitchCase<'a>) {
|
|
53
60
|
if let Some(test) = &case.test
|
|
54
61
|
&& let Some(opcode) = ast::index_literal(test)
|
|
55
62
|
&& let Ok(opcode) = u8::try_from(opcode)
|
|
56
63
|
{
|
|
57
|
-
self.
|
|
58
|
-
.0
|
|
59
|
-
.insert(offsets(case.span, self.start), opcode);
|
|
64
|
+
self.0.insert(case.span, opcode);
|
|
60
65
|
}
|
|
61
|
-
|
|
66
|
+
walk::walk_switch_case(self, case);
|
|
62
67
|
}
|
|
63
68
|
}
|
|
64
|
-
deobfuscate::
|
|
65
|
-
let mut cases = Cases
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
69
|
+
let (deobfuscated, opcodes, decoded) = deobfuscate::deobfuscate_capturing(source, |program| {
|
|
70
|
+
let mut cases = Cases(HashMap::new());
|
|
71
|
+
cases.visit_program(program);
|
|
72
|
+
Ok(cases.0)
|
|
73
|
+
})?;
|
|
74
|
+
let cases = CaseOpcodes {
|
|
75
|
+
opcodes,
|
|
76
|
+
decoded: Some(Arc::new(decoded)),
|
|
77
|
+
};
|
|
78
|
+
Ok((deobfuscated, cases))
|
|
72
79
|
}
|
|
73
80
|
|
|
74
81
|
/// Engine-specific function name and call position, measured from the script start in
|
|
@@ -246,71 +253,79 @@ pub struct DispatchSummary {
|
|
|
246
253
|
|
|
247
254
|
/// Summarizes dispatch loops called by `run`, including whether each case resolves to a function.
|
|
248
255
|
pub fn dispatch_summary(source: &str, cases: &CaseOpcodes) -> Result<Vec<DispatchSummary>> {
|
|
249
|
-
deobfuscate::
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
if !resolved {
|
|
269
|
-
unresolved.push(opcode);
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
let frames = dispatch_loop.call.frames(Name::same("run".to_owned()));
|
|
273
|
-
summaries.push(DispatchSummary {
|
|
274
|
-
function: dispatch_loop.name.0.to_string(),
|
|
275
|
-
guarded: dispatch_loop.guarded,
|
|
276
|
-
frames: frames.into_iter().map(|call| call.name.v8).collect(),
|
|
277
|
-
opcodes,
|
|
278
|
-
unresolved,
|
|
256
|
+
let decoded = deobfuscate::decode(source.to_owned())?;
|
|
257
|
+
let machine = Machine::new(decoded.script(), cases)?;
|
|
258
|
+
let script = &machine.script;
|
|
259
|
+
let mut summaries = Vec::new();
|
|
260
|
+
for dispatch_loop in &machine.loops {
|
|
261
|
+
let name = script.appliers.name(dispatch_loop.name);
|
|
262
|
+
let function = script.function(dispatch_loop.name)?;
|
|
263
|
+
let scope = Scope::new(name, &function, &script.appliers);
|
|
264
|
+
let mut opcodes = 0;
|
|
265
|
+
let mut unresolved = Vec::new();
|
|
266
|
+
for case in cases_of(script.js(), &function) {
|
|
267
|
+
let Some(&opcode) = cases.opcodes.get(&case.span) else {
|
|
268
|
+
continue;
|
|
269
|
+
};
|
|
270
|
+
opcodes += 1;
|
|
271
|
+
let resolved = scope.target(case.call).is_ok_and(|target| {
|
|
272
|
+
target
|
|
273
|
+
.callee
|
|
274
|
+
.is_some_and(|callee| script.function(callee).is_ok())
|
|
279
275
|
});
|
|
276
|
+
if !resolved {
|
|
277
|
+
unresolved.push(opcode);
|
|
278
|
+
}
|
|
280
279
|
}
|
|
281
|
-
|
|
282
|
-
|
|
280
|
+
let frames = dispatch_loop.call.frames(Name::same("run".to_owned()));
|
|
281
|
+
summaries.push(DispatchSummary {
|
|
282
|
+
function: name.to_owned(),
|
|
283
|
+
guarded: dispatch_loop.guarded,
|
|
284
|
+
frames: frames.into_iter().map(|call| call.name.v8).collect(),
|
|
285
|
+
opcodes,
|
|
286
|
+
unresolved,
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
Ok(summaries)
|
|
283
290
|
}
|
|
284
291
|
|
|
285
292
|
/// Parses `source` and places the frames `recover` finds in it as each engine reports them.
|
|
286
293
|
fn geometry(
|
|
287
294
|
source: &str,
|
|
288
295
|
cases: &CaseOpcodes,
|
|
289
|
-
|
|
296
|
+
recover: impl FnOnce(&Machine<'_, '_>) -> Result<Chain>,
|
|
290
297
|
) -> Result<Geometry> {
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
let prefix = source.get(..offset).context("invalid source byte offset")?;
|
|
296
|
-
let (line, column) = engine::line_and_column(prefix, Lines::Script);
|
|
297
|
-
Ok(Frame {
|
|
298
|
-
function,
|
|
299
|
-
line,
|
|
300
|
-
column,
|
|
301
|
-
})
|
|
302
|
-
};
|
|
303
|
-
let mut geometry = Geometry {
|
|
304
|
-
frames: Vec::new(),
|
|
305
|
-
jsc_frames: Vec::new(),
|
|
306
|
-
reflected,
|
|
307
|
-
};
|
|
308
|
-
for Call { name, site } in frames {
|
|
309
|
-
geometry.frames.push(frame(name.v8, site.v8)?);
|
|
310
|
-
geometry.jsc_frames.push(frame(name.jsc, site.jsc)?);
|
|
298
|
+
// Reuse the decoded program `cases` captured if it comes from the same source.
|
|
299
|
+
let Chain { frames, reflected } = match &cases.decoded {
|
|
300
|
+
Some(decoded) if decoded.script().source == source => {
|
|
301
|
+
recover(&Machine::new(decoded.script(), cases)?)?
|
|
311
302
|
}
|
|
312
|
-
|
|
313
|
-
|
|
303
|
+
_ => recover(&Machine::new(
|
|
304
|
+
deobfuscate::decode(source.to_owned())?.script(),
|
|
305
|
+
cases,
|
|
306
|
+
)?)?,
|
|
307
|
+
};
|
|
308
|
+
let frame = |function: String, at: u32| -> Result<Frame> {
|
|
309
|
+
let prefix = source
|
|
310
|
+
.get(..at as usize)
|
|
311
|
+
.context("invalid source byte offset")?;
|
|
312
|
+
let (line, column) = engine::line_and_column(prefix, Lines::Script);
|
|
313
|
+
Ok(Frame {
|
|
314
|
+
function,
|
|
315
|
+
line,
|
|
316
|
+
column,
|
|
317
|
+
})
|
|
318
|
+
};
|
|
319
|
+
let mut geometry = Geometry {
|
|
320
|
+
frames: Vec::new(),
|
|
321
|
+
jsc_frames: Vec::new(),
|
|
322
|
+
reflected,
|
|
323
|
+
};
|
|
324
|
+
for Call { name, site } in frames {
|
|
325
|
+
geometry.frames.push(frame(name.v8, site.v8)?);
|
|
326
|
+
geometry.jsc_frames.push(frame(name.jsc, site.jsc)?);
|
|
327
|
+
}
|
|
328
|
+
Ok(geometry)
|
|
314
329
|
}
|
|
315
330
|
|
|
316
331
|
/// Calls recovered from the innermost frame outward.
|
|
@@ -344,15 +359,16 @@ impl Name {
|
|
|
344
359
|
}
|
|
345
360
|
}
|
|
346
361
|
|
|
347
|
-
/// A call site's
|
|
362
|
+
/// A call site's byte offset for each engine: see [`call_position`] and
|
|
363
|
+
/// [`arguments_position`].
|
|
348
364
|
#[derive(Clone, Copy, Debug)]
|
|
349
365
|
struct Site {
|
|
350
|
-
v8:
|
|
351
|
-
jsc:
|
|
366
|
+
v8: u32,
|
|
367
|
+
jsc: u32,
|
|
352
368
|
}
|
|
353
369
|
|
|
354
370
|
impl Site {
|
|
355
|
-
fn new(call: &
|
|
371
|
+
fn new(call: &CallExpression<'_>) -> Self {
|
|
356
372
|
Self {
|
|
357
373
|
v8: call_position(call),
|
|
358
374
|
jsc: arguments_position(call),
|
|
@@ -366,91 +382,144 @@ struct Call {
|
|
|
366
382
|
site: Site,
|
|
367
383
|
}
|
|
368
384
|
|
|
385
|
+
/// A function that call resolution identifies.
|
|
386
|
+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
|
387
|
+
enum Callee<'a> {
|
|
388
|
+
/// A function or value held by a binding, or by a global.
|
|
389
|
+
Binding(Binding<'a>),
|
|
390
|
+
/// An original function extracted from a merged function, indexed into
|
|
391
|
+
/// [`Appliers::originals`].
|
|
392
|
+
Original(usize),
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
impl Callee<'_> {
|
|
396
|
+
/// The parameter or local `symbol`.
|
|
397
|
+
fn symbol(symbol: SymbolId) -> Self {
|
|
398
|
+
Self::Binding(Binding::Symbol(symbol))
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
369
402
|
/// The VM dispatch method, its runner, and listeners that wrap the runner for each closure
|
|
370
403
|
/// parameter count.
|
|
371
|
-
struct Machine<'
|
|
372
|
-
script: Script
|
|
373
|
-
dispatch: DispatchMethod
|
|
404
|
+
struct Machine<'a, 'cases> {
|
|
405
|
+
script: Script<'a>,
|
|
406
|
+
dispatch: DispatchMethod<'a>,
|
|
374
407
|
/// The dispatch loops `run` calls.
|
|
375
|
-
loops: Vec<DispatchLoop
|
|
376
|
-
cases: &'
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
listeners: Vec<Invocation>,
|
|
408
|
+
loops: Vec<DispatchLoop<'a>>,
|
|
409
|
+
cases: &'cases CaseOpcodes,
|
|
410
|
+
runner: Vec<Call>,
|
|
411
|
+
listeners: Vec<Invocation<'a>>,
|
|
380
412
|
}
|
|
381
413
|
|
|
382
|
-
impl<'
|
|
383
|
-
fn new(
|
|
384
|
-
let mut script = Script::new(
|
|
414
|
+
impl<'a, 'cases> Machine<'a, 'cases> {
|
|
415
|
+
fn new(js: ast::Script<'a>, cases: &'cases CaseOpcodes) -> Result<Self> {
|
|
416
|
+
let mut script = Script::new(js);
|
|
385
417
|
let classes = mem::take(&mut script.classes);
|
|
386
418
|
// The runner passes the closure's program counter, key, and arguments to `run`,
|
|
387
419
|
// copying arguments when required by the VM function.
|
|
388
420
|
let mut runners = Vec::new();
|
|
389
421
|
for (name, function) in &script.functions {
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
422
|
+
let scope = Scope::new(js.name(*name), function, &script.appliers);
|
|
423
|
+
for call in calls_of(js, function) {
|
|
424
|
+
if !call.arguments.last().is_some_and(|argument| {
|
|
425
|
+
matches!(
|
|
426
|
+
ast::argument_expression(argument),
|
|
427
|
+
Expression::ConditionalExpression(_)
|
|
428
|
+
)
|
|
429
|
+
}) {
|
|
430
|
+
continue;
|
|
431
|
+
}
|
|
432
|
+
let calls_run = member(&call.callee)
|
|
433
|
+
.is_some_and(|member| ast::member_str(member) == Some("run"));
|
|
434
|
+
if calls_run && call.arguments.len() == 3 {
|
|
435
|
+
runners.push((*name, Invocation::at(Site::new(call)), None));
|
|
436
|
+
} else if let Ok(invocation) = scope.target(call)
|
|
437
|
+
&& invocation.passed == Some(4)
|
|
438
|
+
&& let Some(callee) = invocation.callee
|
|
439
|
+
&& let Ok(run) = script.function(callee)
|
|
440
|
+
&& let Some(machine) =
|
|
441
|
+
run.parameters().and_then(|params| params.first().copied())
|
|
442
|
+
{
|
|
443
|
+
let loops = dispatch_loops(&script, &run, Some(machine));
|
|
444
|
+
if !loops.is_empty() {
|
|
445
|
+
runners.push((
|
|
446
|
+
*name,
|
|
447
|
+
invocation,
|
|
448
|
+
Some((
|
|
449
|
+
DispatchMethod {
|
|
450
|
+
class: None,
|
|
451
|
+
run,
|
|
452
|
+
frame: Name::same(script.appliers.name(callee).to_owned()),
|
|
453
|
+
},
|
|
454
|
+
loops,
|
|
455
|
+
)),
|
|
456
|
+
));
|
|
457
|
+
}
|
|
398
458
|
}
|
|
399
459
|
}
|
|
400
460
|
}
|
|
401
|
-
let (runner, run_call) =
|
|
461
|
+
let (runner, run_call, free_dispatch) =
|
|
462
|
+
candidates::exactly_one(runners, "closure-to-VM runner call")?;
|
|
463
|
+
let runner = Callee::Binding(runner);
|
|
402
464
|
let mut listeners = Listeners {
|
|
403
465
|
runner,
|
|
404
466
|
appliers: &script.appliers,
|
|
405
467
|
found: Vec::new(),
|
|
406
468
|
};
|
|
407
|
-
|
|
469
|
+
listeners.visit_program(js.program);
|
|
408
470
|
let listeners: Vec<Invocation> = listeners.found.into_iter().collect::<Result<_>>()?;
|
|
409
|
-
let
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
471
|
+
let (dispatch, loops) = match free_dispatch {
|
|
472
|
+
Some(dispatch) => dispatch,
|
|
473
|
+
None => {
|
|
474
|
+
let dispatches = classes.into_iter().filter_map(|dispatch| {
|
|
475
|
+
let loops = dispatch_loops(&script, &dispatch.run, None);
|
|
476
|
+
(!loops.is_empty()).then_some((dispatch, loops))
|
|
477
|
+
});
|
|
478
|
+
candidates::exactly_one(dispatches, "VM dispatch method")?
|
|
479
|
+
}
|
|
480
|
+
};
|
|
414
481
|
Ok(Self {
|
|
415
|
-
runner:
|
|
416
|
-
name: Name::same(runner.0.to_string()),
|
|
417
|
-
site: Site::new(&run_call),
|
|
418
|
-
},
|
|
482
|
+
runner: run_call.frames(Name::same(script.appliers.name(runner).to_owned())),
|
|
419
483
|
listeners,
|
|
420
484
|
dispatch,
|
|
421
485
|
loops,
|
|
422
486
|
script,
|
|
423
487
|
cases,
|
|
424
|
-
start,
|
|
425
488
|
})
|
|
426
489
|
}
|
|
427
490
|
|
|
428
491
|
/// Finds the handler for `opcode` in the dispatch loop with or without exception handling.
|
|
429
|
-
fn handler(&self, opcode: u8, guarded: bool) -> Result<Handler<'_>> {
|
|
492
|
+
fn handler(&self, opcode: u8, guarded: bool) -> Result<Handler<'a, '_>> {
|
|
493
|
+
let appliers = &self.script.appliers;
|
|
430
494
|
for dispatch_loop in &self.loops {
|
|
431
495
|
if dispatch_loop.guarded != guarded {
|
|
432
496
|
continue;
|
|
433
497
|
}
|
|
434
|
-
let function = self.script.function(
|
|
435
|
-
let Some(case) = cases_of(function)
|
|
498
|
+
let function = self.script.function(dispatch_loop.name)?;
|
|
499
|
+
let Some(case) = cases_of(self.script.js(), &function)
|
|
436
500
|
.into_iter()
|
|
437
|
-
.find(|case| self.cases.
|
|
501
|
+
.find(|case| self.cases.opcodes.get(&case.span) == Some(&opcode))
|
|
438
502
|
else {
|
|
439
503
|
continue;
|
|
440
504
|
};
|
|
441
|
-
let scope = Scope::new(
|
|
442
|
-
let dispatch = scope.target(
|
|
505
|
+
let scope = Scope::new(appliers.name(dispatch_loop.name), &function, appliers);
|
|
506
|
+
let dispatch = scope.target(case.call)?;
|
|
443
507
|
let handler = dispatch
|
|
444
508
|
.callee
|
|
445
|
-
.clone()
|
|
446
509
|
.with_context(|| format!("opcode {opcode}'s dispatch calls no named handler"))?;
|
|
510
|
+
let name = appliers.name(handler);
|
|
447
511
|
let frame = Name {
|
|
448
512
|
v8: if dispatch.method {
|
|
449
|
-
format!(
|
|
513
|
+
format!(
|
|
514
|
+
"{}.{name}",
|
|
515
|
+
self.dispatch
|
|
516
|
+
.class
|
|
517
|
+
.context("missing VM class for method handler")?
|
|
518
|
+
)
|
|
450
519
|
} else {
|
|
451
|
-
|
|
520
|
+
name.to_owned()
|
|
452
521
|
},
|
|
453
|
-
jsc:
|
|
522
|
+
jsc: name.to_owned(),
|
|
454
523
|
};
|
|
455
524
|
return Ok(Handler {
|
|
456
525
|
dispatch,
|
|
@@ -463,19 +532,12 @@ impl<'script> Machine<'script> {
|
|
|
463
532
|
}
|
|
464
533
|
|
|
465
534
|
/// Appends frames from the call to `handler` through the dispatch loop, `run`, and the runner.
|
|
466
|
-
fn entered(&self, handler: &Handler<'_>, mut frames: Vec<Call>) -> Vec<Call> {
|
|
467
|
-
let DispatchMethod { class, key, .. } = &self.dispatch;
|
|
535
|
+
fn entered(&self, handler: &Handler<'a, '_>, mut frames: Vec<Call>) -> Vec<Call> {
|
|
468
536
|
let dispatch_loop = handler.dispatch_loop;
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
);
|
|
474
|
-
frames.extend(dispatch_loop.call.frames(Name {
|
|
475
|
-
v8: format!("{class}.run"),
|
|
476
|
-
jsc: key.clone().unwrap_or_default(),
|
|
477
|
-
}));
|
|
478
|
-
frames.push(self.runner.clone());
|
|
537
|
+
let name = self.script.appliers.name(dispatch_loop.name);
|
|
538
|
+
frames.extend(handler.dispatch.frames(Name::same(name.to_owned())));
|
|
539
|
+
frames.extend(dispatch_loop.call.frames(self.dispatch.frame.clone()));
|
|
540
|
+
frames.extend(self.runner.iter().cloned());
|
|
479
541
|
frames
|
|
480
542
|
}
|
|
481
543
|
|
|
@@ -484,21 +546,20 @@ impl<'script> Machine<'script> {
|
|
|
484
546
|
/// arguments.
|
|
485
547
|
fn construction(&self, opcode: u8, guarded: bool) -> Result<Chain> {
|
|
486
548
|
let handler = self.handler(opcode, guarded)?;
|
|
487
|
-
let
|
|
488
|
-
let
|
|
549
|
+
let appliers = &self.script.appliers;
|
|
550
|
+
let function = self.script.function(handler.name)?;
|
|
551
|
+
let scope = Scope::new(appliers.name(handler.name), &function, appliers);
|
|
489
552
|
let mut found = Vec::new();
|
|
490
|
-
for call in calls_of(function) {
|
|
491
|
-
let target = scope.target(
|
|
553
|
+
for call in calls_of(self.script.js(), &function) {
|
|
554
|
+
let target = scope.target(call)?;
|
|
492
555
|
// Find `Reflect.construct` in a binding or array slot, called directly or through a
|
|
493
556
|
// helper.
|
|
494
557
|
let named = target
|
|
495
558
|
.callee
|
|
496
|
-
.
|
|
497
|
-
.is_some_and(|callee| self.script.constructs.contains(callee));
|
|
559
|
+
.is_some_and(|callee| self.script.constructs.contains(&callee));
|
|
498
560
|
let slotted = target
|
|
499
561
|
.slot
|
|
500
|
-
.
|
|
501
|
-
.is_some_and(|slot| self.script.construct_slots.contains(slot));
|
|
562
|
+
.is_some_and(|slot| self.script.construct_slots.contains(&slot));
|
|
502
563
|
if named || slotted {
|
|
503
564
|
found.push(target.frames(handler.frame.clone()));
|
|
504
565
|
}
|
|
@@ -507,23 +568,26 @@ impl<'script> Machine<'script> {
|
|
|
507
568
|
if !reflected {
|
|
508
569
|
#[derive(Default)]
|
|
509
570
|
struct News(Vec<Site>);
|
|
510
|
-
impl Visit for News {
|
|
511
|
-
fn visit_function(&mut self, _: &Function) {}
|
|
512
|
-
fn visit_class(&mut self, _: &Class) {}
|
|
571
|
+
impl<'a> Visit<'a> for News {
|
|
572
|
+
fn visit_function(&mut self, _: &Function<'a>, _: ScopeFlags) {}
|
|
573
|
+
fn visit_class(&mut self, _: &Class<'a>) {}
|
|
574
|
+
fn visit_object_property(&mut self, property: &ObjectProperty<'a>) {
|
|
575
|
+
walk_property(self, property);
|
|
576
|
+
}
|
|
513
577
|
// Ignore guard exceptions; they do not produce the handler's constructed value.
|
|
514
|
-
fn
|
|
515
|
-
fn
|
|
516
|
-
if construction.
|
|
578
|
+
fn visit_throw_statement(&mut self, _: &ThrowStatement<'a>) {}
|
|
579
|
+
fn visit_new_expression(&mut self, construction: &NewExpression<'a>) {
|
|
580
|
+
if construction.arguments.is_empty() {
|
|
517
581
|
self.0.push(Site {
|
|
518
|
-
v8: construction.span.
|
|
519
|
-
jsc: construction.callee.span().
|
|
582
|
+
v8: construction.span.start,
|
|
583
|
+
jsc: construction.callee.span().end,
|
|
520
584
|
});
|
|
521
585
|
}
|
|
522
|
-
|
|
586
|
+
walk::walk_new_expression(self, construction);
|
|
523
587
|
}
|
|
524
588
|
}
|
|
525
589
|
let mut news = News::default();
|
|
526
|
-
function.
|
|
590
|
+
function.visit(&mut news);
|
|
527
591
|
for site in news.0 {
|
|
528
592
|
found.push(vec![Call {
|
|
529
593
|
name: handler.frame.clone(),
|
|
@@ -537,7 +601,7 @@ impl<'script> Machine<'script> {
|
|
|
537
601
|
let Some(frames) = found.pop() else {
|
|
538
602
|
bail!(
|
|
539
603
|
"missing original function {} with the needed shape",
|
|
540
|
-
handler.name
|
|
604
|
+
appliers.name(handler.name)
|
|
541
605
|
);
|
|
542
606
|
};
|
|
543
607
|
Ok(Chain {
|
|
@@ -550,13 +614,14 @@ impl<'script> Machine<'script> {
|
|
|
550
614
|
/// passes the method, receiver, and `passed` arguments to a native apply forwarder.
|
|
551
615
|
fn method_call(&self, opcode: u8, passed: usize, guarded: bool) -> Result<Vec<Call>> {
|
|
552
616
|
let handler = self.handler(opcode, guarded)?;
|
|
553
|
-
let
|
|
554
|
-
let
|
|
617
|
+
let appliers = &self.script.appliers;
|
|
618
|
+
let function = self.script.function(handler.name)?;
|
|
619
|
+
let scope = Scope::new(appliers.name(handler.name), &function, appliers);
|
|
555
620
|
let mut calls = Vec::new();
|
|
556
|
-
for call in calls_of(function) {
|
|
557
|
-
let target = scope.target(
|
|
621
|
+
for call in calls_of(self.script.js(), &function) {
|
|
622
|
+
let target = scope.target(call)?;
|
|
558
623
|
if target.passed == Some(passed + 2)
|
|
559
|
-
&& let Some(callee) =
|
|
624
|
+
&& let Some(callee) = target.callee
|
|
560
625
|
&& let Some(inner) = self.script.method_forwarder(callee)
|
|
561
626
|
{
|
|
562
627
|
calls.push((inner, target));
|
|
@@ -575,10 +640,7 @@ impl<'script> Machine<'script> {
|
|
|
575
640
|
.iter()
|
|
576
641
|
.filter(|target| target.passed == Some(arity))
|
|
577
642
|
.collect();
|
|
578
|
-
let sites: Vec<u32> = listeners
|
|
579
|
-
.iter()
|
|
580
|
-
.map(|target| target.site.v8.0 - self.start.0)
|
|
581
|
-
.collect();
|
|
643
|
+
let sites: Vec<u32> = listeners.iter().map(|target| target.site.v8).collect();
|
|
582
644
|
let listener = candidates::exactly_one(listeners, "listener of the closure's arity")
|
|
583
645
|
.with_context(|| {
|
|
584
646
|
format!("no one listener for a closure of {arity} arguments, at {sites:?}")
|
|
@@ -588,19 +650,19 @@ impl<'script> Machine<'script> {
|
|
|
588
650
|
}
|
|
589
651
|
|
|
590
652
|
/// The function `run` calls for an opcode.
|
|
591
|
-
struct Handler<'machine> {
|
|
653
|
+
struct Handler<'a, 'machine> {
|
|
592
654
|
/// Call from the dispatch loop to this handler.
|
|
593
|
-
dispatch: Invocation
|
|
594
|
-
name:
|
|
655
|
+
dispatch: Invocation<'a>,
|
|
656
|
+
name: Callee<'a>,
|
|
595
657
|
frame: Name,
|
|
596
|
-
dispatch_loop: &'machine DispatchLoop
|
|
658
|
+
dispatch_loop: &'machine DispatchLoop<'a>,
|
|
597
659
|
}
|
|
598
660
|
|
|
599
661
|
/// Function called by `run` with the machine to dispatch opcodes.
|
|
600
|
-
struct DispatchLoop {
|
|
601
|
-
name:
|
|
662
|
+
struct DispatchLoop<'a> {
|
|
663
|
+
name: Callee<'a>,
|
|
602
664
|
/// Call from `run`, either direct or through a helper.
|
|
603
|
-
call: Invocation
|
|
665
|
+
call: Invocation<'a>,
|
|
604
666
|
/// Whether the call occurs in a `try` block that handles exceptions.
|
|
605
667
|
guarded: bool,
|
|
606
668
|
}
|
|
@@ -610,42 +672,70 @@ struct DispatchLoop {
|
|
|
610
672
|
const DISPATCH_CASES: usize = 40;
|
|
611
673
|
|
|
612
674
|
/// Finds functions with dispatch switches that `run` calls with the machine.
|
|
613
|
-
fn dispatch_loops
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
675
|
+
fn dispatch_loops<'a>(
|
|
676
|
+
script: &Script<'a>,
|
|
677
|
+
run: &Body<'a>,
|
|
678
|
+
machine: Option<SymbolId>,
|
|
679
|
+
) -> Vec<DispatchLoop<'a>> {
|
|
680
|
+
struct Calls<'a> {
|
|
681
|
+
js: ast::Script<'a>,
|
|
682
|
+
found: Vec<(&'a CallExpression<'a>, bool)>,
|
|
617
683
|
guarded: bool,
|
|
684
|
+
machine: Option<SymbolId>,
|
|
618
685
|
}
|
|
619
|
-
impl Visit for Calls {
|
|
620
|
-
fn visit_function(&mut self, _: &Function) {}
|
|
621
|
-
fn visit_class(&mut self, _: &Class) {}
|
|
686
|
+
impl<'a> Visit<'a> for Calls<'a> {
|
|
687
|
+
fn visit_function(&mut self, _: &Function<'a>, _: ScopeFlags) {}
|
|
688
|
+
fn visit_class(&mut self, _: &Class<'a>) {}
|
|
689
|
+
fn visit_object_property(&mut self, property: &ObjectProperty<'a>) {
|
|
690
|
+
walk_property(self, property);
|
|
691
|
+
}
|
|
622
692
|
// Only a `try` with a `catch` handles exceptions.
|
|
623
|
-
fn
|
|
693
|
+
fn visit_try_statement(&mut self, statement: &TryStatement<'a>) {
|
|
624
694
|
let outer = self.guarded;
|
|
625
695
|
self.guarded |= statement.handler.is_some();
|
|
626
|
-
statement.block
|
|
696
|
+
self.visit_block_statement(&statement.block);
|
|
627
697
|
self.guarded = outer;
|
|
628
|
-
statement.handler
|
|
629
|
-
|
|
698
|
+
if let Some(handler) = &statement.handler {
|
|
699
|
+
self.visit_catch_clause(handler);
|
|
700
|
+
}
|
|
701
|
+
if let Some(finalizer) = &statement.finalizer {
|
|
702
|
+
self.visit_block_statement(finalizer);
|
|
703
|
+
}
|
|
630
704
|
}
|
|
631
|
-
fn
|
|
632
|
-
if call
|
|
633
|
-
|
|
705
|
+
fn visit_call_expression(&mut self, call: &CallExpression<'a>) {
|
|
706
|
+
if !is_optional_call(call)
|
|
707
|
+
&& call.arguments.iter().any(|argument| {
|
|
708
|
+
match ast::argument_expression(argument).without_parentheses() {
|
|
709
|
+
Expression::ThisExpression(_) => self.machine.is_none(),
|
|
710
|
+
Expression::Identifier(reference) => {
|
|
711
|
+
self.machine.is_some() && self.js.symbol(reference) == self.machine
|
|
712
|
+
}
|
|
713
|
+
_ => false,
|
|
714
|
+
}
|
|
715
|
+
})
|
|
716
|
+
{
|
|
717
|
+
let call = self.js.lend(call, AstKind::as_call_expression);
|
|
718
|
+
self.found.push((call, self.guarded));
|
|
634
719
|
}
|
|
635
|
-
|
|
720
|
+
walk::walk_call_expression(self, call);
|
|
636
721
|
}
|
|
637
722
|
}
|
|
638
|
-
let mut calls = Calls
|
|
639
|
-
|
|
723
|
+
let mut calls = Calls {
|
|
724
|
+
js: script.js(),
|
|
725
|
+
found: Vec::new(),
|
|
726
|
+
guarded: false,
|
|
727
|
+
machine,
|
|
728
|
+
};
|
|
729
|
+
run.visit(&mut calls);
|
|
640
730
|
let scope = Scope::new("", run, &script.appliers);
|
|
641
731
|
calls
|
|
642
732
|
.found
|
|
643
733
|
.into_iter()
|
|
644
734
|
.filter_map(|(call, guarded)| {
|
|
645
|
-
let call = scope.target(
|
|
646
|
-
let name = call.callee
|
|
647
|
-
let function = script.function(
|
|
648
|
-
(cases_of(function).len() > DISPATCH_CASES).then_some(DispatchLoop {
|
|
735
|
+
let call = scope.target(call).ok()?;
|
|
736
|
+
let name = call.callee?;
|
|
737
|
+
let function = script.function(name).ok()?;
|
|
738
|
+
(cases_of(script.js(), &function).len() > DISPATCH_CASES).then_some(DispatchLoop {
|
|
649
739
|
name,
|
|
650
740
|
call,
|
|
651
741
|
guarded,
|
|
@@ -654,148 +744,270 @@ fn dispatch_loops(script: &Script, run: &Function) -> Vec<DispatchLoop> {
|
|
|
654
744
|
.collect()
|
|
655
745
|
}
|
|
656
746
|
|
|
747
|
+
/// A function's parameters and statements. An original function extracted from a merged
|
|
748
|
+
/// function has the merged function's parameters after the selector and its case's statements.
|
|
749
|
+
#[derive(Clone, Copy)]
|
|
750
|
+
struct Body<'a> {
|
|
751
|
+
parameters: &'a [FormalParameter<'a>],
|
|
752
|
+
/// Whether a rest parameter follows `parameters`.
|
|
753
|
+
rest: bool,
|
|
754
|
+
/// Whether the body starts with a directive prologue, which no statement pattern accepts.
|
|
755
|
+
prologue: bool,
|
|
756
|
+
/// The statements, in two parts so that an original function can join its case block to the
|
|
757
|
+
/// statements after it.
|
|
758
|
+
head: &'a [Statement<'a>],
|
|
759
|
+
tail: &'a [Statement<'a>],
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
impl<'a> Body<'a> {
|
|
763
|
+
fn of(function: &'a Function<'a>) -> Self {
|
|
764
|
+
let (prologue, head) = match &function.body {
|
|
765
|
+
Some(body) => (!body.directives.is_empty(), &body.statements[..]),
|
|
766
|
+
None => (false, &[][..]),
|
|
767
|
+
};
|
|
768
|
+
Self {
|
|
769
|
+
parameters: &function.params.items,
|
|
770
|
+
rest: function.params.rest.is_some(),
|
|
771
|
+
prologue,
|
|
772
|
+
head,
|
|
773
|
+
tail: &[],
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
fn statements(&self) -> impl DoubleEndedIterator<Item = &'a Statement<'a>> + Clone + use<'a> {
|
|
778
|
+
self.head.iter().chain(self.tail)
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
/// The statements of a body without a directive prologue.
|
|
782
|
+
fn plain_statements(
|
|
783
|
+
&self,
|
|
784
|
+
) -> Option<impl DoubleEndedIterator<Item = &'a Statement<'a>> + Clone + use<'a>> {
|
|
785
|
+
(!self.prologue).then(|| self.statements())
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
/// The only statement after leading local declarations without initializers.
|
|
789
|
+
fn only_statement(&self) -> Option<&'a Statement<'a>> {
|
|
790
|
+
let mut rest = self.plain_statements()?.skip_while(|statement| {
|
|
791
|
+
matches!(statement, Statement::VariableDeclaration(declaration)
|
|
792
|
+
if !declaration.kind.is_using()
|
|
793
|
+
&& declaration.declarations.iter().all(|declarator| declarator.init.is_none()))
|
|
794
|
+
});
|
|
795
|
+
let only = rest.next()?;
|
|
796
|
+
rest.next().is_none().then_some(only)
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
/// The bindings of the parameters, if they are all plain names.
|
|
800
|
+
fn parameters(&self) -> Option<Vec<SymbolId>> {
|
|
801
|
+
if self.rest {
|
|
802
|
+
return None;
|
|
803
|
+
}
|
|
804
|
+
self.parameters.iter().map(plain_parameter).collect()
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
fn visit(&self, visitor: &mut impl Visit<'a>) {
|
|
808
|
+
for statement in self.statements() {
|
|
809
|
+
visitor.visit_statement(statement);
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
|
|
657
814
|
/// The script's declared and assigned functions, VM dispatch methods, and aliases for
|
|
658
815
|
/// `Reflect.construct` and `Reflect.apply`.
|
|
659
|
-
struct Script {
|
|
660
|
-
functions: Vec<(
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
originals: Vec<(Id, Function)>,
|
|
664
|
-
classes: Vec<DispatchMethod>,
|
|
665
|
-
constructs: Vec<Id>,
|
|
816
|
+
struct Script<'a> {
|
|
817
|
+
functions: Vec<(Binding<'a>, Body<'a>)>,
|
|
818
|
+
classes: Vec<DispatchMethod<'a>>,
|
|
819
|
+
constructs: Vec<Callee<'a>>,
|
|
666
820
|
/// Array slots containing `Reflect.construct`, indexed by array and slot.
|
|
667
|
-
construct_slots: Vec<(
|
|
668
|
-
applies: Vec<
|
|
821
|
+
construct_slots: Vec<(Binding<'a>, usize)>,
|
|
822
|
+
applies: Vec<Callee<'a>>,
|
|
669
823
|
/// Array slots containing `Reflect.apply`, indexed by array and slot.
|
|
670
|
-
apply_slots: Vec<(
|
|
671
|
-
appliers: Appliers
|
|
824
|
+
apply_slots: Vec<(Binding<'a>, usize)>,
|
|
825
|
+
appliers: Appliers<'a>,
|
|
672
826
|
}
|
|
673
827
|
|
|
674
|
-
///
|
|
828
|
+
/// An original function extracted from a merged function. See [`merged`].
|
|
829
|
+
struct Original<'a> {
|
|
830
|
+
/// The merged function's name, which frames of the original use.
|
|
831
|
+
name: &'a str,
|
|
832
|
+
body: Body<'a>,
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
/// Call helpers indexed by callee, including selector-based helpers, merged functions, and
|
|
675
836
|
/// forwarders. Also records numeric constants used by selectors.
|
|
676
|
-
struct Appliers {
|
|
677
|
-
helpers: HashMap<
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
///
|
|
681
|
-
|
|
837
|
+
struct Appliers<'a> {
|
|
838
|
+
helpers: HashMap<Callee<'a>, Selection<Site>>,
|
|
839
|
+
/// Merged functions, selecting originals indexed into `originals`.
|
|
840
|
+
merged: HashMap<Binding<'a>, Selection<usize>>,
|
|
841
|
+
/// Original functions extracted from merged functions.
|
|
842
|
+
originals: Vec<Original<'a>>,
|
|
843
|
+
forwarders: Forwarders<'a>,
|
|
844
|
+
constants: Constants<'a>,
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
/// Bindings assigned a numeric value once, such as a shared case-label mask, and the script
|
|
848
|
+
/// that binds them.
|
|
849
|
+
struct Constants<'a> {
|
|
850
|
+
js: ast::Script<'a>,
|
|
851
|
+
values: HashMap<SymbolId, f64>,
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
impl<'a> Constants<'a> {
|
|
855
|
+
fn new(js: ast::Script<'a>) -> Self {
|
|
856
|
+
let mut values = HashMap::new();
|
|
857
|
+
let mut assigned = HashSet::new();
|
|
858
|
+
for node in js.nodes.iter() {
|
|
859
|
+
match node.kind() {
|
|
860
|
+
AstKind::VariableDeclarator(declarator) => {
|
|
861
|
+
if let BindingPattern::BindingIdentifier(name) = &declarator.id
|
|
862
|
+
&& let Some(value) = declarator
|
|
863
|
+
.init
|
|
864
|
+
.as_ref()
|
|
865
|
+
.and_then(|init| ast::fold_number(init, &|_| None))
|
|
866
|
+
{
|
|
867
|
+
values.insert(name.symbol_id(), value);
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
AstKind::AssignmentExpression(assignment) => {
|
|
871
|
+
if let AssignmentTarget::AssignmentTargetIdentifier(name) = &assignment.left
|
|
872
|
+
&& let Some(symbol) = js.symbol(name)
|
|
873
|
+
{
|
|
874
|
+
assigned.insert(symbol);
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
_ => {}
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
values.retain(|symbol, _| !assigned.contains(symbol));
|
|
881
|
+
Self { js, values }
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
/// The function or value `expression` names.
|
|
885
|
+
fn binding(&self, expression: &Expression<'a>) -> Option<Binding<'a>> {
|
|
886
|
+
match expression.without_parentheses() {
|
|
887
|
+
Expression::Identifier(reference) => Some(self.js.binding(reference)),
|
|
888
|
+
_ => None,
|
|
889
|
+
}
|
|
890
|
+
}
|
|
682
891
|
}
|
|
683
892
|
|
|
684
893
|
/// Functions that call their first parameter with the remaining parameters, as recorded by
|
|
685
894
|
/// [`Scope`].
|
|
686
895
|
#[derive(Default)]
|
|
687
|
-
struct Forwarders {
|
|
896
|
+
struct Forwarders<'a> {
|
|
688
897
|
/// Forwarders indexed by the binding holding their object and their property key.
|
|
689
|
-
on_objects: HashMap<(Holder
|
|
898
|
+
on_objects: HashMap<(Holder<'a>, &'a str), Forwarder>,
|
|
690
899
|
/// Forwarders copied to bindings, as in `f = object.key`, with the frames produced when called
|
|
691
900
|
/// through those bindings.
|
|
692
|
-
held: HashMap<Holder
|
|
901
|
+
held: HashMap<Holder<'a>, Vec<Call>>,
|
|
693
902
|
}
|
|
694
903
|
|
|
695
|
-
impl Forwarders {
|
|
904
|
+
impl<'a> Forwarders<'a> {
|
|
696
905
|
/// Keeps only forwarders whose object bindings are assigned once, so their values are known.
|
|
697
|
-
fn retain_assigned_once(&mut self, assigned: &HashMap<Holder
|
|
906
|
+
fn retain_assigned_once(&mut self, assigned: &HashMap<Holder<'a>, usize>) {
|
|
698
907
|
let once = |holder: &Holder| assigned.get(holder).is_none_or(|&count| count == 1);
|
|
699
908
|
self.on_objects.retain(|(holder, _), _| once(holder));
|
|
700
909
|
self.held.retain(|holder, _| once(holder));
|
|
701
910
|
}
|
|
702
911
|
}
|
|
703
912
|
|
|
704
|
-
impl Appliers {
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
fn visit_var_declarator(&mut self, declarator: &VarDeclarator) {
|
|
715
|
-
if let Pat::Ident(name) = &declarator.name
|
|
716
|
-
&& let Some(value) = declarator
|
|
717
|
-
.init
|
|
718
|
-
.as_deref()
|
|
719
|
-
.and_then(|init| ast::fold_number(init, &|_| None))
|
|
720
|
-
{
|
|
721
|
-
self.values.insert(name.to_id(), value);
|
|
722
|
-
}
|
|
723
|
-
declarator.visit_children_with(self);
|
|
724
|
-
}
|
|
725
|
-
fn visit_assign_expr(&mut self, assign: &AssignExpr) {
|
|
726
|
-
if let Some(name) = assign.left.as_ident() {
|
|
727
|
-
self.assigned.insert(name.to_id());
|
|
728
|
-
}
|
|
729
|
-
assign.visit_children_with(self);
|
|
730
|
-
}
|
|
731
|
-
}
|
|
732
|
-
let mut constants = Constants::default();
|
|
733
|
-
program.visit_with(&mut constants);
|
|
734
|
-
constants
|
|
735
|
-
.values
|
|
736
|
-
.retain(|name, _| !constants.assigned.contains(name));
|
|
737
|
-
let values = constants.values;
|
|
738
|
-
let applier = |(name, function): &(Id, Function)| {
|
|
739
|
-
Some((name.clone(), Applier::new(function, &values)?))
|
|
740
|
-
};
|
|
741
|
-
let mut helpers: HashMap<Id, Applier> = functions.iter().filter_map(applier).collect();
|
|
742
|
-
let mut merged = HashMap::new();
|
|
913
|
+
impl<'a> Appliers<'a> {
|
|
914
|
+
fn new(js: ast::Script<'a>, functions: &[(Binding<'a>, Body<'a>)]) -> Self {
|
|
915
|
+
let constants = Constants::new(js);
|
|
916
|
+
let mut helpers: HashMap<Callee<'a>, Selection<Site>> = functions
|
|
917
|
+
.iter()
|
|
918
|
+
.filter_map(|(name, function)| {
|
|
919
|
+
Some((Callee::Binding(*name), applier(function, &constants)?))
|
|
920
|
+
})
|
|
921
|
+
.collect();
|
|
922
|
+
let mut merged_functions = HashMap::new();
|
|
743
923
|
let mut originals = Vec::new();
|
|
744
924
|
for (name, function) in functions {
|
|
745
|
-
if !helpers.contains_key(name)
|
|
746
|
-
&& let Some(
|
|
925
|
+
if !helpers.contains_key(&Callee::Binding(*name))
|
|
926
|
+
&& let Some(Selection { mask, cases }) = merged(function, &constants)
|
|
747
927
|
{
|
|
748
|
-
|
|
749
|
-
|
|
928
|
+
let mut indexed = Vec::new();
|
|
929
|
+
for (label, body) in cases {
|
|
930
|
+
indexed.push((label, originals.len()));
|
|
931
|
+
originals.push(Original {
|
|
932
|
+
name: js.name(*name),
|
|
933
|
+
body,
|
|
934
|
+
});
|
|
935
|
+
}
|
|
936
|
+
merged_functions.insert(
|
|
937
|
+
*name,
|
|
938
|
+
Selection {
|
|
939
|
+
mask,
|
|
940
|
+
cases: indexed,
|
|
941
|
+
},
|
|
942
|
+
);
|
|
750
943
|
}
|
|
751
944
|
}
|
|
752
945
|
// A merged function may hold call helpers.
|
|
753
|
-
|
|
946
|
+
for (index, original) in originals.iter().enumerate() {
|
|
947
|
+
if let Some(helper) = applier(&original.body, &constants) {
|
|
948
|
+
helpers.insert(Callee::Original(index), helper);
|
|
949
|
+
}
|
|
950
|
+
}
|
|
754
951
|
let mut appliers = Self {
|
|
755
952
|
helpers,
|
|
756
|
-
merged,
|
|
953
|
+
merged: merged_functions,
|
|
954
|
+
originals,
|
|
757
955
|
forwarders: Forwarders::default(),
|
|
758
|
-
constants
|
|
956
|
+
constants,
|
|
759
957
|
};
|
|
760
|
-
appliers.forwarders = forwarders(
|
|
761
|
-
|
|
958
|
+
appliers.forwarders = forwarders(&appliers);
|
|
959
|
+
appliers
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
fn js(&self) -> ast::Script<'a> {
|
|
963
|
+
self.constants.js
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
/// The name of `callee` in the source. An original function has its merged function's name.
|
|
967
|
+
fn name(&self, callee: Callee<'a>) -> &'a str {
|
|
968
|
+
match callee {
|
|
969
|
+
Callee::Binding(binding) => self.js().name(binding),
|
|
970
|
+
Callee::Original(index) => self.originals[index].name,
|
|
971
|
+
}
|
|
762
972
|
}
|
|
763
973
|
|
|
764
|
-
/// Evaluates `
|
|
765
|
-
fn number(&self,
|
|
766
|
-
|
|
974
|
+
/// Evaluates `operand` using numeric literals and known constants.
|
|
975
|
+
fn number(&self, operand: Operand<'a>) -> Option<f64> {
|
|
976
|
+
match operand {
|
|
977
|
+
Operand::Expression(expression) => number(expression, &self.constants),
|
|
978
|
+
Operand::Resolved(Callee::Binding(Binding::Symbol(symbol))) => {
|
|
979
|
+
self.constants.values.get(&symbol).copied()
|
|
980
|
+
}
|
|
981
|
+
Operand::Resolved(_) => None,
|
|
982
|
+
}
|
|
767
983
|
}
|
|
768
984
|
}
|
|
769
985
|
|
|
770
986
|
/// Collects forwarders from each function via [`Scope`]. Keeps only those whose object bindings are
|
|
771
987
|
/// assigned once.
|
|
772
|
-
fn forwarders(
|
|
773
|
-
struct Collect<'
|
|
774
|
-
appliers: &'
|
|
775
|
-
found: Forwarders
|
|
776
|
-
assigned: HashMap<Holder
|
|
777
|
-
}
|
|
778
|
-
impl Collect<'_> {
|
|
779
|
-
fn
|
|
780
|
-
let
|
|
988
|
+
fn forwarders<'a>(appliers: &Appliers<'a>) -> Forwarders<'a> {
|
|
989
|
+
struct Collect<'a, 's> {
|
|
990
|
+
appliers: &'s Appliers<'a>,
|
|
991
|
+
found: Forwarders<'a>,
|
|
992
|
+
assigned: HashMap<Holder<'a>, usize>,
|
|
993
|
+
}
|
|
994
|
+
impl<'a> Visit<'a> for Collect<'a, '_> {
|
|
995
|
+
fn visit_function(&mut self, function: &Function<'a>, flags: ScopeFlags) {
|
|
996
|
+
let function = self.appliers.js().lend(function, AstKind::as_function);
|
|
997
|
+
let name = function.id.as_ref().map_or("", |name| name.name.as_str());
|
|
998
|
+
let scope = Scope::new(name, &Body::of(function), self.appliers);
|
|
781
999
|
self.found.on_objects.extend(scope.forwarders.on_objects);
|
|
782
1000
|
self.found.held.extend(scope.forwarders.held);
|
|
783
1001
|
for (holder, count) in scope.assigned {
|
|
784
1002
|
*self.assigned.entry(holder).or_default() += count;
|
|
785
1003
|
}
|
|
786
|
-
|
|
1004
|
+
walk::walk_function(self, function, flags);
|
|
787
1005
|
}
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
fn visit_fn_decl(&mut self, declaration: &FnDecl) {
|
|
791
|
-
self.scope(&declaration.ident.sym, &declaration.function);
|
|
1006
|
+
fn visit_object_property(&mut self, property: &ObjectProperty<'a>) {
|
|
1007
|
+
walk_property(self, property);
|
|
792
1008
|
}
|
|
793
|
-
fn
|
|
794
|
-
|
|
795
|
-
self.scope(name, &expression.function);
|
|
796
|
-
}
|
|
797
|
-
fn visit_function(&mut self, function: &Function) {
|
|
798
|
-
self.scope("", function);
|
|
1009
|
+
fn visit_method_definition(&mut self, method: &MethodDefinition<'a>) {
|
|
1010
|
+
walk_method(self, method);
|
|
799
1011
|
}
|
|
800
1012
|
}
|
|
801
1013
|
let mut collect = Collect {
|
|
@@ -803,35 +1015,41 @@ fn forwarders(program: &Program, appliers: &Appliers) -> Forwarders {
|
|
|
803
1015
|
found: Forwarders::default(),
|
|
804
1016
|
assigned: HashMap::new(),
|
|
805
1017
|
};
|
|
806
|
-
|
|
1018
|
+
collect.visit_program(appliers.js().program);
|
|
807
1019
|
collect.found.retain_assigned_once(&collect.assigned);
|
|
808
1020
|
collect.found
|
|
809
1021
|
}
|
|
810
1022
|
|
|
811
1023
|
/// Evaluates `expression` using literals and `constants`.
|
|
812
|
-
fn number(expression: &
|
|
813
|
-
ast::fold_number(expression, &|leaf| {
|
|
814
|
-
|
|
1024
|
+
fn number<'a>(expression: &Expression<'a>, constants: &Constants<'a>) -> Option<f64> {
|
|
1025
|
+
ast::fold_number(expression, &|leaf| match leaf {
|
|
1026
|
+
Expression::Identifier(reference) => constants
|
|
1027
|
+
.values
|
|
1028
|
+
.get(&constants.js.symbol(reference)?)
|
|
1029
|
+
.copied(),
|
|
1030
|
+
_ => None,
|
|
815
1031
|
})
|
|
816
1032
|
}
|
|
817
1033
|
|
|
818
|
-
///
|
|
819
|
-
struct DispatchMethod {
|
|
820
|
-
class:
|
|
821
|
-
run:
|
|
822
|
-
|
|
823
|
-
key: Option<String>,
|
|
1034
|
+
/// The VM class method or free function that calls the dispatch loops.
|
|
1035
|
+
struct DispatchMethod<'a> {
|
|
1036
|
+
class: Option<&'a str>,
|
|
1037
|
+
run: Body<'a>,
|
|
1038
|
+
frame: Name,
|
|
824
1039
|
}
|
|
825
1040
|
|
|
826
|
-
impl Script {
|
|
827
|
-
fn new(
|
|
828
|
-
//
|
|
829
|
-
//
|
|
830
|
-
let functions =
|
|
831
|
-
|
|
1041
|
+
impl<'a> Script<'a> {
|
|
1042
|
+
fn new(js: ast::Script<'a>) -> Self {
|
|
1043
|
+
// Semantic analysis gives equal names in different lexical scopes distinct bindings,
|
|
1044
|
+
// while frame names keep their original spelling.
|
|
1045
|
+
let functions: Vec<(Binding<'a>, Body<'a>)> = js
|
|
1046
|
+
.named_functions()
|
|
1047
|
+
.into_iter()
|
|
1048
|
+
.map(|(name, function)| (name, Body::of(function)))
|
|
1049
|
+
.collect();
|
|
1050
|
+
let appliers = Appliers::new(js, &functions);
|
|
832
1051
|
let mut script = Self {
|
|
833
1052
|
functions,
|
|
834
|
-
originals,
|
|
835
1053
|
classes: Vec::new(),
|
|
836
1054
|
constructs: Vec::new(),
|
|
837
1055
|
construct_slots: Vec::new(),
|
|
@@ -839,148 +1057,261 @@ impl Script {
|
|
|
839
1057
|
apply_slots: Vec::new(),
|
|
840
1058
|
appliers,
|
|
841
1059
|
};
|
|
842
|
-
|
|
1060
|
+
for node in js.nodes.iter() {
|
|
1061
|
+
match node.kind() {
|
|
1062
|
+
AstKind::AssignmentExpression(assignment) => script.assignment(assignment),
|
|
1063
|
+
AstKind::Class(class) if class.is_declaration() => script.class(class),
|
|
1064
|
+
_ => {}
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
843
1067
|
script
|
|
844
1068
|
}
|
|
845
1069
|
|
|
846
|
-
fn
|
|
847
|
-
self.
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
1070
|
+
fn js(&self) -> ast::Script<'a> {
|
|
1071
|
+
self.appliers.js()
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
/// Tracks `Reflect.construct` and `Reflect.apply` in bindings or array slots, including
|
|
1075
|
+
/// indirect references to `Reflect`. Each assignment in a chain records its own target.
|
|
1076
|
+
fn assignment(&mut self, assignment: &'a AssignmentExpression<'a>) {
|
|
1077
|
+
let js = self.js();
|
|
1078
|
+
let mut value = &assignment.right;
|
|
1079
|
+
while let Expression::AssignmentExpression(inner) = value.without_parentheses() {
|
|
1080
|
+
value = &inner.right;
|
|
1081
|
+
}
|
|
1082
|
+
let Some(member) = member(value.without_parentheses()) else {
|
|
1083
|
+
return;
|
|
1084
|
+
};
|
|
1085
|
+
let method = ast::member_str(member);
|
|
1086
|
+
if let AssignmentTarget::AssignmentTargetIdentifier(name) = &assignment.left {
|
|
1087
|
+
let name = Callee::Binding(js.binding(name));
|
|
1088
|
+
match method {
|
|
1089
|
+
Some("construct") => self.constructs.push(name),
|
|
1090
|
+
Some("apply") => self.applies.push(name),
|
|
1091
|
+
_ => {}
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
if let Some(MemberExpression::ComputedMemberExpression(slot)) =
|
|
1095
|
+
assigned_member(&assignment.left)
|
|
1096
|
+
&& let Expression::Identifier(array) = &slot.object
|
|
1097
|
+
&& let Some(index) =
|
|
1098
|
+
number(&slot.expression, &self.appliers.constants).and_then(ast::array_index)
|
|
1099
|
+
{
|
|
1100
|
+
let slot = (js.binding(array), index);
|
|
1101
|
+
match method {
|
|
1102
|
+
Some("construct") => self.construct_slots.push(slot),
|
|
1103
|
+
Some("apply") => self.apply_slots.push(slot),
|
|
1104
|
+
_ => {}
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
/// Records a class declaration's `run` method.
|
|
1110
|
+
fn class(&mut self, class: &'a Class<'a>) {
|
|
1111
|
+
let run = class.body.body.iter().find_map(|element| match element {
|
|
1112
|
+
ClassElement::MethodDefinition(method)
|
|
1113
|
+
if method.kind != MethodDefinitionKind::Constructor
|
|
1114
|
+
&& ast::static_key_str(&method.key, method.computed) == Some("run") =>
|
|
1115
|
+
{
|
|
1116
|
+
Some(method)
|
|
1117
|
+
}
|
|
1118
|
+
_ => None,
|
|
1119
|
+
});
|
|
1120
|
+
if let Some(method) = run
|
|
1121
|
+
&& let Some(name) = &class.id
|
|
1122
|
+
{
|
|
1123
|
+
self.classes.push(DispatchMethod {
|
|
1124
|
+
class: Some(name.name.as_str()),
|
|
1125
|
+
run: Body::of(&method.value),
|
|
1126
|
+
frame: Name {
|
|
1127
|
+
v8: format!("{}.run", name.name),
|
|
1128
|
+
jsc: ast::key_str(&method.key, method.computed)
|
|
1129
|
+
.unwrap_or_default()
|
|
1130
|
+
.to_owned(),
|
|
1131
|
+
},
|
|
1132
|
+
});
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
fn function(&self, callee: Callee<'a>) -> Result<Body<'a>> {
|
|
1137
|
+
match callee {
|
|
1138
|
+
Callee::Binding(binding) => self
|
|
1139
|
+
.functions
|
|
1140
|
+
.iter()
|
|
1141
|
+
.find(|(name, _)| *name == binding)
|
|
1142
|
+
.map(|(_, function)| *function),
|
|
1143
|
+
Callee::Original(index) => self
|
|
1144
|
+
.appliers
|
|
1145
|
+
.originals
|
|
1146
|
+
.get(index)
|
|
1147
|
+
.map(|original| original.body),
|
|
1148
|
+
}
|
|
1149
|
+
.with_context(|| format!("missing original function {}", self.appliers.name(callee)))
|
|
853
1150
|
}
|
|
854
1151
|
|
|
855
|
-
/// Returns frames for the function
|
|
856
|
-
///
|
|
857
|
-
fn method_forwarder(&self,
|
|
858
|
-
let function = self.function(
|
|
859
|
-
let parameters = parameters(
|
|
1152
|
+
/// Returns frames for the function `callee` if it forwards a method call. Allows local
|
|
1153
|
+
/// variable declarations before the return.
|
|
1154
|
+
fn method_forwarder(&self, callee: Callee<'a>) -> Option<Vec<Call>> {
|
|
1155
|
+
let function = self.function(callee).ok()?;
|
|
1156
|
+
let parameters = function.parameters()?;
|
|
860
1157
|
let [method, receiver, ..] = parameters.as_slice() else {
|
|
861
1158
|
return None;
|
|
862
1159
|
};
|
|
863
|
-
let
|
|
864
|
-
else {
|
|
1160
|
+
let mut statements = function.plain_statements()?;
|
|
1161
|
+
let Statement::ReturnStatement(returned) = statements.next_back()? else {
|
|
865
1162
|
return None;
|
|
866
1163
|
};
|
|
867
|
-
if !
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
{
|
|
1164
|
+
if !statements.all(|statement| {
|
|
1165
|
+
matches!(statement, Statement::VariableDeclaration(declaration)
|
|
1166
|
+
if !declaration.kind.is_using())
|
|
1167
|
+
}) {
|
|
871
1168
|
return None;
|
|
872
1169
|
}
|
|
873
|
-
let mut value = returned.
|
|
874
|
-
if let
|
|
875
|
-
value = sequence.
|
|
1170
|
+
let mut value = returned.argument.as_ref()?.without_parentheses();
|
|
1171
|
+
if let Expression::SequenceExpression(sequence) = value {
|
|
1172
|
+
value = sequence.expressions.last()?.without_parentheses();
|
|
876
1173
|
}
|
|
877
|
-
let call = value
|
|
878
|
-
|
|
1174
|
+
let Expression::CallExpression(call) = value else {
|
|
1175
|
+
return None;
|
|
1176
|
+
};
|
|
1177
|
+
let name = self.appliers.name(callee);
|
|
1178
|
+
let target = Scope::new(name, &function, &self.appliers)
|
|
879
1179
|
.target(call)
|
|
880
1180
|
.ok()?;
|
|
881
1181
|
// Compare bindings so a nested function with the same name as a native apply
|
|
882
1182
|
// alias cannot be mistaken for that alias. Some builds keep the alias in an array slot.
|
|
883
|
-
let native = match (
|
|
884
|
-
(Some(callee), _) => self.function(callee).is_err() || self.applies.contains(callee),
|
|
885
|
-
(None, Some(slot)) => self.apply_slots.contains(slot),
|
|
1183
|
+
let native = match (target.callee, target.slot) {
|
|
1184
|
+
(Some(callee), _) => self.function(callee).is_err() || self.applies.contains(&callee),
|
|
1185
|
+
(None, Some(slot)) => self.apply_slots.contains(&slot),
|
|
886
1186
|
(None, None) => false,
|
|
887
1187
|
};
|
|
888
1188
|
if !native {
|
|
889
1189
|
return None;
|
|
890
1190
|
}
|
|
891
|
-
let [first, second, ..] = handed(call, &target)?[..] else {
|
|
1191
|
+
let [first, second, ..] = handed(Arguments::of(call), &target)?[..] else {
|
|
892
1192
|
return None;
|
|
893
1193
|
};
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
1194
|
+
let constants = &self.appliers.constants;
|
|
1195
|
+
(first.callee(constants) == Some(Callee::symbol(*method))
|
|
1196
|
+
&& second.callee(constants) == Some(Callee::symbol(*receiver)))
|
|
1197
|
+
.then(|| target.frames(Name::same(name.to_owned())))
|
|
897
1198
|
}
|
|
898
1199
|
}
|
|
899
1200
|
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
1201
|
+
/// A call argument as call resolution reads it.
|
|
1202
|
+
#[derive(Clone, Copy)]
|
|
1203
|
+
enum Operand<'a> {
|
|
1204
|
+
Expression(&'a Expression<'a>),
|
|
1205
|
+
/// A function resolved without an expression that names it: a binding a forwarder reads back
|
|
1206
|
+
/// from an array copy, or an original function selected from a merged function.
|
|
1207
|
+
Resolved(Callee<'a>),
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
impl<'a> Operand<'a> {
|
|
1211
|
+
/// The function or value the operand names.
|
|
1212
|
+
fn callee(self, constants: &Constants<'a>) -> Option<Callee<'a>> {
|
|
1213
|
+
match self {
|
|
1214
|
+
Self::Expression(expression) => constants.binding(expression).map(Callee::Binding),
|
|
1215
|
+
Self::Resolved(callee) => Some(callee),
|
|
908
1216
|
}
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
}
|
|
926
|
-
}
|
|
1217
|
+
}
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
/// A call's arguments. In a forwarder, `copies` resolves arguments read back from array copies,
|
|
1221
|
+
/// at the top level and in array literals.
|
|
1222
|
+
#[derive(Clone, Copy)]
|
|
1223
|
+
struct Arguments<'a, 'c> {
|
|
1224
|
+
list: &'a [Argument<'a>],
|
|
1225
|
+
copies: Option<&'c Copies<'a, 'c>>,
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
impl<'a, 'c> Arguments<'a, 'c> {
|
|
1229
|
+
fn of(call: &'a CallExpression<'a>) -> Self {
|
|
1230
|
+
Self {
|
|
1231
|
+
list: &call.arguments,
|
|
1232
|
+
copies: None,
|
|
927
1233
|
}
|
|
928
|
-
assign.visit_children_with(self);
|
|
929
1234
|
}
|
|
930
1235
|
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
key: ast::key_str(&method.key).map(str::to_owned),
|
|
943
|
-
});
|
|
1236
|
+
/// Reads an argument, resolving it through the copies unless it is an array literal.
|
|
1237
|
+
fn argument(&self, argument: &'a Argument<'a>) -> Operand<'a> {
|
|
1238
|
+
let expression = ast::argument_expression(argument);
|
|
1239
|
+
if let Some(copies) = self.copies
|
|
1240
|
+
&& !matches!(
|
|
1241
|
+
expression.without_parentheses(),
|
|
1242
|
+
Expression::ArrayExpression(_)
|
|
1243
|
+
)
|
|
1244
|
+
&& let Some(binding) = copies.resolve(expression)
|
|
1245
|
+
{
|
|
1246
|
+
return Operand::Resolved(Callee::Binding(binding));
|
|
944
1247
|
}
|
|
945
|
-
|
|
1248
|
+
Operand::Expression(expression)
|
|
1249
|
+
}
|
|
1250
|
+
|
|
1251
|
+
/// Reads an array element, or `None` for a hole.
|
|
1252
|
+
fn element(&self, element: &'a ArrayExpressionElement<'a>) -> Option<Operand<'a>> {
|
|
1253
|
+
let expression = element_expression(element)?;
|
|
1254
|
+
Some(
|
|
1255
|
+
match self.copies.and_then(|copies| copies.resolve(expression)) {
|
|
1256
|
+
Some(binding) => Operand::Resolved(Callee::Binding(binding)),
|
|
1257
|
+
None => Operand::Expression(expression),
|
|
1258
|
+
},
|
|
1259
|
+
)
|
|
946
1260
|
}
|
|
947
1261
|
}
|
|
948
1262
|
|
|
949
|
-
|
|
950
|
-
|
|
1263
|
+
/// The expression an array element holds, ignoring a spread, or `None` for a hole.
|
|
1264
|
+
fn element_expression<'b, 'a>(
|
|
1265
|
+
element: &'b ArrayExpressionElement<'a>,
|
|
1266
|
+
) -> Option<&'b Expression<'a>> {
|
|
1267
|
+
match element {
|
|
1268
|
+
ArrayExpressionElement::SpreadElement(spread) => Some(&spread.argument),
|
|
1269
|
+
ArrayExpressionElement::Elision(_) => None,
|
|
1270
|
+
element => Some(element.to_expression()),
|
|
1271
|
+
}
|
|
951
1272
|
}
|
|
952
1273
|
|
|
953
|
-
///
|
|
954
|
-
fn
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
.collect()
|
|
1274
|
+
/// An array literal, possibly parenthesized.
|
|
1275
|
+
fn array<'b, 'a>(expression: &'b Expression<'a>) -> Option<&'b ArrayExpression<'a>> {
|
|
1276
|
+
match expression.without_parentheses() {
|
|
1277
|
+
Expression::ArrayExpression(array) => Some(array),
|
|
1278
|
+
_ => None,
|
|
1279
|
+
}
|
|
960
1280
|
}
|
|
961
1281
|
|
|
962
|
-
/// Returns the arguments
|
|
1282
|
+
/// Returns the arguments a call forwards to the callee identified by `target`. Skips leading
|
|
963
1283
|
/// selectors, callee, and receiver. The forwarded arguments are either the trailing call arguments
|
|
964
1284
|
/// or the trailing elements of an apply-helper argument list.
|
|
965
|
-
fn handed<'
|
|
966
|
-
let
|
|
967
|
-
let list =
|
|
968
|
-
list.
|
|
1285
|
+
fn handed<'a>(arguments: Arguments<'a, '_>, target: &Invocation<'a>) -> Option<Vec<Operand<'a>>> {
|
|
1286
|
+
let operands: Vec<Operand<'a>> = if target.listed {
|
|
1287
|
+
let list = array(ast::argument_expression(arguments.list.last()?))?;
|
|
1288
|
+
list.elements
|
|
969
1289
|
.iter()
|
|
970
|
-
.map(|element| element
|
|
1290
|
+
.map(|element| arguments.element(element))
|
|
971
1291
|
.collect::<Option<_>>()?
|
|
972
1292
|
} else {
|
|
973
|
-
|
|
1293
|
+
arguments
|
|
1294
|
+
.list
|
|
1295
|
+
.iter()
|
|
1296
|
+
.map(|argument| arguments.argument(argument))
|
|
1297
|
+
.collect()
|
|
974
1298
|
};
|
|
975
|
-
let start =
|
|
976
|
-
Some(
|
|
1299
|
+
let start = operands.len().checked_sub(target.passed?)?;
|
|
1300
|
+
Some(operands[start..].to_vec())
|
|
977
1301
|
}
|
|
978
1302
|
|
|
979
1303
|
/// Checks whether `call` forwards two captured values followed by `function`'s leading
|
|
980
1304
|
/// parameters, as a closure-factory listener does. The function may also declare trailing
|
|
981
1305
|
/// parameters used as local variables.
|
|
982
|
-
fn forwards_parameters(
|
|
983
|
-
|
|
1306
|
+
fn forwards_parameters<'a>(
|
|
1307
|
+
constants: &Constants<'a>,
|
|
1308
|
+
function: &Body<'a>,
|
|
1309
|
+
call: &'a CallExpression<'a>,
|
|
1310
|
+
target: &Invocation<'a>,
|
|
1311
|
+
) -> bool {
|
|
1312
|
+
let (Some(handed), Some(parameters)) =
|
|
1313
|
+
(handed(Arguments::of(call), target), function.parameters())
|
|
1314
|
+
else {
|
|
984
1315
|
return false;
|
|
985
1316
|
};
|
|
986
1317
|
let Some(forwarded) = handed.get(2..) else {
|
|
@@ -990,19 +1321,21 @@ fn forwards_parameters(function: &Function, call: &CallExpr, target: &Invocation
|
|
|
990
1321
|
&& forwarded
|
|
991
1322
|
.iter()
|
|
992
1323
|
.zip(parameters)
|
|
993
|
-
.all(|(argument, parameter)|
|
|
1324
|
+
.all(|(argument, parameter)| {
|
|
1325
|
+
argument.callee(constants) == Some(Callee::symbol(parameter))
|
|
1326
|
+
})
|
|
994
1327
|
}
|
|
995
1328
|
|
|
996
1329
|
/// Arrays a function fills with names before reading them back, such as `g = [f, a]` read as
|
|
997
1330
|
/// `g[0]` and `g[1]`.
|
|
998
|
-
struct Copies<'
|
|
999
|
-
arrays: HashMap<
|
|
1331
|
+
struct Copies<'a, 'c> {
|
|
1332
|
+
arrays: HashMap<Binding<'a>, Vec<Binding<'a>>>,
|
|
1000
1333
|
/// Known constants used to evaluate array indices.
|
|
1001
|
-
constants: &'
|
|
1334
|
+
constants: &'c Constants<'a>,
|
|
1002
1335
|
}
|
|
1003
1336
|
|
|
1004
|
-
impl<'
|
|
1005
|
-
fn new(constants: &'
|
|
1337
|
+
impl<'a, 'c> Copies<'a, 'c> {
|
|
1338
|
+
fn new(constants: &'c Constants<'a>) -> Self {
|
|
1006
1339
|
Self {
|
|
1007
1340
|
arrays: HashMap::new(),
|
|
1008
1341
|
constants,
|
|
@@ -1010,118 +1343,158 @@ impl<'constants> Copies<'constants> {
|
|
|
1010
1343
|
}
|
|
1011
1344
|
|
|
1012
1345
|
/// Records `expression` if it is `array = [name, ...]`.
|
|
1013
|
-
fn record(&mut self, expression: &
|
|
1014
|
-
let
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1346
|
+
fn record(&mut self, expression: &Expression<'a>) -> Option<()> {
|
|
1347
|
+
let Expression::AssignmentExpression(assignment) = expression else {
|
|
1348
|
+
return None;
|
|
1349
|
+
};
|
|
1350
|
+
let AssignmentTarget::AssignmentTargetIdentifier(array) = &assignment.left else {
|
|
1351
|
+
return None;
|
|
1352
|
+
};
|
|
1353
|
+
let Expression::ArrayExpression(list) = &assignment.right else {
|
|
1354
|
+
return None;
|
|
1355
|
+
};
|
|
1356
|
+
let names: Option<Vec<Binding<'a>>> = list
|
|
1357
|
+
.elements
|
|
1020
1358
|
.iter()
|
|
1021
|
-
.map(|element|
|
|
1359
|
+
.map(|element| self.constants.binding(element_expression(element)?))
|
|
1022
1360
|
.collect();
|
|
1023
|
-
self.arrays.insert(
|
|
1361
|
+
self.arrays.insert(self.constants.js.binding(array), names?);
|
|
1024
1362
|
Some(())
|
|
1025
1363
|
}
|
|
1026
1364
|
|
|
1027
|
-
/// The
|
|
1028
|
-
fn resolve(&self, expression: &
|
|
1029
|
-
match expression.
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
let
|
|
1033
|
-
(&*member.obj, &member.prop)
|
|
1034
|
-
else {
|
|
1365
|
+
/// The binding `expression` reads, directly or through an element of a copied array.
|
|
1366
|
+
fn resolve(&self, expression: &Expression<'a>) -> Option<Binding<'a>> {
|
|
1367
|
+
match expression.without_parentheses() {
|
|
1368
|
+
Expression::Identifier(name) => Some(self.constants.js.binding(name)),
|
|
1369
|
+
Expression::ComputedMemberExpression(member) => {
|
|
1370
|
+
let Expression::Identifier(array) = &member.object else {
|
|
1035
1371
|
return None;
|
|
1036
1372
|
};
|
|
1037
|
-
let index = ast::array_index(number(&
|
|
1038
|
-
self.arrays
|
|
1373
|
+
let index = ast::array_index(number(&member.expression, self.constants)?)?;
|
|
1374
|
+
self.arrays
|
|
1375
|
+
.get(&self.constants.js.binding(array))?
|
|
1376
|
+
.get(index)
|
|
1377
|
+
.copied()
|
|
1039
1378
|
}
|
|
1040
1379
|
_ => None,
|
|
1041
1380
|
}
|
|
1042
1381
|
}
|
|
1382
|
+
|
|
1383
|
+
/// The binding `operand` reads. See [`Copies::resolve`].
|
|
1384
|
+
fn resolve_operand(&self, operand: Operand<'a>) -> Option<Binding<'a>> {
|
|
1385
|
+
match operand {
|
|
1386
|
+
Operand::Expression(expression) => self.resolve(expression),
|
|
1387
|
+
Operand::Resolved(Callee::Binding(binding)) => Some(binding),
|
|
1388
|
+
Operand::Resolved(Callee::Original(_)) => None,
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1043
1391
|
}
|
|
1044
1392
|
|
|
1045
1393
|
/// JavaScriptCore's call position: the opening argument parenthesis, which in minified source
|
|
1046
1394
|
/// immediately precedes the first argument. Decoding may change the callee's parentheses but
|
|
1047
1395
|
/// preserves argument positions.
|
|
1048
|
-
fn arguments_position(call: &
|
|
1049
|
-
match call.
|
|
1050
|
-
Some(first) => first.span().
|
|
1051
|
-
None => call.span.
|
|
1396
|
+
fn arguments_position(call: &CallExpression<'_>) -> u32 {
|
|
1397
|
+
match call.arguments.first() {
|
|
1398
|
+
Some(first) => first.span().start - 1,
|
|
1399
|
+
None => call.span.end - 2,
|
|
1052
1400
|
}
|
|
1053
1401
|
}
|
|
1054
1402
|
|
|
1055
1403
|
/// V8's call position: the method or function name, else JavaScriptCore's position.
|
|
1056
|
-
fn call_position(call: &
|
|
1404
|
+
fn call_position(call: &CallExpression<'_>) -> u32 {
|
|
1057
1405
|
match &call.callee {
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
_ => callee.span().hi,
|
|
1062
|
-
},
|
|
1063
|
-
Expr::Ident(_) => call.span.lo,
|
|
1064
|
-
_ => arguments_position(call),
|
|
1406
|
+
callee if let Some(member) = member(callee) => match member {
|
|
1407
|
+
MemberExpression::StaticMemberExpression(member) => member.property.span.start,
|
|
1408
|
+
_ => callee.span().end,
|
|
1065
1409
|
},
|
|
1066
|
-
_ => call.span.
|
|
1410
|
+
Expression::Identifier(_) | Expression::Super(_) => call.span.start,
|
|
1411
|
+
_ => arguments_position(call),
|
|
1067
1412
|
}
|
|
1068
1413
|
}
|
|
1069
1414
|
|
|
1070
1415
|
/// Finds the first call in a dispatch case.
|
|
1071
|
-
struct Case {
|
|
1416
|
+
struct Case<'a> {
|
|
1072
1417
|
span: Span,
|
|
1073
|
-
call:
|
|
1418
|
+
call: &'a CallExpression<'a>,
|
|
1074
1419
|
}
|
|
1075
1420
|
|
|
1076
|
-
fn cases_of(function: &
|
|
1077
|
-
|
|
1078
|
-
|
|
1421
|
+
fn cases_of<'a>(js: ast::Script<'a>, function: &Body<'a>) -> Vec<Case<'a>> {
|
|
1422
|
+
struct Cases<'a> {
|
|
1423
|
+
js: ast::Script<'a>,
|
|
1424
|
+
found: Vec<Case<'a>>,
|
|
1425
|
+
}
|
|
1079
1426
|
// Visitors inspect only the supplied body. Skip nested functions and classes, but include
|
|
1080
1427
|
// arrows.
|
|
1081
|
-
impl Visit for Cases {
|
|
1082
|
-
fn visit_function(&mut self, _: &Function) {}
|
|
1083
|
-
fn visit_class(&mut self, _: &Class) {}
|
|
1084
|
-
fn
|
|
1428
|
+
impl<'a> Visit<'a> for Cases<'a> {
|
|
1429
|
+
fn visit_function(&mut self, _: &Function<'a>, _: ScopeFlags) {}
|
|
1430
|
+
fn visit_class(&mut self, _: &Class<'a>) {}
|
|
1431
|
+
fn visit_object_property(&mut self, property: &ObjectProperty<'a>) {
|
|
1432
|
+
walk_property(self, property);
|
|
1433
|
+
}
|
|
1434
|
+
fn visit_switch_case(&mut self, case: &SwitchCase<'a>) {
|
|
1085
1435
|
if case.test.is_some() {
|
|
1086
|
-
let
|
|
1087
|
-
case.
|
|
1088
|
-
|
|
1089
|
-
self.0.push(Case {
|
|
1436
|
+
let case = self.js.lend(case, AstKind::as_switch_case);
|
|
1437
|
+
if let Some(&call) = calls_in(self.js, &case.consequent).first() {
|
|
1438
|
+
self.found.push(Case {
|
|
1090
1439
|
span: case.span,
|
|
1091
1440
|
call,
|
|
1092
1441
|
});
|
|
1093
1442
|
}
|
|
1094
1443
|
}
|
|
1095
|
-
|
|
1444
|
+
walk::walk_switch_case(self, case);
|
|
1096
1445
|
}
|
|
1097
1446
|
}
|
|
1098
|
-
let mut cases = Cases
|
|
1099
|
-
|
|
1100
|
-
|
|
1447
|
+
let mut cases = Cases {
|
|
1448
|
+
js,
|
|
1449
|
+
found: Vec::new(),
|
|
1450
|
+
};
|
|
1451
|
+
function.visit(&mut cases);
|
|
1452
|
+
cases.found
|
|
1101
1453
|
}
|
|
1102
1454
|
|
|
1103
1455
|
/// Calls made by a function's own body, excluding nested functions.
|
|
1104
|
-
|
|
1105
|
-
|
|
1456
|
+
struct Calls<'a> {
|
|
1457
|
+
js: ast::Script<'a>,
|
|
1458
|
+
found: Vec<&'a CallExpression<'a>>,
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1461
|
+
impl<'a> Visit<'a> for Calls<'a> {
|
|
1462
|
+
fn visit_function(&mut self, _: &Function<'a>, _: ScopeFlags) {}
|
|
1463
|
+
fn visit_class(&mut self, _: &Class<'a>) {}
|
|
1464
|
+
fn visit_object_property(&mut self, property: &ObjectProperty<'a>) {
|
|
1465
|
+
walk_property(self, property);
|
|
1466
|
+
}
|
|
1467
|
+
fn visit_call_expression(&mut self, call: &CallExpression<'a>) {
|
|
1468
|
+
if !is_optional_call(call) {
|
|
1469
|
+
self.found
|
|
1470
|
+
.push(self.js.lend(call, AstKind::as_call_expression));
|
|
1471
|
+
}
|
|
1472
|
+
walk::walk_call_expression(self, call);
|
|
1473
|
+
}
|
|
1474
|
+
}
|
|
1106
1475
|
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1476
|
+
/// Calls made by `statements`, excluding nested functions.
|
|
1477
|
+
fn calls_in<'a>(
|
|
1478
|
+
js: ast::Script<'a>,
|
|
1479
|
+
statements: impl IntoIterator<Item = &'a Statement<'a>>,
|
|
1480
|
+
) -> Vec<&'a CallExpression<'a>> {
|
|
1481
|
+
let mut calls = Calls {
|
|
1482
|
+
js,
|
|
1483
|
+
found: Vec::new(),
|
|
1484
|
+
};
|
|
1485
|
+
for statement in statements {
|
|
1486
|
+
calls.visit_statement(statement);
|
|
1113
1487
|
}
|
|
1488
|
+
calls.found
|
|
1114
1489
|
}
|
|
1115
1490
|
|
|
1116
|
-
fn calls_of(function: &
|
|
1117
|
-
|
|
1118
|
-
function.body.visit_with(&mut calls);
|
|
1119
|
-
calls.0
|
|
1491
|
+
fn calls_of<'a>(js: ast::Script<'a>, function: &Body<'a>) -> Vec<&'a CallExpression<'a>> {
|
|
1492
|
+
calls_in(js, function.statements())
|
|
1120
1493
|
}
|
|
1121
1494
|
|
|
1122
1495
|
/// A call's target, resolved through helper functions.
|
|
1123
|
-
struct Invocation {
|
|
1124
|
-
callee: Option<
|
|
1496
|
+
struct Invocation<'a> {
|
|
1497
|
+
callee: Option<Callee<'a>>,
|
|
1125
1498
|
/// Whether it invokes `callee.call(receiver, ...)`.
|
|
1126
1499
|
method: bool,
|
|
1127
1500
|
/// Intermediate helper frames, innermost first.
|
|
@@ -1132,10 +1505,23 @@ struct Invocation {
|
|
|
1132
1505
|
/// Whether the final argument is an array literal containing the arguments passed to `callee`.
|
|
1133
1506
|
listed: bool,
|
|
1134
1507
|
/// The array slot the called function is read from, if any.
|
|
1135
|
-
slot: Option<(
|
|
1508
|
+
slot: Option<(Binding<'a>, usize)>,
|
|
1136
1509
|
}
|
|
1137
1510
|
|
|
1138
|
-
impl Invocation {
|
|
1511
|
+
impl Invocation<'_> {
|
|
1512
|
+
/// An unresolved call at `site`.
|
|
1513
|
+
fn at(site: Site) -> Self {
|
|
1514
|
+
Self {
|
|
1515
|
+
callee: None,
|
|
1516
|
+
method: false,
|
|
1517
|
+
helpers: Vec::new(),
|
|
1518
|
+
site,
|
|
1519
|
+
passed: None,
|
|
1520
|
+
listed: false,
|
|
1521
|
+
slot: None,
|
|
1522
|
+
}
|
|
1523
|
+
}
|
|
1524
|
+
|
|
1139
1525
|
/// The frames of the call made from `caller`, innermost first.
|
|
1140
1526
|
fn frames(&self, caller: Name) -> Vec<Call> {
|
|
1141
1527
|
let mut frames = self.helpers.clone();
|
|
@@ -1147,224 +1533,186 @@ impl Invocation {
|
|
|
1147
1533
|
}
|
|
1148
1534
|
}
|
|
1149
1535
|
|
|
1150
|
-
///
|
|
1151
|
-
|
|
1152
|
-
///
|
|
1153
|
-
/// Handles copied selectors such as `(s = [selector], mask ^ s[0])`, indirect references to
|
|
1154
|
-
/// `apply`, copied arguments such as `(g = [f, a], (0, g[0])(g[1]))`, and masks or labels computed
|
|
1155
|
-
/// from constants.
|
|
1156
|
-
struct Applier {
|
|
1536
|
+
/// A `switch (selector ^ mask)` whose case labels each select a `T`.
|
|
1537
|
+
struct Selection<T> {
|
|
1157
1538
|
mask: f64,
|
|
1158
|
-
cases: Vec<
|
|
1539
|
+
cases: Vec<(f64, T)>,
|
|
1159
1540
|
}
|
|
1160
1541
|
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1542
|
+
impl<T: Copy> Selection<T> {
|
|
1543
|
+
/// The case `selector` selects.
|
|
1544
|
+
fn select(&self, selector: f64) -> Option<T> {
|
|
1545
|
+
let chosen = f64::from(js::int32(self.mask) ^ js::int32(selector));
|
|
1546
|
+
self.cases
|
|
1547
|
+
.iter()
|
|
1548
|
+
.find(|(label, _)| *label == chosen)
|
|
1549
|
+
.map(|&(_, case)| case)
|
|
1550
|
+
}
|
|
1164
1551
|
}
|
|
1165
1552
|
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1553
|
+
/// Recovers the `Reflect.apply` call site of each case of a selector-based helper. Calls can use
|
|
1554
|
+
/// `Reflect.apply(f, _, list)` or `f(a, b)` with later parameters forwarded in order.
|
|
1555
|
+
///
|
|
1556
|
+
/// Handles copied selectors such as `(s = [selector], mask ^ s[0])`, indirect references to
|
|
1557
|
+
/// `apply`, copied arguments such as `(g = [f, a], (0, g[0])(g[1]))`, and masks or labels computed
|
|
1558
|
+
/// from constants.
|
|
1559
|
+
fn applier<'a>(function: &Body<'a>, constants: &Constants<'a>) -> Option<Selection<Site>> {
|
|
1560
|
+
let parameters = function.parameters()?;
|
|
1561
|
+
let [selector, applied, rest @ ..] = parameters.as_slice() else {
|
|
1562
|
+
return None;
|
|
1563
|
+
};
|
|
1564
|
+
let (mask, switch, copies) = selection(function, *selector, constants)?;
|
|
1565
|
+
let is = |binding: Option<Binding<'a>>, parameter: &SymbolId| {
|
|
1566
|
+
binding == Some(Binding::Symbol(*parameter))
|
|
1567
|
+
};
|
|
1568
|
+
// Match `f(a, b)` when arguments follow parameter order.
|
|
1569
|
+
let passes_on = |call: &CallExpression<'a>| {
|
|
1570
|
+
let (callee, _) = undetached(&call.callee, constants);
|
|
1571
|
+
is(copies.resolve(callee), applied)
|
|
1572
|
+
&& call.arguments.len() <= rest.len()
|
|
1573
|
+
&& call
|
|
1574
|
+
.arguments
|
|
1575
|
+
.iter()
|
|
1576
|
+
.zip(rest)
|
|
1577
|
+
.all(|(argument, parameter)| match argument {
|
|
1578
|
+
Argument::SpreadElement(_) => false,
|
|
1579
|
+
argument => is(copies.resolve(argument.to_expression()), parameter),
|
|
1184
1580
|
})
|
|
1581
|
+
};
|
|
1582
|
+
let applies = |call: &CallExpression<'a>| {
|
|
1583
|
+
let [first, _, third] = &call.arguments[..] else {
|
|
1584
|
+
return false;
|
|
1185
1585
|
};
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1586
|
+
member(call.callee.without_parentheses()).is_some()
|
|
1587
|
+
&& is(constants.binding(ast::argument_expression(first)), applied)
|
|
1588
|
+
&& rest
|
|
1589
|
+
.first()
|
|
1590
|
+
.is_some_and(|list| is(constants.binding(ast::argument_expression(third)), list))
|
|
1591
|
+
};
|
|
1592
|
+
let cases: Vec<(f64, Site)> = switch
|
|
1593
|
+
.cases
|
|
1594
|
+
.iter()
|
|
1595
|
+
.filter_map(|case| {
|
|
1596
|
+
let label = number(case.test.as_ref()?, constants)?;
|
|
1597
|
+
let [Statement::ReturnStatement(returned)] = &case.consequent[..] else {
|
|
1598
|
+
return None;
|
|
1189
1599
|
};
|
|
1190
|
-
call
|
|
1191
|
-
.
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
.iter()
|
|
1201
|
-
.filter_map(|case| {
|
|
1202
|
-
let label = number(case.test.as_deref()?, constants)?;
|
|
1203
|
-
let [Stmt::Return(returned)] = &case.cons[..] else {
|
|
1204
|
-
return None;
|
|
1205
|
-
};
|
|
1206
|
-
let Some(Expr::Call(call)) = returned.arg.as_deref().map(Expr::unwrap_parens)
|
|
1207
|
-
else {
|
|
1208
|
-
return None;
|
|
1209
|
-
};
|
|
1210
|
-
(applies(call) || passes_on(call)).then(|| ApplierCase {
|
|
1211
|
-
label,
|
|
1212
|
-
site: Site::new(call),
|
|
1213
|
-
})
|
|
1214
|
-
})
|
|
1215
|
-
.collect();
|
|
1216
|
-
// Other cases may call more helpers. Selectors routed there have no case in this helper.
|
|
1217
|
-
(!cases.is_empty()).then_some(Self { mask, cases })
|
|
1218
|
-
}
|
|
1219
|
-
|
|
1220
|
-
/// The `Reflect.apply` call in the case `selector` selects.
|
|
1221
|
-
fn call(&self, selector: f64) -> Option<Site> {
|
|
1222
|
-
let chosen = f64::from(js::int32(self.mask) ^ js::int32(selector));
|
|
1223
|
-
self.cases
|
|
1224
|
-
.iter()
|
|
1225
|
-
.find(|case| case.label == chosen)
|
|
1226
|
-
.map(|case| case.site)
|
|
1227
|
-
}
|
|
1600
|
+
let Expression::CallExpression(call) =
|
|
1601
|
+
returned.argument.as_ref()?.without_parentheses()
|
|
1602
|
+
else {
|
|
1603
|
+
return None;
|
|
1604
|
+
};
|
|
1605
|
+
(applies(call) || passes_on(call)).then(|| (label, Site::new(call)))
|
|
1606
|
+
})
|
|
1607
|
+
.collect();
|
|
1608
|
+
// Other cases may call more helpers. Selectors routed there have no case in this helper.
|
|
1609
|
+
(!cases.is_empty()).then_some(Selection { mask, cases })
|
|
1228
1610
|
}
|
|
1229
1611
|
|
|
1230
1612
|
/// Extracts the mask and switch from a function containing `switch (selector ^ mask)`, with
|
|
1231
1613
|
/// optional leading local declarations. Also tracks arrays used to copy selector names, as in `(s =
|
|
1232
1614
|
/// [selector], mask ^ s[0])`.
|
|
1233
|
-
fn selection<'
|
|
1234
|
-
function: &'
|
|
1235
|
-
selector:
|
|
1236
|
-
constants: &'
|
|
1237
|
-
) -> Option<(f64, &'
|
|
1238
|
-
let
|
|
1615
|
+
fn selection<'a, 'c>(
|
|
1616
|
+
function: &Body<'a>,
|
|
1617
|
+
selector: SymbolId,
|
|
1618
|
+
constants: &'c Constants<'a>,
|
|
1619
|
+
) -> Option<(f64, &'a SwitchStatement<'a>, Copies<'a, 'c>)> {
|
|
1620
|
+
let Statement::SwitchStatement(switch) = function.only_statement()? else {
|
|
1239
1621
|
return None;
|
|
1240
1622
|
};
|
|
1241
|
-
let mut discriminant = switch.discriminant.
|
|
1623
|
+
let mut discriminant = switch.discriminant.without_parentheses();
|
|
1242
1624
|
let mut copies = Copies::new(constants);
|
|
1243
|
-
if let
|
|
1244
|
-
let (last, before) = sequence.
|
|
1625
|
+
if let Expression::SequenceExpression(sequence) = discriminant {
|
|
1626
|
+
let (last, before) = sequence.expressions.split_last()?;
|
|
1245
1627
|
for expression in before {
|
|
1246
1628
|
copies.record(expression);
|
|
1247
1629
|
}
|
|
1248
|
-
discriminant = last.
|
|
1630
|
+
discriminant = last.without_parentheses();
|
|
1249
1631
|
}
|
|
1250
|
-
let xor = discriminant
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1632
|
+
let Expression::BinaryExpression(xor) = discriminant else {
|
|
1633
|
+
return None;
|
|
1634
|
+
};
|
|
1635
|
+
if xor.operator != BinaryOperator::BitwiseXOR {
|
|
1636
|
+
return None;
|
|
1637
|
+
}
|
|
1638
|
+
let is_selector =
|
|
1639
|
+
|expression: &Expression<'a>| copies.resolve(expression) == Some(Binding::Symbol(selector));
|
|
1640
|
+
let mask = match (
|
|
1641
|
+
xor.left.without_parentheses(),
|
|
1642
|
+
xor.right.without_parentheses(),
|
|
1643
|
+
) {
|
|
1255
1644
|
(other, mask) | (mask, other) if is_selector(other) => number(mask, constants)?,
|
|
1256
1645
|
_ => return None,
|
|
1257
1646
|
};
|
|
1258
1647
|
Some((mask, switch, copies))
|
|
1259
1648
|
}
|
|
1260
1649
|
|
|
1261
|
-
///
|
|
1262
|
-
|
|
1263
|
-
let start = statements
|
|
1264
|
-
.iter()
|
|
1265
|
-
.position(|statement| {
|
|
1266
|
-
!matches!(statement, Stmt::Decl(Decl::Var(declaration))
|
|
1267
|
-
if declaration.decls.iter().all(|declarator| declarator.init.is_none()))
|
|
1268
|
-
})
|
|
1269
|
-
.unwrap_or(statements.len());
|
|
1270
|
-
&statements[start..]
|
|
1271
|
-
}
|
|
1272
|
-
|
|
1273
|
-
/// A function combining several original functions, selected by its first parameter:
|
|
1650
|
+
/// Recovers the original functions of a function that combines several, selected by its first
|
|
1651
|
+
/// parameter:
|
|
1274
1652
|
///
|
|
1275
1653
|
/// ```js
|
|
1276
1654
|
/// function merged(selector, a) { switch (selector ^ mask) { case 1: { <original> } return; } }
|
|
1277
1655
|
/// ```
|
|
1278
1656
|
///
|
|
1279
1657
|
/// Both V8 and JavaScriptCore name every original function's frame after the merged function.
|
|
1280
|
-
///
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
cases
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
name: &Id,
|
|
1290
|
-
function: &Function,
|
|
1291
|
-
constants: &HashMap<Id, f64>,
|
|
1292
|
-
) -> Option<(Self, Vec<(Id, Function)>)> {
|
|
1293
|
-
let selector = function.params.first()?.pat.as_ident()?.to_id();
|
|
1294
|
-
let (mask, switch, _) = selection(function, &selector, constants)?;
|
|
1295
|
-
let block = function.body.as_ref()?;
|
|
1296
|
-
let mut cases = Vec::new();
|
|
1297
|
-
let mut originals = Vec::new();
|
|
1298
|
-
for (at, case) in switch.cases.iter().enumerate() {
|
|
1299
|
-
let label = case
|
|
1300
|
-
.test
|
|
1301
|
-
.as_deref()
|
|
1302
|
-
.and_then(|test| number(test, constants));
|
|
1303
|
-
// Labels sharing a body fall through to it.
|
|
1304
|
-
let body = switch.cases[at..]
|
|
1305
|
-
.iter()
|
|
1306
|
-
.map(|case| &case.cons)
|
|
1307
|
-
.find(|body| !body.is_empty());
|
|
1308
|
-
let (Some(label), Some(body)) = (label, body) else {
|
|
1309
|
-
continue;
|
|
1310
|
-
};
|
|
1311
|
-
let mut statements: Vec<Stmt> = match &body[..] {
|
|
1312
|
-
[Stmt::Block(original), rest @ ..] => {
|
|
1313
|
-
original.stmts.iter().chain(rest).cloned().collect()
|
|
1314
|
-
}
|
|
1315
|
-
_ => body.clone(),
|
|
1316
|
-
};
|
|
1317
|
-
if let Some(Stmt::Return(ReturnStmt { arg: None, .. })) = statements.last() {
|
|
1318
|
-
statements.pop();
|
|
1319
|
-
}
|
|
1320
|
-
let original = Function {
|
|
1321
|
-
params: function.params[1..].to_vec(),
|
|
1322
|
-
span: function.span,
|
|
1323
|
-
ctxt: function.ctxt,
|
|
1324
|
-
body: Some(BlockStmt {
|
|
1325
|
-
span: block.span,
|
|
1326
|
-
ctxt: block.ctxt,
|
|
1327
|
-
stmts: statements,
|
|
1328
|
-
}),
|
|
1329
|
-
..Function::default()
|
|
1330
|
-
};
|
|
1331
|
-
let binding = (
|
|
1332
|
-
name.0.clone(),
|
|
1333
|
-
SyntaxContext::empty().apply_mark(Mark::new()),
|
|
1334
|
-
);
|
|
1335
|
-
cases.push((label, binding.clone()));
|
|
1336
|
-
originals.push((binding, original));
|
|
1337
|
-
}
|
|
1338
|
-
(!cases.is_empty()).then_some((Self { mask, cases }, originals))
|
|
1339
|
-
}
|
|
1340
|
-
|
|
1341
|
-
/// Returns the original function selected by `selector`.
|
|
1342
|
-
fn original(&self, selector: f64) -> Option<&Id> {
|
|
1343
|
-
let chosen = f64::from(js::int32(self.mask) ^ js::int32(selector));
|
|
1344
|
-
self.cases
|
|
1658
|
+
/// Each original keeps the merged function's other parameters.
|
|
1659
|
+
fn merged<'a>(function: &Body<'a>, constants: &Constants<'a>) -> Option<Selection<Body<'a>>> {
|
|
1660
|
+
let selector = plain_parameter(function.parameters.first()?)?;
|
|
1661
|
+
let (mask, switch, _) = selection(function, selector, constants)?;
|
|
1662
|
+
let mut cases = Vec::new();
|
|
1663
|
+
for (at, case) in switch.cases.iter().enumerate() {
|
|
1664
|
+
let label = case.test.as_ref().and_then(|test| number(test, constants));
|
|
1665
|
+
// Labels sharing a body fall through to it.
|
|
1666
|
+
let body = switch.cases[at..]
|
|
1345
1667
|
.iter()
|
|
1346
|
-
.
|
|
1347
|
-
.
|
|
1668
|
+
.map(|case| &case.consequent)
|
|
1669
|
+
.find(|body| !body.is_empty());
|
|
1670
|
+
let (Some(label), Some(body)) = (label, body) else {
|
|
1671
|
+
continue;
|
|
1672
|
+
};
|
|
1673
|
+
let (mut head, mut tail): (&[Statement], &[Statement]) = match &body[..] {
|
|
1674
|
+
[Statement::BlockStatement(original), rest @ ..] => (&original.body[..], rest),
|
|
1675
|
+
body => (body, &[]),
|
|
1676
|
+
};
|
|
1677
|
+
let last = if tail.is_empty() {
|
|
1678
|
+
&mut head
|
|
1679
|
+
} else {
|
|
1680
|
+
&mut tail
|
|
1681
|
+
};
|
|
1682
|
+
if let [statements @ .., Statement::ReturnStatement(returned)] = *last
|
|
1683
|
+
&& returned.argument.is_none()
|
|
1684
|
+
{
|
|
1685
|
+
*last = statements;
|
|
1686
|
+
}
|
|
1687
|
+
let body = Body {
|
|
1688
|
+
parameters: &function.parameters[1..],
|
|
1689
|
+
rest: function.rest,
|
|
1690
|
+
prologue: false,
|
|
1691
|
+
head,
|
|
1692
|
+
tail,
|
|
1693
|
+
};
|
|
1694
|
+
cases.push((label, body));
|
|
1348
1695
|
}
|
|
1696
|
+
(!cases.is_empty()).then_some(Selection { mask, cases })
|
|
1349
1697
|
}
|
|
1350
1698
|
|
|
1351
1699
|
/// Storage location in a scope: a binding or a constant array slot.
|
|
1352
|
-
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
|
1353
|
-
enum Holder {
|
|
1354
|
-
Name(
|
|
1355
|
-
Slot(
|
|
1700
|
+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
|
1701
|
+
enum Holder<'a> {
|
|
1702
|
+
Name(Binding<'a>),
|
|
1703
|
+
Slot(Binding<'a>, usize),
|
|
1356
1704
|
}
|
|
1357
1705
|
|
|
1358
1706
|
/// A function's local aliases, string constants, and forwarders.
|
|
1359
|
-
struct Scope<'
|
|
1360
|
-
appliers: &'
|
|
1707
|
+
struct Scope<'a, 's> {
|
|
1708
|
+
appliers: &'s Appliers<'a>,
|
|
1361
1709
|
/// V8's name prefix for functions assigned here, such as `Owner.`.
|
|
1362
1710
|
prefix: String,
|
|
1363
|
-
forwarders: Forwarders
|
|
1711
|
+
forwarders: Forwarders<'a>,
|
|
1364
1712
|
/// Assignment count for each storage location. Only values assigned once are known.
|
|
1365
|
-
assigned: HashMap<Holder
|
|
1366
|
-
aliases: HashMap<
|
|
1367
|
-
strings: HashMap<
|
|
1713
|
+
assigned: HashMap<Holder<'a>, usize>,
|
|
1714
|
+
aliases: HashMap<Binding<'a>, Binding<'a>>,
|
|
1715
|
+
strings: HashMap<Binding<'a>, &'a str>,
|
|
1368
1716
|
}
|
|
1369
1717
|
|
|
1370
1718
|
/// Frames for an object function that forwards its first parameter with the remaining arguments.
|
|
@@ -1376,14 +1724,13 @@ struct Forwarder {
|
|
|
1376
1724
|
plain: Vec<Call>,
|
|
1377
1725
|
}
|
|
1378
1726
|
|
|
1379
|
-
impl<'
|
|
1727
|
+
impl<'a, 's> Scope<'a, 's> {
|
|
1380
1728
|
/// `name` is empty for anonymous functions. V8 prefixes the names of functions assigned
|
|
1381
1729
|
/// inside a capitalized function with its name, as it does for constructors.
|
|
1382
|
-
fn new(name: &str, function: &
|
|
1383
|
-
let constructor = name.starts_with(char::is_uppercase);
|
|
1730
|
+
fn new(name: &str, function: &Body<'a>, appliers: &'s Appliers<'a>) -> Self {
|
|
1384
1731
|
let mut scope = Self {
|
|
1385
1732
|
appliers,
|
|
1386
|
-
prefix: if
|
|
1733
|
+
prefix: if name.starts_with(char::is_uppercase) {
|
|
1387
1734
|
format!("{name}.")
|
|
1388
1735
|
} else {
|
|
1389
1736
|
String::new()
|
|
@@ -1393,157 +1740,199 @@ impl<'script> Scope<'script> {
|
|
|
1393
1740
|
aliases: HashMap::new(),
|
|
1394
1741
|
strings: HashMap::new(),
|
|
1395
1742
|
};
|
|
1396
|
-
function.
|
|
1743
|
+
function.visit(&mut scope);
|
|
1397
1744
|
scope.forwarders.retain_assigned_once(&scope.assigned);
|
|
1398
1745
|
scope
|
|
1399
1746
|
}
|
|
1400
1747
|
|
|
1401
|
-
fn
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1748
|
+
fn js(&self) -> ast::Script<'a> {
|
|
1749
|
+
self.appliers.js()
|
|
1750
|
+
}
|
|
1751
|
+
|
|
1752
|
+
fn target(&self, call: &'a CallExpression<'a>) -> Result<Invocation<'a>> {
|
|
1753
|
+
self.resolve(call, None)
|
|
1754
|
+
}
|
|
1755
|
+
|
|
1756
|
+
/// Resolves `call`, reading arguments through `copies` when given.
|
|
1757
|
+
fn resolve(
|
|
1758
|
+
&self,
|
|
1759
|
+
call: &'a CallExpression<'a>,
|
|
1760
|
+
copies: Option<&Copies<'a, '_>>,
|
|
1761
|
+
) -> Result<Invocation<'a>> {
|
|
1762
|
+
let site = Site::new(call);
|
|
1763
|
+
let arguments = Arguments {
|
|
1764
|
+
list: &call.arguments,
|
|
1765
|
+
copies,
|
|
1413
1766
|
};
|
|
1414
|
-
let
|
|
1415
|
-
let (callee, detached) = undetached(callee, &self.appliers.constants);
|
|
1767
|
+
let (callee, detached) = undetached(&call.callee, &self.appliers.constants);
|
|
1416
1768
|
// Resolve the function called by `target`.
|
|
1417
|
-
let
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
return Ok(target);
|
|
1424
|
-
}
|
|
1425
|
-
if let Some((applied, function)) = self.applier_call(name, call)? {
|
|
1426
|
-
target = applied;
|
|
1427
|
-
Some(function)
|
|
1428
|
-
} else {
|
|
1429
|
-
target.passed = Some(arguments);
|
|
1430
|
-
Some(callee)
|
|
1431
|
-
}
|
|
1769
|
+
if let Expression::Identifier(name) = callee {
|
|
1770
|
+
let binding = self.js().binding(name);
|
|
1771
|
+
if let Some((original, arguments)) = self.merged_call(binding, arguments) {
|
|
1772
|
+
// The original function uses the merged function's frame at this call site.
|
|
1773
|
+
let original = Callee::Original(original);
|
|
1774
|
+
return self.named(original, Operand::Resolved(original), arguments, site);
|
|
1432
1775
|
}
|
|
1776
|
+
return self.named(
|
|
1777
|
+
Callee::Binding(binding),
|
|
1778
|
+
Operand::Expression(callee),
|
|
1779
|
+
arguments,
|
|
1780
|
+
site,
|
|
1781
|
+
);
|
|
1782
|
+
}
|
|
1783
|
+
let mut target = Invocation::at(site);
|
|
1784
|
+
let count = arguments.list.len();
|
|
1785
|
+
let function = match member(callee) {
|
|
1433
1786
|
// Some builds keep the key in a local: `handler[c](this)` with `c = "call"`.
|
|
1434
|
-
|
|
1435
|
-
if !detached
|
|
1787
|
+
Some(member)
|
|
1788
|
+
if !detached
|
|
1789
|
+
&& matches!(member.object(), Expression::Identifier(_))
|
|
1790
|
+
&& self.key(member) == Some("call") =>
|
|
1436
1791
|
{
|
|
1437
1792
|
target.method = true;
|
|
1438
|
-
target.passed = Some(
|
|
1439
|
-
Some(
|
|
1793
|
+
target.passed = Some(count.saturating_sub(1));
|
|
1794
|
+
Some(Operand::Expression(member.object()))
|
|
1440
1795
|
}
|
|
1441
1796
|
// The first argument to a forwarder is the function it calls.
|
|
1442
|
-
|
|
1797
|
+
Some(member) if let Some(forwarder) = self.member_forwarder(member) => {
|
|
1443
1798
|
target.helpers = if detached {
|
|
1444
1799
|
forwarder.plain.clone()
|
|
1445
1800
|
} else {
|
|
1446
1801
|
forwarder.method.clone()
|
|
1447
1802
|
};
|
|
1448
|
-
target.passed = Some(
|
|
1449
|
-
|
|
1803
|
+
target.passed = Some(count.saturating_sub(1));
|
|
1804
|
+
arguments
|
|
1805
|
+
.list
|
|
1806
|
+
.first()
|
|
1807
|
+
.map(|first| arguments.argument(first))
|
|
1450
1808
|
}
|
|
1451
1809
|
// Resolve an array-stored function, as in `(0, slots[i])(f, a)`.
|
|
1452
|
-
|
|
1453
|
-
target.passed = Some(
|
|
1454
|
-
Some(callee)
|
|
1810
|
+
Some(member) if self.slot(member).is_some() => {
|
|
1811
|
+
target.passed = Some(count);
|
|
1812
|
+
Some(Operand::Expression(callee))
|
|
1455
1813
|
}
|
|
1456
1814
|
_ => None,
|
|
1457
1815
|
};
|
|
1816
|
+
Ok(self.finish(target, function, arguments))
|
|
1817
|
+
}
|
|
1818
|
+
|
|
1819
|
+
/// Resolves a call to the function `callee`, read as `operand`, directly or through a call
|
|
1820
|
+
/// helper.
|
|
1821
|
+
fn named(
|
|
1822
|
+
&self,
|
|
1823
|
+
callee: Callee<'a>,
|
|
1824
|
+
operand: Operand<'a>,
|
|
1825
|
+
arguments: Arguments<'a, '_>,
|
|
1826
|
+
site: Site,
|
|
1827
|
+
) -> Result<Invocation<'a>> {
|
|
1828
|
+
let (target, function) = match self.applier_call(callee, arguments, site)? {
|
|
1829
|
+
Some(applied) => applied,
|
|
1830
|
+
None => {
|
|
1831
|
+
let mut target = Invocation::at(site);
|
|
1832
|
+
target.passed = Some(arguments.list.len());
|
|
1833
|
+
(target, operand)
|
|
1834
|
+
}
|
|
1835
|
+
};
|
|
1836
|
+
Ok(self.finish(target, Some(function), arguments))
|
|
1837
|
+
}
|
|
1838
|
+
|
|
1839
|
+
/// Follows stored forwarders from `function`, then records the resolved callee and slot.
|
|
1840
|
+
fn finish(
|
|
1841
|
+
&self,
|
|
1842
|
+
mut target: Invocation<'a>,
|
|
1843
|
+
mut function: Option<Operand<'a>>,
|
|
1844
|
+
arguments: Arguments<'a, '_>,
|
|
1845
|
+
) -> Invocation<'a> {
|
|
1458
1846
|
// A stored forwarder calls its first argument with the remaining arguments.
|
|
1459
|
-
while let Some(
|
|
1460
|
-
&& let Some(frames) = self.held_by(
|
|
1461
|
-
&& let Some(first) =
|
|
1847
|
+
while let Some(operand) = function
|
|
1848
|
+
&& let Some(frames) = self.held_by(operand)
|
|
1849
|
+
&& let Some(first) =
|
|
1850
|
+
handed(arguments, &target).and_then(|handed| handed.first().copied())
|
|
1462
1851
|
{
|
|
1463
1852
|
function = Some(first);
|
|
1464
1853
|
target.helpers = frames.iter().cloned().chain(target.helpers).collect();
|
|
1465
1854
|
target.passed = target.passed.map(|passed| passed - 1);
|
|
1466
1855
|
}
|
|
1467
|
-
target.callee = function.and_then(
|
|
1856
|
+
target.callee = function.and_then(|function| function.callee(&self.appliers.constants));
|
|
1468
1857
|
target.slot = function.and_then(|function| match self.holder(function)? {
|
|
1469
1858
|
Holder::Slot(array, index) => Some((array, index)),
|
|
1470
1859
|
Holder::Name(_) => None,
|
|
1471
1860
|
});
|
|
1472
|
-
|
|
1473
|
-
}
|
|
1474
|
-
|
|
1475
|
-
///
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1861
|
+
target
|
|
1862
|
+
}
|
|
1863
|
+
|
|
1864
|
+
/// Resolves `merged(selector, a, b)` to the selected original function and the arguments
|
|
1865
|
+
/// `a, b`.
|
|
1866
|
+
fn merged_call<'c>(
|
|
1867
|
+
&self,
|
|
1868
|
+
callee: Binding<'a>,
|
|
1869
|
+
arguments: Arguments<'a, 'c>,
|
|
1870
|
+
) -> Option<(usize, Arguments<'a, 'c>)> {
|
|
1871
|
+
let merged = self.appliers.merged.get(&callee)?;
|
|
1872
|
+
let (selector, rest) = arguments.list.split_first()?;
|
|
1873
|
+
let original = merged.select(self.appliers.number(arguments.argument(selector))?)?;
|
|
1874
|
+
Some((
|
|
1875
|
+
original,
|
|
1876
|
+
Arguments {
|
|
1877
|
+
list: rest,
|
|
1878
|
+
copies: arguments.copies,
|
|
1879
|
+
},
|
|
1880
|
+
))
|
|
1491
1881
|
}
|
|
1492
1882
|
|
|
1493
1883
|
/// Resolves `applier(selector, f, [args])` or `applier(selector, f, a, b)` through a call
|
|
1494
1884
|
/// helper. Also returns the called function `f`.
|
|
1495
|
-
fn applier_call
|
|
1885
|
+
fn applier_call(
|
|
1496
1886
|
&self,
|
|
1497
|
-
callee:
|
|
1498
|
-
|
|
1499
|
-
|
|
1887
|
+
callee: Callee<'a>,
|
|
1888
|
+
arguments: Arguments<'a, '_>,
|
|
1889
|
+
site: Site,
|
|
1890
|
+
) -> Result<Option<(Invocation<'a>, Operand<'a>)>> {
|
|
1500
1891
|
let (Some(applier), [selector, function, passed @ ..]) =
|
|
1501
|
-
(self.appliers.helpers.get(&callee
|
|
1892
|
+
(self.appliers.helpers.get(&callee), arguments.list)
|
|
1502
1893
|
else {
|
|
1503
1894
|
return Ok(None);
|
|
1504
1895
|
};
|
|
1505
|
-
let Some(selector) = self.appliers.number(
|
|
1896
|
+
let Some(selector) = self.appliers.number(arguments.argument(selector)) else {
|
|
1506
1897
|
return Ok(None);
|
|
1507
1898
|
};
|
|
1899
|
+
let name = self.appliers.name(callee);
|
|
1508
1900
|
let inner = applier
|
|
1509
|
-
.
|
|
1510
|
-
.with_context(|| format!("{} has no case for selector {selector}"
|
|
1901
|
+
.select(selector)
|
|
1902
|
+
.with_context(|| format!("{name} has no case for selector {selector}"))?;
|
|
1511
1903
|
let (passed, listed) = match passed {
|
|
1512
|
-
[list] if let Some(list) =
|
|
1513
|
-
(list.
|
|
1904
|
+
[list] if let Some(list) = array(ast::argument_expression(list)) => {
|
|
1905
|
+
(list.elements.len(), true)
|
|
1514
1906
|
}
|
|
1515
1907
|
passed => (passed.len(), false),
|
|
1516
1908
|
};
|
|
1517
|
-
let invocation = Invocation
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
passed: Some(passed),
|
|
1526
|
-
listed,
|
|
1527
|
-
slot: None,
|
|
1528
|
-
};
|
|
1529
|
-
Ok(Some((invocation, &function.expr)))
|
|
1909
|
+
let mut invocation = Invocation::at(site);
|
|
1910
|
+
invocation.helpers = vec![Call {
|
|
1911
|
+
name: Name::same(name.to_owned()),
|
|
1912
|
+
site: inner,
|
|
1913
|
+
}];
|
|
1914
|
+
invocation.passed = Some(passed);
|
|
1915
|
+
invocation.listed = listed;
|
|
1916
|
+
Ok(Some((invocation, arguments.argument(function))))
|
|
1530
1917
|
}
|
|
1531
1918
|
|
|
1532
1919
|
/// Resolves a member key from a literal or a local string binding.
|
|
1533
|
-
fn key
|
|
1534
|
-
match
|
|
1535
|
-
|
|
1536
|
-
|
|
1920
|
+
fn key(&self, member: &MemberExpression<'a>) -> Option<&'a str> {
|
|
1921
|
+
match member {
|
|
1922
|
+
MemberExpression::ComputedMemberExpression(computed)
|
|
1923
|
+
if let Expression::Identifier(name) = &computed.expression =>
|
|
1924
|
+
{
|
|
1925
|
+
self.strings.get(&self.js().binding(name)).copied()
|
|
1537
1926
|
}
|
|
1538
|
-
|
|
1927
|
+
member => ast::member_str(member),
|
|
1539
1928
|
}
|
|
1540
1929
|
}
|
|
1541
1930
|
|
|
1542
1931
|
/// Finds the forwarder read by `object.key`.
|
|
1543
|
-
fn member_forwarder(&self, member: &
|
|
1932
|
+
fn member_forwarder(&self, member: &'a MemberExpression<'a>) -> Option<&Forwarder> {
|
|
1544
1933
|
let key = (
|
|
1545
|
-
self.holder(
|
|
1546
|
-
self.key(
|
|
1934
|
+
self.holder(Operand::Expression(member.object()))?,
|
|
1935
|
+
self.key(member)?,
|
|
1547
1936
|
);
|
|
1548
1937
|
self.forwarders
|
|
1549
1938
|
.on_objects
|
|
@@ -1551,74 +1940,85 @@ impl<'script> Scope<'script> {
|
|
|
1551
1940
|
.or_else(|| self.appliers.forwarders.on_objects.get(&key))
|
|
1552
1941
|
}
|
|
1553
1942
|
|
|
1554
|
-
/// Returns frames for the stored forwarder read by `
|
|
1555
|
-
fn held_by(&self,
|
|
1556
|
-
let holder = self.holder(
|
|
1943
|
+
/// Returns frames for the stored forwarder read by `operand`.
|
|
1944
|
+
fn held_by(&self, operand: Operand<'a>) -> Option<&Vec<Call>> {
|
|
1945
|
+
let holder = self.holder(operand)?;
|
|
1557
1946
|
self.forwarders
|
|
1558
1947
|
.held
|
|
1559
1948
|
.get(&holder)
|
|
1560
1949
|
.or_else(|| self.appliers.forwarders.held.get(&holder))
|
|
1561
1950
|
}
|
|
1562
1951
|
|
|
1563
|
-
/// Resolves the value read by `
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1952
|
+
/// Resolves the value read by `operand`, following local aliases. An original function is
|
|
1953
|
+
/// never stored.
|
|
1954
|
+
fn holder(&self, operand: Operand<'a>) -> Option<Holder<'a>> {
|
|
1955
|
+
match operand {
|
|
1956
|
+
Operand::Expression(expression) => match expression.without_parentheses() {
|
|
1957
|
+
Expression::Identifier(name) => {
|
|
1958
|
+
Some(Holder::Name(self.object(self.js().binding(name))))
|
|
1959
|
+
}
|
|
1960
|
+
expression => self.slot(member(expression)?),
|
|
1961
|
+
},
|
|
1962
|
+
Operand::Resolved(Callee::Binding(binding)) => Some(Holder::Name(self.object(binding))),
|
|
1963
|
+
Operand::Resolved(Callee::Original(_)) => None,
|
|
1569
1964
|
}
|
|
1570
1965
|
}
|
|
1571
1966
|
|
|
1572
1967
|
/// Resolves `array[index]` when the index is constant.
|
|
1573
|
-
fn slot(&self, member: &
|
|
1574
|
-
let
|
|
1575
|
-
|
|
1968
|
+
fn slot(&self, member: &MemberExpression<'a>) -> Option<Holder<'a>> {
|
|
1969
|
+
let MemberExpression::ComputedMemberExpression(member) = member else {
|
|
1970
|
+
return None;
|
|
1971
|
+
};
|
|
1972
|
+
let Expression::Identifier(array) = &member.object else {
|
|
1973
|
+
return None;
|
|
1974
|
+
};
|
|
1975
|
+
let index = number(&member.expression, &self.appliers.constants)?;
|
|
1576
1976
|
Some(Holder::Slot(
|
|
1577
|
-
self.object(
|
|
1977
|
+
self.object(self.js().binding(array)),
|
|
1578
1978
|
ast::array_index(index)?,
|
|
1579
1979
|
))
|
|
1580
1980
|
}
|
|
1581
1981
|
|
|
1582
1982
|
/// Resolves the object held by `name` through local copies.
|
|
1583
|
-
fn object(&self, name:
|
|
1983
|
+
fn object(&self, name: Binding<'a>) -> Binding<'a> {
|
|
1584
1984
|
let mut name = name;
|
|
1585
1985
|
for _ in 0..self.aliases.len() {
|
|
1586
|
-
match self.aliases.get(name) {
|
|
1587
|
-
Some(original) => name = original,
|
|
1986
|
+
match self.aliases.get(&name) {
|
|
1987
|
+
Some(&original) => name = original,
|
|
1588
1988
|
None => break,
|
|
1589
1989
|
}
|
|
1590
1990
|
}
|
|
1591
|
-
name
|
|
1991
|
+
name
|
|
1592
1992
|
}
|
|
1593
1993
|
|
|
1594
1994
|
/// Records the value stored by assigning `value` to `holder`.
|
|
1595
|
-
fn bind(&mut self, holder: Holder
|
|
1596
|
-
*self.assigned.entry(holder
|
|
1995
|
+
fn bind(&mut self, holder: Holder<'a>, value: &'a Expression<'a>) {
|
|
1996
|
+
*self.assigned.entry(holder).or_default() += 1;
|
|
1597
1997
|
// Chained assignments store the innermost value in each target.
|
|
1598
|
-
let mut value = value.
|
|
1599
|
-
while let
|
|
1600
|
-
&& inner.
|
|
1998
|
+
let mut value = value.without_parentheses();
|
|
1999
|
+
while let Expression::AssignmentExpression(inner) = value
|
|
2000
|
+
&& inner.operator == AssignmentOperator::Assign
|
|
1601
2001
|
{
|
|
1602
|
-
value = inner.right.
|
|
2002
|
+
value = inner.right.without_parentheses();
|
|
1603
2003
|
}
|
|
1604
|
-
let forwarder = match value {
|
|
1605
|
-
|
|
2004
|
+
let forwarder = match member(value) {
|
|
2005
|
+
Some(member) if let Some(forwarder) = self.member_forwarder(member) => {
|
|
1606
2006
|
Some(forwarder.plain.clone())
|
|
1607
2007
|
}
|
|
1608
|
-
|
|
2008
|
+
_ => self.held_by(Operand::Expression(value)).cloned(),
|
|
1609
2009
|
};
|
|
1610
2010
|
if let Some(frames) = forwarder {
|
|
1611
2011
|
self.forwarders.held.insert(holder, frames);
|
|
1612
2012
|
return;
|
|
1613
2013
|
}
|
|
1614
2014
|
match (holder, value) {
|
|
1615
|
-
(Holder::Name(name),
|
|
1616
|
-
self.aliases.insert(name,
|
|
2015
|
+
(Holder::Name(name), Expression::Identifier(original)) => {
|
|
2016
|
+
self.aliases.insert(name, self.js().binding(original));
|
|
1617
2017
|
}
|
|
1618
|
-
(holder,
|
|
2018
|
+
(holder, Expression::ObjectExpression(object)) => self.literal(holder, object),
|
|
1619
2019
|
(Holder::Name(name), value) => {
|
|
1620
2020
|
if let Some(text) = ast::static_str(value) {
|
|
1621
|
-
self.strings.insert(name, text
|
|
2021
|
+
self.strings.insert(name, text);
|
|
1622
2022
|
}
|
|
1623
2023
|
}
|
|
1624
2024
|
_ => {}
|
|
@@ -1627,26 +2027,20 @@ impl<'script> Scope<'script> {
|
|
|
1627
2027
|
|
|
1628
2028
|
/// Records forwarders defined by an object literal assigned to `holder`. Uses property keys as
|
|
1629
2029
|
/// function names, including computed keys.
|
|
1630
|
-
fn literal(&mut self, holder:
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
Some(
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
let Some(key) = ast::static_key_str(key) else {
|
|
1640
|
-
continue;
|
|
1641
|
-
};
|
|
1642
|
-
if let Some(frames) = self.forwarder(function, Name::same(key.to_owned())) {
|
|
2030
|
+
fn literal(&mut self, holder: Holder<'a>, object: &'a ObjectExpression<'a>) {
|
|
2031
|
+
// Matches `key: function` properties and methods, but not accessors.
|
|
2032
|
+
for property in &object.properties {
|
|
2033
|
+
if let ObjectPropertyKind::ObjectProperty(property) = property
|
|
2034
|
+
&& let Expression::FunctionExpression(function) = &property.value
|
|
2035
|
+
&& property.kind == PropertyKind::Init
|
|
2036
|
+
&& let Some(key) = ast::static_key_str(&property.key, property.computed)
|
|
2037
|
+
&& let Some(frames) = self.forwarder(function, Name::same(key.to_owned()))
|
|
2038
|
+
{
|
|
1643
2039
|
let forwarder = Forwarder {
|
|
1644
2040
|
method: frames.clone(),
|
|
1645
2041
|
plain: frames,
|
|
1646
2042
|
};
|
|
1647
|
-
self.forwarders
|
|
1648
|
-
.on_objects
|
|
1649
|
-
.insert((holder.clone(), key.to_owned()), forwarder);
|
|
2043
|
+
self.forwarders.on_objects.insert((holder, key), forwarder);
|
|
1650
2044
|
}
|
|
1651
2045
|
}
|
|
1652
2046
|
}
|
|
@@ -1655,168 +2049,168 @@ impl<'script> Scope<'script> {
|
|
|
1655
2049
|
/// later parameters. Accepts direct calls, array copies such as `(g = [f, b], (0, g[0])(a,
|
|
1656
2050
|
/// g[1]))`, and call helpers. Extra parameters can act as locals. Allows local declarations
|
|
1657
2051
|
/// before the call.
|
|
1658
|
-
fn forwarder(&self, function: &Function
|
|
1659
|
-
let
|
|
2052
|
+
fn forwarder(&self, function: &'a Function<'a>, frame: Name) -> Option<Vec<Call>> {
|
|
2053
|
+
let constants = &self.appliers.constants;
|
|
2054
|
+
let function = Body::of(function);
|
|
2055
|
+
let parameters = function.parameters()?;
|
|
1660
2056
|
let (first, rest) = parameters.split_first()?;
|
|
1661
|
-
let
|
|
1662
|
-
let
|
|
2057
|
+
let first = Binding::Symbol(*first);
|
|
2058
|
+
let mut arrays = Copies::new(constants);
|
|
2059
|
+
let Statement::ReturnStatement(returned) = function.only_statement()? else {
|
|
1663
2060
|
return None;
|
|
1664
2061
|
};
|
|
1665
|
-
let mut value = returned.
|
|
1666
|
-
if let
|
|
1667
|
-
let (last, before) = sequence.
|
|
2062
|
+
let mut value = returned.argument.as_ref()?.without_parentheses();
|
|
2063
|
+
if let Expression::SequenceExpression(sequence) = value {
|
|
2064
|
+
let (last, before) = sequence.expressions.split_last()?;
|
|
1668
2065
|
for expression in before {
|
|
1669
2066
|
// Obfuscation leaves expressions that do nothing.
|
|
1670
|
-
if arrays.record(expression).is_none()
|
|
1671
|
-
&& !is_inert(expression, &self.appliers.constants)
|
|
1672
|
-
{
|
|
2067
|
+
if arrays.record(expression).is_none() && !is_inert(expression, constants) {
|
|
1673
2068
|
return None;
|
|
1674
2069
|
}
|
|
1675
2070
|
}
|
|
1676
|
-
value = last.
|
|
2071
|
+
value = last.without_parentheses();
|
|
1677
2072
|
}
|
|
1678
2073
|
// Forwarded arguments must match the parameters after the target function.
|
|
1679
|
-
let passes_on = |arguments: &[
|
|
2074
|
+
let passes_on = |arguments: &[Operand<'a>]| {
|
|
1680
2075
|
arguments.len() <= rest.len()
|
|
1681
2076
|
&& arguments.iter().zip(rest).all(|(argument, parameter)| {
|
|
1682
|
-
arrays.
|
|
2077
|
+
arrays.resolve_operand(*argument) == Some(Binding::Symbol(*parameter))
|
|
1683
2078
|
})
|
|
1684
2079
|
};
|
|
1685
|
-
let call = value
|
|
1686
|
-
let (callee, _) = undetached(call.callee.as_expr()?, &self.appliers.constants);
|
|
1687
|
-
if call.args.iter().any(|argument| argument.spread.is_some()) {
|
|
2080
|
+
let Expression::CallExpression(call) = value else {
|
|
1688
2081
|
return None;
|
|
1689
|
-
}
|
|
1690
|
-
let
|
|
1691
|
-
|
|
2082
|
+
};
|
|
2083
|
+
let (callee, _) = undetached(&call.callee, constants);
|
|
2084
|
+
// A spread has no expression, so it rejects the call.
|
|
2085
|
+
let arguments: Vec<Operand<'a>> = call
|
|
2086
|
+
.arguments
|
|
2087
|
+
.iter()
|
|
2088
|
+
.map(|argument| argument.as_expression().map(Operand::Expression))
|
|
2089
|
+
.collect::<Option<_>>()?;
|
|
2090
|
+
if arrays.resolve(callee) == Some(first) && passes_on(&arguments) {
|
|
1692
2091
|
return Some(vec![Call {
|
|
1693
2092
|
name: frame,
|
|
1694
2093
|
site: Site::new(call),
|
|
1695
2094
|
}]);
|
|
1696
2095
|
}
|
|
1697
2096
|
// Resolve copied names passed through a call helper.
|
|
1698
|
-
let
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
for element in list.elems.iter_mut().flatten() {
|
|
1704
|
-
if let Some(name) = arrays.resolve(&element.expr) {
|
|
1705
|
-
*element.expr =
|
|
1706
|
-
Expr::Ident(Ident::new(name.0, element.expr.span(), name.1));
|
|
1707
|
-
}
|
|
1708
|
-
}
|
|
1709
|
-
Expr::Array(list)
|
|
1710
|
-
}
|
|
1711
|
-
expression => match arrays.resolve(expression) {
|
|
1712
|
-
Some(name) => Expr::Ident(Ident::new(name.0, expression.span(), name.1)),
|
|
1713
|
-
None => continue,
|
|
1714
|
-
},
|
|
1715
|
-
};
|
|
1716
|
-
*argument.expr = copied;
|
|
1717
|
-
}
|
|
1718
|
-
let target = self.target(&call).ok()?;
|
|
2097
|
+
let target = self.resolve(call, Some(&arrays)).ok()?;
|
|
2098
|
+
let copied = Arguments {
|
|
2099
|
+
list: &call.arguments,
|
|
2100
|
+
copies: Some(&arrays),
|
|
2101
|
+
};
|
|
1719
2102
|
(!target.helpers.is_empty()
|
|
1720
|
-
&& target.callee
|
|
1721
|
-
&& passes_on(&handed(
|
|
2103
|
+
&& target.callee == Some(Callee::Binding(first))
|
|
2104
|
+
&& passes_on(&handed(copied, &target)?))
|
|
1722
2105
|
.then(|| target.frames(frame))
|
|
1723
2106
|
}
|
|
1724
2107
|
}
|
|
1725
2108
|
|
|
1726
2109
|
/// Whether `expression` has no effect, such as obfuscation's `true`, constant comparisons and
|
|
1727
|
-
///
|
|
1728
|
-
fn is_inert(expression: &
|
|
1729
|
-
let inert = |expression: &
|
|
1730
|
-
match expression.
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
2110
|
+
/// `void` expressions, including those using numeric `constants`.
|
|
2111
|
+
fn is_inert<'a>(expression: &Expression<'a>, constants: &Constants<'a>) -> bool {
|
|
2112
|
+
let inert = |expression: &Expression<'a>| is_inert(expression, constants);
|
|
2113
|
+
match expression.without_parentheses() {
|
|
2114
|
+
expression if expression.is_literal() => true,
|
|
2115
|
+
Expression::Identifier(name) => constants
|
|
2116
|
+
.js
|
|
2117
|
+
.symbol(name)
|
|
2118
|
+
.is_some_and(|symbol| constants.values.contains_key(&symbol)),
|
|
2119
|
+
Expression::BinaryExpression(binary) => inert(&binary.left) && inert(&binary.right),
|
|
2120
|
+
Expression::LogicalExpression(logical) => inert(&logical.left) && inert(&logical.right),
|
|
2121
|
+
Expression::UnaryExpression(unary) => {
|
|
2122
|
+
unary.operator != UnaryOperator::Delete && inert(&unary.argument)
|
|
2123
|
+
}
|
|
2124
|
+
Expression::ArrayExpression(array) => array
|
|
2125
|
+
.elements
|
|
1737
2126
|
.iter()
|
|
1738
|
-
.all(|element| element
|
|
2127
|
+
.all(|element| element_expression(element).is_none_or(inert)),
|
|
1739
2128
|
_ => false,
|
|
1740
2129
|
}
|
|
1741
2130
|
}
|
|
1742
2131
|
|
|
1743
2132
|
/// Resolves the function produced by `callee` and checks whether a side-effect-free first operand
|
|
1744
2133
|
/// detaches its receiver. For example, `(0, f)(a)` calls `f` without a receiver.
|
|
1745
|
-
fn undetached<'
|
|
1746
|
-
callee: &'
|
|
1747
|
-
constants: &
|
|
1748
|
-
) -> (&'
|
|
1749
|
-
let callee = callee.
|
|
1750
|
-
if let
|
|
1751
|
-
&& let [discarded, last] = &sequence.
|
|
2134
|
+
fn undetached<'b, 'a>(
|
|
2135
|
+
callee: &'b Expression<'a>,
|
|
2136
|
+
constants: &Constants<'a>,
|
|
2137
|
+
) -> (&'b Expression<'a>, bool) {
|
|
2138
|
+
let callee = callee.without_parentheses();
|
|
2139
|
+
if let Expression::SequenceExpression(sequence) = callee
|
|
2140
|
+
&& let [discarded, last] = &sequence.expressions[..]
|
|
1752
2141
|
&& is_inert(discarded, constants)
|
|
1753
2142
|
{
|
|
1754
|
-
return (last.
|
|
2143
|
+
return (last.without_parentheses(), true);
|
|
1755
2144
|
}
|
|
1756
2145
|
(callee, false)
|
|
1757
2146
|
}
|
|
1758
2147
|
|
|
1759
|
-
impl Visit for Scope<'_> {
|
|
1760
|
-
fn visit_function(&mut self, _: &Function) {}
|
|
1761
|
-
fn visit_class(&mut self, _: &Class) {}
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
2148
|
+
impl<'a> Visit<'a> for Scope<'a, '_> {
|
|
2149
|
+
fn visit_function(&mut self, _: &Function<'a>, _: ScopeFlags) {}
|
|
2150
|
+
fn visit_class(&mut self, _: &Class<'a>) {}
|
|
2151
|
+
fn visit_object_property(&mut self, property: &ObjectProperty<'a>) {
|
|
2152
|
+
walk_property(self, property);
|
|
2153
|
+
}
|
|
2154
|
+
|
|
2155
|
+
fn visit_assignment_expression(&mut self, assignment: &AssignmentExpression<'a>) {
|
|
2156
|
+
let assignment = self
|
|
2157
|
+
.js()
|
|
2158
|
+
.lend(assignment, AstKind::as_assignment_expression);
|
|
2159
|
+
if assignment.operator == AssignmentOperator::Assign {
|
|
2160
|
+
if let AssignmentTarget::AssignmentTargetIdentifier(name) = &assignment.left {
|
|
2161
|
+
self.bind(Holder::Name(self.js().binding(name)), &assignment.right);
|
|
2162
|
+
} else if let Some(member) = assigned_member(&assignment.left) {
|
|
2163
|
+
if let Some(slot) = self.slot(member) {
|
|
2164
|
+
self.bind(slot, &assignment.right);
|
|
2165
|
+
} else if let Expression::FunctionExpression(function) = &assignment.right
|
|
2166
|
+
&& let Some(object) = self.holder(Operand::Expression(member.object()))
|
|
2167
|
+
&& let Some(path) = inferred_path(member.object())
|
|
2168
|
+
&& let Some(name) = ast::member_str(member)
|
|
2169
|
+
{
|
|
2170
|
+
// V8 infers the name of an anonymous function from the member it is
|
|
2171
|
+
// assigned to.
|
|
2172
|
+
let path = format!("{}{path}", self.prefix);
|
|
2173
|
+
let (inferred, method) =
|
|
2174
|
+
if matches!(member, MemberExpression::ComputedMemberExpression(_)) {
|
|
1781
2175
|
let inferred = format!("{path}.<computed>");
|
|
1782
2176
|
(inferred.clone(), format!("{inferred} [as {name}]"))
|
|
1783
2177
|
} else {
|
|
1784
2178
|
let inferred = format!("{path}.{name}");
|
|
1785
2179
|
(inferred.clone(), inferred)
|
|
1786
2180
|
};
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
.on_objects
|
|
1793
|
-
.insert((object, name.to_owned()), Forwarder { method, plain });
|
|
1794
|
-
}
|
|
2181
|
+
let frames = |name: String| self.forwarder(function, Name::anonymous(name));
|
|
2182
|
+
if let (Some(method), Some(plain)) = (frames(method), frames(inferred)) {
|
|
2183
|
+
self.forwarders
|
|
2184
|
+
.on_objects
|
|
2185
|
+
.insert((object, name), Forwarder { method, plain });
|
|
1795
2186
|
}
|
|
1796
2187
|
}
|
|
1797
|
-
_ => {}
|
|
1798
2188
|
}
|
|
1799
2189
|
}
|
|
1800
|
-
|
|
2190
|
+
walk::walk_assignment_expression(self, assignment);
|
|
1801
2191
|
}
|
|
1802
2192
|
|
|
1803
|
-
fn
|
|
1804
|
-
|
|
2193
|
+
fn visit_variable_declarator(&mut self, declarator: &VariableDeclarator<'a>) {
|
|
2194
|
+
let declarator = self.js().lend(declarator, AstKind::as_variable_declarator);
|
|
2195
|
+
if let BindingPattern::BindingIdentifier(name) = &declarator.id
|
|
1805
2196
|
&& let Some(init) = &declarator.init
|
|
1806
2197
|
{
|
|
1807
|
-
self.bind(Holder::Name(name.
|
|
2198
|
+
self.bind(Holder::Name(Binding::Symbol(name.symbol_id())), init);
|
|
1808
2199
|
}
|
|
1809
|
-
|
|
2200
|
+
walk::walk_variable_declarator(self, declarator);
|
|
1810
2201
|
}
|
|
1811
2202
|
}
|
|
1812
2203
|
|
|
1813
2204
|
/// Returns the V8 name inferred from an assignment target: `object` for an object or
|
|
1814
2205
|
/// `array.<computed>` for an array slot.
|
|
1815
|
-
fn inferred_path(object: &
|
|
1816
|
-
match object.
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
2206
|
+
fn inferred_path(object: &Expression<'_>) -> Option<String> {
|
|
2207
|
+
match object.without_parentheses() {
|
|
2208
|
+
Expression::Identifier(name) => Some(name.name.to_string()),
|
|
2209
|
+
Expression::ComputedMemberExpression(member) => {
|
|
2210
|
+
let Expression::Identifier(array) = &member.object else {
|
|
2211
|
+
return None;
|
|
2212
|
+
};
|
|
2213
|
+
Some(format!("{}.<computed>", array.name))
|
|
1820
2214
|
}
|
|
1821
2215
|
_ => None,
|
|
1822
2216
|
}
|
|
@@ -1825,23 +2219,26 @@ fn inferred_path(object: &Expr) -> Option<String> {
|
|
|
1825
2219
|
/// Finds runner calls from closure-factory listeners, including calls made through helpers, and
|
|
1826
2220
|
/// records calls it cannot resolve. Each listener forwards its captured machine and layout
|
|
1827
2221
|
/// followed by its own arguments.
|
|
1828
|
-
struct Listeners<'
|
|
1829
|
-
runner:
|
|
1830
|
-
appliers: &'
|
|
1831
|
-
found: Vec<Result<Invocation
|
|
1832
|
-
}
|
|
1833
|
-
|
|
1834
|
-
impl Visit for Listeners<'_> {
|
|
1835
|
-
fn visit_function(&mut self, function: &Function) {
|
|
1836
|
-
let
|
|
1837
|
-
|
|
1838
|
-
|
|
2222
|
+
struct Listeners<'a, 's> {
|
|
2223
|
+
runner: Callee<'a>,
|
|
2224
|
+
appliers: &'s Appliers<'a>,
|
|
2225
|
+
found: Vec<Result<Invocation<'a>>>,
|
|
2226
|
+
}
|
|
2227
|
+
|
|
2228
|
+
impl<'a> Visit<'a> for Listeners<'a, '_> {
|
|
2229
|
+
fn visit_function(&mut self, function: &Function<'a>, flags: ScopeFlags) {
|
|
2230
|
+
let js = self.appliers.js();
|
|
2231
|
+
let function = js.lend(function, AstKind::as_function);
|
|
2232
|
+
let body = Body::of(function);
|
|
2233
|
+
let scope = Scope::new("", &body, self.appliers);
|
|
2234
|
+
for call in calls_of(js, &body) {
|
|
2235
|
+
match scope.target(call) {
|
|
1839
2236
|
// A listener passes the captured machine and layout, then its own
|
|
1840
2237
|
// parameters. Other scopes may reuse the runner's name for another function,
|
|
1841
2238
|
// and the message handler starts the runner with computed arguments.
|
|
1842
2239
|
Ok(mut target)
|
|
1843
|
-
if target.callee
|
|
1844
|
-
&& forwards_parameters(
|
|
2240
|
+
if target.callee == Some(self.runner)
|
|
2241
|
+
&& forwards_parameters(&self.appliers.constants, &body, call, &target) =>
|
|
1845
2242
|
{
|
|
1846
2243
|
target.passed = target.passed.map(|passed| passed - 2);
|
|
1847
2244
|
self.found.push(Ok(target));
|
|
@@ -1850,7 +2247,15 @@ impl Visit for Listeners<'_> {
|
|
|
1850
2247
|
Err(error) => self.found.push(Err(error)),
|
|
1851
2248
|
}
|
|
1852
2249
|
}
|
|
1853
|
-
|
|
2250
|
+
walk::walk_function(self, function, flags);
|
|
2251
|
+
}
|
|
2252
|
+
|
|
2253
|
+
fn visit_object_property(&mut self, property: &ObjectProperty<'a>) {
|
|
2254
|
+
walk_property(self, property);
|
|
2255
|
+
}
|
|
2256
|
+
|
|
2257
|
+
fn visit_method_definition(&mut self, method: &MethodDefinition<'a>) {
|
|
2258
|
+
walk_method(self, method);
|
|
1854
2259
|
}
|
|
1855
2260
|
}
|
|
1856
2261
|
|
|
@@ -1858,6 +2263,116 @@ impl Visit for Listeners<'_> {
|
|
|
1858
2263
|
mod tests {
|
|
1859
2264
|
use super::*;
|
|
1860
2265
|
|
|
2266
|
+
#[test]
|
|
2267
|
+
fn recovers_method_and_free_dispatch_stacks() {
|
|
2268
|
+
let cases: String = (0..=DISPATCH_CASES)
|
|
2269
|
+
.map(|opcode| format!("case {opcode}: construct(vm); break;"))
|
|
2270
|
+
.collect();
|
|
2271
|
+
for (declaration, call, v8, jsc) in [
|
|
2272
|
+
(
|
|
2273
|
+
"class Machine { run(pc,key,args) { dispatch(this); } }",
|
|
2274
|
+
"vm.run(pc,key,copy ? args.slice() : args)",
|
|
2275
|
+
"Machine.run",
|
|
2276
|
+
"run",
|
|
2277
|
+
),
|
|
2278
|
+
(
|
|
2279
|
+
"function drive(vm,pc,key,args) { dispatch(vm); }",
|
|
2280
|
+
"drive(vm,pc,key,copy ? args.slice() : args)",
|
|
2281
|
+
"drive",
|
|
2282
|
+
"drive",
|
|
2283
|
+
),
|
|
2284
|
+
] {
|
|
2285
|
+
let source = format!(
|
|
2286
|
+
"function construct(vm) {{ return new Error(); }}\n\
|
|
2287
|
+
function dispatch(vm) {{ switch(opcode) {{ {cases} }} }}\n\
|
|
2288
|
+
{declaration}\n\
|
|
2289
|
+
function resume(vm,pc,key,args,copy) {{ return {call}; }}"
|
|
2290
|
+
);
|
|
2291
|
+
let (_, opcodes) = deobfuscate_with_cases(source.clone()).unwrap();
|
|
2292
|
+
let result =
|
|
2293
|
+
geometry(&source, &opcodes, |machine| machine.construction(1, false)).unwrap();
|
|
2294
|
+
assert_eq!(
|
|
2295
|
+
result
|
|
2296
|
+
.frames
|
|
2297
|
+
.iter()
|
|
2298
|
+
.map(|frame| frame.function.as_str())
|
|
2299
|
+
.collect::<Vec<_>>(),
|
|
2300
|
+
["construct", "dispatch", v8, "resume"]
|
|
2301
|
+
);
|
|
2302
|
+
assert_eq!(
|
|
2303
|
+
result
|
|
2304
|
+
.jsc_frames
|
|
2305
|
+
.iter()
|
|
2306
|
+
.map(|frame| frame.function.as_str())
|
|
2307
|
+
.collect::<Vec<_>>(),
|
|
2308
|
+
["construct", "dispatch", jsc, "resume"]
|
|
2309
|
+
);
|
|
2310
|
+
for frames in [&result.frames, &result.jsc_frames] {
|
|
2311
|
+
assert_eq!(
|
|
2312
|
+
frames.iter().map(|frame| frame.line).collect::<Vec<_>>(),
|
|
2313
|
+
[0, 1, 2, 3]
|
|
2314
|
+
);
|
|
2315
|
+
}
|
|
2316
|
+
}
|
|
2317
|
+
}
|
|
2318
|
+
|
|
2319
|
+
#[test]
|
|
2320
|
+
fn free_dispatch_stacks_preserve_helpers_and_merged_frames() {
|
|
2321
|
+
let cases: String = (0..=DISPATCH_CASES)
|
|
2322
|
+
.map(|opcode| format!("case {opcode}: construct(vm); break;"))
|
|
2323
|
+
.collect();
|
|
2324
|
+
for (declaration, call, expected) in [
|
|
2325
|
+
(
|
|
2326
|
+
"function drive(vm,pc,key,args) { dispatch(vm); } function apply(s,f,a,b,c,d) { switch(s^5) { case 6: return f(a,b,c,d); } }",
|
|
2327
|
+
"apply(3,drive,vm,pc,key,copy ? args.slice() : args)",
|
|
2328
|
+
&["construct", "dispatch", "drive", "apply", "resume"][..],
|
|
2329
|
+
),
|
|
2330
|
+
(
|
|
2331
|
+
"function choose(s,vm,pc,key,args) { switch(s^5) { case 6: { dispatch(vm); } return; case 7: { unrelated(); } return; } }",
|
|
2332
|
+
"choose(3,vm,pc,key,copy ? args.slice() : args)",
|
|
2333
|
+
&["construct", "dispatch", "choose", "resume"][..],
|
|
2334
|
+
),
|
|
2335
|
+
] {
|
|
2336
|
+
let source = format!(
|
|
2337
|
+
"function construct(vm) {{ return new Error(); }}\n\
|
|
2338
|
+
function dispatch(vm) {{ switch(opcode) {{ {cases} }} }}\n\
|
|
2339
|
+
{declaration}\n\
|
|
2340
|
+
function resume(vm,pc,key,args,copy) {{ return {call}; }}"
|
|
2341
|
+
);
|
|
2342
|
+
let (_, opcodes) = deobfuscate_with_cases(source.clone()).unwrap();
|
|
2343
|
+
let result =
|
|
2344
|
+
geometry(&source, &opcodes, |machine| machine.construction(1, false)).unwrap();
|
|
2345
|
+
for frames in [&result.frames, &result.jsc_frames] {
|
|
2346
|
+
assert_eq!(
|
|
2347
|
+
frames
|
|
2348
|
+
.iter()
|
|
2349
|
+
.map(|frame| frame.function.as_str())
|
|
2350
|
+
.collect::<Vec<_>>(),
|
|
2351
|
+
expected
|
|
2352
|
+
);
|
|
2353
|
+
}
|
|
2354
|
+
}
|
|
2355
|
+
}
|
|
2356
|
+
|
|
2357
|
+
/// The decoded program captured during deobfuscation equals the one decoding alone
|
|
2358
|
+
/// produces, for a saved script named by `CFSOLVE_CAPTURE_SCRIPT`.
|
|
2359
|
+
#[test]
|
|
2360
|
+
fn captures_the_decoded_program() {
|
|
2361
|
+
let Some(path) = std::env::var_os("CFSOLVE_CAPTURE_SCRIPT") else {
|
|
2362
|
+
return;
|
|
2363
|
+
};
|
|
2364
|
+
let source = std::fs::read_to_string(path).expect("the capture script is readable");
|
|
2365
|
+
// The debug form includes spans and semantic IDs.
|
|
2366
|
+
let debug = |decoded: &ParsedScript| format!("{:?}", decoded.script().program);
|
|
2367
|
+
let (_, cases) = deobfuscate_with_cases(source.clone()).unwrap();
|
|
2368
|
+
let captured = debug(&cases.decoded.unwrap());
|
|
2369
|
+
let alone = debug(&deobfuscate::decode(source).unwrap());
|
|
2370
|
+
assert!(
|
|
2371
|
+
captured == alone,
|
|
2372
|
+
"the captured program differs from the decoded one"
|
|
2373
|
+
);
|
|
2374
|
+
}
|
|
2375
|
+
|
|
1861
2376
|
#[test]
|
|
1862
2377
|
fn appliers_may_copy_their_selector_and_name_apply_indirectly() {
|
|
1863
2378
|
for source in [
|
|
@@ -1865,9 +2380,10 @@ mod tests {
|
|
|
1865
2380
|
"function j(s,f,a,e,c,n){switch(e={},c=`apply`,n=[s],5^n[0]){case 6:return Reflect[c](f,void 0,a)}}",
|
|
1866
2381
|
"var j;j=function(s,f,a){switch(5^s){case 6:return Reflect.apply(f,void 0,a)}}",
|
|
1867
2382
|
] {
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
2383
|
+
with_script(source, |script| {
|
|
2384
|
+
let applier = &script.appliers.helpers[&binding(script, "j")];
|
|
2385
|
+
assert!(applier.select(3.0).is_some(), "{source}");
|
|
2386
|
+
});
|
|
1871
2387
|
}
|
|
1872
2388
|
}
|
|
1873
2389
|
|
|
@@ -1882,11 +2398,11 @@ mod tests {
|
|
|
1882
2398
|
o['k']=function(a,b,c,d,g){return g=[b,d],true,a(g[0],c,g[1])},\
|
|
1883
2399
|
o['s']=function(a,b,c,g){return g=[c,a,b],(0,g[1])(g[2],g[0])},\
|
|
1884
2400
|
q=o,q['k'](B,f,r,q['s'](S,arguments,2))}";
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
2401
|
+
with_script(source, |script| {
|
|
2402
|
+
let frames = script.method_forwarder(binding(script, "PA")).unwrap();
|
|
2403
|
+
assert_eq!(v8_names(&frames), ["PA.o.<computed> [as k]", "PA"]);
|
|
2404
|
+
assert!(script.method_forwarder(binding(script, "S")).is_none());
|
|
2405
|
+
});
|
|
1890
2406
|
}
|
|
1891
2407
|
|
|
1892
2408
|
/// Checks method forwarders that call a `Reflect.apply` alias kept in an array slot, directly
|
|
@@ -1898,107 +2414,108 @@ mod tests {
|
|
|
1898
2414
|
function D(f,r,a){return(0,G[2])(f,r,[a])}\
|
|
1899
2415
|
function H(f,r,a,b){return W(1,G[2],f,r,[a,b])}\
|
|
1900
2416
|
function O(f,r,a){return(0,G[3])(f,r,[a])}";
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
2417
|
+
with_script(source, |script| {
|
|
2418
|
+
let cases: [(&str, &[&str]); 2] = [("D", &["D"]), ("H", &["W", "H"])];
|
|
2419
|
+
for (forwarder, expected) in cases {
|
|
2420
|
+
let frames = script
|
|
2421
|
+
.method_forwarder(binding(script, forwarder))
|
|
2422
|
+
.expect("`G[2]` holds `Reflect.apply`, so the call is a method forwarder");
|
|
2423
|
+
assert_eq!(
|
|
2424
|
+
v8_names(&frames),
|
|
2425
|
+
expected,
|
|
2426
|
+
"frames of forwarder {forwarder}"
|
|
2427
|
+
);
|
|
2428
|
+
}
|
|
2429
|
+
assert!(
|
|
2430
|
+
script.method_forwarder(binding(script, "O")).is_none(),
|
|
2431
|
+
"`G[3]` holds a plain function, not `Reflect.apply`"
|
|
2432
|
+
);
|
|
2433
|
+
});
|
|
1914
2434
|
}
|
|
1915
2435
|
|
|
1916
2436
|
#[test]
|
|
1917
2437
|
fn same_named_functions_resolve_to_the_called_binding() {
|
|
1918
|
-
let (
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
.unwrap()
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
);
|
|
2438
|
+
let source = "function outer(){function h(){wrong()} h()} function h(){right()} function caller(){h()}";
|
|
2439
|
+
with_script(source, |script| {
|
|
2440
|
+
let target = first_target(script, "caller");
|
|
2441
|
+
let handler = script.function(target.callee.unwrap()).unwrap();
|
|
2442
|
+
let call = calls_of(script.js(), &handler)[0];
|
|
2443
|
+
let callee = Operand::Expression(&call.callee)
|
|
2444
|
+
.callee(&script.appliers.constants)
|
|
2445
|
+
.unwrap();
|
|
2446
|
+
assert_eq!(script.appliers.name(callee), "right");
|
|
2447
|
+
});
|
|
1929
2448
|
}
|
|
1930
2449
|
|
|
1931
2450
|
#[test]
|
|
1932
2451
|
fn shadowed_appliers_and_runners_are_distinct_bindings() {
|
|
1933
|
-
let (
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
2452
|
+
let source = "function runner(){} function j(s,f,a){switch(5^s){case 6:return Reflect.apply(f,void 0,a)}} function outer(){function j(){} function runner(){} j(3,f,[]);return function(a){runner(vm,layout,a);j(3,f,[])}} function real(a){runner(vm,layout,a)}";
|
|
2453
|
+
with_script(source, |script| {
|
|
2454
|
+
let runner = first_target(script, "real").callee.unwrap();
|
|
2455
|
+
let mut listeners = Listeners {
|
|
2456
|
+
runner,
|
|
2457
|
+
appliers: &script.appliers,
|
|
2458
|
+
found: Vec::new(),
|
|
2459
|
+
};
|
|
2460
|
+
// Visit every collected function, including the anonymous listener inside outer.
|
|
2461
|
+
for (_, function) in script.js().named_functions() {
|
|
2462
|
+
listeners.visit_function(function, ScopeFlags::Function);
|
|
2463
|
+
}
|
|
2464
|
+
assert_eq!(listeners.found.len(), 1);
|
|
2465
|
+
let target = first_target(script, "outer");
|
|
2466
|
+
assert!(target.helpers.is_empty());
|
|
2467
|
+
assert_eq!(target.passed, Some(3));
|
|
2468
|
+
let callee = target.callee.expect("the call has a named callee");
|
|
2469
|
+
assert!(!script.appliers.helpers.contains_key(&callee));
|
|
2470
|
+
});
|
|
1952
2471
|
}
|
|
1953
2472
|
|
|
1954
2473
|
#[test]
|
|
1955
2474
|
fn native_apply_alias_does_not_match_a_shadowed_function() {
|
|
1956
|
-
let (
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
.method_forwarder(&binding(&script, "wrong"))
|
|
1962
|
-
.is_none()
|
|
1963
|
-
);
|
|
1964
|
-
assert!(script.method_forwarder(&binding(&script, "good")).is_some());
|
|
2475
|
+
let source = "var B=Reflect.apply; function outer(){function B(){} function wrong(f,r){return B(f,r,[])}} function good(f,r){return B(f,r,[])}";
|
|
2476
|
+
with_script(source, |script| {
|
|
2477
|
+
assert!(script.method_forwarder(binding(script, "wrong")).is_none());
|
|
2478
|
+
assert!(script.method_forwarder(binding(script, "good")).is_some());
|
|
2479
|
+
});
|
|
1965
2480
|
}
|
|
1966
2481
|
|
|
1967
2482
|
#[test]
|
|
1968
2483
|
fn block_locals_do_not_replace_an_outer_objects_forwarder() {
|
|
1969
|
-
let (
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
2484
|
+
let source = "function F(){let o={},q=o,c='k';o.k=function(f,a){return f(a)};{let o={},q=o,c='other';o.k=function(f,a){return 0};}return q[c](target,1)}";
|
|
2485
|
+
with_script(source, |script| {
|
|
2486
|
+
let target = first_target(script, "F");
|
|
2487
|
+
assert_eq!(script.appliers.name(target.callee.unwrap()), "target");
|
|
2488
|
+
assert_eq!(target.helpers.len(), 1);
|
|
2489
|
+
assert_eq!(target.helpers[0].name.v8, "F.o.k");
|
|
2490
|
+
});
|
|
1976
2491
|
}
|
|
1977
2492
|
|
|
1978
|
-
///
|
|
1979
|
-
fn
|
|
1980
|
-
deobfuscate::
|
|
1981
|
-
|
|
1982
|
-
})
|
|
1983
|
-
.unwrap()
|
|
2493
|
+
/// Decodes `source` and analyzes it.
|
|
2494
|
+
fn with_script<T>(source: &str, read: impl for<'a> FnOnce(&Script<'a>) -> T) -> T {
|
|
2495
|
+
let decoded = deobfuscate::decode(source.to_owned()).unwrap();
|
|
2496
|
+
read(&Script::new(decoded.script()))
|
|
1984
2497
|
}
|
|
1985
2498
|
|
|
1986
|
-
|
|
1987
|
-
|
|
2499
|
+
/// The binding of the script function named `name`.
|
|
2500
|
+
fn binding<'a>(script: &Script<'a>, name: &str) -> Callee<'a> {
|
|
2501
|
+
let (binding, _) = script
|
|
1988
2502
|
.functions
|
|
1989
2503
|
.iter()
|
|
1990
|
-
.find(|(
|
|
1991
|
-
.unwrap()
|
|
1992
|
-
|
|
1993
|
-
|
|
2504
|
+
.find(|(binding, _)| script.js().name(*binding) == name)
|
|
2505
|
+
.unwrap();
|
|
2506
|
+
Callee::Binding(*binding)
|
|
2507
|
+
}
|
|
2508
|
+
|
|
2509
|
+
fn v8_names(calls: &[Call]) -> Vec<&str> {
|
|
2510
|
+
calls.iter().map(|call| call.name.v8.as_str()).collect()
|
|
1994
2511
|
}
|
|
1995
2512
|
|
|
1996
2513
|
/// Returns the target of the first call in the script function named `name`.
|
|
1997
|
-
fn first_target(script: &Script
|
|
1998
|
-
let function = script.function(
|
|
1999
|
-
let call = calls_of(function)
|
|
2000
|
-
Scope::new(name, function, &script.appliers)
|
|
2001
|
-
.target(
|
|
2514
|
+
fn first_target<'a>(script: &Script<'a>, name: &str) -> Invocation<'a> {
|
|
2515
|
+
let function = script.function(binding(script, name)).unwrap();
|
|
2516
|
+
let call = calls_of(script.js(), &function)[0];
|
|
2517
|
+
Scope::new(name, &function, &script.appliers)
|
|
2518
|
+
.target(call)
|
|
2002
2519
|
.unwrap()
|
|
2003
2520
|
}
|
|
2004
2521
|
|
|
@@ -2010,20 +2527,21 @@ mod tests {
|
|
|
2010
2527
|
function e(s,t,f,a){switch(s^C){case 1:{var y;switch(y=[a],5^t){case 6:return f(y[0])}}return;case 2:{h(a)}return}}\
|
|
2011
2528
|
function caller(x){return e(6,3,g,x)}\
|
|
2012
2529
|
function other(x){e(5,x)}";
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
.
|
|
2018
|
-
.
|
|
2019
|
-
|
|
2020
|
-
.
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2530
|
+
with_script(source, |script| {
|
|
2531
|
+
let target = first_target(script, "caller");
|
|
2532
|
+
assert_eq!(script.appliers.name(target.callee.unwrap()), "g");
|
|
2533
|
+
assert_eq!(v8_names(&target.helpers), ["e"]);
|
|
2534
|
+
assert_eq!(target.passed, Some(1));
|
|
2535
|
+
let original = first_target(script, "other").callee.unwrap();
|
|
2536
|
+
assert!(matches!(original, Callee::Original(_)));
|
|
2537
|
+
assert_eq!(script.appliers.name(original), "e");
|
|
2538
|
+
let function = script.function(original).unwrap();
|
|
2539
|
+
let call = calls_of(script.js(), &function)[0];
|
|
2540
|
+
let callee = Operand::Expression(&call.callee)
|
|
2541
|
+
.callee(&script.appliers.constants)
|
|
2542
|
+
.unwrap();
|
|
2543
|
+
assert_eq!(script.appliers.name(callee), "h");
|
|
2544
|
+
});
|
|
2027
2545
|
}
|
|
2028
2546
|
|
|
2029
2547
|
/// V8 names a forwarder copied to a binding after its assignment target, without the property
|
|
@@ -2035,31 +2553,35 @@ mod tests {
|
|
|
2035
2553
|
function a(s,f,x,y,z){switch(s^5){case 6:return f(x,y,z)}}\
|
|
2036
2554
|
function direct(m){return q(loop,m,1)}\
|
|
2037
2555
|
function applied(m){return a(3,q,loop,m,1)}";
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
.
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
}
|
|
2556
|
+
with_script(source, |script| {
|
|
2557
|
+
for (caller, expected) in [
|
|
2558
|
+
("direct", &["o.<computed>"][..]),
|
|
2559
|
+
("applied", &["o.<computed>", "a"]),
|
|
2560
|
+
] {
|
|
2561
|
+
let target = first_target(script, caller);
|
|
2562
|
+
assert_eq!(
|
|
2563
|
+
script.appliers.name(target.callee.unwrap()),
|
|
2564
|
+
"loop",
|
|
2565
|
+
"{caller}"
|
|
2566
|
+
);
|
|
2567
|
+
assert_eq!(v8_names(&target.helpers), expected, "{caller}");
|
|
2568
|
+
assert_eq!(target.passed, Some(2), "{caller}");
|
|
2569
|
+
}
|
|
2570
|
+
});
|
|
2053
2571
|
}
|
|
2054
2572
|
|
|
2055
2573
|
#[test]
|
|
2056
2574
|
fn a_call_key_kept_in_a_local_still_calls_the_handler() {
|
|
2057
2575
|
let source = "function run(){var c='call';switch(o){case 1:h[c](this);break}}";
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2576
|
+
with_script(source, |script| {
|
|
2577
|
+
let target = first_target(script, "run");
|
|
2578
|
+
assert_eq!(
|
|
2579
|
+
target.callee.map(|callee| script.appliers.name(callee)),
|
|
2580
|
+
Some("h")
|
|
2581
|
+
);
|
|
2582
|
+
assert!(target.method);
|
|
2583
|
+
assert_eq!(target.passed, Some(0));
|
|
2584
|
+
});
|
|
2063
2585
|
}
|
|
2064
2586
|
|
|
2065
2587
|
#[test]
|
|
@@ -2098,18 +2620,19 @@ mod tests {
|
|
|
2098
2620
|
#[test]
|
|
2099
2621
|
fn jsc_frames_are_placed_at_their_arguments() {
|
|
2100
2622
|
let source = "function a(s,f,l){switch(1^s){case 0:return Reflect.construct(f,void 0,l);case 3:return Reflect.apply(f,void 0,l)}}function h(){return a(1,Error,[])}";
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2623
|
+
with_script(source, |script| {
|
|
2624
|
+
let target = first_target(script, "h");
|
|
2625
|
+
let frames = target.frames(Name::same("h".to_owned()));
|
|
2626
|
+
let names: Vec<_> = frames
|
|
2627
|
+
.iter()
|
|
2628
|
+
.map(|call| (call.name.v8.as_str(), call.name.jsc.as_str()))
|
|
2629
|
+
.collect();
|
|
2630
|
+
assert_eq!(names, [("a", "a"), ("h", "h")]);
|
|
2631
|
+
let at = |position: u32| &source[position as usize..][..2];
|
|
2632
|
+
let (applier, handler) = (&frames[0], &frames[1]);
|
|
2633
|
+
assert_eq!(at(handler.site.v8), "a(");
|
|
2634
|
+
assert_eq!(at(handler.site.jsc), "(1");
|
|
2635
|
+
assert_eq!(at(applier.site.jsc), "(f");
|
|
2636
|
+
});
|
|
2114
2637
|
}
|
|
2115
2638
|
}
|