blob: 3c27dcb9e32853aa6b5ba36086fbaf2f257d75e8 [file] [log] [blame]
Orion Hodson563ada22018-09-04 11:28:31 +01001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_LIBARTBASE_BASE_MEMFD_H_
18#define ART_LIBARTBASE_BASE_MEMFD_H_
19
Nicolas Geoffraye3884e32019-10-28 17:04:49 +000020#include <fcntl.h>
21#include <unistd.h>
22
Nicolas Geoffray2411f492019-06-14 08:54:46 +010023#if defined(__BIONIC__)
24#include <linux/memfd.h> // To access memfd flags.
Nicolas Geoffraye3884e32019-10-28 17:04:49 +000025#else
26
27// If memfd flags don't exist in the current toolchain, define them ourselves.
28#ifndef F_ADD_SEALS
29# define F_ADD_SEALS (1033)
30#endif
31
32#ifndef F_GET_SEALS
33# define F_GET_SEALS (1034)
34#endif
35
36#ifndef F_SEAL_SEAL
37# define F_SEAL_SEAL 0x0001
38#endif
39
40#ifndef F_SEAL_SHRINK
41# define F_SEAL_SHRINK 0x0002
42#endif
43
44#ifndef F_SEAL_GROW
45# define F_SEAL_GROW 0x0004
46#endif
47
48#ifndef F_SEAL_WRITE
49# define F_SEAL_WRITE 0x0008
50#endif
51
52#ifndef F_SEAL_FUTURE_WRITE
53# define F_SEAL_FUTURE_WRITE 0x0010
54#endif
55
Hu Wang3326fce2021-08-23 17:41:57 +080056#ifndef MFD_CLOEXEC
57# define MFD_CLOEXEC 0x0001U
58#endif
59
Nicolas Geoffraye3884e32019-10-28 17:04:49 +000060#ifndef MFD_ALLOW_SEALING
61# define MFD_ALLOW_SEALING 0x0002U
62#endif
63
Nicolas Geoffray2411f492019-06-14 08:54:46 +010064#endif
65
Orion Hodson563ada22018-09-04 11:28:31 +010066namespace art {
67
Orion Hodson51f89d92018-09-27 12:51:41 +010068// Call memfd(2) if available on platform and return result. This call also makes a kernel version
69// check for safety on older kernels (b/116769556)..
Orion Hodson563ada22018-09-04 11:28:31 +010070int memfd_create(const char* name, unsigned int flags);
71
Alex Light639e73b2019-05-17 21:44:36 +000072// Call memfd(2) if available on platform and return result. Try to give us an unlinked FD in some
73// other way if memfd fails or isn't supported.
74int memfd_create_compat(const char* name, unsigned int flags);
75
Nicolas Geoffray3a614ea2019-06-27 15:47:09 +010076// Return whether the kernel supports sealing future writes of a memfd.
77bool IsSealFutureWriteSupported();
78
Orion Hodson563ada22018-09-04 11:28:31 +010079} // namespace art
80
81#endif // ART_LIBARTBASE_BASE_MEMFD_H_