11#pragma once
22
3- #include " module.h"
4-
53#include " platform_types.h"
6-
74#include " utils.h"
85#include < cstdint>
96#include < expected>
107#include < filesystem>
118#include < iostream>
9+ #include < map>
10+ #include < memory>
1211#include < optional>
1312#include < span>
1413#include < string>
1514#include < vector>
1615
1716namespace blook {
1817class Pointer ;
18+ class Module ;
1919struct Thread ;
20- class Process : public std ::enable_shared_from_this<Process> {
21- #ifdef _WIN32
20+ class Process ;
2221
22+ using Allocator = struct ProcessAllocator {
23+ explicit ProcessAllocator (std::shared_ptr<Process> proc);
2324
24- explicit Process (HANDLE h );
25+ std::optional<Pointer> allocate ( size_t size, void *nearAddr = nullptr );
2526
26- explicit Process (DWORD pid );
27+ void deallocate (Pointer addr );
2728
28- #endif
29+ private:
30+ struct AllocatedPageData {
31+ size_t size;
32+ std::map<void *, size_t > allocated;
33+ };
34+ std::map<void *, AllocatedPageData> allocatedPages;
35+ std::shared_ptr<Process> proc;
36+ };
2937
30- struct Allocator {
31- explicit Allocator (std::shared_ptr<Process> proc);
38+ class Process : public std ::enable_shared_from_this<Process> {
39+ public:
40+ enum class MemoryProtection {
41+ None = 0 ,
42+ Read = 0x0001 ,
43+ Write = 0x0010 ,
44+ Execute = 0x0100 ,
45+ ReadWrite = Read | Write,
46+ ReadWriteExecute = Read | Write | Execute,
47+ ReadExecute = Read | Execute,
48+ rw = ReadWrite,
49+ rwx = ReadWriteExecute,
50+ rx = ReadExecute
51+ };
3252
33- std::optional<Pointer> allocate ( size_t size, void *nearAddr = nullptr );
53+ # ifdef _WIN32
3454
35- void deallocate (Pointer addr );
55+ explicit Process (HANDLE h );
3656
37- private:
38- struct AllocatedPageData {
39- size_t size;
40- std::map<void *, size_t > allocated;
41- };
42- std::map<void *, AllocatedPageData> allocatedPages;
43- std::shared_ptr<Process> proc;
44- };
57+ explicit Process (DWORD pid);
4558
46- std::optional<Pointer> _memo{};
47- std::optional<Allocator> _allocator{};
59+ #endif
4860
4961 explicit Process (std::string name, size_t skip = 0 );
5062 bool is_self_cached;
@@ -53,17 +65,49 @@ class Process : public std::enable_shared_from_this<Process> {
5365 friend Module;
5466 friend Pointer;
5567
68+ std::shared_ptr<Pointer> _memo_ptr;
69+ std::shared_ptr<struct ProcessAllocator > _allocator_ptr;
70+
5671public:
5772 CLASS_MOVE_ONLY (Process)
5873#ifdef _WIN32
5974 DWORD pid;
6075#endif
61- [[nodiscard]] std::optional<std::vector<std::uint8_t >>
62- read (void *addr, size_t size) const ;
6376
64- void *read (void *dest, void *addr, size_t size) const ;
77+ // Memory APIs
78+ std::expected<void , std::string> try_read (void *dst, void *src,
79+ size_t size) const ;
80+ void read (void *dst, void *src, size_t size) const ;
81+
82+ std::expected<void , std::string> try_write (void *dst, const void *src,
83+ size_t size) const ;
84+ void write (void *dst, const void *src, size_t size) const ;
85+
86+ std::expected<bool , std::string> try_check_readable (void *addr,
87+ size_t size) const ;
88+ bool check_readable (void *addr, size_t size) const ;
89+
90+ std::expected<bool , std::string> try_check_writable (void *addr,
91+ size_t size) const ;
92+ bool check_writable (void *addr, size_t size) const ;
93+
94+ std::expected<MemoryProtection, std::string>
95+ try_set_memory_protect (void *addr, size_t size,
96+ MemoryProtection protect) const ;
97+ MemoryProtection set_memory_protect (void *addr, size_t size,
98+ MemoryProtection protect) const ;
99+
100+ std::expected<bool , std::string> try_check_valid (void *addr) const ;
101+ bool check_valid (void *addr) const ;
102+
103+ std::expected<void *, std::string>
104+ try_malloc (size_t size, MemoryProtection protect = MemoryProtection::rw,
105+ void *nearAddr = nullptr ) const ;
106+ void *malloc (size_t size, MemoryProtection protect = MemoryProtection::rw,
107+ void *nearAddr = nullptr ) const ;
65108
66- std::expected<void , std::string> write (void *addr, std::span<uint8_t >) const ;
109+ std::expected<void , std::string> try_free (void *addr, size_t size = 0 ) const ;
110+ void free (void *addr, size_t size = 0 ) const ;
67111
68112 [[nodiscard]] std::optional<std::shared_ptr<Module>>
69113 module (const std::string &name);
@@ -82,7 +126,7 @@ class Process : public std::enable_shared_from_this<Process> {
82126
83127 Pointer memo ();
84128
85- Allocator allocator ();
129+ struct ProcessAllocator & allocator ();
86130
87131 std::vector<Thread> threads ();
88132
0 commit comments