plugify  1.0.0.0
call.hpp
1 #pragma once
2 
3 #include <asmjit/asmjit.h>
4 #include <plugify/mem_addr.hpp>
5 #include <plugify/method.hpp>
6 #include <string_view>
7 #include <utility>
8 #include <memory>
9 #include <vector>
10 
11 namespace plugify {
21  class JitCall {
22  public:
27  explicit JitCall(std::weak_ptr<asmjit::JitRuntime> rt);
28 
33  JitCall(JitCall&& other) noexcept;
34 
39 
44  struct Parameters {
45  typedef const uint64_t* Data;
46 
51  explicit Parameters(uint8_t count) {
52  arguments.reserve(count);
53  }
54 
61  template<typename T>
62  void AddArgument(T val) {
63  uint64_t& arg = arguments.emplace_back(0);
64  *(T*) &arg = val;
65  }
66 
71  Data GetDataPtr() const noexcept {
72  return arguments.data();
73  }
74 
75  private:
76  std::vector<uint64_t> arguments;
77  };
78 
79  struct Return {
84  template<typename T, typename...Args>
85  void ConstructAt(Args&&... args) const noexcept {
86  std::construct_at((T*) GetReturnPtr(), std::forward<Args>(args)...);
87  }
88 
95  template<typename T>
96  void SetReturn(T val) const noexcept {
97  *(T*) GetReturnPtr() = val;
98  }
99 
105  template<typename T>
106  T GetReturn() const noexcept {
107  return *(T*) GetReturnPtr();
108  }
109 
114  int8_t* GetReturnPtr() const noexcept {
115  return (int8_t*) &ret[0];
116  }
117 
118  private:
119  volatile uint64_t ret[2]{};
120  };
121 
122  enum class WaitType {
123  None,
124  Breakpoint,
125  Wait_Keypress
126  };
127 
128  using CallingFunc = void(*)(Parameters::Data params, const Return*); // Return can be null
129  using HiddenParam = bool(*)(ValueType);
130 
139  MemAddr GetJitFunc(const asmjit::FuncSignature& sig, MemAddr target, WaitType waitType, bool hidden);
140 
149  MemAddr GetJitFunc(MethodRef method, MemAddr target, WaitType waitType = WaitType::None, HiddenParam hidden = &ValueUtils::IsHiddenParam);
150 
156  MemAddr GetFunction() const noexcept { return _function; }
157 
164  MemAddr GetTargetFunc() const noexcept { return _targetFunc; }
165 
170  std::string_view GetError() noexcept { return !_function && _errorCode ? _errorCode : ""; }
171 
172  private:
173  std::weak_ptr<asmjit::JitRuntime> _rt;
174  MemAddr _function;
175  union {
176  MemAddr _targetFunc;
177  const char* _errorCode{};
178  };
179  };
180 } // namespace plugify
Class encapsulates architecture-, OS- and compiler-specific function call semantics in a virtual "bin...
Definition: call.hpp:21
MemAddr GetJitFunc(MethodRef method, MemAddr target, WaitType waitType=WaitType::None, HiddenParam hidden=&ValueUtils::IsHiddenParam)
Get a dynamically created function based on the method reference.
JitCall(JitCall &&other) noexcept
Move constructor.
MemAddr GetTargetFunc() const noexcept
Get the target associated with the object.
Definition: call.hpp:164
MemAddr GetFunction() const noexcept
Get a dynamically created function.
Definition: call.hpp:156
MemAddr GetJitFunc(const asmjit::FuncSignature &sig, MemAddr target, WaitType waitType, bool hidden)
Get a dynamically created function based on the raw signature.
JitCall(std::weak_ptr< asmjit::JitRuntime > rt)
Constructor.
~JitCall()
Destructor.
std::string_view GetError() noexcept
Get the error message, if any.
Definition: call.hpp:170
A wrapper class for memory addresses, providing utility functions for pointer manipulation.
Definition: mem_addr.hpp:11
A reference class for the Method structure.
Definition: method.hpp:118
constexpr bool IsHiddenParam(ValueType type) noexcept
Checks if a given ValueType is considered a hidden object parameter.
Definition: value_type.hpp:379
Structure to represent function parameters.
Definition: call.hpp:44
Data GetDataPtr() const noexcept
Get a pointer to the argument storage.
Definition: call.hpp:71
void AddArgument(T val)
Set the value of the argument at next available position.
Definition: call.hpp:62
Parameters(uint8_t count)
Constructor.
Definition: call.hpp:51
void SetReturn(T val) const noexcept
Set the return value.
Definition: call.hpp:96
T GetReturn() const noexcept
Get the value of the return.
Definition: call.hpp:106
void ConstructAt(Args &&... args) const noexcept
Constructs an object of type T at the memory location.
Definition: call.hpp:85
int8_t * GetReturnPtr() const noexcept
Get a pointer to the return value.
Definition: call.hpp:114