xref: /aosp_15_r20/external/perfetto/src/trace_redaction/prune_package_list.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "src/trace_redaction/prune_package_list.h"
18 
19 #include <string>
20 
21 #include "perfetto/base/logging.h"
22 #include "perfetto/base/status.h"
23 #include "perfetto/protozero/scattered_heap_buffer.h"
24 #include "src/trace_redaction/proto_util.h"
25 
26 #include "protos/perfetto/trace/android/packages_list.pbzero.h"
27 
28 namespace perfetto::trace_redaction {
29 
Transform(const Context & context,std::string * packet) const30 base::Status PrunePackageList::Transform(const Context& context,
31                                          std::string* packet) const {
32   if (!context.package_uid.has_value()) {
33     return base::ErrStatus("PrunePackageList: missing package uid.");
34   }
35 
36   protozero::ProtoDecoder decoder(*packet);
37 
38   protos::pbzero::TracePacket::Decoder trace_packet_decoder(*packet);
39 
40   auto package_list =
41       decoder.FindField(protos::pbzero::TracePacket::kPackagesListFieldNumber);
42 
43   if (!package_list.valid()) {
44     return base::OkStatus();
45   }
46 
47   protozero::HeapBuffered<protos::pbzero::TracePacket> packet_message;
48 
49   for (auto field = decoder.ReadField(); field.valid();
50        field = decoder.ReadField()) {
51     if (field.id() == protos::pbzero::TracePacket::kPackagesListFieldNumber) {
52       OnPackageList(context, field.as_bytes(),
53                     packet_message->set_packages_list());
54     } else {
55       proto_util::AppendField(field, packet_message.get());
56     }
57   }
58 
59   packet->assign(packet_message.SerializeAsString());
60 
61   return base::OkStatus();
62 }
63 
OnPackageList(const Context & context,protozero::ConstBytes bytes,protos::pbzero::PackagesList * message) const64 void PrunePackageList::OnPackageList(
65     const Context& context,
66     protozero::ConstBytes bytes,
67     protos::pbzero::PackagesList* message) const {
68   PERFETTO_DCHECK(message);
69 
70   protozero::ProtoDecoder decoder(bytes);
71 
72   for (auto field = decoder.ReadField(); field.valid();
73        field = decoder.ReadField()) {
74     if (field.id() == protos::pbzero::PackagesList::kPackagesFieldNumber) {
75       // The package uid should already be normalized (see
76       // find_package_info.cc).
77       //
78       // If there are more than one package entry (see
79       // trace_redaction_framework.h for more details), we need to match all
80       // instances here because retained processes will reference them.
81       protos::pbzero::PackagesList::PackageInfo::Decoder info(field.as_bytes());
82 
83       if (info.has_uid() && NormalizeUid(info.uid()) == context.package_uid) {
84         proto_util::AppendField(field, message);
85       }
86     } else {
87       proto_util::AppendField(field, message);
88     }
89   }
90 }
91 
92 }  // namespace perfetto::trace_redaction
93