diff --git a/ffi/MIGRATE.md b/ffi/MIGRATE.md index a0b8e0069..a5ef742af 100644 --- a/ffi/MIGRATE.md +++ b/ffi/MIGRATE.md @@ -2,6 +2,11 @@ ## Unreleased +- A type parameter whose bounds imply bytes, through a supertrait such as moq's + `IntoBytes: AsRef<[u8]>` or a `From` implementation such as moq's + `impl> From for Timed`, exports as bytes rather than + `String`. Kotlin takes a `ByteArray` there: pass text as `"text".encodeToByteArray()`. + C and C++ keep their types, and accept a payload that is not UTF-8 rather than aborting. - `&[T]` of any type and `SocketAddrV4` and `SocketAddrV6` map, so a struct or enum whose fields were refused only for the socket addresses is a value now, not an object, and an item that newly exports can clash with another's C name, which takes a `rename`. diff --git a/ffi/README.md b/ffi/README.md index 95d96f840..8d4c1d4ec 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -82,7 +82,8 @@ export's, and adds nothing; another crate's type is refused. | `impl RangeBounds` of an integer | `_bounds_`: its start and end bounds | | `Option` | a null handle or buffer, or `_option_` | | a `pub const` | a `#define` if a `bool` or integer, else a function returning it | -| `impl Into`, `impl AsRef` | `X` | +| `impl Into`, `impl AsRef`, or a bound implying one, as a supertrait does | `X`, or `&X` and then `String` or `Vec` where `X` is `str` or `[T]` | +| `impl Into`, where `X` does not map | what a `From` implementation into `X` takes, by its source parameter's bounds as above: `impl> From for Timed` takes `Bytes` | | another type parameter | the first of `String`, `Bytes`, and `Vec` meeting its bounds, or a foreign implementation of its one exported trait; else an instance of the function at each type the exports implement a trait bounding it for, as `&X` where that meets its bounds too, so C keeps its handle, and, as an argument of a generic type, at each argument of an instance that meets its bounds and makes each generic type the signature names an instance | A generic type infers the instances it exports at, with no config: the one at its diff --git a/ffi/fixtures/api/examples/transcript.rs b/ffi/fixtures/api/examples/transcript.rs index 5fe4f4b08..24a8ba5ca 100644 --- a/ffi/fixtures/api/examples/transcript.rs +++ b/ffi/fixtures/api/examples/transcript.rs @@ -151,6 +151,8 @@ fn main() { println!("{}", api_lib::greet("x", Shape::Square)); println!("len {}", api_lib::len(b"abc")); + println!("hex {}", api_lib::hex(Bytes::from_static(b"\xff\x00a"))); + println!("timed hex {}", api_lib::timed_hex(Bytes::from_static(b"\xff\x00a"))); for event in [Event::Moved(p), Event::Named { name: "n".to_string() }, Event::Done] { println!("{}", api_lib::describe(&event)); } diff --git a/ffi/fixtures/api/ffi.toml b/ffi/fixtures/api/ffi.toml index c972c5cf8..91c65c77e 100644 --- a/ffi/fixtures/api/ffi.toml +++ b/ffi/fixtures/api/ffi.toml @@ -1,18 +1,21 @@ package = "api-fixture" prefix = "api" # The list's tail is not `Clone`, so its getter has no copy to return, one instance leaves -# out a method the others keep, `Seed` is generic, so no callback implements it, and +# out a method the others keep, `Seed` is generic, so no callback implements it, `Payload` +# consumes itself, which no callback can, `Timed` has no instance that exports, and # `field@` leaves out a field alone where a method takes its path, as `Retry`'s and # `Slot`'s do. `Slot` has no such method, so its path alone names its field. A # function instance is left out by its path, as `depth_past` at `polar::Point` is. exclude = [ "Node::next", "Pair::swap", + "Payload", "Seed", "Slot::value", "field@Retry::delay", "field@Slot::value", "Stack::depth_past", + "Timed", ] # The field `Retry::limit`, whose name its method takes. diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index fecfcedc5..404a2dbf1 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -595,6 +595,67 @@ pub fn len(payload: &[u8]) -> usize { payload.len() } +/// Converts into owned bytes, as moq's `IntoBytes` does: text implements it too, but its +/// supertrait makes a parameter it bounds bytes, and owned ones, as it must be `'static`. +pub trait Payload: AsRef<[u8]> + 'static { + fn into_bytes(self) -> Bytes; +} + +impl Payload for Bytes { + fn into_bytes(self) -> Bytes { + self + } +} + +impl Payload for Vec { + fn into_bytes(self) -> Bytes { + self.into() + } +} + +impl Payload for String { + fn into_bytes(self) -> Bytes { + self.into() + } +} + +impl Payload for &'static str { + fn into_bytes(self) -> Bytes { + Bytes::from_static(self.as_bytes()) + } +} + +impl Payload for &'static [u8] { + fn into_bytes(self) -> Bytes { + Bytes::from_static(self) + } +} + +/// A payload's bytes in hex, which need not be UTF-8. +pub fn hex(payload: impl Payload) -> String { + payload.into_bytes().iter().map(|b| format!("{b:02x}")).collect() +} + +/// A value and when it was stamped, as moq's `Timed` is, which bytes convert into. +pub struct Timed

{ + pub value: P, + pub at: Option, +} + +impl> From for Timed { + fn from(value: B) -> Self { + Self { + value: Bytes::copy_from_slice(value.as_ref()), + at: None, + } + } +} + +/// A timed payload's bytes in hex, which need not be UTF-8, owned as it is `'static`. +pub fn timed_hex(payload: impl Into> + 'static) -> String { + hex(payload.into().value) +} + pub fn greet(name: impl Into, shape: Shape) -> String { format!("hello {}, a {shape:?}", name.into()) } diff --git a/ffi/moq/bench/export.kt b/ffi/moq/bench/export.kt index 6c67eb58f..2f484cc59 100644 --- a/ffi/moq/bench/export.kt +++ b/ffi/moq/bench/export.kt @@ -16,8 +16,8 @@ fun round(): String { broadcast.createTrack("bench", TrackInfo.default().withTimescale(micro)) } - // The C ABI takes moq's `impl Into` frame as a string. - val frame = "x".repeat(100) + // A frame of 100 zero bytes, as bench/export.c writes. + val frame = ByteArray(100) var micros = 0uL // Each frame stamped through a fresh timestamp handle, as a caller holding a u64 must. diff --git a/ffi/moq/moq.c b/ffi/moq/moq.c index 8824e86d5..5d6412ebe 100644 --- a/ffi/moq/moq.c +++ b/ffi/moq/moq.c @@ -124,13 +124,13 @@ int main(void) { moq_error_free(err); CHECK(moq_timestamp_as_secs(at) == 1); - /* The last frame, then the group's end, which reads as None. */ - CHECK(moq_group_producer_write_frame(writer, NULL, str("world"), &err)); + /* The last frame, bytes that are not UTF-8, then the group's end, which reads as None. */ + CHECK(moq_group_producer_write_frame(writer, NULL, str("\xffworld"), &err)); CHECK(moq_group_producer_finish(writer, &err)); reading = moq_group_consumer_read_frame(group); CHECK(moq_group_consumer_read_frame_poll(reading, waker, &frame, &err) == MOQ_POLL_READY); moq_group_consumer_read_frame_future_free(reading); - CHECK(frame != NULL && is(moq_frame_frame_payload(frame), "world")); + CHECK(frame != NULL && is(moq_frame_frame_payload(frame), "\xffworld")); moq_frame_frame_free(frame); reading = moq_group_consumer_read_frame(group); frame = (moq_frame_frame *)1; diff --git a/ffi/moq/moq.cpp b/ffi/moq/moq.cpp index f72ca8df6..8efd3636d 100644 --- a/ffi/moq/moq.cpp +++ b/ffi/moq/moq.cpp @@ -86,11 +86,12 @@ int main() { CHECK(!late && is(late.error().to_string(), "frame timestamp doesn't fit its track")); CHECK(at->as_secs() == 1); - // The last frame, then the group's end, which reads as None, both blocking. - CHECK(writer->write_frame(nullptr, "world")); + // The last frame, bytes that are not UTF-8, then the group's end, which reads as None, + // both blocking. + CHECK(writer->write_frame(nullptr, "\xffworld")); CHECK(std::move(*writer).finish()); auto world = group.read_frame().get(); - CHECK(world && world.value() && is(world.value()->payload(), "world")); + CHECK(world && world.value() && is(world.value()->payload(), "\xffworld")); auto end = group.read_frame().get(); CHECK(end && !end.value()); diff --git a/ffi/moq/moq.kt b/ffi/moq/moq.kt index 1481bc709..18021e082 100644 --- a/ffi/moq/moq.kt +++ b/ffi/moq/moq.kt @@ -75,7 +75,7 @@ fun main() { scope.launch { frame = first.readFrame() } q.drain() expect(frame == null, "a read waits for a frame") - writer.writeFrame(null, "hello") + writer.writeFrame(null, "hello".encodeToByteArray()) expect(q.size == 1, "a frame's arrival wakes the read") q.drain() val hello = frame!! @@ -89,18 +89,19 @@ fun main() { val at = Timestamp.fromMillis(1500u) expect(at.asSecs() == 1uL, "1500 ms is 1 s") try { - writer.writeFrame(at, "late") + writer.writeFrame(at, "late".encodeToByteArray()) expect(false, "a timestamped frame on an untimed track is an error") } catch (e: ErrorException) { expect(e.message == "frame timestamp doesn't fit its track", "the late frame's error is Rust's") } expect(at.asSecs() == 1uL, "a copied timestamp stays usable") - // The last frame, then the group's end, which reads as null. - writer.writeFrame(null, "world") + // The last frame, bytes that are not UTF-8, then the group's end, which reads as null. + val world = byteArrayOf(0xff.toByte()) + "world".encodeToByteArray() + writer.writeFrame(null, world) writer.finish() runBlocking { - expect(first.readFrame()?.payload?.decodeToString() == "world", "the last frame reads") + expect(first.readFrame()?.payload?.contentEquals(world) == true, "the last frame reads") expect(first.readFrame() == null, "a finished group reads null") } diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index 7a541d07e..ffba3f862 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -14,12 +14,15 @@ use std::collections::VecDeque; use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::{CrateNum, DefId}; use rustc_infer::infer::TyCtxtInferExt; +use rustc_infer::traits::util::elaborate; use rustc_infer::traits::{Obligation, ObligationCause}; +use rustc_middle::ty::fast_reject::{TreatParams, simplify_type}; use rustc_middle::ty::{ self, GenericArgKind, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, TypingEnv, }; -use rustc_span::{Span, sym}; +use rustc_span::{DUMMY_SP, Span, sym}; use rustc_trait_selection::infer::InferCtxtExt; +use rustc_trait_selection::traits::ObligationCtxt; use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _; use crate::ir::{ @@ -1849,7 +1852,8 @@ impl<'tcx> Collector<'tcx> { } /// The types a type parameter may be exported as, best first, which typing takes the - /// first of: `X` for `impl Into` or `impl AsRef`, then `String`, `Bytes`, and + /// first of: `X` for each `Into` or `AsRef` its bounds imply, or `&X` and then + /// `String` or `Vec` where `X` is `str` or `[T]`, then `String`, `Bytes`, and /// `Vec`, each that meets its bounds and maps. fn choose(&self, def_id: DefId, param: Ty<'tcx>, base: &[ty::GenericArg<'tcx>]) -> Result>, Why> { let tcx = self.tcx; @@ -1862,44 +1866,66 @@ impl<'tcx> Collector<'tcx> { .filter_map(|clause| clause.as_trait_clause()) .filter(|pred| pred.self_ty().skip_binder() == param) .collect(); + // With the bounds they imply: `IntoBytes: AsRef<[u8]>` makes a parameter it bounds bytes. + let implied: Vec<_> = elaborate(tcx, identity.clauses.iter().map(|clause| clause.skip_norm_wip())).collect(); // A parameter that must outlive the call cannot borrow what C lends for it. - let outlives = identity.clauses.iter().any(|clause| { + let outlives = implied.iter().any(|clause| { clause - .skip_norm_wip() .as_type_outlives_clause() .is_some_and(|pred| pred.skip_binder().0 == param) }); + let string = tcx + .lang_items() + .string() + .map(|string| Ty::new_adt(tcx, tcx.adt_def(string), ty::List::empty())); + let vec = |elem: Ty<'tcx>| { + let vec = tcx.get_diagnostic_item(sym::Vec)?; + let alloc = tcx.lang_items().global_alloc_ty()?; + let alloc = Ty::new_adt(tcx, tcx.adt_def(alloc), ty::List::empty()); + Some(Ty::new_adt( + tcx, + tcx.adt_def(vec), + tcx.mk_args(&[elem.into(), alloc.into()]), + )) + }; let mut candidates = Vec::new(); - for bound in &bounds { + let implied = (implied.iter()) + .filter_map(|clause| clause.as_trait_clause()) + .filter(|pred| pred.self_ty().skip_binder() == param); + for bound in implied { let tr = bound.def_id(); let target = || bound.skip_binder().trait_ref.args.type_at(1); if tcx.is_diagnostic_item(sym::Into, tr) || tcx.is_diagnostic_item(sym::AsRef, tr) { - let target = target(); - match target.kind() { - ty::Str | ty::Slice(_) if outlives => {} - ty::Str | ty::Slice(_) => candidates.push(Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, target)), - _ => candidates.push(target), + // An `Into` target that does not map takes what converts into it instead. + let into = (tcx.is_diagnostic_item(sym::Into, tr) && !self.maps(target(), Pos::Param)) + .then(|| self.sources(def_id, target())); + // A source that must outlive the call, or converts into a parameter that must, owns it. + let sources = into.into_iter().flatten().map(|(x, source)| (x, outlives || source)); + for (target, outlives) in std::iter::once((target(), outlives)).chain(sources) { + let owned = match target.kind() { + ty::Str => string, + ty::Slice(elem) => vec(*elem), + _ => { + candidates.push(target); + continue; + } + }; + // A borrow of what C lends, unless the parameter outlives the call, then the + // owned form, which meets bounds that only owned types implement. + if !outlives { + candidates.push(Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, target)); + } + candidates.extend(owned); } } else if Some(tr) == self.range_bounds { candidates.extend(self.bounds(target())); } } - if let Some(string) = tcx.lang_items().string() { - candidates.push(Ty::new_adt(tcx, tcx.adt_def(string), ty::List::empty())); - } + candidates.extend(string); if let Some(bytes) = self.bytes { candidates.push(Ty::new_adt(tcx, tcx.adt_def(bytes), ty::List::empty())); } - if let Some(vec) = tcx.get_diagnostic_item(sym::Vec) { - let alloc = tcx.lang_items().global_alloc_ty(); - if let Some(alloc) = alloc { - let args = tcx.mk_args(&[ - tcx.types.u8.into(), - Ty::new_adt(tcx, tcx.adt_def(alloc), ty::List::empty()).into(), - ]); - candidates.push(Ty::new_adt(tcx, tcx.adt_def(vec), args)); - } - } + candidates.extend(vec(tcx.types.u8)); // `impl Into>` takes a `String` too, which maps where `Arc` does not. candidates .retain(|&candidate| self.at(def_id, param, base, candidate).is_some() && self.maps(candidate, Pos::Param)); @@ -1924,6 +1950,64 @@ impl<'tcx> Collector<'tcx> { } } + /// What `impl Into` takes through the `From` implementations into `target` + /// whose source is a type parameter: each `Into` or `AsRef` its bounds imply, and + /// whether they make it outlive the call, so `impl> From for + /// Timed` makes `impl Into>` bytes. + fn sources(&self, def_id: DefId, target: Ty<'tcx>) -> Vec<(Ty<'tcx>, bool)> { + let tcx = self.tcx; + let Some(from) = tcx.get_diagnostic_item(sym::From) else { + return Vec::new(); + }; + let Some(simple) = simplify_type(tcx, target, TreatParams::AsRigid) else { + return Vec::new(); + }; + let mut found = Vec::new(); + for &imp in tcx + .trait_impls_of(from) + .non_blanket_impls() + .get(&simple) + .into_iter() + .flatten() + { + let (infcx, env) = tcx + .infer_ctxt() + .build_with_typing_env(TypingEnv::post_analysis(tcx, def_id)); + let args = infcx.fresh_args_for_item(DUMMY_SP, imp); + let trait_ref = tcx.impl_trait_ref(imp).instantiate(tcx, args).skip_norm_wip(); + let source = trait_ref.args.type_at(1); + let ocx = ObligationCtxt::new(&infcx); + if !source.is_ty_var() + || ocx + .eq(&ObligationCause::dummy(), env, trait_ref.self_ty(), target) + .is_err() + { + continue; + } + let source = infcx.deeply_resolve_ignoring_regions(source); + let clauses = tcx.clauses_of(imp).instantiate(tcx, args).clauses; + let implied: Vec<_> = elaborate(tcx, clauses.iter().map(|clause| clause.skip_norm_wip())).collect(); + let outlives = implied.iter().any(|clause| { + clause + .as_type_outlives_clause() + .is_some_and(|pred| infcx.deeply_resolve_ignoring_regions(pred.skip_binder().0) == source) + }); + for bound in (implied.iter()) + .filter_map(|clause| clause.as_trait_clause()) + .filter(|bound| { + tcx.is_diagnostic_item(sym::Into, bound.def_id()) + || tcx.is_diagnostic_item(sym::AsRef, bound.def_id()) + }) { + let bound = infcx.deeply_resolve_ignoring_regions(bound.skip_binder()); + let x = tcx.erase_and_anonymize_regions(bound.trait_ref.args.type_at(1)); + if bound.self_ty() == source && !x.has_non_region_infer() && !found.contains(&(x, outlives)) { + found.push((x, outlives)); + } + } + } + found + } + /// The arguments with `param` of `def_id` as `candidate`, if its bounds hold there. fn at( &self, diff --git a/ffi/tests/api.c b/ffi/tests/api.c index 6a87018d5..db0204dd7 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -299,6 +299,9 @@ static int script(void) { print("", api_greet(str("x"), API_SHAPE_SQUARE), "\n"); printf("len %zu\n", api_len(str("abc"))); + api_buf binary = {.ptr = (const uint8_t *)"\xff\0a", .len = 3}; + print("hex ", api_hex(binary), "\n"); + print("timed hex ", api_timed_hex(binary), "\n"); api_event events[3] = { {.tag = API_EVENT_MOVED, .moved = {._0 = p}}, {.tag = API_EVENT_NAMED, .named = {.name = str("n")}}, diff --git a/ffi/tests/api.cpp b/ffi/tests/api.cpp index 7f04c6159..c1d154d84 100644 --- a/ffi/tests/api.cpp +++ b/ffi/tests/api.cpp @@ -235,6 +235,9 @@ int script() { print("", api::greet("x", api::shape::square), "\n"); std::printf("len %zu\n", api::len("abc")); + const std::uint8_t binary[] = {0xff, 0x00, 'a'}; + print("hex ", api::hex(api::view(binary, sizeof binary)), "\n"); + print("timed hex ", api::timed_hex(api::view(binary, sizeof binary)), "\n"); api::event events[] = {api::event_moved{p}, api::event_named{api::buf("n")}, api::event_done{}}; for (const auto &event : events) { print("", api::describe(event), "\n"); diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index 4671d0a58..58e0c857e 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -133,6 +133,9 @@ fun script() { println(greet("x", Shape.SQUARE)) println("len ${len("abc".encodeToByteArray())}") + val binary = byteArrayOf(0xff.toByte(), 0, 'a'.code.toByte()) + println("hex ${hex(binary)}") + println("timed hex ${timedHex(binary)}") for (event in listOf(Event.Moved(p), Event.Named("n"), Event.Done)) { println(describe(event)) } diff --git a/quest/m1/ffi-export/README.md b/quest/m1/ffi-export/README.md index 689e7faf2..bc5856d34 100644 --- a/quest/m1/ffi-export/README.md +++ b/quest/m1/ffi-export/README.md @@ -47,7 +47,6 @@ Every generator on the C ABI: ## Required -- [Payloads cross as bytes](/quest/m1/ffi-export/payload-bytes.md) - a payload that is not UTF-8 crosses unchanged, where today it aborts from C, C++, and Kotlin - [Iterator and JSON arguments the exporter can't map yet](/quest/m1/ffi-export/instance-keys.md) - `IntoIterator` as a `Vec`, and `serde_json::Value` - [Returned iterators cross as arrays](/quest/m1/ffi-export/iterators.md) - `Hops::iter`, `Versions::iter`, and `Patterns::iter` export, returning arrays as `&[T]` does - [moq-clock's C names once it moves into kio](/quest/m1/ffi-export/crates.md) - `moq_now` and `moq_origin` become the clock's, after moq moves it into `kio::clock` diff --git a/quest/m1/ffi-export/payload-bytes.md b/quest/m1/ffi-export/payload-bytes.md deleted file mode 100644 index 110a522cb..000000000 --- a/quest/m1/ffi-export/payload-bytes.md +++ /dev/null @@ -1,24 +0,0 @@ -# [S] Payloads cross as bytes - -## Goal - -A parameter that takes bytes, such as moq's `impl Into` frame payloads -(`group::Producer::write_frame`), exports as bytes in the C ABI and every generator, so a -payload that is not valid UTF-8 crosses unchanged. Today it resolves to `String`: C and C++ -abort on such a payload, and Kotlin's `writeFrame` takes a `String`. - -## Plan - -Found by [#184](https://github.com/kixelated/rust2/pull/184) on 2026-10-28. This is a -correctness bug: moq's media payloads are binary. - -- Find why first. [`choose`](/ffi/src/collect.rs) puts `X` of `impl Into` ahead of - `String`, so the signature moq writes, or the bound typing reads, differs from what the - rule expects. -- Fix the rule, not moq's case: any parameter whose bounds `Bytes` or `Vec` meets but - whose meaning is bytes must not narrow to `String`. A `String` choice stays only where - the bound itself is about text. -- Prove it from C, C++, and Kotlin with a payload that is not UTF-8, and report the census - before and after. - -Public API: the C, C++, and Kotlin types of the affected parameters.