forked from Vector35/binaryninja-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.rs
More file actions
1312 lines (1200 loc) · 56.3 KB
/
Copy pathprocess.rs
File metadata and controls
1312 lines (1200 loc) · 56.3 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
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
//! Process different types of files into Binary Ninja type libraries.
use binaryninja::architecture::CoreArchitecture;
use dashmap::DashMap;
use std::collections::{HashMap, HashSet};
use std::env::temp_dir;
use std::ffi::OsStr;
use std::fs::File;
use std::path::{Path, PathBuf};
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::Relaxed;
use std::sync::Arc;
use std::time::{Duration, Instant};
use thiserror::Error;
use walkdir::WalkDir;
use crate::helper::visit_type_reference;
use crate::merge::merge_types;
use crate::schema::BntlSchema;
use crate::tbd::{parse_tbd_info, TbdArchitecture};
use crate::winmd::WindowsMetadataImporter;
use binaryninja::background_task::BackgroundTask;
use binaryninja::binary_view::{BinaryView, BinaryViewType};
use binaryninja::file_metadata::FileMetadata;
use binaryninja::metadata::Metadata;
use binaryninja::platform::Platform;
use binaryninja::project::file::ProjectFile;
use binaryninja::project::folder::ProjectFolder;
use binaryninja::project::Project;
use binaryninja::qualified_name::QualifiedName;
use binaryninja::rc::Ref;
use binaryninja::section::Section;
use binaryninja::types::{
CoreTypeParser, NamedTypeReference, Type, TypeClass, TypeLibrary, TypeParser, TypeParserError,
};
use nt_apiset::{ApiSetMap, NtApiSetError};
#[derive(Error, Debug)]
pub enum ProcessingError {
#[error("Binary view load error: {0}")]
BinaryViewLoad(PathBuf),
#[error("Failed to read binary view at offset {0:?} with length {1:?}")]
BinaryViewRead(u64, usize),
#[error("Failed to read .apiset section: {0}")]
FailedToReadApiSet(#[from] NtApiSetError),
#[error("Failed to read file: {0}")]
FileRead(std::io::Error),
#[error("Failed to retrieve path to project file: {0:?}")]
NoPathToProjectFile(Ref<ProjectFile>),
#[error("Processing state has been poisoned")]
StatePoisoned,
#[error("Processing has been cancelled")]
Cancelled,
#[error("Skipping file: {0}")]
SkippedFile(PathBuf),
#[error("Failed to find platform: {0}")]
PlatformNotFound(String),
#[error("Failed to parse types: {0:?}")]
TypeParsingFailed(Vec<TypeParserError>),
#[error("Failed to import winmd: {0}")]
WinMdFailedImport(crate::winmd::ImportError),
#[error("Failed to parse type library: {0}")]
InvalidTypeLibrary(PathBuf),
}
#[derive(Default, Debug)]
pub struct ProcessingState {
pub cancelled: AtomicBool,
pub files: DashMap<PathBuf, bool>,
}
impl ProcessingState {
pub fn is_cancelled(&self) -> bool {
self.cancelled.load(Relaxed)
}
pub fn cancel(&self) {
self.cancelled.store(true, Relaxed)
}
pub fn files_with_state(&self, state: bool) -> usize {
self.files.iter().filter(|f| *f.value() == state).count()
}
pub fn set_file_state(&self, path: PathBuf, state: bool) {
self.files.insert(path, state);
}
pub fn total_files(&self) -> usize {
self.files.len()
}
}
pub fn new_processing_state_background_thread(
task: Ref<BackgroundTask>,
state: Arc<ProcessingState>,
) {
std::thread::spawn(move || {
let start = Instant::now();
while !task.is_finished() {
std::thread::sleep(Duration::from_millis(100));
// Check if the user wants to cancel the processing.
if task.is_cancelled() {
state.cancel();
}
let total = state.total_files();
let processed = state.files_with_state(true);
let unprocessed = state.files_with_state(false);
let completion = (processed as f64 / total as f64) * 100.0;
let elapsed = start.elapsed().as_secs_f32();
let text = format!(
"Processing {} files... {{{}|{}}} ({:.2}%) [{:.2}s]",
total, unprocessed, processed, completion, elapsed
);
task.set_progress_text(&text);
}
});
}
/// The result of running [`TypeLibProcessor`].
#[derive(Debug, Clone)]
pub struct ProcessedData {
pub type_libraries: HashSet<Ref<TypeLibrary>>,
}
impl ProcessedData {
pub fn new(type_libraries: Vec<Ref<TypeLibrary>>) -> Self {
Self {
type_libraries: type_libraries.into_iter().collect(),
}
}
/// Finalizes the processed data, deduplicating types and pruning empty type libraries.
///
/// The `default_name` should be the library name for which you want deduplicated types to be
/// relocated to. This does not need to be a logical-shared library name like `mylib.dll` as it will
/// be only referenced by other loaded type libraries (it cannot contain named objects).
pub fn finalized(mut self, default_name: &str) -> Self {
self.deduplicate_types(default_name);
// TODO: Run remap.
self.prune()
}
/// Prune empty type libraries from the processed data.
///
/// This is useful if you intend to save the type libraries to disk in a finalized form.
pub fn prune(self) -> Self {
let is_empty =
|tl: &TypeLibrary| tl.named_types().is_empty() && tl.named_objects().is_empty();
let pruned_type_libraries = self
.type_libraries
.into_iter()
.filter(|tl| !is_empty(tl))
.collect::<Vec<_>>();
Self::new(pruned_type_libraries)
}
/// Merges multiple [`ProcessedData`] into one, deduplicating type libraries.
///
/// This is necessary to allow the [`TypeLibProcessor`] to operate on a wide range of formats whilst
/// also guaranteeing no collisions and valid external references. Without merging libraries with
/// identical dependency names would be separate, which is not a supported scenario when loading
/// type libraries into Binary Ninja.
pub fn merge(list: &[ProcessedData]) -> Self {
let mut type_libraries = Vec::new();
for data in list {
type_libraries.extend(data.type_libraries.iter().cloned());
}
// We merge type libraries with the same dependency name, as that is what needs to be unique
// when we go to load them into Binary Ninja.
let mut mapped_type_libraries: HashMap<(String, CoreArchitecture), Vec<Ref<TypeLibrary>>> =
HashMap::new();
for tl in type_libraries.iter() {
mapped_type_libraries
.entry((tl.dependency_name(), tl.arch()))
.or_default()
.push(tl.clone());
}
let mut merged_type_libraries = Vec::new();
for ((dependency_name, arch), type_libraries) in mapped_type_libraries {
// Skip the more expensive merging if there is only a single type library.
if type_libraries.len() == 1 {
merged_type_libraries.push(type_libraries[0].clone());
continue;
}
let merged_type_library = TypeLibrary::new(arch, &dependency_name);
merged_type_library.set_dependency_name(&dependency_name);
for tl in type_libraries {
// TODO: Cheap type overrides (if one type is set as void* and the other as Foo* we take Foo*)
for named_type in &tl.named_types() {
merged_type_library.add_named_type(named_type.name.clone(), &named_type.ty);
}
for named_object in &tl.named_objects() {
merged_type_library
.add_named_object(named_object.name.clone(), &named_object.ty);
}
for alt_name in &tl.alternate_names() {
merged_type_library.add_alternate_name(alt_name);
}
for platform_name in &tl.platform_names() {
if let Some(platform) = Platform::by_name(platform_name) {
merged_type_library.add_platform(&platform);
} else {
// TODO: Upgrade this to an error?
tracing::warn!(
"Unknown platform name when merging '{}': '{}'",
dependency_name,
platform_name
);
}
}
// TODO: Stealing the type sources is literally impossible there is no getter, incredible...
// TODO: Replace this with a getter to type sources :/
let tmp_file = temp_dir().join(format!("{}_{}.json", dependency_name, tl.guid()));
if tl.decompress_to_file(&tmp_file) {
let schema = BntlSchema::from_path(&tmp_file);
for type_source in schema.type_sources {
merged_type_library
.add_type_source(type_source.name.into(), &type_source.source);
}
}
// Merge type library metadata, which can contain ordinal mappings.
if let Some(metadata_kv) = tl.metadata().get_value_store() {
// TODO: Handle merging of inner key values.
for (key, value) in metadata_kv {
let _ = merged_type_library.metadata().insert(&key, &value);
}
}
}
merged_type_libraries.push(merged_type_library);
}
Self::new(merged_type_libraries)
}
/// Maps the default type library objects into their locatable type libraries, if available.
///
/// This process is necessary only when the source of the processed data could not determine,
/// like in the case of header files, where the type library dependency name (e.g. "sqlite3.dll")
/// cannot be determined in a vacuum.
///
/// In the absence of that dependency name, the processor also parses auxiliary information like
/// apples TBD (text-based dylib stubs) to find out where to relocate those objects. For more
/// information see [`TypeLibProcessor::process_tbd`]
pub fn remap(&mut self, default_type_library: &str) {
let Some(default_type_library) = self
.type_libraries
.iter()
.find(|tl| tl.name() == default_type_library)
else {
tracing::error!(
"Default type library '{}' not found in processed data",
default_type_library
);
return;
};
// Go through all named objects and search for that symbol in another type library, if
// we find a match relocate the object. To relocate, we delete the object from the default
// type library and conditionally swap the type (to whichever is not void), recording
// visible referenced types to later relocate as well.
let mut recorded_references: HashMap<QualifiedName, HashSet<Ref<TypeLibrary>>> =
HashMap::new();
for tl in &self.type_libraries {
for named_object in &tl.named_objects() {
if let Some(relocated_type) =
default_type_library.get_named_object(named_object.name.clone())
{
// Move the type over to the target type library.
if named_object.ty.type_class() == TypeClass::VoidTypeClass {
// TODO: This visit actually needs to be a
visit_type_reference(&relocated_type, &mut |ntr| {
let ntr_name = ntr.name();
// Copy over the referenced types source library so the target type library
// can use it to resolve the reference at load time.
if let Some(type_source) =
default_type_library.get_named_type_source(ntr_name.clone())
{
tl.add_type_source(ntr_name.clone(), &type_source)
}
// Record all referenced types that reside in the same type library, so
// we can relocate them as well, assuming no other type library also uses it.
let is_associated = default_type_library
.get_named_type(ntr_name.clone())
.is_some();
if is_associated {
recorded_references
.entry(ntr_name)
.or_default()
.insert(tl.clone());
}
});
tl.add_named_object(named_object.name.clone(), &relocated_type);
}
// TODO: Not technically necessary because the imports are keyed off dependency name.
// Remove from the default type library.
default_type_library.remove_named_object(named_object.name.clone());
}
}
}
// TODO: After we have gone through the named objects and moved their types over, we need to
// TODO: enumerate all types in the default type library and determine if they should be relocated
// TODO: to the new type library. Apart of this is also calling `add_type_source(ntr_name, default_type_lib)`
// TODO: for every type that is not relocated, as we now need to tell the target type library
// TODO: that the reference is external and exists in the default type library.
// TODO: Ugh, this needs to be a work list, we have to continue to drill down to relocate.
for (qualified_name, type_libraries) in recorded_references {
if type_libraries.len() == 1 {
// Only one type library uses this type, so we can safely relocate it.
let type_library = type_libraries.iter().next().unwrap();
let named_ty = default_type_library
.get_named_type(qualified_name.clone())
.unwrap();
type_library.add_named_type(qualified_name, &named_ty);
}
}
}
/// Locates named types which exist in multiple distinct type libraries and merges them into
/// a single type library (default type library).
///
/// Example: `Qt5Core.dll.bndb` and `Qt5Charts.dll.bndb` both had pdb info, and both have `QObject`.
/// Assuming `QObject` is mergeable, we will merge it into the default type library.
pub fn deduplicate_types(&mut self, default_type_library_name: &str) {
let mut default_libraries = HashMap::new();
let mut get_default_type_library = |arch: CoreArchitecture| {
default_libraries
.entry(arch)
.or_insert_with(move || TypeLibrary::new(arch, default_type_library_name))
.to_owned()
};
let mut mapped_named_types: HashMap<
(QualifiedName, CoreArchitecture),
Vec<Ref<TypeLibrary>>,
> = HashMap::new();
for tl in &self.type_libraries {
for named_type in &tl.named_types() {
mapped_named_types
.entry((named_type.name.clone(), tl.arch()))
.or_default()
.push(tl.clone());
}
}
for ((qualified_name, arch), type_libraries) in mapped_named_types {
if type_libraries.len() == 1 {
continue;
}
let default_type_library = get_default_type_library(arch);
let unmerged_types: Vec<_> = type_libraries
.iter()
.filter_map(|tl| tl.get_named_type(qualified_name.clone()))
.collect();
if let Some(merged_type) = merge_types(&unmerged_types) {
// Add the merged type to the default type library, then we need to point the type
// libraries to use this newly merged type instead of their type.
default_type_library.add_named_type(qualified_name.clone(), &merged_type);
for type_library in type_libraries {
// If the default type library does not have the platform, it will not be pulled in.
for platform_name in &type_library.platform_names() {
if let Some(platform) = Platform::by_name(platform_name) {
default_type_library.add_platform(&platform);
}
}
type_library.remove_named_type(qualified_name.clone());
type_library.add_type_source(qualified_name.clone(), default_type_library_name);
}
} else {
// TODO: Probably demote this to debug, since they might just be disparate types.
tracing::warn!(
"Unable to merge type for duplicated name: {}",
qualified_name
);
}
}
// Make sure all the default type libraries are within the processed data, if not already.
for (_, default_type_library) in default_libraries {
self.type_libraries.insert(default_type_library);
}
}
}
pub struct TypeLibProcessor {
state: Arc<ProcessingState>,
/// The Binary Ninja settings to use when analyzing the binaries.
analysis_settings: serde_json::Value,
/// The default name to use for the type library dependency name (e.g. "sqlite.dll").
///
/// When processing information that does not contain the dependency name, this will be used,
/// such as processing header files. We need to set a dependency name, otherwise the library
/// will not be able to be referenced by other libraries and/or the binary view.
///
/// This dependency name will NOT be used when it can otherwise be inferred by the processing
/// data, if you wish to override the resulting dependency name, you can do so by calling
/// [`TypeLibrary::set_dependency_name`] on the libraries returned via [`ProcessedData::type_libraries`].
default_dependency_name: String,
/// The default platform name to use when processing (e.g. "windows-x86_64").
///
/// When processing information that does not have an associated platform, this will be used,
/// such as processing header files or processing winmd files. When processing binary files,
/// the platform will be derived from the binary view default platform.
///
/// For WINMD files you typically want to run the processor for each of the following platforms:
///
/// - "windows-x86_64"
/// - "windows-x86"
/// - "windows-aarch64"
default_platform_name: String,
/// Set the include directories to use when processing header files. These will be passed to the
/// Clang type parser, which will use them to resolve header file includes.
include_directories: Vec<PathBuf>,
/// Whether to process existing type libraries when processing a binary file.
process_existing_type_libraries: bool,
/// The compiler flags to use when processing header files.
compiler_options: Vec<String>,
}
impl TypeLibProcessor {
pub fn new(default_dependency_name: &str, default_platform_name: &str) -> Self {
Self {
state: Arc::new(ProcessingState::default()),
analysis_settings: serde_json::json!({
"analysis.linearSweep.autorun": false,
"analysis.mode": "full",
}),
default_dependency_name: default_dependency_name.to_owned(),
default_platform_name: default_platform_name.to_owned(),
include_directories: Vec::new(),
process_existing_type_libraries: false,
compiler_options: Vec::new(),
}
}
/// Retrieve a thread-safe shared reference to the [`ProcessingState`].
pub fn state(&self) -> Arc<ProcessingState> {
self.state.clone()
}
pub fn with_include_directories(mut self, include_directories: Vec<PathBuf>) -> Self {
self.include_directories = include_directories;
self
}
pub fn with_compiler_options(mut self, options: Vec<String>) -> Self {
self.compiler_options = options;
self
}
/// Whether to process existing type libraries when processing a binary file.
///
/// If you open `mymodule.dll` and it imports functions from `kernel32.dll`, any import found
/// within the associated `kernel32.dll.bntl` will not be processed if this is `true`.
pub fn process_existing_type_libraries(
mut self,
process_existing_type_libraries: bool,
) -> Self {
self.process_existing_type_libraries = process_existing_type_libraries;
self
}
/// Place a call to this in places to interrupt when canceled.
fn check_cancelled(&self) -> Result<(), ProcessingError> {
match self.state.is_cancelled() {
true => Err(ProcessingError::Cancelled),
false => Ok(()),
}
}
pub fn process(&self, path: &Path) -> Result<ProcessedData, ProcessingError> {
match path.extension() {
Some(ext) if ext == "bntl" => self.process_type_library(path),
Some(ext) if ext == "h" || ext == "hpp" => self.process_source(path),
// NOTE: A typical processor will not go down this path where we only provide a single
// winmd file to be processed. You almost always want to process multiple winmd files,
// which can be done by passing a directory with the relevant winmd files.
Some(ext) if ext == "winmd" => self.process_winmd(&[path.to_owned()]),
Some(ext) if ext == "tbd" => self.process_tbd(path),
_ if path.is_dir() => self.process_directory(path),
_ => self.process_file(path),
}
}
pub fn process_directory(&self, path: &Path) -> Result<ProcessedData, ProcessingError> {
// Collect all files in the directory
let files = WalkDir::new(path)
.into_iter()
.filter_map(|e| {
let path = e.ok()?.into_path();
if path.is_file() {
Some(path)
} else {
None
}
})
.collect::<Vec<_>>();
// TODO: Parallel processing of files?
let unmerged_data: Result<Vec<_>, _> = files
.iter()
.map(|file| {
self.check_cancelled()?;
self.process(file)
})
.filter_map(|res| match res {
Ok(result) => Some(Ok(result)),
Err(ProcessingError::SkippedFile(path)) => {
tracing::debug!("Skipping directory file: {:?}", path);
None
}
Err(ProcessingError::Cancelled) => Some(Err(ProcessingError::Cancelled)),
Err(e) => {
tracing::error!("Directory file processing error: {:?}", e);
None
}
})
.collect();
Ok(ProcessedData::merge(&unmerged_data?))
}
pub fn process_project(&self, project: &Project) -> Result<ProcessedData, ProcessingError> {
// Inform the state of the new unprocessed project files.
for project_file in &project.files() {
// NOTE: We use the on disk path here because the downstream file state uses that.
if let Some(path) = project_file.path_on_disk() {
self.state.set_file_state(path, false);
}
}
let data: Result<Vec<_>, _> = project
.files()
.iter()
.map(|file| {
self.check_cancelled()?;
self.process_project_file(&file)
})
.filter_map(|res| match res {
Ok(result) => Some(Ok(result)),
Err(ProcessingError::SkippedFile(path)) => {
tracing::debug!("Skipping project file: {:?}", path);
None
}
Err(ProcessingError::Cancelled) => Some(Err(ProcessingError::Cancelled)),
Err(ProcessingError::NoPathToProjectFile(path)) => {
tracing::warn!("Project file not downloaded: {:?}", path);
None
}
Err(e) => {
tracing::error!("Project file processing error: {:?}", e);
None
}
})
.collect();
Ok(ProcessedData::merge(&data?))
}
pub fn process_project_folder(
&self,
project_folder: &ProjectFolder,
) -> Result<ProcessedData, ProcessingError> {
for project_file in &project_folder.files() {
// NOTE: We use the on disk path here because the downstream file state uses that.
if let Some(path) = project_file.path_on_disk() {
self.state.set_file_state(path, false);
}
}
let unmerged_data: Result<Vec<_>, _> = project_folder
.files()
.iter()
.map(|file| {
self.check_cancelled()?;
self.process_project_file(&file)
})
.filter_map(|res| match res {
Ok(result) => Some(Ok(result)),
Err(ProcessingError::SkippedFile(path)) => {
tracing::debug!("Skipping project file: {:?}", path);
None
}
Err(ProcessingError::Cancelled) => Some(Err(ProcessingError::Cancelled)),
Err(ProcessingError::NoPathToProjectFile(path)) => {
tracing::warn!("Project file not downloaded: {:?}", path);
None
}
Err(e) => {
tracing::error!("Project file processing error: {:?}", e);
None
}
})
.collect();
Ok(ProcessedData::merge(&unmerged_data?))
}
pub fn process_project_file(
&self,
project_file: &ProjectFile,
) -> Result<ProcessedData, ProcessingError> {
let file_name = project_file.name();
let extension = file_name.split('.').next_back();
let path = project_file
.path_on_disk()
.ok_or_else(|| ProcessingError::NoPathToProjectFile(project_file.to_owned()))?;
match extension {
Some("bntl") => self.process_type_library(&path),
Some(ext) if ext == "h" || ext == "hpp" => self.process_source(&path),
// NOTE: A typical processor will not go down this path where we only provide a single
// winmd file to be processed. You almost always want to process multiple winmd files,
// which can be done by passing a directory with the relevant winmd files.
Some("winmd") => self.process_winmd(&[path]),
Some("tbd") => self.process_tbd(&path),
_ => {
// If the file cannot be parsed, it should be skipped to avoid a load error.
if !is_parsable(&path) {
return Err(ProcessingError::SkippedFile(path.to_owned()));
}
let settings_str = self.analysis_settings.to_string();
let file = binaryninja::load_project_file_with_progress(
project_file,
false,
Some(settings_str),
|_pos, _total| {
// TODO: Report progress
true
},
)
.ok_or_else(|| ProcessingError::BinaryViewLoad(path.to_owned()))?;
let data = self.process_view(path.to_owned(), &file);
file.file().close();
data
}
}
}
// TODO: Process mapping file
// TODO: A json file that maps type names to their type dlls
// TODO: Apples format (tbd to move symbols from default type lib to their actual place)
/// NOTE: Never pass a project file into this function, use [`TypeLibProcessor::process_project_file`]
/// instead as the file metadata will not attach to the project file to the view otherwise, leading
/// to incorrect dependency names.
pub fn process_file(&self, path: &Path) -> Result<ProcessedData, ProcessingError> {
// If the file cannot be parsed, it should be skipped to avoid a load error.
if !is_parsable(path) {
return Err(ProcessingError::SkippedFile(path.to_owned()));
}
let settings_str = self.analysis_settings.to_string();
let file = binaryninja::load_with_options_and_progress(
path,
false,
Some(settings_str),
|_pos, _total| {
// TODO: Report progress
true
},
)
.ok_or_else(|| ProcessingError::BinaryViewLoad(path.to_owned()))?;
let data = self.process_view(path.to_owned(), &file);
file.file().close();
data
}
pub fn process_view(
&self,
path: PathBuf,
view: &BinaryView,
) -> Result<ProcessedData, ProcessingError> {
self.state.set_file_state(path.to_owned(), false);
let view_platform = view.default_platform().unwrap_or(self.default_platform()?);
// Try and get the original file name, if not fall back to the default dependency name.
// TODO: I give up trying to actually make this reasonable, in the future we need to revisit
// TODO: how we save this information in the core so that its not a dozen lines of code to get
let dependency_name = match view.file().project_file() {
Some(project) => {
// We have to strip the .bndb extension because the project file path on disk is a guid
// so we just grab the project files "display name", because view.file().display_name()
// does not actually do what we want. We for some reason in the core rewrite the file
// name and display the name to be that of the bndb path instead of the file name associated
// with the actual view (which is actually useful information).
project
.path_in_project()
.file_name()
.unwrap_or(OsStr::new(&self.default_dependency_name))
.to_string_lossy()
.strip_suffix(".bndb")
.unwrap_or(&self.default_dependency_name)
.to_string()
}
None => view
.file()
.original_file_path()
.unwrap_or(path.clone())
.file_name()
.map(|s| s.to_string_lossy().to_string())
.unwrap_or_else(|| self.default_dependency_name.clone()),
};
let type_library = TypeLibrary::new(view_platform.arch(), &dependency_name);
type_library.add_platform(&view_platform);
// TODO: This has to be extremely slow
let platform_types = view_platform
.types()
.iter()
.map(|t| t.name.clone())
.collect::<HashSet<_>>();
let mut type_name_to_library = HashMap::new();
for tl in view.type_libraries().iter() {
let lib_name = tl.name().to_string();
for t in tl.named_types().iter() {
type_name_to_library.insert(t.name.clone(), lib_name.clone());
}
}
let add_referenced_types = |type_library: &TypeLibrary, ty: &Type| {
let mut referenced_ntrs: Vec<Ref<NamedTypeReference>> = Vec::new();
visit_type_reference(ty, &mut |ntr| {
referenced_ntrs.push(ntr.to_owned());
});
// Pull in all referenced types recursively.
while let Some(ntr) = referenced_ntrs.pop() {
let referenced_name = ntr.name();
if type_library
.get_named_type(referenced_name.clone())
.is_some()
{
continue;
}
if platform_types.contains(&referenced_name) {
// The type referenced comes from the platform, so we do not need to do anything.
} else if let Some(source) = type_name_to_library.get(&referenced_name) {
type_library.add_type_source(referenced_name, source);
} else {
// Type does not belong to another type library, so we add it to the current one.
if let Some(referenced_ty) = view.type_by_ref(&ntr) {
visit_type_reference(&referenced_ty, &mut |ntr| {
referenced_ntrs.push(ntr.to_owned());
});
type_library.add_named_type(referenced_name, &referenced_ty);
} else {
tracing::debug!(
"Type '{}' referenced by '{}' not found in view, skipping...",
referenced_name,
ty
);
}
}
}
};
let mut ordinals: HashMap<String, String> = HashMap::new();
let functions = view.functions();
tracing::info!("Adding {} functions", functions.len());
for func in &functions {
if !func.is_exported() {
continue;
}
let Some(defined_symbol) = func.defined_symbol() else {
tracing::debug!(
"Function '{}' has no defined symbol, skipping...",
func.symbol()
);
continue;
};
// Common case where we attach a "j_" prefix to the exported name, which ruins the symbol
// since it's expected to be imported by name. https://github.com/Vector35/binaryninja-api/issues/7970
let name = defined_symbol
.raw_name()
.to_string_lossy()
.replace("j_", "");
let qualified_name = QualifiedName::from(name.clone());
type_library.add_named_object(qualified_name, &func.function_type());
add_referenced_types(&type_library, &func.function_type());
if let Some(ordinal) = defined_symbol.ordinal() {
ordinals.insert(ordinal.to_string(), name);
}
}
if !ordinals.is_empty() {
tracing::info!(
"Found {} ordinals in '{}', adding metadata...",
ordinals.len(),
view.file(),
);
// TODO: The ordinal version is OSMAJOR_OSMINOR, pull from pe metadata (use object crate)
let key_md: Ref<Metadata> = String::from("ordinals_10_0").into();
type_library.store_metadata("ordinals", &key_md);
let map_md: Ref<Metadata> = ordinals.into();
type_library.store_metadata("ordinals_10_0", &map_md);
}
let mut processed_data = self.process_external_libraries(view)?;
processed_data.type_libraries.insert(type_library);
if let Some(api_set_section) = view.section_by_name(".apiset") {
let processed_api_set = self.process_api_set(view, &api_set_section)?;
tracing::info!(
"Found {} api set libraries in '{}', adding alternative names...",
processed_api_set.type_libraries.len(),
view.file(),
);
processed_data = ProcessedData::merge(&[processed_data, processed_api_set]);
}
self.state.set_file_state(path.to_owned(), true);
Ok(processed_data)
}
pub fn process_external_libraries(
&self,
view: &BinaryView,
) -> Result<ProcessedData, ProcessingError> {
let view_platform = view.default_platform().unwrap_or(self.default_platform()?);
let mut extern_type_libraries = HashMap::new();
for extern_lib in &view.external_libraries() {
let extern_type_library = TypeLibrary::new(view_platform.arch(), &extern_lib.name());
extern_type_library.add_platform(&view_platform);
extern_type_library.set_dependency_name(&extern_lib.name());
extern_type_libraries.insert(extern_lib.name(), extern_type_library);
}
// Pull import types and add them to respective type libraries.
for extern_loc in &view.external_locations() {
// The source symbol represents the symbol represented in the binary, while the target
// symbol represents the symbol that we intend to map the information to.
let src_sym = extern_loc.source_symbol();
let Some(extern_lib) = extern_loc.library() else {
tracing::debug!(
"External location '{}' has no library, skipping...",
src_sym
);
continue;
};
let Some(extern_type_library) = extern_type_libraries.get_mut(&extern_lib.name())
else {
tracing::warn!(
"External location '{}' is referencing a detached external library, skipping...",
src_sym
);
continue;
};
let Some(src_data_var) = view.data_variable_at_address(src_sym.address()) else {
tracing::debug!(
"External location '{}' has no data variable, skipping...",
src_sym
);
continue;
};
if src_data_var.auto_discovered {
// We do not want to record objects which are not modified by the user, otherwise
// we are recording the object each time we visit a binary view, possibly retrieving
// the old definition of the object.
tracing::debug!(
"External location '{}' is auto discovered, skipping...",
src_sym
);
continue;
}
let target_sym_name = extern_loc
.target_symbol()
.unwrap_or_else(|| src_sym.raw_name());
if !self.process_existing_type_libraries
&& view
.import_type_library_object(target_sym_name.clone(), None)
.is_some()
{
tracing::debug!(
"Skipping external location '{}' as it is already present in a type library",
target_sym_name.to_string_lossy()
);
continue;
}
// TODO: Need to visit all types referenced and add it to the type library.
extern_type_library.add_named_object(target_sym_name.into(), &src_data_var.ty.contents);
}
Ok(ProcessedData::new(
extern_type_libraries.values().cloned().collect(),
))
}
/// Process API sets on Windows binaries, so we can fill in the alternative names for type libraries
/// we are processing.
///
/// Creates an empty type library for the host and adds the alternative names to it. This should then
/// be passed to the [`ProcessedData::merge`] set to be merged with the type library of the host name.
///
/// For more information see: https://learn.microsoft.com/en-us/windows/win32/apiindex/windows-apisets
pub fn process_api_set(
&self,
view: &BinaryView,
section: &Section,
) -> Result<ProcessedData, ProcessingError> {
let section_bytes = view
.read_buffer(section.start(), section.len())
.ok_or_else(|| ProcessingError::BinaryViewRead(section.start(), section.len()))?;
let api_set_map = ApiSetMap::try_from_apiset_section_bytes(section_bytes.get_data())?;
let mut target_map: HashMap<String, HashSet<String>> = HashMap::new();
for entry in api_set_map.namespace_entries()? {
let alternative_name = entry.name()?.to_string_lossy();
for value_entry in entry.value_entries()? {
// TODO: In cases where alt -> kernel32.dll -> kernelbase.dll we currently associate
// TODO: with kernel32.dll as its assumed there is a wrapper function that calls into
// TODO: kernelbase.dll. This keeps us from having to validate against both, in the case
// TODO: of kernelbase.dll being before the function was moved there.
let _forwarder_name = value_entry.name()?.to_string_lossy();
let target_name = value_entry.value()?.to_string_lossy();
target_map
.entry(target_name)
.or_default()
.insert(alternative_name.clone());
}
}
// Instead of using the view, we use the user-provided platform, the reason is because the
// 'apisetschema.dll' is shared across multiple archs, and we need to be able to merge its data
// with other platforms so that they get the correct alternative names.
let platform = self.default_platform()?;
let mut mapping_type_libraries = Vec::new();
for (target_name, alternative_names) in target_map {
let type_library = TypeLibrary::new(platform.arch(), &target_name);
for alt_name in alternative_names {
type_library.add_alternate_name(&alt_name);
}
mapping_type_libraries.push(type_library);
}
Ok(ProcessedData::new(mapping_type_libraries))
}
/// We want to be able to process already created type libraries so that they can be consulted
/// during the [`ProcessedData::merge`] step. This lets us add overrides like extra platforms.
pub fn process_type_library(&self, path: &Path) -> Result<ProcessedData, ProcessingError> {
self.state.set_file_state(path.to_owned(), false);
let finalized_type_library = TypeLibrary::load_from_file(path)
.ok_or_else(|| ProcessingError::InvalidTypeLibrary(path.to_owned()))?;
self.state.set_file_state(path.to_owned(), true);
Ok(ProcessedData::new(vec![finalized_type_library]))
}
pub fn process_source(&self, path: &Path) -> Result<ProcessedData, ProcessingError> {
self.state.set_file_state(path.to_owned(), false);
let platform = self.default_platform()?;
let parser =
CoreTypeParser::parser_by_name("ClangTypeParser").expect("Failed to get clang parser");
let platform_type_container = platform.type_container();
let header_contents = std::fs::read_to_string(path).map_err(ProcessingError::FileRead)?;
let file_name = path
.file_name()
.unwrap_or(OsStr::new("source.hpp"))
.to_string_lossy();
// TODO: Allow specifying options?
let mut include_dirs = self.include_directories.clone();
// TODO: This will not work for projects, we need to remove this parent call
// TODO: and place it in the `include_directories`, where we parse that from the user input
// TODO: To the
if let Some(p) = path.parent() {
include_dirs.push(p.to_owned());
}
let parsed_types = parser
.parse_types_from_source(
&header_contents,
&file_name,
&platform,
&platform_type_container,
&self.compiler_options,
&include_dirs,