Skip to content

backport .rs.digest move to Tools.R and adler32 rewrite - #17799

Merged
gtritchie merged 1 commit into
rel-golden-wattlefrom
bugfix/backport-rs-digest-adler32
Jun 2, 2026
Merged

gtritchie merged 1 commit into
rel-golden-wattlefrom
bugfix/backport-rs-digest-adler32

Conversation

@kevinushey

Copy link
Copy Markdown
Collaborator

Intent

Addresses #17777. Backports the minimal .rs.digest changes from #17789 to rel-golden-wattle.

preview_data_import_async (in SessionData.cpp) spawns a --vanilla R subprocess to run the preview off the main session. That subprocess sources only Tools.R plus a handful of session modules and has no RStudio embedding registered. The SHA-256-backed .rs.digest added in #17595 lived in SessionRUtil.R (not sourced there) and called into rs_sha256 via the embedding (also not available), so any CSV/readr/readxl/haven preview failed with:

Is this a valid CSV file? could not find function ".rs.digest"

The data viewer fingerprint just needs a stable, bounded identity token for collision detection, not a cryptographic hash. This change:

  • Moves .rs.digest from SessionRUtil.R to Tools.R (already sourced by the subprocess).
  • Reimplements it as a vectorized Adler-32 in pure base R. Processed in 1024-byte chunks so every intermediate sum stays within R's signed 32-bit integer range; the result is exact and identical across platforms regardless of input size.
  • Pins serialize(version = 2L) and zeros out bytes 7-10 (the writer's R version field) so the hash is stable across R upgrades. v2 chosen over v3 because v3 additionally embeds the native encoding name in the header, which can vary by platform.
  • Drops the now-unused rs_sha256 C++ helper and its Crypto.hpp include.

.rs.digest keeps the same API (raw or arbitrary R value in, fixed-size hex string out), so existing callers work unchanged.

Scope

This is the minimal .rs.digest-focused subset of #17789. The adjacent cleanups bundled into that PR (the rs_fromJSON PACKAGE qualifier, the dead INTEGER_OR_NULL dlopen removal, the rstudio-tests TTY auto-wrap, the Playwright test hardening, and the new E2E test) are intentionally not included here.

Upgrade note

The fingerprint format changes from n:sha256 (#17595) to n:adler32. Any saved per-object data-viewer state (pin/width/sort/filter) carrying the old anchor will mismatch on first launch and get discarded -- a one-time UI-state reset, no data loss. Since #17595 is still in the open Golden Wattle milestone, no released build is affected.

The async data-preview subprocess (R_PROCESS_VANILLA, launched from
SessionData.cpp) sources only Tools.R plus a handful of session modules
and has no RStudio embedding registered. The SHA-256-backed .rs.digest
added in #17595 lived in SessionRUtil.R (not sourced in the subprocess)
and called into rs_sha256 via the embedding (not available there
either), so the column-names fingerprint failed with "could not find
function '.rs.digest'" on any Import Dataset preview.

The fingerprint just needs a stable identity token for collision
detection, not a cryptographic hash, so move .rs.digest to Tools.R and
implement it as a vectorized Adler-32 in pure base R. The chunked int32
closed form keeps all intermediate sums within R's signed 32-bit range,
and pinning serialize(version = 2L) with the writer-version field zeroed
keeps the hash stable across R upgrades.

Drops the now-unused rs_sha256 C++ helper and its Crypto.hpp include.

Addresses #17777.
@kevinushey kevinushey added this to the Golden Wattle milestone May 29, 2026
@posit-snyk-bot

posit-snyk-bot commented May 29, 2026 •

Copy link
Copy Markdown
Contributor

✅ Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
✅ Open Source Security 0 0 0 0 0 issues
✅ Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@kevinushey
kevinushey requested a review from gtritchie May 29, 2026 17:14
@gtritchie

Copy link
Copy Markdown
Member

Code Review

The core change is correct and clean. I validated the new Adler-32 against a byte-at-a-time reference in R 4.6.0 -- canonical vectors ("abc" -> 024d0127, empty -> 00000001), all chunk-size boundaries (1023/1024/1025/2048), and the worst-case all-0xFF overflow scenario all match. The serialize-v2 stability claim checks out: bytes 7-10 are the writer R version (zeroed), bytes 11-14 are the format-constant min-reader version (stable across upgrades). The rs_sha256 removal is properly scoped -- no orphaned references, and sha256Hex remains in use by ChatIntegrity.cpp.

Two findings, both cases where the backport is narrower than #17789 on main.

Important

1. The riskiest new code ships to a release branch untested. The chunked-integer Adler-32 reformulation (the k*a carry between chunks, the descending seq.int(k, 1L) weights, the boundary math) is bespoke logic. The base branch's existing .rs.digest() tests are algorithm-agnostic -- they only assert distinctness and determinism, which pass for any framing-aware deterministic hash, including a subtly wrong one. main (#17789) carries three correctness-pinning tests that close exactly this gap: a canonical reference value, a chunked-vs-recursive cross-check across the 1024-byte boundary, and a test for the v2 writer-version zeroing. None are backported here. A transcription bug in the chunk arithmetic would produce a stable, well-distributed, wrong hash that passes every test on this branch.

Recommend backporting those three test blocks into src/cpp/tests/testthat/test-dataviewer.R -- additive, low-risk, and it touches a file this PR otherwise doesn't. Run the rsession scope afterward to confirm the serialize-routed 1732015b value matches on this branch's R.

2. Dead is.na(hash) branch and stale comment left in the caller. SessionDataViewer.R (.rs.dataViewer.colsFingerprint, lines ~144-156) still reads:

hash <- .rs.digest(nms)
if (is.na(hash))
   return(NA_character_)

and its doc comment still claims a return path "when the digest helper itself fails." The new Adler-32 .rs.digest always returns a non-NA hex string, so that failure mode no longer exists. #17789 already cleaned this up on main (it does paste0(n, ":", .rs.digest(nms)) directly); this backport left it in place. The dead branch plus the misleading comment will lead a future reader to believe the digest can fail. The empty/NULL-names n == 0L path is unaffected and still correct.

Recommended action

  1. Backport the three Adler-32 test blocks from main into test-dataviewer.R (item 1) -- the highest-value fix, since it guards the novel code on a release branch.
  2. Remove the dead is.na(hash) branch and fix the stale comment in SessionDataViewer.R (item 2) to match what fix import dataset preview by removing embedding dependency from .rs.digest #17789 already did on main.

Both items stem from the backport being narrower than its source PR; addressing them brings rel-golden-wattle in line with the already-merged main change. The production code itself is sound as-is.

@gtritchie gtritchie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@gtritchie
gtritchie merged commit 25eaf33 into rel-golden-wattle Jun 2, 2026
8 checks passed
@gtritchie
gtritchie deleted the bugfix/backport-rs-digest-adler32 branch June 2, 2026 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants