• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "components/zucchini/image_index.h"
6 
7 #include <algorithm>
8 #include <utility>
9 
10 #include "components/zucchini/algorithm.h"
11 #include "components/zucchini/disassembler.h"
12 
13 namespace zucchini {
14 
ImageIndex(ConstBufferView image)15 ImageIndex::ImageIndex(ConstBufferView image)
16     : image_(image), type_tags_(image.size(), kNoTypeTag) {}
17 
18 ImageIndex::ImageIndex(ImageIndex&&) = default;
19 
20 ImageIndex::~ImageIndex() = default;
21 
Initialize(Disassembler * disasm)22 bool ImageIndex::Initialize(Disassembler* disasm) {
23   std::vector<ReferenceGroup> ref_groups = disasm->MakeReferenceGroups();
24   for (const auto& group : ref_groups) {
25     // Build pool-to-type mapping.
26     DCHECK_NE(kNoPoolTag, group.pool_tag());
27     TargetPool& target_pool = target_pools_[group.pool_tag()];
28     target_pool.AddType(group.type_tag());
29     target_pool.InsertTargets(std::move(*group.GetReader(disasm)));
30   }
31   for (const auto& group : ref_groups) {
32     // Find and store all references for each type, returns false on finding
33     // any overlap, to signal error.
34     if (!InsertReferences(group.traits(),
35                           std::move(*group.GetReader(disasm)))) {
36       return false;
37     }
38   }
39   return true;
40 }
41 
IsToken(offset_t location) const42 bool ImageIndex::IsToken(offset_t location) const {
43   TypeTag type = LookupType(location);
44 
45   // |location| points into raw data.
46   if (type == kNoTypeTag)
47     return true;
48 
49   // |location| points into a Reference.
50   Reference reference = refs(type).at(location);
51   // Only the first byte of a reference is a token.
52   return location == reference.location;
53 }
54 
InsertReferences(const ReferenceTypeTraits & traits,ReferenceReader && ref_reader)55 bool ImageIndex::InsertReferences(const ReferenceTypeTraits& traits,
56                                   ReferenceReader&& ref_reader) {
57   // Store ReferenceSet for current type (of |group|).
58   DCHECK_NE(kNoTypeTag, traits.type_tag);
59   auto result = reference_sets_.emplace(
60       traits.type_tag, ReferenceSet(traits, pool(traits.pool_tag)));
61   DCHECK(result.second);
62 
63   result.first->second.InitReferences(std::move(ref_reader));
64   for (auto ref : reference_sets_.at(traits.type_tag)) {
65     DCHECK(RangeIsBounded(ref.location, traits.width, size()));
66     auto cur_type_tag = type_tags_.begin() + ref.location;
67 
68     // Check for overlap with existing reference. If found, then invalidate.
69     if (std::any_of(cur_type_tag, cur_type_tag + traits.width,
70                     [](TypeTag type) { return type != kNoTypeTag; })) {
71       return false;
72     }
73     std::fill(cur_type_tag, cur_type_tag + traits.width, traits.type_tag);
74   }
75   return true;
76 }
77 
78 }  // namespace zucchini
79