forked from idapython/src
-
Notifications
You must be signed in to change notification settings - Fork 5
/
idapython.cpp
2242 lines (2031 loc) · 63.2 KB
/
idapython.cpp
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
//---------------------------------------------------------------------
// IDAPython - Python plugin for Interactive Disassembler
//
// Copyright (c) The IDAPython Team <[email protected]>
//
// All rights reserved.
//
// For detailed copyright information see the file COPYING in
// the root of the distribution archive.
//---------------------------------------------------------------------
// python.cpp - Main plugin code
//---------------------------------------------------------------------
#include <Python.h>
//-------------------------------------------------------------------------
// This define fixes the redefinition of ssize_t
#ifdef HAVE_SSIZE_T
#define _SSIZE_T_DEFINED 1
#endif
// Python defines snprintf macro so we need to allow it
#define USE_DANGEROUS_FUNCTIONS
#include <ida.hpp>
#include <idp.hpp>
#include <expr.hpp>
#include <diskio.hpp>
#include <loader.hpp>
#include <kernwin.hpp>
#include <ida_highlighter.hpp>
#if defined (PY_MAJOR_VERSION) && (PY_MAJOR_VERSION < 3)
// in Python 2.x many APIs accept char * instead of const char *
GCC_DIAG_OFF(write-strings)
#endif
#ifdef PY3
# define PYCODE_OBJECT_TO_PYEVAL_ARG(Expr) (Expr)
#else
# define PYCODE_OBJECT_TO_PYEVAL_ARG(Expr) ((PyCodeObject *) Expr)
#endif
#include "extapi.hpp"
#include "extapi.cpp"
ext_api_t extapi;
#include "pywraps.hpp"
#include "pywraps.cpp"
//-------------------------------------------------------------------------
// Defines and constants
// Python-style version tuple comes from the makefile
// Only the serial and status is set here
#define VER_SERIAL 0
#define VER_STATUS "final"
#define IDAPYTHON_RUNSTATEMENT 0
#define IDAPYTHON_ENABLE_EXTLANG 3
#define IDAPYTHON_DISABLE_EXTLANG 4
#define PYTHON_DIR_NAME "python"
#define S_IDAPYTHON "IDAPython"
#define S_INIT_PY "init.py"
static const char S_IDC_ARGS_VARNAME[] = "ARGV";
static const char S_IDC_EXEC_PYTHON[] = "exec_python";
static const char S_IDC_EVAL_PYTHON[] = "eval_python";
static const char S_IDAPYTHON_DATA_NODE[] = "IDAPython_Data";
//-------------------------------------------------------------------------
// Types
//
enum script_run_when
{
RUN_ON_DB_OPEN = 0, // run script after opening database (default)
RUN_ON_UI_READY = 1, // run script when UI is ready
RUN_ON_INIT = 2, // run script immediately on plugin load (shortly after IDA starts)
};
//-------------------------------------------------------------------------
// Global variables
static bool g_instance_initialized = false; // This instance of the plugin is the one
// that initialized the python interpreter.
static int g_run_when = -1;
static char g_run_script[QMAXPATH];
static char g_idapython_dir[QMAXPATH];
static qstring requested_plugin_path;
//-------------------------------------------------------------------------
// Prototypes and forward declarations
// // Alias to SWIG_Init
// //lint -esym(526,init_idaapi) not defined
// extern "C" void init_idaapi(void);
// Plugin run() callback
bool idaapi run(size_t);
static PyObject *get_module_globals_from_path(const char *path);
static bool idaapi IDAPython_extlang_eval_expr(
idc_value_t *rv,
ea_t /*current_ea*/,
const char *expr,
qstring *errbuf);
//lint -e818 could be pointer to const
//-------------------------------------------------------------------------
// This is a simple tracing code for debugging purposes.
// It might evolve into a tracing facility for user scripts.
//#define ENABLE_PYTHON_PROFILING
#ifdef ENABLE_PYTHON_PROFILING
#include "compile.h"
static int tracefunc(PyObject *obj, _frame *frame, int what, PyObject *arg)
{
PyObject *str;
/* Catch line change events. */
/* Print the filename and line number */
if ( what == PyTrace_LINE )
{
str = PyObject_Str(frame->f_code->co_filename);
if ( str != NULL )
{
msg("PROFILING: %s:%d\n", IDAPyStr_AsUTF8(str), frame->f_lineno);
Py_DECREF(str);
}
}
return 0;
}
#endif
//-------------------------------------------------------------------------
// Helper routines to make Python script execution breakable from IDA
static bool g_ui_ready = false;
static bool g_alert_auto_scripts = true;
static bool g_remove_cwd_sys_path = false;
static bool g_autoimport_compat_idaapi = true;
static bool g_autoimport_compat_ida695 = true;
static bool g_namespace_aware = true;
static bool g_repl_use_sys_displayhook = true;
//-------------------------------------------------------------------------
bool ida_export is_api695_compat_enabled()
{
return g_autoimport_compat_ida695;
}
//-------------------------------------------------------------------------
// Allowing the user to interrupt a script is not entirely trivial.
// Imagine the following script, that is run in an IDB that uses
// an IDAPython processor module (important!) :
// ---
// while True:
// gen_disasm_text(dtext, ea, ea + 4, False)
// ---
// This script will call the processor module's out/outop functions in
// order to generate the text. If the processor module behaves
// correctly (i.e., doesn't take forever to generate said text), if the
// user presses 'Cancel' once the wait dialog box shows, what we want
// to cancel is _this_ script above: we don't want to interrupt the
// processor module while it's doing its thing!
// In order to do that, we will have to remember the time-of-entry of
// various entry points:
// - IDAPython_extlang_compile_file
// - IDAPython_RunStatement
// - ... and more importantly in this case:
// - IDAPython_extlang_call_method (called by the IDA kernel to generate text)
//
// Of course, in case the processor module's out/outop misbehaves, we still
// want the ability to cancel that operation. The following code allows for
// that, too.
//-------------------------------------------------------------------------
struct exec_entry_t
{
time_t etime;
exec_entry_t() { reset_start_time(); }
void reset_start_time() { etime = time(NULL); }
};
DECLARE_TYPE_AS_MOVABLE(exec_entry_t);
typedef qvector<exec_entry_t> exec_entries_t;
//-------------------------------------------------------------------------
struct execution_t
{
exec_entries_t entries;
int timeout;
uint32 steps_before_action;
bool wait_box_shown;
bool interruptible_state;
execution_t()
: timeout(2),
steps_before_action(0),
wait_box_shown(false),
interruptible_state(true)
{
reset_steps();
}
void reset_steps();
void push();
void pop();
bool can_interrupt_current(time_t now) const;
void reset_current_start_time();
void stop_tracking();
void sync_to_present_time();
void maybe_hide_wait_box();
void set_interruptible(bool intr) { interruptible_state = intr; }
bool is_our_wait_box_in_charge() const;
static int on_trace(PyObject *obj, PyFrameObject *frame, int what, PyObject *arg);
};
static execution_t execution;
//-------------------------------------------------------------------------
typedef qvector<bool> idapython_wait_box_requests_t;
static idapython_wait_box_requests_t idapython_wait_box_requests;
static inline bool idapython_is_user_waitbox_shown()
{
return idapython_wait_box_requests.has(false);
}
//-------------------------------------------------------------------------
//lint -esym(683,show_wait_box) function 'show_wait_box' #define'd, semantics may be lost
//lint -esym(750,show_wait_box) local macro '' not referenced
//lint -esym(750,hide_wait_box) local macro '' not referenced
void ida_export idapython_show_wait_box(
bool internal,
const char *message)
{
idapython_wait_box_requests.push_back(internal);
#undef show_wait_box
show_wait_box("%s", message);
#define show_wait_box USE_IDAPYTHON_SHOW_WAIT_BOX
}
//-------------------------------------------------------------------------
void ida_export idapython_hide_wait_box()
{
if ( !idapython_wait_box_requests.empty() )
{
idapython_wait_box_requests.pop_back();
execution.reset_current_start_time();
}
#undef hide_wait_box
hide_wait_box();
#define hide_wait_box USE_IDAPYTHON_HIDE_WAIT_BOX
}
// #define LOG_EXEC 1
#ifdef LOG_EXEC
#define LEXEC(...) msg("IDAPython exec: " __VA_ARGS__)
#else
#define LEXEC(...)
#endif
//-------------------------------------------------------------------------
void execution_t::reset_steps()
{
// we want to trace/check the time about every 10 steps. But we don't
// want it to be exactly 10 steps, or we might never make important
// checks because the tracing happens always at the wrong point.
// E.g., imagine the following loop:
// ---
// while True:
// gen_disasm_text(dtext, ea, ea + 4, False)
// ---
// If we never hit the 'trace' callback while in the 'while True' loop
// but always when performing the call to the processor module's 'out/outop'
// then the loop will never stop. That was happening on windows (optimized.)
steps_before_action = 1 + rand() % 20;
}
//-------------------------------------------------------------------------
void execution_t::push()
{
if ( entries.empty() )
extapi.PyEval_SetTrace_ptr((Py_tracefunc) execution_t::on_trace, NULL); //lint !e611 cast between pointer to function type '' and pointer to object type 'void *'
entries.push_back();
LEXEC("push() (now: %d entries)\n", int(entries.size()));
}
//-------------------------------------------------------------------------
void execution_t::pop()
{
entries.pop_back();
if ( entries.empty() )
stop_tracking();
LEXEC("pop() (now: %d entries)\n", int(entries.size()));
}
//-------------------------------------------------------------------------
void execution_t::stop_tracking()
{
extapi.PyEval_SetTrace_ptr(NULL, NULL);
maybe_hide_wait_box();
}
//-------------------------------------------------------------------------
void execution_t::sync_to_present_time()
{
time_t now = time(NULL);
for ( size_t i = 0, n = entries.size(); i < n; ++i )
entries[i].etime = now;
maybe_hide_wait_box();
}
//-------------------------------------------------------------------------
void execution_t::maybe_hide_wait_box()
{
if ( wait_box_shown )
{
LEXEC("execution_t (%p)::maybe_hide_wait_box() hiding dialog\n", this);
idapython_hide_wait_box();
wait_box_shown = false;
}
}
//-------------------------------------------------------------------------
bool execution_t::can_interrupt_current(time_t now) const
{
LEXEC("can_interrupt_current(): nentries: %d\n", int(entries.size()));
if ( entries.empty() || timeout <= 0 || !interruptible_state )
return false;
const exec_entry_t &last = entries.back();
bool can = (now - last.etime) > timeout;
LEXEC("can_interrupt_current(): last: %d, now: %d (-> %d)\n",
int(last.etime), int(now), can);
return can;
}
//-------------------------------------------------------------------------
void execution_t::reset_current_start_time()
{
if ( !entries.empty() )
{
LEXEC("reset_current_start_time()\n");
entries.back().reset_start_time();
}
}
//------------------------------------------------------------------------
int execution_t::on_trace(PyObject *obj, PyFrameObject *frame, int what, PyObject *arg)
{
LEXEC("on_trace() (steps=%d, nentries=%d)\n",
int(execution.steps_before_action),
int(execution.entries.size()));
// we don't want to query for time at every trace event
if ( execution.steps_before_action-- > 0 )
return 0;
if ( get_active_modal_widget() != NULL )
{
LEXEC("on_trace()::a modal widget is active. Not showing our 'interrupt dialog'.\n");
return 0;
}
execution.reset_steps();
if ( idapython_is_user_waitbox_shown() )
{
LEXEC("on_trace()::a user wait dialog is currently shown. Not showing our 'interrupt dialog'.\n");
return 0;
}
time_t now = time(NULL);
LEXEC("on_trace()::now: %d\n", int(now));
if ( execution.can_interrupt_current(now) )
{
const bool user_clicked_cancel = user_cancelled();
LEXEC("on_trace()::can_interrupt. 'Interrupt dialog' shown=%d, user cancelled=%d\n",
int(execution.wait_box_shown),
int(user_clicked_cancel));
if ( execution.wait_box_shown )
{
if ( user_clicked_cancel )
{
if ( PyErr_Occurred() == NULL )
{
LEXEC("on_trace()::INTERRUPTING (setting 'User interrupted' exception) at line %d\n",
PyFrame_GetLineNumber(frame));
PyErr_SetString(PyExc_KeyboardInterrupt, "User interrupted");
}
return -1;
}
}
else
{
if ( !user_clicked_cancel )
{
LEXEC("on_trace()::showing wait dialog\n");
idapython_show_wait_box(/*internal=*/ true, "Running Python script");
execution.wait_box_shown = true;
}
else
{
LEXEC("on_trace()::not showing wait dialog; user_cancelled()\n");
}
}
}
#ifdef ENABLE_PYTHON_PROFILING
return tracefunc(obj, frame, what, arg);
#else
qnotused(obj);
qnotused(frame);
qnotused(what);
qnotused(arg);
return 0;
#endif
}
//-------------------------------------------------------------------------
void ida_export setup_new_execution(
new_execution_t *instance,
bool setup)
{
if ( setup )
{
instance->created = g_ui_ready && execution.timeout > 0;
if ( instance->created )
{
PYW_GIL_CHECK_LOCKED_SCOPE();
execution.push();
}
}
else
{
PYW_GIL_CHECK_LOCKED_SCOPE();
execution.pop();
}
}
//-------------------------------------------------------------------------
void ida_export set_interruptible_state(bool interruptible)
{
execution.set_interruptible(interruptible);
}
//-------------------------------------------------------------------------
void ida_export prepare_programmatic_plugin_load(const char *path)
{
requested_plugin_path = path;
}
//-------------------------------------------------------------------------
//lint -esym(714,disable_script_timeout) Symbol not referenced
idaman void ida_export disable_script_timeout()
{
// Clear timeout
execution.timeout = 0;
// Uninstall the trace function and hide the waitbox (if it was shown)
execution.stop_tracking();
}
//-------------------------------------------------------------------------
//lint -esym(714,set_script_timeout) Symbol not referenced
idaman int ida_export set_script_timeout(int timeout)
{
// Update the timeout
qswap(timeout, execution.timeout);
// Reset the execution time and hide the waitbox (so it is shown again after timeout elapses)
execution.sync_to_present_time();
return timeout;
}
//------------------------------------------------------------------------
// Return a formatted error or just print it to the console
static void handle_python_error(
qstring *errbuf,
bool clear_error = true)
{
if ( errbuf != NULL )
errbuf->clear();
// No exception?
if ( !PyErr_Occurred() )
return;
PyW_GetError(errbuf, clear_error);
}
//------------------------------------------------------------------------
// Note: The references are borrowed. No need to free them.
static PyObject *get_module_globals(const char *modname=NULL)
{
if ( modname == NULL || modname[0] == '\0' )
modname = S_MAIN;
PyObject *module = PyImport_AddModule(modname);
return module == NULL ? NULL : PyModule_GetDict(module);
}
//-------------------------------------------------------------------------
static ref_t _get_sys_displayhook()
{
ref_t h;
if ( g_repl_use_sys_displayhook )
{
ref_t py_sys(PyW_TryImportModule("sys"));
if ( py_sys != NULL )
h = PyW_TryGetAttrString(py_sys.o, "displayhook");
}
return h;
}
//-------------------------------------------------------------------------
static const char *bomify(qstring *out)
{
out->insert(0, UTF8_BOM, UTF8_BOM_SZ);
return out->c_str();
}
//------------------------------------------------------------------------
static void PythonEvalOrExec(
const char *str,
const char *filename = "<string>")
{
// Compile as an expression
PYW_GIL_CHECK_LOCKED_SCOPE();
qstring qstr(str);
newref_t py_code(Py_CompileString(bomify(&qstr), filename, Py_eval_input));
if ( py_code == NULL || PyErr_Occurred() )
{
// Not an expression?
PyErr_Clear();
// Run as a string
extapi.PyRun_SimpleString_ptr(str);
}
else
{
PyObject *py_globals = get_module_globals();
newref_t py_result(
PyEval_EvalCode(
PYCODE_OBJECT_TO_PYEVAL_ARG(py_code.o),
py_globals,
py_globals));
if ( py_result == NULL || PyErr_Occurred() ) //-V560 is always false: PyErr_Occurred()
{
PyErr_Print();
}
else
{
ref_t sys_displayhook(_get_sys_displayhook());
if ( sys_displayhook != NULL )
{
//lint -esym(1788, res) is referenced only by its constructor or destructor
newref_t res(PyObject_CallFunctionObjArgs(sys_displayhook.o, py_result.o, NULL));
}
else if ( py_result.o != Py_None )
{
bool ok = false;
if ( PyUnicode_Check(py_result.o) )
{
qstring utf8;
#ifdef PY3
IDAPyStr_AsUTF8(&utf8, py_result.o);
#else
newref_t py_result_utf8(PyUnicode_AsUTF8String(py_result.o));
ok = py_result_utf8 != NULL;
if ( ok )
IDAPyStr_AsUTF8(&utf8, py_result_utf8.o);
#endif
msg("%s\n", utf8.c_str());
}
else
{
qstring result_str;
ok = PyW_ObjectToString(py_result.o, &result_str);
if ( ok )
msg("%s\n", result_str.c_str());
}
if ( !ok )
msg("*** IDAPython: Couldn't convert evaluation result\n");
}
}
}
}
//lint -esym(1788, new_execution_t) is referenced only by its constructor or destructor
//------------------------------------------------------------------------
// Executes a simple string
static bool idaapi IDAPython_extlang_eval_snippet(
const char *str,
qstring *errbuf)
{
PYW_GIL_GET;
PyObject *globals = get_module_globals();
bool ok;
if ( globals == NULL )
{
ok = false;
}
else
{
errbuf->clear();
PyErr_Clear();
{
new_execution_t exec;
newref_t result(extapi.PyRun_String_ptr(
str,
Py_file_input,
globals,
globals));
ok = result != NULL && !PyErr_Occurred();
if ( !ok )
handle_python_error(errbuf);
}
}
if ( !ok && errbuf->empty() )
*errbuf = "internal error";
return ok;
}
//------------------------------------------------------------------------
// Simple Python statement runner function for IDC
static error_t idaapi idc_runpythonstatement(
idc_value_t *argv,
idc_value_t *res)
{
qstring errbuf;
bool ok = IDAPython_extlang_eval_snippet(argv[0].c_str(), &errbuf);
if ( ok )
res->set_long(0);
else
res->set_string(errbuf);
return eOk;
}
static const char idc_runpythonstatement_args[] = { VT_STR, 0 };
static const ext_idcfunc_t idc_runpythonstatement_desc =
{
S_IDC_EXEC_PYTHON,
idc_runpythonstatement,
idc_runpythonstatement_args,
NULL,
0,
0
};
//------------------------------------------------------------------------
// Simple Python expression evaluator for IDC
static error_t idaapi idc_eval_python(
idc_value_t *argv,
idc_value_t *res)
{
qstring errbuf;
const char *snippet = argv[0].c_str();
bool ok = IDAPython_extlang_eval_expr(res, BADADDR, snippet, &errbuf);
if ( !ok )
return throw_idc_exception(res, errbuf.c_str());
return eOk;
}
static const char idc_eval_python_args[] = { VT_STR, 0 };
static const ext_idcfunc_t idc_eval_python_desc =
{
S_IDC_EVAL_PYTHON,
idc_eval_python,
idc_eval_python_args,
NULL,
0,
0
};
//--------------------------------------------------------------------------
static const cfgopt_t opts[] =
{
cfgopt_t("SCRIPT_TIMEOUT", &execution.timeout, 0, INT_MAX),
cfgopt_t("ALERT_AUTO_SCRIPTS", &g_alert_auto_scripts, true),
cfgopt_t("REMOVE_CWD_SYS_PATH", &g_remove_cwd_sys_path, true),
cfgopt_t("AUTOIMPORT_COMPAT_IDAAPI", &g_autoimport_compat_idaapi, true),
cfgopt_t("AUTOIMPORT_COMPAT_IDA695", &g_autoimport_compat_ida695, true),
cfgopt_t("NAMESPACE_AWARE", &g_namespace_aware, true),
cfgopt_t("REPL_USE_SYS_DISPLAYHOOK", &g_repl_use_sys_displayhook, true),
};
//-------------------------------------------------------------------------
// Check for the presence of a file in IDADIR/python and complain on error
static bool check_python_dir()
{
static const char *const script_files[] =
{
S_IDC_MODNAME ".py",
S_INIT_PY,
"ida_idaapi.py",
"idautils.py"
};
char filepath[QMAXPATH];
for ( size_t i=0; i < qnumber(script_files); i++ )
{
qmakepath(filepath, sizeof(filepath), g_idapython_dir, script_files[i], NULL);
if ( !qfileexist(filepath) )
{
warning("IDAPython: Missing required file: '%s'", script_files[i]);
return false;
}
}
return true;
}
//-------------------------------------------------------------------------
// This function will execute a script in the main module context
// It does not use 'import', thus the executed script will not yield a new module name
// Caller of this function should call handle_python_error() to clear the exception and print the error
static int PyRunFile(const char *path)
{
#ifdef __NT__
// if the current disk has no space (sic, the current directory, not the one
// with the input file), PyRun_File() will die with a cryptic message that
// C runtime library could not be loaded. So we check the disk space before
// calling it.
char curdir[QMAXPATH];
// check if the current directory is accessible. if not, qgetcwd won't return
qgetcwd(curdir, sizeof(curdir));
if ( get_free_disk_space(curdir) == 0 )
{
warning("No free disk space on %s, python will not be available", curdir);
return 0;
}
#endif
PYW_GIL_CHECK_LOCKED_SCOPE();
PyObject *__main__globals = get_module_globals();
//lint -esym(429, fp) custodial pointer not freed or returned
FILE *fp = qfopen(path, "rt");
if ( fp == NULL )
return 0;
file_janitor_t fpj(fp);
qstring contents;
const uint64 fpsz = qfsize(fp);
if ( fpsz == 0 )
return 0;
contents.resize(fpsz);
qfread(fp, contents.begin(), fpsz);
newref_t code(Py_CompileString(contents.c_str(), path, Py_file_input));
if ( code == NULL )
return 0;
newref_t result(PyEval_EvalCode(
PYCODE_OBJECT_TO_PYEVAL_ARG(code.o),
__main__globals,
__main__globals));
return result != NULL && !PyErr_Occurred();
}
//-------------------------------------------------------------------------
// Execute Python statement(s) from an editor window
void IDAPython_RunStatement(void)
{
qstring qbuf;
netnode history;
// Get the existing or create a new netnode in the database
history.create(S_IDAPYTHON_DATA_NODE);
history.getblob(&qbuf, 0, 'A');
if ( ask_text(&qbuf, 0, qbuf.c_str(), "ACCEPT TABS\nEnter Python expressions") )
{
{
PYW_GIL_GET;
new_execution_t exec;
extapi.PyRun_SimpleString_ptr(qbuf.c_str());
}
// Store the statement to the database
history.setblob(qbuf.c_str(), qbuf.size(), 0, 'A');
}
}
//-------------------------------------------------------------------------
// Convert return value from Python to IDC or report about an error.
// This function also decrements the reference "result" (python variable)
static bool return_python_result(
idc_value_t *idc_result,
const ref_t &py_result,
qstring *errbuf)
{
if ( errbuf != NULL )
errbuf->clear();
if ( py_result == NULL )
{
handle_python_error(errbuf);
return false;
}
int cvt = CIP_OK;
if ( idc_result != NULL )
{
idc_result->clear();
cvt = pyvar_to_idcvar(py_result, idc_result);
if ( cvt < CIP_OK && errbuf != NULL )
*errbuf = "ERROR: bad return value";
}
return cvt >= CIP_OK;
}
//-------------------------------------------------------------------------
// This function will call the Python function 'idaapi.IDAPython_ExecFile'
// It does not use 'import', thus the executed script will not yield a new module name
// It returns the exception and traceback information.
// We use the Python function to execute the script because it knows how to deal with
// module reloading.
static bool IDAPython_ExecFile(
const char *FileName,
PyObject *globals,
qstring *errbuf,
const char *idaapi_script = S_IDAAPI_EXECSCRIPT,
idc_value_t *second_res = NULL,
bool want_tuple = false)
{
PYW_GIL_CHECK_LOCKED_SCOPE();
ref_t py_execscript(get_idaapi_attr(idaapi_script));
if ( py_execscript == NULL )
{
errbuf->sprnt("Could not find %s.%s ?!", S_IDA_IDAAPI_MODNAME, idaapi_script);
return false;
}
char script[MAXSTR];
qstrncpy(script, FileName, sizeof(script));
strrpl(script, '\\', '/');
if ( globals == NULL )
globals = get_module_globals();
newref_t py_script(IDAPyStr_FromUTF8(script));
borref_t py_false(Py_False);
newref_t py_ret(PyObject_CallFunctionObjArgs(
py_execscript.o,
py_script.o,
globals,
py_false.o,
NULL));
// Failure at this point means the script was interrupted
bool interrupted = false;
if ( PyW_GetError(errbuf) || py_ret == NULL )
{
PyErr_Clear();
if ( errbuf->empty() )
*errbuf = "Script interrupted";
interrupted = true;
}
bool ok = false;
if ( !interrupted )
{
PyObject *ret_o;
if ( want_tuple )
{
if ( second_res != NULL
&& PyTuple_Check(py_ret.o)
&& PyTuple_Size(py_ret.o) == 2 )
{
ret_o = PyTuple_GetItem(py_ret.o, 0); // Borrowed reference
}
else
{
INTERR(30444);
}
}
else
{
ret_o = py_ret.o;
}
if ( ret_o == Py_None ) //-V614 uninitialized 'ret_o'
{
if ( want_tuple )
{
borref_t ret2_o(PyTuple_GetItem(py_ret.o, 1));
ok = return_python_result(second_res, ret2_o, errbuf);
}
else
{
ok = true;
}
}
else if ( IDAPyStr_Check(ret_o) )
{
IDAPyStr_AsUTF8(errbuf, ret_o);
}
else
{
INTERR(30154);
}
}
return ok;
}
//-------------------------------------------------------------------------
// Execute the Python script from the plugin
static bool RunScript(const char *script)
{
qstring errbuf;
bool ok;
{
new_execution_t exec;
ok = IDAPython_ExecFile(script, /*globals*/ NULL, &errbuf);
}
if ( !ok )
warning("IDAPython: error executing '%s':\n%s", script, errbuf.c_str());
return ok;
}
//-------------------------------------------------------------------------
// This function parses a name into two different components (if it applies).
// Example:
// parse_py_modname("modname.attrname", mod_buf, attr_buf)
// It splits the full name into two parts.
static bool parse_py_modname(
const char *full_name,
char *modname,
char *attrname,
size_t sz,
const char *defmod = S_IDA_IDAAPI_MODNAME)
{
const char *p = strrchr(full_name, '.');
if ( p == NULL )
{
qstrncpy(modname, defmod, sz);
qstrncpy(attrname, full_name, sz);
}
else
{
qstrncpy(modname, full_name, p - full_name + 1);
qstrncpy(attrname, p + 1, sz);
}
return p != NULL;
}
//-------------------------------------------------------------------------
// Run callback for Python external language evaluator
static bool idaapi IDAPython_extlang_call_func(
idc_value_t *result,
const char *name,
const idc_value_t args[],
size_t nargs,
qstring *errbuf)
{
PYW_GIL_GET;
// Try to extract module name (if any) from the funcname
char modname[MAXSTR];
char funcname[MAXSTR];
bool imported_module = parse_py_modname(name, modname, funcname, MAXSTR);
bool ok = true;
PyObject *module = NULL;
ref_vec_t pargs;
do
{
// Convert arguments to python
ok = pyw_convert_idc_args(args, nargs, pargs, 0, errbuf);
if ( !ok )
break;
const char *final_modname = imported_module ? modname : S_MAIN;
module = PyImport_ImportModule(final_modname);
if ( module == NULL )
{
if ( errbuf != NULL )
errbuf->sprnt("couldn't import module %s", final_modname);
ok = false;
break;
}
PyObject *globals = PyModule_GetDict(module);
QASSERT(30157, globals != NULL);
PyObject *func = PyDict_GetItemString(globals, funcname);
if ( func == NULL )
{
if ( errbuf != NULL )
errbuf->sprnt("undefined function %s", name);
ok = false;
break;
}
borref_t code(extapi.PyFunction_GetCode_ptr(func));
qvector<PyObject*> pargs_ptrs;
pargs.to_pyobject_pointers(&pargs_ptrs);
#ifdef PY3
newref_t py_res(PyEval_EvalCodeEx(
PYCODE_OBJECT_TO_PYEVAL_ARG(code.o),
globals, NULL,
pargs_ptrs.begin(),