stable-diffusion.cpp/test/security_smoke.sh
Claude 071a12b378
feat: add --profile to choose a resource footprint
--profile max|balanced|lite|auto sets, in one option, what a small
machine needs: balanced turns the memory guard on at 90; lite also
puts the text encoder on the CPU, tiles the VAE, runs CFG unbatched
and lowers the guard to 85. An option given on the command line always
wins over the profile, including --memory-guard 0. A profile does not
touch --params-backend or --backend, so --auto-fit keeps deciding
where the weights live, and it never changes the size or the steps of
an image.

auto compares the size of the model files plus a working-memory
reserve with the free memory of the discrete GPU with the most, or
with the available RAM without one, and logs the figures it used. It
leaves the defaults alone when the sizes or the memory are unknown.

parse_options() now records which options were given, through
option_was_given(). The levels below lite (weights streamed from disk,
everything on the CPU) cannot be chosen from the command line; they
are the steps a server takes when a request runs out of memory.

The thresholds of auto are heuristics and have not been tuned on a real
model or a GPU. Checked with the synthetic SD 1.5 model on the CPU: the
three profiles give the same image, and with 3.3 GiB held by another
process auto picks balanced.

Claude-Session: https://claude.ai/code/session_01EhZzDBkuuMp1jBf4WtEySC
2026-10-11 07:19:14 +02:00

82 lines
3.5 KiB
Bash
Executable file

#!/usr/bin/env bash
# Security/robustness smoke test for sd-cli.
# Usage: ./security_smoke.sh [path-to-sd-cli]
# Exit code 0 = all checks passed, 1 = at least one failure.
set -u
SD_CLI="${1:-$(dirname "$0")/../build/bin/sd-cli}"
if [[ ! -x "$SD_CLI" ]]; then
echo "sd-cli not found or not executable: $SD_CLI" >&2
echo "usage: $0 [path-to-sd-cli]" >&2
exit 1
fi
# The script cd's into a temp dir: the binary path must survive that.
SD_CLI="$(cd "$(dirname "$SD_CLI")" && pwd)/$(basename "$SD_CLI")"
WORKDIR="$(mktemp -d)"
trap 'rm -rf "$WORKDIR"' EXIT
cd "$WORKDIR"
failures=0
check() {
local label="$1"
shift
"$@" >out.log 2>&1
local code=$?
if [[ $code -ge 128 ]]; then
echo "FAIL $label (crashed with signal $((code - 128)))"
failures=$((failures + 1))
elif [[ $code -eq 126 || $code -eq 127 ]]; then
echo "FAIL $label (test setup error: binary not runnable, exit $code)"
failures=$((failures + 1))
elif [[ $code -eq 0 ]]; then
echo "FAIL $label (unexpectedly succeeded)"
failures=$((failures + 1))
else
echo "ok $label (clean failure, exit $code)"
fi
}
# --- hostile model files -----------------------------------------------------
head -c 4096 /dev/urandom > garbage.safetensors
printf '\xff\xff\xff\xff\xff\xff\xff\x7f' > huge_header.safetensors
head -c 100 /dev/urandom >> huge_header.safetensors
printf '\xe8\x03\x00\x00\x00\x00\x00\x00{"incomplete' > truncated.safetensors
python3 - <<'EOF'
import json, struct
h = json.dumps({'a': {'dtype': 'F32', 'shape': [1000000, 1000000],
'data_offsets': [0, 4000000000000]}}).encode()
open('bad_offsets.safetensors', 'wb').write(struct.pack('<Q', len(h)) + h)
EOF
printf 'GGUF\x99\x99\x99\x99' > bad_magic.gguf
head -c 64 /dev/urandom >> bad_magic.gguf
for f in nonexistent.safetensors garbage.safetensors huge_header.safetensors \
truncated.safetensors bad_offsets.safetensors bad_magic.gguf; do
check "hostile model: $f" "$SD_CLI" -m "$f" -p test -o out.png
done
# --- extreme/malformed option values ----------------------------------------
long_arg="$(python3 -c 'print("A" * 5000)')"
check "option: --memory-guard huge" "$SD_CLI" -m garbage.safetensors -p t --memory-guard 99999999999
check "option: --memory-guard text" "$SD_CLI" -m garbage.safetensors -p t --memory-guard abc
check "option: --threads huge" "$SD_CLI" -m garbage.safetensors -p t --threads 99999999999
check "option: --seed huge" "$SD_CLI" -m garbage.safetensors -p t --seed 99999999999999999999999
check "option: --skip-layers huge" "$SD_CLI" -m garbage.safetensors -p t --skip-layers 99999999999
check "option: --cfg-scale overflow" "$SD_CLI" -m garbage.safetensors -p t --cfg-scale 1e999
check "option: --max-vram text" "$SD_CLI" -m garbage.safetensors -p t --max-vram abc
check "option: --backend unknown" "$SD_CLI" -m garbage.safetensors -p t --backend no_such_backend --disable-backend-fallback
check "option: --backend long" "$SD_CLI" -m garbage.safetensors -p t --backend "$long_arg"
check "option: --profile unknown" "$SD_CLI" -m garbage.safetensors -p t --profile fast
check "option: --profile long" "$SD_CLI" -m garbage.safetensors -p t --profile "$long_arg"
check "option: --profile no value" "$SD_CLI" -m garbage.safetensors -p t --profile
check "option: --profile auto, bad model" "$SD_CLI" -m garbage.safetensors -p t --profile auto
echo
if [[ $failures -gt 0 ]]; then
echo "$failures check(s) FAILED"
exit 1
fi
echo "all checks passed"