blob: b606818a592170e9a4a075ef3e918e1096336737 [file] [log] [blame]
Lalit Maganti45172a82018-06-01 03:04:43 +01001/*
2 * Copyright (C) 2018 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 "perfetto/protozero/proto_decoder.h"
18
19#include <string.h>
Primiano Tucci22727922019-06-01 10:07:42 +010020#include <limits>
Lalit Maganti45172a82018-06-01 03:04:43 +010021
22#include "perfetto/base/logging.h"
Primiano Tucci2c5488f2019-06-01 03:27:28 +010023#include "perfetto/ext/base/utils.h"
Primiano Tuccif6a3cb02018-07-02 15:38:50 +010024#include "perfetto/protozero/proto_utils.h"
Lalit Maganti45172a82018-06-01 03:04:43 +010025
26namespace protozero {
27
28using namespace proto_utils;
29
Primiano Tuccic1678872019-03-20 11:30:54 +000030#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
Lalit Maganti45172a82018-06-01 03:04:43 +010031#error Unimplemented for big endian archs.
32#endif
33
Primiano Tuccic1678872019-03-20 11:30:54 +000034namespace {
35
36struct ParseFieldResult {
Primiano Tucci2cfc7fd2019-11-28 16:07:27 +000037 enum ParseResult { kAbort, kSkip, kOk };
38 ParseResult parse_res;
Primiano Tuccic1678872019-03-20 11:30:54 +000039 const uint8_t* next;
40 Field field;
41};
42
43// Parses one field and returns the field itself and a pointer to the next
44// field to parse. If parsing fails, the returned |next| == |buffer|.
45PERFETTO_ALWAYS_INLINE ParseFieldResult
46ParseOneField(const uint8_t* const buffer, const uint8_t* const end) {
Primiano Tucci2cfc7fd2019-11-28 16:07:27 +000047 ParseFieldResult res{ParseFieldResult::kAbort, buffer, Field{}};
Lalit Maganti45172a82018-06-01 03:04:43 +010048
49 // The first byte of a proto field is structured as follows:
50 // The least 3 significant bits determine the field type.
51 // The most 5 significant bits determine the field id. If MSB == 1, the
52 // field id continues on the next bytes following the VarInt encoding.
53 const uint8_t kFieldTypeNumBits = 3;
54 const uint64_t kFieldTypeMask = (1 << kFieldTypeNumBits) - 1; // 0000 0111;
Primiano Tuccic1678872019-03-20 11:30:54 +000055 const uint8_t* pos = buffer;
Lalit Maganti45172a82018-06-01 03:04:43 +010056
Lalit Magantidf3e9262018-06-04 17:45:00 +010057 // If we've already hit the end, just return an invalid field.
Primiano Tuccic1678872019-03-20 11:30:54 +000058 if (PERFETTO_UNLIKELY(pos >= end))
59 return res;
Lalit Magantidf3e9262018-06-04 17:45:00 +010060
Primiano Tuccic1678872019-03-20 11:30:54 +000061 uint64_t preamble = 0;
Ryancbd16152019-09-04 16:54:45 +010062 if (PERFETTO_LIKELY(*pos < 0x80)) { // Fastpath for fields with ID < 16.
Primiano Tuccic1678872019-03-20 11:30:54 +000063 preamble = *(pos++);
Primiano Tuccide2476b2018-08-23 00:53:20 +020064 } else {
Primiano Tuccib3e08d02019-11-19 11:10:11 +000065 const uint8_t* next = ParseVarInt(pos, end, &preamble);
66 if (PERFETTO_UNLIKELY(pos == next))
67 return res;
68 pos = next;
Primiano Tuccide2476b2018-08-23 00:53:20 +020069 }
Lalit Maganti45172a82018-06-01 03:04:43 +010070
Primiano Tuccic1678872019-03-20 11:30:54 +000071 uint32_t field_id = static_cast<uint32_t>(preamble >> kFieldTypeNumBits);
72 if (field_id == 0 || pos >= end)
73 return res;
Primiano Tuccide2476b2018-08-23 00:53:20 +020074
Primiano Tuccic1678872019-03-20 11:30:54 +000075 auto field_type = static_cast<uint8_t>(preamble & kFieldTypeMask);
76 const uint8_t* new_pos = pos;
77 uint64_t int_value = 0;
Primiano Tucci2cfc7fd2019-11-28 16:07:27 +000078 uint64_t size = 0;
Lalit Maganti45172a82018-06-01 03:04:43 +010079
Primiano Tuccic1678872019-03-20 11:30:54 +000080 switch (field_type) {
81 case static_cast<uint8_t>(ProtoWireType::kVarInt): {
82 new_pos = ParseVarInt(pos, end, &int_value);
Lalit Maganti45172a82018-06-01 03:04:43 +010083
Lalit Magantif1c27b12018-06-05 12:45:29 +010084 // new_pos not being greater than pos means ParseVarInt could not fully
85 // parse the number. This is because we are out of space in the buffer.
86 // Set the id to zero and return but don't update the offset so a future
87 // read can read this field.
Primiano Tuccic1678872019-03-20 11:30:54 +000088 if (PERFETTO_UNLIKELY(new_pos == pos))
89 return res;
90
Lalit Maganti45172a82018-06-01 03:04:43 +010091 break;
92 }
Lalit Maganti45172a82018-06-01 03:04:43 +010093
Primiano Tuccic1678872019-03-20 11:30:54 +000094 case static_cast<uint8_t>(ProtoWireType::kLengthDelimited): {
95 uint64_t payload_length;
96 new_pos = ParseVarInt(pos, end, &payload_length);
97 if (PERFETTO_UNLIKELY(new_pos == pos))
98 return res;
99
100 // ParseVarInt guarantees that |new_pos| <= |end| when it succeeds;
101 if (payload_length > static_cast<uint64_t>(end - new_pos))
102 return res;
Hector Dearmancf4ef4b2019-02-27 19:10:21 +0000103
Primiano Tucci2cfc7fd2019-11-28 16:07:27 +0000104 const uintptr_t payload_start = reinterpret_cast<uintptr_t>(new_pos);
105 int_value = payload_start;
106 size = payload_length;
Primiano Tuccic1678872019-03-20 11:30:54 +0000107 new_pos += payload_length;
Lalit Maganti45172a82018-06-01 03:04:43 +0100108 break;
109 }
Primiano Tuccic1678872019-03-20 11:30:54 +0000110
111 case static_cast<uint8_t>(ProtoWireType::kFixed64): {
112 new_pos = pos + sizeof(uint64_t);
113 if (PERFETTO_UNLIKELY(new_pos > end))
114 return res;
115 memcpy(&int_value, pos, sizeof(uint64_t));
Primiano Tuccide2476b2018-08-23 00:53:20 +0200116 break;
117 }
Primiano Tuccic1678872019-03-20 11:30:54 +0000118
119 case static_cast<uint8_t>(ProtoWireType::kFixed32): {
120 new_pos = pos + sizeof(uint32_t);
121 if (PERFETTO_UNLIKELY(new_pos > end))
122 return res;
123 memcpy(&int_value, pos, sizeof(uint32_t));
124 break;
125 }
126
127 default:
128 PERFETTO_DLOG("Invalid proto field type: %u", field_type);
129 return res;
130 }
131
Primiano Tucci2cfc7fd2019-11-28 16:07:27 +0000132 res.next = new_pos;
133
Primiano Tuccic1678872019-03-20 11:30:54 +0000134 if (PERFETTO_UNLIKELY(field_id > std::numeric_limits<uint16_t>::max())) {
Primiano Tucci2cfc7fd2019-11-28 16:07:27 +0000135 PERFETTO_DLOG("Skipping field %" PRIu32 " because its id > 0xFFFF",
136 field_id);
137 res.parse_res = ParseFieldResult::kSkip;
Primiano Tuccic1678872019-03-20 11:30:54 +0000138 return res;
139 }
140
Primiano Tucci2cfc7fd2019-11-28 16:07:27 +0000141 if (PERFETTO_UNLIKELY(size > proto_utils::kMaxMessageLength)) {
142 PERFETTO_DLOG("Skipping field %" PRIu32 " because it's too big (%" PRIu64
143 " KB)",
144 field_id, size / 1024);
145 res.parse_res = ParseFieldResult::kSkip;
146 return res;
147 }
148
149 res.parse_res = ParseFieldResult::kOk;
Primiano Tuccic1678872019-03-20 11:30:54 +0000150 res.field.initialize(static_cast<uint16_t>(field_id), field_type, int_value,
Primiano Tucci2cfc7fd2019-11-28 16:07:27 +0000151 static_cast<uint32_t>(size));
Primiano Tuccic1678872019-03-20 11:30:54 +0000152 return res;
153}
154
155} // namespace
156
157Field ProtoDecoder::FindField(uint32_t field_id) {
Eric Secklerd0125972019-04-29 09:49:22 +0000158 Field res{};
Primiano Tuccic1678872019-03-20 11:30:54 +0000159 auto old_position = read_ptr_;
160 read_ptr_ = begin_;
161 for (auto f = ReadField(); f.valid(); f = ReadField()) {
162 if (f.id() == field_id) {
163 res = f;
Primiano Tuccide2476b2018-08-23 00:53:20 +0200164 break;
165 }
Lalit Maganti45172a82018-06-01 03:04:43 +0100166 }
Primiano Tuccic1678872019-03-20 11:30:54 +0000167 read_ptr_ = old_position;
168 return res;
169}
170
171PERFETTO_ALWAYS_INLINE
172Field ProtoDecoder::ReadField() {
Primiano Tucci2cfc7fd2019-11-28 16:07:27 +0000173 ParseFieldResult res;
174 do {
175 res = ParseOneField(read_ptr_, end_);
176 read_ptr_ = res.next;
177 } while (PERFETTO_UNLIKELY(res.parse_res == ParseFieldResult::kSkip));
Primiano Tuccic1678872019-03-20 11:30:54 +0000178 return res.field;
179}
180
181void TypedProtoDecoderBase::ParseAllFields() {
182 const uint8_t* cur = begin_;
183 ParseFieldResult res;
184 for (;;) {
185 res = ParseOneField(cur, end_);
Primiano Tucci2cfc7fd2019-11-28 16:07:27 +0000186 PERFETTO_DCHECK(res.parse_res != ParseFieldResult::kOk || res.next != cur);
Primiano Tuccic1678872019-03-20 11:30:54 +0000187 cur = res.next;
Primiano Tucci2cfc7fd2019-11-28 16:07:27 +0000188 if (PERFETTO_UNLIKELY(res.parse_res == ParseFieldResult::kSkip)) {
189 continue;
190 } else if (PERFETTO_UNLIKELY(res.parse_res == ParseFieldResult::kAbort)) {
191 break;
192 }
193 PERFETTO_DCHECK(res.parse_res == ParseFieldResult::kOk);
194 PERFETTO_DCHECK(res.field.valid());
Primiano Tuccic1678872019-03-20 11:30:54 +0000195 auto field_id = res.field.id();
Hector Dearmanc7c267a2019-03-25 13:06:21 +0000196 if (PERFETTO_UNLIKELY(field_id >= num_fields_))
Primiano Tuccic1678872019-03-20 11:30:54 +0000197 continue;
198
199 Field* fld = &fields_[field_id];
200 if (PERFETTO_LIKELY(!fld->valid())) {
201 // This is the first time we see this field.
202 *fld = std::move(res.field);
203 } else {
Hector Dearmanc7c267a2019-03-25 13:06:21 +0000204 // Repeated field case.
205 // In this case we need to:
206 // 1. Append the last value of the field to end of the repeated field
207 // storage.
208 // 2. Replace the default instance at offset |field_id| with the current
209 // value. This is because in case of repeated field a call to Get(X) is
210 // supposed to return the last value of X, not the first one.
211 // This is so that the RepeatedFieldIterator will iterate in the right
212 // order, see comments on RepeatedFieldIterator.
213 if (PERFETTO_UNLIKELY(size_ >= capacity_)) {
Primiano Tuccic1678872019-03-20 11:30:54 +0000214 ExpandHeapStorage();
Hector Dearmanc7c267a2019-03-25 13:06:21 +0000215 // ExpandHeapStorage moves fields_ so we need to update the ptr to fld:
216 fld = &fields_[field_id];
Primiano Tuccic1678872019-03-20 11:30:54 +0000217 PERFETTO_DCHECK(size_ < capacity_);
218 }
Hector Dearmanc7c267a2019-03-25 13:06:21 +0000219 fields_[size_++] = *fld;
220 *fld = std::move(res.field);
Primiano Tuccic1678872019-03-20 11:30:54 +0000221 }
222 }
223 read_ptr_ = res.next;
224}
225
226void TypedProtoDecoderBase::ExpandHeapStorage() {
227 uint32_t new_capacity = capacity_ * 2;
228 PERFETTO_CHECK(new_capacity > size_);
229 std::unique_ptr<Field[]> new_storage(new Field[new_capacity]);
230
Lalit Magantiea0254c2019-11-15 21:58:46 +0000231 static_assert(std::is_trivially_copyable<Field>::value,
Primiano Tuccic1678872019-03-20 11:30:54 +0000232 "Field must be trivially copyable");
233 memcpy(&new_storage[0], fields_, sizeof(Field) * size_);
234
235 heap_storage_ = std::move(new_storage);
236 fields_ = &heap_storage_[0];
237 capacity_ = new_capacity;
Lalit Maganti45172a82018-06-01 03:04:43 +0100238}
239
Lalit Maganti45172a82018-06-01 03:04:43 +0100240} // namespace protozero