Skip to content

Commit

Permalink
Merge pull request #29 from rodrimati1992/c_abi_testing
Browse files Browse the repository at this point in the history
Version 0.8
  • Loading branch information
rodrimati1992 authored Dec 5, 2019
2 parents 8f535c6 + 52b2645 commit 9418815
Show file tree
Hide file tree
Showing 25 changed files with 763 additions and 28 deletions.
21 changes: 21 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
This is the changelog,summarising changes in each version(some minor changes may be ommited).

# 0.8

Added checks when loading dynamic libraries to ensure that Rust doesn't change how it represents
zero-sized types in the "C" ABI.
This means that in some rare cases,it won't be possible to link dynamic libraries across a
certain Rust version because it changed how it represents zero-sized types in the "C" abi.

Added RBoxError::from_debug for constructing an RBoxError from `Debug+!Display` types.

Added impls of StableAbi for PhantomData of tuples.

Added the `abi_stable::marker_type::NonOwningPhantom` marker type,
which is a more convenient way to have a `PhantomData<extern "C"fn()->PhantomData<T>>` field
PhantomData is returned from the function because of special case support
for PhantomData of tuples (eg:`PhantomData<(Foo,Bar)>`)
(tuples don't have a stable abi,but because this is a 1-aligned zero sized type,
it doesn't matter).

Fixed potential soundness bug by replacing `PhantomData<extern "C" fn( $types )>` fields
with `NonOwningPhantom<$types>`.

# 0.7

### 0.7.4
Expand Down
6 changes: 3 additions & 3 deletions abi_stable/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "abi_stable"
version = "0.7.5"
version = "0.8.1"
authors = ["rodrimati1992 <rodrimatt1985@gmail.com>"]
edition="2018"
license = "MIT/Apache-2.0"
Expand Down Expand Up @@ -50,8 +50,8 @@ channels=["crossbeam-channel"]


[dependencies]
abi_stable_derive= {version="0.7.4",path="../abi_stable_derive"}
abi_stable_shared= {version="0.7.0",path="../abi_stable_shared"}
abi_stable_derive= {version="0.8.0",path="../abi_stable_derive"}
abi_stable_shared= {version="0.8.0",path="../abi_stable_shared"}
serde = { version = "1.0.98", features = ["derive"] }
serde_derive = "1.0.98"
libloading = "0.5.2"
Expand Down
11 changes: 10 additions & 1 deletion abi_stable/src/abi_stability/layout_tests/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
crossbeam_channel::{RReceiver,RSender},
RMutex,RRwLock,ROnce
},
marker_type::UnsafeIgnoredType,
marker_type::{NonOwningPhantom,UnsafeIgnoredType},
std_types::*,
type_layout::{Tag,TLData,TypeLayout},
*,
Expand Down Expand Up @@ -343,7 +343,16 @@ fn same_different_abi_stability() {
<UnsyncRBoxError>::LAYOUT,
<RCmpOrdering>::LAYOUT,
<PhantomData<()>>::LAYOUT,
<PhantomData<(u8,)>>::LAYOUT,
<PhantomData<(u8,u16)>>::LAYOUT,
<PhantomData<(u8,u16,u32)>>::LAYOUT,
<PhantomData<RString>>::LAYOUT,
// NonOwningPhantom and PhantomData share the same type layout,
// so the NonOwningPhantom types here have to be different
<NonOwningPhantom<(RVec<()>,)>>::LAYOUT,
<NonOwningPhantom<(RVec<()>,u16)>>::LAYOUT,
<NonOwningPhantom<(RVec<()>,u16,u32)>>::LAYOUT,
<NonOwningPhantom<RVec<u32>>>::LAYOUT,
<RMutex<()>>::LAYOUT,
<RMutex<RString>>::LAYOUT,
<RRwLock<()>>::LAYOUT,
Expand Down
253 changes: 253 additions & 0 deletions abi_stable/src/abi_stability/stable_abi_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::{
ItemInfo,ReprAttr,TLPrimitive,TLDiscriminants,
GenericTLEnum, MonoTLEnum, CompTLField, CompTLFields, StartLen, DiscriminantRepr,
},
std_types::RSlice,
};


Expand Down Expand Up @@ -295,9 +296,259 @@ where T:StableAbi
GenericTLData::Struct,
)
};
}


macro_rules! phantomdata_tuples {
(ignore; $($anything:tt)*)=>{ 1 };
(
$(($tuple_param:ident,$name_ident:ident=$name_str:literal))*
)=>{
unsafe impl<$($tuple_param,)*>
GetStaticEquivalent_
for PhantomData<($($tuple_param,)*)>
where
$($tuple_param:GetStaticEquivalent_,)*
{
type StaticEquivalent=PhantomData<($($tuple_param::StaticEquivalent,)*)>;
}

unsafe impl<$($tuple_param,)*>
SharedStableAbi
for PhantomData<($($tuple_param,)*)>
where
$($tuple_param:StableAbi,)*
{
type Kind=ValueKind;
type IsNonZeroType = False;

const S_LAYOUT: &'static TypeLayout = {
const MONO_TYPE_LAYOUT:&'static MonoTypeLayout=&MonoTypeLayout::new(
*mono_shared_vars,
rstr!("PhantomData"),
ItemInfo::std_type_in(nul_str!("std::marker")),
MonoTLData::EMPTY,
tl_genparams!(;0..COUNT;),
ReprAttr::C,
ModReflMode::Module,
unsafe{
RSlice::from_raw_parts_with_lifetime(FIELDS,COUNT)
}
);

const FIELDS:&'static [CompTLField;COUNT]={
let mut i=0;
$(
#[allow(non_snake_case)]
let $tuple_param=
CompTLField::std_field($name_ident,LifetimeRange::EMPTY,i);
i+=1;
)*
&[$($tuple_param,)*]
};

const COUNT:usize=$(phantomdata_tuples!(ignore;$tuple_param)+)* 0;

make_shared_vars!{
let (mono_shared_vars,shared_vars)={
strings={ $($name_ident:$name_str,)* },
type_layouts_shared=[$($tuple_param,)*],
};
}

&TypeLayout::from_std::<Self>(
shared_vars,
MONO_TYPE_LAYOUT,
Self::S_ABI_CONSTS,
GenericTLData::Struct,
)
};
}
}
}

/*
fn main(){
for i in 1..=16{
println!("phantomdata_tuples!{{");
for j in 0..i{
println!(" (T{0},p{0}=\"{0}\")",j);
}
println!("}}")
}
}
*/

phantomdata_tuples!{
(T0,p0="0")
}
phantomdata_tuples!{
(T0,p0="0")
(T1,p1="1")
}
phantomdata_tuples!{
(T0,p0="0")
(T1,p1="1")
(T2,p2="2")
}
phantomdata_tuples!{
(T0,p0="0")
(T1,p1="1")
(T2,p2="2")
(T3,p3="3")
}
phantomdata_tuples!{
(T0,p0="0")
(T1,p1="1")
(T2,p2="2")
(T3,p3="3")
(T4,p4="4")
}
phantomdata_tuples!{
(T0,p0="0")
(T1,p1="1")
(T2,p2="2")
(T3,p3="3")
(T4,p4="4")
(T5,p5="5")
}
phantomdata_tuples!{
(T0,p0="0")
(T1,p1="1")
(T2,p2="2")
(T3,p3="3")
(T4,p4="4")
(T5,p5="5")
(T6,p6="6")
}
phantomdata_tuples!{
(T0,p0="0")
(T1,p1="1")
(T2,p2="2")
(T3,p3="3")
(T4,p4="4")
(T5,p5="5")
(T6,p6="6")
(T7,p7="7")
}
phantomdata_tuples!{
(T0,p0="0")
(T1,p1="1")
(T2,p2="2")
(T3,p3="3")
(T4,p4="4")
(T5,p5="5")
(T6,p6="6")
(T7,p7="7")
(T8,p8="8")
}
phantomdata_tuples!{
(T0,p0="0")
(T1,p1="1")
(T2,p2="2")
(T3,p3="3")
(T4,p4="4")
(T5,p5="5")
(T6,p6="6")
(T7,p7="7")
(T8,p8="8")
(T9,p9="9")
}
phantomdata_tuples!{
(T0,p0="0")
(T1,p1="1")
(T2,p2="2")
(T3,p3="3")
(T4,p4="4")
(T5,p5="5")
(T6,p6="6")
(T7,p7="7")
(T8,p8="8")
(T9,p9="9")
(T10,p10="10")
}
phantomdata_tuples!{
(T0,p0="0")
(T1,p1="1")
(T2,p2="2")
(T3,p3="3")
(T4,p4="4")
(T5,p5="5")
(T6,p6="6")
(T7,p7="7")
(T8,p8="8")
(T9,p9="9")
(T10,p10="10")
(T11,p11="11")
}
phantomdata_tuples!{
(T0,p0="0")
(T1,p1="1")
(T2,p2="2")
(T3,p3="3")
(T4,p4="4")
(T5,p5="5")
(T6,p6="6")
(T7,p7="7")
(T8,p8="8")
(T9,p9="9")
(T10,p10="10")
(T11,p11="11")
(T12,p12="12")
}
phantomdata_tuples!{
(T0,p0="0")
(T1,p1="1")
(T2,p2="2")
(T3,p3="3")
(T4,p4="4")
(T5,p5="5")
(T6,p6="6")
(T7,p7="7")
(T8,p8="8")
(T9,p9="9")
(T10,p10="10")
(T11,p11="11")
(T12,p12="12")
(T13,p13="13")
}
phantomdata_tuples!{
(T0,p0="0")
(T1,p1="1")
(T2,p2="2")
(T3,p3="3")
(T4,p4="4")
(T5,p5="5")
(T6,p6="6")
(T7,p7="7")
(T8,p8="8")
(T9,p9="9")
(T10,p10="10")
(T11,p11="11")
(T12,p12="12")
(T13,p13="13")
(T14,p14="14")
}
phantomdata_tuples!{
(T0,p0="0")
(T1,p1="1")
(T2,p2="2")
(T3,p3="3")
(T4,p4="4")
(T5,p5="5")
(T6,p6="6")
(T7,p7="7")
(T8,p8="8")
(T9,p9="9")
(T10,p10="10")
(T11,p11="11")
(T12,p12="12")
(T13,p13="13")
(T14,p14="14")
(T15,p15="15")
}




unsafe impl GetStaticEquivalent_ for () {
Expand Down Expand Up @@ -333,6 +584,8 @@ unsafe impl SharedStableAbi for () {
}




/////////////


Expand Down
1 change: 1 addition & 0 deletions abi_stable/src/derive_macro_reexports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub use crate::{
},
},
inline_storage::InlineStorage,
marker_type::NonOwningPhantom,
nonexhaustive_enum::{
assert_nonexhaustive,
GetVTable as GetNonExhaustiveVTable,
Expand Down
6 changes: 3 additions & 3 deletions abi_stable/src/erased_types/vtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use super::{
use crate::{
StableAbi,
const_utils::Transmuter,
marker_type::ErasedObject,
marker_type::{ErasedObject,NonOwningPhantom},
prefix_type::{PrefixTypeTrait,WithMetadata,panic_on_missing_fieldname},
pointer_trait::{GetPointerKind,CanTransmuteElement},
sabi_types::StaticRef,
Expand Down Expand Up @@ -66,7 +66,7 @@ pub trait GetVtable<'borr,This,ErasedPtr,OrigPtr,I:InterfaceBound> {
#[repr(transparent)]
pub struct VTableDT<'borr,T,ErasedPtr,OrigPtr,I,Unerasability>{
pub(super) vtable:StaticRef<VTable<'borr,ErasedPtr,I>>,
_for:PhantomData<extern "C" fn(T,OrigPtr,Unerasability)>
_for:NonOwningPhantom<(T,OrigPtr,Unerasability)>
}

impl<'borr,T,ErasedPtr,OrigPtr,I,Unerasability> Copy
Expand Down Expand Up @@ -94,7 +94,7 @@ where
InterfaceFor<T,I,Unerasability> as
GetVtable<'borr,T,ErasedPtr,OrigPtr,I>
>::_GET_INNER_VTABLE,
_for:PhantomData,
_for:NonOwningPhantom::NEW,
};
}

Expand Down
Loading

0 comments on commit 9418815

Please sign in to comment.