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 : // Create some dummy blocks, etc. Initially they have no source ranges so
57 : // should all pass as instrumentable.
58 E : BlockGraph::Block block(0, BlockGraph::CODE_BLOCK, 10, "block");
59 E : Instruction inst;
60 E : EXPECT_TRUE(Instruction::FromBuffer(nop, arraysize(nop), &inst));
61 E : BasicCodeBlock code_bb("code_bb");
62 E : code_bb.instructions().push_back(inst);
63 E : BasicDataBlock data_bb("data_bb", data, arraysize(data));
64 E : BasicBlock* code_bb_ptr = &code_bb;
65 E : BasicBlock* data_bb_ptr = &data_bb;
66 :
67 : // We expect nothing to be filtered because there is none.
68 E : EXPECT_FALSE(f.IsFiltered(&block));
69 E : EXPECT_FALSE(f.IsFiltered(&code_bb));
70 E : EXPECT_FALSE(f.IsFiltered(&data_bb));
71 E : EXPECT_FALSE(f.IsFiltered(code_bb_ptr));
72 E : EXPECT_FALSE(f.IsFiltered(data_bb_ptr));
73 E : EXPECT_FALSE(f.IsFiltered(inst));
74 :
75 : // Create a filter and pass it to the Filterable object.
76 E : RelativeAddressFilter raf(Range(RelativeAddress(0), 100));
77 E : raf.Mark(Range(RelativeAddress(10), 10));
78 E : f.set_filter(&raf);
79 :
80 : // Give all of the test data source ranges, but that don't conflict with
81 : // any of the ranges in the filter.
82 : EXPECT_TRUE(block.source_ranges().Push(
83 : BlockGraph::Block::SourceRanges::SourceRange(0, 10),
84 : BlockGraph::Block::SourceRanges::DestinationRange(
85 E : RelativeAddress(35), 10)));
86 : inst.set_source_range(
87 E : Range(RelativeAddress(32), arraysize(nop)));
88 : code_bb.instructions().begin()->set_source_range(
89 E : Range(RelativeAddress(38), arraysize(nop)));
90 E : data_bb.set_source_range(Range(RelativeAddress(29), arraysize(data)));
91 :
92 : // We expect nothing to be filtered.
93 E : EXPECT_FALSE(f.IsFiltered(&block));
94 E : EXPECT_FALSE(f.IsFiltered(&code_bb));
95 E : EXPECT_FALSE(f.IsFiltered(&data_bb));
96 E : EXPECT_FALSE(f.IsFiltered(code_bb_ptr));
97 E : EXPECT_FALSE(f.IsFiltered(data_bb_ptr));
98 E : EXPECT_FALSE(f.IsFiltered(inst));
99 :
100 : // Now mark a conflicting range in the filter.
101 E : raf.Mark(Range(RelativeAddress(30), 10));
102 :
103 : // We expect everything to be filtered.
104 E : EXPECT_TRUE(f.IsFiltered(&block));
105 E : EXPECT_TRUE(f.IsFiltered(&code_bb));
106 E : EXPECT_TRUE(f.IsFiltered(&data_bb));
107 E : EXPECT_TRUE(f.IsFiltered(code_bb_ptr));
108 E : EXPECT_TRUE(f.IsFiltered(data_bb_ptr));
109 E : EXPECT_TRUE(f.IsFiltered(inst));
110 E : }
111 :
112 : } // namespace block_graph
|