From fa06e7e41f34b9e4e0097e145db3a5eea2a8bace Mon Sep 17 00:00:00 2001 From: Mike Smith Date: Mon, 6 Jan 2025 13:20:05 +0800 Subject: [PATCH] use tmpfile to make cuda backend safer --- include/luisa/backends/ext/tex_compress_ext.h | 15 +++++++++++++ src/backends/cuda/cuda_compiler.cpp | 21 ++++--------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/include/luisa/backends/ext/tex_compress_ext.h b/include/luisa/backends/ext/tex_compress_ext.h index b9135482b..464618ab4 100644 --- a/include/luisa/backends/ext/tex_compress_ext.h +++ b/include/luisa/backends/ext/tex_compress_ext.h @@ -2,10 +2,14 @@ #include #include + namespace luisa::compute { + template class BufferView; + class Stream; + template class Image; @@ -16,11 +20,22 @@ class TexCompressExt : public DeviceExtension { public: static constexpr luisa::string_view name = "TexCompressExt"; + +// avoid macro pollution from X11 +#ifdef Success +#undef Success +#endif + +#ifdef Failed +#undef Failed +#endif + enum class Result : int8_t { NotImplemented = -1, Success = 0, Failed = 1 }; + // TODO: astc virtual Result compress_bc6h(Stream &stream, const ImageView &src, diff --git a/src/backends/cuda/cuda_compiler.cpp b/src/backends/cuda/cuda_compiler.cpp index a241113fa..670c4f918 100644 --- a/src/backends/cuda/cuda_compiler.cpp +++ b/src/backends/cuda/cuda_compiler.cpp @@ -38,15 +38,9 @@ namespace luisa::compute::cuda { for (auto o : options) { argv.emplace_back(o); } argv.emplace_back(nullptr); - char temp_file_name[L_tmpnam]; - if (tmpnam(temp_file_name) == nullptr) { - LUISA_ERROR_WITH_LOCATION( - "Failed to get temp file name for CUDA compiler."); - } - auto temp_file = fopen(temp_file_name, "wb+"); + auto temp_file = tmpfile(); LUISA_ASSERT(temp_file != nullptr, - "Failed to create temp file '{}' for CUDA compiler.", - temp_file_name); + "Failed to create temp file for CUDA compiler."); // setup the options reproc::options o; @@ -78,7 +72,7 @@ namespace luisa::compute::cuda { write(src_filename); write(src); using namespace std::chrono_literals; - if (auto [exit_code, error] = p.wait(1024h/* almost forever */); exit_code || error) { + if (auto [exit_code, error] = p.wait(1024h /* almost forever */); exit_code || error) { LUISA_WARNING_WITH_LOCATION( "Failed to terminate the process: {} (exit code = {}).", error.message(), exit_code); @@ -99,14 +93,7 @@ namespace luisa::compute::cuda { "The CUDA kernel might be incomplete."); } if (fclose(temp_file) != 0) { - LUISA_WARNING_WITH_LOCATION( - "Failed to close temp file '{}'.", - temp_file_name); - } - if (std::error_code ec; !std::filesystem::remove(temp_file_name, ec)) { - LUISA_WARNING_WITH_LOCATION( - "Failed to remove temp file '{}': {}.", - temp_file_name, ec.message()); + LUISA_WARNING_WITH_LOCATION("Failed to close temp file."); } return buffer; }