Demonstrate many different elements of the GTIRB API.
#include <boost/uuid/uuid_io.hpp>
#include <fstream>
#include <iostream>
int main() {
AuxDataContainer::registerAuxDataType<gtirb::schema::Types>();
Ir->addModule(M);
BI->addBlock(0, D1);
BI->addBlock(6, D2);
std::array<uint8_t, 8> Bytes{1, 0, 2, 0, 115, 116, 114, 108};
BI->insertBytes(
const_cast<const ByteInterval*
>(BI)->bytes_begin<uint8_t>(),
Bytes.begin(), Bytes.end());
[[maybe_unused]]
auto* Sym1 = M->addSymbol(
Symbol::Create(C, D1,
"data1"));
[[maybe_unused]]
auto* Sym2 = M->addSymbol(
Symbol::Create(C, D2,
"data2"));
BI->addBlock(12, B1);
BI->addBlock(16, B2);
auto& Cfg = Ir->getCFG();
{{D1->getUUID(), "string"}, {D2->getUUID(), "uleb128"}});
for (
const auto& Sym : M->findSymbols(
Addr(2054))) {
[[maybe_unused]] const auto& _ = Sym;
assert(Sym.getAddress() ==
Addr(2054));
assert(Sym.getName() == "data2" || Sym.getName() == "duplicateReferent");
assert(Sym.getReferent<
DataBlock>() ==
nullptr ||
}
for (const auto& Sym : M->findSymbols("duplicateName")) {
[[maybe_unused]] const auto& _ = Sym;
assert(Sym.getName() == "duplicateName");
assert(Sym.getAddress() ==
Addr(2048) || Sym.getAddress() ==
Addr(4096));
}
[[maybe_unused]]
auto* Referent = Sym1->getReferent<
DataBlock>();
assert(Referent != nullptr);
assert(Referent->getAddress() ==
Addr(2054));
assert(Referent->getSize() == 2);
assert(Referent->getByteInterval() == BI);
assert(Referent->getOffset() == 6);
auto Blocks = M->findBlocksAt(
Addr(2048),
Addr(4096));
assert(std::distance(Blocks.begin(), Blocks.end()) == 4);
for (
const auto& B :
blocks(Cfg)) {
std::cout << "Block at address " << B.getAddress() << std::endl;
}
auto [VerticesBegin, VerticesEnd] = boost::vertices(Cfg);
for (const auto& Vertex :
boost::make_iterator_range(VerticesBegin, VerticesEnd)) {
if (Cfg[Vertex] == B2) {
std::cout << "B2's vertex descriptor is: " << Vertex << std::endl;
}
}
auto [EdgesBegin, EdgesEnd] = boost::edges(Cfg);
for (const auto& Edge : boost::make_iterator_range(EdgesBegin, EdgesEnd)) {
auto V1 = boost::source(Edge, Cfg);
auto V2 = boost::target(Edge, Cfg);
std::cout << "Edge: " << Cfg[V1] << " => " << Cfg[V2] << std::endl;
auto Label = *Cfg[Edge];
std::cout << "Conditional? "
<< std::endl;
std::cout << "Direct? "
<< std::endl;
std::cout << "Fallthrough? "
<< std::endl;
}
if (typesMap) {
for (const auto& [DataBlockID, BlockType] : *typesMap) {
std::cout << "Data block with UUID " << DataBlockID << " is of type "
<< BlockType << "!";
}
}
std::ofstream Out("path/to/file");
Ir->save(Out);
std::ifstream In("path/to/file");
[[maybe_unused]]
auto& NewIR = *
IR::load(C, In);
return 0;
}