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