Fix building on macOS.

Adding LEB128 reader class.
This commit is contained in:
Selim Mustafaev 2022-11-28 20:46:19 +03:00
parent 2a6e45b05d
commit 5ec1117337
7 changed files with 125 additions and 11 deletions

View File

@ -3,6 +3,6 @@ project(unflutter)
set(CMAKE_CXX_STANDARD 20)
add_executable(unflutter main.cpp dart/Reader.cpp dart/Reader.h dart/Snapshot.cpp dart/Snapshot.h)
add_executable(unflutter main.cpp dart/Reader.cpp dart/Reader.h dart/Snapshot.cpp dart/Snapshot.h dart/Leb128Int.cpp dart/Leb128Int.h)
target_include_directories(unflutter PRIVATE ${CMAKE_SOURCE_DIR}/ThirdParty)

36
dart/Leb128Int.cpp Normal file
View File

@ -0,0 +1,36 @@
//
// Created by Selim Mustafaev on 28.11.2022.
//
#include "Leb128Int.h"
namespace Dart {
Leb128Int::Leb128Int(): _value(0), _size(0) {
}
Dart::Leb128Int::Leb128Int(const std::byte* ptr): _value(0), _size(0) {
size_t bitShift = 0;
auto data = reinterpret_cast<const uint8_t*>(ptr);
while (*data < 0x80) {
_value |= (*data << bitShift);
bitShift += 7;
data++;
_size++;
}
_value |= ((*data & 0x7f) << bitShift);
_size++;
}
uint64_t Leb128Int::value() const {
return _value;
}
size_t Leb128Int::size() const {
return _size;
}
}

28
dart/Leb128Int.h Normal file
View File

@ -0,0 +1,28 @@
//
// Created by Selim Mustafaev on 28.11.2022.
//
#ifndef UNFLUTTER_LEB128INT_H
#define UNFLUTTER_LEB128INT_H
#include <cstdint>
#include <cstdio>
#include <cstddef>
namespace Dart {
class Leb128Int {
private:
uint64_t _value;
size_t _size;
public:
Leb128Int();
explicit Leb128Int(const std::byte* ptr);
[[nodiscard]] uint64_t value() const;
[[nodiscard]] size_t size() const;
};
}
#endif //UNFLUTTER_LEB128INT_H

View File

@ -4,9 +4,9 @@
#include "Reader.h"
#include "elfio/elfio_dump.hpp"
#include "elfio/elf_types.hpp"
#include <fstream>
#include <elf.h>
namespace Dart {
@ -49,7 +49,7 @@ namespace Dart {
const ELFIO::symbol_section_accessor symbols(_elfio, section.get());
for(size_t i = 0; i < symbols.get_symbols_num(); ++i) {
std::string name;
Elf64_Addr address;
ELFIO::Elf64_Addr address;
ELFIO::Elf_Xword size;
ELFIO::Elf_Half section_index;
unsigned char bind, type, other;

View File

@ -27,15 +27,20 @@ namespace Dart {
data += _versionHash.size();
auto features = std::string((char*)data);
data += features.size();
data += features.size() + 1;
_features = split(features);
auto view = features
| std::ranges::views::split(' ')
| std::ranges::views::transform([](auto &&rng) {
return std::string(&*rng.begin(), std::ranges::distance(rng));
});
_baseObjectsCount = Leb128Int(data);
data += _baseObjectsCount.size();
_features.assign(view.begin(), view.end());
_allObjectsCount = Leb128Int(data);
data += _allObjectsCount.size();
_clustersCount = Leb128Int(data);
data += _clustersCount.size();
_fieldTableLength = Leb128Int(data);
data += _fieldTableLength.size();
}
uint64_t Snapshot::size() const {
@ -72,4 +77,32 @@ namespace Dart {
return ss.str();
}
std::vector<std::string> Snapshot::split(const std::string &str) {
std::vector<std::string> result;
std::stringstream ss(str);
std::string item;
while (std::getline (ss, item, ' ')) {
result.push_back (item);
}
return result;
}
uint64_t Snapshot::baseObjectsCount() const {
return _baseObjectsCount.value();
}
uint64_t Snapshot::allObjectsCount() const {
return _allObjectsCount.value();
}
uint64_t Snapshot::clustersCount() const {
return _clustersCount.value();
}
uint64_t Snapshot::fieldTableLength() const {
return _fieldTableLength.value();
}
}

View File

@ -5,6 +5,8 @@
#ifndef UNFLUTTER_SNAPSHOT_H
#define UNFLUTTER_SNAPSHOT_H
#include "Leb128Int.h"
#include <cstddef>
#include <cstdint>
#include <string>
@ -26,6 +28,13 @@ namespace Dart {
Kind _kind;
std::string _versionHash;
std::vector<std::string> _features;
Leb128Int _baseObjectsCount;
Leb128Int _allObjectsCount;
Leb128Int _clustersCount;
Leb128Int _fieldTableLength;
private:
static std::vector<std::string> split(const std::string &str);
public:
explicit Snapshot(const std::byte* data);
@ -33,6 +42,10 @@ namespace Dart {
[[nodiscard]] std::string kindString() const;
[[nodiscard]] std::string versionHash() const;
[[nodiscard]] std::string featuresString() const;
[[nodiscard]] uint64_t baseObjectsCount() const;
[[nodiscard]] uint64_t allObjectsCount() const;
[[nodiscard]] uint64_t clustersCount() const;
[[nodiscard]] uint64_t fieldTableLength() const;
};
}

View File

@ -2,13 +2,17 @@
#include <iostream>
int main(int argc, char** argv) {
Dart::Reader reader("/home/selim/Downloads/libapp.so");
Dart::Reader reader("/Users/selim/Documents/libapp.so");
auto snapshot = reader.getSnapshot(Dart::DartSnapshot::VM_DATA);
std::cout << "Snapshot kind: " << snapshot.kindString() << std::endl;
std::cout << "Size: " << snapshot.size() << std::endl;
std::cout << "Version hash: " << snapshot.versionHash() << std::endl;
std::cout << "Features: " << snapshot.featuresString() << std::endl;
std::cout << "Base objects: " << snapshot.baseObjectsCount() << std::endl;
std::cout << "All objects: " << snapshot.allObjectsCount() << std::endl;
std::cout << "Clusters: " << snapshot.clustersCount() << std::endl;
std::cout << "Field table length: " << snapshot.fieldTableLength() << std::endl;
return 0;
}