Skip to content

ghostscript: honor SOURCE_DATE_EPOCH=0 for reproducible build - #272

Merged
bryan-minimal merged 2 commits into
mainfrom
bryan/ghostscript-reproducible
Jun 22, 2026
Merged

ghostscript: honor SOURCE_DATE_EPOCH=0 for reproducible build#272
bryan-minimal merged 2 commits into
mainfrom
bryan/ghostscript-reproducible

Conversation

@bryan-minimal

@bryan-minimal bryan-minimal commented Jun 18, 2026

Copy link
Copy Markdown
Member

What

Makes the ghostscript package reproducible by fixing a SOURCE_DATE_EPOCH=0 handling bug in ghostscript's own build tools.

Why

Two from-scratch builds of ghostscript differed in exactly one place: an 8-byte value in usr/bin/gs — the gs_romfs_buildtime global (confirmed via the symbol table), a Unix build timestamp baked into the embedded ROM filesystem.

ghostscript's mkromfs and pack_ps tools try to honor SOURCE_DATE_EPOCH, but the logic is buggy:

if ((env_source_date_epoch = getenv("SOURCE_DATE_EPOCH"))) {
    buildtime = strtoul(env_source_date_epoch, NULL, 10);   // SDE="0" -> buildtime = 0
}
if (!buildtime)                 // 0 is falsy -> treats epoch 0 as "unset"
    buildtime = time(NULL);     // ...and falls back to wall-clock time

The build sandbox sets SOURCE_DATE_EPOCH=0 — which is the canonical reproducible-builds value, and exactly the value ghostscript mishandles: it reads 0, then if (!buildtime) treats it as unset and stamps wall-clock time() into the binary. (base/mkromfs.c:2614, base/pack_ps.c:344.)

Fix

Fall back to time() only when SOURCE_DATE_EPOCH is genuinely unset (!env_source_date_epoch), not when it parses to 0:

sed -i 's/if (!buildtime)/if (!env_source_date_epoch)/' base/mkromfs.c base/pack_ps.c

This 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/gs diverged on the embedded gs_romfs_buildtime.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Chores
    • Improved build reproducibility by refining how build timestamps are handled when using the SOURCE_DATE_EPOCH environment variable.
    • Added safeguards to ensure the expected reproducibility-related logic is present, failing the build if it changes.

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>
@coderabbitai

coderabbitai Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 175cb223-498d-414c-b8a4-2d5b0f84b1ef

📥 Commits

Reviewing files that changed from the base of the PR and between d515926 and ed17a06.

📒 Files selected for processing (1)
  • packages/ghostscript/build.sh
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/ghostscript/build.sh

📝 Walkthrough

Walkthrough

The Ghostscript build script gains a sed-based inline patch applied to base/mkromfs.c and base/pack_ps.c. It replaces the if (!buildtime) fallback condition with a check against env_source_date_epoch, so that a SOURCE_DATE_EPOCH value of 0 (epoch) no longer causes a nondeterministic wall-clock timestamp to be embedded.

Changes

Ghostscript Build Reproducibility Fix

Layer / File(s) Summary
sed patch for buildtime guard
packages/ghostscript/build.sh
Adds a comment and a sed -i invocation that rewrites the if (!buildtime) conditional in base/mkromfs.c and base/pack_ps.c to use env_source_date_epoch, fixing the zero-epoch edge case.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Suggested reviewers

  • twitchyliquid64

Poem

A rabbit checked the clock one day,
"Epoch zero? That's still okay!"
With a sed and a patch so neat,
The build timestamp stays concrete.
🐇 No more random ticks — hooray!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and concisely describes the main change: fixing ghostscript to properly handle SOURCE_DATE_EPOCH=0 for reproducible builds.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bryan/ghostscript-reproducible

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between 087287e and d515926.

📒 Files selected for processing (1)
  • packages/ghostscript/build.sh

Comment on lines +16 to +22
# 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

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 do

As 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.

Suggested change
# 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

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>
@bryan-minimal
bryan-minimal added this pull request to the merge queue Jun 22, 2026
Merged via the queue into main with commit 5c8b5b7 Jun 22, 2026
4 checks passed
@bryan-minimal
bryan-minimal deleted the bryan/ghostscript-reproducible branch June 22, 2026 17:00
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.

2 participants