1
1
/*
2
- * Copyright (C) 2023 Intel Corporation
2
+ * Copyright (C) 2023-2024 Intel Corporation
3
3
*
4
4
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
5
5
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
6
*/
7
7
8
- #include <assert.h>
9
8
#include <errno.h>
10
- #include <limits.h>
11
- #include <string.h>
12
9
#include <sys/mman.h>
13
10
#include <sys/syscall.h>
14
11
#include <unistd.h>
18
15
#include "provider_os_memory_internal.h"
19
16
#include "utils_log.h"
20
17
21
- // maximum value of the off_t type
22
- #define OFF_T_MAX \
23
- (sizeof(off_t) == sizeof(long long) \
24
- ? LLONG_MAX \
25
- : (sizeof(off_t) == sizeof(long) ? LONG_MAX : INT_MAX))
26
-
27
- umf_result_t os_translate_mem_protection_one_flag (unsigned in_protection ,
28
- unsigned * out_protection ) {
29
- switch (in_protection ) {
30
- case UMF_PROTECTION_NONE :
31
- * out_protection = PROT_NONE ;
32
- return UMF_RESULT_SUCCESS ;
33
- case UMF_PROTECTION_READ :
34
- * out_protection = PROT_READ ;
35
- return UMF_RESULT_SUCCESS ;
36
- case UMF_PROTECTION_WRITE :
37
- * out_protection = PROT_WRITE ;
38
- return UMF_RESULT_SUCCESS ;
39
- case UMF_PROTECTION_EXEC :
40
- * out_protection = PROT_EXEC ;
41
- return UMF_RESULT_SUCCESS ;
42
- }
43
- return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
44
- }
45
-
46
18
umf_result_t os_translate_mem_visibility_flag (umf_memory_visibility_t in_flag ,
47
19
unsigned * out_flag ) {
48
20
switch (in_flag ) {
49
21
case UMF_MEM_MAP_PRIVATE :
50
22
* out_flag = MAP_PRIVATE ;
51
23
return UMF_RESULT_SUCCESS ;
52
24
case UMF_MEM_MAP_SHARED :
53
- #ifdef __APPLE__
54
- return UMF_RESULT_ERROR_NOT_SUPPORTED ; // not supported on MacOSX
55
- #else
56
25
* out_flag = MAP_SHARED ;
57
26
return UMF_RESULT_SUCCESS ;
58
- #endif
59
27
}
60
28
return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
61
29
}
62
30
63
- #ifndef __APPLE__
64
31
static int syscall_memfd_secret (void ) {
65
32
int fd = -1 ;
66
33
#ifdef __NR_memfd_secret
@@ -90,14 +57,9 @@ static int syscall_memfd_create(void) {
90
57
#endif /* __NR_memfd_create */
91
58
return fd ;
92
59
}
93
- #endif /* __APPLE__ */
94
60
95
61
// create an anonymous file descriptor
96
62
int os_create_anonymous_fd (unsigned translated_memory_flag ) {
97
- #ifdef __APPLE__
98
- (void )translated_memory_flag ; // unused
99
- return 0 ; // ignored on MacOSX
100
- #else /* !__APPLE__ */
101
63
// fd is created only for MAP_SHARED
102
64
if (translated_memory_flag != MAP_SHARED ) {
103
65
return 0 ;
@@ -118,123 +80,20 @@ int os_create_anonymous_fd(unsigned translated_memory_flag) {
118
80
fd = syscall_memfd_create ();
119
81
120
82
#if !(defined __NR_memfd_secret ) && !(defined __NR_memfd_create )
121
- if (fd == = 1 ) {
83
+ if (fd == - 1 ) {
122
84
LOG_ERR ("cannot create an anonymous file descriptor - neither "
123
85
"memfd_secret() nor memfd_create() are defined" );
124
86
}
125
87
#endif /* !(defined __NR_memfd_secret) && !(defined __NR_memfd_create) */
126
88
127
89
return fd ;
128
-
129
- #endif /* !__APPLE__ */
130
90
}
131
91
132
- size_t get_max_file_size (void ) { return OFF_T_MAX ; }
133
-
134
92
int os_set_file_size (int fd , size_t size ) {
135
- #ifdef __APPLE__
136
- (void )fd ; // unused
137
- (void )size ; // unused
138
- return 0 ; // ignored on MacOSX
139
- #else
140
93
errno = 0 ;
141
94
int ret = ftruncate (fd , size );
142
95
if (ret ) {
143
96
LOG_PERR ("ftruncate(%i, %zu) failed" , fd , size );
144
97
}
145
98
return ret ;
146
- #endif /* __APPLE__ */
147
- }
148
-
149
- umf_result_t os_translate_mem_protection_flags (unsigned in_protection ,
150
- unsigned * out_protection ) {
151
- // translate protection - combination of 'umf_mem_protection_flags_t' flags
152
- return os_translate_flags (in_protection , UMF_PROTECTION_MAX ,
153
- os_translate_mem_protection_one_flag ,
154
- out_protection );
155
- }
156
-
157
- static int os_translate_purge_advise (umf_purge_advise_t advise ) {
158
- switch (advise ) {
159
- case UMF_PURGE_LAZY :
160
- return MADV_FREE ;
161
- case UMF_PURGE_FORCE :
162
- return MADV_DONTNEED ;
163
- }
164
- assert (0 );
165
- return -1 ;
166
- }
167
-
168
- void * os_mmap (void * hint_addr , size_t length , int prot , int flag , int fd ,
169
- size_t fd_offset ) {
170
- fd = (fd == 0 ) ? -1 : fd ;
171
- if (fd == -1 ) {
172
- // MAP_ANONYMOUS - the mapping is not backed by any file
173
- flag |= MAP_ANONYMOUS ;
174
- }
175
-
176
- void * ptr = mmap (hint_addr , length , prot , flag , fd , fd_offset );
177
- if (ptr == MAP_FAILED ) {
178
- return NULL ;
179
- }
180
-
181
- return ptr ;
182
- }
183
-
184
- int os_munmap (void * addr , size_t length ) { return munmap (addr , length ); }
185
-
186
- size_t os_get_page_size (void ) { return sysconf (_SC_PAGE_SIZE ); }
187
-
188
- int os_purge (void * addr , size_t length , int advice ) {
189
- return madvise (addr , length , os_translate_purge_advise (advice ));
190
- }
191
-
192
- void os_strerror (int errnum , char * buf , size_t buflen ) {
193
- strerror_r (errnum , buf , buflen );
194
- }
195
-
196
- int os_getpid (void ) { return getpid (); }
197
-
198
- umf_result_t os_duplicate_fd (int pid , int fd_in , int * fd_out ) {
199
- // pidfd_getfd(2) is used to obtain a duplicate of another process's file descriptor.
200
- // Permission to duplicate another process's file descriptor
201
- // is governed by a ptrace access mode PTRACE_MODE_ATTACH_REALCREDS check (see ptrace(2))
202
- // that can be changed using the /proc/sys/kernel/yama/ptrace_scope interface.
203
- // pidfd_getfd(2) is supported since Linux 5.6
204
- // pidfd_open(2) is supported since Linux 5.3
205
- #if defined(__NR_pidfd_open ) && defined(__NR_pidfd_getfd )
206
- errno = 0 ;
207
- int pid_fd = syscall (SYS_pidfd_open , pid , 0 );
208
- if (pid_fd == -1 ) {
209
- LOG_PDEBUG ("SYS_pidfd_open" );
210
- return UMF_RESULT_ERROR_UNKNOWN ;
211
- }
212
-
213
- int fd_dup = syscall (SYS_pidfd_getfd , pid_fd , fd_in , 0 );
214
- close (pid_fd );
215
- if (fd_dup == -1 ) {
216
- LOG_PDEBUG ("SYS_pidfd_getfd" );
217
- return UMF_RESULT_ERROR_UNKNOWN ;
218
- }
219
-
220
- * fd_out = fd_dup ;
221
-
222
- return UMF_RESULT_SUCCESS ;
223
- #else
224
- // TODO: find another way to obtain a duplicate of another process's file descriptor
225
- (void )pid ; // unused
226
- (void )fd_in ; // unused
227
- (void )fd_out ; // unused
228
- errno = ENOTSUP ;
229
- return UMF_RESULT_ERROR_NOT_SUPPORTED ; // unsupported
230
- #endif /* defined(__NR_pidfd_open) && defined(__NR_pidfd_getfd) */
231
- }
232
-
233
- umf_result_t os_close_fd (int fd ) {
234
- if (close (fd )) {
235
- LOG_PERR ("close() failed" );
236
- return UMF_RESULT_ERROR_UNKNOWN ;
237
- }
238
-
239
- return UMF_RESULT_SUCCESS ;
240
99
}
0 commit comments