-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathSymbolicInterpreter.cpp
More file actions
1477 lines (1352 loc) · 54.5 KB
/
Copy pathSymbolicInterpreter.cpp
File metadata and controls
1477 lines (1352 loc) · 54.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) 2024-present, Trail of Bits, Inc.
//
// This source code is licensed in accordance with the terms specified in
// the LICENSE file found in the root directory of this source tree.
#include "SymbolicInterpreter.h"
#include <multiplier/IR/Interpret/InterpreterLoop.h>
#include <multiplier/IR/Interpret/ConcreteOps.h>
#include <multiplier/IR/Interpret/ConcreteMemory.h>
#include <multiplier/IR/Interpret/Value.h>
#include <multiplier/IR/Function.h>
#include <cassert>
#include <cstring>
#include "Binding.h"
#include "Interpreter.h"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wc99-extensions"
#pragma GCC diagnostic ignored "-Wunused-function"
namespace mx {
using namespace ir::interpret;
using ir::OpCode;
using ir::CastOp;
using ir::ConstOp;
using ir::BitwiseOp;
using ir::FloatOp;
using ir::MemOp;
// ===========================================================================
// Value <-> Python helpers (for concrete fallback inside policy methods)
// ===========================================================================
namespace {
PyObject *value_to_python(const Value &v) {
return PyLong_FromLongLong(v.i64);
}
Value python_to_value(PyObject *obj) {
if (obj == nullptr || obj == Py_None) {
return make_uint(0);
}
if (PyFloat_Check(obj)) {
return make_float(PyFloat_AsDouble(obj));
}
if (PyLong_Check(obj)) {
int64_t v = PyLong_AsLongLong(obj);
if (v == -1 && PyErr_Occurred()) {
PyErr_Clear();
uint64_t uv = PyLong_AsUnsignedLongLong(obj);
if (uv == static_cast<uint64_t>(-1) && PyErr_Occurred()) {
PyErr_Clear();
return make_uint(0);
}
return make_int(static_cast<int64_t>(uv));
}
return make_int(v);
}
return make_uint(0);
}
// Convert concrete Value -> SharedPyPtr (for concrete fallback results).
SharedPyPtr value_to_shared(const Value &v) {
PyObject *obj = value_to_python(v);
SharedPyPtr result(obj);
Py_XDECREF(obj); // SharedPyPtr constructor INCREFed
return result;
}
// PyFloat -> Value. When the consumer expects f32 form (low 32 bits hold
// the f32 bit pattern), narrow through float; otherwise produce f64 form.
// Non-float inputs (PyLong bit pattern) pass through unchanged because
// integers carry their bits in u64 already.
Value python_to_value_f32_aware(PyObject *obj, bool needs_f32) {
if (needs_f32 && PyFloat_Check(obj)) {
return make_float32(static_cast<float>(PyFloat_AsDouble(obj)));
}
if (obj == nullptr || obj == Py_None) return make_uint(0);
if (PyFloat_Check(obj)) return make_float(PyFloat_AsDouble(obj));
if (PyLong_Check(obj)) {
int64_t v = PyLong_AsLongLong(obj);
if (v == -1 && PyErr_Occurred()) {
PyErr_Clear();
uint64_t uv = PyLong_AsUnsignedLongLong(obj);
if (uv == static_cast<uint64_t>(-1) && PyErr_Occurred()) {
PyErr_Clear();
return make_uint(0);
}
return make_int(static_cast<int64_t>(uv));
}
return make_int(v);
}
return make_uint(0);
}
// Float-tagged conversion: produce a PyFloat from the bit pattern. Used
// at type-aware boundaries (memory loads, float arithmetic results) where
// the caller knows the value should surface as a Python float.
SharedPyPtr float_value_to_shared(const Value &v, uint32_t size) {
double d;
if (size <= 4) {
uint32_t bits = static_cast<uint32_t>(v.u64);
float f;
std::memcpy(&f, &bits, sizeof(f));
d = static_cast<double>(f);
} else {
std::memcpy(&d, &v.u64, sizeof(d));
}
PyObject *obj = PyFloat_FromDouble(d);
SharedPyPtr result(obj);
Py_XDECREF(obj);
return result;
}
// Wrapper struct layouts — must match Interpreter.cpp exactly.
struct ConcreteMemoryWrapper {
PyObject_HEAD
ConcreteMemory *memory;
};
// Must match InterpreterStateWrapper in Interpreter.cpp.
//
// `symbolic_state` is a strong reference to a PyObject whose payload is a
// `SymbolicState` — see Sharable<SymbolicState, PyObjectRC>. A null
// pointer means the symbolic arm has not been initialized yet.
struct InterpreterStateWrapper {
PyObject_HEAD
InterpreterState<Value> *state;
::PyObject *symbolic_state;
};
// Extract the embedded SymbolicState from the wrapper's symbolic_state
// PyObject. Returns nullptr if the symbolic arm is uninitialized.
inline SymbolicState *symbolic_state_of(InterpreterStateWrapper *sw) {
if (!sw || !sw->symbolic_state) return nullptr;
return reinterpret_cast<PyWrapperFor<SymbolicState> *>(
sw->symbolic_state)->data;
}
// Drop any existing symbolic state PyObject on `sw` and install a freshly
// constructed one. Returns a reference to the new state for the caller
// to mutate via interp_init_state. The wrapper holds the only strong
// reference; continuations that survive a re-init keep their own.
inline SymbolicState &install_fresh_symbolic_state(
InterpreterStateWrapper *sw) {
auto fresh = make_sharable<SymbolicState>();
Py_XDECREF(sw->symbolic_state);
sw->symbolic_state = fresh.release();
return *reinterpret_cast<PyWrapperFor<SymbolicState> *>(
sw->symbolic_state)->data;
}
// ===========================================================================
// SymbolicState PyTypeObject — the storage for InterpreterState<SharedPyPtr,
// PyObjectRC>. Registered as a hidden private type; never instantiated
// directly from Python. Continuations and the wrapper's `symbolic_state`
// slot hold strong references to instances of this type.
// ===========================================================================
static PyTypeObject SymbolicStatePyType;
static void SymbolicStateType_dealloc(PyObject *self) {
auto *o = reinterpret_cast<PyWrapperFor<SymbolicState> *>(self);
if (o->data) {
o->data->~SymbolicState();
o->data = nullptr;
}
PyObject_Free(self);
}
} // namespace
} // namespace mx
namespace mx::ir::interpret {
template <>
::PyTypeObject &Sharable<mx::SymbolicState, PyObjectRC>::PyType(void) noexcept {
return mx::SymbolicStatePyType;
}
} // namespace mx::ir::interpret
namespace mx {
bool LoadSymbolicStateType(::PyObject *interp_module) {
SymbolicStatePyType = {PyVarObject_HEAD_INIT(nullptr, 0)};
SymbolicStatePyType.tp_name =
"multiplier.ir.interpret._SymbolicState";
SymbolicStatePyType.tp_basicsize = sizeof(PyWrapperFor<SymbolicState>);
SymbolicStatePyType.tp_dealloc = SymbolicStateType_dealloc;
SymbolicStatePyType.tp_flags = Py_TPFLAGS_DEFAULT;
SymbolicStatePyType.tp_doc =
"Internal symbolic interpreter state (PyObjectRC payload)";
if (PyType_Ready(&SymbolicStatePyType) < 0) return false;
Py_INCREF(&SymbolicStatePyType);
return PyModule_AddObject(
interp_module, "_SymbolicState",
reinterpret_cast<PyObject *>(&SymbolicStatePyType)) == 0;
}
// ===========================================================================
// PythonPolicy — construction
// ===========================================================================
PythonPolicy::PythonPolicy(PyObject *py_policy, ConcreteMemory &memory,
FunctionResolver func_resolver,
GlobalResolver global_resolver,
FunctionAddressResolver func_addr_resolver,
EntityByAddressResolver entity_by_addr_resolver)
: py_policy_(py_policy),
memory_(memory),
func_resolver_(std::move(func_resolver)),
global_resolver_(std::move(global_resolver)),
func_addr_resolver_(std::move(func_addr_resolver)),
entity_by_addr_resolver_(std::move(entity_by_addr_resolver)) {}
PythonPolicy::~PythonPolicy() {
Py_XDECREF(cached_make_const_);
Py_XDECREF(cached_binary_op_);
Py_XDECREF(cached_unary_op_);
Py_XDECREF(cached_compare_);
Py_XDECREF(cached_cast_);
Py_XDECREF(cached_is_true_);
Py_XDECREF(cached_resolve_branch_);
Py_XDECREF(cached_resolve_call_);
Py_XDECREF(cached_mem_read_);
Py_XDECREF(cached_mem_write_);
Py_XDECREF(cached_ptr_add_);
Py_XDECREF(cached_ptr_diff_);
Py_XDECREF(cached_ptr_offset_);
Py_XDECREF(cached_symbolic_load_);
Py_XDECREF(cached_symbolic_store_);
Py_XDECREF(cached_on_enter_block_);
Py_XDECREF(cached_on_global_initialized_);
Py_XDECREF(cached_on_instruction_);
Py_XDECREF(cached_mem_bulk_op_);
Py_XDECREF(pending_exc_type_);
Py_XDECREF(pending_exc_value_);
Py_XDECREF(pending_exc_tb_);
}
PyObject *PythonPolicy::lookup_method(PyObject *&cache, const char *name) {
if (!cache) {
PyObject *method = PyObject_GetAttrString(py_policy_.Get(), name);
if (method) {
cache = method;
} else {
PyErr_Clear();
cache = Py_None;
Py_INCREF(Py_None);
}
}
return (cache != Py_None) ? cache : nullptr;
}
// `Py_BuildValue`'s "O" format aborts with `SystemError: NULL object passed
// to Py_BuildValue` if the argument is a real C NULL — `PyObject_CallFunction`
// uses the same builder under the hood. A `SharedPyPtr::Get()` *should*
// always be a real PyObject (Py_None at worst, via `make_default()` /
// `default_value()`), but if a code path ever default-constructs a
// `SharedPyPtr` and stuffs it into the value cache we'd silently feed NULL
// to Py_BuildValue and crash deep in C. Substitute Py_None at the call
// boundary so the failure surfaces as a normal Python None instead.
static inline PyObject *or_none(PyObject *p) noexcept {
return p ? p : Py_None;
}
// ===========================================================================
// 0. Value extraction / construction
// ===========================================================================
std::optional<uint64_t> PythonPolicy::extract_address(const SharedPyPtr &val) {
PyObject *obj = val.Get();
if (!obj || !PyLong_Check(obj) || PyBool_Check(obj)) return std::nullopt;
uint64_t v = PyLong_AsUnsignedLongLong(obj);
if (v == static_cast<uint64_t>(-1) && PyErr_Occurred()) {
PyErr_Clear();
return std::nullopt;
}
return v;
}
int64_t PythonPolicy::extract_int(const SharedPyPtr &val) {
PyObject *obj = val.Get();
if (obj && PyLong_Check(obj)) return PyLong_AsLongLong(obj);
return 0;
}
std::optional<int64_t> PythonPolicy::try_extract_int_impl(
const SharedPyPtr &val) {
PyObject *obj = val.Get();
if (obj && PyLong_Check(obj) && !PyBool_Check(obj)) {
int64_t v = PyLong_AsLongLong(obj);
if (v == -1 && PyErr_Occurred()) {
PyErr_Clear();
return std::nullopt;
}
return v;
}
return std::nullopt;
}
uint64_t PythonPolicy::extract_uint(const SharedPyPtr &val) {
PyObject *obj = val.Get();
if (obj && PyLong_Check(obj)) return PyLong_AsUnsignedLongLong(obj);
return 0;
}
SharedPyPtr PythonPolicy::make_literal_int(int64_t v, uint8_t) {
PyObject *obj = PyLong_FromLongLong(v);
SharedPyPtr result(obj);
Py_XDECREF(obj);
return result;
}
SharedPyPtr PythonPolicy::make_literal_ptr(uint64_t addr) {
PyObject *obj = PyLong_FromUnsignedLongLong(addr);
SharedPyPtr result(obj);
Py_XDECREF(obj);
return result;
}
SharedPyPtr PythonPolicy::make_default() {
return SharedPyPtr(Py_None);
}
bool PythonPolicy::has_address(const SharedPyPtr &val) {
PyObject *obj = val.Get();
if (!obj || !PyLong_Check(obj) || PyBool_Check(obj)) return false;
uint64_t v = PyLong_AsUnsignedLongLong(obj);
if (v == static_cast<uint64_t>(-1) && PyErr_Occurred()) {
PyErr_Clear();
return false;
}
return v != 0;
}
// ===========================================================================
// 1. Value construction
// ===========================================================================
SharedPyPtr PythonPolicy::make_const(ConstOp op, int64_t signed_val,
uint64_t unsigned_val) {
if (PyObject *method = lookup_method(cached_make_const_, "make_const")) {
PyObject *result = PyObject_CallFunction(
method, "ilK", static_cast<int>(op), signed_val, unsigned_val);
if (result && result != Py_NotImplemented) {
SharedPyPtr v(result);
Py_DECREF(result);
return v;
}
if (!result) { capture_exception(); return make_default(); }
Py_DECREF(result); // Py_NotImplemented
}
return value_to_shared(concrete_make_const(op, signed_val, unsigned_val));
}
SharedPyPtr PythonPolicy::make_null_ptr(void) {
return SharedPyPtr(Py_None);
}
// ===========================================================================
// 2. Arithmetic / logic
// ===========================================================================
SharedPyPtr PythonPolicy::binary_op(OpCode op, const SharedPyPtr &lhs,
const SharedPyPtr &rhs) {
if (PyObject *method = lookup_method(cached_binary_op_, "binary_op")) {
PyObject *result = PyObject_CallFunction(
method, "iOO", static_cast<int>(op), or_none(lhs.Get()), or_none(rhs.Get()));
if (result && result != Py_NotImplemented) {
SharedPyPtr v(result);
Py_DECREF(result);
return v;
}
if (!result) { capture_exception(); return make_default(); }
Py_DECREF(result);
}
bool is_float_arith = ir::IsFloatArithmetic(op);
bool is_float_cmp = ir::IsFloatComparison(op);
bool needs_f32 = (is_float_arith || is_float_cmp) &&
(static_cast<unsigned>(op) % 2 == 1);
Value lv = python_to_value_f32_aware(lhs.Get(), needs_f32);
Value rv = python_to_value_f32_aware(rhs.Get(), needs_f32);
Value v = concrete_binary_op(op, lv, rv);
if (is_float_arith) {
return float_value_to_shared(v, needs_f32 ? 4u : 8u);
}
return value_to_shared(v);
}
SharedPyPtr PythonPolicy::unary_op(OpCode op, const SharedPyPtr &operand) {
if (PyObject *method = lookup_method(cached_unary_op_, "unary_op")) {
PyObject *result = PyObject_CallFunction(
method, "iO", static_cast<int>(op), or_none(operand.Get()));
if (result && result != Py_NotImplemented) {
SharedPyPtr v(result);
Py_DECREF(result);
return v;
}
if (!result) { capture_exception(); return make_default(); }
Py_DECREF(result);
}
bool is_float_arith = ir::IsFloatArithmetic(op);
bool needs_f32 = is_float_arith &&
(static_cast<unsigned>(op) % 2 == 1);
Value v = concrete_unary_op(
op, python_to_value_f32_aware(operand.Get(), needs_f32));
if (is_float_arith) {
return float_value_to_shared(v, needs_f32 ? 4u : 8u);
}
return value_to_shared(v);
}
SharedPyPtr PythonPolicy::compare(OpCode op, const SharedPyPtr &lhs,
const SharedPyPtr &rhs) {
if (PyObject *method = lookup_method(cached_compare_, "compare")) {
PyObject *result = PyObject_CallFunction(
method, "iOO", static_cast<int>(op), or_none(lhs.Get()), or_none(rhs.Get()));
if (result && result != Py_NotImplemented) {
SharedPyPtr v(result);
Py_DECREF(result);
return v;
}
if (!result) { capture_exception(); return make_default(); }
Py_DECREF(result);
}
bool needs_f32 = ir::IsFloatComparison(op) &&
(static_cast<unsigned>(op) % 2 == 1);
Value lv = python_to_value_f32_aware(lhs.Get(), needs_f32);
Value rv = python_to_value_f32_aware(rhs.Get(), needs_f32);
return value_to_shared(concrete_compare(op, lv, rv));
}
SharedPyPtr PythonPolicy::cast(CastOp op, const SharedPyPtr &operand) {
if (PyObject *method = lookup_method(cached_cast_, "cast")) {
PyObject *result = PyObject_CallFunction(
method, "iO", static_cast<int>(op), or_none(operand.Get()));
if (result && result != Py_NotImplemented) {
SharedPyPtr v(result);
Py_DECREF(result);
return v;
}
if (!result) { capture_exception(); return make_default(); }
Py_DECREF(result);
}
// Casts from f32 require the input to be in f32 form (low 32 bits).
bool input_is_f32 = (op == CastOp::F32_TO_F64) ||
(op >= CastOp::F32_TO_SI8 && op <= CastOp::F32_TO_SI64) ||
(op >= CastOp::F32_TO_UI8 && op <= CastOp::F32_TO_UI64);
Value v = concrete_cast(
op, python_to_value_f32_aware(operand.Get(), input_is_f32));
bool produces_float = ir::IsIntToFloat(op) ||
op == CastOp::F32_TO_F64 ||
op == CastOp::F64_TO_F32;
if (produces_float) {
bool is_f32 = (op == CastOp::F64_TO_F32) || ir::IsToFloat32(op);
return float_value_to_shared(v, is_f32 ? 4u : 8u);
}
return value_to_shared(v);
}
SharedPyPtr PythonPolicy::ptr_add(const SharedPyPtr &base,
const SharedPyPtr &index,
int64_t element_size) {
if (PyObject *method = lookup_method(cached_ptr_add_, "ptr_add")) {
PyObject *result = PyObject_CallFunction(
method, "OOL", or_none(base.Get()), or_none(index.Get()),
static_cast<long long>(element_size));
if (result && result != Py_NotImplemented) {
SharedPyPtr v(result);
Py_DECREF(result);
return v;
}
if (!result) { capture_exception(); return make_default(); }
Py_DECREF(result);
}
Value v = concrete_ptr_add(
python_to_value(base.Get()), python_to_value(index.Get()), element_size);
return make_literal_ptr(v.u64);
}
SharedPyPtr PythonPolicy::ptr_diff(const SharedPyPtr &lhs,
const SharedPyPtr &rhs,
int64_t element_size) {
if (PyObject *method = lookup_method(cached_ptr_diff_, "ptr_diff")) {
PyObject *result = PyObject_CallFunction(
method, "OOL", or_none(lhs.Get()), or_none(rhs.Get()),
static_cast<long long>(element_size));
if (result && result != Py_NotImplemented) {
SharedPyPtr v(result);
Py_DECREF(result);
return v;
}
if (!result) { capture_exception(); return make_default(); }
Py_DECREF(result);
}
return value_to_shared(concrete_ptr_diff(
python_to_value(lhs.Get()), python_to_value(rhs.Get()), element_size));
}
SharedPyPtr PythonPolicy::ptr_offset(const SharedPyPtr &base,
int64_t byte_offset) {
if (PyObject *method = lookup_method(cached_ptr_offset_, "ptr_offset")) {
PyObject *result = PyObject_CallFunction(
method, "OL", or_none(base.Get()),
static_cast<long long>(byte_offset));
if (result && result != Py_NotImplemented) {
SharedPyPtr v(result);
Py_DECREF(result);
return v;
}
if (!result) { capture_exception(); return make_default(); }
Py_DECREF(result);
}
// GEP_FIELD's result is a pointer — see the ptr_add comment.
Value v = concrete_ptr_offset(python_to_value(base.Get()), byte_offset);
return make_literal_ptr(v.u64);
}
SharedPyPtr PythonPolicy::select(const SharedPyPtr &cond,
const SharedPyPtr &if_true,
const SharedPyPtr &if_false) {
return value_to_shared(concrete_select(
python_to_value(cond.Get()), python_to_value(if_true.Get()),
python_to_value(if_false.Get())));
}
SharedPyPtr PythonPolicy::bitwise_intrinsic(OpCode width_op, BitwiseOp sub,
const SharedPyPtr &val,
const SharedPyPtr &val2) {
return value_to_shared(concrete_bitwise_intrinsic(
width_op, sub, python_to_value(val.Get()), python_to_value(val2.Get())));
}
SharedPyPtr PythonPolicy::float_intrinsic(
FloatOp sub, const std::vector<SharedPyPtr> &operands) {
// Per FloatOp layout: even index = f32, odd = f64.
bool is_f32 = (static_cast<unsigned>(sub) % 2 == 0);
std::vector<Value> concrete_ops;
concrete_ops.reserve(operands.size());
for (auto &op : operands) {
concrete_ops.push_back(python_to_value_f32_aware(op.Get(), is_f32));
}
Value v = concrete_float_intrinsic(sub, concrete_ops);
// ISNAN/ISINF/ISFINITE/SIGNBIT return bool — surface as Python int.
using enum FloatOp;
bool returns_bool = (sub == ISNAN_32 || sub == ISNAN_64 ||
sub == ISINF_32 || sub == ISINF_64 ||
sub == ISFINITE_32 || sub == ISFINITE_64 ||
sub == SIGNBIT_32 || sub == SIGNBIT_64);
if (returns_bool) {
return value_to_shared(v);
}
return float_value_to_shared(v, is_f32 ? 4u : 8u);
}
// ===========================================================================
// 3. Truth test
// ===========================================================================
std::optional<bool> PythonPolicy::is_true(const SharedPyPtr &val) {
if (PyObject *method = lookup_method(cached_is_true_, "is_true")) {
PyObject *result = PyObject_CallFunction(method, "O", or_none(val.Get()));
if (result && result != Py_NotImplemented) {
if (result == Py_None) {
Py_DECREF(result);
return std::nullopt;
}
bool truth = PyObject_IsTrue(result);
Py_DECREF(result);
return truth;
}
if (!result) { capture_exception(); return std::nullopt; }
Py_DECREF(result);
}
return concrete_is_true(python_to_value(val.Get()));
}
// ===========================================================================
// 4. Memory
// ===========================================================================
SharedPyPtr PythonPolicy::mem_allocate(PythonScheduler &, uint64_t size_bytes,
uint64_t align_bytes) {
return make_literal_ptr(memory_.allocate(size_bytes, align_bytes));
}
void PythonPolicy::mem_free(PythonScheduler &, const SharedPyPtr &address) {
if (auto addr = extract_address(address)) {
memory_.free(*addr);
}
}
bool PythonPolicy::mem_read(PythonScheduler &, const SharedPyPtr &addr,
const MemAccessHint &hint, SharedPyPtr &result) {
if (PyObject *method = lookup_method(cached_mem_read_, "mem_read")) {
PyObject *py_result = PyObject_CallFunction(
method, "OIi", or_none(addr.Get()), hint.size_bytes,
static_cast<int>(hint.is_float));
if (py_result && py_result != Py_NotImplemented) {
result = SharedPyPtr(py_result);
Py_DECREF(py_result);
return true;
}
if (!py_result) { capture_exception(); result = make_default(); return true; }
Py_DECREF(py_result); // Py_NotImplemented
}
// Concrete fallback.
auto a = extract_address(addr);
if (!a) {
result = make_default();
return true;
}
Value v = IsBigEndian(hint.sub_op)
? concrete_read_from_mem_be(memory_, *a, hint.size_bytes, hint.is_float)
: concrete_read_from_mem_le(memory_, *a, hint.size_bytes, hint.is_float);
result = hint.is_float ? float_value_to_shared(v, hint.size_bytes)
: value_to_shared(v);
return true;
}
bool PythonPolicy::mem_write(PythonScheduler &, const SharedPyPtr &addr,
const SharedPyPtr &val,
const MemAccessHint &hint) {
if (PyObject *method = lookup_method(cached_mem_write_, "mem_write")) {
PyObject *py_result = PyObject_CallFunction(
method, "OOIi", or_none(addr.Get()), or_none(val.Get()),
hint.size_bytes, static_cast<int>(hint.is_float));
if (py_result && py_result != Py_NotImplemented) {
Py_DECREF(py_result);
return true;
}
if (!py_result) { capture_exception(); return true; }
Py_DECREF(py_result); // Py_NotImplemented
}
// Concrete fallback.
auto a = extract_address(addr);
if (!a) return true;
if (IsBigEndian(hint.sub_op)) {
concrete_write_to_mem_be(memory_, *a, python_to_value(or_none(val.Get())),
hint.size_bytes, hint.is_float);
} else {
concrete_write_to_mem_le(memory_, *a, python_to_value(or_none(val.Get())),
hint.size_bytes, hint.is_float);
}
return true;
}
// Phase 8a: symbolic-LOAD dispatch. The substrate consults this before
// falling through to `with_address`'s suspension path. Python returns a
// resolved value (typically a z3 Select against the region overlay) when
// it can claim the access; NotImplemented (or Py_None) means "fall
// through to the existing suspension path."
bool PythonPolicy::exec_symbolic_load_impl(PythonScheduler &,
const SharedPyPtr &addr,
const MemAccessHint &hint,
SharedPyPtr &result) {
PyObject *method = lookup_method(cached_symbolic_load_, "symbolic_load");
if (!method) return false;
PyObject *py_result = PyObject_CallFunction(
method, "OIi", or_none(addr.Get()), hint.size_bytes,
static_cast<int>(hint.is_float));
if (py_result && py_result != Py_NotImplemented && py_result != Py_None) {
result = SharedPyPtr(py_result);
Py_DECREF(py_result);
return true;
}
if (!py_result) { capture_exception(); return false; }
Py_DECREF(py_result); // Py_NotImplemented or Py_None
return false;
}
bool PythonPolicy::exec_symbolic_store_impl(PythonScheduler &,
const SharedPyPtr &addr,
const SharedPyPtr &val,
const MemAccessHint &hint) {
PyObject *method = lookup_method(cached_symbolic_store_, "symbolic_store");
if (!method) return false;
PyObject *py_result = PyObject_CallFunction(
method, "OOIi", or_none(addr.Get()), or_none(val.Get()),
hint.size_bytes, static_cast<int>(hint.is_float));
if (py_result && py_result != Py_NotImplemented) {
Py_DECREF(py_result);
return true;
}
if (!py_result) { capture_exception(); return false; }
Py_DECREF(py_result); // Py_NotImplemented
return false;
}
bool PythonPolicy::mem_bulk_op(PythonScheduler &, MemOp sub,
const std::vector<SharedPyPtr> &ops,
const MemoryInst &mi, SharedPyPtr &result) {
// Try the Python policy first so analysts can intercept whole bulk
// ops (and so the per-byte default decomposition fires `mem_read` /
// `mem_write` hooks). NotImplemented = "fall through to concrete";
// any other return is the IR result of the bulk op.
if (PyObject *method = lookup_method(cached_mem_bulk_op_, "mem_bulk_op")) {
PyObject *ops_list = PyList_New(static_cast<Py_ssize_t>(ops.size()));
if (!ops_list) {
capture_exception();
result = make_default();
return true;
}
for (size_t i = 0; i < ops.size(); ++i) {
PyObject *o = or_none(ops[i].Get());
Py_INCREF(o);
PyList_SET_ITEM(ops_list, static_cast<Py_ssize_t>(i), o);
}
PyObject *py_result = PyObject_CallFunction(
method, "iN", static_cast<int>(sub), ops_list);
if (!py_result) {
capture_exception();
result = make_default();
return true;
}
if (py_result != Py_NotImplemented) {
result = SharedPyPtr(py_result);
Py_DECREF(py_result);
return true;
}
Py_DECREF(py_result); // NotImplemented — fall through to concrete.
}
std::vector<Value> concrete_ops;
concrete_ops.reserve(ops.size());
for (auto &op : ops) concrete_ops.push_back(python_to_value(op.Get()));
Value concrete_result;
bool alive = concrete_mem_bulk_op(memory_, sub, concrete_ops, mi,
concrete_result);
result = value_to_shared(concrete_result);
return alive;
}
void PythonPolicy::mem_poison(const SharedPyPtr &addr) {
if (auto a = extract_address(addr)) memory_.poison(*a);
}
void PythonPolicy::mem_unpoison(const SharedPyPtr &addr) {
if (auto a = extract_address(addr)) memory_.unpoison(*a);
}
bool PythonPolicy::is_undefined(const SharedPyPtr &val) {
PyObject *obj = val.Get();
return obj == nullptr || obj == Py_None;
}
// Phase 8d: per-block-enter notification.
//
// Invoked at the top of every `enter_block` regardless of how the block
// was reached (initial entry, branch resolution, switch dispatch,
// implicit goto). Lets analysts observe block visits beyond just
// branch transitions, which the renderer in `Path.dot_cfg` consumes.
void PythonPolicy::on_enter_block_impl(const IRBlock &block) {
PyObject *method = lookup_method(cached_on_enter_block_, "on_enter_block");
if (!method) return;
auto block_eid = EntityId(block.id()).Pack();
PyObject *result = PyObject_CallFunction(method, "K",
static_cast<unsigned long long>(block_eid));
if (!result) { capture_exception(); return; }
Py_DECREF(result);
}
// Per-instruction observer hook.
void PythonPolicy::on_instruction_impl_inner(const IRInstruction &inst) {
PyObject *method = lookup_method(cached_on_instruction_, "on_instruction");
if (!method) return;
PyObject *inst_obj = ::mx::to_python<IRInstruction>(inst);
if (!inst_obj) { capture_exception(); return; }
PyObject *result = PyObject_CallFunction(method, "N", inst_obj);
if (!result) { capture_exception(); return; }
Py_DECREF(result);
}
// Global-initialized observer hook.
void PythonPolicy::on_global_initialized_impl_inner(
const IRFunction &init_func, const SharedPyPtr &addr) {
PyObject *method = lookup_method(cached_on_global_initialized_,
"on_global_initialized");
if (!method) return;
// The init frame was always constructed with `params = {addr}` where
// `addr` is a concrete literal pointer (compute_global_ptr). If it
// ever fails to extract here, that's a substrate bug, not a runtime
// condition — surface it loudly instead of silently passing 0.
auto resolved = extract_address(addr);
if (!resolved) {
PyErr_SetString(PyExc_RuntimeError,
"on_global_initialized: initializer frame's address slot is not "
"concrete (substrate invariant broken)");
capture_exception();
return;
}
PyObject *func_obj = ::mx::to_python<IRFunction>(init_func);
if (!func_obj) { capture_exception(); return; }
PyObject *result = PyObject_CallFunction(method, "NK", func_obj,
static_cast<unsigned long long>(*resolved));
if (!result) { capture_exception(); return; }
Py_DECREF(result);
}
// ===========================================================================
// 5. Resolution
// ===========================================================================
bool PythonPolicy::resolve_branch(PythonScheduler &,
const IRInstruction &branch_inst,
const SharedPyPtr &condition,
IRBlock true_block, IRBlock false_block,
IRBlock &chosen_block) {
if (PyObject *method = lookup_method(cached_resolve_branch_,
"resolve_branch")) {
auto true_eid = EntityId(true_block.id()).Pack();
auto false_eid = EntityId(false_block.id()).Pack();
PyObject *inst_obj = ::mx::to_python<IRInstruction>(branch_inst);
if (!inst_obj) { capture_exception(); return false; }
PyObject *result = PyObject_CallFunction(
method, "NOKK", inst_obj, or_none(condition.Get()), true_eid, false_eid);
if (result && result != Py_NotImplemented) {
if (result == Py_None) {
Py_DECREF(result);
return false;
}
if (PyObject_IsTrue(result)) {
Py_DECREF(result);
chosen_block = true_block;
return true;
}
Py_DECREF(result);
chosen_block = false_block;
return true;
}
if (!result) { capture_exception(); return false; }
Py_DECREF(result); // Py_NotImplemented
}
chosen_block = true_block;
return true;
}
bool PythonPolicy::resolve_call(PythonScheduler &,
const IRInstruction &call_inst,
RawEntityId target_eid,
RawEntityId indirect_target_eid,
uint64_t target_addr,
const std::vector<SharedPyPtr> &arguments,
bool is_indirect,
CallResolution<SharedPyPtr> &resolution) {
// Try Python policy first.
if (PyObject *method = lookup_method(cached_resolve_call_, "resolve_call")) {
PyObject *args_list = PyList_New(
static_cast<Py_ssize_t>(arguments.size()));
for (size_t i = 0; i < arguments.size(); ++i) {
PyObject *arg = or_none(arguments[i].Get());
Py_INCREF(arg);
PyList_SET_ITEM(args_list, static_cast<Py_ssize_t>(i), arg);
}
PyObject *inst_obj = ::mx::to_python<IRInstruction>(call_inst);
if (!inst_obj) { capture_exception(); Py_DECREF(args_list); return false; }
PyObject *result = PyObject_CallFunction(
method, "NKKKOi", inst_obj, target_eid, indirect_target_eid,
static_cast<unsigned long long>(target_addr),
args_list, static_cast<int>(is_indirect));
Py_DECREF(args_list);
if (!result) {
// Python exception from the intercept handler — capture it so
// abort_requested() fires and SymbolicStep can re-raise it.
capture_exception();
return false;
}
if (result != Py_NotImplemented) {
// Any non-NotImplemented return is a skip with that value as the
// call's result — `None` stubs the call to return `None`, an int
// / SymExpr / z3 expr is the replacement value, and `Skip(value)`
// is the typed marker analysts can still use (we unwrap it here).
// Only `NotImplemented` falls through to inlining, matching the
// convention used by `mem_read` / `mem_write` and the pure ops.
static PyObject *cls_Skip = nullptr;
if (!cls_Skip) {
PyObject *mod = PyImport_ImportModule("multiplier.symex.events");
if (mod) {
cls_Skip = PyObject_GetAttrString(mod, "Skip");
Py_DECREF(mod);
if (!cls_Skip) PyErr_Clear();
} else {
PyErr_Clear();
}
}
resolution.action = CallAction::SKIP;
if (cls_Skip && PyObject_IsInstance(result, cls_Skip) > 0) {
PyObject *val = PyObject_GetAttrString(result, "value");
if (!val) {
PyErr_Clear();
resolution.return_value = make_default();
} else {
resolution.return_value = SharedPyPtr(val);
Py_DECREF(val);
}
} else {
resolution.return_value = SharedPyPtr(result);
}
Py_DECREF(result);
return true;
}
Py_DECREF(result);
}
// Fall through to C++ func_resolver_.
if (func_resolver_) {
for (auto eid : {target_eid, indirect_target_eid}) {
if (eid != kInvalidEntityId) {
auto ir = func_resolver_(eid);
if (PyErr_Occurred()) {
capture_exception();
return false;
}
if (ir) {
resolution.action = CallAction::INLINE;
resolution.return_value = make_default();
resolution.callee_ir = *std::move(ir);
return true;
}
}
}
}
resolution.action = CallAction::SKIP;
resolution.return_value = make_default();
return true;
}
bool PythonPolicy::resolve_global(PythonScheduler &,
RawEntityId entity_id,
GlobalResolution &resolution) {
if (global_resolver_) {
auto info = global_resolver_(entity_id);
if (PyErr_Occurred()) {
capture_exception();
return false;
}
if (info) {
resolution.info = *std::move(info);
return true;
}
}
resolution = GlobalResolution{};
return true;
}
// ===========================================================================
// Symbolic dispatch helpers
// ===========================================================================
namespace {
FunctionResolver make_func_resolver(PyObject *obj) {
if (!obj || obj == Py_None || !PyCallable_Check(obj)) return {};
SharedPyPtr fr(obj);
return [fr](RawEntityId eid) -> std::optional<IRFunction> {
SharedPyPtr ret(PyObject_CallFunction(fr.Get(), "(K)", eid));
if (!ret) {
return std::nullopt; // exception left pending for caller to capture
}
if (ret.Get() == Py_None) {
return std::nullopt;
}
return from_python<IRFunction>(ret.Get());
};
}
GlobalResolver make_global_resolver(PyObject *obj) {
if (!obj || obj == Py_None || !PyCallable_Check(obj)) return {};
SharedPyPtr gr(obj);
return [gr](RawEntityId eid) -> std::optional<GlobalInfo> {
SharedPyPtr ret(PyObject_CallFunction(gr.Get(), "(K)", eid));
if (!ret) {
// Python exception pending — leave it; resolve_global will capture it.
return std::nullopt;
}
if (ret.Get() == Py_None) {
return std::nullopt;
}
if (!PyTuple_Check(ret.Get()) || PyTuple_Size(ret.Get()) < 3) {
return std::nullopt;
}
GlobalInfo info;
info.canonical_eid = PyLong_AsUnsignedLongLong(
PyTuple_GetItem(ret.Get(), 0));
info.size = static_cast<uint32_t>(
PyLong_AsUnsignedLong(PyTuple_GetItem(ret.Get(), 1)));
info.align = static_cast<uint32_t>(
PyLong_AsUnsignedLong(PyTuple_GetItem(ret.Get(), 2)));