-
Notifications
You must be signed in to change notification settings - Fork 633
Expand file tree
/
Copy pathinc2add.c
More file actions
220 lines (199 loc) · 7.97 KB
/
Copy pathinc2add.c
File metadata and controls
220 lines (199 loc) · 7.97 KB
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
/* **********************************************************
* Copyright (c) 2014-2018 Google, Inc. All rights reserved.
* Copyright (c) 2002-2008 VMware, Inc. All rights reserved.
* **********************************************************/
/*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of VMware, Inc. nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL VMWARE, INC. OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
/* Code Manipulation API Sample:
* inc2add.c
*
* Performs a dynamic optimization: converts the "inc" instruction to
* "add 1" whenever worthwhile and feasible without perturbing the
* target application's behavior. Illustrates a
* microarchitecture-specific optimization best performed at runtime
* when the underlying processor is known.
*/
#include "dr_api.h"
#include "drmgr.h"
#include "drreg.h"
#ifdef WINDOWS
# define DISPLAY_STRING(msg) dr_messagebox(msg)
# define ATOMIC_INC(var) _InterlockedIncrement((volatile LONG *)(&(var)))
#else
# define DISPLAY_STRING(msg) dr_printf("%s\n", msg);
# define ATOMIC_INC(var) __asm__ __volatile__("lock incl %0" : "=m"(var) : : "memory")
#endif
static bool enable;
/* Use atomic operations to increment these to avoid the hassle of locking. */
static int num_examined, num_converted;
/* Replaces inc with add 1, dec with sub 1.
* Returns true if successful, false if not.
*/
static bool
replace_inc_with_add(void *drcontext, instr_t *inst, instrlist_t *trace);
static dr_emit_flags_t
event_instruction_change(void *drcontext, void *tag, instrlist_t *bb, bool for_trace,
bool translating);
static void
event_exit(void);
DR_EXPORT void
dr_client_main(client_id_t id, int argc, const char *argv[])
{
/* We only used drreg for liveness, not for spilling, so we need no slots. */
drreg_options_t ops = { sizeof(ops), 0 /*no slots needed*/, false };
dr_set_client_name("DynamoRIO Sample Client 'inc2add'",
"http://dynamorio.org/issues");
if (!drmgr_init() || drreg_init(&ops) != DRREG_SUCCESS)
DR_ASSERT(false);
/* Register for our events: process exit, and code transformation.
* We're changing the app's code, rather than just inserting observational
* instrumentation.
*/
drmgr_register_exit_event(event_exit);
if (!drmgr_register_bb_app2app_event(event_instruction_change, NULL))
DR_ASSERT(false);
/* Long ago, this optimization would target the Pentium 4 (identified via
* "proc_get_family() == FAMILY_PENTIUM_4"), where an add of 1 is faster
* than an inc. For illustration purposes we leave a boolean controlling it
* but we turn it on all the time in this sample and leave it for future
* work to determine whether to disable it on certain microarchitectures.
*/
enable = true;
/* Make it easy to tell by looking at the log file which client executed. */
dr_log(NULL, DR_LOG_ALL, 1, "Client 'inc2add' initializing\n");
#ifdef SHOW_RESULTS
/* Also give notification to stderr */
if (dr_is_notify_on()) {
# ifdef WINDOWS
/* Ask for best-effort printing to cmd window. Must be called at init. */
dr_enable_console_printing();
# endif
dr_fprintf(STDERR, "Client inc2add is running\n");
}
#endif
/* Initialize our global variables. */
num_examined = 0;
num_converted = 0;
}
static void
event_exit(void)
{
#ifdef SHOW_RESULTS
char msg[256];
int len;
if (enable) {
len = dr_snprintf(msg, sizeof(msg) / sizeof(msg[0]),
"converted %d out of %d inc/dec to add/sub\n", num_converted,
num_examined);
} else {
len = dr_snprintf(msg, sizeof(msg) / sizeof(msg[0]),
"decided to keep all original inc/dec\n");
}
DR_ASSERT(len > 0);
msg[sizeof(msg) / sizeof(msg[0]) - 1] = '\0';
DISPLAY_STRING(msg);
#endif /* SHOW_RESULTS */
if (!drmgr_unregister_bb_app2app_event(event_instruction_change) ||
drreg_exit() != DRREG_SUCCESS)
DR_ASSERT(false);
drmgr_exit();
}
/* Replaces inc with add 1, dec with sub 1.
* If cannot replace (eflags constraints), leaves original instruction alone.
*/
static dr_emit_flags_t
event_instruction_change(void *drcontext, void *tag, instrlist_t *bb, bool for_trace,
bool translating)
{
int opcode;
instr_t *instr, *next_instr;
/* Only bother replacing for hot code, i.e., when for_trace is true, and
* when the underlying microarchitecture calls for it.
*/
if (!for_trace || !enable)
return DR_EMIT_DEFAULT;
for (instr = instrlist_first_app(bb); instr != NULL; instr = next_instr) {
/* We're deleting some instrs, so get the next first. */
next_instr = instr_get_next_app(instr);
opcode = instr_get_opcode(instr);
if (opcode == OP_inc || opcode == OP_dec) {
if (!translating)
ATOMIC_INC(num_examined);
if (replace_inc_with_add(drcontext, instr, bb)) {
if (!translating)
ATOMIC_INC(num_converted);
}
}
}
return DR_EMIT_DEFAULT;
}
/* Replaces inc with add 1, dec with sub 1.
* Returns true if successful, false if not.
*/
static bool
replace_inc_with_add(void *drcontext, instr_t *instr, instrlist_t *bb)
{
instr_t *new_instr;
uint eflags;
int opcode = instr_get_opcode(instr);
DR_ASSERT(opcode == OP_inc || opcode == OP_dec);
#ifdef VERBOSE
dr_print_instr(drcontext, STDOUT, instr, "in replace_inc_with_add:\n\t");
#endif
/* Add/sub writes CF, inc/dec does not, so we make sure that's ok.
* We use drreg's liveness analysis, which includes the rest of this block.
* To be more sophisticated, we could examine instructions at target of each
* direct exit instead of assuming CF is live across any branch.
*/
if (drreg_aflags_liveness(drcontext, instr, &eflags) != DRREG_SUCCESS ||
(eflags & EFLAGS_READ_CF) != 0) {
#ifdef VERBOSE
dr_printf("\tCF is live, cannot replace inc with add ");
#endif
return false;
}
if (opcode == OP_inc) {
#ifdef VERBOSE
dr_printf("\treplacing inc with add\n");
#endif
new_instr =
INSTR_CREATE_add(drcontext, instr_get_dst(instr, 0), OPND_CREATE_INT8(1));
} else {
#ifdef VERBOSE
dr_printf("\treplacing dec with sub\n");
#endif
new_instr =
INSTR_CREATE_sub(drcontext, instr_get_dst(instr, 0), OPND_CREATE_INT8(1));
}
if (instr_get_prefix_flag(instr, PREFIX_LOCK))
instr_set_prefix_flag(new_instr, PREFIX_LOCK);
instr_set_translation(new_instr, instr_get_app_pc(instr));
instrlist_replace(bb, instr, new_instr);
instr_destroy(drcontext, instr);
return true;
}