-
-
Notifications
You must be signed in to change notification settings - Fork 222
/
dif_upload.rs
2156 lines (1899 loc) · 70.7 KB
/
dif_upload.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Searches, processes and uploads debug information files (DIFs). See
//! `DifUpload` for more information.
use std::collections::{BTreeMap, BTreeSet};
use std::convert::TryInto;
use std::ffi::{OsStr, OsString};
use std::fmt::{self, Display};
use std::fs::{self, File};
use std::io::{BufReader, BufWriter, Read, Seek, Write};
use std::iter::IntoIterator;
use std::mem::transmute;
use std::ops::Deref;
use std::path::{Component, Path, PathBuf};
use std::process::Command;
use std::slice::{Chunks, Iter};
use std::str;
use std::thread;
use std::time::{Duration, Instant};
use anyhow::{bail, format_err, Error, Result};
use console::style;
use indicatif::HumanBytes;
use log::{debug, info, warn};
use sha1_smol::Digest;
use symbolic::common::{AsSelf, ByteView, DebugId, SelfCell, Uuid};
use symbolic::debuginfo::macho::{BcSymbolMap, UuidMapping};
use symbolic::debuginfo::pe::PeObject;
use symbolic::debuginfo::sourcebundle::{SourceBundleWriter, SourceFileDescriptor};
use symbolic::debuginfo::{Archive, FileEntry, FileFormat, Object};
use symbolic::il2cpp::ObjectLineMapping;
use walkdir::WalkDir;
use which::which;
use zip::result::ZipError;
use zip::{write::FileOptions, ZipArchive, ZipWriter};
use crate::api::{
Api, ChunkUploadCapability, ChunkUploadOptions, ChunkedDifRequest, ChunkedFileState,
};
use crate::config::Config;
use crate::constants::{DEFAULT_MAX_DIF_SIZE, DEFAULT_MAX_WAIT};
use crate::utils::chunks::{
upload_chunks, BatchedSliceExt, Chunk, ItemSize, ASSEMBLE_POLL_INTERVAL,
};
use crate::utils::dif::ObjectDifFeatures;
use crate::utils::fs::{get_sha1_checksum, get_sha1_checksums, TempDir, TempFile};
use crate::utils::progress::{ProgressBar, ProgressStyle};
use crate::utils::ui::{copy_with_progress, make_byte_progress_bar};
/// A debug info file on the server.
pub use crate::api::DebugInfoFile;
/// Fallback maximum number of chunks in a batch for the legacy upload.
static MAX_CHUNKS: u64 = 64;
/// An iterator over chunks of data in a `ChunkedDifMatch` object.
///
/// This struct is returned by `ChunkedDifMatch::chunks`.
struct DifChunks<'a> {
checksums: Iter<'a, Digest>,
iter: Chunks<'a, u8>,
}
impl<'a> Iterator for DifChunks<'a> {
type Item = Chunk<'a>;
fn next(&mut self) -> Option<Self::Item> {
match (self.checksums.next(), self.iter.next()) {
(Some(checksum), Some(data)) => Some(Chunk((*checksum, data))),
(_, _) => None,
}
}
}
/// A Debug Information File.
///
/// This is primarily used to store inside the [`DifMatch`] so does not contain any
/// information already present there. You probably should look whether you can use
/// [`DifMatch`] instead of this instead.
enum ParsedDif<'a> {
Object(Box<Object<'a>>),
BcSymbolMap(BcSymbolMap<'a>),
UuidMap(UuidMapping),
Il2Cpp,
}
impl<'slf, 'data: 'slf> AsSelf<'slf> for ParsedDif<'data> {
type Ref = ParsedDif<'data>;
fn as_self(&'slf self) -> &Self::Ref {
self
}
}
/// Contains backing data for a `DifMatch`.
///
/// This can be used to store the actual data that a `FatObject` might be
/// relying upon, such as temporary files or extracted archives. It will be
/// disposed along with a `DifMatch` once it is dropped.
#[derive(Debug)]
enum DifBacking {
Temp(TempFile),
}
/// A handle to a debug information file found by `DifUpload`.
///
/// It contains a `FatObject` giving access to the metadata and contents of the
/// debug information file. `DifMatch::attachments` may contain supplemental
/// files used to further process this file, such as dSYM PLists.
struct DifMatch<'data> {
_backing: Option<DifBacking>,
dif: SelfCell<ByteView<'data>, ParsedDif<'data>>,
name: String,
debug_id: Option<DebugId>,
attachments: Option<BTreeMap<String, ByteView<'static>>>,
}
impl<'data> DifMatch<'data> {
fn from_temp_object<S>(temp_file: TempFile, name: S, debug_id: Option<DebugId>) -> Result<Self>
where
S: Into<String>,
{
let buffer = ByteView::open(temp_file.path()).map_err(Error::new)?;
let dif = SelfCell::try_new(buffer, |b| {
Object::parse(unsafe { &*b }).map(|object| ParsedDif::Object(Box::new(object)))
})?;
Ok(DifMatch {
_backing: Some(DifBacking::Temp(temp_file)),
dif,
name: name.into(),
debug_id,
attachments: None,
})
}
fn from_temp_line_mapping<S>(
temp_file: TempFile,
name: S,
debug_id: Option<DebugId>,
) -> Result<Self>
where
S: Into<String>,
{
let buffer = ByteView::open(temp_file.path()).map_err(Error::new)?;
let dif = SelfCell::try_new(buffer, |_| Ok::<_, anyhow::Error>(ParsedDif::Il2Cpp))?;
Ok(DifMatch {
_backing: Some(DifBacking::Temp(temp_file)),
dif,
name: name.into(),
debug_id,
attachments: None,
})
}
/// Creates a [`DifMatch`] from a `.bcsymbolmap` file.
///
/// The `uuid` is the DebugID of the symbolmap while `name` is the filename of the file.
/// Normally the filename should be the `uuid` with `.bcsymbolmap` appended to it.
fn from_bcsymbolmap(uuid: DebugId, name: String, data: ByteView<'static>) -> Result<Self> {
let dif = SelfCell::try_new(data, |buf| {
BcSymbolMap::parse(unsafe { &*buf }).map(ParsedDif::BcSymbolMap)
})?;
Ok(Self {
_backing: None,
dif,
name,
debug_id: Some(uuid),
attachments: None,
})
}
fn from_plist(uuid: DebugId, name: String, data: ByteView<'static>) -> Result<Self> {
let dif = SelfCell::try_new(data, |buf| {
UuidMapping::parse_plist(uuid, unsafe { &*buf }).map(ParsedDif::UuidMap)
})?;
Ok(Self {
_backing: None,
dif,
name,
debug_id: Some(uuid),
attachments: None,
})
}
/// Moves the specified temporary debug file to a safe location and assumes
/// ownership. The file will be deleted in the file system when this
/// `DifMatch` is dropped.
///
/// The path must point to a `FatObject` containing exactly one `Object`.
fn take_temp<P, S>(path: P, name: S) -> Result<Self>
where
P: AsRef<Path>,
S: Into<String>,
{
// Even though we could supply the debug_id here from the object we do not, the
// server will do the same anyway and we actually have control over the version of
// the code running there so can fix bugs more reliably.
let temp_file = TempFile::take(path)?;
Self::from_temp_object(temp_file, name, None)
}
/// Returns the parsed [`Object`] of this DIF.
pub fn object(&self) -> Option<&Object<'data>> {
match self.dif.get() {
ParsedDif::Object(ref obj) => Some(obj),
ParsedDif::BcSymbolMap(_) => None,
ParsedDif::UuidMap(_) => None,
ParsedDif::Il2Cpp => None,
}
}
pub fn format(&self) -> DifFormat {
match self.dif.get() {
ParsedDif::Object(ref object) => DifFormat::Object(object.file_format()),
ParsedDif::BcSymbolMap(_) => DifFormat::BcSymbolMap,
ParsedDif::UuidMap(_) => DifFormat::PList,
ParsedDif::Il2Cpp => DifFormat::Il2Cpp,
}
}
/// Returns the raw binary data of this DIF.
pub fn data(&self) -> &[u8] {
match self.dif.get() {
ParsedDif::Object(ref obj) => obj.data(),
ParsedDif::BcSymbolMap(_) => self.dif.owner(),
ParsedDif::UuidMap(_) => self.dif.owner(),
ParsedDif::Il2Cpp => self.dif.owner(),
}
}
/// Returns the size of of this DIF in bytes.
pub fn size(&self) -> u64 {
self.data().len() as u64
}
/// Returns the path of this DIF relative to the search origin.
pub fn path(&self) -> &str {
&self.name
}
/// Returns the name of this DIF, including its file extension.
pub fn file_name(&self) -> &str {
Path::new(self.path())
.file_name()
.and_then(OsStr::to_str)
.unwrap_or("Generic")
}
/// Returns attachments of this DIF, if any.
pub fn attachments(&self) -> Option<&BTreeMap<String, ByteView<'static>>> {
self.attachments.as_ref()
}
/// Determines whether this file needs resolution of hidden symbols.
pub fn needs_symbol_map(&self) -> bool {
// XCode release archives and dSYM bundles downloaded from iTunes
// Connect contain Swift library symbols. These have caused various
// issues in the past, so we ignore them for now. In particular, there
// are never any BCSymbolMaps generated for them and the DBGOriginalUUID
// in the plist is the UUID of the original dsym file.
//
// We *might* have to locate the original library in the Xcode
// distribution, then build a new non-fat dSYM file from it and patch
// the the UUID.
if self.file_name().starts_with("libswift") {
return false;
}
match self.object() {
Some(Object::MachO(ref macho)) => macho.requires_symbolmap(),
_ => false,
}
}
}
impl<'data> fmt::Debug for DifMatch<'data> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DifMatch")
.field("name", &self.name)
.field("format", &self.format())
.field("debug_id", &self.debug_id)
.field("object", &self.object())
.finish()
}
}
/// A `DifMatch` with computed SHA1 checksum.
#[derive(Debug)]
struct HashedDifMatch<'data> {
inner: DifMatch<'data>,
checksum: Digest,
}
impl<'data> HashedDifMatch<'data> {
/// Calculates the SHA1 checksum for the given DIF.
fn from(inner: DifMatch<'data>) -> Result<Self> {
let checksum = get_sha1_checksum(inner.data())?;
Ok(HashedDifMatch { inner, checksum })
}
/// Returns the SHA1 checksum of this DIF.
fn checksum(&self) -> Digest {
self.checksum
}
}
impl<'data> Deref for HashedDifMatch<'data> {
type Target = DifMatch<'data>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<'data> ItemSize for HashedDifMatch<'data> {
fn size(&self) -> u64 {
self.deref().size()
}
}
/// A chunked `DifMatch` with computed SHA1 checksums.
#[derive(Debug)]
struct ChunkedDifMatch<'data> {
inner: HashedDifMatch<'data>,
chunks: Vec<Digest>,
chunk_size: u64,
}
impl<'data> ChunkedDifMatch<'data> {
/// Slices the DIF into chunks of `chunk_size` bytes each, and computes SHA1
/// checksums for every chunk as well as the entire DIF.
pub fn from(inner: DifMatch<'data>, chunk_size: u64) -> Result<Self> {
let (checksum, chunks) = get_sha1_checksums(inner.data(), chunk_size)?;
Ok(ChunkedDifMatch {
inner: HashedDifMatch { inner, checksum },
chunks,
chunk_size,
})
}
/// Returns an iterator over all chunk checksums.
pub fn checksums(&self) -> Iter<'_, Digest> {
self.chunks.iter()
}
/// Returns an iterator over all `DifChunk`s.
pub fn chunks(&self) -> DifChunks<'_> {
DifChunks {
checksums: self.checksums(),
iter: self.data().chunks(self.chunk_size as usize),
}
}
/// Creates a tuple which can be collected into a `ChunkedDifRequest`.
// Some(...) for debug_id can only be done if the ChunkedUploadCapability::Pdbs is
// present, which is kind of a protocol bug. Not supplying it means more recent
// sentry-cli versions keep working with ancient versions of sentry by not
// triggering this protocol bug in most common situations.
// See: https://github.com/getsentry/sentry-cli/issues/980
// See: https://github.com/getsentry/sentry-cli/issues/1056
pub fn to_assemble(&self, with_debug_id: bool) -> (Digest, ChunkedDifRequest<'_>) {
(
self.checksum(),
ChunkedDifRequest {
name: self.file_name(),
debug_id: if with_debug_id { self.debug_id } else { None },
chunks: &self.chunks,
},
)
}
}
impl<'data> Deref for ChunkedDifMatch<'data> {
type Target = HashedDifMatch<'data>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<'data> ItemSize for ChunkedDifMatch<'data> {
fn size(&self) -> u64 {
self.deref().size()
}
}
type ZipFileArchive = ZipArchive<BufReader<File>>;
/// A handle to the source of a potential `DifMatch` used inside `search_difs`.
///
/// The primary use of this handle is to resolve files relative to the debug
/// information file and store them in `DifMatch::attachments`. These could be
/// companion files or metadata files needed to process the DIFs in sentry-cli,
/// or later even on Sentry.
#[derive(Debug)]
enum DifSource<'a> {
/// A file located in the file system
FileSystem(&'a Path),
/// An entry in a ZIP file
Zip(&'a mut ZipFileArchive, &'a str),
}
impl<'a> DifSource<'a> {
/// Resolves a file relative to the directory of `base`, stripping of the
/// file name.
fn get_relative_fs(base: &Path, path: &Path) -> Option<ByteView<'static>> {
// Use parent() to get to the directory and then move relative from
// there. ByteView will internally cannonicalize the path and resolve
// symlinks.
base.parent()
.and_then(|p| ByteView::open(p.join(path)).ok())
}
/// Extracts a file relative to the directory of `name`, stripping of the
/// file name.
fn get_relative_zip(
zip: &mut ZipFileArchive,
name: &str,
path: &Path,
) -> Option<ByteView<'static>> {
// There is no built-in utility that normalizes paths without access to
// the file system. We start by removing the file name from the given
// path and then start to manually resolve the path components to a
// final path.
let mut zip_path = PathBuf::from(name);
zip_path.pop();
for component in path.components() {
match component {
Component::ParentDir => {
zip_path.pop();
}
Component::Normal(p) => {
zip_path.push(p);
}
_ => {
// `Component::CurDir` leaves the path as-is, and the
// remaining `Component::RootDir` and `Component::Prefix` do
// not make sense in ZIP files.
}
}
}
zip_path
.to_str()
.and_then(|name| zip.by_name(name).ok())
.and_then(|f| ByteView::read(f).ok())
}
/// Resolves a file relative to this source and reads it into a `ByteView`.
///
/// The target is always resolved relative to the directory of the source,
/// excluding its file name. The path "../changed" relative to a source
/// pointing to "path/to/file" will resolve in "path/changed".
///
/// The returned ByteView will allow random-access to the data until it is
/// disposed. If the source points to a ZIP file, the target is fully read
/// into a memory buffer. See `ByteView::from_reader` for more information.
pub fn get_relative<P>(&mut self, path: P) -> Option<ByteView<'static>>
where
P: AsRef<Path>,
{
match *self {
DifSource::FileSystem(base) => Self::get_relative_fs(base, path.as_ref()),
DifSource::Zip(ref mut zip, name) => Self::get_relative_zip(zip, name, path.as_ref()),
}
}
}
/// Information returned by `assemble_difs` containing flat lists of incomplete
/// DIFs and their missing chunks.
type MissingDifsInfo<'data, 'm> = (Vec<&'m ChunkedDifMatch<'data>>, Vec<Chunk<'m>>);
/// Verifies that the given path contains a ZIP file and opens it.
fn try_open_zip<P>(path: P) -> Result<Option<ZipFileArchive>>
where
P: AsRef<Path>,
{
if path.as_ref().extension() != Some("zip".as_ref()) {
return Ok(None);
}
let mut magic: [u8; 2] = [0; 2];
let mut file = File::open(path)?;
if file.read_exact(&mut magic).is_err() {
// Catch empty or single-character files
return Ok(None);
}
file.rewind()?;
Ok(match &magic {
b"PK" => Some(ZipArchive::new(BufReader::new(file))?),
_ => None,
})
}
/// Searches the given ZIP for potential DIFs and passes them to the callback.
///
/// To avoid unnecessary file operations, the file extension is already checked
/// for every entry before opening it.
///
/// This function will not recurse into ZIPs contained in this ZIP.
fn walk_difs_zip<F>(mut zip: ZipFileArchive, options: &DifUpload, mut func: F) -> Result<()>
where
F: FnMut(DifSource<'_>, String, ByteView<'static>) -> Result<()>,
{
for index in 0..zip.len() {
let (name, buffer) = {
let zip_file = zip.by_index(index)?;
let name = zip_file.name().to_string();
if !options.valid_extension(Path::new(&name).extension()) {
continue;
}
(name, ByteView::read(zip_file).map_err(Error::new)?)
};
func(DifSource::Zip(&mut zip, &name), name.clone(), buffer)?;
}
Ok(())
}
/// Recursively searches the given location for potential DIFs and passes them
/// to the callback.
///
/// If `DifUpload::allow_zips` is set, then this function will attempt to open
/// the ZIP and search it for DIFs as well, however not recursing further into
/// nested ZIPs.
///
/// To avoid unnecessary file operations, the file extension is already checked
/// for every entry before opening it.
fn walk_difs_directory<F, P>(location: P, options: &DifUpload, mut func: F) -> Result<()>
where
P: AsRef<Path>,
F: FnMut(DifSource<'_>, String, ByteView<'static>) -> Result<()>,
{
let location = location.as_ref();
let directory = if location.is_dir() {
location
} else {
location.parent().unwrap_or_else(|| Path::new(""))
};
debug!("searching location {}", location.display());
for entry in WalkDir::new(location)
.follow_links(true)
.into_iter()
.filter_map(Result::ok)
{
if !entry.metadata()?.is_file() {
// Walkdir recurses automatically into folders
continue;
}
let path = entry.path();
if options.zips_allowed {
match try_open_zip(path) {
Ok(Some(zip)) => {
debug!("searching zip archive {}", path.display());
if let Err(err) = walk_difs_zip(zip, options, &mut func) {
if let Some(e) = err.downcast_ref::<ZipError>() {
debug!("skipping zip archive {}", path.display());
debug!("error: {}", e);
continue;
};
return Err(err);
}
debug!("finished zip archive {}", path.display());
continue;
}
Err(e) => {
debug!("skipping zip archive {}", path.display());
debug!("error: {}", e);
continue;
}
Ok(None) => {
// this is not a zip archive
}
}
}
if !options.valid_extension(path.extension()) {
continue;
}
let buffer = ByteView::open(path).map_err(Error::new)?;
let name = path
.strip_prefix(directory)
.unwrap()
.to_string_lossy()
.into_owned();
func(DifSource::FileSystem(path), name, buffer)?;
}
debug!("finished location {}", directory.display());
Ok(())
}
/// Searches for mapping PLists next to the given `source`. It returns a mapping
/// of Plist name to owning buffer of the file's contents. This function should
/// only be called for dSYMs.
fn find_uuid_plists(
object: &Object<'_>,
source: &mut DifSource<'_>,
) -> Option<BTreeMap<String, ByteView<'static>>> {
let uuid = object.debug_id().uuid();
if uuid.is_nil() {
return None;
}
// When uploading an XCode build archive to iTunes Connect, Apple will
// re-build the app for different architectures, causing new UUIDs in the
// final bundle. To allow mapping back to the original symbols, it adds
// PList files in the `Resources` folder (one level above the binary) that
// contains the original UUID, one for each object contained in the fat
// object.
//
// The folder structure looks like this:
//
// App.dSYM
// ├─ Info.plist
// └─ Resources
// ├─ 1B205CD0-67D0-4D69-A0FA-C6BDDDB2A609.plist
// ├─ 1C228684-3EE5-472B-AB8D-29B3FBF63A70.plist
// └─ DWARF
// └─ App
let plist_name = format!("{:X}.plist", uuid.as_hyphenated());
let plist = match source.get_relative(format!("../{}", &plist_name)) {
Some(plist) => plist,
None => return None,
};
let mut plists = BTreeMap::new();
plists.insert(plist_name, plist);
Some(plists)
}
/// Patch debug identifiers for PDBs where the corresponding PE specifies a different age.
fn fix_pdb_ages(difs: &mut [DifMatch<'_>], age_overrides: &BTreeMap<Uuid, u32>) {
for dif in difs {
if let Some(object) = dif.object() {
if object.file_format() != FileFormat::Pdb {
continue;
}
let debug_id = object.debug_id();
let age = match age_overrides.get(&debug_id.uuid()) {
Some(age) => *age,
None => continue,
};
if age == debug_id.appendix() {
continue;
}
debug!(
"overriding age for {} ({} -> {})",
dif.name,
debug_id.appendix(),
age
);
dif.debug_id = Some(DebugId::from_parts(debug_id.uuid(), age));
}
}
}
/// Searches matching debug information files.
fn search_difs(options: &DifUpload) -> Result<Vec<DifMatch<'static>>> {
let progress_style = ProgressStyle::default_spinner().template(
"{spinner} Searching for debug symbol files...\
\n found {prefix:.yellow} {msg:.dim}",
);
let pb = ProgressBar::new_spinner();
pb.enable_steady_tick(100);
pb.set_style(progress_style);
let mut age_overrides = BTreeMap::new();
let mut collected = Vec::new();
for base_path in &options.paths {
if base_path == Path::new("") {
warn!(
"Skipping uploading from an empty path (\"\"). \
Maybe you expanded an empty shell variable?"
);
continue;
}
walk_difs_directory(base_path, options, |source, name, buffer| {
debug!("trying to process {}", name);
pb.set_message(&name);
if Archive::peek(&buffer) != FileFormat::Unknown {
let mut difs =
collect_object_dif(source, name, buffer, options, &mut age_overrides);
collected.append(difs.as_mut());
} else if BcSymbolMap::test(&buffer) {
if let Some(dif) = collect_auxdif(name, buffer, options, AuxDifKind::BcSymbolMap) {
collected.push(dif);
}
} else if buffer.starts_with(b"<?xml") {
if let Some(dif) = collect_auxdif(name, buffer, options, AuxDifKind::UuidMap) {
collected.push(dif);
}
};
pb.set_prefix(&collected.len().to_string());
Ok(())
})?;
}
if !age_overrides.is_empty() {
fix_pdb_ages(&mut collected, &age_overrides);
}
pb.finish_and_clear();
print!(
"{} Found {} debug information {}",
style(">").dim(),
style(collected.len()).yellow(),
match collected.len() {
1 => "file",
_ => "files",
}
);
let count_with_sources = collected
.iter()
.filter(|dif| match dif.object() {
Some(object) => object.has_sources(),
None => false,
})
.count();
match count_with_sources {
0 => println!(),
_ => println!(" ({count_with_sources} with embedded sources)"),
}
Ok(collected)
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
enum AuxDifKind {
BcSymbolMap,
UuidMap,
}
impl Display for AuxDifKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
AuxDifKind::BcSymbolMap => write!(f, "BCSymbolMap"),
AuxDifKind::UuidMap => write!(f, "UuidMap"),
}
}
}
/// Collects a possible BCSymbolmap or PList into a [`DifMatch`].
///
/// The `name` is the relative path of the file processed, while `buffer` contains the
/// actual data.
fn collect_auxdif<'a>(
name: String,
buffer: ByteView<'static>,
options: &DifUpload,
kind: AuxDifKind,
) -> Option<DifMatch<'a>> {
let file_stem = Path::new(&name)
.file_stem()
.map(|stem| stem.to_string_lossy())
.unwrap_or_default();
let uuid: DebugId = match file_stem.parse() {
Ok(uuid) => uuid,
Err(_) => {
if kind == AuxDifKind::BcSymbolMap {
// There are loads of plists in a normal XCode Archive that are not valid
// UUID mappings. Warning for all these is pointless.
warn!(
"Skipping {kind} with invalid filename: {name}",
kind = kind,
name = name
);
}
return None;
}
};
let dif_result = match kind {
AuxDifKind::BcSymbolMap => DifMatch::from_bcsymbolmap(uuid, name.clone(), buffer),
AuxDifKind::UuidMap => DifMatch::from_plist(uuid, name.clone(), buffer),
};
let dif = match dif_result {
Ok(dif) => dif,
Err(err) => {
warn!(
"Skipping invalid {kind} file {name}: {err}",
kind = kind,
name = name,
err = err
);
return None;
}
};
// Skip this file if we don't want to process it.
if !options.validate_dif(&dif) {
return None;
}
Some(dif)
}
/// Processes and [`DifSource`] which is expected to be an object file.
fn collect_object_dif<'a>(
mut source: DifSource<'_>,
name: String,
buffer: ByteView<'static>,
options: &DifUpload,
age_overrides: &mut BTreeMap<Uuid, u32>,
) -> Vec<DifMatch<'a>> {
let mut collected = Vec::with_capacity(2);
// Try to parse a potential object file. If this is not possible,
// then we're not dealing with an object file, thus silently
// skipping it.
let format = Archive::peek(&buffer);
// Override this behavior for PE files. Their debug identifier is
// needed in case PDBs should be uploaded to fix an eventual age
// mismatch
let should_override_age =
format == FileFormat::Pe && options.valid_format(DifFormat::Object(FileFormat::Pdb));
if !should_override_age && !options.valid_format(DifFormat::Object(format)) {
return collected;
}
debug!("trying to parse dif {}", name);
let archive = match Archive::parse(&buffer) {
Ok(archive) => archive,
Err(e) => {
warn!("Skipping invalid debug file {}: {}", name, e);
return collected;
}
};
// Each `FatObject` might contain multiple matching objects, each of
// which needs to retain a reference to the original fat file. We
// create a shared instance here and clone it into `DifMatch`es
// below.
for object in archive.objects() {
// Silently skip all objects that we cannot process. This can
// happen due to invalid object files, which we then just
// discard rather than stopping the scan.
let object = match object {
Ok(object) => object,
Err(_) => continue,
};
// Objects without debug id will be skipped altogether. While frames
// during symbolication might be lacking debug identifiers,
// Sentry requires object files to have one during upload.
let id = object.debug_id();
if id.is_nil() {
continue;
}
// If this is a PE file with an embedded Portable PDB, we extract and process the PPDB separately.
if let Object::Pe(pe) = &object {
if let Ok(Some(ppdb_dif)) = extract_embedded_ppdb(pe, name.as_str()) {
if options.validate_dif(&ppdb_dif) {
collected.push(ppdb_dif);
}
}
};
// Store a mapping of "age" values for all encountered PE files,
// regardless of whether they will be uploaded. This is used later
// to fix up PDB files.
if should_override_age {
age_overrides.insert(id.uuid(), id.appendix());
}
// Invoke logic to retrieve attachments specific to the kind
// of object file. These are used for processing. Since only
// dSYMs equire processing currently, all other kinds are
// skipped.
let attachments = match object.file_format() {
FileFormat::MachO => find_uuid_plists(&object, &mut source),
_ => None,
};
// We retain the buffer and the borrowed object in a new SelfCell. This is
// incredibly unsafe, but in our case it is fine, since the SelfCell owns the same
// buffer that was used to retrieve the object.
let cell = unsafe {
SelfCell::from_raw(
buffer.clone(),
ParsedDif::Object(Box::new(transmute(object))),
)
};
let dif = DifMatch {
_backing: None,
dif: cell,
name: name.clone(),
debug_id: Some(id),
attachments,
};
// Skip this file if we don't want to process it.
if !options.validate_dif(&dif) {
continue;
}
collected.push(dif);
}
collected
}
/// Resolves BCSymbolMaps and replaces hidden symbols in a `DifMatch` using
/// `dsymutil`. If successful, this will return a new `DifMatch` based on a
/// temporary file. The original dSYM is not touched.
///
/// Note that this process copies the file to a temporary location and might
/// incur significant I/O for larger debug files.
fn resolve_hidden_symbols<'a>(dif: DifMatch<'a>, symbol_map: &Path) -> Result<DifMatch<'a>> {
if dif.attachments.is_none() {
println!(
"{} {}: Could not locate UUID mapping for {}",
style(">").dim(),
style("Warning").red(),
style(dif.file_name()).yellow(),
);
return Ok(dif);
}
// We need to rebuild the Resources folder of a dSYM structure in a temp
// directory that is guaranteed to be deleted after this operation. The
// Info.plist is not needed for this operation:
// Resources
// ├─ 1B205CD0-67D0-4D69-A0FA-C6BDDDB2A609.plist
// ├─ 1C228684-3EE5-472B-AB8D-29B3FBF63A70.plist
// └─ DWARF
// └─ ObjectFile
let temp_dir = TempDir::create()?;
fs::create_dir_all(temp_dir.path().join("DWARF"))?;
// Copy the object file binary
let temp_path = temp_dir.path().join("DWARF").join(dif.file_name());
let mut temp_file = File::create(&temp_path)?;
temp_file.write_all(dif.data())?;
temp_file.sync_data()?;
// Copy the UUID plists
for (name, view) in dif.attachments().unwrap() {
let mut plist = File::create(temp_dir.path().join(name))?;
plist.write_all(view)?;
plist.sync_data()?;
}
let output = Command::new("dsymutil")
.arg("-symbol-map")
.arg(symbol_map)
.arg(&temp_path)
.output()?;
if !output.status.success() {
if let Ok(error) = str::from_utf8(&output.stderr) {
bail!("Could not resolve BCSymbolMaps: {}", error);
} else {
bail!("Could not resolve BCSymbolMaps due to an unknown error");
}
}
// Take ownership of the modified (fat) object file and move it somewhere
// else so it is safe to delete the temp directory.
DifMatch::take_temp(temp_path, dif.path())
}
/// Runs all `DifMatch` objects through the provided callback and displays a
/// progress bar while doing so.
///
/// ```
/// prepare_difs(processed, |m| HashedDifMatch::from(m))?
/// ```
fn prepare_difs<'data, F, T>(items: Vec<DifMatch<'data>>, mut func: F) -> Result<Vec<T>>
where
F: FnMut(DifMatch<'data>) -> Result<T>,
{
let progress_style = ProgressStyle::default_bar().template(
"{prefix:.dim} Preparing for upload... {msg:.dim}\
\n{wide_bar} {pos}/{len}",
);