Skip to content

Commit e226aff

Browse files
authored
Merge pull request #937 from ldorau/Add_tests_for_utils_posix_common.c
Add tests for utils_posix_common.c
2 parents 8e34fb0 + c1cb0d2 commit e226aff

File tree

4 files changed

+115
-8
lines changed

4 files changed

+115
-8
lines changed

src/utils/utils_common.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ extern "C" {
2323
#endif
2424

2525
typedef enum umf_purge_advise_t {
26-
UMF_PURGE_LAZY,
26+
UMF_PURGE_LAZY = 1,
2727
UMF_PURGE_FORCE,
28+
29+
UMF_PURGE_MAX, // must be the last one
2830
} umf_purge_advise_t;
2931

3032
#define DO_WHILE_EMPTY \
@@ -117,6 +119,8 @@ int utils_gettid(void);
117119
// close file descriptor
118120
int utils_close_fd(int fd);
119121

122+
umf_result_t utils_errno_to_umf_result(int err);
123+
120124
// obtain a duplicate of another process's file descriptor
121125
umf_result_t utils_duplicate_fd(int pid, int fd_in, int *fd_out);
122126

@@ -130,6 +134,8 @@ umf_result_t utils_translate_flags(unsigned in_flags, unsigned max,
130134
umf_result_t utils_translate_mem_protection_flags(unsigned in_protection,
131135
unsigned *out_protection);
132136

137+
int utils_translate_purge_advise(umf_purge_advise_t advise);
138+
133139
umf_result_t
134140
utils_translate_mem_visibility_flag(umf_memory_visibility_t in_flag,
135141
unsigned *out_flag);

src/utils/utils_posix_common.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ int utils_gettid(void) {
6464

6565
int utils_close_fd(int fd) { return close(fd); }
6666

67-
#ifndef __APPLE__
68-
static umf_result_t errno_to_umf_result(int err) {
67+
umf_result_t utils_errno_to_umf_result(int err) {
6968
switch (err) {
7069
case EBADF:
7170
case EINVAL:
@@ -83,7 +82,6 @@ static umf_result_t errno_to_umf_result(int err) {
8382
return UMF_RESULT_ERROR_UNKNOWN;
8483
}
8584
}
86-
#endif
8785

8886
umf_result_t utils_duplicate_fd(int pid, int fd_in, int *fd_out) {
8987
#ifdef __APPLE__
@@ -102,14 +100,14 @@ umf_result_t utils_duplicate_fd(int pid, int fd_in, int *fd_out) {
102100
int pid_fd = syscall(__NR_pidfd_open, pid, 0);
103101
if (pid_fd == -1) {
104102
LOG_PERR("__NR_pidfd_open");
105-
return errno_to_umf_result(errno);
103+
return utils_errno_to_umf_result(errno);
106104
}
107105

108106
int fd_dup = syscall(__NR_pidfd_getfd, pid_fd, fd_in, 0);
109107
close(pid_fd);
110108
if (fd_dup == -1) {
111109
LOG_PERR("__NR_pidfd_open");
112-
return errno_to_umf_result(errno);
110+
return utils_errno_to_umf_result(errno);
113111
}
114112

115113
*fd_out = fd_dup;
@@ -147,14 +145,15 @@ umf_result_t utils_translate_mem_protection_flags(unsigned in_protection,
147145
out_protection);
148146
}
149147

150-
static int utils_translate_purge_advise(umf_purge_advise_t advise) {
148+
int utils_translate_purge_advise(umf_purge_advise_t advise) {
151149
switch (advise) {
152150
case UMF_PURGE_LAZY:
153151
return MADV_FREE;
154152
case UMF_PURGE_FORCE:
155153
return MADV_DONTNEED;
154+
default:
155+
return -1;
156156
}
157-
return -1;
158157
}
159158

160159
void *utils_mmap(void *hint_addr, size_t length, int prot, int flag, int fd,

src/utils/utils_windows_common.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ int utils_close_fd(int fd) {
4545
return -1;
4646
}
4747

48+
umf_result_t utils_errno_to_umf_result(int err) {
49+
(void)err; // unused
50+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
51+
}
52+
4853
umf_result_t utils_duplicate_fd(int pid, int fd_in, int *fd_out) {
4954
(void)pid; // unused
5055
(void)fd_in; // unused
@@ -87,6 +92,11 @@ umf_result_t utils_translate_mem_protection_flags(unsigned in_protection,
8792
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
8893
}
8994

95+
int utils_translate_purge_advise(umf_purge_advise_t advise) {
96+
(void)advise; // unused
97+
return -1;
98+
}
99+
90100
umf_result_t
91101
utils_translate_mem_visibility_flag(umf_memory_visibility_t in_flag,
92102
unsigned *out_flag) {

test/utils/utils_linux.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
33
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44

5+
#include <sys/mman.h>
6+
57
#include "base.hpp"
68
#include "utils/utils_common.h"
79

@@ -77,3 +79,93 @@ TEST_F(test, utils_get_size_threshold) {
7779
EXPECT_EQ(utils_get_size_threshold((char *)"size.threshold=abc"), -1);
7880
EXPECT_EQ(utils_get_size_threshold((char *)"size.threshold=-111"), -1);
7981
}
82+
83+
TEST_F(test, utils_errno_to_umf_result) {
84+
EXPECT_EQ(utils_errno_to_umf_result(EBADF),
85+
UMF_RESULT_ERROR_INVALID_ARGUMENT);
86+
EXPECT_EQ(utils_errno_to_umf_result(EINVAL),
87+
UMF_RESULT_ERROR_INVALID_ARGUMENT);
88+
EXPECT_EQ(utils_errno_to_umf_result(ESRCH),
89+
UMF_RESULT_ERROR_INVALID_ARGUMENT);
90+
EXPECT_EQ(utils_errno_to_umf_result(EPERM),
91+
UMF_RESULT_ERROR_INVALID_ARGUMENT);
92+
93+
EXPECT_EQ(utils_errno_to_umf_result(EMFILE),
94+
UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY);
95+
EXPECT_EQ(utils_errno_to_umf_result(ENOMEM),
96+
UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY);
97+
98+
EXPECT_EQ(utils_errno_to_umf_result(ENODEV),
99+
UMF_RESULT_ERROR_NOT_SUPPORTED);
100+
EXPECT_EQ(utils_errno_to_umf_result(ENOSYS),
101+
UMF_RESULT_ERROR_NOT_SUPPORTED);
102+
EXPECT_EQ(utils_errno_to_umf_result(ENOTSUP),
103+
UMF_RESULT_ERROR_NOT_SUPPORTED);
104+
105+
EXPECT_EQ(utils_errno_to_umf_result(E2BIG), UMF_RESULT_ERROR_UNKNOWN);
106+
}
107+
108+
TEST_F(test, utils_translate_mem_protection_flags) {
109+
umf_result_t umf_result;
110+
unsigned out_protection;
111+
112+
umf_result = utils_translate_mem_protection_flags(UMF_PROTECTION_NONE,
113+
&out_protection);
114+
EXPECT_EQ(umf_result, UMF_RESULT_SUCCESS);
115+
EXPECT_EQ(out_protection, PROT_NONE);
116+
117+
umf_result = utils_translate_mem_protection_flags(UMF_PROTECTION_READ,
118+
&out_protection);
119+
EXPECT_EQ(umf_result, UMF_RESULT_SUCCESS);
120+
EXPECT_EQ(out_protection, PROT_READ);
121+
122+
umf_result = utils_translate_mem_protection_flags(UMF_PROTECTION_WRITE,
123+
&out_protection);
124+
EXPECT_EQ(umf_result, UMF_RESULT_SUCCESS);
125+
EXPECT_EQ(out_protection, PROT_WRITE);
126+
127+
umf_result = utils_translate_mem_protection_flags(UMF_PROTECTION_EXEC,
128+
&out_protection);
129+
EXPECT_EQ(umf_result, UMF_RESULT_SUCCESS);
130+
EXPECT_EQ(out_protection, PROT_EXEC);
131+
132+
umf_result = utils_translate_mem_protection_flags(
133+
UMF_PROTECTION_READ | UMF_PROTECTION_WRITE, &out_protection);
134+
EXPECT_EQ(umf_result, UMF_RESULT_SUCCESS);
135+
EXPECT_EQ(out_protection, PROT_READ | PROT_WRITE);
136+
137+
umf_result = utils_translate_mem_protection_flags(
138+
UMF_PROTECTION_READ | UMF_PROTECTION_WRITE | UMF_PROTECTION_EXEC,
139+
&out_protection);
140+
EXPECT_EQ(umf_result, UMF_RESULT_SUCCESS);
141+
EXPECT_EQ(out_protection, PROT_READ | PROT_WRITE | PROT_EXEC);
142+
143+
umf_result = utils_translate_mem_protection_flags(
144+
UMF_PROTECTION_READ | UMF_PROTECTION_EXEC, &out_protection);
145+
EXPECT_EQ(umf_result, UMF_RESULT_SUCCESS);
146+
EXPECT_EQ(out_protection, PROT_READ | PROT_EXEC);
147+
148+
umf_result = utils_translate_mem_protection_flags(
149+
UMF_PROTECTION_WRITE | UMF_PROTECTION_EXEC, &out_protection);
150+
EXPECT_EQ(umf_result, UMF_RESULT_SUCCESS);
151+
EXPECT_EQ(out_protection, PROT_WRITE | PROT_EXEC);
152+
153+
// see https://github.com/oneapi-src/unified-memory-framework/issues/923
154+
out_protection = 0;
155+
umf_result = utils_translate_mem_protection_flags(
156+
0xFFFF & ~(((UMF_PROTECTION_MAX - 1) << 1) - 1), &out_protection);
157+
EXPECT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT);
158+
EXPECT_EQ(out_protection, 0);
159+
}
160+
161+
TEST_F(test, utils_translate_purge_advise) {
162+
EXPECT_EQ(utils_translate_purge_advise(UMF_PURGE_LAZY), MADV_FREE);
163+
EXPECT_EQ(utils_translate_purge_advise(UMF_PURGE_FORCE), MADV_DONTNEED);
164+
EXPECT_EQ(utils_translate_purge_advise(UMF_PURGE_MAX), -1);
165+
}
166+
167+
TEST_F(test, utils_open) {
168+
EXPECT_EQ(utils_devdax_open(NULL), -1);
169+
EXPECT_EQ(utils_file_open(NULL), -1);
170+
EXPECT_EQ(utils_file_open_or_create(NULL), -1);
171+
}

0 commit comments

Comments
 (0)