FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Validate native downcasts against the allocated payload layout by 1ndahous3 · Pull Request #8963 · RustPython/RustPython · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .rs  (12) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
19 changes: 18 additions & 1 deletion crates/derive-impl/src/pyclass.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -669,9 +669,21 @@ fn generate_class_def(
checks: member_checks,
} = extras.member_table;

let native_layout = if is_repr_transparent {
base.as_ref().map(|base| {
quote! {
const NATIVE_LAYOUT_ID: ::core::any::TypeId =
<#base as ::rustpython_vm::class::PyClassDef>::NATIVE_LAYOUT_ID;
}
})
} else {
None
};

let tokens = quote! {
impl ::rustpython_vm::class::PyClassDef for #ident {
const NAME: &'static str = #name;
#native_layout
const MODULE_NAME: Option<&'static str> = #module_name;
const TP_NAME: &'static str = #module_class_name;
const DOC: ::rustpython_vm::function::ItemDoc = #doc;
Expand Down Expand Up @@ -903,9 +915,14 @@ pub(crate) fn impl_pyclass(attr: PunctuatedNestedMeta, item: Item) -> Result<Tok
impl ::rustpython_vm::PyPayload for #ident {
const PAYLOAD_TYPE_ID: ::core::any::TypeId = <#base_type as ::rustpython_vm::PyPayload>::PAYLOAD_TYPE_ID;

fn supports_native_layout(layout: ::core::any::TypeId) -> bool {
layout == <Self as ::rustpython_vm::class::PyClassDef>::NATIVE_LAYOUT_ID
|| <#base_type as ::rustpython_vm::PyPayload>::supports_native_layout(layout)
}

#[inline]
unsafe fn validate_downcastable_from(obj: &::rustpython_vm::PyObject) -> bool {
<Self as ::rustpython_vm::class::PyClassDef>::BASICSIZE <= obj.class().payload().slots.basicsize && obj.class().fast_issubclass(<Self as ::rustpython_vm::class::StaticType>::static_type())
obj.supports_native_layout(<Self as ::rustpython_vm::class::PyClassDef>::NATIVE_LAYOUT_ID) && obj.class().fast_issubclass(<Self as ::rustpython_vm::class::StaticType>::static_type())
}

fn class(ctx: &::rustpython_vm::vm::Context) -> &'static ::rustpython_vm::Py<::rustpython_vm::builtins::PyType> {
Expand Down
8 changes: 7 additions & 1 deletion crates/derive-impl/src/pystructseq.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ pub(crate) fn impl_pystruct_sequence(
const ATTR_DOCS: &'static [&'static str] = #attr_names;
}}
const BASICSIZE: usize = 0;
const NATIVE_LAYOUT_ID: ::core::any::TypeId = ::core::any::TypeId::of::<::rustpython_vm::builtins::PyTuple>();
const UNHASHABLE: bool = false;

type Base = ::rustpython_vm::builtins::PyTuple;
Expand All @@ -577,9 +578,14 @@ pub(crate) fn impl_pystruct_sequence(
impl ::rustpython_vm::PyPayload for #pytype_ident {
const PAYLOAD_TYPE_ID: ::core::any::TypeId = <::rustpython_vm::builtins::PyTuple as ::rustpython_vm::PyPayload>::PAYLOAD_TYPE_ID;

fn supports_native_layout(layout: ::core::any::TypeId) -> bool {
<::rustpython_vm::builtins::PyTuple as ::rustpython_vm::PyPayload>::supports_native_layout(layout)
}

#[inline]
unsafe fn validate_downcastable_from(obj: &::rustpython_vm::PyObject) -> bool {
obj.class().fast_issubclass(<Self as ::rustpython_vm::class::StaticType>::static_type())
obj.supports_native_layout(::core::any::TypeId::of::<::rustpython_vm::builtins::PyTuple>())
&& obj.class().fast_issubclass(<Self as ::rustpython_vm::class::StaticType>::static_type())
}

fn class(_ctx: &::rustpython_vm::vm::Context) -> &'static ::rustpython_vm::Py<::rustpython_vm::builtins::PyType> {
Expand Down
4 changes: 4 additions & 0 deletions crates/vm/src/builtins/str.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2139,6 +2139,10 @@ impl MaybeTraverse for PyUtf8Str {
}

impl PyPayload for PyUtf8Str {
fn supports_native_layout(layout: core::any::TypeId) -> bool {
PyStr::supports_native_layout(layout)
}

#[inline]
fn class(ctx: &Context) -> &'static Py<PyType> {
ctx.types.str_type
Expand Down
6 changes: 5 additions & 1 deletion crates/vm/src/builtins/type.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,9 @@ impl PyType {
ctx: &Context,
defer_mro: bool,
) -> Result<PyRef<Self>, String> {
if slots.native_layout_id.is_none() {
slots.native_layout_id = base.slots.native_layout_id;
}
let mro = if defer_mro {
// Leave tp_mro unset so a custom metaclass mro() sees __mro__ is None.
Vec::new()
Expand Down Expand Up @@ -3798,7 +3801,8 @@ pub(crate) fn compatible_for_assignment(
(None, None) => true,
_ => false,
};
let compatible = newbase.is(oldbase) || (bases_equal && same_slots_added(newbase, oldbase));
let compatible = old_to.slots.native_layout_id == new_to.slots.native_layout_id
&& (newbase.is(oldbase) || (bases_equal && same_slots_added(newbase, oldbase)));
if compatible {
return Ok(());
}
Expand Down
6 changes: 5 additions & 1 deletion crates/vm/src/builtins/weakproxy.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ pub struct PyWeakProxy(PyWeak);
impl PyPayload for PyWeakProxy {
const PAYLOAD_TYPE_ID: core::any::TypeId = <PyWeak as PyPayload>::PAYLOAD_TYPE_ID;

fn supports_native_layout(layout: core::any::TypeId) -> bool {
PyWeak::supports_native_layout(layout)
}

#[inline]
unsafe fn validate_downcastable_from(obj: &PyObject) -> bool {
<Self as ::rustpython_vm::class::PyClassDef>::BASICSIZE <= obj.class().slots().basicsize
obj.supports_native_layout(core::any::TypeId::of::<Self>())
&& obj
.class()
.fast_issubclass(<Self as ::rustpython_vm::class::StaticType>::static_type())
Expand Down
6 changes: 6 additions & 0 deletions crates/vm/src/builtins/weakref.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ pub struct WeakNewArgs {
}

impl PyPayload for PyWeak {
fn supports_native_layout(layout: core::any::TypeId) -> bool {
// Proxies are transparent views of the same PyWeak allocation.
layout == core::any::TypeId::of::<Self>()
|| layout == core::any::TypeId::of::<super::PyWeakProxy>()
}

#[inline]
fn class(ctx: &Context) -> &'static Py<PyType> {
ctx.types.weakref_type
Expand Down
4 changes: 3 additions & 1 deletion crates/vm/src/class.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ pub trait StaticType {
}
}

pub trait PyClassDef {
pub trait PyClassDef: 'static {
const NAME: &'static str;
const MODULE_NAME: Option<&'static str>;
const TP_NAME: &'static str;
Expand All @@ -175,6 +175,7 @@ pub trait PyClassDef {
const ATTR_DOCS: &'static [&'static str] = &[];
const BASICSIZE: usize;
const ITEMSIZE: usize = 0;
const NATIVE_LAYOUT_ID: core::any::TypeId = core::any::TypeId::of::<Self>();
const UNHASHABLE: bool = false;
const MEMBERS: &'static [crate::builtins::descriptor::PyMemberSpec] = &[];

Expand Down Expand Up @@ -427,6 +428,7 @@ pub trait PyClassImpl: PyClassDef {
flags: crate::types::AtomicPyTypeFlags::from_plain(Self::TP_FLAGS),
name: Self::TP_NAME,
basicsize: Self::BASICSIZE,
native_layout_id: Some(Self::NATIVE_LAYOUT_ID),
itemsize: Self::ITEMSIZE,
doc: {
let internal = Self::INTERNAL_DOC;
Expand Down
5 changes: 5 additions & 0 deletions crates/vm/src/exceptions.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ impl PyPayload for PyBaseException {

impl PyPayload for PyMemoryError {
const PAYLOAD_TYPE_ID: core::any::TypeId = <PyException as PyPayload>::PAYLOAD_TYPE_ID;

fn supports_native_layout(layout: core::any::TypeId) -> bool {
<PyException as PyPayload>::supports_native_layout(layout)
}

const HAS_FREELIST: bool = true;
const MAX_FREELIST: usize = MEMORY_ERROR_FREELIST_SIZE;

Expand Down
Loading
Loading

Back | FazBrowse Home | New Git URL