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

Commit 50a6a97c authored by Alan Stokes's avatar Alan Stokes Committed by Android (Google) Code Review
Browse files

Merge "Apply a per-package limit to DCL data stored."

parents 343a9574 8f7dc7c4
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -61,6 +61,13 @@ class PackageDynamicCodeLoading extends AbstractStatsBase<Void> {
    private static final char FIELD_SEPARATOR = ':';
    private static final String PACKAGE_SEPARATOR = ",";

    /**
     * Limit on how many files we store for a single owner, to avoid one app causing
     * unbounded memory consumption.
     */
    @VisibleForTesting
    static final int MAX_FILES_PER_OWNER = 100;

    /**
     * Regular expression to match the expected format of an input line describing one file.
     * <p>Example: {@code D:10:package.name1,package.name2:/escaped/path}
@@ -515,6 +522,9 @@ class PackageDynamicCodeLoading extends AbstractStatsBase<Void> {
        private boolean add(String path, char fileType, int userId, String loadingPackage) {
            DynamicCodeFile fileInfo = mFileUsageMap.get(path);
            if (fileInfo == null) {
                if (mFileUsageMap.size() >= MAX_FILES_PER_OWNER) {
                    return false;
                }
                fileInfo = new DynamicCodeFile(fileType, userId, loadingPackage);
                mFileUsageMap.put(path, fileInfo);
                return true;
+21 −0
Original line number Diff line number Diff line
@@ -16,9 +16,12 @@

package com.android.server.pm.dex;

import static com.android.server.pm.dex.PackageDynamicCodeLoading.MAX_FILES_PER_OWNER;
import static com.android.server.pm.dex.PackageDynamicCodeLoading.escape;
import static com.android.server.pm.dex.PackageDynamicCodeLoading.unescape;

import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
@@ -118,6 +121,24 @@ public class PackageDynamicCodeLoadingTests {
        assertThrows(() -> record(info, entry));
    }

    @Test
    public void testRecord_tooManyFiles_ignored() {
        PackageDynamicCodeLoading info = new PackageDynamicCodeLoading();
        int tooManyFiles = MAX_FILES_PER_OWNER + 1;
        for (int i = 1; i <= tooManyFiles; i++) {
            Entry entry = new Entry("owning.package", "/path/file" + i, 'D', 10, "loading.package");
            boolean added = record(info, entry);
            Set<Entry> entries = entriesFrom(info);
            if (i < tooManyFiles) {
                assertThat(entries).contains(entry);
                assertTrue(added);
            } else {
                assertThat(entries).doesNotContain(entry);
                assertFalse(added);
            }
        }
    }

    @Test
    public void testClear() {
        Entry[] entries = {