aboutsummaryrefslogtreecommitdiff
path: root/zucchini_tools.cc
diff options
context:
space:
mode:
authorSamuel Huang <huangs@chromium.org>2018-03-13 18:19:34 +0000
committerEdward Lesmes <ehmaldonado@google.com>2021-07-23 21:50:59 +0000
commit06f1ae9aaca969ee95ef840f22b6b461c304542d (patch)
treef1e5c6624e70628e81fbf38d6cd14b974abe5d93 /zucchini_tools.cc
downloadzucchini-06f1ae9aaca969ee95ef840f22b6b461c304542d.tar.gz
[Zucchini] Move Zucchini from /chrome/installer/ to /components/.
(Use "git log --follow" to see older revisions of files). /components/ is the most logical place to put Zucchini, which only depends on /base and /testing/gtest. This move also enables Zucchini to be used by the Component Updater. Details: - Move all files; run the following to change deps and guards: sed 's/chrome\/installer/components/' *.cc *.h -i sed 's/CHROME_INSTALLER/COMPONENTS/' *.cc *.h -i - Sorting works out pretty well! - Change all 'chrome/installer/zucchini' to 'components/zucchini' throughout other parts of the repo; sort if necessary. - Fix 6 'git cl lint' errors. - Change 1 Bind() usage to BindRepeated(). - Update OWNER. Bug: 729154 Change-Id: I50c5a7d411ea85f707b5994ab319dfb2a1acccf7 Reviewed-on: https://chromium-review.googlesource.com/954923 Reviewed-by: Greg Thompson <grt@chromium.org> Reviewed-by: Jochen Eisinger <jochen@chromium.org> Reviewed-by: Samuel Huang <huangs@chromium.org> Commit-Queue: Samuel Huang <huangs@chromium.org> Cr-Commit-Position: refs/heads/master@{#542857} NOKEYCHECK=True GitOrigin-RevId: 577ef6c435e8d43be6e3e60ccbcbd1881780f4ec
Diffstat (limited to 'zucchini_tools.cc')
-rw-r--r--zucchini_tools.cc126
1 files changed, 126 insertions, 0 deletions
diff --git a/zucchini_tools.cc b/zucchini_tools.cc
new file mode 100644
index 0000000..784e355
--- /dev/null
+++ b/zucchini_tools.cc
@@ -0,0 +1,126 @@
+// 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/zucchini_tools.h"
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include <algorithm>
+#include <memory>
+#include <ostream>
+#include <string>
+
+#include "base/bind.h"
+#include "base/strings/stringprintf.h"
+#include "components/zucchini/disassembler.h"
+#include "components/zucchini/element_detection.h"
+#include "components/zucchini/ensemble_matcher.h"
+#include "components/zucchini/heuristic_ensemble_matcher.h"
+#include "components/zucchini/io_utils.h"
+
+namespace zucchini {
+
+status::Code ReadReferences(ConstBufferView image,
+ bool do_dump,
+ std::ostream& out) {
+ std::unique_ptr<Disassembler> disasm = MakeDisassemblerWithoutFallback(image);
+ if (!disasm) {
+ out << "Input file not recognized as executable." << std::endl;
+ return status::kStatusInvalidOldImage;
+ }
+
+ std::vector<offset_t> targets;
+ for (const auto& group : disasm->MakeReferenceGroups()) {
+ targets.clear();
+ auto refs = group.GetReader(disasm.get());
+ for (auto ref = refs->GetNext(); ref.has_value(); ref = refs->GetNext())
+ targets.push_back(ref->target);
+
+ size_t num_locations = targets.size();
+ std::sort(targets.begin(), targets.end());
+ targets.erase(std::unique(targets.begin(), targets.end()), targets.end());
+ size_t num_targets = targets.size();
+
+ out << "Type " << int(group.type_tag().value());
+ out << ": Pool=" << static_cast<uint32_t>(group.pool_tag().value());
+ out << ", width=" << group.width();
+ out << ", #locations=" << num_locations;
+ out << ", #targets=" << num_targets;
+ if (num_targets > 0) {
+ double ratio = static_cast<double>(num_locations) / num_targets;
+ out << " (ratio=" << base::StringPrintf("%.4f", ratio) << ")";
+ }
+ out << std::endl;
+
+ if (do_dump) {
+ auto refs = group.GetReader(disasm.get());
+
+ for (auto ref = refs->GetNext(); ref; ref = refs->GetNext()) {
+ out << " " << AsHex<8>(ref->location);
+ out << " " << AsHex<8>(ref->target) << std::endl;
+ }
+ }
+ }
+
+ return status::kStatusSuccess;
+}
+
+status::Code DetectAll(ConstBufferView image,
+ std::ostream& out,
+ std::vector<ConstBufferView>* sub_image_list) {
+ DCHECK_NE(sub_image_list, nullptr);
+ sub_image_list->clear();
+
+ const size_t size = image.size();
+ size_t last_out_pos = 0;
+ size_t total_bytes_found = 0;
+
+ auto print_range = [&out](size_t pos, size_t size, const std::string& msg) {
+ out << "-- " << AsHex<8, size_t>(pos) << " +" << AsHex<8, size_t>(size)
+ << ": " << msg << std::endl;
+ };
+
+ ElementFinder finder(image,
+ base::BindRepeating(DetectElementFromDisassembler));
+ for (auto element = finder.GetNext(); element.has_value();
+ element = finder.GetNext()) {
+ ConstBufferView sub_image = image[element->region()];
+ sub_image_list->push_back(sub_image);
+ size_t pos = sub_image.begin() - image.begin();
+ size_t prog_size = sub_image.size();
+ if (last_out_pos < pos)
+ print_range(last_out_pos, pos - last_out_pos, "?");
+ auto disasm = MakeDisassemblerOfType(sub_image, element->exe_type);
+ print_range(pos, prog_size, disasm->GetExeTypeString());
+ total_bytes_found += prog_size;
+ last_out_pos = pos + prog_size;
+ }
+ if (last_out_pos < size)
+ print_range(last_out_pos, size - last_out_pos, "?");
+ out << std::endl;
+
+ // Print summary, using decimal instead of hexadecimal.
+ out << "Detected " << total_bytes_found << "/" << size << " bytes => ";
+ double percent = total_bytes_found * 100.0 / size;
+ out << base::StringPrintf("%.2f", percent) << "%." << std::endl;
+
+ return status::kStatusSuccess;
+}
+
+status::Code MatchAll(ConstBufferView old_image,
+ ConstBufferView new_image,
+ std::ostream& out) {
+ HeuristicEnsembleMatcher matcher(&out);
+ if (!matcher.RunMatch(old_image, new_image)) {
+ out << "RunMatch() failed.";
+ return status::kStatusFatal;
+ }
+ out << "Found " << matcher.matches().size() << " nontrivial matches and "
+ << matcher.num_identical() << " identical matches." << std::endl;
+
+ return status::kStatusSuccess;
+}
+
+} // namespace zucchini