Skip to content

Commit a1fa320

Browse files
committed
Fix miri UB: compare function pointers without usize cast
Function pointer identity checks used `f as usize == g as *const () as usize`, which strips pointer provenance and triggers miri's Undefined Behavior detector. Compare via `*const ()` directly to preserve provenance. Assisted-by: Claude
1 parent 30634d6 commit a1fa320

5 files changed

Lines changed: 26 additions & 17 deletions

File tree

‎crates/vm/src/builtins/type.rs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2827,10 +2827,11 @@ impl Callable for PyType {
28272827
// so a mismatch is conservative: if it ever compared unequal for the
28282828
// wrapper it would only take the slower cloning path, never the fast
28292829
// path incorrectly.
2830-
if zelf.slots.init.load().is_none()
2831-
&& !zelf.is(vm.ctx.types.type_type)
2832-
&& slot_new as usize != crate::types::new_wrapper as crate::types::NewFunc as usize
2833-
{
2830+
if zelf.slots.init.load().is_none() && !zelf.is(vm.ctx.types.type_type) && {
2831+
#[allow(unpredictable_function_pointer_comparisons)]
2832+
let is_wrapper = slot_new == (crate::types::new_wrapper as crate::types::NewFunc);
2833+
!is_wrapper
2834+
} {
28342835
return slot_new(zelf.to_owned(), args, vm);
28352836
}
28362837
args.clone()

‎crates/vm/src/class.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ pub fn add_operators(class: &'static Py<PyType>, ctx: &Context) {
2323
}
2424

2525
// Special handling for __hash__ = None
26+
#[allow(unpredictable_function_pointer_comparisons)]
2627
if def.name == "__hash__"
2728
&& class
2829
.slots
2930
.hash
3031
.load()
31-
.is_some_and(|h| h as usize == hash_not_implemented as *const () as usize)
32+
.is_some_and(|h| h == hash_not_implemented)
3233
{
3334
class.set_attr(ctx.names.__hash__, ctx.none.clone().into());
3435
continue;
@@ -205,7 +206,10 @@ pub trait PyClassImpl: PyClassDef {
205206
let object_new = ctx.types.object_type.slots.new.load();
206207
let is_object_itself = core::ptr::eq(class, ctx.types.object_type);
207208
let is_inherited_from_object = !is_object_itself
208-
&& object_new.is_some_and(|obj_new| slot_new as usize == obj_new as usize);
209+
&& object_new.is_some_and(
210+
#[allow(unpredictable_function_pointer_comparisons)]
211+
|obj_new| slot_new == obj_new,
212+
);
209213

210214
if !is_inherited_from_object {
211215
let bound_new =

‎crates/vm/src/frame.rs‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9149,11 +9149,12 @@ impl ExecutingFrame<'_> {
91499149
}
91509150

91519151
// Only specialize if getattro is the default (PyBaseObject::getattro)
9152+
#[allow(unpredictable_function_pointer_comparisons)]
91529153
let is_default_getattro = cls
91539154
.slots
91549155
.getattro
91559156
.load()
9156-
.is_some_and(|f| f as usize == PyBaseObject::getattro as *const () as usize);
9157+
.is_some_and(|f| f == PyBaseObject::getattro);
91579158
if !is_default_getattro {
91589159
let getattribute = cls.get_attr(identifier!(_vm, __getattribute__));
91599160
if !oparg.is_method()
@@ -10170,8 +10171,11 @@ impl ExecutingFrame<'_> {
1017010171
let cls_alloc = cls.slots.alloc.load();
1017110172
if let (Some(cls_new_fn), Some(obj_new_fn), Some(cls_alloc_fn), Some(obj_alloc_fn)) =
1017210173
(cls_new, object_new, cls_alloc, object_alloc)
10173-
&& cls_new_fn as usize == obj_new_fn as usize
10174-
&& cls_alloc_fn as usize == obj_alloc_fn as usize
10174+
&& {
10175+
#[allow(unpredictable_function_pointer_comparisons)]
10176+
let same = cls_new_fn == obj_new_fn && cls_alloc_fn == obj_alloc_fn;
10177+
same
10178+
}
1017510179
{
1017610180
if type_version == 0 {
1017710181
unsafe {
@@ -10954,11 +10958,12 @@ impl ExecutingFrame<'_> {
1095410958
}
1095510959

1095610960
// Only specialize if setattr is the default (generic_setattr)
10961+
#[allow(unpredictable_function_pointer_comparisons)]
1095710962
let is_default_setattr = cls
1095810963
.slots
1095910964
.setattro
1096010965
.load()
10961-
.is_some_and(|f| f as usize == PyBaseObject::slot_setattro as *const () as usize);
10966+
.is_some_and(|f| f == PyBaseObject::slot_setattro);
1096210967
if !is_default_setattr {
1096310968
unsafe {
1096410969
self.code.instructions.write_adaptive_counter(

‎crates/vm/src/types/slot.rs‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -953,11 +953,8 @@ impl PyType {
953953
self.slots.setattro.store(Some(setattro_wrapper));
954954
}
955955
(NativeSlot(set), NativeSlot(del)) => {
956-
let func = if set as usize == del as usize {
957-
set
958-
} else {
959-
setattro_wrapper
960-
};
956+
#[allow(unpredictable_function_pointer_comparisons)]
957+
let func = if set == del { set } else { setattro_wrapper };
961958
self.slots.setattro.store(Some(func));
962959
}
963960
(NativeSlot(func), NotFound) | (NotFound, NativeSlot(func)) => {
@@ -988,7 +985,8 @@ impl PyType {
988985
self.slots.descr_set.store(Some(descr_set_wrapper));
989986
}
990987
(NativeSlot(set), NativeSlot(delete)) => {
991-
let func = if set as usize == delete as usize {
988+
#[allow(unpredictable_function_pointer_comparisons)]
989+
let func = if set == delete {
992990
set
993991
} else {
994992
descr_set_wrapper

‎crates/vm/src/vm/method.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ impl PyMethod {
2222
pub(crate) fn get(obj: PyObjectRef, name: &Py<PyStr>, vm: &VirtualMachine) -> PyResult<Self> {
2323
let cls = obj.class();
2424
let getattro = cls.slots.getattro.load().unwrap();
25-
if getattro as usize != PyBaseObject::getattro as *const () as usize {
25+
#[allow(unpredictable_function_pointer_comparisons)]
26+
if getattro != PyBaseObject::getattro {
2627
return obj.get_attr(name, vm).map(Self::Attribute);
2728
}
2829

0 commit comments

Comments
 (0)