|
| 1 | +// 层:内核层 / il2cpp —— 直调实现(保护边界复用 il2cpp/Guard) |
| 2 | +#include "il2cpp/CallFrame.h" |
| 3 | + |
| 4 | +#include <cstdio> |
| 5 | +#include <cstring> |
| 6 | +#include <string> |
| 7 | + |
| 8 | +#include "il2cpp/Domain.h" |
| 9 | +#include "il2cpp/Guard.h" |
| 10 | + |
| 11 | +namespace pylon::il2cpp { |
| 12 | + |
| 13 | +namespace { |
| 14 | + |
| 15 | +thread_local std::string t_error; |
| 16 | +thread_local char t_error_c[512] = {0}; |
| 17 | + |
| 18 | +void SetError(const std::string& text) { |
| 19 | + t_error = text; |
| 20 | + std::snprintf(t_error_c, sizeof(t_error_c), "%s", t_error.c_str()); |
| 21 | +} |
| 22 | + |
| 23 | +#if defined(__aarch64__) |
| 24 | +extern "C" void pylon_call_frame(void* fn, CallFrame* frame); |
| 25 | +#endif |
| 26 | + |
| 27 | +struct Pending { |
| 28 | + void* fn; |
| 29 | + CallFrame* frame; |
| 30 | +}; |
| 31 | + |
| 32 | +// AAPCS64: x0-x7、d0-d7、8 个栈槽。 |
| 33 | +#define PYLON_CS_PARAMS \ |
| 34 | + intptr_t, intptr_t, intptr_t, intptr_t, intptr_t, intptr_t, intptr_t, \ |
| 35 | + intptr_t, double, double, double, double, double, double, double, double, \ |
| 36 | + intptr_t, intptr_t, intptr_t, intptr_t, intptr_t, intptr_t, intptr_t, \ |
| 37 | + intptr_t |
| 38 | + |
| 39 | +struct S16 { uint64_t a, b; }; |
| 40 | +struct H1F { float a; }; |
| 41 | +struct H2F { float a, b; }; |
| 42 | +struct H3F { float a, b, c; }; |
| 43 | +struct H4F { float a, b, c, d; }; |
| 44 | +struct H1D { double a; }; |
| 45 | +struct H2D { double a, b; }; |
| 46 | +struct H3D { double a, b, c; }; |
| 47 | +struct H4D { double a, b, c, d; }; |
| 48 | +struct Big { uint8_t b[kMaxIndirectReturn]; }; |
| 49 | + |
| 50 | +/// 在保护边界内跑:只装寄存器、跳转、拷返回物,不能有析构。 |
| 51 | +void Run(void* p) { |
| 52 | + const Pending& c = *static_cast<const Pending*>(p); |
| 53 | +#if defined(__aarch64__) |
| 54 | + pylon_call_frame(c.fn, c.frame); |
| 55 | +#else |
| 56 | + CallFrame& f = *c.frame; |
| 57 | + const intptr_t* x = f.x; |
| 58 | + double d[8]; |
| 59 | + std::memcpy(d, f.d, sizeof(d)); |
| 60 | + |
| 61 | +#define PYLON_CS_ARGS x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7], \ |
| 62 | + d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], \ |
| 63 | + x[8], x[9], x[10], x[11], x[12], x[13], x[14], x[15] |
| 64 | +#define PYLON_CS_CALL(T) \ |
| 65 | + do { \ |
| 66 | + T r = reinterpret_cast<T (*)(PYLON_CS_PARAMS)>(c.fn)(PYLON_CS_ARGS); \ |
| 67 | + std::memcpy(f.out, &r, sizeof(r)); \ |
| 68 | + } while (0) |
| 69 | + |
| 70 | + switch (static_cast<RetKind>(f.ret)) { |
| 71 | + case RetKind::Int: PYLON_CS_CALL(intptr_t); break; |
| 72 | + case RetKind::F32: PYLON_CS_CALL(float); break; |
| 73 | + case RetKind::F64: PYLON_CS_CALL(double); break; |
| 74 | + case RetKind::S16: PYLON_CS_CALL(S16); break; |
| 75 | + case RetKind::H1F: PYLON_CS_CALL(H1F); break; |
| 76 | + case RetKind::H2F: PYLON_CS_CALL(H2F); break; |
| 77 | + case RetKind::H3F: PYLON_CS_CALL(H3F); break; |
| 78 | + case RetKind::H4F: PYLON_CS_CALL(H4F); break; |
| 79 | + case RetKind::H1D: PYLON_CS_CALL(H1D); break; |
| 80 | + case RetKind::H2D: PYLON_CS_CALL(H2D); break; |
| 81 | + case RetKind::H3D: PYLON_CS_CALL(H3D); break; |
| 82 | + case RetKind::H4D: PYLON_CS_CALL(H4D); break; |
| 83 | + case RetKind::Indirect: { |
| 84 | + Big r = reinterpret_cast<Big (*)(PYLON_CS_PARAMS)>(c.fn)(PYLON_CS_ARGS); |
| 85 | + std::memcpy(f.indirect, &r, static_cast<size_t>(f.retSize)); |
| 86 | + break; |
| 87 | + } |
| 88 | + } |
| 89 | +#undef PYLON_CS_CALL |
| 90 | +#undef PYLON_CS_ARGS |
| 91 | +#endif |
| 92 | +} |
| 93 | + |
| 94 | +} // namespace |
| 95 | + |
| 96 | +} // namespace pylon::il2cpp |
| 97 | + |
| 98 | +extern "C" __attribute__((visibility("default"))) int32_t PylonMod_CallFrame(void* fn, void* framePtr) { |
| 99 | + using namespace pylon::il2cpp; |
| 100 | + t_error_c[0] = '\0'; |
| 101 | + if (fn == nullptr) { |
| 102 | + SetError("target 为空"); |
| 103 | + return 0; |
| 104 | + } |
| 105 | + if (framePtr == nullptr) { |
| 106 | + SetError("frame 为空"); |
| 107 | + return 0; |
| 108 | + } |
| 109 | + CallFrame& frame = *static_cast<CallFrame*>(framePtr); |
| 110 | + frame.exc = nullptr; |
| 111 | + const auto kind = static_cast<RetKind>(frame.ret); |
| 112 | + if (frame.ret < 0 || frame.ret > static_cast<int32_t>(RetKind::Indirect)) { |
| 113 | + SetError("RetKind 非法"); |
| 114 | + return 0; |
| 115 | + } |
| 116 | + if (kind == RetKind::Indirect && |
| 117 | + (frame.retSize <= 0 || frame.retSize > kMaxIndirectReturn || frame.indirect == nullptr)) { |
| 118 | + SetError("间接返回参数非法"); |
| 119 | + return 0; |
| 120 | + } |
| 121 | + |
| 122 | + if (!Domain::Attached()) { |
| 123 | + const auto attached = Domain::Attach(); |
| 124 | + if (!attached) { |
| 125 | + SetError("未 attach 到 IL2CPP 域:" + attached.reason); |
| 126 | + return 0; |
| 127 | + } |
| 128 | + } |
| 129 | + if (!Domain::OnGameThread()) { |
| 130 | + SetError(Domain::RequireGameThread("直调游戏方法").reason); |
| 131 | + return 0; |
| 132 | + } |
| 133 | + // methodPointer 包装可能转换参数/返回 ABI,不能跳过包装直调内部 icall。 |
| 134 | + Pending pending{fn, &frame}; |
| 135 | + Caught caught; |
| 136 | + if (Guard::Run(Run, &pending, &caught)) { |
| 137 | + t_error_c[0] = '\0'; |
| 138 | + return 1; |
| 139 | + } |
| 140 | + |
| 141 | + frame.exc = caught.exception; |
| 142 | + SetError(Describe(caught)); |
| 143 | + return 0; |
| 144 | +} |
| 145 | + |
| 146 | +extern "C" __attribute__((visibility("default"))) const char* PylonMod_CallFrameError() { |
| 147 | + return pylon::il2cpp::t_error_c; |
| 148 | +} |
0 commit comments