1 : // Copyright 2012 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/instrument/mutators/add_indexed_data_ranges_stream.h"
16 :
17 : #include "syzygy/common/indexed_frequency_data.h"
18 : #include "syzygy/pdb/pdb_byte_stream.h"
19 :
20 : namespace instrument {
21 : namespace mutators {
22 :
23 : const char AddIndexedDataRangesStreamPdbMutator::kMutatorName[] =
24 : "AddIndexedDataRangesStreamPdbMutator";
25 :
26 : bool AddIndexedDataRangesStreamPdbMutator::AddNamedStreams(
27 E : const pdb::PdbFile& pdb_file) {
28 E : if (indexed_data_ranges_.size() == 0) {
29 E : LOG(INFO) << "Indexed data addresses vector is empty. Not adding stream.";
30 E : return true;
31 : }
32 :
33 : // Create the stream.
34 E : scoped_refptr<pdb::PdbByteStream> stream(new pdb::PdbByteStream);
35 : CHECK(stream->Init(
36 : reinterpret_cast<const uint8*>(&indexed_data_ranges_.at(0)),
37 E : indexed_data_ranges_.size() * sizeof(indexed_data_ranges_.at(0))));
38 :
39 : // Add the stream to the PDB.
40 E : if (!SetNamedStream(stream_name_.c_str(), stream.get())) {
41 : // This should not happen, as it indicates we are trying to doubly
42 : // instrument a given binary.
43 E : LOG(ERROR) << "Indexed data ranges stream already exists.";
44 E : return false;
45 : }
46 :
47 E : return true;
48 E : }
49 :
50 : } // namespace mutators
51 : } // namespace instrument
|