-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathChromium.cpp
More file actions
140 lines (116 loc) · 4.17 KB
/
Copy pathChromium.cpp
File metadata and controls
140 lines (116 loc) · 4.17 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <fstream>
#include <filesystem>
#include <benchmark/benchmark.h>
#include <libhat/scanner.hpp>
#include <format>
#include "vendor/UC1.hpp"
#include "vendor/UC2.hpp"
#define WIDE_STR_(x) L ## #x
#define WIDE_STR(x) WIDE_STR_(x)
#define DLL_MAIN_SIGNATURE "48 89 5C 24 08 48 89 74 24 10 57 48 83 EC ?? 49 8B F8"
static const hat::signature DllMainSignature = [] {
auto result = hat::parse_signature(DLL_MAIN_SIGNATURE);
if (!result.has_value()) {
std::terminate();
}
return result.value();
}();
static std::span<const std::byte> get_file_data() {
static std::vector<std::byte> data = []{
const std::filesystem::path path{WIDE_STR(CHROME_DLL_PATH)};
std::ifstream file(path, std::ios::binary);
if (!file.is_open()) {
std::terminate();
}
file.seekg(0, std::ios::end);
const std::streampos fileSize = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<std::byte> contents(static_cast<size_t>(fileSize));
file.read(reinterpret_cast<char*>(contents.data()), fileSize);
return contents;
}();
return data;
}
static void BM_find(benchmark::State& state) {
const auto buf = get_file_data();
hat::const_scan_result result;
for (auto _ : state) {
benchmark::DoNotOptimize(result = hat::find_pattern(buf, DllMainSignature));
}
if (!result.has_result()) {
std::terminate();
}
state.SetBytesProcessed(state.iterations() * (result.get() - buf.data()));
}
static void BM_find_align(benchmark::State& state) {
const auto buf = get_file_data();
hat::const_scan_result result;
for (auto _ : state) {
benchmark::DoNotOptimize(result = hat::find_pattern(buf, DllMainSignature, hat::scan_alignment::X16));
}
if (!result.has_result()) {
std::terminate();
}
state.SetBytesProcessed(state.iterations() * (result.get() - buf.data()));
}
static void BM_find_hint(benchmark::State& state) {
const auto buf = get_file_data();
hat::const_scan_result result;
for (auto _ : state) {
benchmark::DoNotOptimize(result = hat::find_pattern(buf, DllMainSignature, hat::scan_alignment::X1, hat::scan_hint::x86_64));
}
if (!result.has_result()) {
std::terminate();
}
state.SetBytesProcessed(state.iterations() * (result.get() - buf.data()));
}
static void BM_find_align_hint(benchmark::State& state) {
const auto buf = get_file_data();
hat::const_scan_result result;
for (auto _ : state) {
benchmark::DoNotOptimize(result = hat::find_pattern(buf, DllMainSignature, hat::scan_alignment::X16, hat::scan_hint::x86_64));
}
if (!result.has_result()) {
std::terminate();
}
state.SetBytesProcessed(state.iterations() * (result.get() - buf.data()));
}
static void BM_UC1(benchmark::State& state) {
const auto buf = get_file_data();
const auto begin = std::to_address(buf.begin());
const auto end = std::to_address(buf.end());
const std::byte* result{};
const auto sig = UC1::pattern_to_byte(DLL_MAIN_SIGNATURE);
for (auto _ : state) {
benchmark::DoNotOptimize(result = UC1::PatternScan(begin, end, sig));
}
if (!result) {
std::terminate();
}
state.SetBytesProcessed(state.iterations() * (result - buf.data()));
}
static void BM_UC2(benchmark::State& state) {
const auto buf = get_file_data();
const auto begin = std::to_address(buf.begin());
const auto end = std::to_address(buf.end());
const std::byte* result{};
for (auto _ : state) {
benchmark::DoNotOptimize(result = UC2::findPattern(begin, end, DLL_MAIN_SIGNATURE));
}
if (!result) {
std::terminate();
}
state.SetBytesProcessed(state.iterations() * (result - buf.data()));
}
#define LIBHAT_BENCHMARK(...) BENCHMARK(__VA_ARGS__) \
->Threads(1) \
->MinWarmUpTime(2) \
->MinTime(4) \
->UseRealTime();
LIBHAT_BENCHMARK(BM_find);
LIBHAT_BENCHMARK(BM_find_align);
LIBHAT_BENCHMARK(BM_find_hint);
LIBHAT_BENCHMARK(BM_find_align_hint);
LIBHAT_BENCHMARK(BM_UC1);
LIBHAT_BENCHMARK(BM_UC2);
BENCHMARK_MAIN();