-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhack.h
180 lines (156 loc) · 7.34 KB
/
hack.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <dlfcn.h>
#include <openssl/ssl.h>
#include <cuda.h>
#include <cuda_runtime.h>
#define HOOK_C_API extern "C"
#define HOOK_DECL_EXPORT __attribute__((visibility("default")))
static inline void *get_symbol_cuda(const char *name)
{
auto handle = dlopen("/usr/local/cuda/lib64/libcudart.so.12", RTLD_NOW | RTLD_LOCAL);
return dlsym(handle, name);
}
static inline cudaError_t real_cudaMemcpyAsync(void *dst, const void *src, size_t count,
enum cudaMemcpyKind kind, cudaStream_t stream)
{
using func_ptr = cudaError_t (*)(void *, const void *, size_t, enum cudaMemcpyKind, cudaStream_t);
static auto func_entry = reinterpret_cast<func_ptr>(get_symbol_cuda("cudaMemcpyAsync"));
return func_entry(dst, src, count, kind, stream);
}
static inline cudaError_t real_cudaMemcpy(void *dst, const void *src, size_t count,
enum cudaMemcpyKind kind)
{
using func_ptr = cudaError_t (*)(void *, const void *, size_t, enum cudaMemcpyKind);
static auto func_entry = reinterpret_cast<func_ptr>(get_symbol_cuda("cudaMemcpy"));
return func_entry(dst, src, count, kind);
}
static inline cudaError_t real_cudaGetLastError()
{
using func_ptr = cudaError_t (*)(void);
static auto func_entry = reinterpret_cast<func_ptr>(get_symbol_cuda("cudaGetLastError"));
return func_entry();
}
static inline cudaError_t real_cudaMalloc(void **devPtr, size_t size)
{
using func_ptr = cudaError_t (*)(void **devPtr, size_t size);
static auto func_entry = reinterpret_cast<func_ptr>(get_symbol_cuda("cudaMalloc"));
return func_entry(devPtr, size);
}
static inline cudaError_t real_cudaMallocHost(void **devPtr, size_t size)
{
using func_ptr = cudaError_t (*)(void **devPtr, size_t size);
static auto func_entry = reinterpret_cast<func_ptr>(get_symbol_cuda("cudaMallocHost"));
return func_entry(devPtr, size);
}
static inline cudaError_t real_cudaFree(void *devPtr)
{
using func_ptr = cudaError_t (*)(void *devPtr);
static auto func_entry = reinterpret_cast<func_ptr>(get_symbol_cuda("cudaFree"));
return func_entry(devPtr);
}
static inline cudaError_t real_cudaFreeHost(void *ptr)
{
using func_ptr = cudaError_t (*)(void *ptr);
static auto func_entry = reinterpret_cast<func_ptr>(get_symbol_cuda("cudaFreeHost"));
return func_entry(ptr);
}
static inline cudaError_t real_cudaMemsetAsync(void *devPtr, int value, size_t count, cudaStream_t stream)
{
using func_ptr = cudaError_t (*)(void *, int, size_t, cudaStream_t);
static auto func_entry = reinterpret_cast<func_ptr>(get_symbol_cuda("cudaMemsetAsync"));
return func_entry(devPtr, value, count, stream);
}
static inline cudaError_t real_cudaStreamSynchronize(cudaStream_t stream)
{
using func_ptr = cudaError_t (*)(cudaStream_t);
static auto func_entry = reinterpret_cast<func_ptr>(get_symbol_cuda("cudaStreamSynchronize"));
return func_entry(stream);
}
static inline cudaError_t real_cudaStreamCreate(cudaStream_t* pstream)
{
using func_ptr = cudaError_t (*)(cudaStream_t*);
static auto func_entry = reinterpret_cast<func_ptr>(get_symbol_cuda("cudaStreamCreate"));
return func_entry(pstream);
}
static inline cudaError_t real_cudaStreamCreateWithFlags(cudaStream_t* pstream, unsigned int flags)
{
using func_ptr = cudaError_t (*)(cudaStream_t*, unsigned int);
static auto func_entry = reinterpret_cast<func_ptr>(get_symbol_cuda("cudaStreamCreateWithFlags"));
return func_entry(pstream, flags);
}
static inline cudaError_t real_cudaDeviceSynchronize()
{
using func_ptr = cudaError_t (*)(void);
static auto func_entry = reinterpret_cast<func_ptr>(get_symbol_cuda("cudaDeviceSynchronize"));
return func_entry();
}
static inline cudaError_t real_cudaStreamWaitEvent(cudaStream_t stream, cudaEvent_t event, unsigned int flags)
{
using func_ptr = cudaError_t (*)(cudaStream_t stream, cudaEvent_t event, unsigned int flags);
static auto func_entry = reinterpret_cast<func_ptr>(get_symbol_cuda("cudaStreamWaitEvent"));
return func_entry(stream, event, flags);
}
static inline void *get_symbol_openssl(const char *name)
{
auto handle = dlopen("/root/anaconda3/envs/vllm/lib/libcrypto.so.4", RTLD_NOW | RTLD_LOCAL);
return dlsym(handle, name);
}
static inline EVP_CIPHER_CTX *real_EVP_CIPHER_CTX_new()
{
using func_ptr = EVP_CIPHER_CTX *(*)(void);
static auto func_entry = reinterpret_cast<func_ptr>(get_symbol_openssl("EVP_CIPHER_CTX_new"));
return func_entry();
}
static inline int real_EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
const unsigned char *in, int inl)
{
using func_ptr = int (*)(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
const unsigned char *in, int inl);
static auto func_entry = reinterpret_cast<func_ptr>(get_symbol_openssl("EVP_EncryptUpdate"));
return func_entry(ctx, out, outl, in, inl);
}
static inline int real_EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
ENGINE *impl, const unsigned char *key,
const unsigned char *iv)
{
using func_ptr = int (*)(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
ENGINE *impl, const unsigned char *key,
const unsigned char *iv);
static auto func_entry = reinterpret_cast<func_ptr>(get_symbol_openssl("EVP_EncryptInit_ex"));
return func_entry(ctx, cipher, impl, key, iv);
}
static inline int real_EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
{
using func_ptr = int (*)(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);
static auto func_entry = reinterpret_cast<func_ptr>(get_symbol_openssl("EVP_EncryptFinal_ex"));
return func_entry(ctx, out, outl);
}
static inline int real_EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
{
using func_ptr = int (*)(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
static auto func_entry = reinterpret_cast<func_ptr>(get_symbol_openssl("EVP_CIPHER_CTX_ctrl"));
return func_entry(ctx, type, arg, ptr);
}
static inline int real_EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
const unsigned char *in, int inl)
{
using func_ptr = int (*)(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
const unsigned char *in, int inl);
static auto func_entry = reinterpret_cast<func_ptr>(get_symbol_openssl("EVP_DecryptUpdate"));
return func_entry(ctx, out, outl, in, inl);
}
static inline int real_EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
ENGINE *impl, const unsigned char *key,
const unsigned char *iv)
{
using func_ptr = int (*)(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
ENGINE *impl, const unsigned char *key,
const unsigned char *iv);
static auto func_entry = reinterpret_cast<func_ptr>(get_symbol_openssl("EVP_DecryptInit_ex"));
return func_entry(ctx, cipher, impl, key, iv);
}
static inline int real_EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
{
using func_ptr = int (*)(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);
static auto func_entry = reinterpret_cast<func_ptr>(get_symbol_openssl("EVP_DecryptFinal_ex"));
return func_entry(ctx, out, outl);
}