diff --git a/runtime/cursor_art.c b/runtime/cursor_art.c index 3a9278f6..e576e5e8 100644 --- a/runtime/cursor_art.c +++ b/runtime/cursor_art.c @@ -62,6 +62,7 @@ static float sample(V p, float *r, float *g, float *b) { float a = ds < -0.8f ? 1 : (0.8f - ds) / 1.6f; return 0.38f * a; } + *r = *g = *b = 0; return 0; } diff --git a/tests/cursor_art_check.py b/tests/cursor_art_check.py new file mode 100644 index 00000000..81a57629 --- /dev/null +++ b/tests/cursor_art_check.py @@ -0,0 +1,74 @@ +"""The production custom cursor must not depend on uninitialized stack colours. + + python tests/cursor_art_check.py --cc path/to/clang +""" +import argparse +from pathlib import Path +import shutil +import struct +import subprocess +import tempfile + +ROOT = Path(__file__).resolve().parents[1] +HARNESS = r''' +#include +#include +#include +#include +#ifdef _WIN32 +#include +#include +#endif +#include "input_wsys.h" +int main(int argc, char **argv) { + assert(argc == 2); + int size = atoi(argv[1]), hot; + assert(size > 0 && size <= 128); + uint32_t *pixels = malloc((size_t)size * size * sizeof *pixels); + assert(pixels); + cursor_art(pixels, size, &hot); +#ifdef _WIN32 + assert(_setmode(_fileno(stdout), _O_BINARY) != -1); +#endif + assert(fwrite(&hot, sizeof hot, 1, stdout) == 1); + assert(fwrite(pixels, sizeof *pixels, (size_t)size * size, stdout) == (size_t)size * size); + free(pixels); + return 0; +} +''' + + +def main(): + parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + parser.add_argument('--cc', default=shutil.which('clang') or r'C:\msys64\ucrt64\bin\clang.exe') + args = parser.parse_args() + with tempfile.TemporaryDirectory(prefix='cursor-art-') as temp: + work = Path(temp) + source = work / 'probe.c' + source.write_text(HARNESS, encoding='utf-8') + outputs = {} + for mode in ('zero', 'pattern'): + exe = work / f'{mode}.exe' + subprocess.run([args.cc, '-O2', '-std=gnu11', f'-ftrivial-auto-var-init={mode}', + '-I', str(ROOT / 'runtime'), str(source), str(ROOT / 'runtime/cursor_art.c'), + '-lm', '-o', str(exe)], check=True) + outputs[mode] = {} + for size in (16, 24, 32, 48, 64, 96, 128): + data = subprocess.run([str(exe), str(size)], check=True, capture_output=True).stdout + assert len(data) == 4 + size * size * 4 + pixels = struct.unpack(f'<{size * size}I', data[4:]) + assert any(p == 0 for p in pixels), 'the cursor must have transparent pixels' + assert any(p >> 24 and p & 0xffffff for p in pixels), 'the cursor must contain visible colour' + outputs[mode][size] = data + differences = [] + for size in outputs['zero']: + a, b = outputs['zero'][size], outputs['pattern'][size] + different = sum(a[i:i + 4] != b[i:i + 4] for i in range(4, len(a), 4)) + if different or a[:4] != b[:4]: + differences.append((size, different)) + print(f'cursor {size}x{size}: {different} pixels differ between zero and pattern stack initialization') + assert not differences, f'cursor pixels depend on uninitialized automatic variables: {differences}' + + +if __name__ == '__main__': + main() diff --git a/tests/run_all.py b/tests/run_all.py index fc74a277..4fc262d9 100644 --- a/tests/run_all.py +++ b/tests/run_all.py @@ -67,6 +67,7 @@ TESTS = { 'settings_doc_check': ('.venv', None, [], ''), 'mouse_direct_check': ('.venv', 'cc', ['--cc', ''], 'compiles its checks with clang'), 'cheats_vm_check': ('.venv', 'cc', ['--cc', ''], 'compiles runtime/cheats.c into a harness with clang'), + 'cursor_art_check': ('.venv', 'cc', ['--cc', ''], 'compiles the cursor with zero and pattern stack initialization'), 'romfs_shapes_check': ('.venv', 'cc', ['--cc', ''], 'compiles the production overlay selection helpers'), 'frame_world_check': ('.venv', 'cc', ['--cc', ''], 'compiles the production world-clock player walk'), 'sync_ahead_check': ('.venv', 'cc', ['--cc', ''], 'compiles the production sync-ahead wait selection'),