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

Commit 2e2685c3 authored by Josh Gao's avatar Josh Gao Committed by Elliott Hughes
Browse files

Reapply "SharedMemory: mark fdsan ownership"

The previous attempt failed to remove `mFileDescriptor.setInt$(-1)`
from close(), so codepaths that explicitly closed their SharedMemory
would leak.

This reverts commit 4caec17d.

Bug: http://b/347867443
Test: treehugger
Change-Id: I2ead9e56635dbc04a0342d3ec5c7bbde972d647b
parent 4caec17d
Loading
Loading
Loading
Loading
+9 −9
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.io.IOException;
@@ -63,7 +65,7 @@ public final class SharedMemory implements Parcelable, Closeable {

        mMemoryRegistration = new MemoryRegistration(mSize);
        mCleaner = Cleaner.create(mFileDescriptor,
                new Closer(mFileDescriptor.getInt$(), mMemoryRegistration));
                new Closer(mFileDescriptor, mMemoryRegistration));
    }

    /**
@@ -276,7 +278,6 @@ public final class SharedMemory implements Parcelable, Closeable {
     */
    @Override
    public void close() {
        mFileDescriptor.setInt$(-1);
        if (mCleaner != null) {
            mCleaner.clean();
            mCleaner = null;
@@ -326,21 +327,20 @@ public final class SharedMemory implements Parcelable, Closeable {
     * Cleaner that closes the FD
     */
    private static final class Closer implements Runnable {
        private int mFd;
        private FileDescriptor mFd;
        private MemoryRegistration mMemoryReference;

        private Closer(int fd, MemoryRegistration memoryReference) {
        private Closer(FileDescriptor fd, MemoryRegistration memoryReference) {
            mFd = 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;
        }
+1 −1
Original line number Diff line number Diff line
@@ -87,7 +87,7 @@ public class ContentResolverTest {
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, mImage.getOutputStream());

        final AssetFileDescriptor afd = new AssetFileDescriptor(
                new ParcelFileDescriptor(mImage.getFileDescriptor()), 0, mSize, null);
                ParcelFileDescriptor.dup(mImage.getFileDescriptor()), 0, mSize, null);
        when(mProvider.openTypedAssetFile(any(), any(), any(), any(), any())).thenReturn(
                afd);
    }