Shell-quote every compiler option token on POSIX #907
clvk tries to avoid platform specific code outside of utils.cpp, I think it would be better to move that over there.
Also a test would be welcome here, it should not be too complicated to add.
Moved to utils.cpp as shell_quote_token and quote_options_for_shell, with tests under tests/utils. While moving it I also made the splitter honour double-quoted spans and drop the quotes, so an application option like -DMSG="hello world" now reaches clspv as a single argv entry, and quoted the -I/temp-dir/input/output paths that get appended outside prepare_build_options.
This function is not using platform specific code and is only used once. I don't mind keeping it, but I don't think utils.cpp is the right place for it.
I don't think we want a new test for those. Can't we have them added inside api_tests?
Done in c339880: shell_quote_token moves to program.cpp next to the file paths that use it, utils.cpp keeps only the option-string splitter with its own local per-token helper, and the tests now live inside api_tests (tests/api/shell_quote.cpp) instead of a separate target.
This function is platform specific, it should not be here
This function is still not platform specific, it should not be here.
api_tests is not meant to test clvk inside the function.
That test should use the OpenCL API to test the quote in the build options.
Also, I think it should be platform-agnostic.
All comments addressed in bd82dba:
shell_quote_token (with the WIN32 path) is back in utils.cpp where platform-specific code belongs; quote_options_for_shell moved to program.cpp as a static helper next to prepare_build_options, its only consumer.WithContext fixture, clBuildProgram with nine option sets including the hashcat pattern (-DXM2S(x)=#x) and a single-quote case. Success of the build is the assertion, platform-agnostic.popen command lines (llvmspirv_input_file, the from-IL clspv_input_file) are quoted.Not useful I believe, and also not always true. clspv can be setup to be used "online" instead of executed through popen
I don't understand why all the filenames (llvmspirv_input_file, clspv_input_file, input_file & tmp_folder) need to be quoted. Also if we are to quote them, can we do it only once per file?
Removed. You are right that it is not always true: the online compiler path bypasses popen entirely.
My understanding is that the test check that we do not get any segfault. Could we also make sure that the macro is well passed in the test?
I think we should be able to use printf in the kernel and check the output on the host side for example.
Have a look at tests/api/printf.cpp for example on how to use printf in the test.
Done in bbb8a07.
Done in 922e88e. The new test builds a kernel with -D XM2S(x)=#x and prints XM2S(hello) with printf, asserting the host-side output is hello. This checks that the macro reaches the compiler with its exact value, not just that the build survives. The build-only loop stays for the wider set of metacharacters.
Both points are addressed in 3545fdf.
The paths need quoting because they inherit the temp directory (config.compiler_temp_dir(), which defaults to std::filesystem::temp_directory_path()), so a TMPDIR containing spaces or shell special characters would otherwise split the command line. The online path is unaffected; every call site quoting a path is in the offline one. Each file is now quoted exactly once. The SPIR-V branch computes the quoted form of each path next to its construction, and the source branch appends the quoted input file at a single point after the .bc/.cl suffix is chosen.
It wasn't a new test I was looking for, but rather a modification of the existing one. Verifying that all options provided here are being accurately relayed to the kernel is essential.
Done in c4a77ae. The existing test now drives a table of (options, expression, expected) cases: each row builds a kernel whose printf prints the macro, stringized through a STR(x)=#x helper where needed, and asserts the exact relayed value. The separate test I had added is folded into the table as its first two rows.
Addressed in c339880 and 44e07a0: the quote-aware tokenizer stayed in program.cpp next to its only caller, and only the platform-specific single-token quoting moved to utils.cpp.
Done in 44e07a0: the unit test and its CMake target are gone; the coverage lives in tests/api/shell_quote.cpp and runs with api_tests.
Done in 44e07a0: shell_quote_token, the platform-specific half, now lives in utils.cpp.
Done in 44e07a0: the test drives the OpenCL API (clBuildProgram with the options) and, since 3cc5035, also checks each relayed macro value through a kernel printf.
I'm not sure why the functions include "shell" in their names, but apart from that, LGTM.
The shell in the names is the interpreter on the other side. cvk_exec runs the command line through popen(3), so the quoting rules that make the tokens arrive verbatim are the POSIX shell's (single quotes, embedded quotes escaped as '\''). The Windows branch keeps the previous double-quoting behaviour for cmd.exe. Happy to drop shell from the names if you prefer them shorter.
std::string quoted;
std::string token;
bool in_quotes = false;
auto flush_token = [&]() {
if (!token.empty()) {
quoted += shell_quote_token(token);
quoted += ' ';
token.clear();
}
};
for (char c : options) {
if (c == '"') {
in_quotes = !in_quotes;
} else if (c == ' ' && !in_quotes) {
flush_token();
} else {
token += c;
}
}
flush_token();
return quoted;
#ifdef WIN32
if (!token.empty() && token.front() == '-') {
return std::string(token);
}
return "\"" + std::string(token) + "\"";
#else
std::string quoted;
quoted.reserve(token.size() + 2);
quoted += '\'';
size_t pos = 0;
for (size_t next; (next = token.find('\'', pos)) != std::string_view::npos;
pos = next + 1) {
quoted.append(token, pos, next - pos);
quoted += "'\\''";
}
quoted.append(token, pos, token.size() - pos);
quoted += '\'';
return quoted;
#endif
I've asked an AI to propose more optimised implementations for shell_quote_token & quote_options_for_shell. Here is what it came with.
Applied in 9469eb0, thanks. The single-pass tokenizer is indeed cleaner than the collect-then-quote two-phase version.
Applied in 9469eb0 verbatim, with <string_view> added to the includes in utils.cpp for the npos reference.
rjodinchr
paoloantinori
prepare_build_optionsonly quoted tokens that do not start with a dash, and it used double quotes. Both choices are unsafe because the command line reaches clspv throughpopen(3):-DXM2S(x)=#x(valid per the OpenCL specification, which allows both-D nameand-Dname) is passed through unquoted. The shell aborts with a syntax error before clspv runs, and the application seesCL_BUILD_PROGRAM_FAILUREwith an empty build log. hashcat passes exactly this kind of define, which is how I found it.-DVALUE=$xexpands silently.This change POSIX-quotes every token, using single quotes with the standard
'\''escape for embedded single quotes, so every token reaches clspv verbatim. Windows keeps the previous behavior because cmd.exe treats single quotes as regular characters.Verification: I extracted the exact quoting loop into a standalone test program, generated the command line for adversarial tokens (parentheses, dollar signs, single and double quotes, semicolons, backticks, spaces inside a token, globs, pipes), ran it through
popenagainst a receiver script, and compared the child argv. All tokens arrive intact. The full library build is left to CI; the change only touches string assembly inprepare_build_options.Found while investigating #906 (different root cause, same session tripped over this first).