aboutsummaryrefslogtreecommitdiff
path: root/image_index.cc
blob: 1efe5d8f08a2b5e6e257ca23f49fd9f21d4de02f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/zucchini/image_index.h"

#include <algorithm>
#include <utility>

#include "components/zucchini/algorithm.h"
#include "components/zucchini/disassembler.h"

namespace zucchini {

ImageIndex::ImageIndex(ConstBufferView image)
    : image_(image), type_tags_(image.size(), kNoTypeTag) {}

ImageIndex::ImageIndex(ImageIndex&&) = default;

ImageIndex::~ImageIndex() = default;

bool ImageIndex::Initialize(Disassembler* disasm) {
  std::vector<ReferenceGroup> ref_groups = disasm->MakeReferenceGroups();
  for (const auto& group : ref_groups) {
    // Build pool-to-type mapping.
    DCHECK_NE(kNoPoolTag, group.pool_tag());
    TargetPool& target_pool = target_pools_[group.pool_tag()];
    target_pool.AddType(group.type_tag());
    target_pool.InsertTargets(std::move(*group.GetReader(disasm)));
  }
  for (const auto& group : ref_groups) {
    // Find and store all references for each type, returns false on finding
    // any overlap, to signal error.
    if (!InsertReferences(group.traits(),
                          std::move(*group.GetReader(disasm)))) {
      return false;
    }
  }
  return true;
}

bool ImageIndex::IsToken(offset_t location) const {
  TypeTag type = LookupType(location);

  // |location| points into raw data.
  if (type == kNoTypeTag)
    return true;

  // |location| points into a Reference.
  Reference reference = refs(type).at(location);
  // Only the first byte of a reference is a token.
  return location == reference.location;
}

bool ImageIndex::InsertReferences(const ReferenceTypeTraits& traits,
                                  ReferenceReader&& ref_reader) {
  // Store ReferenceSet for current type (of |group|).
  DCHECK_NE(kNoTypeTag, traits.type_tag);
  auto result = reference_sets_.emplace(
      traits.type_tag, ReferenceSet(traits, pool(traits.pool_tag)));
  DCHECK(result.second);

  result.first->second.InitReferences(std::move(ref_reader));
  for (auto ref : reference_sets_.at(traits.type_tag)) {
    DCHECK(RangeIsBounded(ref.location, traits.width, size()));
    auto cur_type_tag = type_tags_.begin() + ref.location;

    // Check for overlap with existing reference. If found, then invalidate.
    if (std::any_of(cur_type_tag, cur_type_tag + traits.width,
                    [](TypeTag type) { return type != kNoTypeTag; })) {
      return false;
    }
    std::fill(cur_type_tag, cur_type_tag + traits.width, traits.type_tag);
  }
  return true;
}

}  // namespace zucchini