Skip to content

Commit d63c493

Browse files
authored
Add instruction address metadata to vivisect broker (#28)
2 parents 09a0bc2 + 6f97ace commit d63c493

File tree

13 files changed

+225
-236
lines changed

13 files changed

+225
-236
lines changed

MetadataCategories.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,26 @@
22
#define LLVM_MCA_METADATACATEGORIES_H
33
namespace llvm {
44
namespace mcad {
5+
6+
enum MetadataCategories {
7+
58
// Metadata for LSUnit
6-
static constexpr unsigned MD_LSUnit_MemAccess = 0;
9+
MD_LSUnit_MemAccess = 0,
710

811
// Metadata for Branch Prediction
9-
static constexpr unsigned MD_FrontEnd_BranchFlow = 1;
12+
MD_FrontEnd_BranchFlow = 1,
13+
14+
// Virtual address at which an instruction is located in memory
15+
MD_InstrAddr = 2,
1016

1117
// Used for marking the start of custom MD Category
12-
static constexpr unsigned MD_LAST = MD_FrontEnd_BranchFlow;
18+
MD_LAST,
1319

1420
// Metadata categories (custom)
15-
static constexpr unsigned MD_BinaryRegionMarkers = MD_LAST + 1;
21+
MD_BinaryRegionMarkers
22+
23+
};
24+
1625
} // end namespace mcad
1726
} // end namespace llvm
1827
#endif

plugins/vivisect-broker/Broker.cpp

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -128,26 +128,40 @@ class VivisectBroker : public Broker {
128128

129129
MCIS[i] = &MCI; // return a pointer into our MCI_Pool
130130

131-
if (MDE && insn.has_memory_access()) {
132-
auto MemAccess = insn.memory_access();
133-
auto &Registry = MDE->MDRegistry;
134-
auto &IndexMap = MDE->IndexMap;
135-
auto &MemAccessCat = Registry[MD_LSUnit_MemAccess];
136-
IndexMap[i] = TotalNumTraces;
137-
MemAccessCat[TotalNumTraces] = std::move(MDMemoryAccess{
138-
MemAccess.is_store(),
139-
MemAccess.vaddr(),
140-
MemAccess.size(),
141-
});
142-
}
131+
// Add metadata to the fetched instruction if metadata exchanger
132+
// is available
133+
if (MDE) {
134+
135+
// The registry stores the metadata
136+
auto &Registry = MDE->MDRegistry;
137+
138+
// The IndexMap maps instruction index (within this region) to
139+
// the identifier we chose for our metadata. We chose a
140+
// monotonically increasing counter as the identifier for each
141+
// metadata entry.
142+
auto &IndexMap = MDE->IndexMap;
143+
IndexMap[i] = TotalNumTraces;
144+
145+
auto &InstrAddrCat = Registry[MD_InstrAddr];
146+
InstrAddrCat[TotalNumTraces] = insn.addr();
147+
148+
if (insn.has_memory_access()) {
149+
auto MemAccess = insn.memory_access();
150+
auto &MemAccessCat = Registry[MD_LSUnit_MemAccess];
151+
MemAccessCat[TotalNumTraces] = std::move(MDMemoryAccess{
152+
MemAccess.is_store(),
153+
MemAccess.vaddr(),
154+
MemAccess.size(),
155+
});
156+
}
157+
158+
if (insn.has_branch_flow()) {
159+
auto BranchFlow = insn.branch_flow();
160+
auto &Registry = MDE->MDRegistry;
161+
auto &BranchFlowCat = Registry[MD_FrontEnd_BranchFlow];
162+
BranchFlowCat[TotalNumTraces] = BranchFlow.is_mispredict();
163+
}
143164

144-
if (MDE && insn.has_branch_flow()) {
145-
auto BranchFlow = insn.branch_flow();
146-
auto &Registry = MDE->MDRegistry;
147-
auto &IndexMap = MDE->IndexMap;
148-
auto &BranchFlowCat = Registry[MD_FrontEnd_BranchFlow];
149-
IndexMap[i] = TotalNumTraces;
150-
BranchFlowCat[TotalNumTraces] = BranchFlow.is_mispredict();
151165
}
152166

153167
++TotalNumTraces;

plugins/vivisect-broker/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ C++-based server and a python client. To update them, edit the interface
99
`emulator.proto` and rerun the following.
1010

1111
```
12-
python3 -m grpc_tools.protoc -I. --python_out=. --pyi_out=. --grpc_python_out=. emulator.proto
12+
python3 -m grpc_tools.protoc -I. --python_out=grpc_client --pyi_out=grpc_client --grpc_python_out=grpc_client emulator.proto
1313
protoc --grpc_out=. --cpp_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` emulator.proto
1414
```
1515

plugins/vivisect-broker/emulator.pb.cc

Lines changed: 64 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)