Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
vmprof-python/src/trampoline.c at master · cfbolz/vmprof-python · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
cfbolz
/
vmprof-python
Public
forked from
vmprof/vmprof-python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
vmprof-python
/
src
/
trampoline.c
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
243 lines (212 loc) · 6.56 KB
master
Breadcrumbs
vmprof-python
/
src
/
trampoline.c
Copy path
Top
File metadata and controls
Code
Blame
243 lines (212 loc) · 6.56 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include
"trampoline.h"
#include
"vmprof.h"
#include
"machine.h"
#define
_GNU_SOURCE
1
#include
<string.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<sys/mman.h>
#include
<dlfcn.h>
#include
<errno.h>
#include
<stdint.h>
#include
<unistd.h>
#if
__APPLE__
#include
<mach-o/dyld.h>
#endif
#define
PAGE_ALIGNED
(
a
,
size
) (void*)(((uintptr_t)a) & ~(size - 1))
/*
* The trampoline works the following way:
*
* `eval` is the traditional PyEval_EvalFrameEx (for 2.7)
* `page` is allocated and used as memory block to execute
* the first few instructions from eval
* `vmprof_eval` is a function just saving the
* frame in rbx
*
* +--- eval ----------+
* +----| jmp vmprof_eval | <-- patched, original bits moved to page
* | +->| asm instr 1 |
* | | | asm instr 2 |
* | | | ... |
* | | +-------------------+
* | |
* | | +--- page ----------+<-+
* | | | push rbp | <-- copied from PyEval_Loop
* | | | mov rsp -> rbp | |
* | | | ... | |
* | | | ... | |
* | +--| jmp eval+copied | |
* | +-------------------+ |
* | |
* +--->+--- vmprof_eval ---+ |
* | ... | |
* | push rbx | |
* | mov rdi -> rbx | <-- save the frame, custom method
* | call eval |--+
* | ... |
* | retq |
* +-------------------+
*/
static
int
g_patched
=
0
;
static
char
*
g_trampoline
=
NULL
;
// the machine code size copied over from the callee
static
int
g_trampoline_length
;
int
_jmp_to
(
char
*
a
,
uintptr_t
addr
) {
#ifdef
X86_64
// moveabsq <addr>, <reg>
a
[
0
]
=
0x48
;
// REX.W
a
[
1
]
=
0xba
;
// %rdx
a
[
2
]
=
addr
&
0xff
;
a
[
3
]
=
(
addr
>>
8
)
&
0xff
;
a
[
4
]
=
(
addr
>>
16
)
&
0xff
;
a
[
5
]
=
(
addr
>>
24
)
&
0xff
;
a
[
6
]
=
(
addr
>>
32
)
&
0xff
;
a
[
7
]
=
(
addr
>>
40
)
&
0xff
;
a
[
8
]
=
(
addr
>>
48
)
&
0xff
;
a
[
9
]
=
(
addr
>>
56
)
&
0xff
;
// jmp %edx
a
[
10
]
=
0xff
;
a
[
11
]
=
0xe2
;
return
12
;
#elif
defined(
X86_32
)
// mov <addr>, %edx
a
[
0
]
=
0xba
;
a
[
1
]
=
addr
&
0xff
;
a
[
2
]
=
(
addr
>>
8
)
&
0xff
;
a
[
3
]
=
(
addr
>>
16
)
&
0xff
;
a
[
4
]
=
(
addr
>>
24
)
&
0xff
;
// jmp %edx
a
[
5
]
=
0xff
;
a
[
6
]
=
0xe2
;
return
7
;
#endif
return
0
;
}
#ifdef
X86_32
int
patch_relative_call
(
void
*
base
,
char
*
rel_call
,
char
*
rel_call_end
,
int
bytes_after
) {
if
(
bytes_after
!=
0
) {
return
0
;
}
char
*
r
=
rel_call
+
1
;
int
off
=
r
[
0
] |
((
r
[
1
]
&
0xff
) <<
8
) |
((
r
[
2
]
&
0xff
) <<
16
) |
((
r
[
3
]
&
0xff
) <<
24
);
// instruction pointer is just after the whole instruction
intptr_t
addr
=
(
intptr_t
)
base
+
5
+
off
;
rel_call
[
0
]
=
0xb8
;
rel_call
[
1
]
=
addr
&
0xff
;
rel_call
[
2
]
=
(
addr
>>
8
)
&
0xff
;
rel_call
[
3
]
=
(
addr
>>
16
)
&
0xff
;
rel_call
[
4
]
=
(
addr
>>
24
)
&
0xff
;
// jmp %edx
rel_call
[
5
]
=
0xff
;
rel_call
[
6
]
=
0xd0
;
return
2
;
}
#endif
#include
"libudis86/udis86.h"
unsigned
int
vmp_machine_code_instr_length
(
char
*
pc
)
{
struct
ud
u
;
ud_init
(
&
u
);
ud_set_input_buffer
(
&
u
, (
uint8_t
*
)
pc
,
12
);
ud_set_mode
(
&
u
,
vmp_machine_bits
());
return
ud_decode
(
&
u
);
}
// a hilarious typo, tramp -> trump :)
int
_redirect_trampoline_and_back
(
char
*
eval
,
char
*
trump
,
char
*
vmprof_eval
) {
char
*
trump_first_byte
=
trump
;
#ifdef
X86_64
int
needed_bytes
=
12
;
#elif
defined(
X86_32
)
int
needed_bytes
=
8
;
int
relative_call_at_pos
=
-1
;
#else
# error
"platform not supported"
#endif
int
bytes
=
0
;
int
off
=
0
;
char
*
ptr
=
eval
;
// 1) copy the instructions that should be redone in the trampoline
while
(
bytes
<
needed_bytes
) {
unsigned
int
res
=
vmp_machine_code_instr_length
(
ptr
);
if
(
res
==
0
) {
fprintf
(
stderr
,
"could not determine length of instr for trampoline\n"
);
fprintf
(
stderr
,
" %x %x %x %x %x\n"
,
ptr
[
0
],
ptr
[
1
],
ptr
[
2
],
ptr
[
3
],
ptr
[
4
]);
return
1
;
}
#ifdef
X86_32
if
(
ptr
[
0
]
==
'\xe8'
) {
// occur on 32bit linux
relative_call_at_pos
=
bytes
;
}
#endif
bytes
+=
res
;
ptr
+=
res
;
}
g_trampoline_length
=
bytes
;
// 2) initiate the first few instructions of the eval loop
{
(
void
)
memcpy
(
trump
,
eval
,
bytes
);
#ifdef
X86_32
if
(
relative_call_at_pos
!=
-1
) {
off
=
patch_relative_call
(
eval
+
relative_call_at_pos
,
trump
+
relative_call_at_pos
,
trump
+
relative_call_at_pos
+
5
,
bytes
-
relative_call_at_pos
-
5
);
}
#endif
_jmp_to
(
trump
+
bytes
+
off
, (
uintptr_t
)
eval
+
bytes
);
}
// 3) overwrite the first few bytes of callee to jump to tramp
// callee must call back
_jmp_to
(
eval
, (
uintptr_t
)
vmprof_eval
);
return
0
;
}
int
vmp_patch_callee_trampoline
(
void
*
callee_addr
,
void
*
vmprof_eval
,
void
*
*
vmprof_eval_target
)
{
int
result
;
int
pagesize
;
if
(
g_trampoline
!=
NULL
) {
//fprintf(stderr, "trampoline already patched\n");
return
0
;
// already patched
}
pagesize
=
sysconf
(
_SC_PAGESIZE
);
errno
=
0
;
result
=
mprotect
(
PAGE_ALIGNED
(
callee_addr
,
pagesize
),
pagesize
*
2
,
PROT_READ
|
PROT_WRITE
);
if
(
result
!=
0
) {
fprintf
(
stderr
,
"read|write protecting callee_addr\n"
);
return
-1
;
}
// create a new page and set it all of it writable
char
*
page
=
(
char
*
)
mmap
(
NULL
,
pagesize
,
PROT_READ
|
PROT_WRITE
|
PROT_EXEC
,
MAP_ANON
|
MAP_PRIVATE
,
0
,
0
);
if
(
page
==
NULL
) {
fprintf
(
stderr
,
"could not allocate page for trampoline\n"
);
return
-1
;
}
char
*
a
=
(
char
*
)
callee_addr
;
if
(
_redirect_trampoline_and_back
(
a
,
page
,
vmprof_eval
)
!=
0
) {
fprintf
(
stderr
,
"could not redirect eval->vmprof_eval->trampoline->eval+off\n"
);
return
-1
;
}
result
=
mprotect
(
PAGE_ALIGNED
(
callee_addr
,
pagesize
),
pagesize
*
2
,
PROT_READ
|
PROT_EXEC
);
if
(
result
!=
0
) {
fprintf
(
stderr
,
"read|exec protecting callee addr\n"
);
return
-1
;
}
// revert, the page should not be writable any more now!
result
=
mprotect
((
void
*
)
page
,
pagesize
,
PROT_READ
|
PROT_EXEC
);
if
(
result
!=
0
) {
fprintf
(
stderr
,
"read|exec protecting tramp\n"
);
return
-1
;
}
g_trampoline
=
page
;
*
vmprof_eval_target
=
page
;
return
0
;
}
int
vmp_unpatch_callee_trampoline
(
void
*
callee_addr
)
{
return
0
;
// currently the trampoline is not removed
}
You can’t perform that action at this time.