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

Commit fd9ddd1a authored by Brian Carlstrom's avatar Brian Carlstrom
Browse files

Integrate StrictMode with CloseGuard

In additional to adding the StringMode API for controling CloseGuard,
this checkin fixes several CloseGuard issues found booting a device.

Bug: 3041575
Change-Id: I4dffd184f49438d6d477ed81a1c2a2a5b56cc76b
parent cc82f3ae
Loading
Loading
Loading
Loading
+10 −2
Original line number Original line Diff line number Diff line
@@ -230,7 +230,11 @@ public class ParcelFileDescriptor implements Parcelable {


        @Override
        @Override
        public void close() throws IOException {
        public void close() throws IOException {
            try {
                mFd.close();
                mFd.close();
            } finally {
                super.close();
            }
        }
        }
    }
    }
    
    
@@ -249,7 +253,11 @@ public class ParcelFileDescriptor implements Parcelable {


        @Override
        @Override
        public void close() throws IOException {
        public void close() throws IOException {
            try {
                mFd.close();
                mFd.close();
            } finally {
                super.close();
            }
        }
        }
    }
    }
    
    
+37 −6
Original line number Original line Diff line number Diff line
@@ -26,6 +26,7 @@ import android.util.Printer;
import com.android.internal.os.RuntimeInit;
import com.android.internal.os.RuntimeInit;


import dalvik.system.BlockGuard;
import dalvik.system.BlockGuard;
import dalvik.system.CloseGuard;


import java.io.PrintWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.StringWriter;
@@ -70,6 +71,7 @@ import java.util.HashMap;
 *                 .build());
 *                 .build());
 *         StrictMode.setVmPolicy(new {@link VmPolicy.Builder StrictMode.VmPolicy.Builder}()
 *         StrictMode.setVmPolicy(new {@link VmPolicy.Builder StrictMode.VmPolicy.Builder}()
 *                 .detectLeakedSqlLiteObjects()
 *                 .detectLeakedSqlLiteObjects()
 *                 .detectLeakedClosableObjects()
 *                 .penaltyLog()
 *                 .penaltyLog()
 *                 .penaltyDeath()
 *                 .penaltyDeath()
 *                 .build());
 *                 .build());
@@ -139,6 +141,12 @@ public final class StrictMode {
     */
     */
    public static final int DETECT_VM_CURSOR_LEAKS = 0x200;  // for ProcessPolicy
    public static final int DETECT_VM_CURSOR_LEAKS = 0x200;  // for ProcessPolicy


    /**
     * Note, a "VM_" bit, not thread.
     * @hide
     */
    public static final int DETECT_VM_CLOSABLE_LEAKS = 0x400;  // for ProcessPolicy

    /**
    /**
     * @hide
     * @hide
     */
     */
@@ -450,12 +458,12 @@ public final class StrictMode {
            /**
            /**
             * Detect everything that's potentially suspect.
             * Detect everything that's potentially suspect.
             *
             *
             * <p>As of the Gingerbread release this only includes
             * <p>In the Honeycomb release this includes leaks of
             * SQLite cursor leaks but will likely expand in future
             * SQLite cursors and other closable objects but will
             * releases.
             * likely expand in future releases.
             */
             */
            public Builder detectAll() {
            public Builder detectAll() {
                return enable(DETECT_VM_CURSOR_LEAKS);
                return enable(DETECT_VM_CURSOR_LEAKS | DETECT_VM_CLOSABLE_LEAKS);
            }
            }


            /**
            /**
@@ -471,6 +479,18 @@ public final class StrictMode {
                return enable(DETECT_VM_CURSOR_LEAKS);
                return enable(DETECT_VM_CURSOR_LEAKS);
            }
            }


            /**
             * Detect when an {@link java.io.Closeable} or other
             * object with a explict termination method is finalized
             * without having been closed.
             *
             * <p>You always want to explicitly close such objects to
             * avoid unnecessary resources leaks.
             */
            public Builder detectLeakedClosableObjects() {
                return enable(DETECT_VM_CLOSABLE_LEAKS);
            }

            /**
            /**
             * Crashes the whole process on violation.  This penalty runs at
             * Crashes the whole process on violation.  This penalty runs at
             * the end of all enabled penalties so yo you'll still get
             * the end of all enabled penalties so yo you'll still get
@@ -671,6 +691,7 @@ public final class StrictMode {
            StrictMode.DETECT_NETWORK |
            StrictMode.DETECT_NETWORK |
            StrictMode.PENALTY_DROPBOX);
            StrictMode.PENALTY_DROPBOX);
        sVmPolicyMask = StrictMode.DETECT_VM_CURSOR_LEAKS |
        sVmPolicyMask = StrictMode.DETECT_VM_CURSOR_LEAKS |
                StrictMode.DETECT_VM_CLOSABLE_LEAKS |
                StrictMode.PENALTY_DROPBOX |
                StrictMode.PENALTY_DROPBOX |
                StrictMode.PENALTY_LOG;
                StrictMode.PENALTY_LOG;
        return true;
        return true;
@@ -1030,6 +1051,7 @@ public final class StrictMode {
     */
     */
    public static void setVmPolicy(final VmPolicy policy) {
    public static void setVmPolicy(final VmPolicy policy) {
        sVmPolicyMask = policy.mask;
        sVmPolicyMask = policy.mask;
        CloseGuard.setEnabled(vmClosableObjectLeaksEnabled());
    }
    }


    /**
    /**
@@ -1043,8 +1065,9 @@ public final class StrictMode {
     * Enable the recommended StrictMode defaults, with violations just being logged.
     * Enable the recommended StrictMode defaults, with violations just being logged.
     *
     *
     * <p>This catches disk and network access on the main thread, as
     * <p>This catches disk and network access on the main thread, as
     * well as leaked SQLite cursors.  This is simply a wrapper around
     * well as leaked SQLite cursors and unclosed resources.  This is
     * {@link #setVmPolicy} and {@link #setThreadPolicy}.
     * simply a wrapper around {@link #setVmPolicy} and {@link
     * #setThreadPolicy}.
     */
     */
    public static void enableDefaults() {
    public static void enableDefaults() {
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
@@ -1053,6 +1076,7 @@ public final class StrictMode {
                                   .build());
                                   .build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                               .detectLeakedSqlLiteObjects()
                               .detectLeakedSqlLiteObjects()
                               .detectLeakedClosableObjects()
                               .penaltyLog()
                               .penaltyLog()
                               .build());
                               .build());
    }
    }
@@ -1064,6 +1088,13 @@ public final class StrictMode {
        return (sVmPolicyMask & DETECT_VM_CURSOR_LEAKS) != 0;
        return (sVmPolicyMask & DETECT_VM_CURSOR_LEAKS) != 0;
    }
    }


    /**
     * @hide
     */
    public static boolean vmClosableObjectLeaksEnabled() {
        return (sVmPolicyMask & DETECT_VM_CLOSABLE_LEAKS) != 0;
    }

    /**
    /**
     * @hide
     * @hide
     */
     */
+1 −0
Original line number Original line Diff line number Diff line
@@ -259,6 +259,7 @@ public class NativeLibraryHelper {
                File destFile = new File(sharedLibraryDir, entry.second);
                File destFile = new File(sharedLibraryDir, entry.second);
                copyNativeBinaryLI(zipFile, entry.first, sharedLibraryDir, destFile);
                copyNativeBinaryLI(zipFile, entry.first, sharedLibraryDir, destFile);
            }
            }
            zipFile.close();
        } catch (ZipException e) {
        } catch (ZipException e) {
            Slog.w(TAG, "Failed to extract data from package file", e);
            Slog.w(TAG, "Failed to extract data from package file", e);
            return PackageManager.INSTALL_FAILED_INVALID_APK;
            return PackageManager.INSTALL_FAILED_INVALID_APK;
+1 −0
Original line number Original line Diff line number Diff line
@@ -102,6 +102,7 @@ class DockObserver extends UEventObserver {
        try {
        try {
            FileReader file = new FileReader(DOCK_STATE_PATH);
            FileReader file = new FileReader(DOCK_STATE_PATH);
            int len = file.read(buffer, 0, 1024);
            int len = file.read(buffer, 0, 1024);
            file.close();
            mPreviousDockState = mDockState = Integer.valueOf((new String(buffer, 0, len)).trim());
            mPreviousDockState = mDockState = Integer.valueOf((new String(buffer, 0, len)).trim());
        } catch (FileNotFoundException e) {
        } catch (FileNotFoundException e) {
            Slog.w(TAG, "This kernel does not have dock station support");
            Slog.w(TAG, "This kernel does not have dock station support");
+3 −1
Original line number Original line Diff line number Diff line
@@ -493,7 +493,9 @@ public class InputManager {
                    CALIBRATION_DIR_PATH + deviceName + ".idc");
                    CALIBRATION_DIR_PATH + deviceName + ".idc");
            if (calibrationFile.exists()) {
            if (calibrationFile.exists()) {
                try {
                try {
                    properties.load(new FileInputStream(calibrationFile));
                    FileInputStream fis = new FileInputStream(calibrationFile);
                    properties.load(fis);
                    fis.close();
                } catch (IOException ex) {
                } catch (IOException ex) {
                    Slog.w(TAG, "Error reading input device calibration properties for device "
                    Slog.w(TAG, "Error reading input device calibration properties for device "
                            + deviceName + " from " + calibrationFile + ".", ex);
                            + deviceName + " from " + calibrationFile + ".", ex);
Loading