Skip to content

Truly independent web browser

LibIPC: Let a message carry an argument that only Rust decodes

Aliaksandr Kalenik3f26e228d036
diff --git a/Libraries/LibIPC/Concepts.h b/Libraries/LibIPC/Concepts.h
index e51a7509898..389e93ccb33 100644
--- a/Libraries/LibIPC/Concepts.h
+++ b/Libraries/LibIPC/Concepts.h
@@ -13,6 +13,13 @@
#include <AK/Variant.h>
#include <AK/Vector.h>
+namespace IPC {
+
+template<typename T>
+class Encoded;
+
+}
+
// These concepts are used to help the compiler distinguish between specializations that would be
// ambiguous otherwise. For example, if the specializations for int and Vector<T> were declared as
// follows:
@@ -68,4 +75,7 @@ concept Variant = SpecializationOf<T, AK::Variant>;
template<typename T>
concept DistinctNumeric = SpecializationOf<T, AK::DistinctNumeric>;
+template<typename T>
+concept Encoded = SpecializationOf<T, IPC::Encoded>;
+
}
diff --git a/Libraries/LibIPC/Decoder.h b/Libraries/LibIPC/Decoder.h
index 20e9cc75502..5e699473fdc 100644
--- a/Libraries/LibIPC/Decoder.h
+++ b/Libraries/LibIPC/Decoder.h
@@ -272,6 +272,12 @@ ErrorOr<T> decode(Decoder& decoder)
return Detail::decode_variant<T>(decoder, index);
}
+template<Concepts::Encoded T>
+ErrorOr<T> decode(Decoder& decoder)
+{
+ return T::from_bytes(TRY(decoder.decode<ByteBuffer>()));
+}
+
// This must be last so that it knows about the above specializations.
template<typename T>
ErrorOr<T> Decoder::decode()
diff --git a/Libraries/LibIPC/Encoded.h b/Libraries/LibIPC/Encoded.h
new file mode 100644
index 00000000000..6a46e041ba6
--- /dev/null
+++ b/Libraries/LibIPC/Encoded.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/ByteBuffer.h>
+#include <AK/Span.h>
+#include <LibIPC/Decoder.h>
+#include <LibIPC/Encoder.h>
+#include <LibIPC/Message.h>
+
+namespace IPC {
+
+// A value of C++ that Rust takes: C++ encodes it and never decodes it. As an argument of a message that C++ receives,
+// it reaches the handler still encoded, for the handler to hand to Rust as its bytes, which are a
+// `libipc_rust::cpp_connection::EncodedValue` there.
+template<typename T>
+class Encoded {
+public:
+ using ValueType = T;
+
+ Encoded() = default;
+
+ explicit Encoded(T const& value)
+ {
+ MessageBuffer buffer;
+ Encoder encoder { buffer };
+ MUST(encoder.encode(value));
+ VERIFY(buffer.attachments().is_empty());
+ m_bytes = MUST(ByteBuffer::copy(buffer.data().span()));
+ }
+
+ static Encoded from_bytes(ByteBuffer bytes)
+ {
+ Encoded encoded;
+ encoded.m_bytes = move(bytes);
+ return encoded;
+ }
+
+ ReadonlyBytes bytes() const { return m_bytes.bytes(); }
+
+private:
+ ByteBuffer m_bytes;
+};
+
+}
diff --git a/Libraries/LibIPC/Encoder.h b/Libraries/LibIPC/Encoder.h
index 7b745083645..3e9573a0874 100644
--- a/Libraries/LibIPC/Encoder.h
+++ b/Libraries/LibIPC/Encoder.h
@@ -222,6 +222,13 @@ ErrorOr<void> encode(Encoder& encoder, T const& variant)
});
}
+template<Concepts::Encoded T>
+ErrorOr<void> encode(Encoder& encoder, T const& value)
+{
+ TRY(encoder.encode_size(value.bytes().size()));
+ return encoder.append(value.bytes().data(), value.bytes().size());
+}
+
// This must be last so that it knows about the above specializations.
template<typename T>
ErrorOr<void> Encoder::encode(T const& value)
diff --git a/Libraries/LibIPC/Rust/src/cpp_connection.rs b/Libraries/LibIPC/Rust/src/cpp_connection.rs
index 3c793221ba0..e00adf89a5f 100644
--- a/Libraries/LibIPC/Rust/src/cpp_connection.rs
+++ b/Libraries/LibIPC/Rust/src/cpp_connection.rs
@@ -12,7 +12,9 @@
use core::ffi::{c_int, c_void};
use crate::attachment::Attachment;
-use crate::wire::{DispatchError, EncodeError, EncodedMessage, EndpointMessage, Misbehavior};
+use crate::wire::{
+ Decode, DecodeError, Decoder, DispatchError, EncodeError, EncodedMessage, EndpointMessage, Misbehavior,
+};
/// A Mach port name and its disposition on Apple, and a descriptor elsewhere: `IPC::RawAttachment`.
#[repr(C)]
@@ -143,6 +145,21 @@ impl RawMessage<'_> {
}
}
+/// A value as C++ encoded it for Rust to decode, lent for a call: the bytes of an `IPC::Encoded<T>`.
+#[repr(transparent)]
+pub struct EncodedValue<'a>(ak::Span<'a, u8>);
+
+impl EncodedValue<'_> {
+ pub fn decode<T: Decode>(&self) -> Result<T, DecodeError> {
+ let mut decoder = Decoder::new(self.0.as_slice(), []);
+ let value = decoder.decode()?;
+ if !decoder.is_at_end() {
+ return Err(DecodeError::InvalidValue("bytes after an encoded value"));
+ }
+ Ok(value)
+ }
+}
+
/// One reference to an `IPC::ThreadSafeMessagePoster`, and how to reach it: `IPC::FfiThreadSafeMessagePoster`.
#[repr(C)]
#[derive(Clone, Copy)]