diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index f74f24427f..f77f0123a8 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -409,6 +409,13 @@ if(BUILD_TESTING) target_link_options(unimplemented_policy_test PRIVATE /OPT:NOREF) add_dependencies(unimplemented_policy_test gta6rt_tls_host) add_test(NAME unimplemented_policy COMMAND unimplemented_policy_test) + + # Exactly one CLI mode: combining --dry-run with --run (or --init) is refused with the usage exit code before + # any image is mapped, so --dry-run cannot run image code by accident. No image or fixture is needed. + add_test(NAME cli_modes + COMMAND ${CMAKE_COMMAND} -DGTA6RT=$ + -P ${CMAKE_CURRENT_SOURCE_DIR}/test/cli_modes_test.cmake + ) endif() if(WIN32 AND TARGET root_sig_test) diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index 20c870377a..375261f4a8 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp @@ -64,8 +64,10 @@ int wmain(int argc, wchar_t** argv) { else if (image.empty()) image = a; else return usage(); } - if (image.empty()) return usage(); - if (!dry_run && !init && !run) return usage(); + // Exactly one mode: --dry-run promises that no image code runs, so pairing it with --run (or --init) must be + // refused before anything is mapped, not silently resolved in favour of the one that runs code. + const int modes = int(dry_run) + int(init) + int(run); + if (image.empty() || modes != 1) return usage(); if (game_dir.empty()) game_dir = image.parent_path(); rt::d3d12_trace::set_enabled(trace_d3d12); diff --git a/runtime/test/cli_modes_test.cmake b/runtime/test/cli_modes_test.cmake new file mode 100644 index 0000000000..d1cb013dc6 --- /dev/null +++ b/runtime/test/cli_modes_test.cmake @@ -0,0 +1,16 @@ +# gta6rt must accept exactly one of --dry-run, --init and --run. Combining modes used to pass validation and then +# run the executable's entry point even with --dry-run, which promises that no image code runs. +# +# usage() returns 2. The image path need not exist: the mode check runs before the image is mapped. +function(expect_usage label) + execute_process(COMMAND "${GTA6RT}" ${ARGN} RESULT_VARIABLE rc OUTPUT_QUIET ERROR_QUIET) + if(NOT rc EQUAL 2) + message(FATAL_ERROR "cli_modes: ${label} returned ${rc}, expected 2") + endif() +endfunction() + +expect_usage("--dry-run --run" --dry-run --run x.exe) +expect_usage("--dry-run --init" --dry-run --init x.exe) +expect_usage("--init --run" --init --run x.exe) +expect_usage("no mode" x.exe) +expect_usage("no image" --run)