Snap for 8414339 from 9f98f4c7cd2dca329bdb840f5d801756926b4bf9 to tm-qpr1-release

Change-Id: I50ee1eedb24946dd458fae3c2b01ef3a16dce49c
diff --git a/fuse_media.c b/fuse_media.c
index 0a2b33c..0ebb391 100644
--- a/fuse_media.c
+++ b/fuse_media.c
@@ -30,51 +30,40 @@
 DEFINE_BPF_PROG("fuse/media", AID_ROOT, AID_MEDIA_RW, fuse_media)
 (struct fuse_args* fa) {
     switch (fa->opcode) {
-        case FUSE_ACCESS | FUSE_PREFILTER: {
-            bpf_printk("Access: %d", fa->nodeid);
-            return FUSE_BPF_BACKING;
-        }
-
-        case FUSE_GETATTR | FUSE_PREFILTER: {
-            const struct fuse_getattr_in* fgi = fa->in_args[0].value;
-
-            bpf_printk("Get Attr %d", fgi->fh);
-            return FUSE_BPF_BACKING;
-        }
-
-        case FUSE_SETATTR | FUSE_PREFILTER: {
-            const struct fuse_setattr_in* fsi = fa->in_args[0].value;
-
-            bpf_printk("Set Attr %d", fsi->fh);
-            return FUSE_BPF_BACKING;
-        }
-
-        case FUSE_OPENDIR | FUSE_PREFILTER: {
-            bpf_printk("Open Dir: %d", fa->nodeid);
-            return FUSE_BPF_BACKING;
-        }
-
-        case FUSE_READDIR | FUSE_PREFILTER: {
-            const struct fuse_read_in* fri = fa->in_args[0].value;
-            bpf_printk("Read Dir: fh: %lu", fri->fh, fri->offset);
-            return FUSE_BPF_BACKING;
-        }
-
         case FUSE_LOOKUP | FUSE_PREFILTER: {
             const char* name = fa->in_args[0].value;
 
-            bpf_printk("Lookup: %lx %s", fa->nodeid, name);
+            bpf_printk("LOOKUP: %lx %s", fa->nodeid, name);
             if (fa->nodeid == 1)
                 return FUSE_BPF_USER_FILTER | FUSE_BPF_BACKING;
             else
                 return FUSE_BPF_BACKING;
         }
 
+            /* FUSE_FORGET */
+
+        case FUSE_GETATTR | FUSE_PREFILTER: {
+            const struct fuse_getattr_in* fgi = fa->in_args[0].value;
+
+            bpf_printk("GETATTR: %d", fgi->fh);
+            return FUSE_BPF_BACKING;
+        }
+
+        case FUSE_SETATTR | FUSE_PREFILTER: {
+            const struct fuse_setattr_in* fsi = fa->in_args[0].value;
+
+            bpf_printk("SETATTR: %d", fsi->fh);
+            return FUSE_BPF_BACKING;
+        }
+
+            /* FUSE_READLINK */
+            /* FUSE_SYMLINK */
+
         case FUSE_MKNOD | FUSE_PREFILTER: {
             const struct fuse_mknod_in* fmi = fa->in_args[0].value;
             const char* name = fa->in_args[1].value;
 
-            bpf_printk("mknod %s %x %x", name, fmi->rdev | fmi->mode, fmi->umask);
+            bpf_printk("MKNOD: %s %x %x", name, fmi->rdev | fmi->mode, fmi->umask);
             return FUSE_BPF_BACKING;
         }
 
@@ -82,21 +71,29 @@
             const struct fuse_mkdir_in* fmi = fa->in_args[0].value;
             const char* name = fa->in_args[1].value;
 
-            bpf_printk("mkdir: %s %x %x", name, fmi->mode, fmi->umask);
-            return FUSE_BPF_BACKING;
-        }
-
-        case FUSE_RMDIR | FUSE_PREFILTER: {
-            const char* name = fa->in_args[0].value;
-
-            bpf_printk("rmdir: %s", name);
+            bpf_printk("MKDIR: %s %x %x", name, fmi->mode, fmi->umask);
             return FUSE_BPF_BACKING;
         }
 
         case FUSE_UNLINK | FUSE_PREFILTER: {
             const char* name = fa->in_args[0].value;
 
-            bpf_printk("unlink: %s", name);
+            bpf_printk("UNLINK: %s", name);
+            return FUSE_BPF_BACKING;
+        }
+
+        case FUSE_RMDIR | FUSE_PREFILTER: {
+            const char* name = fa->in_args[0].value;
+
+            bpf_printk("RMDIR: %s", name);
+            return FUSE_BPF_BACKING;
+        }
+
+        case FUSE_RENAME | FUSE_PREFILTER: {
+            const char* name_old = fa->in_args[1].value;
+            const char* name_new = fa->in_args[2].value;
+
+            bpf_printk("RENAME: %s to %s", name_old, name_new);
             return FUSE_BPF_BACKING;
         }
 
@@ -104,93 +101,136 @@
             const struct fuse_link_in* fli = fa->in_args[0].value;
             const char* dst_name = fa->in_args[1].value;
 
-            bpf_printk("Link: %d %s", fli->oldnodeid, dst_name);
-            return FUSE_BPF_BACKING;
-        }
-
-        case FUSE_RELEASE | FUSE_PREFILTER: {
-            const struct fuse_release_in* fri = fa->in_args[0].value;
-
-            bpf_printk("Release: %d", fri->fh);
-            return FUSE_BPF_BACKING;
-        }
-
-        case FUSE_RELEASEDIR | FUSE_PREFILTER: {
-            const struct fuse_release_in* fri = fa->in_args[0].value;
-
-            bpf_printk("Release Dir: %d", fri->fh);
-            return FUSE_BPF_BACKING;
-        }
-
-        case FUSE_CREATE | FUSE_PREFILTER: {
-            bpf_printk("Create %s", fa->in_args[1].value);
+            bpf_printk("LINK: %d %s", fli->oldnodeid, dst_name);
             return FUSE_BPF_BACKING;
         }
 
         case FUSE_OPEN | FUSE_PREFILTER: {
-            bpf_printk("Open: %d", fa->nodeid);
+            bpf_printk("OPEN: %d", fa->nodeid);
             return FUSE_BPF_BACKING;
         }
 
         case FUSE_READ | FUSE_PREFILTER: {
             const struct fuse_read_in* fri = fa->in_args[0].value;
 
-            bpf_printk("Read: fh: %lu, offset %lu, size %lu", fri->fh, fri->offset, fri->size);
+            bpf_printk("READ: fh: %lu, offset %lu, size %lu", fri->fh, fri->offset, fri->size);
             return FUSE_BPF_BACKING;
         }
 
         case FUSE_WRITE | FUSE_PREFILTER: {
             const struct fuse_write_in* fwi = fa->in_args[0].value;
 
-            bpf_printk("Write: fh: %lu, offset %lu, size %lu", fwi->fh, fwi->offset, fwi->size);
+            bpf_printk("WRITE: fh: %lu, offset %lu, size %lu", fwi->fh, fwi->offset, fwi->size);
             return FUSE_BPF_BACKING;
         }
 
-        case FUSE_FLUSH | FUSE_PREFILTER: {
-            const struct fuse_flush_in* ffi = fa->in_args[0].value;
+            /* FUSE_STATFS */
 
-            bpf_printk("Flush %d", ffi->fh);
+        case FUSE_RELEASE | FUSE_PREFILTER: {
+            const struct fuse_release_in* fri = fa->in_args[0].value;
+
+            bpf_printk("RELEASE: %d", fri->fh);
             return FUSE_BPF_BACKING;
         }
 
-        case FUSE_FALLOCATE | FUSE_PREFILTER: {
-            const struct fuse_fallocate_in* ffa = fa->in_args[0].value;
+            /* FUSE_FSYNC */
 
-            bpf_printk("Fallocate %d %lu", ffa->fh, ffa->length);
+        case FUSE_SETXATTR | FUSE_PREFILTER: {
+            const char* name = fa->in_args[1].value;
+
+            bpf_printk("SETXATTR: %d %s", fa->nodeid, name);
             return FUSE_BPF_BACKING;
         }
 
         case FUSE_GETXATTR | FUSE_PREFILTER: {
             const char* name = fa->in_args[1].value;
 
-            bpf_printk("Getxattr %d %s", fa->nodeid, name);
+            bpf_printk("GETXATTR: %d %s", fa->nodeid, name);
             return FUSE_BPF_BACKING;
         }
 
         case FUSE_LISTXATTR | FUSE_PREFILTER: {
             const char* name = fa->in_args[1].value;
 
-            bpf_printk("Listxattr %d %s", fa->nodeid, name);
+            bpf_printk("LISTXATTR: %d %s", fa->nodeid, name);
             return FUSE_BPF_BACKING;
         }
 
-        case FUSE_SETXATTR | FUSE_PREFILTER: {
-            const char* name = fa->in_args[1].value;
+            /* FUSE_REMOVEXATTR */
 
-            bpf_printk("Setxattr %d %s", fa->nodeid, name);
+        case FUSE_FLUSH | FUSE_PREFILTER: {
+            const struct fuse_flush_in* ffi = fa->in_args[0].value;
+
+            bpf_printk("FLUSH: %d", ffi->fh);
             return FUSE_BPF_BACKING;
         }
 
+            /* FUSE_INIT */
+
+        case FUSE_OPENDIR | FUSE_PREFILTER: {
+            bpf_printk("OPENDIR: %d", fa->nodeid);
+            return FUSE_BPF_BACKING;
+        }
+
+        case FUSE_READDIR | FUSE_PREFILTER: {
+            const struct fuse_read_in* fri = fa->in_args[0].value;
+            bpf_printk("READDIR: fh: %lu", fri->fh, fri->offset);
+            return FUSE_BPF_BACKING;
+        }
+
+        case FUSE_RELEASEDIR | FUSE_PREFILTER: {
+            const struct fuse_release_in* fri = fa->in_args[0].value;
+
+            bpf_printk("RELEASEDIR: %d", fri->fh);
+            return FUSE_BPF_BACKING;
+        }
+
+            /* FUSE_FSYNCDIR */
+            /* FUSE_GETLK */
+            /* FUSE_SETLK */
+            /* FUSE_SETLKW */
+
+        case FUSE_ACCESS | FUSE_PREFILTER: {
+            bpf_printk("ACCESS: %d", fa->nodeid);
+            return FUSE_BPF_BACKING;
+        }
+
+        case FUSE_CREATE | FUSE_PREFILTER: {
+            bpf_printk("CREATE: %s", fa->in_args[1].value);
+            return FUSE_BPF_BACKING;
+        }
+
+            /* FUSE_INTERRUPT */
+            /* FUSE_BMAP */
+            /* FUSE_DESTROY */
+            /* FUSE_IOCTL */
+            /* FUSE_POLL */
+            /* FUSE_NOTIFY_REPLY */
+            /* FUSE_BATCH_FORGET */
+
+        case FUSE_FALLOCATE | FUSE_PREFILTER: {
+            const struct fuse_fallocate_in* ffa = fa->in_args[0].value;
+
+            bpf_printk("FALLOCATE: %d %lu", ffa->fh, ffa->length);
+            return FUSE_BPF_BACKING;
+        }
+
+            /* FUSE_READDIRPLUS */
+            /* FUSE_RENAME2 */
+            /* FUSE_LSEEK */
+            /* FUSE_COPY_FILE_RANGE */
+            /* CUSE_INIT */
+
         case FUSE_CANONICAL_PATH | FUSE_PREFILTER: {
-            bpf_printk("CanonicalPath %d", fa->nodeid);
+            bpf_printk("CANONICAL_PATH: %d", fa->nodeid);
             return FUSE_BPF_BACKING;
         }
 
         default:
             if (fa->opcode & FUSE_PREFILTER)
-                bpf_printk("prefilter *** UNKNOWN *** opcode: %d", fa->opcode & FUSE_OPCODE_FILTER);
+                bpf_printk("Prefilter *** UNKNOWN *** opcode: %d", fa->opcode & FUSE_OPCODE_FILTER);
             else if (fa->opcode & FUSE_POSTFILTER)
-                bpf_printk("postfilter *** UNKNOWN *** opcode: %d",
+                bpf_printk("Postfilter *** UNKNOWN *** opcode: %d",
                            fa->opcode & FUSE_OPCODE_FILTER);
             else
                 bpf_printk("*** UNKNOWN *** opcode: %d", fa->opcode);
diff --git a/time_in_state.c b/time_in_state.c
index 4404cd7..1bb7646 100644
--- a/time_in_state.c
+++ b/time_in_state.c
@@ -59,6 +59,46 @@
     int next_prio;
 };
 
+static inline __always_inline void update_uid(uint32_t uid, uint64_t delta, uint64_t time,
+                                              uint8_t freq_idx, uint32_t active,
+                                              uint32_t policy_active) {
+    time_key_t key = {.uid = uid, .bucket = freq_idx / FREQS_PER_ENTRY};
+    tis_val_t* val = bpf_uid_time_in_state_map_lookup_elem(&key);
+    if (!val) {
+        tis_val_t zero_val = {.ar = {0}};
+        bpf_uid_time_in_state_map_update_elem(&key, &zero_val, BPF_NOEXIST);
+        val = bpf_uid_time_in_state_map_lookup_elem(&key);
+    }
+    if (val) val->ar[freq_idx % FREQS_PER_ENTRY] += delta;
+
+    key.bucket = active / CPUS_PER_ENTRY;
+    concurrent_val_t* ct = bpf_uid_concurrent_times_map_lookup_elem(&key);
+    if (!ct) {
+        concurrent_val_t zero_val = {.active = {0}, .policy = {0}};
+        bpf_uid_concurrent_times_map_update_elem(&key, &zero_val, BPF_NOEXIST);
+        ct = bpf_uid_concurrent_times_map_lookup_elem(&key);
+    }
+    if (ct) ct->active[active % CPUS_PER_ENTRY] += delta;
+
+    if (policy_active / CPUS_PER_ENTRY != key.bucket) {
+        key.bucket = policy_active / CPUS_PER_ENTRY;
+        ct = bpf_uid_concurrent_times_map_lookup_elem(&key);
+        if (!ct) {
+            concurrent_val_t zero_val = {.active = {0}, .policy = {0}};
+            bpf_uid_concurrent_times_map_update_elem(&key, &zero_val, BPF_NOEXIST);
+            ct = bpf_uid_concurrent_times_map_lookup_elem(&key);
+        }
+    }
+    if (ct) ct->policy[policy_active % CPUS_PER_ENTRY] += delta;
+    uint64_t* uid_last_update = bpf_uid_last_update_map_lookup_elem(&uid);
+    if (uid_last_update) {
+        *uid_last_update = time;
+    } else {
+        bpf_uid_last_update_map_update_elem(&uid, &time, BPF_NOEXIST);
+    }
+    return;
+}
+
 DEFINE_BPF_PROG("tracepoint/sched/sched_switch", AID_ROOT, AID_SYSTEM, tp_sched_switch)
 (struct switch_args* args) {
     const int ALLOW = 1;  // return 1 to avoid blocking simpleperf from receiving events.
@@ -116,15 +156,20 @@
     uint8_t freq_idx = *freq_idxp - 1;
 
     uint32_t uid = bpf_get_current_uid_gid();
-    time_key_t key = {.uid = uid, .bucket = freq_idx / FREQS_PER_ENTRY};
-    tis_val_t* val = bpf_uid_time_in_state_map_lookup_elem(&key);
-    if (!val) {
-        tis_val_t zero_val = {.ar = {0}};
-        bpf_uid_time_in_state_map_update_elem(&key, &zero_val, BPF_NOEXIST);
-        val = bpf_uid_time_in_state_map_lookup_elem(&key);
-    }
     uint64_t delta = time - old_last;
-    if (val) val->ar[freq_idx % FREQS_PER_ENTRY] += delta;
+
+    // For UIDs in the SDK sandbox range, we account per-UID times twice, both to the corresponding
+    // app uid and to the "virtual" UID AID_SDK_SANDBOX which is reserved for collecting total times
+    // across all SDK sandbox UIDs. Special handling for this reserved UID in framework code
+    // prevents double counting in systemwide totals.
+    if (((uid % AID_USER_OFFSET) >= AID_SDK_SANDBOX_PROCESS_START) &&
+        ((uid % AID_USER_OFFSET) <= AID_SDK_SANDBOX_PROCESS_END)) {
+        uid -= AID_SDK_SANDBOX_PROCESS_START - AID_APP_START;
+        update_uid(uid, delta, time, freq_idx, nactive, policy_nactive);
+        update_uid(AID_SDK_SANDBOX, delta, time, freq_idx, nactive, policy_nactive);
+    } else {
+        update_uid(uid, delta, time, freq_idx, nactive, policy_nactive);
+    }
 
     // Add delta to total.
     const uint32_t total_freq_idx = freq_idx < MAX_FREQS_FOR_TOTAL ? freq_idx :
@@ -170,31 +215,6 @@
         }
         if (task_val) task_val->ar[freq_idx % FREQS_PER_ENTRY] += delta;
     }
-    key.bucket = nactive / CPUS_PER_ENTRY;
-    concurrent_val_t* ct = bpf_uid_concurrent_times_map_lookup_elem(&key);
-    if (!ct) {
-        concurrent_val_t zero_val = {.active = {0}, .policy = {0}};
-        bpf_uid_concurrent_times_map_update_elem(&key, &zero_val, BPF_NOEXIST);
-        ct = bpf_uid_concurrent_times_map_lookup_elem(&key);
-    }
-    if (ct) ct->active[nactive % CPUS_PER_ENTRY] += delta;
-
-    if (policy_nactive / CPUS_PER_ENTRY != key.bucket) {
-        key.bucket = policy_nactive / CPUS_PER_ENTRY;
-        ct = bpf_uid_concurrent_times_map_lookup_elem(&key);
-        if (!ct) {
-            concurrent_val_t zero_val = {.active = {0}, .policy = {0}};
-            bpf_uid_concurrent_times_map_update_elem(&key, &zero_val, BPF_NOEXIST);
-            ct = bpf_uid_concurrent_times_map_lookup_elem(&key);
-        }
-    }
-    if (ct) ct->policy[policy_nactive % CPUS_PER_ENTRY] += delta;
-    uint64_t* uid_last_update = bpf_uid_last_update_map_lookup_elem(&uid);
-    if (uid_last_update) {
-        *uid_last_update = time;
-    } else {
-        bpf_uid_last_update_map_update_elem(&uid, &time, BPF_NOEXIST);
-    }
     return ALLOW;
 }
 
diff --git a/time_in_state_test.cpp b/time_in_state_test.cpp
index e94ec1a..1042221 100644
--- a/time_in_state_test.cpp
+++ b/time_in_state_test.cpp
@@ -241,3 +241,26 @@
 
     assertConcurrentTimes(42, 0, {100}, {100});
 }
+
+TEST(time_in_state, tp_sched_switch_sdk_sandbox) {
+    mock_bpf_set_ktime_ns(1000);
+    mock_bpf_set_current_uid_gid(AID_SDK_SANDBOX_PROCESS_START);
+
+    initCpuPolicy(0, {0}, {1000, 2000}, true);
+
+    enableTracking();
+
+    mock_bpf_set_smp_processor_id(0);
+
+    noteSchedSwitch(0, 1);
+
+    mock_bpf_set_ktime_ns(1100);
+
+    noteSchedSwitch(1, 2);
+
+    assertTimeInState(AID_APP_START, 0, {100, 0});
+    assertTimeInState(AID_SDK_SANDBOX, 0, {100, 0});
+
+    assertConcurrentTimes(AID_APP_START, 0, {100}, {100});
+    assertConcurrentTimes(AID_SDK_SANDBOX, 0, {100}, {100});
+}