From 5f28002f095985ee1daad59863f091a651ffb9d4 Mon Sep 17 00:00:00 2001 From: riddle_hsu Date: Mon, 23 Nov 2015 13:22:03 +0800 Subject: Fix race condition when collecting op callback. Other threads may change mOpModeWatchers, mPackageModeWatchers by startWatchingMode, stopWatchingMode. Change-Id: Id260172979ddfc2df9331880805d16ee13e183eb --- .../java/com/android/server/AppOpsService.java | 46 +++++++++++----------- 1 file changed, 24 insertions(+), 22 deletions(-) (limited to 'services') diff --git a/services/core/java/com/android/server/AppOpsService.java b/services/core/java/com/android/server/AppOpsService.java index b5bf12c..83a0a99 100644 --- a/services/core/java/com/android/server/AppOpsService.java +++ b/services/core/java/com/android/server/AppOpsService.java @@ -560,33 +560,35 @@ public class AppOpsService extends IAppOpsService.Stub { String[] uidPackageNames = getPackagesForUid(uid); ArrayMap> callbackSpecs = null; - ArrayList callbacks = mOpModeWatchers.get(code); - if (callbacks != null) { - final int callbackCount = callbacks.size(); - for (int i = 0; i < callbackCount; i++) { - Callback callback = callbacks.get(i); - ArraySet changedPackages = new ArraySet<>(); - Collections.addAll(changedPackages, uidPackageNames); - callbackSpecs = new ArrayMap<>(); - callbackSpecs.put(callback, changedPackages); - } - } - - for (String uidPackageName : uidPackageNames) { - callbacks = mPackageModeWatchers.get(uidPackageName); + synchronized (this) { + ArrayList callbacks = mOpModeWatchers.get(code); if (callbacks != null) { - if (callbackSpecs == null) { - callbackSpecs = new ArrayMap<>(); - } final int callbackCount = callbacks.size(); for (int i = 0; i < callbackCount; i++) { Callback callback = callbacks.get(i); - ArraySet changedPackages = callbackSpecs.get(callback); - if (changedPackages == null) { - changedPackages = new ArraySet<>(); - callbackSpecs.put(callback, changedPackages); + ArraySet changedPackages = new ArraySet<>(); + Collections.addAll(changedPackages, uidPackageNames); + callbackSpecs = new ArrayMap<>(); + callbackSpecs.put(callback, changedPackages); + } + } + + for (String uidPackageName : uidPackageNames) { + callbacks = mPackageModeWatchers.get(uidPackageName); + if (callbacks != null) { + if (callbackSpecs == null) { + callbackSpecs = new ArrayMap<>(); + } + final int callbackCount = callbacks.size(); + for (int i = 0; i < callbackCount; i++) { + Callback callback = callbacks.get(i); + ArraySet changedPackages = callbackSpecs.get(callback); + if (changedPackages == null) { + changedPackages = new ArraySet<>(); + callbackSpecs.put(callback, changedPackages); + } + changedPackages.add(uidPackageName); } - changedPackages.add(uidPackageName); } } } -- cgit v1.1