Skip to content

Commit 0dbdd2f

Browse files
proggeramlugRalph Küpper
andauthored
fix(codegen): root the URL constructor's coerced string across base lowering (Layer 1) (#7453)
* fix(codegen): root the URL constructor's coerced string across base lowering `js_url_coerce_string` returns a RAW StringHeader pointer, not a NaN-boxed value, so nothing else keeps it alive. In `new URL(input, base)` two collection points then stood between it and its use: lowering `base` runs arbitrary user code, and the second coercion allocates whenever `base` is not already a string. An evacuating cycle in either window left the pointer forwarded and `js_url_new_with_base` parsing freed bytes. Emitted IR before: %r23 = call js_url_coerce_string(...) %r24 = call perry_fn__mk() ; user call, may collect %r25 = call js_url_coerce_string(...) ; may collect %r26 = call js_url_new_with_base(%r23, %r25) and after: %r23 = call js_url_coerce_string(...) %r24 = call js_gc_temp_root_push(%r23) %r25 = call perry_fn__mk() %r26 = call js_url_coerce_string(%r25) %r27 = call js_gc_temp_root_get(%r24) %r28 = call js_url_new_with_base(%r27, %r26) call js_gc_temp_root_truncate(%r24) UrlPatternNew held its NaN-boxed input in a bare register across the same window; it now lowers both operands through lower_exprs_rooted. Latent, as every member of this class is: the value is only wrong if a collection lands inside the window, and at collection time there is nothing for the collector to find. The evidence is the IR, per docs/src/internals/gc-rooting-invariant.md. * docs: changelog fragment for #7453 --------- Co-authored-by: Ralph Küpper <ralph@skelpo.com>
1 parent 45b9f79 commit 0dbdd2f

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- **`new URL(input, base)` no longer holds a raw heap string pointer across a collection point.** `js_url_coerce_string` returns a raw `StringHeader` pointer rather than a NaN-boxed value, so nothing else kept it alive while `base` lowered (arbitrary user code) and was itself coerced (allocates for any non-string). An evacuating cycle in either window left `js_url_new_with_base` parsing freed bytes. The coerced pointer is now rooted before the window and re-read after it — the ordering is the fix, since rooting after the coercion would root an already-stale value. `URLPattern` held its input in a bare register across the same window and now lowers both operands through `lower_exprs_rooted`. Latent, as every member of this class is: the static IR is the evidence, per `docs/src/internals/gc-rooting-invariant.md`. (#7453)

‎crates/perry-codegen/src/expr/url_main.rs‎

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,33 +39,62 @@ pub(crate) fn lower(ctx: &mut FnCtx<'_>, expr: &Expr) -> Result<String> {
3939
.block()
4040
.call(I64, "js_url_coerce_string", &[(DOUBLE, &url_v)]);
4141
let obj = if let Some(base) = base {
42+
// `js_url_coerce_string` returns a RAW `StringHeader` pointer,
43+
// not a NaN-boxed value, so nothing else keeps it alive. Two
44+
// collection points then stand between it and its use:
45+
// lowering `base` runs arbitrary user code, and the second
46+
// coercion allocates whenever `base` is not already a string.
47+
// An evacuating cycle in either window leaves `url_ptr`
48+
// pointing at a forwarded object and
49+
// `js_url_new_with_base` parses freed bytes.
50+
//
51+
// Root before the first collection point and re-read after the
52+
// last, per `docs/src/internals/gc-rooting-invariant.md` — the
53+
// ordering is the whole fix; adding the root after the coercion
54+
// would root an already-stale pointer.
55+
let url_slot = super::temp_root::temp_root_push_i64(ctx, &url_ptr);
4256
let base_v = lower_expr(ctx, base)?;
4357
let base_ptr = ctx
4458
.block()
4559
.call(I64, "js_url_coerce_string", &[(DOUBLE, &base_v)]);
46-
ctx.block().call(
60+
let url_ptr = super::temp_root::temp_root_get_i64(ctx, &url_slot);
61+
let obj = ctx.block().call(
4762
I64,
4863
"js_url_new_with_base",
4964
&[(I64, &url_ptr), (I64, &base_ptr)],
50-
)
65+
);
66+
super::temp_root::temp_root_truncate(ctx, &url_slot);
67+
obj
5168
} else {
5269
ctx.block().call(I64, "js_url_new", &[(I64, &url_ptr)])
5370
};
5471
Ok(nanbox_pointer_inline(ctx.block(), &obj))
5572
}
5673

5774
Expr::UrlPatternNew { input, base } => {
58-
let input_v = lower_expr(ctx, input)?;
59-
let base_v = if let Some(base) = base {
60-
lower_expr(ctx, base)?
75+
// Same window as `UrlNew` above: `input_v` is a NaN-boxed heap
76+
// value held in a register while `base` lowers, which can run user
77+
// code and collect. `lower_exprs_rooted` protects each operand
78+
// whose later siblings may trigger GC and hands back reloaded
79+
// values, so nothing crosses the window in a bare register.
80+
let (input_v, base_v, operand_guard) = if let Some(base) = base {
81+
let (vals, guard) = super::temp_root::lower_exprs_rooted(ctx, &[input, base])?;
82+
(vals[0].clone(), vals[1].clone(), guard)
6183
} else {
62-
double_literal(f64::from_bits(crate::nanbox::TAG_UNDEFINED))
84+
let input_v = lower_expr(ctx, input)?;
85+
(
86+
input_v,
87+
double_literal(f64::from_bits(crate::nanbox::TAG_UNDEFINED)),
88+
None,
89+
)
6390
};
6491
let obj = ctx.block().call(
6592
I64,
6693
"js_url_pattern_new",
6794
&[(DOUBLE, &input_v), (DOUBLE, &base_v)],
6895
);
96+
// Released only after the last use of both operands.
97+
super::temp_root::temp_root_release(ctx, operand_guard);
6998
Ok(nanbox_pointer_inline(ctx.block(), &obj))
7099
}
71100

0 commit comments

Comments
 (0)