Commit 2017304
resolver: restore package_manager.log after resolve to avoid dangling stack pointer (#31020)
## What
`resolveMaybeNeedsTrailingSlash` swaps `vm.log` / `resolver.log` to a
stack-local `Log` for the duration of `_resolve`, then restores them via
a drop guard. The Zig original also swaps and restores
`transpiler.linker.log` and `resolver.package_manager.log`; the Rust
port had those behind a `TODO(b2-cycle)` and only handled `vm.log` +
`resolver.log`.
When auto-install is enabled and the resolver lazily creates the
`PackageManager` during `_resolve`, `Resolver::get_package_manager`
seeds `pm.log` from `resolver.log` — which at that point is the
**stack-local** `Log`. Because the restore guard never touched `pm.log`,
it was left pointing into a dead stack frame after the function
returned. The next resolve at a different stack depth that routes
through the auto-install task runner dereferenced that stale pointer in
`Log::add_error_fmt`, tripping ASAN's `stack-use-after-scope` (or
segfaulting / executing garbage in release builds).
Stack at the fault:
```
#0 bun_ast::Log::add_formatted_msg
#1 bun_ast::Log::add_error_fmt
#2 bun_install::…::run_tasks
#7 bun_install::…::enqueue_dependency_to_root
#9 bun_resolver::Resolver::enqueue_dependency_to_resolve
#14 bun_resolver::Resolver::resolve_and_auto_install
#15 bun_jsc::VirtualMachine::_resolve
#16 bun_jsc::VirtualMachine::resolve_maybe_needs_trailing_slash::<true>
```
## Fix
Swap and restore `linker.log` and (when present) `package_manager.log`
in both copies of the resolve log guard
(`VirtualMachine::resolve_maybe_needs_trailing_slash` and
`jsc_hooks::resolve_hook`), matching `VirtualMachine.zig`. The restore
re-checks `resolver.package_manager` at drop time so a PM that was
lazily created during `_resolve` is also pointed back at the VM log.
Also adds the missing `<cassert>` include in `wtf-bindings.cpp`, which
stopped being pulled in transitively.
## Repro
```js
// run from an empty dir with
// BUN_CONFIG_INSTALL=fallback BUN_CONFIG_REGISTRY=http://127.0.0.1:1
const realm = new ShadowRealm();
const variants = [
() => realm.importValue("pkg-not-found-a", "x"),
() => (() => realm.importValue("pkg-not-found-b", "x"))(),
() => (() => (() => realm.importValue("pkg-not-found-c", "x"))())(),
() => import("pkg-not-found-f"),
];
for (let i = 0; i < 100; i++)
for (const v of variants) try { v()?.catch?.(() => {}); } catch {}
```
Segfaults on `main`, clean after this change.
Fixes #14432
Fixes #22407
---------
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>1 parent 2f15816 commit 2017304
3 files changed
Lines changed: 93 additions & 26 deletions
File tree
- src
- jsc
- runtime
- test/js/bun/resolve
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4259 | 4259 | | |
4260 | 4260 | | |
4261 | 4261 | | |
4262 | | - | |
4263 | | - | |
| 4262 | + | |
| 4263 | + | |
| 4264 | + | |
| 4265 | + | |
| 4266 | + | |
| 4267 | + | |
4264 | 4268 | | |
4265 | 4269 | | |
4266 | 4270 | | |
| |||
4278 | 4282 | | |
4279 | 4283 | | |
4280 | 4284 | | |
| 4285 | + | |
| 4286 | + | |
| 4287 | + | |
| 4288 | + | |
| 4289 | + | |
| 4290 | + | |
| 4291 | + | |
| 4292 | + | |
| 4293 | + | |
| 4294 | + | |
| 4295 | + | |
4281 | 4296 | | |
4282 | 4297 | | |
4283 | 4298 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2238 | 2238 | | |
2239 | 2239 | | |
2240 | 2240 | | |
2241 | | - | |
2242 | | - | |
2243 | | - | |
2244 | | - | |
2245 | | - | |
2246 | | - | |
2247 | | - | |
2248 | | - | |
2249 | | - | |
2250 | | - | |
2251 | | - | |
2252 | | - | |
2253 | | - | |
| 2241 | + | |
| 2242 | + | |
| 2243 | + | |
| 2244 | + | |
2254 | 2245 | | |
2255 | 2246 | | |
2256 | 2247 | | |
2257 | 2248 | | |
2258 | | - | |
2259 | | - | |
2260 | | - | |
2261 | | - | |
2262 | | - | |
2263 | | - | |
2264 | | - | |
2265 | | - | |
| 2249 | + | |
| 2250 | + | |
| 2251 | + | |
| 2252 | + | |
2266 | 2253 | | |
2267 | 2254 | | |
2268 | 2255 | | |
| |||
5029 | 5016 | | |
5030 | 5017 | | |
5031 | 5018 | | |
5032 | | - | |
5033 | | - | |
| 5019 | + | |
| 5020 | + | |
| 5021 | + | |
5034 | 5022 | | |
5035 | 5023 | | |
5036 | 5024 | | |
5037 | 5025 | | |
5038 | | - | |
| 5026 | + | |
| 5027 | + | |
| 5028 | + | |
5039 | 5029 | | |
5040 | 5030 | | |
5041 | 5031 | | |
5042 | 5032 | | |
| 5033 | + | |
| 5034 | + | |
| 5035 | + | |
5043 | 5036 | | |
5044 | 5037 | | |
5045 | 5038 | | |
| |||
Lines changed: 59 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
0 commit comments