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/assm/unittest_util.h"
19 : #include "syzygy/block_graph/basic_block_subgraph.h"
20 :
21 : namespace block_graph {
22 :
23 : namespace {
24 :
25 : typedef core::RelativeAddress RelativeAddress;
26 : typedef RelativeAddressFilter::Range Range;
27 :
28 : } // namespace
29 :
30 E : TEST(FilterableTest, DefaultConstructor) {
31 E : Filterable f;
32 E : EXPECT_TRUE(f.filter() == NULL);
33 E : }
34 :
35 E : TEST(FilterableTest, ConstructorWithFilter) {
36 E : RelativeAddressFilter raf;
37 E : Filterable f(&raf);
38 E : EXPECT_EQ(&raf, f.filter());
39 E : }
40 :
41 E : TEST(FilterableTest, Accessors) {
42 E : Filterable f;
43 :
44 E : RelativeAddressFilter raf;
45 E : f.set_filter(&raf);
46 E : EXPECT_EQ(&raf, f.filter());
47 :
48 E : f.set_filter(NULL);
49 E : EXPECT_TRUE(f.filter() == NULL);
50 E : }
51 :
52 E : TEST(FilterableTest, IsFiltered) {
53 E : Filterable f;
54 :
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 : EXPECT_TRUE(Instruction::FromBuffer(testing::kNop1,
66 E : arraysize(testing::kNop1), &inst));
67 E : BasicCodeBlock* code_bb = subgraph.AddBasicCodeBlock("code_bb");
68 E : code_bb->instructions().push_back(inst);
69 : BasicDataBlock* data_bb =
70 E : subgraph.AddBasicDataBlock("data_bb", arraysize(data), data);
71 E : BasicBlock* code_bb_ptr = code_bb;
72 E : BasicBlock* data_bb_ptr = data_bb;
73 :
74 : // We expect nothing to be filtered because there is none.
75 E : EXPECT_FALSE(f.IsFiltered(block));
76 E : EXPECT_FALSE(f.IsFiltered(code_bb));
77 E : EXPECT_FALSE(f.IsFiltered(data_bb));
78 E : EXPECT_FALSE(f.IsFiltered(code_bb_ptr));
79 E : EXPECT_FALSE(f.IsFiltered(data_bb_ptr));
80 E : EXPECT_FALSE(f.IsFiltered(inst));
81 :
82 : // Create a filter and pass it to the Filterable object.
83 E : RelativeAddressFilter raf(Range(RelativeAddress(0), 100));
84 E : raf.Mark(Range(RelativeAddress(10), 10));
85 E : f.set_filter(&raf);
86 :
87 : // Give all of the test data source ranges, but that don't conflict with
88 : // any of the ranges in the filter.
89 : EXPECT_TRUE(block->source_ranges().Push(
90 : BlockGraph::Block::SourceRanges::SourceRange(0, 10),
91 : BlockGraph::Block::SourceRanges::DestinationRange(
92 E : RelativeAddress(35), 10)));
93 : inst.set_source_range(
94 E : Range(RelativeAddress(32), arraysize(testing::kNop1)));
95 : code_bb->instructions().begin()->set_source_range(
96 E : Range(RelativeAddress(38), arraysize(testing::kNop1)));
97 E : data_bb->set_source_range(Range(RelativeAddress(29), arraysize(data)));
98 :
99 : // We expect nothing to be filtered.
100 E : EXPECT_FALSE(f.IsFiltered(block));
101 E : EXPECT_FALSE(f.IsFiltered(code_bb));
102 E : EXPECT_FALSE(f.IsFiltered(data_bb));
103 E : EXPECT_FALSE(f.IsFiltered(code_bb_ptr));
104 E : EXPECT_FALSE(f.IsFiltered(data_bb_ptr));
105 E : EXPECT_FALSE(f.IsFiltered(inst));
106 :
107 : // Now mark a conflicting range in the filter.
108 E : raf.Mark(Range(RelativeAddress(30), 10));
109 :
110 : // We expect everything to be filtered.
111 E : EXPECT_TRUE(f.IsFiltered(block));
112 E : EXPECT_TRUE(f.IsFiltered(code_bb));
113 E : EXPECT_TRUE(f.IsFiltered(data_bb));
114 E : EXPECT_TRUE(f.IsFiltered(code_bb_ptr));
115 E : EXPECT_TRUE(f.IsFiltered(data_bb_ptr));
116 E : EXPECT_TRUE(f.IsFiltered(inst));
117 E : }
118 :
119 : } // namespace block_graph
|