Skip to content

CRC of objects with references is not comparable across separate databases #74

Open

Description

@SkowronskiAndrew

Summary

objects.crc32 is meant to be a content fingerprint, and the comparing-builds workflow expects you can analyze two builds into two separate databases and diff CRCs to find which objects changed. That works for leaf assets (Texture2D/Mesh/AudioClip — no references), but it is broken for any object that contains references (Materials, prefabs/GameObjects, MonoBehaviours, etc.): identical content produces different CRCs in two separate analyze runs.

Cause

When PPtrAndCrcProcessor.ExtractPPtr folds a reference into the CRC, it uses the resolved analyzer/database object id returned by the callback, not the PPtr's own identity:

var refId = m_Callback(m_ObjectId, fileId, pathId, ...);   // analyzer db id
m_Crc32 = Crc32Algorithm.Append(m_Crc32, <refId bytes>);

That id comes from ObjectIdProvider.GetId((m_LocalToDbFileId[fileId], pathId)), and both the serialized-file id and the object id are assigned sequentially per analyze run. So the same logical object gets different ids in db1 vs db2 → different CRC for identical content → cross-database comparison reports spurious differences for every object that has references.

Why we can't just hash the raw PPtr (the tradeoff)

The obvious fix is to hash the raw on-disk PPtr (fileId + pathId) instead of the resolved id. But the resolved id is currently what makes within-database duplicate detection (view_potential_duplicates) work across bundles: two copies of the same object in different bundles reference the same target, and resolving through m_LocalToDbFileId (keyed by filename) + pathId normalizes them to the same id → same CRC → detected as duplicates.

fileId is a local index into a serialized file's external-reference list, so two copies of an object in different bundles can have different fileId values for the same target. Hashing the raw PPtr would therefore weaken duplicate detection. Deduplication is an important feature and is probably not well covered by tests yet, so we don't want to risk regressing it.

Options to evaluate

  1. Raw PPtr (fileId + pathId) — simplest; fixes cross-db comparison in the common case; risks weakening view_potential_duplicates (local fileId differs between bundles).
  2. Stable target identity + pathId — resolve fileId to a stable identifier for the target file and hash that + pathId, so it is independent of the local index. This fixes cross-db comparison AND preserves cross-bundle duplicate detection, but the "stable identifier" differs by source:
    • Build output external references carry a path (e.g. archive:/CAB-...), not a GUID.
    • Editor / Library references carry a GUID (the source asset's GUID).
      So the CRC needs to mix in whichever of ExternalReference.Path / ExternalReference.Guid is populated (and a fixed marker for local refs, fileId == 0). Relies on those fields being present and stable.
      More code: thread the external-reference info from sf.ExternalReferences into the CRC.
  3. Status quo — cross-db comparison stays broken for referenced objects.

Prerequisite

Add test coverage for view_potential_duplicates / cross-bundle deduplication before changing the CRC, so a fix can be validated to not regress it.

Context

Discovered while reviewing #73 / #70. Note that this is independent of the CRC changes made there (the ManagedReferenceData size fix, the ComputeCRC chunking fix, and the cah:/ stream hashing) — those also change CRC values vs. older tool versions, so CRCs are not comparable across tool versions regardless.

Related: #44 (refs table).

Activity

  1. added a commit that references this issue on Jun 12, 2026
  2. SkowronskiAndrew commented on Sep 16, 2026

    @SkowronskiAndrew
    CollaboratorAuthor

    AGENT: Some findings from the Unity 6.7 work in #145 that bear on this, plus a fourth option that came up while discussing it.

    What the CRC actually covers today

    Worth writing down precisely, because "content fingerprint" suggests something simpler than what PPtrAndCrcProcessor computes. It is not a hash of the object's bytes. Walking the TypeTree, it appends:

    • basic types — their bytes (AppendCrc(m_Offset, node.Size))
    • strings — the 4-byte length plus the characters
    • StreamingInfo / StreamedResource — the referenced stream's bytes, read from the companion .resS/.resource, or the cah:/<hash> path itself for content-addressed streams
    • PPtrs — not their bytes, but the 4-byte resolved analyzer object id, which is the subject of this issue

    and it never appends:

    • alignment padding. Where a node is flagged to align, the walk does m_Offset = (m_Offset + 3) & ~3 with no AppendCrc, so those bytes are skipped. Normally zeros, but nothing guarantees it.
    • any byte the TypeTree does not describe.

    So two objects can have equal CRCs while differing in bytes, and vice versa. That matters for the options below.

    What #145 changed

    From SerializedFile v25 the [SerializeReference] registry is a self-delimiting frame in the object's data that no TypeTree node describes. #145 CRCs the frame's header and tables as the raw bytes they are, then walks each instance's blob through its own TypeTree with the usual PPtr substitution.

    Two consequences:

    1. The v1/v2 and v3 registry paths hash slightly different things. In v1/v2, ProcessManagedReferenceData appends each of the three FQN strings as length + characters and skips the alignment padding after each. In v3 the whole header-and-tables range goes in as one contiguous span, padding included. Both are "the registry's raw bytes", but not byte-identical treatment.
    2. A v23 and a v26 build of the same content cannot have equal CRCs, whatever we do here — the registry moves to a different place in the object, is laid out differently, and has a different byte count. Andrew confirmed on the PR that this is acceptable and not a goal, so it is not an argument for or against any option; it just means cross-Unity-version comparison was never on the table.

    Neither changes the analysis in the issue body. The PPtr substitution is untouched, so cross-database comparison is broken exactly as before.

    Option 4: CRC the object's raw bytes

    Raised by Andrew on #145: instead of deriving a CRC from the walk, hash the object's byte range directly (obj.Offset .. obj.Offset + obj.Size), plus any external stream content it references.

    Arguments for:

    • It is genuinely simpler, and it is the thing people assume crc32 already is.
    • It has no dependency on analyzer ids, so it fixes cross-database comparison outright — the motivating problem here.
    • It is robust to how we walk. Today a bug in the walk (a mis-stepped registry frame, a shared subtree read as a primitive — both real failure modes in 6.7) silently changes CRCs. A raw-byte CRC cannot be wrong about layout, because it does not have a view of layout.
    • It covers bytes the walk currently skips, including alignment padding.

    Arguments against, and one correction to an assumption:

    • It does not save the walk. The walk still has to happen for reference extraction, so this is not a performance win — it changes what is hashed, not how much work is done.
    • It has option 1's weakness on duplicate detection, in full. Raw bytes include the PPtr's raw m_FileID, which is a local index into that serialized file's external-reference list. Two copies of the same object in different bundles can carry different fileId values for the same target, so their raw bytes differ and view_potential_duplicates stops seeing them as duplicates. This is the same tradeoff the issue already identifies for option 1, so the prerequisite (test coverage for dedup first) applies equally.
    • It would also fold in any byte Unity leaves non-deterministic inside an object, if there is any. I have not looked for cases.

    So option 4 is best read as option 1 plus padding, minus the walk-derived-ness — the same duplicate-detection risk, with extra robustness and simplicity in exchange. It is not a way around the tradeoff.

    A wrinkle for option 2

    Option 2 wants a stable identity for the target of a reference. Note that from v26 the registry frame carries the fully qualified type name (class / namespace / assembly) of every [SerializeReference] instance inline, in its Types table, and those bytes are already in the CRC. That is a stable, build-independent identity, so for registry-held instances the "stable identifier" problem is already solved by the format — the open question is only about PPtrs to other serialized objects.

    Suggestion

    The prerequisite in the issue body still looks like the right first move, and #145 does not change it: add coverage for view_potential_duplicates / cross-bundle deduplication before touching the CRC. Until that exists, options 1, 2 and 4 all carry the same unmeasured risk, and there is no way to tell which of them actually costs us something.

    One more thing worth deciding explicitly when this is picked up: whether crc32 should be one column trying to serve both "did this object change between builds" and "are these two objects the same content", since those two questions pull in opposite directions on exactly the PPtr treatment. Two columns would let each be answered correctly.

  3. SkowronskiAndrew commented on Sep 29, 2026

    @SkowronskiAndrew
    CollaboratorAuthor

    This ticket has a lot of AI mumble jumble on it. Its still not clear to me if there truly is a bug or not "in practice" when comparing builds.
    And it seems the actual meaning of the CRC value is part of the confusion.
    So it will be worth investigating this so that we can better reenforce our support for comparing builds and quickly narrowing down which objects have changed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions