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

Commit c8f54809 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "StrictMode: Add support for warning on non SDK API usage." into pi-dev

parents d9cb5962 ad1b2a9c
Loading
Loading
Loading
Loading
+5 −0
Original line number Original line Diff line number Diff line
@@ -33160,6 +33160,7 @@ package android.os {
    method public android.os.StrictMode.VmPolicy.Builder detectLeakedClosableObjects();
    method public android.os.StrictMode.VmPolicy.Builder detectLeakedClosableObjects();
    method public android.os.StrictMode.VmPolicy.Builder detectLeakedRegistrationObjects();
    method public android.os.StrictMode.VmPolicy.Builder detectLeakedRegistrationObjects();
    method public android.os.StrictMode.VmPolicy.Builder detectLeakedSqlLiteObjects();
    method public android.os.StrictMode.VmPolicy.Builder detectLeakedSqlLiteObjects();
    method public android.os.StrictMode.VmPolicy.Builder detectNonSdkApiUsage();
    method public android.os.StrictMode.VmPolicy.Builder detectUntaggedSockets();
    method public android.os.StrictMode.VmPolicy.Builder detectUntaggedSockets();
    method public android.os.StrictMode.VmPolicy.Builder penaltyDeath();
    method public android.os.StrictMode.VmPolicy.Builder penaltyDeath();
    method public android.os.StrictMode.VmPolicy.Builder penaltyDeathOnCleartextNetwork();
    method public android.os.StrictMode.VmPolicy.Builder penaltyDeathOnCleartextNetwork();
@@ -33167,6 +33168,7 @@ package android.os {
    method public android.os.StrictMode.VmPolicy.Builder penaltyDropBox();
    method public android.os.StrictMode.VmPolicy.Builder penaltyDropBox();
    method public android.os.StrictMode.VmPolicy.Builder penaltyListener(java.util.concurrent.Executor, android.os.StrictMode.OnVmViolationListener);
    method public android.os.StrictMode.VmPolicy.Builder penaltyListener(java.util.concurrent.Executor, android.os.StrictMode.OnVmViolationListener);
    method public android.os.StrictMode.VmPolicy.Builder penaltyLog();
    method public android.os.StrictMode.VmPolicy.Builder penaltyLog();
    method public android.os.StrictMode.VmPolicy.Builder permitNonSdkApiUsage();
    method public android.os.StrictMode.VmPolicy.Builder setClassInstanceLimit(java.lang.Class, int);
    method public android.os.StrictMode.VmPolicy.Builder setClassInstanceLimit(java.lang.Class, int);
  }
  }
@@ -33581,6 +33583,9 @@ package android.os.strictmode {
  public final class NetworkViolation extends android.os.strictmode.Violation {
  public final class NetworkViolation extends android.os.strictmode.Violation {
  }
  }
  public final class NonSdkApiUsedViolation extends android.os.strictmode.Violation {
  }
  public final class ResourceMismatchViolation extends android.os.strictmode.Violation {
  public final class ResourceMismatchViolation extends android.os.strictmode.Violation {
  }
  }
+1 −0
Original line number Original line Diff line number Diff line
@@ -527,6 +527,7 @@ package android.os {
    field public static final int DETECT_VM_CURSOR_LEAKS = 256; // 0x100
    field public static final int DETECT_VM_CURSOR_LEAKS = 256; // 0x100
    field public static final int DETECT_VM_FILE_URI_EXPOSURE = 8192; // 0x2000
    field public static final int DETECT_VM_FILE_URI_EXPOSURE = 8192; // 0x2000
    field public static final int DETECT_VM_INSTANCE_LEAKS = 2048; // 0x800
    field public static final int DETECT_VM_INSTANCE_LEAKS = 2048; // 0x800
    field public static final int DETECT_VM_NON_SDK_API_USAGE = 1073741824; // 0x40000000
    field public static final int DETECT_VM_REGISTRATION_LEAKS = 4096; // 0x1000
    field public static final int DETECT_VM_REGISTRATION_LEAKS = 4096; // 0x1000
    field public static final int DETECT_VM_UNTAGGED_SOCKET = -2147483648; // 0x80000000
    field public static final int DETECT_VM_UNTAGGED_SOCKET = -2147483648; // 0x80000000
  }
  }
+43 −1
Original line number Original line Diff line number Diff line
@@ -39,6 +39,7 @@ import android.os.strictmode.InstanceCountViolation;
import android.os.strictmode.IntentReceiverLeakedViolation;
import android.os.strictmode.IntentReceiverLeakedViolation;
import android.os.strictmode.LeakedClosableViolation;
import android.os.strictmode.LeakedClosableViolation;
import android.os.strictmode.NetworkViolation;
import android.os.strictmode.NetworkViolation;
import android.os.strictmode.NonSdkApiUsedViolation;
import android.os.strictmode.ResourceMismatchViolation;
import android.os.strictmode.ResourceMismatchViolation;
import android.os.strictmode.ServiceConnectionLeakedViolation;
import android.os.strictmode.ServiceConnectionLeakedViolation;
import android.os.strictmode.SqliteObjectLeakedViolation;
import android.os.strictmode.SqliteObjectLeakedViolation;
@@ -76,6 +77,7 @@ import java.util.HashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;


/**
/**
 * StrictMode is a developer tool which detects things you might be doing by accident and brings
 * StrictMode is a developer tool which detects things you might be doing by accident and brings
@@ -262,6 +264,9 @@ public final class StrictMode {
    /** @hide */
    /** @hide */
    @TestApi public static final int DETECT_VM_UNTAGGED_SOCKET = 0x80 << 24; // for VmPolicy
    @TestApi public static final int DETECT_VM_UNTAGGED_SOCKET = 0x80 << 24; // for VmPolicy


    /** @hide */
    @TestApi public static final int DETECT_VM_NON_SDK_API_USAGE = 0x40 << 24; // for VmPolicy

    private static final int ALL_VM_DETECT_BITS =
    private static final int ALL_VM_DETECT_BITS =
            DETECT_VM_CURSOR_LEAKS
            DETECT_VM_CURSOR_LEAKS
                    | DETECT_VM_CLOSABLE_LEAKS
                    | DETECT_VM_CLOSABLE_LEAKS
@@ -271,7 +276,9 @@ public final class StrictMode {
                    | DETECT_VM_FILE_URI_EXPOSURE
                    | DETECT_VM_FILE_URI_EXPOSURE
                    | DETECT_VM_CLEARTEXT_NETWORK
                    | DETECT_VM_CLEARTEXT_NETWORK
                    | DETECT_VM_CONTENT_URI_WITHOUT_PERMISSION
                    | DETECT_VM_CONTENT_URI_WITHOUT_PERMISSION
                    | DETECT_VM_UNTAGGED_SOCKET;
                    | DETECT_VM_UNTAGGED_SOCKET
                    | DETECT_VM_NON_SDK_API_USAGE;



    // Byte 3: Penalty
    // Byte 3: Penalty


@@ -413,6 +420,13 @@ public final class StrictMode {
     */
     */
    private static final AtomicInteger sDropboxCallsInFlight = new AtomicInteger(0);
    private static final AtomicInteger sDropboxCallsInFlight = new AtomicInteger(0);


    /**
     * Callback supplied to dalvik / libcore to get informed of usages of java API that are not
     * a part of the public SDK.
     */
    private static final Consumer<String> sNonSdkApiUsageConsumer =
            message -> onVmPolicyViolation(new NonSdkApiUsedViolation(message));

    private StrictMode() {}
    private StrictMode() {}


    /**
    /**
@@ -795,6 +809,23 @@ public final class StrictMode {
                return disable(DETECT_VM_ACTIVITY_LEAKS);
                return disable(DETECT_VM_ACTIVITY_LEAKS);
            }
            }


            /**
             * Detect reflective usage of APIs that are not part of the public Android SDK.
             */
            public Builder detectNonSdkApiUsage() {
                return enable(DETECT_VM_NON_SDK_API_USAGE);
            }

            /**
             * Permit reflective usage of APIs that are not part of the public Android SDK. Note
             * that this <b>only</b> affects {@code StrictMode}, the underlying runtime may
             * continue to restrict or warn on access to methods that are not part of the
             * public SDK.
             */
            public Builder permitNonSdkApiUsage() {
                return disable(DETECT_VM_NON_SDK_API_USAGE);
            }

            /**
            /**
             * Detect everything that's potentially suspect.
             * Detect everything that's potentially suspect.
             *
             *
@@ -826,6 +857,8 @@ public final class StrictMode {
                    detectContentUriWithoutPermission();
                    detectContentUriWithoutPermission();
                    detectUntaggedSockets();
                    detectUntaggedSockets();
                }
                }

                // TODO: Decide whether to detect non SDK API usage beyond a certain API level.
                return this;
                return this;
            }
            }


@@ -1848,6 +1881,13 @@ public final class StrictMode {
            } else if (networkPolicy != NETWORK_POLICY_ACCEPT) {
            } else if (networkPolicy != NETWORK_POLICY_ACCEPT) {
                Log.w(TAG, "Dropping requested network policy due to missing service!");
                Log.w(TAG, "Dropping requested network policy due to missing service!");
            }
            }


            if ((sVmPolicy.mask & DETECT_VM_NON_SDK_API_USAGE) != 0) {
                VMRuntime.setNonSdkApiUsageConsumer(sNonSdkApiUsageConsumer);
            } else {
                VMRuntime.setNonSdkApiUsageConsumer(null);
            }
        }
        }
    }
    }


@@ -2576,6 +2616,8 @@ public final class StrictMode {
                return DETECT_VM_CONTENT_URI_WITHOUT_PERMISSION;
                return DETECT_VM_CONTENT_URI_WITHOUT_PERMISSION;
            } else if (mViolation instanceof UntaggedSocketViolation) {
            } else if (mViolation instanceof UntaggedSocketViolation) {
                return DETECT_VM_UNTAGGED_SOCKET;
                return DETECT_VM_UNTAGGED_SOCKET;
            } else if (mViolation instanceof NonSdkApiUsedViolation) {
                return DETECT_VM_NON_SDK_API_USAGE;
            }
            }
            throw new IllegalStateException("missing violation bit");
            throw new IllegalStateException("missing violation bit");
        }
        }
+28 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.os.strictmode;

/**
 * Subclass of {@code Violation} that is used when a process accesses
 * a non SDK API.
 */
public final class NonSdkApiUsedViolation extends Violation {
    /** @hide */
    public NonSdkApiUsedViolation(String message) {
        super(message);
    }
}