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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
/*
 * Panopticon - A libre disassembler
 * Copyright (C) 2014, 2015  Panopticon authors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

//! Functions are a graph of `BasicBlock`s connected with conditional jumps.
//!
//! Functions have an entry point, a (non-unique) name and an unique identifier. Functions
//! do not share basic blocks. In case functions overlap in the binary, the basic blocks are copied
//! by the disassembler.
//!
//! Functions have the concept of unresolved basic blocks. These are inserted into the graph if a
//! indirect branch could not be resolved. If disassembly failes for example because an unknown
//! instruction was found, an error node is inserted into the graph to allow displaying a message
//! on the front-end.


use {Architecture, BasicBlock, Guard, Mnemonic, Operation, Region, Result, Rvalue, Statement};

use panopticon_graph_algos::{AdjacencyList, EdgeListGraphTrait, GraphTrait, MutableGraphTrait, VertexListGraphTrait};
use panopticon_graph_algos::adjacency_list::{AdjacencyListEdgeDescriptor, AdjacencyListVertexDescriptor, VertexLabelIterator};
use panopticon_graph_algos::search::{TraversalOrder, TreeIterator};
use std::borrow::Cow;
use std::collections::{BTreeMap, HashMap, HashSet};
use uuid::Uuid;

/// An iterator over every BasicBlock in a Function
pub struct BasicBlockIterator<'a> {
    iter: VertexLabelIterator<'a, ControlFlowRef, ControlFlowTarget>
}

impl<'a> BasicBlockIterator<'a> {
    /// Create a new statement iterator from `mnemonics`
    pub fn new(cfg: &'a ControlFlowGraph) -> Self {
        BasicBlockIterator {
            iter: cfg.vertex_labels(),
        }
    }
}

impl<'a> Iterator for BasicBlockIterator<'a> {
    type Item = &'a BasicBlock;
    fn next(&mut self) ->  Option<Self::Item> {
        loop {
            match self.iter.next() {
                None => return None,
                Some(&ControlFlowTarget::Resolved(ref bb)) => return Some(bb),
                _ => ()
            }
        }
    }
}

/// Node of the function graph.
#[derive(Serialize,Deserialize,Debug,Clone)]
pub enum ControlFlowTarget {
    /// A basic block
    Resolved(BasicBlock),
    /// An unresolved indirect jump
    Unresolved(Rvalue),
    /// An error occured while disassembling
    Failed(u64, Cow<'static, str>),
}

/// Graph of basic blocks and jumps
pub type ControlFlowGraph = AdjacencyList<ControlFlowTarget, Guard>;
/// Stable reference to a node in the `ControlFlowGraph`
pub type ControlFlowRef = AdjacencyListVertexDescriptor;
/// Stable reference to an edge in the `ControlFlowGraph`
pub type ControlFlowEdge = AdjacencyListEdgeDescriptor;

#[derive(Debug, Clone, Serialize, Deserialize)]
/// The kind of function this is, to distinguish plt stubs from regular functions.
pub enum FunctionKind {
    /// A regular function
    Regular,
    /// A PLT stub, which is a name  and an address pointing to its PLT table entry
    Stub {
        /// The import name of this stub, as found in the PLT table
        name: String,
        /// The address of this stub in the PLT table
        plt_address: u64
    }
}

/// A set of basic blocks connected by conditional jumps
#[derive(Serialize,Deserialize,Debug,Clone)]
pub struct Function {
    /// Display name of the function.
    pub name: String,
    aliases: Vec<String>,
    /// Unique, immutable identifier for this function.
    uuid: Uuid,
    /// Graph of basic blocks and jumps
    cflow_graph: ControlFlowGraph,
    /// The function's entry point
    entry_point: ControlFlowRef,
    /// Name of the memory region the function is part of
    region: String,
    /// The size of this function, in bytes (only counts the number of instructions, not padding bytes, or gaps for non-contiguous functions)
    size: usize,
    /// What kind of function is this
    kind: FunctionKind,
}

#[derive(Clone,PartialEq,Eq,Debug)]
enum MnemonicOrError {
    Mnemonic(Mnemonic),
    Error(u64, Cow<'static, str>),
}

impl Function {
    /// Create an undefined Function. This function has undefined behavior. Creating an undefined Function always succeeds, and is usually a bad idea. Don't do it unless you know what you're doing.
    pub fn undefined(start: u64, uuid: Option<Uuid>, region: &Region, name: Option<String>) -> Function {
        let mut cflow_graph = AdjacencyList::new();
        let entry_point = ControlFlowTarget::Unresolved(Rvalue::new_u64(start));
        let entry_point = cflow_graph.add_vertex(entry_point);
        Function {
            name: name.unwrap_or(format!("func_{:#x}", start)),
            aliases: Vec::new(),
            uuid: uuid.unwrap_or(Uuid::new_v4()),
            cflow_graph,
            entry_point,
            region: region.name().clone(),
            size: 0,
            kind: FunctionKind::Regular,
        }
    }
    // this private method is where the meat of making a function is;
    // almost all perf gains for function disassembly will be in here, and related functions like, assemble_cflow_graph, etc.
    fn disassemble<A: Architecture>(start: u64, cflow_graph: &mut ControlFlowGraph, size: &mut usize, name: &str, uuid: &Uuid, region: &Region, init: A::Configuration) -> Result<ControlFlowRef> {
        let (mut mnemonics, mut by_source, mut by_destination) = Self::index_cflow_graph(cflow_graph, start);

        let mut todo = cflow_graph.vertex_labels().filter_map(|lb| {
            if let &ControlFlowTarget::Unresolved(Rvalue::Constant{ value,.. }) = lb {
                Some(value)
            } else {
                None
            }
        }).collect::<HashSet<u64>>();

        todo.insert(start);

        while let Some(addr) = todo.iter().next().cloned() {
            let maybe_mnes = mnemonics.iter().find(|x| *x.0 >= addr).map(|x| x.1.clone());

            assert!(todo.remove(&addr));

            if let Some(mnes) = maybe_mnes {
                if !mnes.is_empty() {
                    match mnes.first() {
                        Some(&MnemonicOrError::Mnemonic(ref mne)) => {
                            if mne.area.start < addr && mne.area.end > addr {
                                mnemonics.entry(addr).or_insert(Vec::new()).push(MnemonicOrError::Error(addr, "Jump inside instruction".into()));
                                continue;
                            } else if mne.area.start == addr {
                                *size += mne.size();
                                continue;
                            }
                        }
                        Some(&MnemonicOrError::Error(ref pos, _)) => {
                            if *pos == addr {
                                continue;
                            }
                        }
                        None => unreachable!(),
                    }
                }
            }

            let maybe_match = A::decode(region, addr, &init);

            match maybe_match {
                Ok(match_st) => {
                    if match_st.mnemonics.is_empty() {
                        mnemonics.entry(addr).or_insert(Vec::new()).push(MnemonicOrError::Error(addr, "Unrecognized instruction".into()));
                    } else {
                        for mne in match_st.mnemonics {
                            debug!(
                                "{:x}: {} ({:?})",
                                mne.area.start,
                                mne.opcode,
                                match_st.tokens
                            );
                            *size += mne.size();
                            mnemonics.entry(mne.area.start).or_insert(Vec::new()).push(MnemonicOrError::Mnemonic(mne));
                        }
                    }

                    for (origin, tgt, gu) in match_st.jumps {
                        debug!("jump to {:?}", tgt);
                        match tgt {
                            Rvalue::Constant { value: ref c, .. } => {
                                by_source.entry(origin).or_insert(Vec::new()).push((tgt.clone(), gu.clone()));
                                by_destination.entry(*c).or_insert(Vec::new()).push((Rvalue::new_u64(origin), gu.clone()));
                                todo.insert(*c);
                            }
                            _ => {
                                by_source.entry(origin).or_insert(Vec::new()).push((tgt, gu.clone()));
                            }
                        }
                    }
                }
                Err(e) => {
                    error!("failed to disassemble: {}", e);
                    mnemonics.entry(addr).or_insert(Vec::new()).push(MnemonicOrError::Error(addr, "Unrecognized instruction".into()));
                }
            }
        }

        let cfg = Self::assemble_cflow_graph(mnemonics, by_source, by_destination, start);
        let ep = cfg
            .vertices()
            .find(
                |&x| match cfg.vertex_label(x) {
                    Some(&ControlFlowTarget::Resolved(ref bb)) => bb.area.start == start && bb.area.end > start,
                    _ => false,
                }
            );

        match ep {
            Some(entry_point) => {
                *cflow_graph = cfg;
                Ok(entry_point)
            },
            None => {
                Err(format!("function ({}) {} has no entry point", name, uuid).into())
            }
        }
    }
    /// Continue disassembling from `start`, at `region`, with CPU `configuration`, using the functions current, internal control flow graph.
    pub fn cont<A: Architecture>(&mut self, start: u64, region: &Region, configuration: A::Configuration) -> Result<()> {
        self.entry_point = Self::disassemble::<A>(start, &mut self.cflow_graph, &mut self.size, &self.name, &self.uuid, region, configuration)?;
        Ok(())
    }

    /// Create and start disassembling a new function with `name`, inside memory `region`, starting at entry point `start`, with a random UUID.
    pub fn new<A: Architecture>(start: u64, region: &Region, name: Option<String>, init: A::Configuration) -> Result<Function> {
        let mut cflow_graph = AdjacencyList::new();
        let entry_point = ControlFlowTarget::Unresolved(Rvalue::new_u64(start));
        cflow_graph.add_vertex(entry_point);
        let mut size = 0;
        let name = name.unwrap_or(format!("func_{:#x}", start));
        let uuid = Uuid::new_v4();
        let entry_point = Self::disassemble::<A>(start, &mut cflow_graph, &mut size, &name, &uuid, region, init)?;
        Ok(Function {
            name,
            aliases: Vec::new(),
            uuid,
            cflow_graph,
            entry_point,
            region: region.name().clone(),
            size,
            kind: FunctionKind::Regular,
        })
    }

    /// Returns the start address of the first basic block in this function
    pub fn start(&self) -> u64 {
        self.entry_point().area.start
    }

    /// Returns the end address of the highest basic block in this function
    pub fn end(&self) -> u64 {
        let mut end = self.entry_point().area.end;
        for bb in self.basic_blocks() {
            end = ::std::cmp::max(bb.area.end, end);
        }
        end
    }

    /// Whether the given address is contained within this function
    pub fn contains(&self, address: u64) -> bool {
        for bb in self.basic_blocks() {
            if bb.area.start >= address && address < bb.area.end {
                return true
            }
        }
        false
    }

    /// New function starting at `start`, with name `name`, inside memory region `region` and UUID `uuid`.
    pub fn with_uuid<A: Architecture>(start: u64, uuid: &Uuid, region: &Region, name: Option<String>, init: A::Configuration) -> Result<Function> {
        let mut f = Function::new::<A>(start, region, name, init)?;
        f.uuid = uuid.clone();
        Ok(f)
    }

    /// Returns the UUID of this function
    pub fn uuid(&self) -> &Uuid {
        &self.uuid
    }

    /// The size of this function, in bytes (only counts the number of instructions, not padding bytes, or gaps for non-contiguous functions)
    pub fn len(&self) -> usize {
        self.size
    }

    /// Returns a reference to this functions control flow graph
    pub fn cfg(&self) -> &ControlFlowGraph {
        &self.cflow_graph
    }

    /// Adds `alias` to this functions known aliases
    pub fn add_alias(&mut self, alias: String) {
        self.aliases.push(alias)
    }

    /// Sets this function's plt stub entry at `plt_address`, as `name`. **Note** This will alter the function's kind from `Regular` to `Stub`, and will also change move its canonical name into aliases.
    pub fn set_plt(&mut self, name: &str, plt_address: u64) {
        let old_name = self.name.clone();
        self.aliases.push(old_name);
        self.name = format!("{}@plt", name);
        self.kind = FunctionKind::Stub { name: name.to_string(), plt_address };
    }

    /// Returns this functions FunctionKind
    pub fn kind(&self) -> &FunctionKind {
        &self.kind
    }

    /// Returns this functions known name aliases (names pointing to the same start address)
    pub fn aliases(&self) -> &[String] {
        self.aliases.as_slice()
    }

    /// Returns a mutable reference to this functions control flow graph; **WARNING** this can cause instability if the entry point is not correctly updated
    pub fn cfg_mut(&mut self) -> &mut ControlFlowGraph {
        &mut self.cflow_graph
    }

    /// Returns a reference to the entry point vertex in the cfg
    pub fn entry_point_ref(&self) -> ControlFlowRef {
        self.entry_point
    }

    /// Sets the functions entry point vertex in the cfg to `vx` (this is primarily for use in tests).
    ///
    /// **WARNING** Make sure the vertex descriptor actually is the entry point _and_ points to a _resolved_ basic block, otherwise subsequent operations on this function will be undefined.
    pub fn set_entry_point_ref(&mut self, vx: ControlFlowRef) {
        self.entry_point = vx;
    }

    /// Returns a reference to the BasicBlock entry point of this function.
    pub fn entry_point(&self) -> &BasicBlock {
        match self.cflow_graph.vertex_label(self.entry_point).unwrap() {
            &ControlFlowTarget::Resolved(ref bb) => bb,
            _ => panic!("Function {} has an unresolved entry point - this is a bug, dumping the cfg: {:?}", self.name, self.cflow_graph)
        }
    }

    /// Returns a mutable reference to the BasicBlock entry point of this function.
    pub fn entry_point_mut(&mut self) -> &mut BasicBlock {
        match self.cflow_graph.vertex_label_mut(self.entry_point).unwrap() {
            &mut ControlFlowTarget::Resolved(ref mut bb) => bb,
            _ => panic!("Function {} has an unresolved entry point - this is a bug!", self.name) // can't dump cfg here because borrowed mutable ;)
        }
    }

    /// Whether this function is a leaf function or not (no outgoing calls)
    pub fn is_leaf(&self) -> bool {
        for bb in self.basic_blocks() {
            for statement in bb.statements() {
                match statement {
                    &Statement { op: Operation::Call(_), .. } => return false,
                    _ => ()
                }
            }
        }
        true
    }

    fn index_cflow_graph(
        g: &ControlFlowGraph,
        entry: u64,
    ) -> (BTreeMap<u64, Vec<MnemonicOrError>>, HashMap<u64, Vec<(Rvalue, Guard)>>, HashMap<u64, Vec<(Rvalue, Guard)>>) {
        let mut mnemonics = BTreeMap::new();
        let mut by_source = HashMap::<u64, Vec<(Rvalue, Guard)>>::new();
        let mut by_destination = HashMap::<u64, Vec<(Rvalue, Guard)>>::new();

        by_destination.insert(entry, vec![(Rvalue::Undefined, Guard::always())]);

        for cft in g.vertex_labels() {
            match cft {
                &ControlFlowTarget::Resolved(ref bb) => {
                    let mut prev_mne = None;

                    for mne in &bb.mnemonics {
                        mnemonics.entry(mne.area.start).or_insert(Vec::new()).push(MnemonicOrError::Mnemonic(mne.clone()));

                        if let Some(prev) = prev_mne {
                            by_source.entry(prev).or_insert(Vec::new()).push((Rvalue::new_u64(mne.area.start), Guard::always()));
                            by_destination.entry(mne.area.start).or_insert(Vec::new()).push((Rvalue::new_u64(prev), Guard::always()));
                        }
                        prev_mne = Some(mne.area.start);
                    }
                }
                &ControlFlowTarget::Failed(ref pos, ref msg) => {
                    mnemonics.entry(*pos).or_insert(Vec::new()).push(MnemonicOrError::Error(*pos, msg.clone()));
                }
                &ControlFlowTarget::Unresolved(_) => {}
            }
        }

        for e in g.edges() {
            let gu = g.edge_label(e).unwrap().clone();
            let src = g.vertex_label(g.source(e));
            let tgt = g.vertex_label(g.target(e));

            match (src, tgt) {
                // Resolved -> Resolved
                (Some(&ControlFlowTarget::Resolved(ref src_bb)), Some(&ControlFlowTarget::Resolved(ref tgt_bb))) => {
                    let last = src_bb.mnemonics.last().map_or(src_bb.area.start, |mne| mne.area.start);
                    by_source.entry(last).or_insert(Vec::new()).push((Rvalue::new_u64(tgt_bb.area.start), gu.clone()));
                    by_destination.entry(tgt_bb.area.start).or_insert(Vec::new()).push((Rvalue::new_u64(last), gu));
                }
                // Resolved -> Unresolved(Constant)
                (Some(&ControlFlowTarget::Resolved(ref src_bb)), Some(&ControlFlowTarget::Unresolved(Rvalue::Constant { value: ref c, .. }))) => {
                    let last = src_bb.mnemonics.last().map_or(src_bb.area.start, |mne| mne.area.start);
                    by_source.entry(last).or_insert(Vec::new()).push((Rvalue::new_u64(*c), gu.clone()));
                    by_destination.entry(*c).or_insert(Vec::new()).push((Rvalue::new_u64(last), gu));
                }
                // Unresolved(Constant) -> Resolved
                (Some(&ControlFlowTarget::Unresolved(Rvalue::Constant { value: ref c, .. })), Some(&ControlFlowTarget::Resolved(ref tgt_bb))) => {
                    by_source.entry(*c).or_insert(Vec::new()).push((Rvalue::new_u64(tgt_bb.area.start), gu.clone()));
                    by_destination.entry(tgt_bb.area.start).or_insert(Vec::new()).push((Rvalue::new_u64(*c), gu));
                }
                // Resolved -> Unresolved
                (Some(&ControlFlowTarget::Resolved(ref src_bb)), Some(&ControlFlowTarget::Unresolved(ref r))) => {
                    by_source.entry(src_bb.area.start).or_insert(Vec::new()).push((r.clone(), gu));
                }
                // Unresolved -> Resolved
                (Some(&ControlFlowTarget::Unresolved(ref t)), Some(&ControlFlowTarget::Resolved(ref tgt_bb))) => {
                    by_destination.entry(tgt_bb.area.start).or_insert(Vec::new()).push((t.clone(), gu));
                }
                // Failed -> Resolved
                (Some(&ControlFlowTarget::Failed(ref src_pos, _)), Some(&ControlFlowTarget::Resolved(ref tgt_bb))) => {
                    by_source.entry(*src_pos).or_insert(Vec::new()).push((Rvalue::new_u64(tgt_bb.area.start), gu.clone()));
                    by_destination.entry(tgt_bb.area.start).or_insert(Vec::new()).push((Rvalue::new_u64(*src_pos), gu));
                }
                // Resolved -> Failed
                (Some(&ControlFlowTarget::Resolved(ref src_bb)), Some(&ControlFlowTarget::Failed(ref tgt_pos, _))) => {
                    let last = src_bb.mnemonics.last().map_or(src_bb.area.start, |mne| mne.area.start);
                    by_source.entry(last).or_insert(Vec::new()).push((Rvalue::new_u64(*tgt_pos), gu.clone()));
                    by_destination.entry(*tgt_pos).or_insert(Vec::new()).push((Rvalue::new_u64(last), gu));
                }
                // Failed -> Failed
                (Some(&ControlFlowTarget::Failed(ref src_pos, _)), Some(&ControlFlowTarget::Failed(ref tgt_pos, _))) => {
                    by_source.entry(*src_pos).or_insert(Vec::new()).push((Rvalue::new_u64(*tgt_pos), gu.clone()));
                    by_destination.entry(*tgt_pos).or_insert(Vec::new()).push((Rvalue::new_u64(*src_pos), gu));
                }
                // Failed -> Unresolved(Constant)
                (Some(&ControlFlowTarget::Failed(ref src_pos, _)), Some(&ControlFlowTarget::Unresolved(Rvalue::Constant { value: ref c, .. }))) => {
                    by_source.entry(*src_pos).or_insert(Vec::new()).push((Rvalue::new_u64(*c), gu.clone()));
                    by_destination.entry(*c).or_insert(Vec::new()).push((Rvalue::new_u64(*src_pos), gu));
                }
                // Unresolved(Constant) -> Failed
                (Some(&ControlFlowTarget::Unresolved(Rvalue::Constant { value: ref c, .. })), Some(&ControlFlowTarget::Failed(ref tgt_pos, _))) => {
                    by_source.entry(*c).or_insert(Vec::new()).push((Rvalue::new_u64(*tgt_pos), gu.clone()));
                    by_destination.entry(*tgt_pos).or_insert(Vec::new()).push((Rvalue::new_u64(*c), gu));
                }
                // Failed -> Unresolved
                (Some(&ControlFlowTarget::Failed(ref src_pos, _)), Some(&ControlFlowTarget::Unresolved(ref r))) => {
                    by_source.entry(*src_pos).or_insert(Vec::new()).push((r.clone(), gu));
                }
                // Unresolved -> Failed
                (Some(&ControlFlowTarget::Unresolved(ref t)), Some(&ControlFlowTarget::Failed(ref tgt_pos, _))) => {
                    by_destination.entry(*tgt_pos).or_insert(Vec::new()).push((t.clone(), gu));
                }
                // Unresolved -> Unresolved
                (Some(&ControlFlowTarget::Unresolved(_)), Some(&ControlFlowTarget::Unresolved(_))) => {}
                (None, _) | (_, None) => unreachable!(),
            }
        }

        (mnemonics, by_source, by_destination)
    }

    fn assemble_cflow_graph(
        mut mnemonics: BTreeMap<u64, Vec<MnemonicOrError>>,
        by_source: HashMap<u64, Vec<(Rvalue, Guard)>>,
        by_destination: HashMap<u64, Vec<(Rvalue, Guard)>>,
        start: u64,
    ) -> ControlFlowGraph {
        let mut ret = ControlFlowGraph::new();
        let mut bblock = Vec::<Mnemonic>::new();

        for (_, mnes) in mnemonics.iter_mut() {
            if !bblock.is_empty() && !mnes.is_empty() {
                if let Some(&MnemonicOrError::Mnemonic(ref mne)) = mnes.first() {
                    let last_mne = &bblock.last().unwrap().clone();

                    // if next mnemonics aren't adjacent
                    let mut new_bb = bblock.last().unwrap().area.end != mne.area.start;

                    // or any following jumps aren't to adjacent mnemonics
                    new_bb |= by_source
                        .get(&last_mne.area.start)
                        .unwrap_or(&Vec::new())
                        .iter()
                        .any(
                            |&(ref opt_dest, _)| if let &Rvalue::Constant { value, .. } = opt_dest {
                                value != mne.area.start
                            } else {
                                false
                            }
                        );

                    // or any jumps pointing to the next that aren't from here
                    new_bb |= by_destination
                        .get(&mne.area.start)
                        .unwrap_or(&Vec::new())
                        .iter()
                        .any(
                            |&(ref opt_src, _)| if let &Rvalue::Constant { value, .. } = opt_src {
                                value != last_mne.area.start
                            } else {
                                false
                            }
                        );

                    // or the entry point does not point here
                    new_bb |= mne.area.start == start;

                    if new_bb {
                        let bb = BasicBlock::from_vec(bblock.clone());

                        bblock.clear();
                        ret.add_vertex(ControlFlowTarget::Resolved(bb));
                    }
                }
            }

            for moe in mnes.drain(..) {
                match moe {
                    MnemonicOrError::Mnemonic(mne) => {
                        bblock.push(mne);
                    }
                    MnemonicOrError::Error(pos, msg) => {
                        ret.add_vertex(ControlFlowTarget::Failed(pos, msg));
                    }
                }
            }
        }

        // last basic block
        if !bblock.is_empty() {
            ret.add_vertex(ControlFlowTarget::Resolved(BasicBlock::from_vec(bblock)));
        }

        // connect basic blocks
        for (src_off, tgts) in by_source.iter() {
            for &(ref tgt, ref gu) in tgts {

                let from_bb = ret.vertices()
                    .find(
                        |&t| match ret.vertex_label(t) {
                            Some(&ControlFlowTarget::Resolved(ref bb)) => bb.mnemonics.last().map_or(false, |x| x.area.start == *src_off),
                            Some(&ControlFlowTarget::Unresolved(Rvalue::Constant { value: v, .. })) => v == *src_off,
                            Some(&ControlFlowTarget::Unresolved(_)) => false,
                            Some(&ControlFlowTarget::Failed(pos, _)) => pos == *src_off,
                            None => unreachable!(),
                        }
                    );
                let to_bb = ret.vertices()
                    .find(
                        |&t| match (tgt, ret.vertex_label(t)) {
                            (&Rvalue::Constant { value, .. }, Some(&ControlFlowTarget::Resolved(ref bb))) => bb.area.start == value,
                            (&Rvalue::Constant { value, .. }, Some(&ControlFlowTarget::Failed(pos, _))) => pos == value,
                            (rv, Some(&ControlFlowTarget::Unresolved(ref v))) => *v == *rv,
                            (_, None) => unreachable!(),
                            _ => false,
                        }
                    );

                match (from_bb, to_bb) {
                    (Some(from), Some(to)) => {
                        ret.add_edge(gu.clone(), from, to);
                    }
                    (None, Some(to)) => {
                        if let Some(&ControlFlowTarget::Resolved(ref bb)) = ret.vertex_label(to) {
                            if bb.area.start <= *src_off && bb.area.end > *src_off {
                                continue;
                            }
                        }

                        let vx = ret.add_vertex(ControlFlowTarget::Unresolved(Rvalue::new_u64(*src_off)));
                        ret.add_edge(gu.clone(), vx, to);
                    }
                    (Some(from), None) => {
                        if let Some(&ControlFlowTarget::Resolved(ref bb)) = ret.vertex_label(from) {
                            if let &Rvalue::Constant { value, .. } = tgt {
                                if bb.area.start <= value && bb.area.end > value {
                                    continue;
                                }
                            }
                        }

                        let vx = ret.add_vertex(ControlFlowTarget::Unresolved(tgt.clone()));
                        ret.add_edge(gu.clone(), from, vx);
                    }
                    _ => {
                        trace!(
                            "jump from 0x{:x} to {} doesn't hit any blocks",
                            src_off,
                            tgt
                        )
                    }
                }
            }
        }

        ret
    }

    /// Returns an iterator over this functions `BasicBlock`s
    pub fn basic_blocks(&self) -> BasicBlockIterator {
        BasicBlockIterator::new(&self.cflow_graph)
    }

    /// Returns the address of every function this function calls
    pub fn collect_call_addresses(&self) -> Vec<u64> {
        let mut ret = Vec::new();
        for bb in self.basic_blocks() {
            for statement in bb.statements() {
                match statement {
                    &Statement { op: Operation::Call(Rvalue::Constant{ value, .. }), .. } => ret.push(value),
                    _ => ()
                }
            }
        }
        debug!("collected calls: {:?}", ret);
        ret
    }

    /// Returns all call targets.
    pub fn collect_calls(&self) -> Vec<Rvalue> {
        let mut ret = Vec::new();
        for bb in self.basic_blocks() {
            for statement in bb.statements() {
                match statement {
                    &Statement { op: Operation::Call(ref t), .. } => ret.push(t.clone()),
                    _ => ()
                }
            }
        }
        debug!("collected calls: {:?}", ret);
        ret
    }

    /// Returns the basic block that begins at `a`.
    pub fn find_basic_block_by_start(&self, a: u64) -> Option<ControlFlowRef> {
        self.cflow_graph
            .vertices()
            .find(
                |&x| match self.cflow_graph.vertex_label(x) {
                    Some(&ControlFlowTarget::Resolved(ref bb)) => bb.area.start == a && bb.area.end > a,
                    _ => false,
                }
            )
    }

    /// Returns the basic block that contains `a`.
    pub fn find_basic_block_at(&self, a: u64) -> Option<&BasicBlock> {
        self.basic_blocks().find(|&bb| bb.area.start <= a && bb.area.end > a)
    }

    /// Returns all nodes in the graph of this function in post order.
    pub fn postorder(&self) -> Vec<ControlFlowRef> {
        TreeIterator::new(
            self.entry_point,
            TraversalOrder::Postorder,
            &self.cflow_graph,
        )
                .collect()
    }

    /// Return a boxed iterator over every statement in this function
    pub fn statements<'b>(&'b self) -> Box<Iterator<Item=&'b Statement> + 'b> {
        Box::new(self.basic_blocks().map(|bb| bb.statements()).flat_map(|ss| ss))
    }

    /// Returns the functions basic block graph in graphivz's DOT format. Useful for debugging.
    pub fn to_dot(&self) -> String {
        let mut ret = "digraph G {".to_string();

        for v in self.cflow_graph.vertices() {
            match self.cflow_graph.vertex_label(v) {
                Some(&ControlFlowTarget::Resolved(ref bb)) => {
                    ret = format!(
                        "{}\n{} [label=<<table border=\"0\"><tr><td>{}:{}</td></tr>",
                        ret,
                        v.0,
                        bb.area.start,
                        bb.area.end
                    );

                    for mne in bb.mnemonics.iter() {
                        ret = format!("{}<tr><td align=\"left\">{}</td></tr>", ret, mne.opcode);
                        for i in mne.instructions.iter() {
                            ret = format!(
                                "{}<tr><td align=\"left\">&nbsp;&nbsp;&nbsp;&nbsp;{}</td></tr>",
                                ret,
                                i
                            );
                        }
                    }

                    ret = format!("{}</table>>,shape=record];", ret);
                }
                Some(&ControlFlowTarget::Unresolved(ref c)) => {
                    ret = format!("{}\n{} [label=\"{:?}\",shape=circle];", ret, v.0, c);
                }
                _ => {
                    ret = format!("{}\n{} [label=\"?\",shape=circle];", ret, v.0);
                }
            }
        }

        for e in self.cflow_graph.edges() {
            ret = format!(
                "{}\n{} -> {} [label=\"{}\"];",
                ret,
                self.cflow_graph.source(e).0,
                self.cflow_graph.target(e).0,
                self.cflow_graph.edge_label(e).unwrap()
            );
        }

        format!("{}\n}}", ret)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use {Architecture, BasicBlock, Bound, Disassembler, Guard, Match, Mnemonic, OpaqueLayer, Region, Result, Rvalue, State};
    use panopticon_graph_algos::{AdjacencyMatrixGraphTrait, EdgeListGraphTrait, VertexListGraphTrait};
    use panopticon_graph_algos::{GraphTrait, MutableGraphTrait};
    use std::borrow::Cow;
    use std::sync::Arc;

    #[derive(Clone,Debug)]
    enum TestArchShort {}
    impl Architecture for TestArchShort {
        type Token = u8;
        type Configuration = Arc<Disassembler<TestArchShort>>;

        fn prepare(_: &Region, _: &Self::Configuration) -> Result<Vec<(&'static str, u64, &'static str)>> {
            unimplemented!()
        }

        fn decode(reg: &Region, addr: u64, cfg: &Self::Configuration) -> Result<Match<Self>> {
            if let Some(s) = cfg.next_match(&mut reg.iter().seek(addr), addr, cfg.clone()) {
                Ok(s.into())
            } else {
                Err("No match".into())
            }
        }
    }

    #[derive(Clone,Debug)]
    enum TestArchWide {}
    impl Architecture for TestArchWide {
        type Token = u16;
        type Configuration = Arc<Disassembler<TestArchWide>>;

        fn prepare(_: &Region, _: &Self::Configuration) -> Result<Vec<(&'static str, u64, &'static str)>> {
            unimplemented!()
        }

        fn decode(reg: &Region, addr: u64, cfg: &Self::Configuration) -> Result<Match<Self>> {
            if let Some(s) = cfg.next_match(&mut reg.iter().seek(addr), addr, cfg.clone()) {
                Ok(s.into())
            } else {
                Err("No match".into())
            }
        }
    }

    #[test]
    fn new() {
        let f = Function::undefined(100, None, &Region::undefined("ram".to_owned(), 100), Some("test".to_owned()));

        assert_eq!(f.name, "test".to_string());
        assert_eq!(f.cflow_graph.num_vertices(), 1);
        assert_eq!(f.cflow_graph.num_edges(), 0);
        match f.cflow_graph.vertex_label(f.entry_point_ref()).unwrap() {
            &ControlFlowTarget::Unresolved(Rvalue::Constant{ value, size }) => {
                assert_eq!(value, 100);
                assert_eq!(size, 64);
            },
            _ => assert!(false)
        }
    }

    #[test]
    fn index_resolved() {
        let mut cfg = ControlFlowGraph::new();

        let bb0 = BasicBlock::from_vec(
            vec![
                Mnemonic::dummy(0..1),
                Mnemonic::dummy(1..2),
                Mnemonic::dummy(2..5),
                Mnemonic::dummy(5..6),
            ]
        );
        let bb1 = BasicBlock::from_vec(
            vec![
                Mnemonic::dummy(10..11),
                Mnemonic::dummy(11..12),
                Mnemonic::dummy(12..15),
                Mnemonic::dummy(15..16),
            ]
        );
        let bb2 = BasicBlock::from_vec(vec![Mnemonic::dummy(6..10)]);

        let vx0 = cfg.add_vertex(ControlFlowTarget::Resolved(bb0));
        let vx1 = cfg.add_vertex(ControlFlowTarget::Resolved(bb1));
        let vx2 = cfg.add_vertex(ControlFlowTarget::Resolved(bb2));

        cfg.add_edge(Guard::always(), vx0, vx1);
        cfg.add_edge(Guard::always(), vx1, vx1);
        cfg.add_edge(Guard::always(), vx1, vx2);
        cfg.add_edge(Guard::always(), vx2, vx0);

        let (mnes, src, dest) = Function::index_cflow_graph(&cfg, 0);

        assert_eq!(mnes.len(), 9);
        assert_eq!(src.values().fold(0, |acc, x| acc + x.len()), 10);
        assert_eq!(dest.values().fold(0, |acc, x| acc + x.len()), 11); // because index_cflow_graph adds the start/entry value

        let cfg_re = Function::assemble_cflow_graph(mnes, src, dest, 0);

        assert_eq!(cfg_re.num_vertices(), 3);
        assert_eq!(cfg_re.num_edges(), 4);

        for vx in cfg_re.vertices() {
            if let Some(&ControlFlowTarget::Resolved(ref bb)) = cfg_re.vertex_label(vx) {
                assert!((bb.area.start == 0 && bb.area.end == 6) || (bb.area.start == 10 && bb.area.end == 16) || (bb.area.start == 6 && bb.area.end == 10));
            } else {
                unreachable!();
            }
        }

        for e in cfg_re.edges() {
            if let Some(&ControlFlowTarget::Resolved(ref from)) = cfg_re.vertex_label(cfg_re.source(e)) {
                if let Some(&ControlFlowTarget::Resolved(ref to)) = cfg_re.vertex_label(cfg_re.target(e)) {
                    assert!(
                        (from.area.start == 0 && to.area.start == 10) || (from.area.start == 10 && to.area.start == 10) ||
                        (from.area.start == 10 && to.area.start == 6) || (from.area.start == 6 && to.area.start == 0)
                    );
                } else {
                    unreachable!();
                }
            } else {
                unreachable!();
            }
        }
    }

    #[test]
    fn index_unresolved() {
        let mut cfg = ControlFlowGraph::new();

        let bb0 = BasicBlock::from_vec(vec![Mnemonic::dummy(0..1)]);
        let bb1 = BasicBlock::from_vec(vec![Mnemonic::dummy(10..11)]);

        let vx0 = cfg.add_vertex(ControlFlowTarget::Resolved(bb0));
        let vx1 = cfg.add_vertex(ControlFlowTarget::Resolved(bb1));
        let vx2 = cfg.add_vertex(ControlFlowTarget::Unresolved(Rvalue::new_u32(42)));
        let vx3 = cfg.add_vertex(ControlFlowTarget::Unresolved(Rvalue::new_u32(23)));
        let vx4 = cfg.add_vertex(
            ControlFlowTarget::Unresolved(
                Rvalue::Variable {
                    name: Cow::Borrowed("a"),
                    size: 8,
                    offset: 0,
                    subscript: None,
                }
            )
        );

        cfg.add_edge(Guard::always(), vx0, vx1);
        cfg.add_edge(Guard::always(), vx2, vx1);
        cfg.add_edge(Guard::always(), vx3, vx0);
        cfg.add_edge(Guard::always(), vx4, vx3);

        let (mnes, src, dest) = Function::index_cflow_graph(&cfg, 0);

        assert_eq!(mnes.len(), 2);
        assert_eq!(src.values().fold(0, |acc, x| acc + x.len()), 3);
        assert_eq!(dest.values().fold(0, |acc, x| acc + x.len()), 4); // because index_cflow_graph automatically adds the functions start entry

        let cfg_re = Function::assemble_cflow_graph(mnes, src, dest, 0);

        assert_eq!(cfg_re.num_vertices(), 4);
        assert_eq!(cfg_re.num_edges(), 3);

        for vx in cfg_re.vertices() {
            match cfg_re.vertex_label(vx) {
                Some(&ControlFlowTarget::Resolved(ref bb)) => {
                    assert!((bb.area.start == 0 && bb.area.end == 1) || (bb.area.start == 10 && bb.area.end == 11));
                }
                Some(&ControlFlowTarget::Unresolved(Rvalue::Constant { value: ref c, size: 64 })) => {
                    assert!(*c == 42 || *c == 23);
                }
                _ => {
                    unreachable!();
                }
            }
        }
    }

    #[test]
    fn add_single() {
        let main = new_disassembler!(TestArchShort =>
            [ 0 ] = |st: &mut State<TestArchShort>| {
                st.mnemonic(1,"A","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                true
            }
        );
        let data = OpaqueLayer::wrap(vec![0]);
        let reg = Region::new("".to_string(), data);
        let func = Function::new::<TestArchShort>(0, &reg, None, main).unwrap();

        assert_eq!(func.cflow_graph.num_vertices(), 1);
        assert_eq!(func.cflow_graph.num_edges(), 0);

        if let Some(vx) = func.cflow_graph.vertices().next() {
            if let Some(&ControlFlowTarget::Resolved(ref bb)) = func.cflow_graph.vertex_label(vx) {
                assert_eq!(bb.mnemonics.len(), 1);
                assert_eq!(bb.mnemonics[0].opcode, "A".to_string());
                assert_eq!(bb.mnemonics[0].area, Bound::new(0, 1));
                assert_eq!(bb.area, Bound::new(0, 1));
            } else {
                unreachable!();
            }
        } else {
            unreachable!();
        }

        assert_eq!(func.entry_point_ref(), func.cflow_graph.vertices().next().unwrap());
        assert_eq!(func.name, "func_0x0".to_string());
    }

    #[test]
    fn continuous() {
        let main = new_disassembler!(TestArchShort =>
            [ 0 ] = |st: &mut State<TestArchShort>| {
                let next = st.address;
                st.mnemonic(1,"test0","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                st.jump(Rvalue::new_u64(next + 1),Guard::always()).unwrap();
                true
            },
            [ 1 ] = |st: &mut State<TestArchShort>| {
                let next = st.address;
                st.mnemonic(1,"test1","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                st.jump(Rvalue::new_u64(next + 1),Guard::always()).unwrap();
                true
            },
            [ 2 ] = |st: &mut State<TestArchShort>| {
                let next = st.address;
                st.mnemonic(1,"test2","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                st.jump(Rvalue::new_u64(next + 1),Guard::always()).unwrap();
                true
            },
            [ 3 ] = |st: &mut State<TestArchShort>| {
                let next = st.address;
                st.mnemonic(1,"test3","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                st.jump(Rvalue::new_u64(next + 1),Guard::always()).unwrap();
                true
            },
            [ 4 ] = |st: &mut State<TestArchShort>| {
                let next = st.address;
                st.mnemonic(1,"test4","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                st.jump(Rvalue::new_u64(next + 1),Guard::always()).unwrap();
                true
            },
            [ 5 ] = |st: &mut State<TestArchShort>| {
                let next = st.address;
                st.mnemonic(1,"test5","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                st.jump(Rvalue::new_u64(next + 1),Guard::always()).unwrap();
                true
            }
        );

        let data = OpaqueLayer::wrap(vec![0, 1, 2, 3, 4, 5]);
        let reg = Region::new("".to_string(), data);
        let func = Function::new::<TestArchShort>(0, &reg, None, main).unwrap();

        assert_eq!(func.cflow_graph.num_vertices(), 2);
        assert_eq!(func.cflow_graph.num_edges(), 1);

        let mut bb_vx = None;
        let mut ures_vx = None;

        for vx in func.cflow_graph.vertices() {
            if let Some(&ControlFlowTarget::Resolved(ref bb)) = func.cflow_graph.vertex_label(vx) {
                assert_eq!(bb.mnemonics.len(), 6);
                assert_eq!(bb.mnemonics[0].opcode, "test0".to_string());
                assert_eq!(bb.mnemonics[0].area, Bound::new(0, 1));
                assert_eq!(bb.mnemonics[1].opcode, "test1".to_string());
                assert_eq!(bb.mnemonics[1].area, Bound::new(1, 2));
                assert_eq!(bb.mnemonics[2].opcode, "test2".to_string());
                assert_eq!(bb.mnemonics[2].area, Bound::new(2, 3));
                assert_eq!(bb.mnemonics[3].opcode, "test3".to_string());
                assert_eq!(bb.mnemonics[3].area, Bound::new(3, 4));
                assert_eq!(bb.mnemonics[4].opcode, "test4".to_string());
                assert_eq!(bb.mnemonics[4].area, Bound::new(4, 5));
                assert_eq!(bb.mnemonics[5].opcode, "test5".to_string());
                assert_eq!(bb.mnemonics[5].area, Bound::new(5, 6));
                assert_eq!(bb.area, Bound::new(0, 6));
                bb_vx = Some(vx);
            } else if let Some(&ControlFlowTarget::Failed(c, _)) = func.cflow_graph.vertex_label(vx) {
                assert_eq!(c, 6);
                ures_vx = Some(vx);
            } else {
                unreachable!();
            }
        }

        assert!(ures_vx.is_some() && bb_vx.is_some());
        assert_eq!(Some(func.entry_point_ref()), bb_vx);
        assert_eq!(func.name, "func_0x0".to_string());
        assert!(func.cflow_graph.edge(bb_vx.unwrap(), ures_vx.unwrap()).is_some());
    }

    #[test]
    fn branch() {
        let main = new_disassembler!(TestArchShort =>
            [ 0 ] = |st: &mut State<TestArchShort>| {
                st.mnemonic(1,"test0","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                st.jump(Rvalue::new_u32(1),Guard::always()).unwrap();
                st.jump(Rvalue::new_u32(2),Guard::always()).unwrap();
                true
            },
            [ 1 ] = |st: &mut State<TestArchShort>| {
                st.mnemonic(1,"test1","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                st.jump(Rvalue::new_u32(3),Guard::always()).unwrap();
                true
            },
            [ 2 ] = |st: &mut State<TestArchShort>| {
                st.mnemonic(1,"test2","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                st.jump(Rvalue::new_u32(1),Guard::always()).unwrap();
                true
            }
        );

        let data = OpaqueLayer::wrap(vec![0, 1, 2]);
        let reg = Region::new("".to_string(), data);
        let func = Function::new::<TestArchShort>(0, &reg, None, main).unwrap();

        assert_eq!(func.cflow_graph.num_vertices(), 4);
        assert_eq!(func.cflow_graph.num_edges(), 4);

        let mut bb0_vx = None;
        let mut bb1_vx = None;
        let mut bb2_vx = None;
        let mut ures_vx = None;

        for vx in func.cflow_graph.vertices() {
            if let Some(&ControlFlowTarget::Resolved(ref bb)) = func.cflow_graph.vertex_label(vx) {
                if bb.area.start == 0 {
                    assert_eq!(bb.mnemonics.len(), 1);
                    assert_eq!(bb.mnemonics[0].opcode, "test0".to_string());
                    assert_eq!(bb.mnemonics[0].area, Bound::new(0, 1));
                    assert_eq!(bb.area, Bound::new(0, 1));
                    bb0_vx = Some(vx);
                } else if bb.area.start == 1 {
                    assert_eq!(bb.mnemonics.len(), 1);
                    assert_eq!(bb.mnemonics[0].opcode, "test1".to_string());
                    assert_eq!(bb.mnemonics[0].area, Bound::new(1, 2));
                    assert_eq!(bb.area, Bound::new(1, 2));
                    bb1_vx = Some(vx);
                } else if bb.area.start == 2 {
                    assert_eq!(bb.mnemonics.len(), 1);
                    assert_eq!(bb.mnemonics[0].opcode, "test2".to_string());
                    assert_eq!(bb.mnemonics[0].area, Bound::new(2, 3));
                    assert_eq!(bb.area, Bound::new(2, 3));
                    bb2_vx = Some(vx);
                } else {
                    unreachable!();
                }
            } else if let Some(&ControlFlowTarget::Failed(c, _)) = func.cflow_graph.vertex_label(vx) {
                assert_eq!(c, 3);
                ures_vx = Some(vx);
            } else {
                unreachable!();
            }
        }

        assert!(ures_vx.is_some() && bb0_vx.is_some() && bb1_vx.is_some() && bb2_vx.is_some());
        assert_eq!(Some(func.entry_point_ref()), bb0_vx);
        assert_eq!(func.name, "func_0x0".to_string());
        assert!(func.cflow_graph.edge(bb0_vx.unwrap(), bb1_vx.unwrap()).is_some());
        assert!(func.cflow_graph.edge(bb0_vx.unwrap(), bb2_vx.unwrap()).is_some());
        assert!(func.cflow_graph.edge(bb1_vx.unwrap(), ures_vx.unwrap()).is_some());
        assert!(func.cflow_graph.edge(bb2_vx.unwrap(), bb1_vx.unwrap()).is_some());
    }

    #[test]
    fn function_loop() {
        let main = new_disassembler!(TestArchShort =>
            [ 0 ] = |st: &mut State<TestArchShort>| {
                st.mnemonic(1,"test0","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                st.jump(Rvalue::new_u32(1),Guard::always()).unwrap();
                true
            },
            [ 1 ] = |st: &mut State<TestArchShort>| {
                st.mnemonic(1,"test1","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                st.jump(Rvalue::new_u32(2),Guard::always()).unwrap();
                true
            },
            [ 2 ] = |st: &mut State<TestArchShort>| {
                st.mnemonic(1,"test2","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                st.jump(Rvalue::new_u32(0),Guard::always()).unwrap();
                true
            }
        );

        let data = OpaqueLayer::wrap(vec![0, 1, 2]);
        let reg = Region::new("".to_string(), data);
        let func = Function::new::<TestArchShort>(0, &reg, None, main).unwrap();

        assert_eq!(func.cflow_graph.num_vertices(), 1);
        assert_eq!(func.cflow_graph.num_edges(), 1);

        let vx = func.cflow_graph.vertices().next().unwrap();
        if let Some(&ControlFlowTarget::Resolved(ref bb)) = func.cflow_graph.vertex_label(vx) {
            if bb.area.start == 0 {
                assert_eq!(bb.mnemonics.len(), 3);
                assert_eq!(bb.mnemonics[0].opcode, "test0".to_string());
                assert_eq!(bb.mnemonics[0].area, Bound::new(0, 1));
                assert_eq!(bb.mnemonics[1].opcode, "test1".to_string());
                assert_eq!(bb.mnemonics[1].area, Bound::new(1, 2));
                assert_eq!(bb.mnemonics[2].opcode, "test2".to_string());
                assert_eq!(bb.mnemonics[2].area, Bound::new(2, 3));
                assert_eq!(bb.area, Bound::new(0, 3));
            } else {
                unreachable!();
            }
        }

        assert_eq!(func.name, "func_0x0".to_string());
        assert_eq!(Some(func.entry_point_ref()), Some(vx));
        assert!(func.cflow_graph.edge(vx, vx).is_some());
    }

    #[test]
    fn empty() {
        let main = new_disassembler!(TestArchShort =>
            [ 0 ] = |st: &mut State<TestArchShort>| {
                st.mnemonic(1,"test0","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                st.jump(Rvalue::new_u32(1),Guard::always()).unwrap();
                true
            },
            [ 1 ] = |st: &mut State<TestArchShort>| {
                st.mnemonic(1,"test1","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                st.jump(Rvalue::new_u32(2),Guard::always()).unwrap();
                true
            },
            [ 2 ] = |st: &mut State<TestArchShort>| {
                st.mnemonic(1,"test2","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                st.jump(Rvalue::new_u32(0),Guard::always()).unwrap();
                true
            }
        );

        let data = OpaqueLayer::wrap(vec![]);
        let reg = Region::new("".to_string(), data);
        let func = Function::new::<TestArchShort>(0, &reg, None, main);
        assert!(func.is_err());
        // these tests have been rendered somewhat moot now since the entry point must be present
        // assert_eq!(func.cflow_graph.num_vertices(), 1);
        // assert_eq!(func.cflow_graph.num_edges(), 0);
        // assert_eq!(func.name, "func_0x0".to_string());

        // let vx = func.cflow_graph.vertices().next().unwrap();
        // if let Some(&ControlFlowTarget::Failed(v, _)) = func.cflow_graph.vertex_label(vx) {
        //     assert_eq!(v, 0);
        // }
    }

    #[test]
    fn entry_split() {
        let main = new_disassembler!(TestArchShort =>
            [ 0 ] = |st: &mut State<TestArchShort>| {
                st.mnemonic(1,"test0","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                st.jump(Rvalue::new_u32(1),Guard::always()).unwrap();
                true
            },
            [ 1 ] = |st: &mut State<TestArchShort>| {
                st.mnemonic(1,"test1","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                st.jump(Rvalue::new_u32(2),Guard::always()).unwrap();
                true
            },
            [ 2 ] = |st: &mut State<TestArchShort>| {
                st.mnemonic(1,"test2","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                st.jump(Rvalue::new_u32(1),Guard::always()).unwrap();
                true
            }
        );

        let data = OpaqueLayer::wrap(vec![0, 1, 2]);
        let reg = Region::new("".to_string(), data);
        let bb = BasicBlock::from_vec(vec![Mnemonic::dummy(0..1), Mnemonic::dummy(1..2)]);
        let mut func = Function::undefined(0, None, &reg, Some("test".to_owned()));
        let vx0 = func.cflow_graph.add_vertex(ControlFlowTarget::Resolved(bb));
        let vx1 = func.cflow_graph.add_vertex(ControlFlowTarget::Unresolved(Rvalue::new_u32(2)));

        func.set_entry_point_ref(vx0);
        func.cflow_graph.add_edge(Guard::always(), vx0, vx1);

        func.cont::<TestArchShort>(0, &reg, main).unwrap();
        assert_eq!(func.cflow_graph.num_vertices(), 2);
        assert_eq!(func.cflow_graph.num_edges(), 2);
        assert_eq!(func.name, "test".to_string());

        let mut bb0_vx = None;
        let mut bb1_vx = None;

        for vx in func.cflow_graph.vertices() {
            if let Some(&ControlFlowTarget::Resolved(ref bb)) = func.cflow_graph.vertex_label(vx) {
                if bb.area.start == 0 {
                    assert_eq!(bb.mnemonics.len(), 1);
                    assert_eq!(bb.mnemonics[0].opcode, "dummy".to_string());
                    assert_eq!(bb.mnemonics[0].area, Bound::new(0, 1));
                    assert_eq!(bb.area, Bound::new(0, 1));
                    bb0_vx = Some(vx);
                } else if bb.area.start == 1 {
                    assert_eq!(bb.mnemonics.len(), 2);
                    assert_eq!(bb.mnemonics[0].opcode, "dummy".to_string());
                    assert_eq!(bb.mnemonics[0].area, Bound::new(1, 2));
                    assert_eq!(bb.mnemonics[1].opcode, "test2".to_string());
                    assert_eq!(bb.mnemonics[1].area, Bound::new(2, 3));
                    assert_eq!(bb.area, Bound::new(1, 3));
                    bb1_vx = Some(vx);
                } else {
                    unreachable!();
                }
            } else {
                unreachable!();
            }
        }

        assert!(bb0_vx.is_some() && bb1_vx.is_some());
        assert_eq!(Some(func.entry_point_ref()), bb0_vx);
        assert!(func.cflow_graph.edge(bb0_vx.unwrap(), bb1_vx.unwrap()).is_some());
        assert!(func.cflow_graph.edge(bb1_vx.unwrap(), bb1_vx.unwrap()).is_some());
    }

    #[test]
    fn wide_token() {
        let def = OpaqueLayer::wrap(vec![0x11, 0x22, 0x33, 0x44, 0x55, 0x44]);
        let reg = Region::new("".to_string(), def);
        let dec = new_disassembler!(TestArchWide =>
            [0x2211] = |s: &mut State<TestArchWide>|
            {
                let a = s.address;
                s.mnemonic(2,"A","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                s.jump(Rvalue::new_u64(a + 2),Guard::always()).unwrap();
                true
            },

            [0x4433] = |s: &mut State<TestArchWide>|
            {
                let a = s.address;
                s.mnemonic(2,"B","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                s.jump(Rvalue::new_u64(a + 2),Guard::always()).unwrap();
                s.jump(Rvalue::new_u64(a + 4),Guard::always()).unwrap();
                true
            },

            [0x4455] = |s: &mut State<TestArchWide>|
            {
                s.mnemonic(2, "C","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                true
            }
        );

        let func = Function::new::<TestArchWide>(0, &reg, None, dec).unwrap();
        assert_eq!(func.cflow_graph.num_vertices(), 3);
        assert_eq!(func.cflow_graph.num_edges(), 2);

        let mut bb0_vx = None;
        let mut bb1_vx = None;

        for vx in func.cflow_graph.vertices() {
            match func.cflow_graph.vertex_label(vx) {
                Some(&ControlFlowTarget::Resolved(ref bb)) => {
                    if bb.area.start == 0 {
                        assert_eq!(bb.mnemonics.len(), 2);
                        assert_eq!(bb.area, Bound::new(0, 4));
                        bb0_vx = Some(vx);
                    } else if bb.area.start == 4 {
                        assert_eq!(bb.mnemonics.len(), 1);
                        assert_eq!(bb.area, Bound::new(4, 6));
                        bb1_vx = Some(vx);
                    } else {
                        unreachable!();
                    }
                }
                Some(&ControlFlowTarget::Failed(6, _)) => {}
                _ => unreachable!(),
            }
        }

        assert!(bb0_vx.is_some() && bb1_vx.is_some());
        assert_eq!(Some(func.entry_point_ref()), bb0_vx);
    }

    #[test]
    fn issue_51_treat_entry_point_as_incoming_edge() {
        let main = new_disassembler!(TestArchShort =>
            [ 0 ] = |st: &mut State<TestArchShort>| {
                st.mnemonic(1,"test0","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                st.jump(Rvalue::new_u32(1),Guard::always()).unwrap();
                true
            },
            [ 1 ] = |st: &mut State<TestArchShort>| {
                st.mnemonic(1,"test1","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                st.jump(Rvalue::new_u32(2),Guard::always()).unwrap();
                true
            },
            [ 2 ] = |st: &mut State<TestArchShort>| {
                st.mnemonic(1,"test2","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                st.jump(Rvalue::new_u32(0),Guard::always()).unwrap();
                true
            }
        );

        let data = OpaqueLayer::wrap(vec![0, 1, 2]);
        let reg = Region::new("".to_string(), data);
        let func = Function::new::<TestArchShort>(1, &reg, None, main).unwrap();
        assert_eq!(func.cflow_graph.num_vertices(), 2);
        assert_eq!(func.cflow_graph.num_edges(), 2);

        let mut bb0_vx = None;
        let mut bb1_vx = None;

        for vx in func.cflow_graph.vertices() {
            if let Some(&ControlFlowTarget::Resolved(ref bb)) = func.cflow_graph.vertex_label(vx) {
                if bb.area.start == 0 {
                    assert_eq!(bb.mnemonics.len(), 1);
                    assert_eq!(bb.area, Bound::new(0, 1));
                    bb0_vx = Some(vx);
                } else if bb.area.start == 1 {
                    assert_eq!(bb.mnemonics.len(), 2);
                    assert_eq!(bb.area, Bound::new(1, 3));
                    bb1_vx = Some(vx);
                } else {
                    unreachable!();
                }
            } else {
                unreachable!();
            }
        }

        assert!(bb0_vx.is_some() && bb1_vx.is_some());
        assert_eq!(Some(func.entry_point_ref()), bb1_vx);
        assert!(func.cflow_graph.edge(bb0_vx.unwrap(), bb1_vx.unwrap()).is_some());
        assert!(func.cflow_graph.edge(bb1_vx.unwrap(), bb0_vx.unwrap()).is_some());
    }

    #[test]
    fn issue_232_overlap_with_entry_point() {
        let main = new_disassembler!(TestArchShort =>
            [ 0, 1 ] = |st: &mut State<TestArchShort>| {
                st.mnemonic(2,"test01","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                st.jump(Rvalue::new_u32(2),Guard::always()).unwrap();
                true
            },
            [ 1 ] = |st: &mut State<TestArchShort>| {
                st.mnemonic(1,"test1","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                st.jump(Rvalue::new_u32(2),Guard::always()).unwrap();
                true
            },
            [ 2 ] = |st: &mut State<TestArchShort>| {
                st.mnemonic(1,"test2","",vec!(),&|_| { Ok(vec![]) }).unwrap();
                st.jump(Rvalue::new_u32(0),Guard::always()).unwrap();
                true
            }
        );

        let data = OpaqueLayer::wrap(vec![0, 1, 2]);
        let reg = Region::new("".to_string(), data);
        let func = Function::new::<TestArchShort>(1, &reg, None, main).unwrap();
        assert_eq!(func.cflow_graph.num_vertices(), 3);
        assert_eq!(func.cflow_graph.num_edges(), 3);

        let mut bb01_vx = None;
        let mut bb1_vx = None;
        let mut bb2_vx = None;

        for vx in func.cflow_graph.vertices() {
            if let Some(&ControlFlowTarget::Resolved(ref bb)) = func.cflow_graph.vertex_label(vx) {
                if bb.area.start == 0 {
                    assert_eq!(bb.mnemonics.len(), 1);
                    assert_eq!(bb.area, Bound::new(0, 2));
                    bb01_vx = Some(vx);
                } else if bb.area.start == 1 {
                    assert_eq!(bb.mnemonics.len(), 1);
                    assert_eq!(bb.area, Bound::new(1, 2));
                    bb1_vx = Some(vx);
                } else if bb.area.start == 2 {
                    assert_eq!(bb.mnemonics.len(), 1);
                    assert_eq!(bb.area, Bound::new(2, 3));
                    bb2_vx = Some(vx);
                } else {
                    unreachable!();
                }
            } else {
                unreachable!();
            }
        }

        assert!(bb01_vx.is_some());
        assert!(bb1_vx.is_some());
        assert!(bb2_vx.is_some());
        assert_eq!(Some(func.entry_point_ref()), bb1_vx);
        assert!(func.cflow_graph.edge(bb01_vx.unwrap(), bb2_vx.unwrap()).is_some());
        assert!(func.cflow_graph.edge(bb1_vx.unwrap(), bb2_vx.unwrap()).is_some());
        assert!(func.cflow_graph.edge(bb2_vx.unwrap(), bb01_vx.unwrap()).is_some());
    }
}