chipyard icon indicating copy to clipboard operation
chipyard copied to clipboard

build-setup.sh pins sysroot_linux-64 to the host glibc without checking it exists

Open cwssemi opened this issue 3 months ago • 7 comments

Background Work

Chipyard Version and Hash

Release: main and 1.14 Hash: e602d917dcc495c58cabe906535e411707096c9c

OS Setup

Linux ws020 6.8.0-134-generic #134~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue Jun 30 14:05:04 UTC x86_64 x86_64 x86_64 GNU/Linux

Distributor ID: Ubuntu Description: Ubuntu 22.04.5 LTS Release: 22.04 Codename: jammy

Other Setup

Followed documented setup from 1.13 which was OK, but 1.14 and main failed

Current Behavior

Failed to complete build-setup.sh successfuly

Expected Behavior

Complete build-setup.sh successfuly

Other Information

Severity: blocks initial repository setup entirely on any host whose glibc is not one of the six versions conda-forge packages a sysroot for. Compounded by a destructive failure mode: the committed lockfile is removed before its replacement is known to solve, leaving the tree worse than before the script ran.

Verified against: main e602d917dcc495c58cabe906535e411707096c9c — the tip as of 2026-08-26 — and tag 1.14.0 (0acc1e1). Reproduced on Ubuntu 22.04, glibc 2.35, conda-lock 2.5.7.

Affected versions: 1.14.0 and later, introduced by ucb-bar/chipyard#2271 (merged 2026-03-08, bcb6129). 1.13.0 has no glibc adaptation at all and pins sysroot_linux-64=2.17 unconditionally.

Summary

scripts/build-setup.sh adapts the conda sysroot_linux-64 pin to the machine it runs on. It reads the host glibc and, if it differs from the pinned value, writes the host's version string into the pin verbatim and regenerates the conda lockfiles from it:

# scripts/build-setup.sh:188-195 (unchanged from 1.14.0 to main e602d91)
SYS_GLIBC=$(ldd --version | awk '/ldd/{print $NF}')
DEFAULT_GLIBC=$(grep -i "sysroot_linux-64=" conda-reqs/chipyard-base.yaml | awk -F= '{print $2}')
if [ "$SYS_GLIBC" != "$DEFAULT_GLIBC" ]; then
    # replace the glibc version
    sed -i.bak "s/^\([[:space:]]*-\s*sysroot_linux-64=\).*/\1$SYS_GLIBC/" conda-reqs/chipyard-base.yaml
    $CYDIR/scripts/generate-conda-lockfiles.sh
    exit_if_last_command_failed
fi

Nothing checks that a sysroot_linux-64 package exists at that version. conda-forge publishes only 2.12, 2.12.2, 2.17, 2.28, 2.34, 2.39 (api.anaconda.org, checked 2026-08-26).

On a host with glibc 2.35 — Ubuntu 22.04's — the script pins sysroot_linux-64=2.35, which cannot be satisfied, and the solve fails:

Could not lock the environment for platform linux-64
The following packages are not available from current channels:

  - sysroot_linux-64=2.35
...
build-setup.sh: Build script failed with exit code 1 at step 1: Conda environment setup

The guard never guards: the comparison mis-parses its own pinned line

DEFAULT_GLIBC is extracted with awk -F= '{print $2}', which returns everything after the first = — including the trailing comment on that line:

committed line:  - sysroot_linux-64=2.34 # need to be close to system glibc for VCS compatibility
DEFAULT_GLIBC:  [2.34 # need to be close to system glibc for VCS compatibility]
SYS_GLIBC:      [2.35]

SYS_GLIBC is a bare version, so the two can never be equal on any host — including one whose glibc is exactly the pinned 2.34. The mismatch branch is therefore unconditional on every fresh clone: the pin is always rewritten, the committed lockfile is always deleted, and the environment is always re-solved from scratch.

Two consequences:

  • The committed lockfiles are dead on arrival. They are never what a fresh clone installs, which defeats the purpose of committing them — independent of the availability bug.
  • The availability failure is not an unlucky-host edge case. Every fresh setup takes the regeneration path and merely survives where the host glibc happens to be a published sysroot version. The bug is universal; only its symptom is host-dependent.

Why this one line and no other comment in the file. Every other # in chipyard-base.yaml is a whole-line comment, and every other consumer of the file is conda, which parses YAML and strips comments correctly. This is the only line that is both an inline trailing comment and read by an ad-hoc shell parser — build-setup.sh is the only thing that greps and awks the YAML rather than parsing it, and sysroot_linux-64 is the only value it extracts that way. So it is not that comments break the file; it is one hand-rolled parser meeting the one line it cannot handle.

Triage trap, please read before reproducing: the failure does not reproduce by re-running in the same tree, because the first run's sed rewrote the pinned line without its comment, after which the comparison behaves as intended. A maintainer who simply retries will see it pass. Reset with git checkout -- conda-reqs/ first.

Three separable defects

(a) The pin is set to a version that may not exist, instead of one that does. A conda sysroot is the build-target floor: it must be ≤ the host glibc for the result to run, and per ucb-bar/chipyard#2174 ≥ 2.33 for VCS. It does not need to equal the host glibc — the comment on the pinned line says as much ("close to system glibc"). The correct adaptation is to clamp to the greatest published sysroot ≤ host glibc, warning when that falls below the VCS floor:

host glibc distro current clamped
2.31 Ubuntu 20.04, Debian 11 pins 2.31 → fails 2.28 + warn (below VCS floor)
2.34 RHEL/Rocky 9 rewrite+relock, lands 2.34 2.34 — no-op
2.35 Ubuntu 22.04 pins 2.35 → fails 2.34 — no-op
2.36 Debian 12 pins 2.36 → fails 2.34
2.39 Ubuntu 24.04, Fedora 40 rewrite+relock, lands 2.39 2.39

Note this also means ucb-bar/chipyard#2271 does not fix the case it was written for: ucb-bar/chipyard#2241 was reported from Ubuntu 20.04, whose glibc 2.31 is likewise unpublished, so that reporter now fails at the conda solve rather than at the build.

(b) The committed lockfile is deleted before its replacement is known to solve. scripts/generate-conda-lockfiles.sh runs under set -e and regenerates both committed lockfiles — full then lean — each as rm -rf $LOCKFILE followed by conda-lock … --lockfile $LOCKFILE (:20-31 and :33-43). The first solve is the one that fails, so the tree is left missing a committed file:

 M conda-reqs/chipyard-base.yaml
 D conda-reqs/conda-lock-reqs/conda-requirements-riscv-tools-linux-64.conda-lock.yml

A user who does not think to git checkout cannot simply retry — the fallback the script would otherwise have had (install the committed lockfile) was destroyed by the attempt to improve on it. Generating to a temporary path and moving into place on success removes this entirely.

Two details that make this worse than it first looks:

  • --use-lean-conda does not avoid it. The regeneration is not per-flavour: the script deletes and re-solves the full lockfile first, and that is where it dies — so a lean setup destroys the non-lean lockfile, a file that run was never going to install.
  • --use-unpinned-deps relocks twice. It calls the same script unconditionally (scripts/build-setup.sh:183-187) and then falls straight through into the guard above, which calls it again.

(c) Adjacent: retrying after any step-1 failure hits Error: Conda environment directories already exist! (scripts/build-setup.sh:170-175), because the failed run leaves .conda-lock-env behind — so the first thing a user meets after this bug is a second, unrelated-looking error.

Suggested fix

Three independent changes, smallest first; each removes a distinct failure:

  1. Parse the pin correctly. Two forms, and they are complementary rather than alternatives:

    1a — harden the parser (fixes the class):

    -DEFAULT_GLIBC=$(grep -i "sysroot_linux-64=" conda-reqs/chipyard-base.yaml | awk -F= '{print $2}')
    +DEFAULT_GLIBC=$(grep -i "sysroot_linux-64=" conda-reqs/chipyard-base.yaml | sed 's/#.*//' | awk -F= '{print $2}' | tr -d '[:space:]')
    

    1b — move the comment to its own line (fixes the instance, and makes the intent obvious to the next reader):

      conda-reqs/chipyard-base.yaml:19
         - gxx=13.2
    +    # sysroot must be close to system glibc for VCS compatibility.
    +    # Keep this comment on its own line: build-setup.sh greps this value.
    -    - sysroot_linux-64=2.34 # need to be close to system glibc for VCS compatibility
    +    - sysroot_linux-64=2.34
    

    Verified on the file as committed at 1.14.0: with 1b alone, awk yields 2.34, a host at glibc 2.34 now matches and skips the guard, and a host at 2.35 mismatches and rewrites — which is the intended behaviour in both directions.

    1b alone is enough to stop the unconditional rewrite, and 1a is what stops it coming back. A future contributor re-appending a trailing comment silently reintroduces the bug, and the symptom — lockfiles regenerated on every clone — is invisible unless someone is looking for it. Taking both costs one extra line.

  2. Generate to a temp path, move on success — removes the destructive half of (b).

  3. Clamp rather than assign — greatest published sysroot ≤ host glibc, warn below 2.33.

Reproduction

On any x86-64 Linux host whose glibc is not in {2.12, 2.12.2, 2.17, 2.28, 2.34, 2.39}:

ldd --version | head -1            # e.g. ldd (Ubuntu GLIBC 2.35-0ubuntu3.14) 2.35
git clone https://github.com/ucb-bar/chipyard.git && cd chipyard
git checkout 1.14.0
./build-setup.sh riscv-tools --use-lean-conda

Fails in step 1. git status afterwards shows the modified yaml and the deleted lockfile.

The guard mis-parse can be shown without running any of it:

SYS_GLIBC=$(ldd --version | awk '/ldd/{print $NF}')
DEFAULT_GLIBC=$(git show 1.14.0:conda-reqs/chipyard-base.yaml \
                | grep -i "sysroot_linux-64=" | awk -F= '{print $2}')
printf '[%s] vs [%s]\n' "$SYS_GLIBC" "$DEFAULT_GLIBC"

# and with the parser hardened, on the same unmodified file:
git show 1.14.0:conda-reqs/chipyard-base.yaml \
  | grep -i "sysroot_linux-64=" | sed 's/#.*//' | awk -F= '{print $2}' | tr -d '[:space:]'
# -> 2.34

That second block reads the committed file, so it is independent of the host and of any earlier run.

Workaround

Both forms do the same thing — make the test compare equal, so the branch is skipped and conda-lock install runs against the committed lockfile.

Preventive, on a fresh clone before the first build-setup.sh. Note it must drop the trailing comment, or the extraction still will not match:

sed -i.orig "s/^\([[:space:]]*-[[:space:]]*sysroot_linux-64=\).*/\1$(ldd --version | awk '/ldd/{print $NF}')/" \
    conda-reqs/chipyard-base.yaml
./build-setup.sh riscv-tools --use-lean-conda

Recovery, for a tree that has already failed:

git checkout -- conda-reqs/conda-lock-reqs/    # restore the lockfile(s) the failed relock deleted
rm -rf .conda-env .conda-lock-env              # defect (c): stale env dirs block any retry
./build-setup.sh riscv-tools --use-lean-conda

Leave conda-reqs/chipyard-base.yaml at the host's glibc value that the failed run already wrote — reverting it to 2.34 re-arms the failure. That value is read only by the mismatch test and the regeneration it triggers; once it equals the host glibc the branch is skipped and the committed lockfile installs as authored, pinning sysroot 2.34 — the correct value per (a).

Context

Found while standing up a Chipyard 1.14 rig for RISC-V vector work. We are not blocked — the workaround is cheap — but it is needed on every fresh clone on this machine class, and a first-time user has no reason to suspect the lockfile they need has just been deleted. It can take over 15 minutes to hit the error so it is difficult to debug if a new user was so inclined.

cwssemi avatar Aug 26 '26 01:08 cwssemi

Same for me on Fedora 44 after running ./build-setup.sh riscv-tools Commit hash: e602d917dcc495c58cabe906535e411707096c9c

The error:

The following packages are not available from current channels:

  - sysroot_linux-64=2.43

My host:

NAME="Fedora Linux"
VERSION="44 (Forty Four)"
RELEASE_TYPE=stable
ID=fedora
VERSION_ID=44

My glibc:

ldd --version | head -1 
ldd (GNU libc) 2.43

Probably check for available sysroot_linux-64 versions before replacing the version is a fix? This code works for me, it basically search for available sysroot_linux-64 versions and take min(conda_version, host_version):

diff --git a/scripts/build-setup.sh b/scripts/build-setup.sh
index 709fc4db..14357922 100755
--- a/scripts/build-setup.sh
+++ b/scripts/build-setup.sh
@@ -186,13 +186,20 @@ conda environment or \`source env.sh\` and skip this step with \`-s 1\`." >&2
         exit_if_last_command_failed
     fi
     SYS_GLIBC=$(ldd --version | awk '/ldd/{print $NF}')
-    DEFAULT_GLIBC=$(grep -i "sysroot_linux-64=" conda-reqs/chipyard-base.yaml | awk -F= '{print $2}')
-    if [ "$SYS_GLIBC" != "$DEFAULT_GLIBC" ]; then
+    # DEFAULT_GLIBC=$(grep -i "sysroot_linux-64=" conda-reqs/chipyard-base.yaml | awk -F= '{print $2}')
+    DEFAULT_GLIBC=$(
+    conda search --json --override-channels \
+        -c conda-forge "sysroot_linux-64<=$SYS_GLIBC" |
+    jq -r '."sysroot_linux-64"[].version' |
+    sort -Vru |
+    head -n 1
+    )
+    # if [ "$SYS_GLIBC" != "$DEFAULT_GLIBC" ]; then
         # replace the glibc version
-        sed -i.bak "s/^\([[:space:]]*-\s*sysroot_linux-64=\).*/\1$SYS_GLIBC/" conda-reqs/chipyard-base.yaml
+        sed -i.bak "s/^\([[:space:]]*-\s*sysroot_linux-64=\).*/\1$DEFAULT_GLIBC/" conda-reqs/chipyard-base.yaml
         $CYDIR/scripts/generate-conda-lockfiles.sh
         exit_if_last_command_failed
-    fi
+    # fi
     echo "Using lockfile for conda: $LOCKFILE"

     # use conda-lock to create env

NakanoMiku39 avatar Aug 26 '26 09:08 NakanoMiku39

BTW the behavior of the --skip flag doesn’t quite match what is described in the documentation I tried to skip step 8 and begin with step 9 using the -s 8 flag, but it ended up skipping both steps 8 and 9. Perhaps the documentation could be clarified?

The code snippet:

--skip-conda)
            SKIP_LIST+=(1) ;;
        --skip-submodules)
            SKIP_LIST+=(2) ;;
        --skip-toolchain)
            SKIP_LIST+=(3) ;;
        --skip-ctags)
            SKIP_LIST+=(4) ;;
        --skip-precompile)
            SKIP_LIST+=(5 6) ;;
        --skip-firesim)
            SKIP_LIST+=(6 7) ;;
        --skip-marshal)
            SKIP_LIST+=(8 9) ;;
        --skip-circt)
            SKIP_LIST+=(10) ;;
        --skip-clean)
            SKIP_LIST+=(11) ;;

While the doc:

-s 1 skips initializing Conda environment

-s 2 skips initializing Chipyard submodules

-s 3 skips initializing toolchain collateral (Spike, PK, tests, libgloss)

-s 4 skips initializing ctags

-s 5 skips pre-compiling Chipyard Scala sources

-s 6 skips initializing FireSim

-s 7 skips pre-compiling FireSim sources

-s 8 skips initializing FireMarshal

-s 9 skips pre-compiling FireMarshal default buildroot Linux sources

-s 10 skips installing CIRCT

-s 11 skips running repository clean-up

NakanoMiku39 avatar Aug 27 '26 08:08 NakanoMiku39

BTW the behavior of the --skip flag doesn’t quite match what is described in the documentation I tried to skip step 8 and begin with step 9 using the -s 8 flag, but it ended up skipping both steps 8 and 9. Perhaps the documentation could be clarified?

The code snippet:

--skip-conda)
            SKIP_LIST+=(1) ;;
        --skip-submodules)
            SKIP_LIST+=(2) ;;
        --skip-toolchain)
            SKIP_LIST+=(3) ;;
        --skip-ctags)
            SKIP_LIST+=(4) ;;
        --skip-precompile)
            SKIP_LIST+=(5 6) ;;
        --skip-firesim)
            SKIP_LIST+=(6 7) ;;
        --skip-marshal)
            SKIP_LIST+=(8 9) ;;
        --skip-circt)
            SKIP_LIST+=(10) ;;
        --skip-clean)
            SKIP_LIST+=(11) ;;

While the doc:

-s 1 skips initializing Conda environment -s 2 skips initializing Chipyard submodules -s 3 skips initializing toolchain collateral (Spike, PK, tests, libgloss) -s 4 skips initializing ctags -s 5 skips pre-compiling Chipyard Scala sources -s 6 skips initializing FireSim -s 7 skips pre-compiling FireSim sources -s 8 skips initializing FireMarshal -s 9 skips pre-compiling FireMarshal default buildroot Linux sources -s 10 skips installing CIRCT -s 11 skips running repository clean-up

step 8 (initializing FireMarshal) is a prerequisite for step 9 (FireMarshal default buildroot Linux sources)

raghav-g13 avatar Oct 06 '26 04:10 raghav-g13

I'd like to work on this unless someone already is.

My plan is to split it into three small commits:

  1. Pin sysroot_linux-64 to the newest version conda-forge actually publishes that is <= the host glibc (looked up with 'conda search' at runtime), and exit with a clear error if there isn't one. So 2.35 -> 2.34 and 2.43 -> 2.39.
  2. Have generate-conda-lockfiles.sh write to a temp file and only move it into place after the solve succeeds, so a failed solve doesn't delete the committed lockfiles.
  3. Clean up the .conda-lock-env that a failed run created, or at least print the exact commands to recover.

I'd leave the '-s' skip numbering from NakanoMiku39's comment for a separate PR. Does that approach work for you, or would you rather handle the sysroot pin another way?

mert-ogul avatar Oct 12 '26 12:10 mert-ogul

@mert-ogul Take a look at https://github.com/ucb-bar/chipyard/pull/2374, it may already address what you you were intending. The PR is a 3 commits that address different parts of what you are wanting.

cwssemi avatar Oct 12 '26 22:10 cwssemi

@cwssemi Thanks, I hadn’t seen this. It covers what I had planned, so I’ll leave it to you.

One thing I noticed while reading it: since #2380 and #2381 landed, it conflicts with main in chipyard-base.yaml and generate-conda-lockfiles.sh. The lockfile script now loops over linux-64 and linux-aarch64, so the temp-path change needs to go inside that loop. I’m happy to help with the rebase or test it.

The leftover .conda-lock-env that blocks a retry isn’t covered here or in #2379. If you’re not planning to do that one, I can send it as a small separate PR.

mert-ogul avatar Oct 15 '26 12:10 mert-ogul

fixed by https://github.com/ucb-bar/chipyard/commit/e184ee9bd54f6b804a46139cc6c6971541834d53

jimfangx avatar Oct 15 '26 13:10 jimfangx