Skip to content

Commit 23146bf

Browse files
committed
fix(jit): harden native tail-link publication
1 parent 5c4dff9 commit 23146bf

3 files changed

Lines changed: 57 additions & 29 deletions

File tree

‎src/vm/jit/native/lower.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ pub(crate) fn compile_tail_side_link_body(
189189
let slot_address = iconst_ptr_from_addr(builder, pointer_type, slot_address)?;
190190
let target = builder
191191
.ins()
192-
.load(pointer_type, MemFlags::new(), slot_address, 0);
192+
.atomic_load(pointer_type, MemFlags::new(), slot_address);
193193
let is_null = builder.ins().icmp_imm(IntCC::Equal, target, 0);
194194
builder.ins().brif(is_null, deopt, &[], linked, &[]);
195195

@@ -279,7 +279,7 @@ pub(crate) fn compile_tail_trace_dispatcher(
279279
let slot_address = iconst_ptr_from_addr(builder, pointer_type, slot_address)?;
280280
let target = builder
281281
.ins()
282-
.load(pointer_type, MemFlags::new(), slot_address, 0);
282+
.atomic_load(pointer_type, MemFlags::new(), slot_address);
283283
let is_null = builder.ins().icmp_imm(IntCC::Equal, target, 0);
284284
builder
285285
.ins()
@@ -344,7 +344,7 @@ pub(crate) fn compile_tail_owned_side_link_body(
344344
let slot_address = iconst_ptr_from_addr(builder, pointer_type, slot_address)?;
345345
let target = builder
346346
.ins()
347-
.load(pointer_type, MemFlags::new(), slot_address, 0);
347+
.atomic_load(pointer_type, MemFlags::new(), slot_address);
348348
let is_null = builder.ins().icmp_imm(IntCC::Equal, target, 0);
349349
builder.ins().brif(is_null, deopt, &[], linked, &[]);
350350

‎src/vm/jit/native/mod.rs‎

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,19 @@ pub(crate) fn classify_side_entry_repr(
6666

6767
pub(crate) fn classify_side_entry_materialization(
6868
materialization: &SsaMaterialization,
69-
) -> InheritedStateAbiClass {
69+
defining_repr: Option<SsaValueRepr>,
70+
) -> Option<InheritedStateAbiClass> {
7071
match materialization {
7172
SsaMaterialization::Value(_) => {
72-
InheritedStateAbiClass::Tagged(SideEntryOwnership::Borrowed)
73+
defining_repr.map(|repr| classify_side_entry_repr(repr, SideEntryOwnership::Borrowed))
7374
}
74-
SsaMaterialization::BoxInt(_) => InheritedStateAbiClass::ScalarInt,
75-
SsaMaterialization::BoxFloat(_) => InheritedStateAbiClass::ScalarFloat,
76-
SsaMaterialization::BoxBool(_) => InheritedStateAbiClass::ScalarBool,
77-
SsaMaterialization::BoxHeapPtr { tag, .. } => InheritedStateAbiClass::HeapPointer {
75+
SsaMaterialization::BoxInt(_) => Some(InheritedStateAbiClass::ScalarInt),
76+
SsaMaterialization::BoxFloat(_) => Some(InheritedStateAbiClass::ScalarFloat),
77+
SsaMaterialization::BoxBool(_) => Some(InheritedStateAbiClass::ScalarBool),
78+
SsaMaterialization::BoxHeapPtr { tag, .. } => Some(InheritedStateAbiClass::HeapPointer {
7879
tag: *tag,
7980
ownership: SideEntryOwnership::Owned,
80-
},
81+
}),
8182
}
8283
}
8384

@@ -104,7 +105,7 @@ impl NativeSideLinkSlot {
104105
self.entry.store(std::ptr::null_mut(), Ordering::Release);
105106
}
106107

107-
pub(crate) fn address(&self) -> *mut *mut u8 {
108+
pub(crate) fn address(self: &Arc<Self>) -> *mut *mut u8 {
108109
self.entry.as_ptr()
109110
}
110111
}
@@ -296,30 +297,54 @@ mod tests {
296297
fn side_entry_abi_classifies_scalar_pointer_tagged_borrowed_and_owned_values() {
297298
let value = SsaValueId::new(7);
298299
assert_eq!(
299-
classify_side_entry_materialization(&SsaMaterialization::BoxInt(value)),
300-
InheritedStateAbiClass::ScalarInt
300+
classify_side_entry_materialization(&SsaMaterialization::BoxInt(value), None),
301+
Some(InheritedStateAbiClass::ScalarInt)
301302
);
302303
assert_eq!(
303-
classify_side_entry_materialization(&SsaMaterialization::BoxFloat(value)),
304-
InheritedStateAbiClass::ScalarFloat
304+
classify_side_entry_materialization(&SsaMaterialization::BoxFloat(value), None),
305+
Some(InheritedStateAbiClass::ScalarFloat)
305306
);
306307
assert_eq!(
307-
classify_side_entry_materialization(&SsaMaterialization::BoxBool(value)),
308-
InheritedStateAbiClass::ScalarBool
308+
classify_side_entry_materialization(&SsaMaterialization::BoxBool(value), None),
309+
Some(InheritedStateAbiClass::ScalarBool)
309310
);
311+
for (repr, expected) in [
312+
(SsaValueRepr::I64, InheritedStateAbiClass::ScalarInt),
313+
(SsaValueRepr::F64, InheritedStateAbiClass::ScalarFloat),
314+
(SsaValueRepr::Bool, InheritedStateAbiClass::ScalarBool),
315+
(
316+
SsaValueRepr::HeapPtr(ValueType::Array),
317+
InheritedStateAbiClass::HeapPointer {
318+
tag: ValueType::Array,
319+
ownership: SideEntryOwnership::Borrowed,
320+
},
321+
),
322+
(
323+
SsaValueRepr::Tagged,
324+
InheritedStateAbiClass::Tagged(SideEntryOwnership::Borrowed),
325+
),
326+
] {
327+
assert_eq!(
328+
classify_side_entry_materialization(&SsaMaterialization::Value(value), Some(repr),),
329+
Some(expected)
330+
);
331+
}
310332
assert_eq!(
311-
classify_side_entry_materialization(&SsaMaterialization::Value(value)),
312-
InheritedStateAbiClass::Tagged(SideEntryOwnership::Borrowed)
333+
classify_side_entry_materialization(&SsaMaterialization::Value(value), None),
334+
None
313335
);
314336
assert_eq!(
315-
classify_side_entry_materialization(&SsaMaterialization::BoxHeapPtr {
316-
value,
317-
tag: ValueType::String,
318-
}),
319-
InheritedStateAbiClass::HeapPointer {
337+
classify_side_entry_materialization(
338+
&SsaMaterialization::BoxHeapPtr {
339+
value,
340+
tag: ValueType::String,
341+
},
342+
None,
343+
),
344+
Some(InheritedStateAbiClass::HeapPointer {
320345
tag: ValueType::String,
321346
ownership: SideEntryOwnership::Owned,
322-
}
347+
})
323348
);
324349
assert_eq!(
325350
classify_side_entry_repr(
@@ -341,7 +366,7 @@ mod tests {
341366
}
342367
const DEOPT_STATUS: i32 = 17;
343368
const CHILD_STATUS: i32 = 23;
344-
let slot = Box::new(NativeSideLinkSlot::new());
369+
let slot = Arc::new(NativeSideLinkSlot::new());
345370
let child = compile_tail_status_body(CHILD_STATUS).expect("tail child should compile");
346371
let root = compile_tail_side_link_body(slot.address() as usize, DEOPT_STATUS)
347372
.expect("tail root should compile");
@@ -376,7 +401,7 @@ mod tests {
376401
}
377402
const DEOPT_STATUS: i32 = 29;
378403
const CHILD_STATUS: i32 = 31;
379-
let slot = Box::new(NativeSideLinkSlot::new());
404+
let slot = Arc::new(NativeSideLinkSlot::new());
380405
let child =
381406
compile_tail_owned_clear_body(CHILD_STATUS).expect("owned child should compile");
382407
let root = compile_tail_owned_side_link_body(slot.address() as usize, DEOPT_STATUS)

‎src/vm/native/exec.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,12 @@ const fn map_jit_flag() -> i32 {
182182

183183
#[cfg(all(unix, target_os = "macos", target_arch = "aarch64"))]
184184
pub(crate) unsafe fn protect_executable_buffer(_ptr: *mut u8, _len: usize) -> VmResult<()> {
185-
if unsafe { libc::pthread_jit_write_protect_supported_np() } != 0 {
186-
unsafe { libc::pthread_jit_write_protect_np(1) };
185+
if unsafe { libc::pthread_jit_write_protect_supported_np() } == 0 {
186+
return Err(VmError::JitNative(
187+
"macOS JIT write protection is unavailable".to_string(),
188+
));
187189
}
190+
unsafe { libc::pthread_jit_write_protect_np(1) };
188191
Ok(())
189192
}
190193

0 commit comments

Comments
 (0)