Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix segfault when passing DartOpaque through ffi boundaries #2259

Merged
merged 7 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl<'a> CodecSseTyTrait for DartOpaqueCodecSseTy<'a> {
&self.mir.get_delegate(),
match lang {
Lang::DartLang(_) => {
"BigInt.from(PlatformPointerUtil.ptrToInt(encodeDartOpaque(self, portManager.dartHandlerPort, generalizedFrbRustBinding))).toUnsigned(64)"
"PlatformPointerUtil.ptrToPlatformInt64(encodeDartOpaque(self, portManager.dartHandlerPort, generalizedFrbRustBinding))"
}
Lang::RustLang(_) => "self.encode()",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl PathTexts {
pub(crate) fn write_to_disk(&self) -> anyhow::Result<()> {
self.assert_no_duplicate_paths();
for item in self.0.iter() {
create_dir_all_and_write(&item.path, &item.text.all_code())?;
create_dir_all_and_write(&item.path, item.text.all_code())?;
}
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion frb_codegen/src/library/codegen/ir/mir/ty/dart_opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ impl MirTypeTrait for MirTypeDartOpaque {

impl MirTypeDartOpaque {
pub(crate) fn get_delegate(&self) -> MirType {
MirType::Primitive(MirTypePrimitive::Usize)
MirType::Primitive(MirTypePrimitive::Isize)
}
}
3 changes: 3 additions & 0 deletions frb_dart/lib/src/platform_types/_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ class PlatformPointerUtil {
/// {@macro flutter_rust_bridge.internal}
static int ptrToInt(PlatformPointer ptr) => ptr.address;

/// {@macro flutter_rust_bridge.internal}
static PlatformInt64 ptrToPlatformInt64(PlatformPointer ptr) => ptrToInt(ptr);

/// {@macro flutter_rust_bridge.internal}
static PlatformPointer nullPtr() => ffi.Pointer.fromAddress(0);

Expand Down
4 changes: 4 additions & 0 deletions frb_dart/lib/src/platform_types/_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class PlatformPointerUtil {
/// {@macro flutter_rust_bridge.internal}
static int ptrToInt(int ptr) => ptr;

/// {@macro flutter_rust_bridge.internal}
static PlatformInt64 ptrToPlatformInt64(PlatformPointer ptr) =>
BigInt.from(ptrToInt(ptr));

/// {@macro flutter_rust_bridge.internal}
static PlatformPointer nullPtr() => 0;

Expand Down
27 changes: 22 additions & 5 deletions frb_example/integrate_third_party/lib/src/rust/frb_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21119,6 +21119,12 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
return raw as int;
}

@protected
PlatformInt64 dco_decode_isize(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
return dcoDecodeI64(raw);
}

@protected
List<MediaStreamTrack>
dco_decode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerMediaStreamTrack(
Expand Down Expand Up @@ -22506,7 +22512,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
@protected
Object sse_decode_DartOpaque(SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
var inner = sse_decode_usize(deserializer);
var inner = sse_decode_isize(deserializer);
return decodeDartOpaque(inner, generalizedFrbRustBinding);
}

Expand Down Expand Up @@ -23940,6 +23946,12 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
return deserializer.buffer.getInt32();
}

@protected
PlatformInt64 sse_decode_isize(SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
return deserializer.buffer.getPlatformInt64();
}

@protected
List<MediaStreamTrack>
sse_decode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerMediaStreamTrack(
Expand Down Expand Up @@ -25465,10 +25477,9 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
@protected
void sse_encode_DartOpaque(Object self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
sse_encode_usize(
BigInt.from(PlatformPointerUtil.ptrToInt(encodeDartOpaque(
self, portManager.dartHandlerPort, generalizedFrbRustBinding)))
.toUnsigned(64),
sse_encode_isize(
PlatformPointerUtil.ptrToPlatformInt64(encodeDartOpaque(
self, portManager.dartHandlerPort, generalizedFrbRustBinding)),
serializer);
}

Expand Down Expand Up @@ -27058,6 +27069,12 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
serializer.buffer.putInt32(self);
}

@protected
void sse_encode_isize(PlatformInt64 self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
serializer.buffer.putPlatformInt64(self);
}

@protected
void
sse_encode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerMediaStreamTrack(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1501,6 +1501,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
int dco_decode_i_32(dynamic raw);

@protected
PlatformInt64 dco_decode_isize(dynamic raw);

@protected
List<MediaStreamTrack>
dco_decode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerMediaStreamTrack(
Expand Down Expand Up @@ -2760,6 +2763,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
int sse_decode_i_32(SseDeserializer deserializer);

@protected
PlatformInt64 sse_decode_isize(SseDeserializer deserializer);

@protected
List<MediaStreamTrack>
sse_decode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerMediaStreamTrack(
Expand Down Expand Up @@ -4056,6 +4062,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
void sse_encode_i_32(int self, SseSerializer serializer);

@protected
void sse_encode_isize(PlatformInt64 self, SseSerializer serializer);

@protected
void
sse_encode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerMediaStreamTrack(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
int dco_decode_i_32(dynamic raw);

@protected
PlatformInt64 dco_decode_isize(dynamic raw);

@protected
List<MediaStreamTrack>
dco_decode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerMediaStreamTrack(
Expand Down Expand Up @@ -2762,6 +2765,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
int sse_decode_i_32(SseDeserializer deserializer);

@protected
PlatformInt64 sse_decode_isize(SseDeserializer deserializer);

@protected
List<MediaStreamTrack>
sse_decode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerMediaStreamTrack(
Expand Down Expand Up @@ -4058,6 +4064,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
void sse_encode_i_32(int self, SseSerializer serializer);

@protected
void sse_encode_isize(PlatformInt64 self, SseSerializer serializer);

@protected
void
sse_encode_list_Auto_Owned_RustOpaque_flutter_rust_bridgefor_generatedRustAutoOpaqueInnerMediaStreamTrack(
Expand Down
21 changes: 19 additions & 2 deletions frb_example/integrate_third_party/rust/src/frb_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29963,7 +29963,7 @@ impl SseDecode for WaveShaperNode {
impl SseDecode for flutter_rust_bridge::DartOpaque {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
let mut inner = <usize>::sse_decode(deserializer);
let mut inner = <isize>::sse_decode(deserializer);
return unsafe { flutter_rust_bridge::for_generated::sse_decode_dart_opaque(inner) };
}
}
Expand Down Expand Up @@ -31014,6 +31014,13 @@ impl SseDecode for i32 {
}
}

impl SseDecode for isize {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
deserializer.cursor.read_i64::<NativeEndian>().unwrap() as _
}
}

impl SseDecode for Vec<MediaStreamTrack> {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
Expand Down Expand Up @@ -36738,7 +36745,7 @@ impl SseEncode for WaveShaperNode {
impl SseEncode for flutter_rust_bridge::DartOpaque {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {
<usize>::sse_encode(self.encode(), serializer);
<isize>::sse_encode(self.encode(), serializer);
}
}

Expand Down Expand Up @@ -37759,6 +37766,16 @@ impl SseEncode for i32 {
}
}

impl SseEncode for isize {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {
serializer
.cursor
.write_i64::<NativeEndian>(self as _)
.unwrap();
}
}

impl SseEncode for Vec<MediaStreamTrack> {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {
Expand Down
9 changes: 4 additions & 5 deletions frb_example/pure_dart/lib/src/rust/frb_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121866,7 +121866,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
@protected
Object sse_decode_DartOpaque(SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
var inner = sse_decode_usize(deserializer);
var inner = sse_decode_isize(deserializer);
return decodeDartOpaque(inner, generalizedFrbRustBinding);
}

Expand Down Expand Up @@ -147921,10 +147921,9 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
@protected
void sse_encode_DartOpaque(Object self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
sse_encode_usize(
BigInt.from(PlatformPointerUtil.ptrToInt(encodeDartOpaque(
self, portManager.dartHandlerPort, generalizedFrbRustBinding)))
.toUnsigned(64),
sse_encode_isize(
PlatformPointerUtil.ptrToPlatformInt64(encodeDartOpaque(
self, portManager.dartHandlerPort, generalizedFrbRustBinding)),
serializer);
}

Expand Down
4 changes: 2 additions & 2 deletions frb_example/pure_dart/rust/src/frb_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49817,7 +49817,7 @@ impl SseDecode for MessageWithCustomSerializerTwinNormal {
impl SseDecode for flutter_rust_bridge::DartOpaque {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
let mut inner = <usize>::sse_decode(deserializer);
let mut inner = <isize>::sse_decode(deserializer);
return unsafe { flutter_rust_bridge::for_generated::sse_decode_dart_opaque(inner) };
}
}
Expand Down Expand Up @@ -87295,7 +87295,7 @@ impl SseEncode for MessageWithCustomSerializerTwinNormal {
impl SseEncode for flutter_rust_bridge::DartOpaque {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {
<usize>::sse_encode(self.encode(), serializer);
<isize>::sse_encode(self.encode(), serializer);
}
}

Expand Down
9 changes: 4 additions & 5 deletions frb_example/pure_dart_pde/lib/src/rust/frb_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54235,7 +54235,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
@protected
Object sse_decode_DartOpaque(SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
var inner = sse_decode_usize(deserializer);
var inner = sse_decode_isize(deserializer);
return decodeDartOpaque(inner, generalizedFrbRustBinding);
}

Expand Down Expand Up @@ -65971,10 +65971,9 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
@protected
void sse_encode_DartOpaque(Object self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
sse_encode_usize(
BigInt.from(PlatformPointerUtil.ptrToInt(encodeDartOpaque(
self, portManager.dartHandlerPort, generalizedFrbRustBinding)))
.toUnsigned(64),
sse_encode_isize(
PlatformPointerUtil.ptrToPlatformInt64(encodeDartOpaque(
self, portManager.dartHandlerPort, generalizedFrbRustBinding)),
serializer);
}

Expand Down
4 changes: 2 additions & 2 deletions frb_example/pure_dart_pde/rust/src/frb_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31307,7 +31307,7 @@ impl SseDecode for MessageWithCustomSerializerTwinNormal {
impl SseDecode for flutter_rust_bridge::DartOpaque {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
let mut inner = <usize>::sse_decode(deserializer);
let mut inner = <isize>::sse_decode(deserializer);
return unsafe { flutter_rust_bridge::for_generated::sse_decode_dart_opaque(inner) };
}
}
Expand Down Expand Up @@ -51300,7 +51300,7 @@ impl SseEncode for MessageWithCustomSerializerTwinNormal {
impl SseEncode for flutter_rust_bridge::DartOpaque {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {
<usize>::sse_encode(self.encode(), serializer);
<isize>::sse_encode(self.encode(), serializer);
}
}

Expand Down
2 changes: 1 addition & 1 deletion frb_rust/src/dart_opaque/dart2rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub unsafe fn cst_decode_dart_opaque(raw: usize) -> DartOpaque {
/// # Safety
///
/// This should never be called manually.
pub unsafe fn sse_decode_dart_opaque(raw: usize) -> DartOpaque {
pub unsafe fn sse_decode_dart_opaque(raw: isize) -> DartOpaque {
DartOpaque::from_raw(raw as _)
}

Expand Down
4 changes: 2 additions & 2 deletions frb_rust/src/dart_opaque/rust2dart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ impl From<DartOpaque> for DartAbi {
}

impl DartOpaque {
pub fn encode(self) -> usize {
self.into_raw() as usize
pub fn encode(self) -> isize {
self.into_raw() as isize
}
}

Expand Down
Loading