Skip to content

Commit

Permalink
use tmpfile to make cuda backend safer
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-Leo-Smith committed Jan 6, 2025
1 parent 936cb35 commit fa06e7e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
15 changes: 15 additions & 0 deletions include/luisa/backends/ext/tex_compress_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

#include <luisa/runtime/rhi/device_interface.h>
#include <luisa/runtime/image.h>

namespace luisa::compute {

template<typename T>
class BufferView;

class Stream;

template<typename T>
class Image;

Expand All @@ -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<float> &src,
Expand Down
21 changes: 4 additions & 17 deletions src/backends/cuda/cuda_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down

0 comments on commit fa06e7e

Please sign in to comment.