| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.os; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.compat.annotation.UnsupportedAppUsage; |
| 22 |
import android.system.ErrnoException; |
| 23 |
import android.system.Os; |
| 24 |
import android.system.OsConstants; |
| 25 |
|
| 26 |
import dalvik.system.VMRuntime; |
| 27 |
|
| 28 |
import java.io.Closeable; |
| 29 |
import java.io.FileDescriptor; |
| 30 |
import java.io.IOException; |
| 31 |
import java.nio.ByteBuffer; |
| 32 |
import java.nio.DirectByteBuffer; |
| 33 |
import java.nio.NioUtils; |
| 34 |
|
| 35 |
import sun.misc.Cleaner; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public final class SharedMemory implements Parcelable, Closeable { |
| 41 |
|
| 42 |
private final FileDescriptor mFileDescriptor; |
| 43 |
private final int mSize; |
| 44 |
private final MemoryRegistration mMemoryRegistration; |
| 45 |
private Cleaner mCleaner; |
| 46 |
|
| 47 |
private SharedMemory(FileDescriptor fd) { |
| 48 |
|
| 49 |
|
| 50 |
if (fd == null) { |
| 51 |
throw new IllegalArgumentException( |
| 52 |
"Unable to create SharedMemory from a null FileDescriptor"); |
| 53 |
} |
| 54 |
if (!fd.valid()) { |
| 55 |
throw new IllegalArgumentException( |
| 56 |
"Unable to create SharedMemory from closed FileDescriptor"); |
| 57 |
} |
| 58 |
mFileDescriptor = fd; |
| 59 |
mSize = nGetSize(mFileDescriptor); |
| 60 |
if (mSize <= 0) { |
| 61 |
throw new IllegalArgumentException("FileDescriptor is not a valid ashmem fd"); |
| 62 |
} |
| 63 |
|
| 64 |
mMemoryRegistration = new MemoryRegistration(mSize); |
| 65 |
mCleaner = Cleaner.create(mFileDescriptor, |
| 66 |
new Closer(mFileDescriptor.getInt$(), mMemoryRegistration)); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public static @NonNull SharedMemory create(@Nullable String name, int size) |
| 83 |
throws ErrnoException { |
| 84 |
if (size <= 0) { |
| 85 |
throw new IllegalArgumentException("Size must be greater than zero"); |
| 86 |
} |
| 87 |
return new SharedMemory(nCreate(name, size)); |
| 88 |
} |
| 89 |
|
| 90 |
private void checkOpen() { |
| 91 |
if (!mFileDescriptor.valid()) { |
| 92 |
throw new IllegalStateException("SharedMemory is closed"); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public static @NonNull SharedMemory fromFileDescriptor(@NonNull ParcelFileDescriptor fd) { |
| 111 |
FileDescriptor f = new FileDescriptor(); |
| 112 |
f.setInt$(fd.detachFd()); |
| 113 |
return new SharedMemory(f); |
| 114 |
} |
| 115 |
|
| 116 |
private static final int PROT_MASK = OsConstants.PROT_READ | OsConstants.PROT_WRITE |
| 117 |
| OsConstants.PROT_EXEC | OsConstants.PROT_NONE; |
| 118 |
|
| 119 |
private static void validateProt(int prot) { |
| 120 |
if ((prot & ~PROT_MASK) != 0) { |
| 121 |
throw new IllegalArgumentException("Invalid prot value"); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public boolean setProtect(int prot) { |
| 151 |
checkOpen(); |
| 152 |
validateProt(prot); |
| 153 |
int errno = nSetProt(mFileDescriptor, prot); |
| 154 |
return errno == 0; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public @NonNull FileDescriptor getFileDescriptor() { |
| 168 |
return mFileDescriptor; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
@UnsupportedAppUsage(trackingBug = 171971817) |
| 182 |
public int getFd() { |
| 183 |
return mFileDescriptor.getInt$(); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public int getSize() { |
| 190 |
checkOpen(); |
| 191 |
return mSize; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public @NonNull ByteBuffer mapReadWrite() throws ErrnoException { |
| 205 |
return map(OsConstants.PROT_READ | OsConstants.PROT_WRITE, 0, mSize); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public @NonNull ByteBuffer mapReadOnly() throws ErrnoException { |
| 219 |
return map(OsConstants.PROT_READ, 0, mSize); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public @NonNull ByteBuffer map(int prot, int offset, int length) throws ErrnoException { |
| 237 |
checkOpen(); |
| 238 |
validateProt(prot); |
| 239 |
if (offset < 0) { |
| 240 |
throw new IllegalArgumentException("Offset must be >= 0"); |
| 241 |
} |
| 242 |
if (length <= 0) { |
| 243 |
throw new IllegalArgumentException("Length must be > 0"); |
| 244 |
} |
| 245 |
if (offset + length > mSize) { |
| 246 |
throw new IllegalArgumentException("offset + length must not exceed getSize()"); |
| 247 |
} |
| 248 |
long address = Os.mmap(0, length, prot, OsConstants.MAP_SHARED, mFileDescriptor, offset); |
| 249 |
boolean readOnly = (prot & OsConstants.PROT_WRITE) == 0; |
| 250 |
Runnable unmapper = new Unmapper(address, length, mMemoryRegistration.acquire()); |
| 251 |
return new DirectByteBuffer(length, address, mFileDescriptor, unmapper, readOnly); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
public static void unmap(@NonNull ByteBuffer buffer) { |
| 263 |
if (buffer instanceof DirectByteBuffer) { |
| 264 |
NioUtils.freeDirectBuffer(buffer); |
| 265 |
} else { |
| 266 |
throw new IllegalArgumentException( |
| 267 |
"ByteBuffer wasn't created by #map(int, int, int); can't unmap"); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
@Override |
| 278 |
public void close() { |
| 279 |
mFileDescriptor.setInt$(-1); |
| 280 |
if (mCleaner != null) { |
| 281 |
mCleaner.clean(); |
| 282 |
mCleaner = null; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
@Override |
| 287 |
public int describeContents() { |
| 288 |
return CONTENTS_FILE_DESCRIPTOR; |
| 289 |
} |
| 290 |
|
| 291 |
@Override |
| 292 |
public void writeToParcel(@NonNull Parcel dest, int flags) { |
| 293 |
checkOpen(); |
| 294 |
dest.writeFileDescriptor(mFileDescriptor); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
public ParcelFileDescriptor getFdDup() throws IOException { |
| 308 |
return ParcelFileDescriptor.dup(mFileDescriptor); |
| 309 |
} |
| 310 |
|
| 311 |
public static final @android.annotation.NonNull Parcelable.Creator<SharedMemory> CREATOR = |
| 312 |
new Parcelable.Creator<SharedMemory>() { |
| 313 |
@Override |
| 314 |
public SharedMemory createFromParcel(Parcel source) { |
| 315 |
FileDescriptor descriptor = source.readRawFileDescriptor(); |
| 316 |
return new SharedMemory(descriptor); |
| 317 |
} |
| 318 |
|
| 319 |
@Override |
| 320 |
public SharedMemory[] newArray(int size) { |
| 321 |
return new SharedMemory[size]; |
| 322 |
} |
| 323 |
}; |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
private static final class Closer implements Runnable { |
| 329 |
private int mFd; |
| 330 |
private MemoryRegistration mMemoryReference; |
| 331 |
|
| 332 |
private Closer(int fd, MemoryRegistration memoryReference) { |
| 333 |
mFd = fd; |
| 334 |
mMemoryReference = memoryReference; |
| 335 |
} |
| 336 |
|
| 337 |
@Override |
| 338 |
public void run() { |
| 339 |
try { |
| 340 |
FileDescriptor fd = new FileDescriptor(); |
| 341 |
fd.setInt$(mFd); |
| 342 |
Os.close(fd); |
| 343 |
} catch (ErrnoException e) { } |
| 344 |
mMemoryReference.release(); |
| 345 |
mMemoryReference = null; |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
private static final class Unmapper implements Runnable { |
| 353 |
private long mAddress; |
| 354 |
private int mSize; |
| 355 |
private MemoryRegistration mMemoryReference; |
| 356 |
|
| 357 |
private Unmapper(long address, int size, MemoryRegistration memoryReference) { |
| 358 |
mAddress = address; |
| 359 |
mSize = size; |
| 360 |
mMemoryReference = memoryReference; |
| 361 |
} |
| 362 |
|
| 363 |
@Override |
| 364 |
public void run() { |
| 365 |
try { |
| 366 |
Os.munmap(mAddress, mSize); |
| 367 |
} catch (ErrnoException e) { } |
| 368 |
mMemoryReference.release(); |
| 369 |
mMemoryReference = null; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
private static final class MemoryRegistration { |
| 378 |
private int mSize; |
| 379 |
private int mReferenceCount; |
| 380 |
|
| 381 |
private MemoryRegistration(int size) { |
| 382 |
mSize = size; |
| 383 |
mReferenceCount = 1; |
| 384 |
VMRuntime.getRuntime().registerNativeAllocation(mSize); |
| 385 |
} |
| 386 |
|
| 387 |
public synchronized MemoryRegistration acquire() { |
| 388 |
mReferenceCount++; |
| 389 |
return this; |
| 390 |
} |
| 391 |
|
| 392 |
public synchronized void release() { |
| 393 |
mReferenceCount--; |
| 394 |
if (mReferenceCount == 0) { |
| 395 |
VMRuntime.getRuntime().registerNativeFree(mSize); |
| 396 |
} |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
private static native FileDescriptor nCreate(String name, int size) throws ErrnoException; |
| 401 |
private static native int nGetSize(FileDescriptor fd); |
| 402 |
private static native int nSetProt(FileDescriptor fd, int prot); |
| 403 |
} |