Arion 1.0.2-alpha
A high-performance C++ framework for emulating executable binaries.
 
Loading...
Searching...
No Matches
threading_manager.hpp
Go to the documentation of this file.
1#ifndef ARION_THREADING_MANAGER_HPP
2#define ARION_THREADING_MANAGER_HPP
3
5#include <arion/unicorn/unicorn.h>
6#include <map>
7#include <memory>
8#include <stack>
9#include <vector>
10
11namespace arion
12{
13
14class Arion;
15
44/*
45 * Serializes an ARION_FUTEX instance into a vector of bytes.
46 * @param[in] arion_f The ARION_FUTEX to be serialized.
47 * @return The serialized vector of bytes.
48 */
49std::vector<BYTE> serialize_arion_futex(ARION_FUTEX *arion_f);
50/*
51 * Deserializes an ARION_FUTEX instance from a vector of bytes.
52 * @param[in] srz_futex The serialized vector of bytes.
53 * @return The deserialized ARION_FUTEX.
54 */
55ARION_FUTEX *deserialize_arion_futex(std::vector<BYTE> srz_futex);
56
59{
61 pid_t tid;
63 pid_t tgid;
67 uint64_t flags;
76 std::unique_ptr<std::map<REG, RVAL>> regs_state = nullptr;
80 ADDR wait_status_addr = 0;
82 bool stopped = false;
84 ADDR robust_list_head = 0;
86 ADDR rseq_addr = 0;
88 uint32_t rseq_len = 0;
90 uint32_t rseq_sig = 0;
109 ARION_THREAD(int exit_signal, uint64_t flags, ADDR child_cleartid_addr, ADDR child_settid_addr,
110 ADDR parent_tid_addr, std::unique_ptr<std::map<REG, RVAL>> regs_state, ADDR tls_addr)
111 : exit_signal(exit_signal), flags(flags), child_cleartid_addr(child_cleartid_addr),
112 child_settid_addr(child_settid_addr), parent_tid_addr(parent_tid_addr), regs_state(std::move(regs_state)),
113 tls_addr(tls_addr) {};
119 : tid(arion_t->tid), tgid(arion_t->tgid), exit_signal(arion_t->exit_signal), flags(arion_t->flags),
120 child_cleartid_addr(arion_t->child_cleartid_addr), child_settid_addr(arion_t->child_settid_addr),
121 parent_tid_addr(arion_t->parent_tid_addr),
122 regs_state(arion_t->regs_state ? std::make_unique<std::map<REG, RVAL>>(*arion_t->regs_state) : nullptr),
123 tls_addr(arion_t->tls_addr), wait_status_addr(arion_t->wait_status_addr), stopped(arion_t->stopped) {};
124};
125/*
126 * Serializes an ARION_THREAD instance into a vector of bytes.
127 * @param[in] arion_t The ARION_THREAD to be serialized.
128 * @return The serialized vector of bytes.
129 */
130std::vector<BYTE> serialize_arion_thread(ARION_THREAD *arion_t);
131/*
132 * Deserializes an ARION_THREAD instance from a vector of bytes.
133 * @param[in] srz_thread The serialized vector of bytes.
134 * @return The deserialized ARION_THREAD.
135 */
136ARION_THREAD *deserialize_arion_thread(std::vector<BYTE> srz_thread);
137
141{
143 pid_t tid;
145 pid_t tgid;
147 pid_t pid;
154 ARION_TGROUP_ENTRY(pid_t tid, pid_t tgid, pid_t pid) : tid(tid), tgid(tgid), pid(pid) {};
155};
156
160{
161 private:
163 std::weak_ptr<Arion> arion;
165 pid_t curr_id = 1;
167 pid_t running_tid = 1;
169 std::stack<pid_t> free_thread_ids;
174 pid_t gen_next_id();
180 void remove_thread_entry_internal(pid_t tid, bool clearing = false);
181
182 public:
184 static std::map<pid_t, std::vector<std::unique_ptr<ARION_TGROUP_ENTRY>>> thread_groups;
186 std::map<pid_t, std::unique_ptr<ARION_THREAD>> threads_map;
188 std::map<ADDR, std::vector<std::unique_ptr<ARION_FUTEX>> *> futex_list;
194 static std::unique_ptr<ThreadingManager> initialize(std::weak_ptr<Arion> arion);
199 ThreadingManager(std::weak_ptr<Arion> arion);
209 pid_t ARION_EXPORT add_thread_entry(std::unique_ptr<ARION_THREAD> thread);
229 pid_t clone_thread(uint64_t flags, ADDR new_sp, ADDR new_tls, ADDR child_tid_addr, ADDR parent_tid_addr,
230 int exit_signal);
241 pid_t fork_process(uint64_t flags, ADDR new_sp, ADDR new_tls, ADDR child_tid_addr, ADDR parent_tid_addr,
242 int exit_signal);
247 void switch_to_thread(pid_t tid);
258 void futex_wait(pid_t tid, ADDR futex_addr, uint32_t futex_bitmask);
264 void futex_wait_curr(ADDR futex_addr, uint32_t futex_bitmask);
272 bool signal_wait(pid_t target_tid, pid_t source_pid, ADDR wait_status_addr);
279 bool signal_wait_curr(pid_t source_pid, ADDR wait_status_addr);
286 size_t futex_wake(ADDR futex_addr, uint32_t futex_bitmask);
306 void set_running_tid(pid_t tid);
313 void set_tgid(pid_t tid, pid_t tgid, bool init = false);
319 void set_all_tgid(pid_t tgid, bool init = false);
320};
321
322}; // namespace arion
323
324#endif // ARION_THREADING_MANAGER_HPP
Definition threading_manager.hpp:160
void remove_thread_entry_internal(pid_t tid, bool clearing=false)
void switch_to_thread(pid_t tid)
std::map< pid_t, std::unique_ptr< ARION_THREAD > > threads_map
A map identifying an ARION_THREAD instance given its associated Thread ID.
Definition threading_manager.hpp:186
static std::unique_ptr< ThreadingManager > initialize(std::weak_ptr< Arion > arion)
void set_tgid(pid_t tid, pid_t tgid, bool init=false)
std::weak_ptr< Arion > arion
The Arion instance associated with this instance.
Definition threading_manager.hpp:163
pid_t fork_process(uint64_t flags, ADDR new_sp, ADDR new_tls, ADDR child_tid_addr, ADDR parent_tid_addr, int exit_signal)
size_t futex_wake(ADDR futex_addr, uint32_t futex_bitmask)
void futex_wait_curr(ADDR futex_addr, uint32_t futex_bitmask)
void remove_thread_entry(pid_t tid)
pid_t clone_thread(uint64_t flags, ADDR new_sp, ADDR new_tls, ADDR child_tid_addr, ADDR parent_tid_addr, int exit_signal)
void set_all_tgid(pid_t tgid, bool init=false)
std::stack< pid_t > free_thread_ids
Stack of reusable thread IDs from previously terminated threads.
Definition threading_manager.hpp:169
static std::map< pid_t, std::vector< std::unique_ptr< ARION_TGROUP_ENTRY > > > thread_groups
A map identifying a thread group given its associated Thread ID.
Definition threading_manager.hpp:184
ThreadingManager(std::weak_ptr< Arion > arion)
std::map< ADDR, std::vector< std::unique_ptr< ARION_FUTEX > > * > futex_list
A map identifying an ARION_FUTEX instance given the address of the futex.
Definition threading_manager.hpp:188
pid_t add_thread_entry(std::unique_ptr< ARION_THREAD > thread)
bool signal_wait_curr(pid_t source_pid, ADDR wait_status_addr)
bool signal_wait(pid_t target_tid, pid_t source_pid, ADDR wait_status_addr)
void set_running_tid(pid_t tid)
void futex_wait(pid_t tid, ADDR futex_addr, uint32_t futex_bitmask)
#define ARION_EXPORT
Defines which symbols should be exported from the library.
Definition global_defs.hpp:13
Definition arch_x86-64.hpp:11
uint64_t REG
Identifies a Unicorn register.
Definition global_defs.hpp:42
uint64_t ADDR
Identifies a memory address.
Definition global_defs.hpp:36
std::vector< BYTE > serialize_arion_futex(ARION_FUTEX *arion_f)
ARION_THREAD * deserialize_arion_thread(std::vector< BYTE > srz_thread)
ARION_FUTEX * deserialize_arion_futex(std::vector< BYTE > srz_futex)
std::vector< BYTE > serialize_arion_thread(ARION_THREAD *arion_t)
This structure holds data about a Fast Userspace muTEX.
Definition threading_manager.hpp:18
ARION_FUTEX(ARION_FUTEX *arion_f)
Definition threading_manager.hpp:41
uint32_t futex_bitmask
Bitmask of the futex, used to encode various states or conditions.
Definition threading_manager.hpp:22
ARION_FUTEX()
Definition threading_manager.hpp:28
ARION_FUTEX(ADDR futex_addr, uint32_t futex_bitmask, pid_t tid)
Definition threading_manager.hpp:35
ADDR futex_addr
Memory address of the futex.
Definition threading_manager.hpp:20
pid_t tid
The Thread ID associated with the futex.
Definition threading_manager.hpp:24
Definition threading_manager.hpp:141
ARION_TGROUP_ENTRY(pid_t tid, pid_t tgid, pid_t pid)
Definition threading_manager.hpp:154
pid_t tgid
The Thread Group ID (TGID) associated with the thread.
Definition threading_manager.hpp:145
pid_t pid
The Process ID (PID) associated with the thread.
Definition threading_manager.hpp:147
pid_t tid
The Thread ID (TID) of the thread.
Definition threading_manager.hpp:143
This structure holds data about a UNIX thread.
Definition threading_manager.hpp:59
ADDR child_settid_addr
The address of the child_tid variable to be set when the thread starts (used with CLONE_CHILD_SETTID)...
Definition threading_manager.hpp:71
ARION_THREAD(int exit_signal, uint64_t flags, ADDR child_cleartid_addr, ADDR child_settid_addr, ADDR parent_tid_addr, std::unique_ptr< std::map< REG, RVAL > > regs_state, ADDR tls_addr)
Definition threading_manager.hpp:109
ADDR tls_addr
The address of the thread’s thread-local storage (TLS) block.
Definition threading_manager.hpp:78
ARION_THREAD(ARION_THREAD *arion_t)
Definition threading_manager.hpp:118
pid_t tgid
The thread group ID (TGID) associated with this thread.
Definition threading_manager.hpp:63
ADDR parent_tid_addr
Definition threading_manager.hpp:74
ADDR child_cleartid_addr
The address of the child_tid variable to be cleared when the thread exits (used with CLONE_CHILD_CLEA...
Definition threading_manager.hpp:69
ARION_THREAD()
Definition threading_manager.hpp:94
int exit_signal
The signal to be sent to the parent process when the thread terminates.
Definition threading_manager.hpp:65
uint64_t flags
The clone flags used when the thread was created.
Definition threading_manager.hpp:67
pid_t tid
The thread ID (TID) associated with this thread.
Definition threading_manager.hpp:61
Identifies a size-agnostic register value.
Definition global_defs.hpp:64