-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary.cpp
More file actions
363 lines (335 loc) · 13.2 KB
/
Copy pathbinary.cpp
File metadata and controls
363 lines (335 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2025-Present Datadog, Inc.
#include "datadog/impl/core/attribute/binary.hpp"
#include <cinttypes>
#include <cstring>
#include "datadog/impl/core/attribute/cow.hpp"
#include "datadog/impl/core/storage/filesystem_wrapper.hpp"
#include "datadog/impl/core/util/unreachable.hpp"
namespace datadog::impl {
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
AttributeBinarySerialization::Result AttributeBinarySerialization::Write(
const Attribute& attr, File& file
) {
// Prepare a small buffer to contain our single-byte ValueType identifier, plus an
// additional value of up to 16 bytes (for primitive/UUID values or compound-type size
// prefix)
char buf[17];
// Store the attribute type in the first byte of the buffer: all binary-encoded
// attributes begin with a single-byte type identifier
buf[0] = static_cast<char>(attr.type);
// Helper function to flush N bytes from buf to the file, returning a Result struct
// that propagates any filesystem error and sets an `ok` flag iff FilesystemResult is
// OK _and_ all data was written
auto flush_buf = [&file, &buf](size_t n) -> Result {
auto write_res = file.Write(&buf[0], n);
if (write_res.value == FilesystemResult::OK && write_res.bytes_written == n) {
return Result{FilesystemResult::OK, true};
}
return Result{write_res.value, false};
};
// Branch based on type, populating buf and flushing it to the file for primitive
// types and UUID, or writing additional payload data for compound types
switch (attr.type) {
case ValueType::Null:
// Null: <1-byte type prefix>; no value
return flush_buf(1);
case ValueType::Bool:
case ValueType::Int:
case ValueType::Timestamp:
// Primitive types encoded as int64_t: <1-byte type prefix> + <8-byte value>
std::memcpy(&buf[1], &attr.value.i64, 8);
return flush_buf(9);
case ValueType::UInt:
// Primitive types encoded as uint64_t: <1-byte type prefix> + <8-byte value>
std::memcpy(&buf[1], &attr.value.u64, 8);
return flush_buf(9);
case ValueType::Double:
// Primitive types encoded as double: <1-byte type prefix> + <8-byte value>
std::memcpy(&buf[1], &attr.value.f64, 8);
return flush_buf(9);
case ValueType::UUID: {
// UUID: <1-byte type prefix> + <16-byte raw UUID value>
const CowValue& cow = *attr.value.ptr;
std::memcpy(&buf[1], cow.value.uid.bytes.data(), 16);
return flush_buf(17);
}
case ValueType::String: {
// String: <1-byte type prefix> + <8-byte unsigned length> ...
const CowValue& cow = *attr.value.ptr;
const uint64_t str_len = cow.value.string.size();
std::memcpy(&buf[1], &str_len, 8);
if (auto res = flush_buf(9); !res.ok) {
return res;
}
// ... + <str_len bytes of raw string data, w/o terminator>
if (str_len > 0) {
auto write_res = file.Write(cow.value.string.data(), str_len);
if (write_res.value != FilesystemResult::OK ||
write_res.bytes_written != str_len) {
return Result{write_res.value, false};
}
}
return Result{FilesystemResult::OK, true};
}
case ValueType::Array: {
// Array: <1-byte type prefix> + <8-byte unsigned item count> ...
const CowValue& cow = *attr.value.ptr;
const uint64_t num_items = cow.value.array.size();
std::memcpy(&buf[1], &num_items, 8);
if (auto res = flush_buf(9); !res.ok) {
return res;
}
// ...then, for each item in the array:
for (const Attribute& array_item : cow.value.array) {
// <binary-encoded Attribute value>
if (auto res = Write(array_item, file); !res.ok) {
return res;
}
}
return Result{FilesystemResult::OK, true};
}
case ValueType::Object: {
// Object: <1-byte type prefix> + <8-byte unsigned property count> ...
const CowValue& cow = *attr.value.ptr;
const uint64_t num_properties = cow.value.object.size();
std::memcpy(&buf[1], &num_properties, 8);
if (auto res = flush_buf(9); !res.ok) {
return res;
}
// ...then, for each property in the object:
for (const auto& [property_name, property_value] : cow.value.object) {
// <length-prefixed string indicating property name>
const uint64_t property_name_len = property_name.size();
std::memcpy(&buf[0], &property_name_len, 8);
if (auto res = flush_buf(8); !res.ok) {
return res;
}
if (property_name_len > 0) {
auto write_res = file.Write(property_name.data(), property_name_len);
if (write_res.value != FilesystemResult::OK ||
write_res.bytes_written != property_name_len) {
return Result{write_res.value, false};
}
}
// + <binary-encoded Attribute value>
if (auto res = Write(property_value, file); !res.ok) {
return res;
}
}
return Result{FilesystemResult::OK, true};
}
}
// switch/case is exhaustive for all value types: an invalid enum in attr.type would
// indicate memory corruption or serious misuse
DATADOG_UNREACHABLE;
}
AttributeBinarySerialization::Result AttributeBinarySerialization::Parse(
File& file, Attribute& out_attr
) {
return ParseImpl(file, out_attr, 0);
}
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
AttributeBinarySerialization::Result AttributeBinarySerialization::ParseImpl(
File& file, Attribute& out_attr, size_t depth
) {
// Halt parsing and consider the data invalid if we've exceeded a reasonable max
// recursion depth
if (depth > 64) {
return Result{FilesystemResult::OK, false};
}
// Prepare a buffer that's large enough to fit any primitive or UUID value, as well as
// single-byte type identifiers
char buf[16];
// Helper function to read N bytes from the file into buf, returning a Result struct
// that propagates any filesystem error and sets an `ok` flag iff FilesystemResult is
// OK _and_ we read all N bytes
auto read_into_buf = [&file, &buf](size_t n) -> Result {
auto read_res = file.Read(&buf[0], n);
if (read_res.value == FilesystemResult::OK && read_res.bytes_read == n) {
return Result{FilesystemResult::OK, true};
}
return Result{read_res.value, false};
};
// Read a single byte to get our encoded ValueType identifier
if (auto res = read_into_buf(1); !res.ok) {
return res;
}
// Verify that the type ID falls within the valid range for ValueType: if not, the
// data is malformed
const uint8_t type_id = buf[0];
static_assert(
static_cast<uint8_t>(ValueType::Object) == 9,
"a ValueType enumeration has been inserted before ValueType::Object; this will "
"break AttributeBinarySerialization compatibility"
);
if (type_id > static_cast<uint8_t>(ValueType::Object)) {
return Result{FilesystemResult::OK, false};
}
// Branch based on ValueType, initiating more reads and storing the relevant data in
// out_attr
switch (static_cast<ValueType>(type_id)) {
case ValueType::Null:
// Null: no value
out_attr.SetNull();
return Result{FilesystemResult::OK, true};
case ValueType::Bool: {
// Bool: int64_t encoding false (0) or true (canonically 1, but in practice
// anything other than 0)
if (auto res = read_into_buf(8); !res.ok) {
return res;
}
int64_t i64; // NOLINT(cppcoreguidelines-init-variables)
std::memcpy(&i64, &buf[0], 8);
out_attr.SetBool(i64 != 0);
return Result{FilesystemResult::OK, true};
}
case ValueType::Int: {
// Int: int64_t value
if (auto res = read_into_buf(8); !res.ok) {
return res;
}
int64_t i64; // NOLINT(cppcoreguidelines-init-variables)
std::memcpy(&i64, &buf[0], 8);
out_attr.SetInt(i64);
return Result{FilesystemResult::OK, true};
}
case ValueType::UInt: {
// UInt: uint64_t value
if (auto res = read_into_buf(8); !res.ok) {
return res;
}
uint64_t u64; // NOLINT(cppcoreguidelines-init-variables)
std::memcpy(&u64, &buf[0], 8);
out_attr.SetUInt(u64);
return Result{FilesystemResult::OK, true};
}
case ValueType::Timestamp: {
// Timestamp: int64_t value representing nanoseconds elapsed since epoch
if (auto res = read_into_buf(8); !res.ok) {
return res;
}
int64_t i64; // NOLINT(cppcoreguidelines-init-variables)
std::memcpy(&i64, &buf[0], 8);
out_attr.SetTimestamp(Timestamp{std::chrono::nanoseconds(i64)});
return Result{FilesystemResult::OK, true};
}
case ValueType::Double: {
// Double: double value
if (auto res = read_into_buf(8); !res.ok) {
return res;
}
double f64; // NOLINT(cppcoreguidelines-init-variables)
std::memcpy(&f64, &buf[0], 8);
out_attr.SetDouble(f64);
return Result{FilesystemResult::OK, true};
}
case ValueType::UUID: {
// UUID: raw 16 bytes held by UUID buffer
UUID uuid{};
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto read_res = file.Read(reinterpret_cast<char*>(uuid.bytes.data()), 16);
if (read_res.value != FilesystemResult::OK || read_res.bytes_read != 16) {
return Result{read_res.value, false};
}
out_attr.SetUUID(uuid);
return Result{FilesystemResult::OK, true};
}
case ValueType::String: {
// String: uint64_t length prefix...
if (auto res = read_into_buf(8); !res.ok) {
return res;
}
uint64_t str_len; // NOLINT(cppcoreguidelines-init-variables)
std::memcpy(&str_len, &buf[0], 8);
// (reject string values that exceed 64k, as a hard internal limit to prevent
// massive allocations due to corrupt or malicious data)
if (str_len > 65535) {
return Result{FilesystemResult::OK, false};
}
// ...then non-null-terminated character data
out_attr = Attribute(ValueType::String, CowValue::String(std::string_view{}));
CowValue& cow = *out_attr.value.ptr;
if (str_len > 0) {
cow.value.string.resize(str_len);
auto read_res = file.Read(cow.value.string.data(), str_len);
if (read_res.value != FilesystemResult::OK || read_res.bytes_read != str_len) {
return Result{read_res.value, false};
}
}
return Result{FilesystemResult::OK, true};
}
case ValueType::Array: {
// Array: uint64_t item count...
if (auto res = read_into_buf(8); !res.ok) {
return res;
}
uint64_t num_items; // NOLINT(cppcoreguidelines-init-variables)
std::memcpy(&num_items, &buf[0], 8);
// (reject array values that exceed 4096 items, as a hard internal limit to
// prevent massive allocations due to corrupt or malicious data)
if (num_items > 4096) {
return Result{FilesystemResult::OK, false};
}
// ...then a sequence of binary-encoded Attribute values for each item
out_attr = Attribute::Array(num_items);
for (uint64_t i = 0; i < num_items; ++i) {
Attribute item;
if (auto res = ParseImpl(file, item, depth + 1); !res.ok) {
return res;
}
out_attr.ArrayPush(item);
}
return Result{FilesystemResult::OK, true};
}
case ValueType::Object: {
// Object: uint64_t property count...
if (auto res = read_into_buf(8); !res.ok) {
return res;
}
uint64_t num_properties; // NOLINT(cppcoreguidelines-init-variables)
std::memcpy(&num_properties, &buf[0], 8);
// (reject object values that exceed 4096 properties, as a hard internal limit to
// prevent massive allocations due to corrupt or malicious data)
if (num_properties > 4096) {
return Result{FilesystemResult::OK, false};
}
// ...then a sequence of entries, each consisting of:
out_attr = Attribute::Object(num_properties);
for (uint64_t i = 0; i < num_properties; ++i) {
// Length-prefixed string for property name (rejecting overly-long names)
if (auto res = read_into_buf(8); !res.ok) {
return res;
}
uint64_t name_len; // NOLINT(cppcoreguidelines-init-variables)
std::memcpy(&name_len, &buf[0], 8);
if (name_len > 65535) {
return Result{FilesystemResult::OK, false};
}
std::string name;
if (name_len > 0) {
name.resize(name_len);
auto read_res = file.Read(name.data(), name_len);
if (read_res.value != FilesystemResult::OK ||
read_res.bytes_read != name_len) {
return Result{read_res.value, false};
}
}
// ...and binary-encoded property value
Attribute value;
if (auto res = ParseImpl(file, value, depth + 1); !res.ok) {
return res;
}
out_attr.SetObjectProperty(name, value);
}
return Result{FilesystemResult::OK, true};
}
}
// switch/case is exhaustive for all value types: an invalid enum in type_id is
// impossible given that we validated the value read from the file
DATADOG_UNREACHABLE;
}
} // namespace datadog::impl