|
| 1 | +#!/usr/bin/env bash |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +# Verify whether compute-driver code is present in a compiled binary. |
| 6 | +# |
| 7 | +# Per-driver Cargo features (driver-kubernetes, driver-docker, driver-podman, |
| 8 | +# driver-vm) on openshell-server, all default-on, gate the driver crates and |
| 9 | +# their in-server plumbing. Building with --no-default-features and enabling |
| 10 | +# only a subset must produce a binary that carries only those drivers. This |
| 11 | +# guard inspects a built binary for markers that only exist when a given |
| 12 | +# driver is compiled in. |
| 13 | +# |
| 14 | +# Markers are strings baked into each driver's code and must not overlap |
| 15 | +# with other drivers or shared code. The `present` positive control fails |
| 16 | +# loudly if a marker goes stale, so `absent` checks can never become |
| 17 | +# silently vacuous. |
| 18 | + |
| 19 | +set -euo pipefail |
| 20 | + |
| 21 | +# Marker table: DRIVER=marker_that_only_appears_when_that_driver_is_compiled_in |
| 22 | +# |
| 23 | +# Each driver crate declares a `#[used] static COMPILE_MARKER` holding a |
| 24 | +# well-known byte string; the VM driver's marker lives in compute::vm behind |
| 25 | +# `#[cfg(feature = "driver-vm")]`. `#[used]` prevents dead-code elimination |
| 26 | +# so the marker survives every optimization level and strip mode. Keep these |
| 27 | +# in sync with the driver crates. |
| 28 | +declare -A MARKERS=( |
| 29 | + [kubernetes]="OPENSHELL_DRIVER_MARKER:kubernetes" |
| 30 | + [docker]="OPENSHELL_DRIVER_MARKER:docker" |
| 31 | + [podman]="OPENSHELL_DRIVER_MARKER:podman" |
| 32 | + [vm]="OPENSHELL_DRIVER_MARKER:vm" |
| 33 | +) |
| 34 | + |
| 35 | +ALL_DRIVERS=(kubernetes docker podman vm) |
| 36 | + |
| 37 | +usage() { |
| 38 | + cat >&2 <<'EOF' |
| 39 | +Usage: |
| 40 | + verify-drivers-compiled-out.sh present <driver|all> <binary> |
| 41 | + Assert the driver's markers ARE present. Use as a positive control. |
| 42 | + verify-drivers-compiled-out.sh absent <driver|all> <binary> |
| 43 | + Assert the driver's markers are NOT present. |
| 44 | + verify-drivers-compiled-out.sh only <driver[,driver...]> <binary> |
| 45 | + Assert markers are present for each listed driver AND absent for |
| 46 | + each unlisted one. Covers presence + absence in a single pass. |
| 47 | +
|
| 48 | +Drivers: kubernetes docker podman vm (or 'all') |
| 49 | +EOF |
| 50 | +} |
| 51 | + |
| 52 | +if [[ $# -lt 3 ]]; then |
| 53 | + usage |
| 54 | + exit 2 |
| 55 | +fi |
| 56 | + |
| 57 | +mode=$1 |
| 58 | +selector=$2 |
| 59 | +binary=$3 |
| 60 | + |
| 61 | +if [[ ! -f $binary ]]; then |
| 62 | + echo "error: binary not found: $binary" >&2 |
| 63 | + exit 2 |
| 64 | +fi |
| 65 | +if ! command -v strings >/dev/null 2>&1; then |
| 66 | + echo "error: 'strings' (binutils) is required to inspect the binary" >&2 |
| 67 | + exit 2 |
| 68 | +fi |
| 69 | + |
| 70 | +dump=$(strings -a "$binary") |
| 71 | +failed=0 |
| 72 | + |
| 73 | +# Assert that $1 marker appears at least once in $dump for driver $2. |
| 74 | +assert_present() { |
| 75 | + local driver=$1 |
| 76 | + local marker=${MARKERS[$driver]} |
| 77 | + local count |
| 78 | + count=$(grep -c -F "$marker" <<<"$dump" || true) |
| 79 | + if [[ $count -eq 0 ]]; then |
| 80 | + echo "FAIL: driver '$driver' marker '$marker' missing from $(basename "$binary")" >&2 |
| 81 | + failed=1 |
| 82 | + else |
| 83 | + echo "OK: driver '$driver' compiled in ($count occurrence(s) of '$marker')" |
| 84 | + fi |
| 85 | +} |
| 86 | + |
| 87 | +# Assert that $1 marker does NOT appear in $dump for driver $2. |
| 88 | +assert_absent() { |
| 89 | + local driver=$1 |
| 90 | + local marker=${MARKERS[$driver]} |
| 91 | + local count |
| 92 | + count=$(grep -c -F "$marker" <<<"$dump" || true) |
| 93 | + if [[ $count -ne 0 ]]; then |
| 94 | + echo "FAIL: driver '$driver' marker '$marker' found in $(basename "$binary") ($count occurrence(s)); driver was not compiled out" >&2 |
| 95 | + failed=1 |
| 96 | + else |
| 97 | + echo "OK: driver '$driver' compiled out (0 occurrences of '$marker')" |
| 98 | + fi |
| 99 | +} |
| 100 | + |
| 101 | +# Expand 'all' or a comma-separated list into the SELECTED array in the |
| 102 | +# caller's scope. Exits 2 on any unknown driver. Runs in the current shell |
| 103 | +# so that exit propagates — do not call this from a subshell or process |
| 104 | +# substitution. |
| 105 | +resolve_drivers() { |
| 106 | + local input=$1 |
| 107 | + SELECTED=() |
| 108 | + if [[ $input == "all" ]]; then |
| 109 | + SELECTED=("${ALL_DRIVERS[@]}") |
| 110 | + return |
| 111 | + fi |
| 112 | + local drivers |
| 113 | + IFS=',' read -r -a drivers <<<"$input" |
| 114 | + for d in "${drivers[@]}"; do |
| 115 | + if [[ -z ${MARKERS[$d]+set} ]]; then |
| 116 | + echo "error: unknown driver '$d'. known: ${ALL_DRIVERS[*]}" >&2 |
| 117 | + exit 2 |
| 118 | + fi |
| 119 | + SELECTED+=("$d") |
| 120 | + done |
| 121 | +} |
| 122 | + |
| 123 | +resolve_drivers "$selector" |
| 124 | + |
| 125 | +case "$mode" in |
| 126 | + present) |
| 127 | + for d in "${SELECTED[@]}"; do |
| 128 | + assert_present "$d" |
| 129 | + done |
| 130 | + ;; |
| 131 | + absent) |
| 132 | + for d in "${SELECTED[@]}"; do |
| 133 | + assert_absent "$d" |
| 134 | + done |
| 135 | + ;; |
| 136 | + only) |
| 137 | + declare -A present_set=() |
| 138 | + for d in "${SELECTED[@]}"; do |
| 139 | + present_set[$d]=1 |
| 140 | + done |
| 141 | + for d in "${ALL_DRIVERS[@]}"; do |
| 142 | + if [[ -n ${present_set[$d]+set} ]]; then |
| 143 | + assert_present "$d" |
| 144 | + else |
| 145 | + assert_absent "$d" |
| 146 | + fi |
| 147 | + done |
| 148 | + ;; |
| 149 | + *) |
| 150 | + usage |
| 151 | + exit 2 |
| 152 | + ;; |
| 153 | +esac |
| 154 | + |
| 155 | +exit "$failed" |
0 commit comments