Skip to content

Commit f92d14f

Browse files
author
Ralph Küpper
committed
fix(tls): expose ALPN and socket prototype surface (#6765)
1 parent c86835f commit f92d14f

9 files changed

Lines changed: 371 additions & 7 deletions

File tree

‎crates/perry-api-manifest/src/entries/part_1.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,14 @@ pub(crate) const API_MANIFEST_PART_1: &[ApiEntry] = &[
797797
&[p_any("hostname"), p_any("cert")],
798798
TypeSpec::Any,
799799
),
800+
method_sig(
801+
"tls",
802+
"convertALPNProtocols",
803+
false,
804+
None,
805+
&[p_any("protocols"), p_any("out")],
806+
TypeSpec::Any,
807+
),
800808
method_sig(
801809
"tls",
802810
"createSecureContext",

‎crates/perry-codegen/src/lower_call/native_table/node_core/module_sea_tls_test.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,15 @@ pub(crate) const NODE_CORE_MODULE_SEA_TLS_TEST_ROWS: &[NativeModSig] = &[
274274
args: &[NA_F64, NA_F64],
275275
ret: NR_F64,
276276
},
277+
NativeModSig {
278+
module: "tls",
279+
has_receiver: false,
280+
method: "convertALPNProtocols",
281+
class_filter: None,
282+
runtime: "js_tls_convert_alpn_protocols",
283+
args: &[NA_F64, NA_F64],
284+
ret: NR_F64,
285+
},
277286
NativeModSig {
278287
module: "tls",
279288
has_receiver: false,

‎crates/perry-runtime/src/object/instanceof.rs‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,15 @@ pub extern "C" fn js_instanceof_dynamic(value: f64, type_ref: f64) -> f64 {
296296
method.as_str(),
297297
"Readable" | "Writable" | "Duplex" | "Transform" | "PassThrough" | "Stream"
298298
)
299-
&& crate::node_stream::is_classic_stream_instance_of(value, method.as_str())
299+
&& (crate::node_stream::is_classic_stream_instance_of(value, method.as_str())
300+
|| super::tls_constructor_prototype_is_instance_of(value, method.as_str()))
300301
{
301302
return f64::from_bits(crate::value::TAG_TRUE);
302303
}
303-
if module == "events" && method == "EventEmitter" && is_event_emitter_instance_value(value)
304+
if module == "events"
305+
&& method == "EventEmitter"
306+
&& (is_event_emitter_instance_value(value)
307+
|| super::tls_constructor_prototype_is_instance_of(value, method.as_str()))
304308
{
305309
return f64::from_bits(crate::value::TAG_TRUE);
306310
}
@@ -1007,14 +1011,18 @@ pub extern "C" fn js_instanceof(value: f64, class_id: u32) -> f64 {
10071011
_ => None,
10081012
};
10091013
if let Some(name) = classic_stream_name {
1010-
return if crate::node_stream::is_classic_stream_instance_of(value, name) {
1014+
return if crate::node_stream::is_classic_stream_instance_of(value, name)
1015+
|| super::tls_constructor_prototype_is_instance_of(value, name)
1016+
{
10111017
true_val
10121018
} else {
10131019
false_val
10141020
};
10151021
}
10161022
if class_id == CLASS_ID_EVENT_EMITTER {
1017-
return if is_event_emitter_instance_value(value) {
1023+
return if is_event_emitter_instance_value(value)
1024+
|| super::tls_constructor_prototype_is_instance_of(value, "EventEmitter")
1025+
{
10181026
true_val
10191027
} else {
10201028
false_val

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ pub(crate) use callable_exports::{
2727
fs_namespace_descriptor_getter_value, fs_namespace_descriptor_setter_value,
2828
is_buffer_constructor_value, is_cluster_emitter_method, module_cjs_cache_value,
2929
module_cjs_extensions_value, module_cjs_global_paths_value, module_cjs_path_cache_value,
30-
native_string_value, set_bound_native_closure_name, set_builtin_closure_length,
31-
set_builtin_closure_non_constructable, sqlite_session_constructor_value,
32-
sqlite_statement_sync_constructor_value, timers_promises_parent_namespace,
30+
native_string_value, scan_tls_derived_prototype_roots_mut, set_bound_native_closure_name,
31+
set_builtin_closure_length, set_builtin_closure_non_constructable,
32+
sqlite_session_constructor_value, sqlite_statement_sync_constructor_value,
33+
timers_promises_parent_namespace, tls_constructor_prototype_is_instance_of,
3334
util_inspect_default_options_value, zlib_codes_object,
3435
};
3536
pub(crate) use constants::get_native_module_constant;
@@ -239,6 +240,7 @@ pub fn scan_native_callable_export_roots_mut(visitor: &mut crate::gc::RuntimeRoo
239240
#[cfg(feature = "mod-http2-constants")]
240241
crate::node_http2_constants::scan_roots_mut(visitor);
241242
scan_stream_event_emitter_prototype_roots_mut(visitor);
243+
scan_tls_derived_prototype_roots_mut(visitor);
242244
}
243245

244246
/// Special class ID for native module namespace objects

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ pub(crate) fn is_native_module_callable_export_reference(module: &str, prop: &st
283283
| ("net", "_normalizeArgs")
284284
| ("net", "_createServerHandle")
285285
| ("tls", "connect")
286+
| ("tls", "convertALPNProtocols")
286287
| ("tls", "createServer")
287288
| ("tls", "Server")
288289
| ("tls", "TLSSocket")
@@ -1834,6 +1835,7 @@ static CALLABLE_EXPORT_TABLE: &[(&str, &[&str])] = &[
18341835
"TLSSocket",
18351836
"checkServerIdentity",
18361837
"connect",
1838+
"convertALPNProtocols",
18371839
"createSecureContext",
18381840
"createServer",
18391841
"getCACertificates",

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

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ fn native_callable_export_arity_reference(module: &str, prop: &str) -> Option<u3
258258
Some(1)
259259
}
260260
("tls", "checkServerIdentity") => Some(2),
261+
("tls", "convertALPNProtocols") => Some(2),
261262
("tls", "SecureContext") => Some(1),
262263
// #3726: `crypto.Cipheriv` / `crypto.Decipheriv` constructor exports —
263264
// `(cipher, key, iv, options)` arity matches Node's length 4.
@@ -1396,6 +1397,135 @@ fn attach_tls_secure_context_prototype(constructor_value: f64) {
13961397
crate::tls::attach_secure_context_constructor_prototype(constructor_value);
13971398
}
13981399

1400+
const TLS_SOCKET_PROTOTYPE_METHODS: &[(&str, u32)] = &[
1401+
("setKeyCert", 1),
1402+
("getSharedSigalgs", 0),
1403+
("getX509Certificate", 0),
1404+
("getPeerX509Certificate", 1),
1405+
];
1406+
1407+
thread_local! {
1408+
static TLS_DERIVED_PROTOTYPES: RefCell<Vec<(u64, u8)>> = const { RefCell::new(Vec::new()) };
1409+
}
1410+
1411+
const TLS_PARENT_EVENT_EMITTER: u8 = 1;
1412+
const TLS_PARENT_DUPLEX: u8 = 2;
1413+
1414+
pub(crate) fn scan_tls_derived_prototype_roots_mut(
1415+
visitor: &mut crate::gc::RuntimeRootVisitor<'_>,
1416+
) {
1417+
TLS_DERIVED_PROTOTYPES.with(|prototypes| {
1418+
for (bits, _) in prototypes.borrow_mut().iter_mut() {
1419+
visitor.visit_nanbox_u64_slot(bits);
1420+
}
1421+
});
1422+
}
1423+
1424+
extern "C" fn tls_prototype_method_thunk(
1425+
closure: *const crate::closure::ClosureHeader,
1426+
rest: f64,
1427+
) -> f64 {
1428+
unsafe {
1429+
let name_ptr = crate::closure::js_closure_get_capture_ptr(closure, 0) as *const i8;
1430+
let name_len = crate::closure::js_closure_get_capture_ptr(closure, 1) as usize;
1431+
let receiver = crate::object::js_implicit_this_get();
1432+
let args_array = crate::value::js_nanbox_get_pointer(rest);
1433+
crate::object::js_native_call_method_apply(receiver, name_ptr, name_len, args_array)
1434+
}
1435+
}
1436+
1437+
fn attach_tls_constructor_prototype(constructor_value: f64, constructor_name: &str) {
1438+
let methods = if constructor_name == "TLSSocket" {
1439+
TLS_SOCKET_PROTOTYPE_METHODS
1440+
} else {
1441+
&[]
1442+
};
1443+
let constructor_js = JSValue::from_bits(constructor_value.to_bits());
1444+
if !constructor_js.is_pointer() {
1445+
return;
1446+
}
1447+
let constructor = constructor_js.as_pointer::<crate::closure::ClosureHeader>() as usize;
1448+
if constructor == 0 {
1449+
return;
1450+
}
1451+
1452+
let prototype = js_object_alloc(0, 0);
1453+
if prototype.is_null() {
1454+
return;
1455+
}
1456+
let constructor_key =
1457+
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);
1459+
super::super::set_builtin_property_attrs(
1460+
prototype as usize,
1461+
"constructor".to_string(),
1462+
super::super::PropertyAttrs::new(true, false, true),
1463+
);
1464+
1465+
let thunk = tls_prototype_method_thunk as *const u8;
1466+
crate::closure::js_register_closure_rest(thunk, 0);
1467+
for &(name, length) in methods {
1468+
let method = crate::closure::js_closure_alloc(thunk, 2);
1469+
if method.is_null() {
1470+
continue;
1471+
}
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);
1477+
js_object_set_field_by_name(
1478+
prototype,
1479+
key,
1480+
crate::value::js_nanbox_pointer(method as i64),
1481+
);
1482+
super::super::set_builtin_property_attrs(
1483+
prototype as usize,
1484+
name.to_string(),
1485+
super::super::PropertyAttrs::new(true, false, true),
1486+
);
1487+
}
1488+
1489+
crate::closure::closure_set_dynamic_prop(
1490+
constructor,
1491+
"prototype",
1492+
crate::value::js_nanbox_pointer(prototype as i64),
1493+
);
1494+
let parent_kind = match constructor_name {
1495+
"Server" => TLS_PARENT_EVENT_EMITTER,
1496+
"TLSSocket" => TLS_PARENT_DUPLEX,
1497+
_ => 0,
1498+
};
1499+
if parent_kind != 0 {
1500+
let bits = crate::value::js_nanbox_pointer(prototype as i64).to_bits();
1501+
TLS_DERIVED_PROTOTYPES.with(|prototypes| {
1502+
let mut prototypes = prototypes.borrow_mut();
1503+
if !prototypes.iter().any(|(existing, _)| *existing == bits) {
1504+
prototypes.push((bits, parent_kind));
1505+
}
1506+
});
1507+
}
1508+
super::super::set_builtin_property_attrs(
1509+
constructor,
1510+
"prototype".to_string(),
1511+
super::super::PropertyAttrs::new(false, false, false),
1512+
);
1513+
}
1514+
1515+
pub(crate) fn tls_constructor_prototype_is_instance_of(value: f64, parent_name: &str) -> bool {
1516+
let parent_kind = match parent_name {
1517+
"EventEmitter" => TLS_PARENT_EVENT_EMITTER,
1518+
"Duplex" => TLS_PARENT_DUPLEX,
1519+
_ => return false,
1520+
};
1521+
TLS_DERIVED_PROTOTYPES.with(|prototypes| {
1522+
prototypes
1523+
.borrow()
1524+
.iter()
1525+
.any(|(bits, kind)| *bits == value.to_bits() && *kind == parent_kind)
1526+
})
1527+
}
1528+
13991529
pub(crate) unsafe fn bound_native_callable_module_and_method(
14001530
value: f64,
14011531
) -> Option<(String, String)> {
@@ -1574,6 +1704,8 @@ pub(crate) unsafe fn nm_attach_tls(
15741704
) -> f64 {
15751705
if property_name == "SecureContext" {
15761706
attach_tls_secure_context_prototype(value);
1707+
} else if matches!(property_name, "Server" | "TLSSocket") {
1708+
attach_tls_constructor_prototype(value, property_name);
15771709
}
15781710
value
15791711
}
@@ -2103,6 +2235,7 @@ static CALLABLE_EXPORT_ARITY_TABLE: &[(&str, &[(&str, u32)])] = &[
21032235
("TLSSocket", 2),
21042236
("checkServerIdentity", 2),
21052237
("connect", 4),
2238+
("convertALPNProtocols", 2),
21062239
("createSecureContext", 1),
21072240
("createServer", 2),
21082241
("getCACertificates", 1),
@@ -2220,4 +2353,50 @@ mod callable_export_arity_table_tests {
22202353
}
22212354
}
22222355
}
2356+
2357+
#[test]
2358+
fn tls_constructor_prototypes_match_node_parent_classes() {
2359+
let server = bound_native_callable_export_value("tls", "Server");
2360+
let server_addr = (server.to_bits() & crate::value::POINTER_MASK) as usize;
2361+
let server_proto = crate::closure::closure_get_dynamic_prop(server_addr, "prototype");
2362+
assert!(tls_constructor_prototype_is_instance_of(
2363+
server_proto,
2364+
"EventEmitter"
2365+
));
2366+
assert_eq!(
2367+
crate::object::js_instanceof(server_proto, 0xFFFF_0076).to_bits(),
2368+
crate::value::TAG_TRUE
2369+
);
2370+
let event_emitter = bound_native_callable_export_value("events", "EventEmitter");
2371+
assert_eq!(
2372+
crate::object::js_instanceof_dynamic(server_proto, event_emitter).to_bits(),
2373+
crate::value::TAG_TRUE
2374+
);
2375+
2376+
let socket = bound_native_callable_export_value("tls", "TLSSocket");
2377+
let socket_addr = (socket.to_bits() & crate::value::POINTER_MASK) as usize;
2378+
let socket_proto = crate::closure::closure_get_dynamic_prop(socket_addr, "prototype");
2379+
assert!(tls_constructor_prototype_is_instance_of(
2380+
socket_proto,
2381+
"Duplex"
2382+
));
2383+
let socket_proto_obj =
2384+
JSValue::from_bits(socket_proto.to_bits()).as_pointer::<ObjectHeader>();
2385+
for &(name, length) in TLS_SOCKET_PROTOTYPE_METHODS {
2386+
let key = crate::string::js_string_from_bytes(name.as_ptr(), name.len() as u32);
2387+
let method = crate::object::js_object_get_field_by_name(socket_proto_obj, key);
2388+
let method_addr = method.as_pointer::<crate::closure::ClosureHeader>() as usize;
2389+
assert!(crate::closure::is_closure_ptr(method_addr), "{name}");
2390+
assert_eq!(builtin_closure_length(method_addr), Some(length), "{name}");
2391+
}
2392+
assert_eq!(
2393+
crate::object::js_instanceof(socket_proto, 0xFFFF_0073).to_bits(),
2394+
crate::value::TAG_TRUE
2395+
);
2396+
let duplex = bound_native_callable_export_value("stream", "Duplex");
2397+
assert_eq!(
2398+
crate::object::js_instanceof_dynamic(socket_proto, duplex).to_bits(),
2399+
crate::value::TAG_TRUE
2400+
);
2401+
}
22232402
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1827,6 +1827,7 @@ pub(crate) fn native_module_enumerable_keys(module_name: &str) -> Option<&'stati
18271827
"tls" => Some(&[
18281828
b"checkServerIdentity",
18291829
b"connect",
1830+
b"convertALPNProtocols",
18301831
b"createServer",
18311832
b"createSecureContext",
18321833
b"getCACertificates",

0 commit comments

Comments
 (0)