1 : // Copyright 2013 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/block_graph/filterable.h"
16 :
17 : #include "gtest/gtest.h"
18 : #include "syzygy/block_graph/basic_block_subgraph.h"
19 :
20 : namespace block_graph {
21 :
22 : namespace {
23 :
24 : typedef core::RelativeAddress RelativeAddress;
25 : typedef RelativeAddressFilter::Range Range;
26 :
27 : } // namespace
28 :
29 E : TEST(FilterableTest, DefaultConstructor) {
30 E : Filterable f;
31 E : EXPECT_TRUE(f.filter() == NULL);
32 E : }
33 :
34 E : TEST(FilterableTest, ConstructorWithFilter) {
35 E : RelativeAddressFilter raf;
36 E : Filterable f(&raf);
37 E : EXPECT_EQ(&raf, f.filter());
38 E : }
39 :
40 E : TEST(FilterableTest, Accessors) {
41 E : Filterable f;
42 :
43 E : RelativeAddressFilter raf;
44 E : f.set_filter(&raf);
45 E : EXPECT_EQ(&raf, f.filter());
46 :
47 E : f.set_filter(NULL);
48 E : EXPECT_TRUE(f.filter() == NULL);
49 E : }
50 :
51 E : TEST(FilterableTest, IsFiltered) {
52 E : Filterable f;
53 :
54 E : const uint8 nop[] = { 0x90 };
55 E : const uint8 data[10] = {};
56 :
57 E : BlockGraph block_graph;
58 E : BasicBlockSubGraph subgraph;
59 :
60 : // Create some dummy blocks, etc. Initially they have no source ranges so
61 : // should all pass as instrumentable.
62 : BlockGraph::Block* block =
63 E : block_graph.AddBlock(BlockGraph::CODE_BLOCK, 10, "block");
64 E : Instruction inst;
65 E : EXPECT_TRUE(Instruction::FromBuffer(nop, arraysize(nop), &inst));
66 E : BasicCodeBlock* code_bb = subgraph.AddBasicCodeBlock("code_bb");
67 E : code_bb->instructions().push_back(inst);
68 : BasicDataBlock* data_bb =
69 E : subgraph.AddBasicDataBlock("data_bb", arraysize(data), data);
70 E : BasicBlock* code_bb_ptr = code_bb;
71 E : BasicBlock* data_bb_ptr = data_bb;
72 :
73 : // We expect nothing to be filtered because there is none.
74 E : EXPECT_FALSE(f.IsFiltered(block));
75 E : EXPECT_FALSE(f.IsFiltered(code_bb));
76 E : EXPECT_FALSE(f.IsFiltered(data_bb));
77 E : EXPECT_FALSE(f.IsFiltered(code_bb_ptr));
78 E : EXPECT_FALSE(f.IsFiltered(data_bb_ptr));
79 E : EXPECT_FALSE(f.IsFiltered(inst));
80 :
81 : // Create a filter and pass it to the Filterable object.
82 E : RelativeAddressFilter raf(Range(RelativeAddress(0), 100));
83 E : raf.Mark(Range(RelativeAddress(10), 10));
84 E : f.set_filter(&raf);
85 :
86 : // Give all of the test data source ranges, but that don't conflict with
87 : // any of the ranges in the filter.
88 : EXPECT_TRUE(block->source_ranges().Push(
89 : BlockGraph::Block::SourceRanges::SourceRange(0, 10),
90 : BlockGraph::Block::SourceRanges::DestinationRange(
91 E : RelativeAddress(35), 10)));
92 : inst.set_source_range(
93 E : Range(RelativeAddress(32), arraysize(nop)));
94 : code_bb->instructions().begin()->set_source_range(
95 E : Range(RelativeAddress(38), arraysize(nop)));
96 E : data_bb->set_source_range(Range(RelativeAddress(29), arraysize(data)));
97 :
98 : // We expect nothing to be filtered.
99 E : EXPECT_FALSE(f.IsFiltered(block));
100 E : EXPECT_FALSE(f.IsFiltered(code_bb));
101 E : EXPECT_FALSE(f.IsFiltered(data_bb));
102 E : EXPECT_FALSE(f.IsFiltered(code_bb_ptr));
103 E : EXPECT_FALSE(f.IsFiltered(data_bb_ptr));
104 E : EXPECT_FALSE(f.IsFiltered(inst));
105 :
106 : // Now mark a conflicting range in the filter.
107 E : raf.Mark(Range(RelativeAddress(30), 10));
108 :
109 : // We expect everything to be filtered.
110 E : EXPECT_TRUE(f.IsFiltered(block));
111 E : EXPECT_TRUE(f.IsFiltered(code_bb));
112 E : EXPECT_TRUE(f.IsFiltered(data_bb));
113 E : EXPECT_TRUE(f.IsFiltered(code_bb_ptr));
114 E : EXPECT_TRUE(f.IsFiltered(data_bb_ptr));
115 E : EXPECT_TRUE(f.IsFiltered(inst));
116 E : }
117 :
118 : } // namespace block_graph
|