ghostscript: honor SOURCE_DATE_EPOCH=0 for reproducible build - #272
Conversation
mkromfs/pack_ps read SOURCE_DATE_EPOCH but then do `if (!buildtime) buildtime = time(NULL)`, treating the sandbox's SOURCE_DATE_EPOCH=0 (epoch 0 is falsy) as unset and falling back to wall-clock time. That stamped a non-deterministic gs_romfs_buildtime into usr/bin/gs. Fall back to time() only when SOURCE_DATE_EPOCH is genuinely unset (base/mkromfs.c:2614, base/pack_ps.c:344). Verified on aarch64: two from-scratch builds are byte-identical (repro-check diff, 196/196 files). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThe Ghostscript build script gains a ChangesGhostscript Build Reproducibility Fix
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/ghostscript/build.sh`:
- Around line 16-22: The reproducibility fix depends on SOURCE_DATE_EPOCH=0
being set by the caller, but if it is not explicitly set in the script, the
fallback to wall-clock time in mkromfs and pack_ps will cause non-deterministic
behavior. Add explicit export statements at the beginning of the build.sh script
to set both SOURCE_DATE_EPOCH=0 and PYTHONHASHSEED=0 before the sed command that
modifies base/mkromfs.c and base/pack_ps.c. This ensures deterministic behavior
is guaranteed regardless of the caller's environment.
- Line 21: The sed substitution on line 21 can silently fail to apply if the
Ghostscript source changes upstream, causing reproducibility to regress without
a build error. Add a guard check before the sed command to verify that the
pattern "if (!buildtime)" exists in both base/mkromfs.c and base/pack_ps.c files
using grep, and add a verification check after the sed command to confirm the
substitution was successful by checking that "if (!env_source_date_epoch)" now
exists in both files. If either guard check fails, the script should exit with
an error to prevent silent reproducibility failures.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: d69ac19d-3d0f-430d-8ffd-5fc796b600ff
📒 Files selected for processing (1)
packages/ghostscript/build.sh
| # Reproducibility: mkromfs/pack_ps read SOURCE_DATE_EPOCH but then do | ||
| # `if (!buildtime) buildtime = time(NULL)`, which treats the sandbox's | ||
| # SOURCE_DATE_EPOCH=0 (epoch 0 is falsy) as "unset" and falls back to wall-clock | ||
| # time -> non-deterministic gs_romfs_buildtime baked into the gs binary. Only | ||
| # fall back to time() when SOURCE_DATE_EPOCH is genuinely unset. | ||
| sed -i 's/if (!buildtime)/if (!env_source_date_epoch)/' base/mkromfs.c base/pack_ps.c | ||
|
|
There was a problem hiding this comment.
Export reproducibility env vars in-script to guarantee deterministic behavior.
This fix depends on SOURCE_DATE_EPOCH=0; if it is not set by the caller, the code still falls back to wall-clock time. Set reproducibility env vars explicitly in this script.
Suggested addition
export LDFLAGS="-Wl,--build-id=none"
export CXXFLAGS="${CFLAGS}"
+export SOURCE_DATE_EPOCH=0
+export PYTHONHASHSEED=0
# Reproducibility: mkromfs/pack_ps read SOURCE_DATE_EPOCH but then doAs per coding guidelines, for embedded timestamps and Python-based build steps in packages/*/build.sh, export SOURCE_DATE_EPOCH=0 and PYTHONHASHSEED=0.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| # Reproducibility: mkromfs/pack_ps read SOURCE_DATE_EPOCH but then do | |
| # `if (!buildtime) buildtime = time(NULL)`, which treats the sandbox's | |
| # SOURCE_DATE_EPOCH=0 (epoch 0 is falsy) as "unset" and falls back to wall-clock | |
| # time -> non-deterministic gs_romfs_buildtime baked into the gs binary. Only | |
| # fall back to time() when SOURCE_DATE_EPOCH is genuinely unset. | |
| sed -i 's/if (!buildtime)/if (!env_source_date_epoch)/' base/mkromfs.c base/pack_ps.c | |
| export LDFLAGS="-Wl,--build-id=none" | |
| export CXXFLAGS="${CFLAGS}" | |
| export SOURCE_DATE_EPOCH=0 | |
| export PYTHONHASHSEED=0 | |
| # Reproducibility: mkromfs/pack_ps read SOURCE_DATE_EPOCH but then do | |
| # `if (!buildtime) buildtime = time(NULL)`, which treats the sandbox's | |
| # SOURCE_DATE_EPOCH=0 (epoch 0 is falsy) as "unset" and falls back to wall-clock | |
| # time -> non-deterministic gs_romfs_buildtime baked into the gs binary. Only | |
| # fall back to time() when SOURCE_DATE_EPOCH is genuinely unset. | |
| sed -i 's/if (!buildtime)/if (!env_source_date_epoch)/' base/mkromfs.c base/pack_ps.c |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@packages/ghostscript/build.sh` around lines 16 - 22, The reproducibility fix
depends on SOURCE_DATE_EPOCH=0 being set by the caller, but if it is not
explicitly set in the script, the fallback to wall-clock time in mkromfs and
pack_ps will cause non-deterministic behavior. Add explicit export statements at
the beginning of the build.sh script to set both SOURCE_DATE_EPOCH=0 and
PYTHONHASHSEED=0 before the sed command that modifies base/mkromfs.c and
base/pack_ps.c. This ensures deterministic behavior is guaranteed regardless of
the caller's environment.
Source: Coding guidelines
| # SOURCE_DATE_EPOCH=0 (epoch 0 is falsy) as "unset" and falls back to wall-clock | ||
| # time -> non-deterministic gs_romfs_buildtime baked into the gs binary. Only | ||
| # fall back to time() when SOURCE_DATE_EPOCH is genuinely unset. | ||
| sed -i 's/if (!buildtime)/if (!env_source_date_epoch)/' base/mkromfs.c base/pack_ps.c |
There was a problem hiding this comment.
Make the inline sed patch fail-fast on upstream drift.
Line 21 can silently no-op if Ghostscript source changes, and reproducibility regresses without a build failure. Add guard checks before/after substitution.
Suggested hardening
+# Fail fast if upstream changes and the patch no longer applies.
+grep -q 'if (!buildtime)' base/mkromfs.c
+grep -q 'if (!buildtime)' base/pack_ps.c
sed -i 's/if (!buildtime)/if (!env_source_date_epoch)/' base/mkromfs.c base/pack_ps.c
+! grep -q 'if (!buildtime)' base/mkromfs.c
+! grep -q 'if (!buildtime)' base/pack_ps.c
+grep -q 'if (!env_source_date_epoch)' base/mkromfs.c
+grep -q 'if (!env_source_date_epoch)' base/pack_ps.c🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@packages/ghostscript/build.sh` at line 21, The sed substitution on line 21
can silently fail to apply if the Ghostscript source changes upstream, causing
reproducibility to regress without a build error. Add a guard check before the
sed command to verify that the pattern "if (!buildtime)" exists in both
base/mkromfs.c and base/pack_ps.c files using grep, and add a verification check
after the sed command to confirm the substitution was successful by checking
that "if (!env_source_date_epoch)" now exists in both files. If either guard
check fails, the script should exit with an error to prevent silent
reproducibility failures.
…hanges) Adopts the hardening idea from the Edge Delta bot's #274 (cleanly — no whole-file reindentation, executable bit preserved): verify the 'if (!buildtime)' pattern exists in both files before sed-ing, so a future ghostscript that renames it fails the build instead of silently shipping a non-reproducible binary. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
What
Makes the
ghostscriptpackage reproducible by fixing aSOURCE_DATE_EPOCH=0handling bug in ghostscript's own build tools.Why
Two from-scratch builds of
ghostscriptdiffered in exactly one place: an 8-byte value inusr/bin/gs— thegs_romfs_buildtimeglobal (confirmed via the symbol table), a Unix build timestamp baked into the embedded ROM filesystem.ghostscript's
mkromfsandpack_pstools try to honorSOURCE_DATE_EPOCH, but the logic is buggy:The build sandbox sets
SOURCE_DATE_EPOCH=0— which is the canonical reproducible-builds value, and exactly the value ghostscript mishandles: it reads0, thenif (!buildtime)treats it as unset and stamps wall-clocktime()into the binary. (base/mkromfs.c:2614,base/pack_ps.c:344.)Fix
Fall back to
time()only whenSOURCE_DATE_EPOCHis genuinely unset (!env_source_date_epoch), not when it parses to0:sed -i 's/if (!buildtime)/if (!env_source_date_epoch)/' base/mkromfs.c base/pack_ps.cThis is upstream-worthy — the bug bites any builder that sets
SOURCE_DATE_EPOCH=0.Verification
Build-twice-and-diff on aarch64 (
--rebuild --no-fetch): with the fix, two from-scratch builds are byte-for-byte identical (repro-check diff, 196/196 files). Before the fix,usr/bin/gsdiverged on the embeddedgs_romfs_buildtime.🤖 Generated with Claude Code
Summary by CodeRabbit
SOURCE_DATE_EPOCHenvironment variable.