Skip to content

Commit efeeecc

Browse files
author
Ralph Küpper
committed
fix(tls): root constructor prototype metadata (#6765)
1 parent 4a07e6e commit efeeecc

2 files changed

Lines changed: 75 additions & 21 deletions

File tree

‎crates/perry-runtime/src/object/native_module/callable_exports.rs‎

Lines changed: 67 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,7 +1401,7 @@ const TLS_SOCKET_PROTOTYPE_METHODS: &[(&str, u32)] = &[
14011401
("setKeyCert", 1),
14021402
("getSharedSigalgs", 0),
14031403
("getX509Certificate", 0),
1404-
("getPeerX509Certificate", 1),
1404+
("getPeerX509Certificate", 0),
14051405
];
14061406

14071407
thread_local! {
@@ -1434,30 +1434,45 @@ extern "C" fn tls_prototype_method_thunk(
14341434
}
14351435
}
14361436

1437-
fn attach_tls_constructor_prototype(constructor_value: f64, constructor_name: &str) {
1437+
fn attach_tls_constructor_prototype(constructor_value: f64, constructor_name: &str) -> f64 {
14381438
let methods = if constructor_name == "TLSSocket" {
14391439
TLS_SOCKET_PROTOTYPE_METHODS
14401440
} else {
14411441
&[]
14421442
};
14431443
let constructor_js = JSValue::from_bits(constructor_value.to_bits());
14441444
if !constructor_js.is_pointer() {
1445-
return;
1445+
return constructor_value;
14461446
}
14471447
let constructor = constructor_js.as_pointer::<crate::closure::ClosureHeader>() as usize;
14481448
if constructor == 0 {
1449-
return;
1449+
return constructor_value;
14501450
}
14511451

1452+
// Every allocator below can move objects. Hold only updateable handles
1453+
// across allocations and reload the current address at each use.
1454+
let scope = crate::gc::RuntimeHandleScope::new();
1455+
let constructor_handle =
1456+
scope.root_raw_mut_ptr(constructor as *mut crate::closure::ClosureHeader);
14521457
let prototype = js_object_alloc(0, 0);
14531458
if prototype.is_null() {
1454-
return;
1459+
return crate::value::js_nanbox_pointer(
1460+
constructor_handle.get_raw_mut_ptr::<crate::closure::ClosureHeader>() as i64,
1461+
);
14551462
}
1463+
let prototype_handle = scope.root_raw_mut_ptr(prototype);
14561464
let constructor_key =
14571465
crate::string::js_string_from_bytes(b"constructor".as_ptr(), "constructor".len() as u32);
1458-
js_object_set_field_by_name(prototype, constructor_key, constructor_value);
1466+
let constructor_key_handle = scope.root_string_ptr(constructor_key);
1467+
js_object_set_field_by_name(
1468+
prototype_handle.get_raw_mut_ptr(),
1469+
constructor_key_handle.get_raw_mut_ptr(),
1470+
crate::value::js_nanbox_pointer(
1471+
constructor_handle.get_raw_mut_ptr::<crate::closure::ClosureHeader>() as i64,
1472+
),
1473+
);
14591474
super::super::set_builtin_property_attrs(
1460-
prototype as usize,
1475+
prototype_handle.get_raw_mut_ptr::<ObjectHeader>() as usize,
14611476
"constructor".to_string(),
14621477
super::super::PropertyAttrs::new(true, false, true),
14631478
);
@@ -1469,35 +1484,63 @@ fn attach_tls_constructor_prototype(constructor_value: f64, constructor_name: &s
14691484
if method.is_null() {
14701485
continue;
14711486
}
1472-
crate::closure::js_closure_set_capture_ptr(method, 0, name.as_ptr() as i64);
1473-
crate::closure::js_closure_set_capture_ptr(method, 1, name.len() as i64);
1474-
set_bound_native_closure_name(method, name);
1475-
set_builtin_closure_length(method as usize, length);
1476-
let key = crate::string::js_string_from_bytes(name.as_ptr(), name.len() as u32);
1487+
let method_handle = scope.root_raw_mut_ptr(method);
1488+
crate::closure::js_closure_set_capture_ptr(
1489+
method_handle.get_raw_mut_ptr(),
1490+
0,
1491+
name.as_ptr() as i64,
1492+
);
1493+
crate::closure::js_closure_set_capture_ptr(
1494+
method_handle.get_raw_mut_ptr(),
1495+
1,
1496+
name.len() as i64,
1497+
);
1498+
let name_string = crate::string::js_string_from_bytes(name.as_ptr(), name.len() as u32);
1499+
let name_handle = scope.root_string_ptr(name_string);
1500+
crate::closure::closure_set_dynamic_prop(
1501+
method_handle.get_raw_mut_ptr::<crate::closure::ClosureHeader>() as usize,
1502+
"name",
1503+
f64::from_bits(JSValue::string_ptr(name_handle.get_raw_mut_ptr()).bits()),
1504+
);
1505+
super::super::set_builtin_property_attrs(
1506+
method_handle.get_raw_mut_ptr::<crate::closure::ClosureHeader>() as usize,
1507+
"name".to_string(),
1508+
super::super::PropertyAttrs::new(false, false, true),
1509+
);
1510+
set_builtin_closure_length(
1511+
method_handle.get_raw_mut_ptr::<crate::closure::ClosureHeader>() as usize,
1512+
length,
1513+
);
14771514
js_object_set_field_by_name(
1478-
prototype,
1479-
key,
1480-
crate::value::js_nanbox_pointer(method as i64),
1515+
prototype_handle.get_raw_mut_ptr(),
1516+
name_handle.get_raw_mut_ptr(),
1517+
crate::value::js_nanbox_pointer(
1518+
method_handle.get_raw_mut_ptr::<crate::closure::ClosureHeader>() as i64,
1519+
),
14811520
);
14821521
super::super::set_builtin_property_attrs(
1483-
prototype as usize,
1522+
prototype_handle.get_raw_mut_ptr::<ObjectHeader>() as usize,
14841523
name.to_string(),
14851524
super::super::PropertyAttrs::new(true, false, true),
14861525
);
14871526
}
14881527

14891528
crate::closure::closure_set_dynamic_prop(
1490-
constructor,
1529+
constructor_handle.get_raw_mut_ptr::<crate::closure::ClosureHeader>() as usize,
14911530
"prototype",
1492-
crate::value::js_nanbox_pointer(prototype as i64),
1531+
crate::value::js_nanbox_pointer(prototype_handle.get_raw_mut_ptr::<ObjectHeader>() as i64),
14931532
);
14941533
let parent_kind = match constructor_name {
14951534
"Server" => TLS_PARENT_EVENT_EMITTER,
14961535
"TLSSocket" => TLS_PARENT_DUPLEX,
14971536
_ => 0,
14981537
};
14991538
if parent_kind != 0 {
1500-
let bits = crate::value::js_nanbox_pointer(prototype as i64).to_bits();
1539+
let bits = crate::value::js_nanbox_pointer(
1540+
prototype_handle.get_raw_mut_ptr::<ObjectHeader>() as i64,
1541+
)
1542+
.to_bits();
1543+
crate::gc::runtime_write_barrier_root_nanbox(bits);
15011544
TLS_DERIVED_PROTOTYPES.with(|prototypes| {
15021545
let mut prototypes = prototypes.borrow_mut();
15031546
if !prototypes.iter().any(|(existing, _)| *existing == bits) {
@@ -1506,10 +1549,13 @@ fn attach_tls_constructor_prototype(constructor_value: f64, constructor_name: &s
15061549
});
15071550
}
15081551
super::super::set_builtin_property_attrs(
1509-
constructor,
1552+
constructor_handle.get_raw_mut_ptr::<crate::closure::ClosureHeader>() as usize,
15101553
"prototype".to_string(),
15111554
super::super::PropertyAttrs::new(false, false, false),
15121555
);
1556+
crate::value::js_nanbox_pointer(
1557+
constructor_handle.get_raw_mut_ptr::<crate::closure::ClosureHeader>() as i64,
1558+
)
15131559
}
15141560

15151561
pub(crate) fn tls_constructor_prototype_is_instance_of(value: f64, parent_name: &str) -> bool {
@@ -1705,7 +1751,7 @@ pub(crate) unsafe fn nm_attach_tls(
17051751
if property_name == "SecureContext" {
17061752
attach_tls_secure_context_prototype(value);
17071753
} else if matches!(property_name, "Server" | "TLSSocket") {
1708-
attach_tls_constructor_prototype(value, property_name);
1754+
value = attach_tls_constructor_prototype(value, property_name);
17091755
}
17101756
value
17111757
}

‎crates/perry/tests/issue_6765_tls_surface.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ console.log(
2525
typeof tls.TLSSocket.prototype.getX509Certificate,
2626
typeof tls.TLSSocket.prototype.getPeerX509Certificate,
2727
);
28+
console.log(
29+
"lengths:",
30+
tls.TLSSocket.prototype.setKeyCert.length,
31+
tls.TLSSocket.prototype.getSharedSigalgs.length,
32+
tls.TLSSocket.prototype.getX509Certificate.length,
33+
tls.TLSSocket.prototype.getPeerX509Certificate.length,
34+
);
2835
2936
const out: any = {};
3037
tls.convertALPNProtocols(["h2", "http/1.1"], out);
@@ -74,6 +81,7 @@ try {
7481
String::from_utf8_lossy(&run.stdout),
7582
concat!(
7683
"surface: function function function function function\n",
84+
"lengths: 1 0 0 0\n",
7785
"array: true 02683208687474702f312e31\n",
7886
"copy: 026832\n",
7987
"range: true ERR_OUT_OF_RANGE\n",

0 commit comments

Comments
 (0)