-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathunreal.rs
427 lines (349 loc) · 15.4 KB
/
unreal.rs
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
use chrono::{TimeZone, Utc};
use relay_config::Config;
use relay_general::protocol::{
AsPair, Breadcrumb, ClientSdkInfo, Context, Contexts, DeviceContext, Event, EventId,
GpuContext, LenientString, Level, LogEntry, Message, OsContext, TagEntry, Tags, Timestamp,
User, UserReport, Values,
};
use relay_general::types::{self, Annotated, Array, Object, Value};
use symbolic_unreal::{
Unreal4Context, Unreal4Crash, Unreal4Error, Unreal4ErrorKind, Unreal4FileType, Unreal4LogEntry,
};
use crate::constants::{
ITEM_NAME_BREADCRUMBS1, ITEM_NAME_BREADCRUMBS2, ITEM_NAME_EVENT, UNREAL_USER_HEADER,
};
use crate::envelope::{AttachmentType, ContentType, Envelope, Item, ItemType};
/// Maximum number of unreal logs to parse for breadcrumbs.
const MAX_NUM_UNREAL_LOGS: usize = 40;
/// Name of the custom XML tag in Unreal GameData for Sentry event payloads.
const SENTRY_PAYLOAD_KEY: &str = "__sentry";
/// Client SDK name used for the event payload to identify the UE4 crash reporter.
const CLIENT_SDK_NAME: &str = "unreal.crashreporter";
fn get_event_item(data: &[u8]) -> Result<Option<Item>, Unreal4Error> {
let mut context = Unreal4Context::parse(data)?;
let json = match context.game_data.remove(SENTRY_PAYLOAD_KEY) {
Some(json) if !json.is_empty() => json,
_ => return Ok(None),
};
relay_log::trace!("adding event payload from unreal context");
let mut item = Item::new(ItemType::Event);
item.set_payload(ContentType::Json, json);
Ok(Some(item))
}
/// Expands Unreal 4 items inside an envelope.
///
/// If the envelope does NOT contain an `UnrealReport` item, it doesn't do anything. If the envelope
/// contains an `UnrealReport` item, it removes it from the envelope and inserts new items for each
/// of its contents.
///
/// After this, the `EnvelopeProcessor` should be able to process the envelope the same way it
/// processes any other envelopes.
pub fn expand_unreal_envelope(
unreal_item: Item,
envelope: &mut Envelope,
config: &Config,
) -> Result<(), Unreal4Error> {
let payload = unreal_item.payload();
let crash = Unreal4Crash::parse_with_limit(&payload, config.max_envelope_size())?;
let mut has_event = envelope
.get_item_by(|item| item.ty() == &ItemType::Event)
.is_some();
for file in crash.files() {
let (content_type, attachment_type) = match file.ty() {
Unreal4FileType::Minidump => (ContentType::Minidump, AttachmentType::Minidump),
Unreal4FileType::AppleCrashReport => {
(ContentType::Text, AttachmentType::AppleCrashReport)
}
Unreal4FileType::Log => (ContentType::Text, AttachmentType::UnrealLogs),
Unreal4FileType::Config => (ContentType::OctetStream, AttachmentType::Attachment),
Unreal4FileType::Context => (ContentType::Xml, AttachmentType::UnrealContext),
_ => match file.name() {
self::ITEM_NAME_EVENT => (ContentType::MsgPack, AttachmentType::EventPayload),
self::ITEM_NAME_BREADCRUMBS1 => (ContentType::MsgPack, AttachmentType::Breadcrumbs),
self::ITEM_NAME_BREADCRUMBS2 => (ContentType::MsgPack, AttachmentType::Breadcrumbs),
_ => (ContentType::OctetStream, AttachmentType::Attachment),
},
};
if !has_event && attachment_type == AttachmentType::UnrealContext {
if let Some(event_item) = get_event_item(file.data())? {
envelope.add_item(event_item);
has_event = true;
}
}
let mut item = Item::new(ItemType::Attachment);
item.set_filename(file.name());
// TODO: This clones data. Update symbolic to allow moving the bytes out.
item.set_payload(content_type, file.data().to_owned());
item.set_attachment_type(attachment_type);
envelope.add_item(item);
}
if !super::check_envelope_size_limits(config, envelope) {
return Err(Unreal4ErrorKind::TooLarge.into());
}
Ok(())
}
fn merge_unreal_user_info(event: &mut Event, user_info: &str) {
let mut parts = user_info.split('|');
if let Some(user_id) = parts.next() {
let user = event.user.value_mut().get_or_insert_with(User::default);
user.id = Annotated::new(LenientString(user_id.to_owned()));
}
if let Some(epic_account_id) = parts.next() {
let tags = event.tags.value_mut().get_or_insert_with(Tags::default);
tags.push(Annotated::new(TagEntry(
Annotated::new("epic_account_id".to_string()),
Annotated::new(epic_account_id.to_string()),
)));
}
if let Some(machine_id) = parts.next() {
let tags = event.tags.value_mut().get_or_insert_with(Tags::default);
tags.push(Annotated::new(TagEntry::from_pair((
Annotated::new("machine_id".to_string()),
Annotated::new(machine_id.to_string()),
))));
}
}
/// Merge an unreal logs object into event breadcrumbs.
fn merge_unreal_logs(event: &mut Event, data: &[u8]) -> Result<(), Unreal4Error> {
let logs = Unreal4LogEntry::parse(data, MAX_NUM_UNREAL_LOGS)?;
let breadcrumbs = event
.breadcrumbs
.value_mut()
.get_or_insert_with(Values::default)
.values
.value_mut()
.get_or_insert_with(Array::default);
for log in logs {
let timestamp = log
.timestamp
.and_then(|ts| {
Utc.timestamp_opt(ts.unix_timestamp(), ts.nanosecond())
.latest()
})
.map(Timestamp);
breadcrumbs.push(Annotated::new(Breadcrumb {
timestamp: Annotated::from(timestamp),
category: Annotated::from(log.component),
message: Annotated::new(log.message),
..Breadcrumb::default()
}));
}
Ok(())
}
/// Parses `UserReport` from unreal user information.
fn get_unreal_user_report(event_id: EventId, context: &mut Unreal4Context) -> Option<Item> {
let runtime_props = context.runtime_properties.as_mut()?;
let user_description = runtime_props.user_description.take()?;
let user_name = runtime_props
.username
.clone()
.unwrap_or_else(|| "unknown".to_owned());
let user_report = UserReport {
email: "".to_owned(),
comments: user_description,
event_id,
name: user_name,
};
let json = serde_json::to_string(&user_report).ok()?;
let mut item = Item::new(ItemType::UserReport);
item.set_payload(ContentType::Json, json);
Some(item)
}
/// Merges an unreal context object into an event
fn merge_unreal_context(event: &mut Event, context: Unreal4Context) {
let mut runtime_props = match context.runtime_properties {
Some(runtime_props) => runtime_props,
None => return,
};
if let Some(msg) = runtime_props.error_message.take() {
event
.logentry
.get_or_insert_with(LogEntry::default)
.formatted = Annotated::new(Message::from(msg));
}
if let Some(username) = runtime_props.username.take() {
event
.user
.get_or_insert_with(User::default)
.username
.set_value(Some(username));
}
let contexts = event.contexts.get_or_insert_with(Contexts::default);
if let Some(memory_physical) = runtime_props.memory_stats_total_physical.take() {
let device_context = contexts.get_or_insert_with(DeviceContext::default_key(), || {
Context::Device(Box::default())
});
if let Context::Device(device_context) = device_context {
device_context.memory_size = Annotated::new(memory_physical);
}
}
// OS information is likely overwritten by Minidump processing later.
if let Some(os_major) = runtime_props.misc_os_version_major.take() {
let os_context =
contexts.get_or_insert_with(OsContext::default_key(), || Context::Os(Box::default()));
if let Context::Os(os_context) = os_context {
os_context.name = Annotated::new(os_major);
}
}
if let Some(gpu_brand) = runtime_props.misc_primary_gpu_brand.take() {
let gpu_context =
contexts.get_or_insert_with(GpuContext::default_key(), || Context::Gpu(Box::default()));
if let Context::Gpu(gpu_context) = gpu_context {
gpu_context.name = Annotated::new(gpu_brand);
}
}
if runtime_props.is_assert.unwrap_or(false) {
event.level = Annotated::new(Level::Error)
}
if runtime_props.is_ensure.unwrap_or(false) {
event.level = Annotated::new(Level::Warning)
}
// Modules are not used and later replaced with Modules from the Minidump or Apple Crash Report.
runtime_props.modules.take();
// Promote all game data (except the special `__sentry` key) into a context.
if !context.game_data.is_empty() {
let game_context = contexts.get_or_insert_with("game", || Context::Other(Object::new()));
if let Context::Other(game_context) = game_context {
let filtered_keys = context
.game_data
.into_iter()
.filter(|(key, _)| key != SENTRY_PAYLOAD_KEY)
.map(|(key, value)| (key, Annotated::new(Value::String(value))));
game_context.extend(filtered_keys);
}
}
// Add sdk information for analytics.
event.client_sdk.get_or_insert_with(|| ClientSdkInfo {
name: CLIENT_SDK_NAME.to_owned().into(),
version: runtime_props
.crash_reporter_client_version
.take()
.unwrap_or_else(|| "0.0.0".to_owned())
.into(),
..ClientSdkInfo::default()
});
if let Ok(Some(Value::Object(props))) = types::to_value(&runtime_props) {
let unreal_context =
contexts.get_or_insert_with("unreal", || Context::Other(Object::new()));
if let Context::Other(unreal_context) = unreal_context {
unreal_context.extend(props);
}
}
}
pub fn process_unreal_envelope(
event: &mut Annotated<Event>,
envelope: &mut Envelope,
) -> Result<(), Unreal4Error> {
let user_header = envelope
.get_header(UNREAL_USER_HEADER)
.and_then(Value::as_str);
let context_item =
envelope.get_item_by(|item| item.attachment_type() == Some(&AttachmentType::UnrealContext));
let logs_item =
envelope.get_item_by(|item| item.attachment_type() == Some(&AttachmentType::UnrealLogs));
// Early exit if there is no information.
if user_header.is_none() && context_item.is_none() && logs_item.is_none() {
return Ok(());
}
// If we have UE4 info, ensure an event is there to fill. DO NOT fill if there is no unreal
// information, or otherwise `EnvelopeProcessorService::process` breaks.
let event = event.get_or_insert_with(Event::default);
if let Some(user_info) = user_header {
merge_unreal_user_info(event, user_info);
}
if let Some(logs_item) = logs_item {
merge_unreal_logs(event, &logs_item.payload())?;
}
if let Some(context_item) = context_item {
let mut context = Unreal4Context::parse(&context_item.payload())?;
// the `unwrap_or_default` here can produce an invalid user report if the envelope id
// is indeed missing. This should not happen under normal circumstances since the EventId is
// created statically.
let event_id = envelope.event_id().unwrap_or_default();
debug_assert!(!event_id.is_nil());
if let Some(report) = get_unreal_user_report(event_id, &mut context) {
envelope.add_item(report);
}
merge_unreal_context(event, context);
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
fn get_context() -> Unreal4Context {
let raw_context = br##"<?xml version="1.0" encoding="UTF-8"?>
<FGenericCrashContext>
<RuntimeProperties>
<UserName>bruno</UserName>
<MemoryStats.TotalPhysical>6896832512</MemoryStats.TotalPhysical>
<Misc.OSVersionMajor>Windows 10</Misc.OSVersionMajor>
<Misc.OSVersionMinor />
<Misc.PrimaryGPUBrand>Parallels Display Adapter (WDDM)</Misc.PrimaryGPUBrand>
<ErrorMessage>Access violation - code c0000005 (first/second chance not available)</ErrorMessage>
<CrashVersion>3</CrashVersion>
<Misc.Is64bitOperatingSystem>1</Misc.Is64bitOperatingSystem>
<Misc.CPUVendor>GenuineIntel</Misc.CPUVendor>
<Misc.CPUBrand>Intel(R) Core(TM) i7-7920HQ CPU @ 3.10GHz</Misc.CPUBrand>
<GameStateName />
<MemoryStats.TotalVirtual>140737488224256</MemoryStats.TotalVirtual>
<PlatformFullName>Win64 [Windows 10 64b]</PlatformFullName>
<CrashReportClientVersion>1.0</CrashReportClientVersion>
<Modules>\\Mac\Home\Desktop\WindowsNoEditor\YetAnother\Binaries\Win64\YetAnother.exe
\\Mac\Home\Desktop\WindowsNoEditor\Engine\Binaries\ThirdParty\PhysX3\Win64\VS2015\PxFoundationPROFILE_x64.dll</Modules>
</RuntimeProperties>
<PlatformProperties>
<PlatformIsRunningWindows>1</PlatformIsRunningWindows>
<PlatformCallbackResult>0</PlatformCallbackResult>
</PlatformProperties>
</FGenericCrashContext>
"##;
Unreal4Context::parse(raw_context).unwrap()
}
#[test]
fn test_merge_unreal_context() {
let context = get_context();
let mut event = Event::default();
merge_unreal_context(&mut event, context);
insta::assert_snapshot!(Annotated::new(event).to_json_pretty().unwrap());
}
#[test]
fn test_merge_unreal_context_is_assert_level_error() {
let mut context = get_context();
let mut runtime_props = context.runtime_properties.as_mut().unwrap();
runtime_props.is_assert = Some(true);
let mut event = Event::default();
merge_unreal_context(&mut event, context);
assert_eq!(event.level, Annotated::new(Level::Error));
}
#[test]
fn test_merge_unreal_context_is_esure_level_warning() {
let mut context = get_context();
let mut runtime_props = context.runtime_properties.as_mut().unwrap();
runtime_props.is_ensure = Some(true);
let mut event = Event::default();
merge_unreal_context(&mut event, context);
assert_eq!(event.level, Annotated::new(Level::Warning));
}
#[test]
fn test_merge_unreal_logs() {
let logs = br##"Log file open, 10/29/18 17:56:37
[2018.10.29-16.56.38:332][ 0]LogGameplayTags: Display: UGameplayTagsManager::DoneAddingNativeTags. DelegateIsBound: 0
[2018.10.29-16.56.39:332][ 0]LogStats: UGameplayTagsManager::ConstructGameplayTagTree: ImportINI prefixes - 0.000 s
[2018.10.29-16.56.40:332][ 0]LogStats: UGameplayTagsManager::ConstructGameplayTagTree: Construct from data asset - 0.000 s
[2018.10.29-16.56.41:332][ 0]LogStats: UGameplayTagsManager::ConstructGameplayTagTree: ImportINI - 0.000 s"##;
let mut event = Event::default();
merge_unreal_logs(&mut event, logs).ok();
insta::assert_snapshot!(Annotated::new(event).to_json_pretty().unwrap());
}
#[test]
fn test_merge_unreal_context_event() {
let bytes = include_bytes!("../../../tests/integration/fixtures/native/unreal_crash");
let user_id = "ebff51ef3c4878627823eebd9ff40eb4|2e7d369327054a448be6c8d3601213cb|C52DC39D-DAF3-5E36-A8D3-BF5F53A5D38F";
let crash = Unreal4Crash::parse(bytes).unwrap();
let mut event = Event::default();
merge_unreal_user_info(&mut event, user_id);
merge_unreal_context(&mut event, crash.context().unwrap().unwrap());
insta::assert_snapshot!(Annotated::new(event).to_json_pretty().unwrap());
}
}