Skip to content

Commit f4f535e

Browse files
authored
[SYCL][Test] Fix mock calls in render-group test (#19685)
Enhance the mock library used in the render-group SYCL test to support both standard and large file variants (glob, glob64, open, open64, globfree, globfree64). When large file support is enabled in the build, the mock library now correctly provides the required "*64" symbols. Additionally, if files are not `/dev/dri/renderD*` then fallback to the real library functions, preserving rest of the sycl-ls functionality. This improves reliability across different build configurations.
1 parent c7ff7ef commit f4f535e

File tree

1 file changed

+136
-33
lines changed

1 file changed

+136
-33
lines changed
Lines changed: 136 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,167 @@
11
#define _GNU_SOURCE
22

3+
#include <assert.h>
4+
#include <dlfcn.h>
35
#include <errno.h>
46
#include <glob.h>
57
#include <stdarg.h>
68
#include <stdlib.h>
79
#include <string.h>
810

11+
const char *renderd = "/dev/dri/renderD*";
12+
const char *mock_render_dirs[2] = {"/dev/dri/renderD128",
13+
"/dev/dri/renderD129"};
14+
915
int glob(const char *pattern, int flags, int (*errfunc)(const char *, int),
1016
glob_t *pglob) {
1117
const char *mock_mode = getenv("MOCK_GLOB_MODE");
12-
if (mock_mode && strcmp(mock_mode, "exists") == 0) {
13-
// Simulate that /dev/dri/renderD* exists
14-
pglob->gl_pathc = 2;
15-
pglob->gl_pathv = malloc(2 * sizeof(char *));
16-
pglob->gl_pathv[0] = strdup("/dev/dri/renderD128");
17-
pglob->gl_pathv[1] = strdup("/dev/dri/renderD129");
18+
if (!mock_mode || (strcmp(pattern, renderd) != 0)) {
19+
// Delegate to real glob
20+
int (*real_glob)(const char *, int, int (*)(const char *, int), glob_t *);
21+
real_glob = dlsym(RTLD_NEXT, "glob");
22+
if (!real_glob) {
23+
errno = ENOSYS;
24+
return -1;
25+
}
26+
return real_glob(pattern, flags, errfunc, pglob);
27+
}
28+
if (strcmp(mock_mode, "notfound") == 0) {
29+
pglob->gl_pathc = 0;
30+
pglob->gl_pathv = NULL;
1831
return 0;
1932
}
20-
// Default behavior: no matches
21-
pglob->gl_pathc = 0;
22-
pglob->gl_pathv = NULL;
33+
assert(strcmp(mock_mode, "exists") == 0);
34+
pglob->gl_pathc = 2;
35+
pglob->gl_pathv = mock_render_dirs;
2336
return 0;
2437
}
2538

26-
void globfree(glob_t *pglob) {
27-
if (pglob->gl_pathv) {
28-
for (size_t i = 0; i < pglob->gl_pathc; ++i) {
29-
free(pglob->gl_pathv[i]);
39+
int glob64(const char *pattern, int flags, int (*errfunc)(const char *, int),
40+
glob64_t *pglob) {
41+
const char *mock_mode = getenv("MOCK_GLOB_MODE");
42+
if (!mock_mode || (strcmp(pattern, renderd) != 0)) {
43+
// Delegate to real glob64
44+
int (*real_glob64)(const char *, int, int (*)(const char *, int),
45+
glob64_t *);
46+
real_glob64 = dlsym(RTLD_NEXT, "glob64");
47+
if (!real_glob64) {
48+
errno = ENOSYS;
49+
return -1;
3050
}
31-
free(pglob->gl_pathv);
51+
return real_glob64(pattern, flags, errfunc, pglob);
52+
}
53+
if (strcmp(mock_mode, "notfound") == 0) {
54+
pglob->gl_pathc = 0;
55+
pglob->gl_pathv = NULL;
56+
return 0;
57+
}
58+
assert(strcmp(mock_mode, "exists") == 0);
59+
pglob->gl_pathc = 2;
60+
pglob->gl_pathv = mock_render_dirs;
61+
return 0;
62+
}
63+
64+
void globfree(glob_t *pglob) {
65+
if (pglob->gl_pathv == mock_render_dirs) {
3266
pglob->gl_pathv = NULL;
3367
pglob->gl_pathc = 0;
68+
return;
3469
}
70+
// Default behavior: call real globfree
71+
void (*real_globfree)(glob_t *);
72+
real_globfree = dlsym(RTLD_NEXT, "globfree");
73+
if (!real_globfree) {
74+
errno = ENOSYS;
75+
return;
76+
}
77+
real_globfree(pglob);
3578
}
3679

37-
int open(const char *pathname, int flags, ...) {
80+
void globfree64(glob64_t *pglob) {
81+
if (pglob->gl_pathv == mock_render_dirs) {
82+
pglob->gl_pathv = NULL;
83+
pglob->gl_pathc = 0;
84+
return;
85+
}
86+
void (*real_globfree64)(glob64_t *);
87+
real_globfree64 = dlsym(RTLD_NEXT, "globfree64");
88+
if (!real_globfree64) {
89+
errno = ENOSYS;
90+
return;
91+
}
92+
real_globfree64(pglob);
93+
}
94+
95+
#define DUMMY_FD_128 128
96+
#define DUMMY_FD_129 129
97+
98+
int mock_open_helper(const char *pathname) {
3899
const char *mock_mode = getenv("MOCK_OPEN_MODE");
39-
if (strstr(pathname, "renderD12")) {
40-
if (mock_mode && strcmp(mock_mode, "deny") == 0) {
100+
assert(mock_mode != NULL && "MOCK_OPEN_MODE environment variable is not set");
101+
if (strcmp(mock_mode, "deny") == 0) {
102+
errno = EACCES;
103+
return -1;
104+
}
105+
106+
if (pathname == mock_render_dirs[0])
107+
return DUMMY_FD_128;
108+
109+
if (pathname == mock_render_dirs[1]) {
110+
if (mock_mode && strcmp(mock_mode, "deny_second") == 0) {
41111
errno = EACCES;
42112
return -1;
43113
}
114+
return DUMMY_FD_129;
115+
}
116+
assert(0 && "Unexpected pathname in mock_open_helper");
117+
}
44118

45-
if (mock_mode && strcmp(mock_mode, "deny_second") == 0) {
46-
// Simulate that the second file is not accessible
47-
if (strstr(pathname, "renderD129")) {
48-
errno = EACCES;
49-
return -1;
50-
} else {
51-
return 3;
52-
}
53-
}
119+
int open(const char *pathname, int flags, ...) {
120+
if (pathname == mock_render_dirs[0] || pathname == mock_render_dirs[1])
121+
return mock_open_helper(pathname);
54122

55-
if (mock_mode && strcmp(mock_mode, "allow") == 0) {
56-
return 3; // Dummy fd
57-
}
123+
// Call the real open function if not renderD12*.
124+
va_list ap;
125+
va_start(ap, flags);
126+
mode_t mode = va_arg(ap, mode_t);
127+
va_end(ap);
128+
int (*real_open)(const char *, int, ...);
129+
real_open = dlsym(RTLD_NEXT, "open");
130+
if (!real_open) {
131+
errno = ENOSYS;
132+
return -1;
133+
}
134+
return real_open(pathname, flags, mode);
135+
}
136+
137+
int open64(const char *pathname, int flags, ...) {
138+
if (pathname == mock_render_dirs[0] || pathname == mock_render_dirs[1])
139+
return mock_open_helper(pathname);
140+
141+
// Call the real open function if not renderD12*.
142+
va_list ap;
143+
va_start(ap, flags);
144+
mode_t mode = va_arg(ap, mode_t);
145+
va_end(ap);
146+
int (*real_open64)(const char *, int, ...);
147+
real_open64 = dlsym(RTLD_NEXT, "open64");
148+
if (!real_open64) {
149+
errno = ENOSYS;
150+
return -1;
58151
}
59-
// Default: permission denied
60-
errno = EACCES;
61-
return -1;
152+
return real_open64(pathname, flags, mode);
62153
}
63154

64-
int close(int fd) { return 0; }
155+
int close(int fd) {
156+
if (fd == DUMMY_FD_128 || fd == DUMMY_FD_129) {
157+
// Mock close for our dummy file descriptors.
158+
return 0;
159+
}
160+
161+
int (*real_close)(int) = dlsym(RTLD_NEXT, "close");
162+
if (!real_close) {
163+
errno = ENOSYS;
164+
return -1;
165+
}
166+
return real_close(fd);
167+
}

0 commit comments

Comments
 (0)