Skip to content

Commit 52d39ee

Browse files
paskocopybara-github
authored andcommitted
android: cleanup: Rename shared memory functions
The existing function naming and documentation is riddled with historical cruft. Hopefully it gets better after the changes proposed here: * Remove the word "ashmem" from function names that are not necessarily backed by /dev/ashmem * Change function names to CamelCase to match Chromium C++ style * Inline tiny functions that are used only once * Add high level docs to functions declared in base/android/linker/ashmem.h Bypass-Check-License: AOSP copyright+license for ashmem.h Bug: None Change-Id: I41dcab9406142119b2943234bf4e0cf64d49d484 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7170448 Commit-Queue: Egor Pasko <[email protected]> Reviewed-by: Joe Mason <[email protected]> Cr-Commit-Position: refs/heads/main@{#1550464} NOKEYCHECK=True GitOrigin-RevId: 87f99867450bddb99725955ddb722b64d305ed59
1 parent 6bcf237 commit 52d39ee

File tree

5 files changed

+106
-108
lines changed

5 files changed

+106
-108
lines changed

android/linker/ashmem.cc

Lines changed: 43 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,21 @@ namespace {
5353
*/
5454

5555
/* Callback used with __system_property_read_callback. */
56-
void prop_read_int(void* cookie,
57-
const char* name,
58-
const char* value,
59-
uint32_t serial) {
56+
void ReadIntProperty(void* cookie,
57+
const char* name,
58+
const char* value,
59+
uint32_t serial) {
6060
*reinterpret_cast<int*>(cookie) = atoi(value);
6161
(void)name;
6262
(void)serial;
6363
}
6464

65-
int system_property_get_int(const char* name) {
65+
int SystemPropertyGetInt(const char* name) {
6666
int result = 0;
6767
if (__builtin_available(android 26, *)) {
6868
const prop_info* info = __system_property_find(name);
6969
if (info) {
70-
__system_property_read_callback(info, &prop_read_int, &result);
70+
__system_property_read_callback(info, &ReadIntProperty, &result);
7171
}
7272
} else {
7373
char value[PROP_VALUE_MAX] = {};
@@ -78,10 +78,10 @@ int system_property_get_int(const char* name) {
7878
return result;
7979
}
8080

81-
int vendor_api_level() {
81+
int VendorApiLevel() {
8282
static int v_api_level = -1;
8383
if (v_api_level < 0) {
84-
v_api_level = system_property_get_int("ro.vendor.api_level");
84+
v_api_level = SystemPropertyGetInt("ro.vendor.api_level");
8585
}
8686
return v_api_level;
8787
}
@@ -96,7 +96,7 @@ AshmemStatus s_ashmem_status = ASHMEM_STATUS_INIT;
9696
dev_t s_ashmem_dev;
9797

9898
/* Return the dev_t of a given file path, or 0 if not available, */
99-
dev_t ashmem_find_dev(const char* path) {
99+
dev_t FindAshmemDevice(const char* path) {
100100
struct stat st;
101101
dev_t result = 0;
102102
if (stat(path, &st) == 0 && S_ISCHR(st.st_mode)) {
@@ -105,48 +105,30 @@ dev_t ashmem_find_dev(const char* path) {
105105
return result;
106106
}
107107

108-
AshmemStatus ashmem_get_status() {
108+
AshmemStatus GetAshmemSupportStatus() {
109109
/* NOTE: No need to make this thread-safe, assuming that
110110
* all threads will find the same value. */
111111
if (s_ashmem_status != ASHMEM_STATUS_INIT) {
112112
return s_ashmem_status;
113113
}
114114

115-
s_ashmem_dev = ashmem_find_dev(ASHMEM_DEVICE);
115+
s_ashmem_dev = FindAshmemDevice(ASHMEM_DEVICE);
116116
s_ashmem_status = (s_ashmem_dev == 0) ? ASHMEM_STATUS_NOT_SUPPORTED
117117
: ASHMEM_STATUS_SUPPORTED;
118118
return s_ashmem_status;
119119
}
120120

121121
/* Returns true iff the ashmem device ioctl should be used for a given fd.
122122
* NOTE: Try not to use fstat() when possible to avoid performance issues. */
123-
bool is_ashmem_fd(int fd) {
124-
if (ashmem_get_status() == ASHMEM_STATUS_SUPPORTED) {
123+
bool IsAshmemFd(int fd) {
124+
if (GetAshmemSupportStatus() == ASHMEM_STATUS_SUPPORTED) {
125125
struct stat st;
126126
return (fstat(fd, &st) == 0 && S_ISCHR(st.st_mode) && st.st_dev != 0 &&
127127
st.st_dev == s_ashmem_dev);
128128
}
129129
return false;
130130
}
131131

132-
int ashmem_dev_get_prot_region(int fd) {
133-
return ioctl(fd, ASHMEM_GET_PROT_MASK);
134-
}
135-
136-
int ashmem_dev_pin_region(int fd, size_t offset, size_t len) {
137-
struct ashmem_pin pin = {static_cast<__u32>(offset), static_cast<__u32>(len)};
138-
return ioctl(fd, ASHMEM_PIN, &pin);
139-
}
140-
141-
int ashmem_dev_unpin_region(int fd, size_t offset, size_t len) {
142-
struct ashmem_pin pin = {static_cast<__u32>(offset), static_cast<__u32>(len)};
143-
return ioctl(fd, ASHMEM_UNPIN, &pin);
144-
}
145-
146-
size_t ashmem_dev_get_size_region(int fd) {
147-
return ioctl(fd, ASHMEM_GET_SIZE, NULL);
148-
}
149-
150132
// Starting with API level 26, the following functions from
151133
// libandroid.so should be used to create shared memory regions,
152134
// unless the device's vendor.api_level is 202604 (Android 17)
@@ -167,7 +149,7 @@ struct ASharedMemoryFuncs {
167149
ASharedMemoryFuncs s_ashmem_funcs = {};
168150
pthread_once_t s_ashmem_funcs_once = PTHREAD_ONCE_INIT;
169151

170-
int memfd_create_region(const char* name, size_t size) {
152+
int MemfdCreateRegion(const char* name, size_t size) {
171153
int fd = syscall(__NR_memfd_create, name, MFD_CLOEXEC | MFD_ALLOW_SEALING);
172154
if (fd < 0) {
173155
LOG_E("memfd_create(%s, %zd) failed: %m", name, size);
@@ -226,18 +208,18 @@ int memfd_set_prot_region(int fd, int prot) {
226208
return 0;
227209
}
228210

229-
int memfd_get_prot_region(int fd) {
211+
int MemfdGetProtRegion(int fd) {
230212
int prot = PROT_READ;
231213
int seals = fcntl(fd, F_GET_SEALS);
232214
if (seals == -1) {
233-
LOG_E("memfd_get_prot_region(%d): F_GET_SEALS failed: %m", fd);
215+
LOG_E("MemfdGetProtRegion(%d): F_GET_SEALS failed: %m", fd);
234216
} else if (!(seals & (F_SEAL_FUTURE_WRITE | F_SEAL_WRITE))) {
235217
prot |= PROT_WRITE;
236218
}
237219
return prot;
238220
}
239221

240-
void ashmem_init_funcs() {
222+
void InitAshmemFuncs() {
241223
ASharedMemoryFuncs* funcs = &s_ashmem_funcs;
242224
/*
243225
* When a device conforms to the VSR for API level 202604 (Android 17),
@@ -249,8 +231,8 @@ void ashmem_init_funcs() {
249231
* the existing sepolicy for appdomain_tmpfs files, just allocate memfds
250232
* directly if the device conforms to the VSR for API level 202604.
251233
*/
252-
if (vendor_api_level() >= 202604) {
253-
funcs->create = &memfd_create_region;
234+
if (VendorApiLevel() >= 202604) {
235+
funcs->create = &MemfdCreateRegion;
254236
funcs->setProt = &memfd_set_prot_region;
255237
} else {
256238
/* Leaked intentionally! */
@@ -262,12 +244,12 @@ void ashmem_init_funcs() {
262244
}
263245
}
264246

265-
const ASharedMemoryFuncs* ashmem_get_funcs() {
266-
pthread_once(&s_ashmem_funcs_once, &ashmem_init_funcs);
247+
const ASharedMemoryFuncs* GetAshmemFuncs() {
248+
pthread_once(&s_ashmem_funcs_once, &InitAshmemFuncs);
267249
return &s_ashmem_funcs;
268250
}
269251

270-
bool is_memfd_fd(int fd) {
252+
bool IsMemfdFd(int fd) {
271253
if (fcntl(fd, F_GET_SEALS, 0) == -1) {
272254
return false;
273255
}
@@ -276,57 +258,47 @@ bool is_memfd_fd(int fd) {
276258

277259
} // namespace
278260

279-
int ashmem_create_region(const char* name, size_t size) {
280-
return ashmem_get_funcs()->create(name, size);
261+
int SharedMemoryRegionCreate(const char* name, size_t size) {
262+
return GetAshmemFuncs()->create(name, size);
281263
}
282264

283-
int ashmem_set_prot_region(int fd, int prot) {
284-
return ashmem_get_funcs()->setProt(fd, prot);
265+
int SharedMemoryRegionSetProtectionFlags(int fd, int prot) {
266+
return GetAshmemFuncs()->setProt(fd, prot);
285267
}
286268

287-
int ashmem_get_prot_region(int fd) {
288-
if (is_memfd_fd(fd)) {
289-
return memfd_get_prot_region(fd);
269+
int SharedMemoryRegionGetProtectionFlags(int fd) {
270+
if (IsMemfdFd(fd)) {
271+
return MemfdGetProtRegion(fd);
290272
}
291273

292-
if (is_ashmem_fd(fd)) {
293-
return ashmem_dev_get_prot_region(fd);
274+
if (IsAshmemFd(fd)) {
275+
return ioctl(fd, ASHMEM_GET_PROT_MASK);
294276
}
295277

296278
return -1;
297279
}
298280

299-
int ashmem_pin_region(int fd, size_t offset, size_t len) {
300-
if (is_ashmem_fd(fd)) {
301-
return ashmem_dev_pin_region(fd, offset, len);
281+
int AshmemPinRegion(int fd, size_t offset, size_t len) {
282+
if (IsAshmemFd(fd)) {
283+
struct ashmem_pin pin = {static_cast<__u32>(offset),
284+
static_cast<__u32>(len)};
285+
return ioctl(fd, ASHMEM_PIN, &pin);
302286
}
303287
return ASHMEM_NOT_PURGED;
304288
}
305289

306-
int ashmem_unpin_region(int fd, size_t offset, size_t len) {
307-
if (is_ashmem_fd(fd)) {
308-
return ashmem_dev_unpin_region(fd, offset, len);
290+
int AshmemUnpinRegion(int fd, size_t offset, size_t len) {
291+
if (IsAshmemFd(fd)) {
292+
struct ashmem_pin pin = {static_cast<__u32>(offset),
293+
static_cast<__u32>(len)};
294+
return ioctl(fd, ASHMEM_UNPIN, &pin);
309295
}
310296
/* NOTE: It is not possible to use madvise() here because it requires a
311297
* memory address. This could be done in the caller though, instead of
312298
* this function. */
313299
return 0;
314300
}
315301

316-
int ashmem_get_size_region(int fd) {
317-
if (is_ashmem_fd(fd)) {
318-
return ashmem_dev_get_size_region(fd);
319-
}
320-
321-
struct stat sb;
322-
if (fstat(fd, &sb) == -1) {
323-
LOG_E("fstat(%d) failed: %m", fd);
324-
return -1;
325-
}
326-
327-
return sb.st_size;
328-
}
329-
330-
int ashmem_device_is_supported() {
331-
return ashmem_get_status() == ASHMEM_STATUS_SUPPORTED;
302+
int AshmemDeviceIsSupported() {
303+
return GetAshmemSupportStatus() == ASHMEM_STATUS_SUPPORTED;
332304
}

android/linker/ashmem.h

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,48 @@
2121

2222
#include <cstddef>
2323

24-
/* Returns true if the ashmem device is supported on this device.
25-
* Note that even if the device is not supported,
26-
* ashmem_{create,set_prot,get_prot,get_size}_region() will still work
27-
* because they will use the ASharedMemory functions from libandroid.so
28-
* instead. But ashmem_{pin,unpin}_region() will be no-ops. Starting with API
29-
* level 26, memfd regions are used under the scenes, also working as no-op
30-
* for pin/unpin.
31-
*/
32-
int ashmem_device_is_supported();
33-
34-
int ashmem_create_region(const char *name, size_t size);
35-
int ashmem_set_prot_region(int fd, int prot);
36-
int ashmem_get_prot_region(int fd);
37-
int ashmem_pin_region(int fd, size_t offset, size_t len);
38-
int ashmem_unpin_region(int fd, size_t offset, size_t len);
39-
int ashmem_get_size_region(int fd);
24+
// Management of shared memory regions changed across OS releases. These minimal
25+
// low level wrappers abstract away those release-specific differences.
26+
27+
// On new OS releases returns false.
28+
//
29+
// On old systems returns true when the "/dev/ashmem" device is present on the
30+
// system and looks 'functional'.
31+
//
32+
// Without /dev/ashmem the functions below will continue working, but may
33+
// use different mechanisms and system library calls under the scenes.
34+
//
35+
// The functions can be disabled system-wide without removing /dev/ashmem. This
36+
// should be rare, and the rest of the functions declared below will behave just
37+
// like /dev/ashmem does not exist.
38+
int AshmemDeviceIsSupported();
39+
40+
// Implements ASharedMemory_create(), as described in NDK docs (API level 26).
41+
int SharedMemoryRegionCreate(const char* name, size_t size);
42+
43+
// Implements ASharedMemory_setProt(), as described in NDK docs (API level 26).
44+
int SharedMemoryRegionSetProtectionFlags(int fd, int prot);
45+
46+
// Returns the memory protection flags for the region (PROT_READ, PROT_WRITE or
47+
// both).
48+
int SharedMemoryRegionGetProtectionFlags(int fd);
49+
50+
// Pins the region to memory on old systems.
51+
//
52+
// Behaves as no-op when the region is not served by the ashmem device
53+
// (irrelevant FDs).
54+
//
55+
// Note: Even if AshmemDeviceIsSupported() is true, pinning may still be
56+
// disabled.
57+
// Note: This functionality was never officially provided by Android NDK, and
58+
// the underlying mechanisms are being removed.
59+
int AshmemPinRegion(int fd, size_t offset, size_t len);
60+
61+
// Unpins the region from memory on old systems, allowing the OS to free it
62+
// without asking the userspace.
63+
//
64+
// Just like the AshmemPinRegion() above, behaves as no-op when the OS support
65+
// is removed. Also it is a no-op for irrelevant FDs.
66+
int AshmemUnpinRegion(int fd, size_t offset, size_t len);
4067

4168
#endif // BASE_ANDROID_LINKER_ASHMEM_H_

android/linker/linker_jni.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,13 +489,13 @@ bool NativeLibInfo::CreateSharedRelroFd() {
489489
}
490490

491491
// Create a writable shared memory region.
492-
int shared_mem_fd = ashmem_create_region("cr_relro", relro_size_);
492+
int shared_mem_fd = SharedMemoryRegionCreate("cr_relro", relro_size_);
493493
if (shared_mem_fd == -1) {
494494
LOG_ERROR("Cannot create the shared memory file");
495495
return false;
496496
}
497497
int rw_flags = PROT_READ | PROT_WRITE;
498-
ashmem_set_prot_region(shared_mem_fd, rw_flags);
498+
SharedMemoryRegionSetProtectionFlags(shared_mem_fd, rw_flags);
499499

500500
// Map the region as writable.
501501
void* relro_copy_addr =
@@ -519,7 +519,7 @@ bool NativeLibInfo::CreateSharedRelroFd() {
519519
// writable memory mappings, since they are not directly affected by the
520520
// change of region's protection flags.
521521
munmap(relro_copy_addr, relro_size_);
522-
if (ashmem_set_prot_region(shared_mem_fd, PROT_READ) == -1) {
522+
if (SharedMemoryRegionSetProtectionFlags(shared_mem_fd, PROT_READ) == -1) {
523523
LOG_ERROR("Failed to set the RELRO FD as read-only.");
524524
close(shared_mem_fd);
525525
return false;

memory/discardable_shared_memory.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ size_t AlignToPageSize(size_t size) {
134134

135135
#if BUILDFLAG(IS_ANDROID)
136136
bool UseAshmemUnpinningForDiscardableMemory() {
137-
if (!ashmem_device_is_supported()) {
137+
if (!AshmemDeviceIsSupported()) {
138138
return false;
139139
}
140140

@@ -544,7 +544,7 @@ DiscardableSharedMemory::LockResult DiscardableSharedMemory::LockPages(
544544
if (region.IsValid()) {
545545
if (UseAshmemUnpinningForDiscardableMemory()) {
546546
int pin_result =
547-
ashmem_pin_region(region.GetPlatformHandle(), offset, length);
547+
AshmemPinRegion(region.GetPlatformHandle(), offset, length);
548548
if (pin_result == ASHMEM_WAS_PURGED) {
549549
return PURGED;
550550
}
@@ -566,7 +566,7 @@ void DiscardableSharedMemory::UnlockPages(
566566
if (region.IsValid()) {
567567
if (UseAshmemUnpinningForDiscardableMemory()) {
568568
int unpin_result =
569-
ashmem_unpin_region(region.GetPlatformHandle(), offset, length);
569+
AshmemUnpinRegion(region.GetPlatformHandle(), offset, length);
570570
DCHECK_EQ(0, unpin_result);
571571
}
572572
}

0 commit comments

Comments
 (0)