stable-diffusion.cpp/test
Claude 7ed90dace3
fix: keep model file handles open between tensor loads
The loader opened the model file again for every batch of tensors, in every
worker thread: once per component at startup (text encoder, UNet, VAE) and
then again each time weights are loaded back from disk (per segment, per
residency swap, per request). Each open is a chance to fail, and on Windows
it can fail exactly when the system is short of memory or handles
("failed to open '...safetensors'" at VAE decode, after RAM hit 100%).

A new FileHandlePool keeps the read streams open and hands them out to the
workers one at a time. ModelLoader owns one; copies and the per-file readers
used for LoRA tensors share it. A stream is dropped when the file's size or
modification time changes, when it is in an error state, or when its source
is removed. If a read fails the worker reopens the file once and repeats the
read, so a stream that went bad while idle (removable or network storage)
no longer fails the whole generation. The open retry with errno/doserrno
logging moves into the pool and now also covers .ckpt (zip) files.

Measured with a synthetic SD 1.5 checkpoint on CPU: the file is opened 5
times for three server requests instead of 9 per request; an injected EIO is
absorbed and the image is pixel-identical; persistent read errors end in a
clean error. test/file_handle_pool_test.cpp covers reuse, changed and
replaced files, failed streams, limits and concurrent use, and passes under
ASan/UBSan and ThreadSanitizer.

Claude-Session: https://claude.ai/code/session_01EhZzDBkuuMp1jBf4WtEySC
2026-10-11 06:09:49 +02:00
..
api_robustness.cpp implemented stable diffusions regular and specific with margin of limits of CPU/GPU 2026-10-07 19:23:28 +02:00
file_handle_pool_test.cpp fix: keep model file handles open between tensor loads 2026-10-11 06:09:49 +02:00
fuzz_model_loader.py implemented stable diffusions regular and specific with margin of limits of CPU/GPU 2026-10-07 19:23:28 +02:00
pe_imports_test.cpp feat: log why a GPU backend module could not be used 2026-10-11 05:52:43 +02:00
README.md fix: keep model file handles open between tensor loads 2026-10-11 06:09:49 +02:00
security_smoke.sh implemented stable diffusions regular and specific with margin of limits of CPU/GPU 2026-10-07 19:23:28 +02:00

Test di robustezza e sicurezza (fork)

Script locali di questo fork per verificare che il motore fallisca in modo pulito con input ostili, senza crash né errori di memoria. Istruzioni complete in italiano: docs/integrazione_progetti.md, sezione "Sicurezza".

Test Cosa verifica Come si esegue
security_smoke.sh file modello ostili + opzioni CLI estreme → mai crash (exit < 128) ./test/security_smoke.sh build/bin/sd-cli
api_robustness.cpp API C: puntatori nulli, indici fuori range, buffer piccoli, stress multi-thread (con ASan/UBSan) vedi intestazione del file
pe_imports_test.cpp lettore della tabella di importazione dei file PE (diagnostica delle DLL mancanti su Windows): file validi, troncati e corrotti (con ASan/UBSan) vedi intestazione del file
file_handle_pool_test.cpp pool dei flussi aperti sui file modello: riuso, file modificato o sostituito, flussi in errore, limiti, uso concorrente (con ASan/UBSan e TSan) vedi intestazione del file
fuzz_model_loader.py fuzzing a mutazione del caricamento modelli (safetensors/GGUF) python3 test/fuzz_model_loader.py <sd-cli> --seconds 300

Per il fuzzing è consigliata una build con AddressSanitizer:

cmake -B build_asan -DCMAKE_BUILD_TYPE=RelWithDebInfo -DSD_WEBP=OFF -DSD_WEBM=OFF \
      -DCMAKE_C_FLAGS=-fsanitize=address -DCMAKE_CXX_FLAGS=-fsanitize=address \
      -DCMAKE_EXE_LINKER_FLAGS=-fsanitize=address
cmake --build build_asan -j --target sd-cli
python3 test/fuzz_model_loader.py build_asan/bin/sd-cli --seconds 600

Gli input che causano una scoperta vengono salvati in test/fuzz_findings/ (da non committare). Questi test riducono il rischio ma non dimostrano l'assenza di vulnerabilità.