-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_test.cpp
More file actions
152 lines (143 loc) · 6.41 KB
/
config_test.cpp
File metadata and controls
152 lines (143 loc) · 6.41 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
141
142
143
144
145
146
147
148
149
150
151
152
#include "yaps/pipeline/analysis_request.hpp"
#include "yaps/support/runtime.hpp"
#include <iostream>
#include <stdexcept>
#include <nlohmann/json.hpp>
int main() {
try {
yaps::ConfigureProcessForTerminalDiagnostics();
const nlohmann::json json = {
{"target_arch", "x86_64"},
{"vpc",
{{"mode", "manual"},
{"storage", "memory_at_register"},
{"base_register", "rbp"},
{"displacement", -120},
{"width", 8}}},
{"vip",
{{"mode", "derived_from_vpc"},
{"operation", "xor"},
{"constant", "0x55"}}},
{"bytecode", {{"base", "0x401000"}, {"size", "0x200"}}},
{"constant_regions", {{{"base", "0x500000"}, {"size", "0x1000"}}}},
{"entry_context",
{{"initial_vpc", "0x401000"},
{"registers", {{"rax", "0x1"}, {"rbx", 2}}},
{"memory",
{{{"storage", "memory_at_register"},
{"base_register", "rsp"},
{"displacement", 0},
{"width", 8},
{"value", "0x401234"}}}}}},
{"warmup",
{{"enabled", true},
{"max_instructions", 2048},
{"stop_on_accepted_vpc_update", true},
{"stop_on_executable_indirect_branch", false}}},
};
const yaps::pipeline::AnalysisConfig config = json.get<yaps::pipeline::AnalysisConfig>();
if (config.target_arch != "x86_64") {
throw std::runtime_error("target_arch parse failed");
}
if (config.vpc.storage != yaps::vm::VpcStorage::MemoryAtRegister ||
!config.vpc.base_register || *config.vpc.base_register != "rbp" ||
config.vpc.displacement != -120) {
throw std::runtime_error("vpc memory_at_register parse failed");
}
if (config.bytecode.base != 0x401000 || config.bytecode.size != 0x200) {
throw std::runtime_error("bytecode parse failed");
}
if (config.constant_regions.size() != 1 || config.constant_regions.front().base != 0x500000 ||
config.constant_regions.front().size != 0x1000) {
throw std::runtime_error("constant_regions parse failed");
}
if (!config.entry_context || !config.entry_context->initial_vpc ||
*config.entry_context->initial_vpc != 0x401000) {
throw std::runtime_error("entry_context parse failed");
}
if (!config.warmup || !config.warmup->enabled || config.warmup->max_instructions != 2048 ||
!config.warmup->stop_on_accepted_vpc_update ||
config.warmup->stop_on_executable_indirect_branch) {
throw std::runtime_error("warmup parse failed");
}
if (config.entry_context->memory.size() != 1 ||
config.entry_context->memory.front().storage != yaps::vm::VpcStorage::MemoryAtRegister ||
!config.entry_context->memory.front().base_register ||
*config.entry_context->memory.front().base_register != "rsp" ||
config.entry_context->memory.front().displacement != 0 ||
config.entry_context->memory.front().width != 8 ||
config.entry_context->memory.front().value != 0x401234) {
throw std::runtime_error("entry_context memory parse failed");
}
if (config.vip.mode != yaps::vm::VipMode::DerivedFromVpc ||
config.vip.operation != yaps::vm::VipOperation::Xor ||
config.vip.constant != 0x55) {
throw std::runtime_error("vip parse failed");
}
const nlohmann::json roundtrip = config;
if (roundtrip.at("bytecode").at("base").get<uint64_t>() != 0x401000) {
throw std::runtime_error("config roundtrip failed");
}
if (roundtrip.at("vip").at("operation").get<std::string>() != "xor") {
throw std::runtime_error("vip roundtrip failed");
}
if (roundtrip.at("vpc").at("storage").get<std::string>() != "memory_at_register") {
throw std::runtime_error("vpc storage roundtrip failed");
}
if (roundtrip.at("entry_context").at("memory").at(0).at("base_register").get<std::string>() !=
"rsp") {
throw std::runtime_error("entry_context memory roundtrip failed");
}
if (roundtrip.at("constant_regions").at(0).at("base").get<uint64_t>() != 0x500000) {
throw std::runtime_error("constant_regions roundtrip failed");
}
if (!roundtrip.contains("warmup") ||
roundtrip.at("warmup").at("max_instructions").get<uint64_t>() != 2048) {
throw std::runtime_error("warmup roundtrip failed");
}
const nlohmann::json vip_expr_json = {
{"target_arch", "x86_64"},
{"vpc", {{"mode", "manual"}, {"storage", "register"}, {"register", "r15"}, {"width", 8}}},
{"vip",
{{"mode", "manual_expression"},
{"expression",
{{"kind", "add"},
{"operands",
{{{"kind", "memory_at_register"},
{"base_register", "rsp"},
{"displacement", 0},
{"width", 8}},
{{"kind", "register"}, {"register", "rbp"}, {"width", 8}}}}}}}},
{"bytecode", {{"base", 0}, {"size", 0}}},
};
const yaps::pipeline::AnalysisConfig vip_expr_config = vip_expr_json.get<yaps::pipeline::AnalysisConfig>();
if (vip_expr_config.vip.mode != yaps::vm::VipMode::ManualExpression ||
!vip_expr_config.vip.expression ||
vip_expr_config.vip.expression->kind != yaps::vm::VipExprKind::Add ||
vip_expr_config.vip.expression->operands.size() != 2 ||
vip_expr_config.vip.expression->operands[0].kind != yaps::vm::VipExprKind::MemoryAtRegister ||
vip_expr_config.vip.expression->operands[1].kind != yaps::vm::VipExprKind::Register) {
throw std::runtime_error("vip manual_expression parse failed");
}
const nlohmann::json vip_expr_roundtrip = vip_expr_config;
if (vip_expr_roundtrip.at("vip").at("mode").get<std::string>() != "manual_expression") {
throw std::runtime_error("vip manual_expression roundtrip failed");
}
const nlohmann::json no_bytecode_json = {
{"target_arch", "x86_64"},
{"vpc", {{"mode", "manual"}, {"storage", "register"}, {"register", "r15"}, {"width", 8}}},
};
const yaps::pipeline::AnalysisConfig no_bytecode_config =
no_bytecode_json.get<yaps::pipeline::AnalysisConfig>();
if (no_bytecode_config.bytecode.base != 0 || no_bytecode_config.bytecode.size != 0) {
throw std::runtime_error("omitted bytecode should default to an empty region");
}
return 0;
} catch (const std::exception& ex) {
std::cerr << ex.what() << '\n';
return 1;
} catch (...) {
std::cerr << "unknown non-std exception\n";
return 1;
}
}