Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,32 @@
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <string>

#ifdef _WIN32
#include <process.h>
#define getpid _getpid
#else
#include <unistd.h>
#endif

#include <hip/hip_runtime_api.h>
#include <hip_test_common.hh>
#include <contract_cleanup.hh>

namespace {
constexpr size_t kByteCount = 64;

// Builds a unique temporary file path for the exported dot graph so concurrent
// test binaries do not collide on a shared filename.
std::string UniqueDotPath() {
// Use the temp directory because installed test directories may be read-only.
std::string DotPath() {
int device = 0;
HIP_CHECK(hipGetDevice(&device));
return std::string("hip_contract_graph_debug_") + std::to_string(device) + "_" +
std::to_string(static_cast<long long>(reinterpret_cast<intptr_t>(&device))) + ".dot";
return (std::filesystem::temp_directory_path() /
("hip_contract_graph_debug_" + std::to_string(device) + "_" +
std::to_string(getpid()) + ".dot"))
.string();
Comment thread
iassiour marked this conversation as resolved.
Resolved
}

hipMemsetParams MakeByteMemsetParams(void* device_ptr, unsigned int value) {
Expand Down Expand Up @@ -61,7 +70,7 @@ HIP_TEST_CASE(Contract_GraphDebug_HipGraphDebugDotPrint_Default_WritesNonEmptyFi
hipMemsetParams memset_params = MakeByteMemsetParams(device_ptr, 0x5A);
HIP_CHECK(hipGraphAddMemsetNode(&node, graph, nullptr, 0, &memset_params));

const std::string path = UniqueDotPath();
const std::string path = DotPath();
std::remove(path.c_str());

// Exporting a non-empty graph to a dot file must succeed (or report the
Expand All @@ -87,7 +96,7 @@ HIP_TEST_CASE(Contract_GraphDebug_HipGraphDebugDotPrint_Default_VerboseFlagIsAcc
cleanup.Add([graph] { (void)hipGraphDestroy(graph); });
HIP_CHECK(hipGraphAddEmptyNode(&node, graph, nullptr, 0));

const std::string path = UniqueDotPath();
const std::string path = DotPath();
std::remove(path.c_str());

// The verbose flag augments the output but must not change the success
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@
#include <hip_test_common.hh>

#include <cstdio>
#include <filesystem>
#include <fstream>
#include <string>
#include <vector>

#ifdef _WIN32
#include <process.h>
#define getpid _getpid
#else
#include <unistd.h>
#endif

// The library-from-file and managed-symbol APIs (hipLibraryLoadFromFile,
// hipLibraryGetManaged) are exercised on both backends: on NVIDIA they map to
// the CUDA driver cuLibrary* entry points.
Expand Down Expand Up @@ -68,15 +76,18 @@ bool CompileLibrarySource(std::vector<char>& code) {
return true;
}

// Compiles the library source and writes it to a unique per-test file in the
// current working directory, returning the path. Skips when HIPRTC is
// unavailable. The caller must remove the file when done.
// Compile to a temp file: installed test directories may be read-only.
// Returns the path; the caller must remove the file.
std::string WriteCodeObjectFile(const char* suffix) {
std::vector<char> code;
if (!CompileLibrarySource(code)) {
HIP_SKIP_TEST("HIPRTC compilation is not supported by this device/runtime path.");
}
const std::string path = std::string("hip-contract-library-file-") + suffix + ".code";
const std::string path =
(std::filesystem::temp_directory_path() /
("hip-contract-library-file-" + std::string(suffix) + "-" +
std::to_string(getpid()) + ".code"))
.string();
Comment thread
iassiour marked this conversation as resolved.
Resolved
std::ofstream out(path, std::ios::binary);
REQUIRE(out.is_open());
out.write(code.data(), static_cast<std::streamsize>(code.size()));
Expand Down
27 changes: 22 additions & 5 deletions projects/hip-tests/catch/unit/memory/mempool_common.hh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <utils.hh>

#include <chrono>
#include <filesystem>
#include <thread>

namespace {
Expand Down Expand Up @@ -628,6 +629,13 @@ struct ipcHdl {
char *name;
};

#if !HT_WIN
// IPC socket path in the temp directory so tests work from read-only cwd.
inline std::string ipcSocketPath(pid_t pid) {
return (std::filesystem::temp_directory_path() / std::to_string(pid)).string();
}
Comment thread
Copilot marked this conversation as resolved.
Resolved
Comment thread
iassiour marked this conversation as resolved.
Resolved
#endif

class ipcSocketCom {
ipcHdl *handle;

Expand All @@ -645,8 +653,8 @@ class ipcSocketCom {
int server_fd;
struct sockaddr_un servaddr;

char name[16];
sprintf(name, "%u", getpid());
std::string nameStr = ipcSocketPath(getpid());
const char *name = nameStr.c_str();

handle = new ipcHdl;
if (nullptr == handle) {
Expand Down Expand Up @@ -729,10 +737,14 @@ class ipcSocketCom {

bzero(&cliaddr, sizeof(cliaddr));
cliaddr.sun_family = AF_UNIX;
char name[16];

sprintf(name, "%u", getpid());
std::string nameStr = ipcSocketPath(getpid());
const char *name = nameStr.c_str();

if (strlen(name) > (sizeof(cliaddr.sun_path) - 1)) {
perror("Socket failure: Cannot bind provided name to socket. Name too large");
Comment thread
Copilot marked this conversation as resolved.
Resolved
return -1;
}
strcpy(cliaddr.sun_path, name);
if (bind(sock, (struct sockaddr *)&cliaddr, sizeof(cliaddr)) < 0) {
perror("Socket failure: Binding socket failed");
Expand Down Expand Up @@ -892,7 +904,12 @@ public:

bzero(&cliaddr, sizeof(cliaddr));
cliaddr.sun_family = AF_UNIX;
strcpy(cliaddr.sun_path, std::to_string(process).c_str());
std::string destPath = ipcSocketPath(process);
if (destPath.size() > (sizeof(cliaddr.sun_path) - 1)) {
perror("Socket failure: Cannot address socket. Name too large");
return -1;
}
strcpy(cliaddr.sun_path, destPath.c_str());

int sendfd = (int)shareableHdl;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <memory.h>
#include <sys/un.h>
#endif
#include <filesystem>
#include "hip_test_context.hh"

#define checkVMMSupported(device) \
Expand Down Expand Up @@ -71,16 +72,22 @@ struct ipcHdl {
char *name;
};

#ifdef __linux__
// IPC socket path in the temp directory so tests work from read-only cwd.
inline std::string ipcSocketPath(pid_t pid) {
return (std::filesystem::temp_directory_path() / std::to_string(pid)).string();
}
Comment thread
Copilot marked this conversation as resolved.
Resolved
Comment thread
iassiour marked this conversation as resolved.
Resolved
#endif

class ipcSocketCom {
ipcHdl *handle;
// method to create socket from server
int createSocket() {
int server_fd;
struct sockaddr_un servaddr;

char name[16];
// Create a unique socket name based on current pid
sprintf(name, "%u", getpid());
std::string nameStr = ipcSocketPath(getpid());
const char *name = nameStr.c_str();

// Create the socket handle
handle = new ipcHdl;
Expand Down Expand Up @@ -140,11 +147,14 @@ class ipcSocketCom {

bzero(&cliaddr, sizeof(cliaddr));
cliaddr.sun_family = AF_UNIX;
char name[16];

// Create a unique socket name based on current process id.
sprintf(name, "%u", getpid());
std::string nameStr = ipcSocketPath(getpid());
const char *name = nameStr.c_str();

if (strlen(name) > (sizeof(cliaddr.sun_path) - 1)) {
perror("Socket failure: Cannot bind provided name to socket. Name too large");
Comment thread
Copilot marked this conversation as resolved.
Resolved
return -1;
}
strcpy(cliaddr.sun_path, name);
if (bind(sock, (struct sockaddr *)&cliaddr, sizeof(cliaddr)) < 0) {
perror("Socket failure: Binding socket failed");
Expand Down Expand Up @@ -242,7 +252,12 @@ public:
// Construct client address to send this SHareable handle to
bzero(&cliaddr, sizeof(cliaddr));
cliaddr.sun_family = AF_UNIX;
strcpy(cliaddr.sun_path, std::to_string(process).c_str());
std::string destPath = ipcSocketPath(process);
if (destPath.size() > (sizeof(cliaddr.sun_path) - 1)) {
perror("Socket failure: Cannot address socket. Name too large");
return -1;
}
strcpy(cliaddr.sun_path, destPath.c_str());

// Send corresponding shareable handle to the client
int sendfd = (int)shareableHdl;
Expand Down
Loading