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
use std::collections::HashMap;

use anyhow::Error;
use itertools::izip;

use crate::abstract_domain::TryToBitvec;
use crate::analysis::pointer_inference::State as PointerInferenceState;
use crate::prelude::*;
use crate::{
    abstract_domain::{AbstractDomain, DataDomain, DomainInsertion, HasTop, IntervalDomain},
    analysis::string_abstraction::{context::Context, state::State},
    intermediate_representation::{Arg, Datatype, ExternSymbol},
    utils::arguments::get_variable_parameters,
};

impl<'a, T: AbstractDomain + DomainInsertion + HasTop + Eq + From<String>> Context<'a, T> {
    /// Handles the detection of string parameters to scanf calls.
    /// Adds new string abstract domains to the current state.
    pub fn handle_scanf_calls(&self, state: &State<T>, extern_symbol: &ExternSymbol) -> State<T> {
        let mut new_state = state.clone();
        if let Some(pi_state) = state.get_pointer_inference_state() {
            // Check whether the format string parameters can be parsed.
            if let Ok(return_values) = get_variable_parameters(
                self.project,
                pi_state,
                extern_symbol,
                &self.format_string_index_map,
            ) {
                self.create_abstract_domain_entries_for_function_return_values(
                    pi_state,
                    &mut new_state,
                    return_values.into_iter().map(|arg| (arg, None)).collect(),
                );
            }
        }

        new_state
    }

    /// Creates string abstract domains for return values of (s)scanf calls.
    pub fn create_abstract_domain_entries_for_function_return_values(
        &self,
        pi_state: &PointerInferenceState,
        state: &mut State<T>,
        arg_to_value_map: HashMap<Arg, Option<String>>,
    ) {
        for (argument, value) in arg_to_value_map.into_iter() {
            if argument.get_data_type().unwrap() == Datatype::Pointer {
                if let Ok(data) =
                    pi_state.eval_parameter_arg(&argument, &self.project.runtime_memory_image)
                {
                    if !data.get_relative_values().is_empty() {
                        Context::add_constant_or_top_value_to_return_locations(
                            state, pi_state, data, value,
                        );
                    }
                }
            }
        }
    }

    /// Adds constant or *Top* value to return location given a pointer and a potential value.
    pub fn add_constant_or_top_value_to_return_locations(
        state: &mut State<T>,
        pi_state: &PointerInferenceState,
        return_target: DataDomain<IntervalDomain>,
        value: Option<String>,
    ) {
        if let Some(string) = value {
            Context::add_new_string_abstract_domain(
                state,
                pi_state,
                return_target.get_relative_values(),
                T::from(string),
            );
        } else {
            Context::add_new_string_abstract_domain(
                state,
                pi_state,
                return_target.get_relative_values(),
                T::create_top_value_domain(),
            );
        }

        state.add_unassigned_return_pointer(return_target);
    }

    /// Handles calls to sscanf. If the source string is known, it is split by spaces
    /// and for each substring a string abstract domain is generated at its corresponding location.
    pub fn handle_sscanf_calls(&self, state: &State<T>, extern_symbol: &ExternSymbol) -> State<T> {
        let mut new_state = state.clone();
        if let Some(pi_state) = state.get_pointer_inference_state() {
            if let Some(source_string_arg) = extern_symbol.parameters.first() {
                if let Ok(source_string) = pi_state
                    .eval_parameter_arg(source_string_arg, &self.project.runtime_memory_image)
                {
                    if self.source_string_mapped_to_return_locations(
                        pi_state,
                        &mut new_state,
                        &source_string,
                        extern_symbol,
                    ) {
                        return new_state;
                    }
                }
            }
            new_state = self.handle_scanf_calls(&new_state, extern_symbol);
        }

        new_state
    }

    /// Maps the source string to the return locations of the call and returns an boolean
    /// which indicates whether the operation was successful.
    pub fn source_string_mapped_to_return_locations(
        &self,
        pi_state: &PointerInferenceState,
        state: &mut State<T>,
        source_string: &DataDomain<IntervalDomain>,
        extern_symbol: &ExternSymbol,
    ) -> bool {
        if let Some(global_address) = source_string.get_absolute_value() {
            if let Ok(source_string) = self
                .project
                .runtime_memory_image
                .read_string_until_null_terminator(
                    &global_address
                        .try_to_bitvec()
                        .expect("Could not translate interval address to bitvector."),
                )
            {
                if let Ok(source_return_string_map) = self
                    .map_source_string_parameters_to_return_arguments(
                        pi_state,
                        extern_symbol,
                        source_string,
                    )
                {
                    self.create_abstract_domain_entries_for_function_return_values(
                        pi_state,
                        state,
                        source_return_string_map,
                    );

                    return true;
                }
            }
        }

        false
    }

    /// Maps source strings parameters to return arguments for sscanf calls.
    pub fn map_source_string_parameters_to_return_arguments(
        &self,
        pi_state: &PointerInferenceState,
        extern_symbol: &ExternSymbol,
        source_string: &str,
    ) -> Result<HashMap<Arg, Option<String>>, Error> {
        if let Ok(all_parameters) = get_variable_parameters(
            self.project,
            pi_state,
            extern_symbol,
            &self.format_string_index_map,
        ) {
            let return_values: Vec<String> =
                source_string.split(' ').map(|s| s.to_string()).collect();

            return Ok(Context::<T>::filter_out_all_non_string_args(
                all_parameters,
                return_values,
            ));
        }

        Err(anyhow!("Could not map source string to return parameters."))
    }

    /// Filters out all parameters that are not of type string.
    pub fn filter_out_all_non_string_args(
        all_parameters: Vec<Arg>,
        return_values: Vec<String>,
    ) -> HashMap<Arg, Option<String>> {
        izip!(all_parameters, return_values)
            .filter_map(|(param, value)| {
                if matches!(param.get_data_type(), Some(Datatype::Pointer)) {
                    Some((param, Some(value)))
                } else {
                    None
                }
            })
            .collect()
    }
}

#[cfg(test)]
mod tests {
    use super::super::tests::*;
    use super::*;
    use crate::abstract_domain::{AbstractIdentifier, AbstractLocation, CharacterInclusionDomain};
    use crate::analysis::pointer_inference::PointerInference as PointerInferenceComputation;
    use crate::analysis::string_abstraction::tests::mock_project_with_intraprocedural_control_flow;
    use crate::{expr, intermediate_representation::*, variable};

    #[test]
    fn test_handle_scanf_calls() {
        let scanf_symbol = ExternSymbol::mock_scanf_symbol_arm();

        let project = mock_project_with_intraprocedural_control_flow(
            vec![(scanf_symbol.clone(), vec![true])],
            "func",
        );
        let mut pi_results = PointerInferenceComputation::mock(&project);
        pi_results.compute(false);

        let setup: Setup<CharacterInclusionDomain> = Setup::new(&pi_results);

        let new_state = setup
            .context
            .handle_scanf_calls(&setup.state_before_call, &scanf_symbol);

        let top_value = CharacterInclusionDomain::from("".to_string()).top();

        let stack_id = AbstractIdentifier::new(
            Tid::new("func"),
            AbstractLocation::from_var(&variable!("sp:4")).unwrap(),
        );

        assert!(new_state
            .get_unassigned_return_pointer()
            .contains(&DataDomain::from_target(
                stack_id.clone(),
                Bitvector::from_i32(-0x74).into(),
            )));

        assert!(new_state
            .get_unassigned_return_pointer()
            .contains(&DataDomain::from_target(
                stack_id.clone(),
                Bitvector::from_i32(-0x5e).into(),
            )));

        assert!(new_state
            .get_unassigned_return_pointer()
            .contains(&DataDomain::from_target(
                stack_id.clone(),
                Bitvector::from_i32(-0x4c).into(),
            )));

        assert!(new_state
            .get_unassigned_return_pointer()
            .contains(&DataDomain::from_target(
                stack_id.clone(),
                Bitvector::from_i32(-0x38).into(),
            )));

        assert_eq!(
            *new_state
                .get_stack_offset_to_string_map()
                .get(&(-0x74 as i64))
                .unwrap(),
            top_value,
        );
        assert_eq!(
            *new_state
                .get_stack_offset_to_string_map()
                .get(&(-0x5e as i64))
                .unwrap(),
            top_value,
        );
        assert_eq!(
            *new_state
                .get_stack_offset_to_string_map()
                .get(&(-0x4c as i64))
                .unwrap(),
            top_value,
        );
        assert_eq!(
            *new_state
                .get_stack_offset_to_string_map()
                .get(&(-0x38 as i64))
                .unwrap(),
            top_value,
        );
    }

    #[test]
    fn test_create_abstract_domain_entries_for_function_return_values_with_known_values() {
        let r2_reg = variable!("r2:4");
        let sscanf_symbol = ExternSymbol::mock_sscanf_symbol_arm();

        let project = mock_project_with_intraprocedural_control_flow(
            vec![(sscanf_symbol.clone(), vec![true, true])],
            "func",
        );
        let mut pi_results = PointerInferenceComputation::mock(&project);
        pi_results.compute(false);

        let mut setup: Setup<CharacterInclusionDomain> = Setup::new(&pi_results);

        let stack_id = AbstractIdentifier::new(
            Tid::new("func"),
            AbstractLocation::from_var(&variable!("sp:4")).unwrap(),
        );

        let mut arg_to_value_map: HashMap<Arg, Option<String>> = HashMap::new();

        let register_arg = Arg::Register {
            expr: Expression::Var(r2_reg.clone()),
            data_type: Some(Datatype::Pointer),
        };
        let stack_arg = Arg::Stack {
            address: expr!("sp:4"),
            size: ByteSize::new(4),
            data_type: Some(Datatype::Pointer),
        };
        arg_to_value_map.insert(register_arg, Some("a".to_string()));
        arg_to_value_map.insert(stack_arg, Some("b".to_string()));

        setup
            .context
            .create_abstract_domain_entries_for_function_return_values(
                &setup.pi_state_before_symbol_call,
                &mut setup.state_before_call,
                arg_to_value_map,
            );

        assert_eq!(
            *setup
                .state_before_call
                .get_stack_offset_to_string_map()
                .get(&(-0x7c as i64))
                .unwrap(),
            CharacterInclusionDomain::from("a".to_string())
        );

        assert_eq!(
            *setup
                .state_before_call
                .get_stack_offset_to_string_map()
                .get(&(-0x92 as i64))
                .unwrap(),
            CharacterInclusionDomain::from("b".to_string())
        );

        assert!(setup
            .state_before_call
            .get_unassigned_return_pointer()
            .contains(&DataDomain::from_target(
                stack_id.clone(),
                Bitvector::from_i32(-0x7c).into(),
            )));

        assert!(setup
            .state_before_call
            .get_unassigned_return_pointer()
            .contains(&DataDomain::from_target(
                stack_id.clone(),
                Bitvector::from_i32(-0x92).into(),
            )));
    }

    #[test]
    fn test_create_abstract_domain_entries_for_function_return_values_with_unknown_values() {
        let r1_reg = variable!("r1:4");
        let scanf_symbol = ExternSymbol::mock_scanf_symbol_arm();

        let project = mock_project_with_intraprocedural_control_flow(
            vec![(scanf_symbol.clone(), vec![false])],
            "func",
        );
        let mut pi_results = PointerInferenceComputation::mock(&project);
        pi_results.compute(false);

        let mut setup: Setup<CharacterInclusionDomain> = Setup::new(&pi_results);

        let stack_id = AbstractIdentifier::new(
            Tid::new("func"),
            AbstractLocation::from_var(&variable!("sp:4")).unwrap(),
        );

        let mut arg_to_value_map: HashMap<Arg, Option<String>> = HashMap::new();
        let register_arg = Arg::Register {
            expr: Expression::Var(r1_reg.clone()),
            data_type: Some(Datatype::Pointer),
        };
        let stack_arg = Arg::Stack {
            address: expr!("sp:4"),
            size: ByteSize::new(4),
            data_type: Some(Datatype::Pointer),
        };
        arg_to_value_map.insert(register_arg, None);
        arg_to_value_map.insert(stack_arg, None);

        setup
            .context
            .create_abstract_domain_entries_for_function_return_values(
                &setup.pi_state_before_symbol_call,
                &mut setup.state_before_call,
                arg_to_value_map,
            );

        assert_eq!(
            *setup
                .state_before_call
                .get_stack_offset_to_string_map()
                .get(&(-0x74 as i64))
                .unwrap(),
            CharacterInclusionDomain::Top
        );

        assert_eq!(
            *setup
                .state_before_call
                .get_stack_offset_to_string_map()
                .get(&(-0x38 as i64))
                .unwrap(),
            CharacterInclusionDomain::Top
        );

        assert!(setup
            .state_before_call
            .get_unassigned_return_pointer()
            .contains(&DataDomain::from_target(
                stack_id.clone(),
                Bitvector::from_i32(-0x74).into(),
            )));

        assert!(setup
            .state_before_call
            .get_unassigned_return_pointer()
            .contains(&DataDomain::from_target(
                stack_id.clone(),
                Bitvector::from_i32(-0x38).into(),
            )));
    }

    #[test]
    fn test_add_constant_or_top_value_to_return_locations() {
        let sscanf_symbol = ExternSymbol::mock_sscanf_symbol_arm();
        let project = mock_project_with_intraprocedural_control_flow(
            vec![(sscanf_symbol.clone(), vec![false, false])],
            "func",
        );
        let mut pi_results = PointerInferenceComputation::mock(&project);
        pi_results.compute(false);

        let mut setup: Setup<CharacterInclusionDomain> = Setup::new(&pi_results);

        let stack_id = AbstractIdentifier::new(
            Tid::new("func"),
            AbstractLocation::from_var(&variable!("sp:4")).unwrap(),
        );

        let return_target: DataDomain<IntervalDomain> =
            DataDomain::from_target(stack_id, IntervalDomain::mock(-124, -124));

        Context::<CharacterInclusionDomain>::add_constant_or_top_value_to_return_locations(
            &mut setup.state_before_call,
            &setup.pi_state_before_symbol_call,
            return_target.clone(),
            Some("str1".to_string()),
        );

        assert_eq!(
            *setup
                .state_before_call
                .get_stack_offset_to_string_map()
                .get(&-124)
                .unwrap(),
            CharacterInclusionDomain::ci("str1")
        );
        assert!(setup
            .state_before_call
            .get_unassigned_return_pointer()
            .contains(&return_target));
    }

    #[test]
    fn test_handle_sscanf_calls_unknown_source_unknown_format() {
        let sscanf_symbol = ExternSymbol::mock_sscanf_symbol_arm();

        let project = mock_project_with_intraprocedural_control_flow(
            vec![(sscanf_symbol.clone(), vec![false, false])],
            "func",
        );
        let mut pi_results = PointerInferenceComputation::mock(&project);
        pi_results.compute(false);

        let setup: Setup<CharacterInclusionDomain> = Setup::new(&pi_results);

        let new_state = setup
            .context
            .handle_sscanf_calls(&setup.state_before_call, &sscanf_symbol);

        assert!(new_state.get_stack_offset_to_string_map().is_empty());
        assert!(new_state.get_unassigned_return_pointer().is_empty());
    }

    #[test]
    fn test_handle_sscanf_calls_known_source_unknown_format() {
        let sscanf_symbol = ExternSymbol::mock_sscanf_symbol_arm();

        let project = mock_project_with_intraprocedural_control_flow(
            vec![(sscanf_symbol.clone(), vec![true, false])],
            "func",
        );
        let mut pi_results = PointerInferenceComputation::mock(&project);
        pi_results.compute(false);

        let setup: Setup<CharacterInclusionDomain> = Setup::new(&pi_results);

        let new_state = setup
            .context
            .handle_sscanf_calls(&setup.state_before_call, &sscanf_symbol);

        assert!(new_state.get_unassigned_return_pointer().is_empty());
        assert!(new_state.get_stack_offset_to_string_map().is_empty());
    }

    #[test]
    fn test_handle_sscanf_calls_unknown_source_known_format() {
        let sscanf_symbol = ExternSymbol::mock_sscanf_symbol_arm();

        let project = mock_project_with_intraprocedural_control_flow(
            vec![(sscanf_symbol.clone(), vec![false, true])],
            "func",
        );
        let mut pi_results = PointerInferenceComputation::mock(&project);
        pi_results.compute(false);

        let setup: Setup<CharacterInclusionDomain> = Setup::new(&pi_results);

        let stack_id = AbstractIdentifier::new(
            Tid::new("func"),
            AbstractLocation::from_var(&variable!("sp:4")).unwrap(),
        );

        let new_state = setup
            .context
            .handle_sscanf_calls(&setup.state_before_call, &sscanf_symbol);

        assert_eq!(
            *new_state
                .get_stack_offset_to_string_map()
                .get(&(-0x7c as i64))
                .unwrap(),
            CharacterInclusionDomain::Top
        );

        assert_eq!(
            *new_state
                .get_stack_offset_to_string_map()
                .get(&(-0x92 as i64))
                .unwrap(),
            CharacterInclusionDomain::Top
        );

        assert!(new_state
            .get_unassigned_return_pointer()
            .contains(&DataDomain::from_target(
                stack_id.clone(),
                Bitvector::from_i32(-0x7c).into(),
            )));

        assert!(new_state
            .get_unassigned_return_pointer()
            .contains(&DataDomain::from_target(
                stack_id.clone(),
                Bitvector::from_i32(-0x92).into(),
            )));
    }

    #[test]
    fn test_handle_sscanf_calls_known_source_known_format() {
        let sscanf_symbol = ExternSymbol::mock_sscanf_symbol_arm();

        let project = mock_project_with_intraprocedural_control_flow(
            vec![(sscanf_symbol.clone(), vec![true, true])],
            "func",
        );
        let mut pi_results = PointerInferenceComputation::mock(&project);
        pi_results.compute(false);

        let setup: Setup<CharacterInclusionDomain> = Setup::new(&pi_results);

        let stack_id = AbstractIdentifier::new(
            Tid::new("func"),
            AbstractLocation::from_var(&variable!("sp:4")).unwrap(),
        );

        let new_state = setup
            .context
            .handle_sscanf_calls(&setup.state_before_call, &sscanf_symbol);

        assert_eq!(
            *new_state
                .get_stack_offset_to_string_map()
                .get(&(-0x7c as i64))
                .unwrap(),
            CharacterInclusionDomain::from("str1".to_string())
        );
        assert_eq!(
            *new_state
                .get_stack_offset_to_string_map()
                .get(&(-0x68 as i64))
                .unwrap(),
            CharacterInclusionDomain::from("str2".to_string())
        );
        assert_eq!(
            *new_state
                .get_stack_offset_to_string_map()
                .get(&(-0x92 as i64))
                .unwrap(),
            CharacterInclusionDomain::from("str3".to_string())
        );
        assert_eq!(
            *new_state
                .get_stack_offset_to_string_map()
                .get(&(-0x84 as i64))
                .unwrap(),
            CharacterInclusionDomain::from("str4".to_string())
        );

        assert!(new_state
            .get_unassigned_return_pointer()
            .contains(&DataDomain::from_target(
                stack_id.clone(),
                Bitvector::from_i32(-0x7c).into(),
            )));

        assert!(new_state
            .get_unassigned_return_pointer()
            .contains(&DataDomain::from_target(
                stack_id.clone(),
                Bitvector::from_i32(-0x68).into(),
            )));

        assert!(new_state
            .get_unassigned_return_pointer()
            .contains(&DataDomain::from_target(
                stack_id.clone(),
                Bitvector::from_i32(-0x92).into(),
            )));

        assert!(new_state
            .get_unassigned_return_pointer()
            .contains(&DataDomain::from_target(
                stack_id.clone(),
                Bitvector::from_i32(-0x84).into(),
            )));
    }

    #[test]
    fn test_source_string_mapped_to_return_locations() {
        let source_string: DataDomain<IntervalDomain> =
            DataDomain::from(Bitvector::from_i32(0x7000));
        let sscanf_symbol = ExternSymbol::mock_sscanf_symbol_arm();

        let project = mock_project_with_intraprocedural_control_flow(
            vec![(sscanf_symbol.clone(), vec![true, true])],
            "func",
        );
        let mut pi_results = PointerInferenceComputation::mock(&project);
        pi_results.compute(false);

        let mut setup: Setup<CharacterInclusionDomain> = Setup::new(&pi_results);

        setup.context.source_string_mapped_to_return_locations(
            &setup.pi_state_before_symbol_call,
            &mut setup.state_before_call,
            &source_string,
            &sscanf_symbol,
        );

        assert_eq!(
            CharacterInclusionDomain::ci("str1"),
            *setup
                .state_before_call
                .get_stack_offset_to_string_map()
                .get(&-124)
                .unwrap()
        );

        assert_eq!(
            CharacterInclusionDomain::ci("str2"),
            *setup
                .state_before_call
                .get_stack_offset_to_string_map()
                .get(&-104)
                .unwrap()
        );

        assert_eq!(
            CharacterInclusionDomain::ci("str4"),
            *setup
                .state_before_call
                .get_stack_offset_to_string_map()
                .get(&-132)
                .unwrap()
        );

        assert_eq!(
            CharacterInclusionDomain::ci("str3"),
            *setup
                .state_before_call
                .get_stack_offset_to_string_map()
                .get(&-146)
                .unwrap()
        );
    }

    #[test]
    fn test_map_source_string_parameters_to_return_arguments() {
        let source_string = "str1 str2 str3 str4";
        let sscanf_symbol = ExternSymbol::mock_sscanf_symbol_arm();

        let project = mock_project_with_intraprocedural_control_flow(
            vec![(sscanf_symbol.clone(), vec![true, true])],
            "func",
        );
        let mut pi_results = PointerInferenceComputation::mock(&project);
        pi_results.compute(false);

        let setup: Setup<CharacterInclusionDomain> = Setup::new(&pi_results);

        let expected_result: HashMap<Arg, Option<String>> = vec![
            (
                Arg::mock_pointer_register("r2", 4),
                Some("str1".to_string()),
            ),
            (
                Arg::mock_pointer_register("r3", 4),
                Some("str2".to_string()),
            ),
            (
                Arg::Stack {
                    address: expr!("sp:4"),
                    size: ByteSize::new(4),
                    data_type: Some(Datatype::Pointer),
                },
                Some("str3".to_string()),
            ),
            (
                Arg::Stack {
                    address: expr!("sp:4 + 4:4"),
                    size: ByteSize::new(4),
                    data_type: Some(Datatype::Pointer),
                },
                Some("str4".to_string()),
            ),
        ]
        .into_iter()
        .collect();

        assert_eq!(
            expected_result,
            setup
                .context
                .map_source_string_parameters_to_return_arguments(
                    &setup.pi_state_before_symbol_call,
                    &sscanf_symbol,
                    source_string
                )
                .unwrap()
        );
    }

    #[test]
    fn test_filter_out_all_non_string_args() {
        let args = vec![
            Arg::mock_pointer_register("r0", 4),
            Arg::mock_register("r1", 4),
        ];
        let values = vec!["cat ".to_string(), "2".to_string()];

        let expected_output: HashMap<Arg, Option<String>> = vec![(
            Arg::mock_pointer_register("r0", 4),
            Some("cat ".to_string()),
        )]
        .into_iter()
        .collect();

        assert_eq!(
            expected_output,
            Context::<CharacterInclusionDomain>::filter_out_all_non_string_args(args, values)
        );
    }
}