forked from DynamoRIO/dynamorio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeployment.dox
More file actions
1118 lines (921 loc) · 49.5 KB
/
deployment.dox
File metadata and controls
1118 lines (921 loc) · 49.5 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ******************************************************************************
* Copyright (c) 2010-2025 Google, Inc. All rights reserved.
* Copyright (c) 2011 Massachusetts Institute of Technology All rights reserved.
* Copyright (c) 2007-2010 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.
*/
/**
***************************************************************************
\page page_build_client How to Build a Tool
To use the DynamoRIO API, a tool, or "client" of DynamoRIO, should
include the main DynamoRIO header file:
\code
#include "dr_api.h"
\endcode
The client's target operating system and architecture must
be specified by setting pre-processor defines before including the
DynamoRIO header files. The appropriate library must then be linked
with. The define choices are:
-# \p WINDOWS, \p LINUX, or (coming soon) \p MACOS
-# \p X86_32, \p X86_64, \p ARM_32, or \p ARM_64
Currently we provide a private loader for both Windows and Linux.
With private loading, clients use a separate copy of each library
from any copy used by the application.
If the private loader is deliberately disabled, for transparency reasons
(see \ref transparency), clients should be
self-contained and should not share libraries with the application.
Without the private loader, 64-bit clients must take care to try and load
themselves within reachable range of DynamoRIO's code caches by setting a
preferred base address, although this may not always be honored by the
system loader.
The DynamoRIO release supplies <a href="http://www.cmake.org">CMake</a>
configuration files to facilitate building clients with the proper
compiler and linker flags. CMake is a cross-platform build system that
generates Makefiles or other development system project files. A \p
DynamoRIOConfig.cmake configuration file, along with supporting files, is
distributed in the \p cmake/ directory.
In its \p CMakeLists.txt file, a client should first invoke a \p
find_package(DynamoRIO) command. This can optionally take a version
parameter. This adds DynamoRIO as an imported target. If found, the
client should then invoke the \p configure_DynamoRIO_client() function in
order to configure build settings. Here is an example:
\code
add_library(myclient SHARED myclient.c)
find_package(DynamoRIO)
if (NOT DynamoRIO_FOUND)
message(FATAL_ERROR "DynamoRIO package required to build")
endif(NOT DynamoRIO_FOUND)
configure_DynamoRIO_client(myclient)
\endcode
The \p samples/CMakeLists.txt file in the release package serves as another
example. The top of \p DynamoRIOConfig.cmake contains detailed
instructions as well.
If your client uses DynamoRIO extensions like drmgr or drx be sure to call
use_DynamoRIO_extension() in your client's CMakeLists.txt file.
When configuring, the \p DynamoRIO_DIR CMake variable can be passed in to
identify the directory that contains the \p DynamoRIOConfig.cmake file. For
example:
\code
mkdir ../build
cd ../build
cmake -DDynamoRIO_DIR=$DYNAMORIO_HOME/cmake ../myclient
make
\endcode
The compiler needs to be configured prior to invoking cmake. If using gcc
with a non-default target platform, the \p CFLAGS and \p CXXFLAGS
environment variables should be set prior to invoking cmake. For example,
to configure a 32-bit client when gcc's default is 64-bit:
\code
mkdir ../build
cd ../build
CFLAGS=-m32 cmake -DDynamoRIO_DIR=$DYNAMORIO_HOME/cmake ../myclient
make
\endcode
Note that \p CXXFLAGS should be set instead for a C++ client, and both should
be set when building both types of clients from the same configuration
(e.g., \p samples/CMakeLists.txt).
To improve clean call performance (see \ref sec_clean_call and \ref
op_cleancall "-opt_cleancall"), we recommend high levels of optimization
when building a client.
If a client is not using CMake, the appropriate compiler and linker flags
can be gleaned from \p DynamoRIOConfig.cmake. One method is to invoke CMake to
generate a Makefile and then build with \p VERBOSE=1. We also summarize
here the key flags required for 32-bit clients for \p gcc:
\code
gcc -fPIC -shared -lgcc -DLINUX -DX86_32 -I$DYNAMORIO_HOME/include my-client.c
\endcode
And for \p cl:
\code
cl my-client.c /I$DYNAMORIO_HOME/include /GS- /DWINDOWS /DX86_32
/link /libpath:$DYNAMORIO_HOME/bin dynamorio.lib /dll /out:my-client.dll
\endcode
For a 64-bit client with \p cl:
\code
cl my-client.c /I$DYNAMORIO_HOME/include /GS- /DWINDOWS /DX86_64
/link /libpath:$DYNAMORIO_HOME/bin dynamorio.lib /dll /out:my-client.dll
/base:0x72000000 /fixed
\endcode
For 64-bit Linux clients, setting the preferred base takes several steps.
Refer to \p DynamoRIOConfig.cmake for details.
To make clean call sequences more likely to be optimized, it is recommended
to compile the client with optimizations, \p -O2 for gcc or \p /O2 for cl.
***************************************************************************
\page page_deploy How to Run
DynamoRIO can be run from a build tree, an install tree created via
\ref sec_build_install, or a release package (see \ref sec_package). See
\ref page_building for build steps. Once
one of these layouts is available, configuration and execution of
applications under DynamoRIO is handled by a set of libraries and tools.
Helper tools such as \c drrun reside under architecture-specific
subdirectories like \c bin32/ or \c bin64/ in each layout. The relative
subdirectory is consistent, while the absolute path depends on whether
DynamoRIO is used from a build tree, install tree, or release package. On Windows, the
tools are \c drconfig.exe, \c drrun.exe, and \c drinject.exe. The
corresponding libraries (whose APIs are exposed by the tools) are \c
drconfiglib.dll and \c drinjectlib.dll with header files \c dr_config.h and
\c dr_inject.h. On Linux, the tools are named \c drconfig, \c drrun, and \c
drinject, and the libraries are \c libdrconfiglib.a and \c
libdrinjectlib.a.
When using DynamoRIO as a third-party disassembly library (see \ref
page_standalone), no deployment is needed, as DynamoRIO does not control a
target application when used as a regular library.
\section sec_deploy_launch_models Choosing a Launch Method
\c drrun launches a new process with a one-time configuration in a single step.
\c drconfig writes a persistent configuration for a named application, which
can be reused across launches.
\c drinject injects into an existing process or launches using an existing
configuration, enabling two-step workflows.
Before selecting an OS-specific workflow, ensure you have a build tree, an
install tree, or a release package available. A first successful run simply
means launching a target application under DynamoRIO control with the expected
client or default configuration active.
\section sec_win_deploy Windows Deployment
There are two methods for running a process under DynamoRIO: the one-time
configure-and-run, and the two-step separate configuration and execution.
The \c drrun.exe tool supports the first, simpler model, while the \c
drconfig.exe and \c drinject.exe tools support the second, more powerful
model. The \c drconfig.exe tool, or the corresponding the \c
drconfiglib.dll library, can also be used to \ref sec_comm "nudge" running
processes.
Configuration information is stored in files in the current user's profile
directory, which is obtained from the environment variable \c USERPROFILE.
Thus, configurations are persistent across reboots and are private to each
user. If the <tt>DYNAMORIO_CONFIGDIR</tt> environment variable is set,
its value is used instead of \c USERPROFILE.
If neither is set, a temp directory will be used when creating new
configuration files for configure-and-run execution.
DynamoRIO also supports global configurations, which are stored in
the "config" subdirectory of the directory specified by the \c
DYNAMORIO_HOME registry value in the registry key \c
\\HKLM\\SOFTWARE\\DynamoRIO\\DynamoRIO (or for 32-bit on 64-bit Windows
(WOW64) \c \\HKLM\\SOFTWARE\\Wow6432Node\\DynamoRIO\\DynamoRIO). Setting
that \c DYNAMORIO_HOME value and creating the directory it points to must
be done manually. The provided tools support reading and writing both
local and global configuration files, and automatically creating the local
directory. DynamoRIO gives local files precedence when both exist. Note
that applications that do not have a \c USEPROFILE environment variable can
be controlled using <tt>DYNAMORIO_CONFIGDIR</tt> or global configurations.
Also note that by default \c USERPROFILE is not set over cygwin ssh and
must be explicitly set in the shell startup files.
Configurations are per-process, with the basename of the process used for
identification (e.g., \c notepad.exe). One-time configuration also uses the
process id to specify that the configuration is for that process instance
only.
As an example, assume you have unpacked the DynamoRIO distribution and
your current directory is its base directory. Run \c notepad.exe with the
bbsize sample client using the following configure-and-run command:
\code
bin32/drrun.exe -c samples/bin32/bbsize.dll -- notepad
\endcode
Alternatively, you can first run \c notepad.exe, and then use \c drrun
to attach to it, but please note that attaching is an experimental
feature and is not yet as well-supported as launching a new process:
\code
bin32/drrun.exe -attach <notepad_pid> -c samples/bin32/bbsize.dll
\endcode
Then, you can also detach DynamoRIO from the process of \c notepad.exe
without affecting the normal execution of \c notepad.exe.
\code
bin32/drconfig.exe -detach <notepad_pid>
\endcode
To use system-wide injection, allowing for an application to be run
under DynamoRIO regardless of how it is invoked, configure the application
first (-syswide_on requires administrative privileges):
\code
bin32/drconfig.exe -reg notepad.exe -syswide_on -c samples/bin32/bbsize.dll
\endcode
The next time \c notepad.exe is started by the current user, it will run under
DynamoRIO with the bbsize client.
To unregister \c notepad.exe, issue the following command:
\code
bin32/drconfig.exe -unreg notepad.exe
\endcode
Invoke any of the \c drconfig.exe, \c drrun.exe, or \c drinject.exe tools
with no arguments to see the full list of options available.
By default, DynamoRIO follows into all child processes, with the parent's
settings inherited by the child if there is no configuration set up ahead
of time for the child application. To instead only
follow children that are configured (via \c drconfig.exe), use the
\ref op_children "-no_follow_children" runtime option.
To ensure a client is loaded into a child process of a different
bitwidth (i.e., a 32-bit child created by a 64-bit parent), use the \c
-c32 and \c -c64 options to \c drconfig or \c drrun, with \c -- ending
the first client's options:
bin32/drrun.exe -c32 samples/bin32/bbsize.dll -- -c64 samples/bin64/bbsize.dll -- notepad
The order matters: \c -c32 must come first.
To \ref sec_comm "nudge" all instances of \c notepad.exe running under
DynamoRIO with argument "5", use:
\code
bin32/drconfig.exe -nudge notepad.exe 0 5
\endcode
This will result in a nudge event with argument=5 delivered to the
client callback registered with dr_register_nudge_event() in all
\c notepad.exe processes running under DynamoRIO. The third argument,
0, is an ID supplied at registration which uniquely identifies the
target client (see dr_deploy.h for details).
To view 32-bit or WOW64 processes running under DynamoRIO the
\c drview.exe tool can be used. The bin64 version will display both 32-bit
and 64-bit processes and will indicate which are 32-bit. The bin32 version
will display 64-bit processes but is unable to determine whether DynamoRIO
is present.
\attention
Note that on Windows NT a reboot is required after using -syswide_on or -syswide_off.
DynamoRIO uses the
<tt>\\HKLM\\SOFTWARE\\Microsoft\\Windows\\Windows NT\\CurrentVersion\\AppInit_DLLs</tt>
key
(for 32-bit on 64-bit Windows (WOW64),
<tt>\\HKLM\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows NT\\CurrentVersion\\Windows\\AppInit_DLLs</tt>)
for -syswide_on to inject into new processes without having to directly
launch them \c drrun.exe or \c drinject.exe. For injection to work, the
registered process must statically link to user32.dll (only a few small
non-graphical windows applications don't link user32.dll). If a target
application does not link to user32.dll, DynamoRIO can still inject if the
process is launched with \c drinject.exe or if the parent process (usually
cmd.exe or explorer.exe for user launched processes) is running under
DynamoRIO. The drinject.exe tool uses the configuration information set
by \c drconfig.exe for the target application.
\attention
The -syswide_on, -syswide_off, use of global configuration files, and
nudging certain processes may require administrative privileges. On
Windows Vista or higher, if UAC is enabled, use an elevated (runas admin)
process. When using \c drconfig.exe and \c drrun.exe in these scenarios, be
sure that the cmd shell being used was started with elevated permissions.
An alternative method to run an application under DynamoRIO is the \ref
sec_startstop "app_start()/app_stop()" interface, which requires modifying
application source code.
\section sec_lin_deploy Linux Deployment
Once DynamoRIO has been unpacked, the same set of helper binaries as on
Windows provide flexibility in configuring and executing applications.
There are two methods for invoking an application under DynamoRIO:
-# Configure and launch in one step via \p drrun
-# Configure via \p drconfig and launch via \p drinject
As an example of the simpler method, the following command runs \c ls
under DynamoRIO with the bbsize sample client:
\code
% bin64/drrun -c samples/bin64/libbbsize.so -- ls
\endcode
Alternatively, you can first run the target, and then use \c drrun
to attach to it, but please note that attaching is an experimental
feature and is not yet as well-supported as launching a new process.
In particular, if the application is in the middle of a blocking syscall,
DynamoRIO will wait for that to finish. To instead force interruption of
the syscall, additionally pass -skip_syscall.
\code
% bin64/drrun -attach <target_pid> -c samples/bin64/libbbsize.so
\endcode
This attach feature requires ptrace capabilities on Linux, which can be
enabled with this command:
\code
% echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
\endcode
Then, you can also detach DynamoRIO from the target process
without affecting the normal execution of the application.
\code
% bin64/drconfig -detach <target_pid>
\endcode
Run \c drrun with no options to get a list of the options and
environment variable shortcuts it supports. To disable following across
child execve calls, use the \ref op_children "-no_follow_children" runtime
option.
Use the tools in \c bin32/ for 32-bit applications and the tools in \c
bin64/ for 64-bit applications.
The two-step method allows for greater control over child processes. The
\p drconfig tool writes a configuration file for a given application
name. DynamoRIO reads its options from the configuration file at runtime.
Once each process name is configured, the \p drinject tool can be used to
invoke the parent process. The \p drrun tool can also be used but it
creates a temporary configuration file that will override settings
requested via \p drconfig. The configuration file for each application is
stored in <tt>$DYNAMORIO_CONFIGDIR/.dynamorio/<appname>.config32</tt>
(or a \p config64 suffix for 64-bit). If <tt>DYNAMORIO_CONFIGDIR</tt> is
not set, <tt>$HOME/.dynamorio/<appname>.config32</tt> is used;
if neither is set, a temp directory will be used when creating new
configuration files for configure-and-run execution. On Android, if
neither <tt>/data/local/tmp</tt> nor the current working directory are
writable, you will need to specify a writable directory by setting the
<tt>DYNAMORIO_CONFIGDIR</tt> environment variable.
DynamoRIO also supports global configuration
files in <tt>/etc/dynamorio/<appname>.config32</tt> when a local
configuration file is not found. \p drconfig does not support directly
writing a global config file but such files can be copied from or modeled
on local files.
If a target application executes an \c execve that discards the \c HOME
environment variable, the resulting process will still run under DynamoRIO
control with the same settings as the parent process.
Use <tt>DYNAMORIO_CONFIGDIR</tt> or global configuration files to
specify separate options for such a child process.
When running scripts it is best to explicitly invoke the interpreter rather
than invoking the script directly:
\code
% bin64/drrun -- /bin/bash myscript.sh
\endcode
To \ref sec_comm "nudge" a process with pid \c targetpid running under
DynamoRIO and pass argument "5" to the nudge callback, use the \c
drnudgeunix tool:
\code
bin64/drnudgeunix -pid targetpid -client 0 5
\endcode
This will result in a nudge event with argument=5 delivered to the
client callback registered with dr_register_nudge_event() in the
target process. The 0 argument is an ID supplied at registration which
uniquely identifies the target client (see dr_deploy.h for details).
If you used the -c argument to drrun or drconfig to register the client, the
client's id defaults to 0.
An alternative method to run an application under DynamoRIO is the \ref
sec_startstop "app_start()/app_stop()" interface, which requires modifying
application source code.
\section sec_deploy_advanced Advanced / Platform-Specific Deployment
\subsection sec_android_deploy Android Deployment
Android deployment is generally the same as \ref sec_lin_deploy except for the
following differences.
For pure native applications, the default configuration file location (if
<tt>DYNAMORIO_CONFIGDIR</tt> is not explicitly set) is usually (depending
on whether <tt>$HOME</tt> happens to be writable) <tt>/data/local/tmp</tt>.
For Android applications on recent versions of Android, SELinux prevents
writing to <tt>/data/local/tmp</tt>. The application's data directory
should be pointed at in the \p TMPDIR or <tt>DYNAMORIO_CONFIGDIR</tt>
environment variables, as shown below in the example wrapper script. We
recommend using \p TMPDIR as its value will also be used by tools such as
Dr. Memory for their log files.
To launch an Android application under DynamoRIO, use a wrapper script and
point at the script via the \p logwrapper property set on your
application's name prefixed by \p <tt>wrap.</tt>. For example, if your
application's name is <tt>com.myco.appname</tt>, set the property for
<tt>wrap.com.myco.appname</tt>, truncating to 31 characters:
\code
setprop wrap.com.myco.appname "logwrapper /system/xbin/wrap.sh"
\endcode
The wrapper shell script should contain the command line prefix you wish to
use to launch your application under DynamoRIO:
\code
#!/system/bin/sh
export TMPDIR=/data/data/com.myco.appname
exec /system/xbin/dynamorio/bin32/drrun -- $@
\endcode
Be sure to place the DynamoRIO binaries and the wrapper script in an
executable location, such as <tt>/system/xbin</tt>. Alternatively, disable
SELinux via <tt>setenforce 0</tt>.
If you run into problems with configuration files being created due to
SELinux denials (look in the logs for such denials), it may be simplest to
disable SELinux via <tt>setenforce 0</tt>. We have attempted to get
everything to work without this step, but we are not able to test on all
versions or configurations of Android.
\subsection sec_qemu_deploy Running Under QEMU
To run DynamoRIO under QEMU, use the \ref op_xarch_root "-xarch_root"
option to point at the base directory of the guest system libraries.
This is the same path passed to QEMU's `-L` option:
```
$ qemu-arm -L /usr/arm-linux-gnueabihf bin32/drrun -xarch_root /usr/arm-linux-gnueabihf -- hello_world
```
Be sure to install both the `qemu-user` and `qemu-user-binfmt`
packages in order for QEMU to impose itself across an execve:
```
$ sudo apt-get install qemu-user qemu-user-binfmt
```
If using a custom build of QEMU, be sure to update the binfmt rule to
ensure the proper build is used across execve: QEMU does *not* ensure
that the same build is preserved and instead relies on the global
binfmt rule. On an Ubuntu system, edit `/usr/share/binfmts/qemu-arm`
and replace the `interpreter` path. Then run:
```
$ sudo update-binfmts --import /usr/share/binfmts/qemu-arm
```
Confirm the change took effect:
```
$ update-binfmts --find bin32/drrun
```
On Fedora, edit `/usr/lib/binfmt.d/qemu-arm-dynamic.conf` and then run:
```
$ sudo systemctl restart systemd-binfmt.service
```
\section sec_client_ops Passing Options to Clients
All of the earlier examples did not need to pass any arguments to the client.
When using the -c argument to set the client, all arguments between the client
path and the double dash are passed to the client. When using the -client
argument to drrun, the third argument following -client is passed through to
the client. For example, all these invocations of drrun pass '-op1
-op2 "value with spaces"' to the client:
\code
bin32/drrun.exe -c libmyclient.dll -op1 -op2 \"value with spaces\" -- notepad
bin32/drrun.exe -client myclient.dll 0 '-op1 -op2 "value with spaces"' -- notepad
\endcode
On Linux:
\code
bin32/drrun -c libmyclient.so -op1 -op2 \"value with spaces\" -- ls
bin32/drrun -client libmyclient.so 0 '-op1 -op2 "value with spaces"' -- ls
\endcode
When using a two-step model, the options are passed to \p drconfig:
\code
bin32/drconfig.exe -reg notepad.exe -c myclient.dll -op1 -op2 \"value with spaces\"
bin32/drconfig.exe -reg notepad.exe -client myclient.dll 0 '-op1 -op2 "value with spaces"'
\endcode
The client's options are passed directly to its \p dr_client_main()
initialization routine, in the same manner as arguments are passed to a
regular application's \p main() routine. To match standalone application
conventions, \p argv[0] is set to the client library path, with the actual
parameters starting at index 1. The client can also optionally call
dr_get_option_array() to retrieve the options passed to it. C++ clients
can use the convenience of the \ref page_droption.
Client options are not allowed to contain semicolons. Additionally, the
client option string combined with the path to the client library cannot
contain all three quote characters (\', \", \`) simultaneously.
To ensure a client is loaded into a child process of a different
bitwidth (i.e., a 32-bit child created by a 64-bit parent), use the \c
-c32 and \c -c64 options to \c drconfig or \c drrun, with \c -- ending
the first client's options:
bin32/drrun -c32 samples/bin32/bbsize.dll -- -c64 samples/bin64/bbsize.dll -- notepad
The order matters: \c -c32 must come first.
\section sec_multi_client Multiple Clients
DynamoRIO does support multiple clients. It is each client's
responsibility, however, to ensure compatibility with other clients.
DynamoRIO makes no attempt to force cooperation among clients. For
example, instruction stream modifcations made by one client are
visible to other clients. Systems employing multiple clients must
be aware of such interactions and design accordingly.
Client registration requires users to specify the \em priority of each
client. DynamoRIO calls each client's
dr_client_main() routine sequentially according to this priority. Clients
with a numerically lower priority value are called first and therefore
given the first opportunity to register callbacks (the client with
priority 0 is called first). Since DynamoRIO delivers event
callbacks sequentially, client priority and the order of event
registration is important. For a given event, the \em first
registered callback is called \em last. This scheme gives precedence
to the first registered callback since that callback is given the final
opportunity to modify the instruction stream or influence DynamoRIO's
operation.
\section sec_tool_frontend End-User Tools
A client can be packaged up with DynamoRIO to create an end-user tool. For
many tools, a separate front-end executable is not necessary, and \p drrun
is sufficient. Using \p drrun for a tool is made simpler by the \p -t
option. To use the option, first create a file in the \p tools
subdirectory of the root of the DynamoRIO installation called \p
toolname.drrun32 or \p toolname.drrun64, depending on the target
architecture. Here, \p toolname is the desired external name of the tool.
This file should contain one of the following lines, or two if
they are a pair of CLIENT32_* and CLIENT64_* specifiers:
\code
CLIENT_ABS=/absolute/path/to/client
CLIENT32_ABS=/absolute/path/to/32-bit-client
CLIENT64_ABS=/absolute/path/to/64-bit-client
\endcode
or
\code
CLIENT_REL=relative/path/to/client/from/DynamoRIO/root
CLIENT32_REL=relative/path/to/32-bit-client/from/DynamoRIO/root
CLIENT64_REL=relative/path/to/64-bit-client/from/DynamoRIO/root
\endcode
This enables \p drrun to locate the tool's client library. The 32 and
64 specifiers allow pointing at alternate-bitwidth paths for use if
the target application creates a child process of a different bitwidth.
The file can also modify the default DynamoRIO runtime options (see \ref
sec_options) via \p DR_OP= lines. Each line contains only one option string
token. For example:
\code
DR_OP=-persist
DR_OP=-persist_dir
DR_OP=c:\\path with spaces\\subdir
\endcode
Tool options can also be specified, but normally the defaults should be set
up appropriately in the client itself:
\code
TOOL_OP=-custom_op1
TOOL_OP=-custom_op2
\endcode
Lines beginning with \p # are considered comments.
When \p drrun is passed the option string <tt>-t toolname</tt>, it looks
for <tt>tools/toolname.drrun64</tt> or <tt>tools/toolname.drrun32</tt> and
reads the file to determine the client library to use and the default
DynamoRIO options. This makes for a simpler launching command, rather than
the end user needing to name the exact location of the client library. For
example, this command:
\code
bin64/drrun -t mytool -tool_option1 -tool_option2 -- myapp
\endcode
can be made to expand to this equivalent command:
\code
bin64/drrun -mytool_dr_option1 -mytool_dr_option2 -c tools/mytool/libmytool.so -tool_option1 -tool_option2 -- myapp
\endcode
For more extensive actions on launching the tool, a custom front-end
executable can be created that replaces \p drrun by using \p drinjectlib,
\p drconfiglib, and \p drfrontendlib. These three libraries facilitate
creating cross-platform tools for configuring and launching applications
under DynamoRIO. For more information about the interfaces they provide,
see their header files: dr_inject.h, dr_config.h, dr_frontend.h.
A custom front-end executable can be invoked via a \p drrun \p -t
configuration file using one of the following lines:
\code
FRONTEND_ABS=/absolute/path/to/front-end
\endcode
or
\code
FRONTEND_REL=relative/path/to/front-end/from/DynamoRIO/root
\endcode
This will cause \p drrun to transfer control to the specified front-end
executable, passing any tool arguments (including client paths, if \p
CLIENT{,32,64}_{ABS,REL} appears after the \p FRONTEND_* command)
followed by "--" and the target application command line.
The path to the DynamoRIO install base can be included in the front-end
options via this line
\code
TOOL_OP_DR_PATH
\endcode
The DynamoRIO runtime options can be included in a single token, preceded by a prefix,
via this line, using "-dr_ops" as an example prefix:
\code
TOOL_OP_DR_BUNDLE=-dr_ops
\endcode
A warning message can be presented up front to the user with:
\code
USER_NOTICE=This tool is currently experimental. Please report issues to mytool.com/issues.
\endcode
\section sec_startstop Running a Subset of an Application
An alternative to running an entire application under DynamoRIO control is to
use the Application Interface to specify a portion of the application to
run. This interface consists of the following routines:
\code
dr_app_setup()
dr_app_start()
dr_app_stop()
dr_app_cleanup()
dr_app_take_over()
dr_app_setup_and_start()
dr_app_stop_and_cleanup()
dr_app_stop_and_cleanup_with_stats()
dr_app_running_under_dynamorio()
\endcode
When building an executable that uses DynamoRIO's Application Interface,
follow the steps for \ref page_build_client to include the header files and link
with the DynamoRIO library, but omit the linker flags requesting no
standard libraries or startup files. DynamoRIO's CMake support does this
automatically, as the linker flags for shared libraries are separate from
those for executables.
\section sec_static_DR Statically Linking DynamoRIO
Limited support is provided to statically link DynamoRIO and clients
into the application itself. This generally requires the start-stop
interface (see \ref sec_startstop) in order to trigger takeover.
The configure_DynamoRIO_static and use_DynamoRIO_static_client CMake
utilities are provided to help in setting up the link steps.
One significant downside of statically linking is that DynamoRIO is
not able to provide library isolation in this mode. This means that
clients cannot safely use third-party library code at runtime. It is
considered safe to use libraries during process-wide initialization,
as that occurs in just one thread which is at a relatively safe point
having called something like dr_app_start(). The
#DR_DISALLOW_UNSAFE_STATIC and dr_allow_unsafe_static_behavior()
features are available to help detect violations of this rule, but
they only detect these violations when using DynamoRIO in its dynamic
library form. Thus, the recommendation is to support both modes and
employ regression tests which set #DR_DISALLOW_UNSAFE_STATIC in
dynamic mode in order to catch unsafe library code being added later.
***************************************************************************
\section sec_options DynamoRIO Runtime Options
DynamoRIO's behavior can be fine-tuned using runtime parameters. Options
are specified via \c drconfig, \c drrun, or dr_register_process(). See
\ref page_deploy.
- \b -no_follow_children: \anchor op_children
By default, DynamoRIO follows all child processes. When this option
is disabled via \p -no_follow_children, DynamoRIO follows only
into child processes for which a configuration file exists (typically
created by \c drconfig; see \ref page_deploy). On Linux,
forked children are always followed and this option only affects execve.
To follow all children in general but exclude certain children, leave \p
-follow_children on (the default) and create config files that exclude
the desired applications by running \c drconfig with the \c -norun
option.
- \b -xarch_root: \anchor op_xarch_root
Support for running under an emulator, in particular QEMU. This option takes
a string pointing at the base directory for guest system libraries, which DynamoRIO
uses to prefix the application executable's interpreter, client library paths,
and SYS_openat. The option also enables workarounds for problems with QEMU's
threads which would otherwise cause a hang while DynamoRIO tries to take them over.
- \b -opt_memory: \anchor op_memory
Reduce memory usage, but potentially at the cost of performance. This
option can result in memory savings as high as 20%, and usually incurs
no noticable performance degradation. However, it conflicts with the
\ref op_enable "-enable_full_api option" and cannot be used with
dr_unlink_flush_region().
- \b -opt_cleancall \e \<number\>: \anchor op_cleancall
Optimize (shrink or inline) the clean call sequences (see \ref sec_clean_call).
When DynamoRIO analyzes the callee and optimizes each clean call invocation,
it assumes that a client will not modify the clean call callee or application
instructions after the inserted clean call.
If a client changes application instructions after an inserted clean call,
the client may need to reduce the -opt_cleancall level to preserve correct
execution.
There are four optimization levels:
- 0: no optimization.
- 1: callee register usage analysis and optimization on context switch.
- 2: simple callee inline optimization, callee-save register analysis,
and aflags usage analysis on the instruction list to be inserted.
- 3: more aggressive, but potentially unsafe, optimizations.
By default, the clean call optimization level is 2.
The clean call will only be optimized if it is a leaf function.
Currently, the callee will be inlined only if it is small, has at most
one argument, and has no control flow other than for the PIC base.
Compiling the client with optimizations makes clean call sequences more likely
to be optimized.
The optimization results (e.g. whether the inserted clean call is inlined or not,
and which registers were saved on each context switch) are logged.
Users can run DynamoRIO debug build with the runtime option
"-loglevel 2 -logmask 0x02000000" (the logmask is optional but reduces the
logfile size significantly) and grep for
"CLEANCALL" in the log file to retrieve the information
about clean call optimization.
- \b -opt_speed: \anchor op_speed
By default, DynamoRIO provides a more straightforward code stream to
clients in lieu of performance optimizations. This option attempts
to obtain higher performance with potential loss of client simplicity.
In particular, unconditional branches (both jumps and calls) and in some
cases indirect calls may be elided in basic blocks. See also \ref sec_limit_perf.
Note that dr_insert_mbr_instrumentation() is not supported when -opt_speed
is specified.
- \b -stack_size \e \<number\>: \anchor op_stack_size
DynamoRIO's per-thread stack is limited to 56KB by default on systems
with a 4K page size (this may seem small, but this is much larger than its size
when no client is present). For systems with different page sizes, it defaults
to 2 * page size. This parameter can be used to increase the size;
however, larger stack sizes use significantly more memory when targeting
applications with hundreds of threads. The parameter can take a 'K'
suffix, and must be a multiple of the page size (4K, 16K, or 64K depending on
the system). This stack is used by the routines dr_insert_clean_call(),
dr_swap_to_clean_stack(), dr_prepare_for_call(),
dr_insert_call_instrumentation(), dr_insert_mbr_instrumentation(),
dr_insert_cbr_instrumentation(), and dr_insert_ubr_instrumentation().
The stack is started fresh for each use, so <em>no persistent state may be
stored on it</em>.
- \b -signal_stack_size \e \<number\>:
DynamoRIO uses an alternate signal handling stack which is smaller than
the regular stack by default, as signal handlers are called in
relatively fragile contexts and cannot safely use arbitrary library
routines: it is 32K for systems with a 4K page size. For systems with
different page sizes, it defaults to 2 * page size. This parameter can
be used to change the size. The parameter can take a 'K' suffix, and
must be a multiple of the page size (4K, 16K, or 64K depending on the
system).
- \b -thread_private: \anchor op_thread_priv
By default, DynamoRIO's code caches are shared across threads. This
option requests code caches that are private to each thread. For
applications with many threads, thread-private code caches use more
memory. However, they can be more efficient, particularly when
inserting thread-specific instrumentation.
- \b -disable_traces:
By default, DynamoRIO builds both a <em>basic block</em> code cache and
a <em>trace</em> code cache (see \ref sec_IR). This option disables
trace building, which can have a negative performance impact.
When running large, short-running applications, however, disabling
traces can improve performance.
When traces are disabled, dr_register_trace_event() has no effect.
DynamoRIO tries to keep traces transparent to a client who is
interested in all code and not only hot code, so there is rarely a
reason to disable traces.
\if internal_comment
if we expose -enable_traces, note that it must be specified BEFORE
-thread_private as today it turns on -shared_traces
\endif
- \b -enable_full_api: \anchor op_enable
DynamoRIO's default internal options balance performance with API
usability. A few API functions, such as dr_unlink_flush_region(),
are incompatible with this default mode. Client users can gain
access to the entire set of API functions with -enable_full_api.
Note that this option may result in a small performance degradation.
- \b -reachable_heap: \anchor op_reachable_heap
By default, DynamoRIO guarantees that heap allocated directly through
its API routines dr_thread_alloc(), dr_global_alloc(),
dr_nonheap_alloc(), or dr_custom_alloc() with #DR_ALLOC_CACHE_REACHABLE
is reachable by a 32-bit displacement from the code cache. However, it
does not guarantee that memory allocated through system libraries
(including malloc, operator new, and HeapAlloc) or DynamoRIO's own
internal memory is reachable. Turning this option on combines all of
the heap memory such that it is all guaranteed to be reachable from the
code cache, at the risk of running out memory due to the smaller space
of available memory.
- \b -reachable_client: \anchor op_reachable_client
By default, DynamoRIO guarantees that client libraries are reachable
from its code caches by a 32-bit displacement (except for clients
statically linked into the application). Disabling this option
removes that guarantee and allows a client to be located elsewhere.
The option is automatically turned off when DynamoRIO is statically
linked into the application.
- \b -max_bb_instrs:
DynamoRIO stops building a basic block if it hits this application
instruction count limit before hitting control flow or other block
termination conditions. The default value is 1024; lower it if
extensive client instrumentation is running into code cache size
limit asserts.
- \b -max_trace_bbs:
DynamoRIO will not build a trace with larger than this number of
constituent basic block. The default value is 128; lower it if
extensive client instrumentation is running into code cache size
limit asserts.
- \b -sysenter_is_int80: \anchor op_sysenter
This option only applies to Linux. If sysenter is the system call
gateway, DynamoRIO normally hooks the vsyscall vdso page when it can.
This option requests that DynamoRIO convert sysenter into int 0x80
instead. See \ref sec_extlibs.
- \b -disable_rseq: \anchor op_disable_rseq
This option only applies to Linux. It returns -ENOSYS from the SYS_rseq
system call, forcing applications to fall back to code that does not
use restartable sequences. See \ref sec_rseq for more information.
- \b -no_ldstex2cas: \anchor op_ldstex2cas
This option only applies to ARM and AArch64. It disables the conversion of
exclusive monitor regions to compare-and-swap operations. That conversion can
cause behavior changes in applications, but disabling it can result in failure to
add instrumentation. See \ref sec_ldrex for more information.
- \b -multi_thread_exit:
By default, DynamoRIO synchronizes with all remaining threads
at process exit time and the process exit event executes with only
one live thread. This option requests that in release build the
synchronization be avoided. The process exit event must be written
in a thread-safe manner. Note that if thread exit events are
registered, to avoid the synchronization the -skip_thread_exit_at_exit
option must also be set. These options can also be enabled
programmatically via dr_set_process_exit_behavior().
- \b -skip_thread_exit_at_exit:
By default, DynamoRIO synchronizes with all remaining threads at
process exit time in order to safely call each thread exit event. This
option requests that in release build the synchronization be avoided by
removing the invocation of thread exit events at process exit time.
Note that if the process exit event is registered, to avoid the
synchronization the -multi_thread_exit option must also be set. These
options can also be enabled programmatically via
dr_set_process_exit_behavior().
- \b -persist:\anchor op_persist
Enables persisting of code caches to disk and re-use on subsequent runs.
Caches are persisted in units that correspond to application libraries,
or sometimes smaller units. Each unit is persisted to its own file
in a subdirectory of the base directory specified by \p -persist_dir.
See \ref sec_pcache for more details.
- \b -persist_dir \e \<path\>:
Sets the base directory for persistent code cache files. If unset,
the default base directory is the log directory. A different
sub-directory will be created for each user inside the specified
directory.
- \b -translate_fpu_pc:\anchor op_translate_fpu_pc
Enables translation of the last floating-point instruction address when
the last floating-point instruction is not in the same basic block as
the instruction saving the FPU state. This is off by default as it
incurs significant performance penalties and few applications require
this feature.
\if cache_sizing
XXX: users may want control over adaptive wset cache management,
particularly for thread-private to avoid deletions, but also for shared if
they want to shrink memory usage
\endif
- \b -syntax_intel: \anchor op_syntax_intel
This option causes DynamoRIO to output all disassembly using Intel
syntax rather than the default show-implicit-operands syntax. This can also be set
using disassemble_set_syntax().
- \b -syntax_att: \anchor op_syntax_att
This option causes DynamoRIO to output all disassembly using AT&T
syntax rather than the default show-implicit-operands syntax. This can also be set
using disassemble_set_syntax().
- \b -syntax_arm: \anchor op_syntax_arm
This option causes DynamoRIO to output all disassembly using standard
ARM assembler syntax rather than the default show-implicit-operands
syntax. This can also be set using disassemble_set_syntax().
- \b -syntax_riscv: \anchor op_syntax_riscv
This option causes DynamoRIO to output all disassembly using
RISC-V assembler syntax rather than the default show-implicit-operands
syntax. This can also be set using disassemble_set_syntax().
- \b -disasm_mask:
This option sets the disassembly style to the specified bitmask of
dr_disasm_flags_t values. This option overlaps with -syntax_intel,
-syntax_att, and -syntax_arm. The style can also be set using
disassemble_set_syntax().
- \b -tracedump_text and \b -tracedump_binary:
These options cause DynamoRIO to output all traces that were created
to the log file \e traces-shared.0.TID.html, where \e
TID is the thread id of the initial thread; any thread-private traces
(see \ref op_thread_priv "-thread_private option") produce per-thread
files \e traces.TID.html.
Traces are logged whenever they are flushed from the cache (which can