Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 75f730ab authored by Josh Gao's avatar Josh Gao
Browse files

SharedMemory: use fdsan to protect our fd.

Bug: http://b/138422309
Test: booted, saw the GraphicsStatsService fd misacquisition
Change-Id: I9350ec13e523e1bf86797a1231769d890f277008
parent 9fdb8f99
Loading
Loading
Loading
Loading
+12 −7
Original line number Diff line number Diff line
@@ -25,6 +25,8 @@ import android.system.OsConstants;

import dalvik.system.VMRuntime;

import libcore.io.IoUtils;

import java.io.Closeable;
import java.io.FileDescriptor;
import java.nio.ByteBuffer;
@@ -293,21 +295,24 @@ public final class SharedMemory implements Parcelable, Closeable {
     * Cleaner that closes the FD
     */
    private static final class Closer implements Runnable {
        private int mFd;
        // This is a copy of the FileDescriptor we're attached to, in order to avoid a reference
        // cycle.
        private FileDescriptor mFd;
        private MemoryRegistration mMemoryReference;

        private Closer(int fd, MemoryRegistration memoryReference) {
            mFd = fd;
            mFd = new FileDescriptor();
            mFd.setInt$(fd);
            IoUtils.setFdOwner(mFd, this);

            mMemoryReference = memoryReference;
        }

        @Override
        public void run() {
            try {
                FileDescriptor fd = new FileDescriptor();
                fd.setInt$(mFd);
                Os.close(fd);
            } catch (ErrnoException e) { /* swallow error */ }
            IoUtils.closeQuietly(mFd);
            mFd = null;

            mMemoryReference.release();
            mMemoryReference = null;
        }