Skip to content

[SYCL][Test] Fix mock calls in render-group test #19685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: sycl
Choose a base branch
from
Open
Changes from all 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
108 changes: 87 additions & 21 deletions sycl/test/tools/Inputs/mock_renderd_access.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#define _GNU_SOURCE

#include <assert.h>
#include <dlfcn.h>
#include <errno.h>
#include <glob.h>
#include <stdarg.h>
Expand All @@ -10,7 +12,22 @@ int glob(const char *pattern, int flags, int (*errfunc)(const char *, int),
glob_t *pglob) {
const char *mock_mode = getenv("MOCK_GLOB_MODE");
if (mock_mode && strcmp(mock_mode, "exists") == 0) {
// Simulate that /dev/dri/renderD* exists
pglob->gl_pathc = 2;
pglob->gl_pathv = malloc(2 * sizeof(char *));
pglob->gl_pathv[0] = strdup("/dev/dri/renderD128");
pglob->gl_pathv[1] = strdup("/dev/dri/renderD129");
return 0;
}
// Default behavior: no matches
pglob->gl_pathc = 0;
pglob->gl_pathv = NULL;
return 0;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we apply

Additionally, if files are not /dev/dri/renderD* then fallback to the real library functions, preserving rest of the sycl-ls functionality.

here as well?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also move "/dev/dri/renderD128" to a global constant pointer, so that we could simply compare pglob->gl_pathv[0] == global_render_d128_const in the globfree to decide if we need to delegate it to a real implementation or not.


int glob64(const char *pattern, int flags, int (*errfunc)(const char *, int),
glob64_t *pglob) {
const char *mock_mode = getenv("MOCK_GLOB_MODE");
if (mock_mode && strcmp(mock_mode, "exists") == 0) {
pglob->gl_pathc = 2;
pglob->gl_pathv = malloc(2 * sizeof(char *));
pglob->gl_pathv[0] = strdup("/dev/dri/renderD128");
Expand All @@ -34,31 +51,80 @@ void globfree(glob_t *pglob) {
}
}

int open(const char *pathname, int flags, ...) {
void globfree64(glob64_t *pglob) {
if (pglob->gl_pathv) {
for (size_t i = 0; i < pglob->gl_pathc; ++i) {
free(pglob->gl_pathv[i]);
}
free(pglob->gl_pathv);
pglob->gl_pathv = NULL;
pglob->gl_pathc = 0;
}
}

static int (*real_open)(const char *, int, ...) = NULL;
static int (*real_open64)(const char *, int, ...) = NULL;

#define DUMMY_FD_128 128
#define DUMMY_FD_129 129

int mock_open_helper(const char *pathname) {
const char *mock_mode = getenv("MOCK_OPEN_MODE");
if (strstr(pathname, "renderD12")) {
if (mock_mode && strcmp(mock_mode, "deny") == 0) {
assert(mock_mode != NULL && "MOCK_OPEN_MODE environment variable is not set");
if (strcmp(mock_mode, "deny") == 0) {
errno = EACCES;
return -1;
}

if (strstr(pathname, "renderD128"))
return DUMMY_FD_128;

if (strstr(pathname, "renderD129")) {
if (mock_mode && strcmp(mock_mode, "deny_second") == 0) {
errno = EACCES;
return -1;
}
return DUMMY_FD_129;
}
assert(0 && "Unexpected pathname in mock_open_helper");
}

if (mock_mode && strcmp(mock_mode, "deny_second") == 0) {
// Simulate that the second file is not accessible
if (strstr(pathname, "renderD129")) {
errno = EACCES;
return -1;
} else {
return 3;
}
}
int open(const char *pathname, int flags, ...) {
if (strstr(pathname, "renderD12"))
return mock_open_helper(pathname);

if (mock_mode && strcmp(mock_mode, "allow") == 0) {
return 3; // Dummy fd
}
}
// Default: permission denied
errno = EACCES;
return -1;
// Call the real open function if not renderD12*.
va_list ap;
va_start(ap, flags);
mode_t mode = va_arg(ap, mode_t);
va_end(ap);
if (!real_open)
real_open = dlsym(RTLD_NEXT, "open");
return real_open(pathname, flags, mode);
}

int open64(const char *pathname, int flags, ...) {
if (strstr(pathname, "renderD12"))
return mock_open_helper(pathname);

// Call the real open function if not renderD12*.
va_list ap;
va_start(ap, flags);
mode_t mode = va_arg(ap, mode_t);
va_end(ap);
if (!real_open64)
real_open64 = dlsym(RTLD_NEXT, "open64");
return real_open64(pathname, flags, mode);
}

int close(int fd) { return 0; }
static int (*real_close)(int) = NULL;

int close(int fd) {
if (fd == DUMMY_FD_128 || fd == DUMMY_FD_129) {
// Mock close for our dummy file descriptors.
return 0;
}

int (*real_close)(int) = dlsym(RTLD_NEXT, "close");
return real_close(fd);
}
Loading