Unverified Commit 51c04a95 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

Assert transient task references are not serialized (#99300)

### What?
Assert in debug builds that transient `TaskId`s and packed `RawVc`
references are not serialized by bincode or serde.

### Why?
Transient tasks do not survive a restart. #99383 avoids serializing
suspended operations by waiting for them to settle; this encoding-time
guard catches transient references that reach any remaining
serialization path.

If we don't filter transient task references, then warm builds attempt
to recover missing tasks which is ambiguous with a GC bug.

### How?
Replace the derived bincode `Encode` and serde `Serialize`
implementations for both types with equivalent implementations that
validate transience before delegating to the same underlying fields.
Existing wire formats, deserialization, and release-mode behavior remain
unchanged.

Stacked on #99249, above #99383.

<!-- fleet e2b0931e-5002-47b5-b5e1-3a96fbc88cab -->

---------
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarLuke Sandberg <210140+lukesandberg@users.noreply.github.com>
parent 4868b992
......@@ -139,7 +139,7 @@ define_id!(
TaskId: u32,
// Capped below `u32::MAX` so the id fits in 31 bits when packed into `RawVc`.
max = TASK_ID_MAX,
derive(Serialize, Deserialize, Encode, Decode),
derive(Deserialize, Decode),
serde(transparent),
);
define_id!(
......@@ -163,6 +163,25 @@ define_id!(
leak. This value may overflow and re-use old values.",
);
// Preserve transparent serde and the derived NonZero<u32> bincode format, but reject references
// to transient tasks. RawVc packs TaskId into its own integer and checks that path separately.
impl Serialize for TaskId {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
debug_assert!(
!self.is_transient(),
"transient TaskId must not be serialized"
);
self.id.serialize(serializer)
}
}
impl Encode for TaskId {
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
debug_assert!(!self.is_transient(), "transient TaskId must not be encoded");
self.id.encode(encoder)
}
}
impl Debug for TaskId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TaskId").field("id", &self.id).finish()
......
......@@ -8,7 +8,7 @@ use std::{
};
use anyhow::Result;
use bincode::{Decode, Encode};
use bincode::{Decode, Encode, enc::Encoder, error::EncodeError};
use serde::{Deserialize, Serialize};
use crate::{
......@@ -143,9 +143,28 @@ impl Display for CellId {
/// ```
/// [`Vc`]: crate::Vc
/// [monomorphization]: https://doc.rust-lang.org/book/ch10-01-syntax.html#performance-of-code-using-generics
#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Encode, Decode)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, Deserialize, Decode)]
pub struct RawVc(NonZeroU64);
// RawVc serializes its packed integer directly, so TaskId's guards cannot check embedded task
// ids. Keep the original serde newtype shape and bincode format while rejecting transient values.
impl Serialize for RawVc {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
debug_assert!(
!self.is_transient(),
"transient RawVc must not be serialized"
);
serializer.serialize_newtype_struct("RawVc", &self.0)
}
}
impl Encode for RawVc {
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
debug_assert!(!self.is_transient(), "transient RawVc must not be encoded");
self.0.encode(encoder)
}
}
/// The unpacked form of [`RawVc`], produced by [`RawVc::unpack`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RawVcUnpacked {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment