-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitspec.py
1153 lines (972 loc) · 48.4 KB
/
bitspec.py
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
# MIT License
#
# Copyright (c) 2020 amtal
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Bit pattern mini-language for instruction encodings. Bytes in, IR out!
Machine code and interpreter bytecode usually have densely-packed semantic
structure that speeds up tool development when exposed. Specifying encodings
declaratively allows the intermediate representation (IR) to be laid out based
on that structure, not bit-level quirks.
# Example Disassembler
Here's a peculiar group of 4-byte long Zilog Z80 instructions. The architecture
is a byte-prefixed extension of the Intel 8080 crammed into 4 unused opcodes.
Some encoding behaviors were left undefined - possibly to leave room for
further extension.
>>> import bitspec
>>> @bitspec.bitspec('11 xy:1 11101 0xCB nn:s8 00 000 r:3', op='RLC')
... @bitspec.bitspec('11 xy:1 11101 0xCB nn:s8 00 001 r:3', op='RRC')
... @bitspec.bitspec('11 xy:1 11101 0xCB nn:s8 00 010 r:3', op='RL ')
... @bitspec.bitspec('11 xy:1 11101 0xCB nn:s8 00 011 r:3', op='RR ')
... @bitspec.bitspec('11 xy:1 11101 0xCB nn:s8 00 100 r:3', op='SLA')
... @bitspec.bitspec('11 xy:1 11101 0xCB nn:s8 00 101 r:3', op='SRA')
... @bitspec.bitspec('11 xy:1 11101 0xCB nn:s8 00 110 r:3', op='SL1') # "SLL"
... @bitspec.bitspec('11 xy:1 11101 0xCB nn:s8 00 111 r:3', op='SRL')
... @bitspec.bitspec('11 xy:1 11101 0xCB nn:s8 10 b:3 r:3', op='RES')
... @bitspec.bitspec('11 xy:1 11101 0xCB nn:s8 11 b:3 r:3', op='SET')
... class Z80UndocBitOps: # NORTHERN BYTES Volume 3 #10 (October 1982)
... def __str__(self): # mirrored at http://z80.info/z80undoc.htm
... dst = ['B,', 'C,', 'D,', 'E,', 'H,', 'L,', '', 'A,'][self.r]
... bit = '' if self.b == None else f'{self.b},'
... src = ['IX', 'IY'][self.xy]
... return f'{self.op} {dst}{bit}({src}{self.nn:+})'
... def __repr__(self): return f'<{self!s}>'
... def __init__(self, xy, nn, r, op, b=None):
... self.xy, self.nn, self.r, self.op, self.b = xy, nn, r, op, b
The class decorators add `Bitspec.from_bytes` and `Bitspec.iter_bytes`
classmethods that build pattern-matched objects based on the bitfields
specified. Endianness and signed fields are supported.
>>> code = bytes.fromhex('fdCB7f17 ddCBfe88 fdCB0125')
>>> Z80UndocBitOps.from_bytes(code)
<RL A,(IY+127)>
>>> {hex(op.addr):op for op in Z80UndocBitOps.iter_bytes(code, addr=0x50)}
{'0x50': <RL A,(IY+127)>, '0x54': <RES B,1,(IX-2)>, '0x58': <SLA L,(IY+1)>}
The objects get a `__len__` implementation based on which decorator matched.
There's also a `Bitspec.to_bytes` method in case you ever need to patch or
re-assemble code. It's a niche use case, but with a declarative spec it comes
for free!
>>> Z80UndocBitOps.from_bytes(b'\\xdd\\xcb\\xfe\\x88\\x00\\x00')
<RES B,1,(IX-2)>
>>> i = _; assert len(i) == 4
>>> i.nn = 2; {str(i): i.to_bytes()}
{'RES B,1,(IX+2)': b'\\xdd\\xcb\\x02\\x88'}
# Identifying Structure in an ISA
The data structure above is terse but inconvenient to lift or interpret. We're
already pattern matching once - why switch over the values again in order to
separate simple shifter operations from bit flips? Someone familiar with the Z80's
history would trivially carve it up into octet prefixes and hextet operands.
This library is for incremental tool development on unfamiliar or poorly
documented targets.
## Specializing Instructions
To decode multiple kinds of instructions, group them in a class hierarchy. All
the subclass specifications are merged together and matched at once. (The
`dataclass` decorator is identical to `bitspec`, but also adds default PEP 557
dataclass methods.)
>>> @bitspec.dataclass
... class HPPA: pass # Hewlett Packard Precision Architecture (PA-RISC)
>>> @bitspec.dataclass('000010 ----- ----- 0000 001001 0 00000')
... class NOP(HPPA): # ^^^^^
... pass # R0 is always zero. Writing to it is a NOP.
>>> @bitspec.dataclass('000010 00000 r1:5 0000 001001 0 rt:5')
... @bitspec.dataclass('000010 r1:5 00000 0000 001001 0 rt:5') # hack!
... class COPY(NOP): # ^^^^^ ^^^^^
... r1: int # If one operand is R0, boolean OR is actually a COPY.
... rt: int # To simplify lifting, this IR pretends r2 is r1.
>>> @bitspec.dataclass('000010 r2:5 r1:5 cf:4 001001 0 rt:5')
... class OR(COPY): # Finally here's the full instruction encoding.
... cf: int # You *could* lift OR, COPY, and NOP from it by checking
... r2: int # for 0-fields, but those cases are already handled.
>>> import dataclasses; [dataclasses.is_dataclass(c) for c in OR.__mro__]
[True, True, True, True, False]
Matches are prioritized based on the maximum number of constant bits, with
shortest class distance from the root class as a tie-breaker. This is handy for
architectures where specialized instructions are actually subsets of more
generic ones, or an architecture extension was allocated within a no-op
encoding of an unrelated operation.
>>> HPPA.from_bytes(b'\\x08\\x1f\\x02\\x5f')
COPY(r1=31, rt=31)
>>> HPPA.from_bytes(OR(cf=0, r2=0, r1=31, rt=31).to_bytes())
COPY(r1=31, rt=31)
>>> HPPA.from_bytes(COPY(r1=31, rt=0).to_bytes())
NOP()
## Factoring Operands
While keyword argument values usually get assigned as-is, bitspec-decorated
classes get instantiated with `from_bytes` on the same input as the top-level
object. This allows commonly-used addressing modes and operand types to be
factored into their own classes for easy lifting.
>>> class Operand: # Intel MCS-51 addressing modes
... def lift_load(self, il): raise NotImplementedError
... def lift_store(self, il): raise NotImplementedError
>>> @bitspec.dataclass('.... ....')
... class Acc(Operand): pass # A
>>> @bitspec.dataclass('.... .... addr:8')
... class Dir(Operand): addr: int # [addr]
>>> @bitspec.dataclass('.... . reg:3')
... class Reg(Operand): reg: int # R0..R7
>>> @bitspec.dataclass('.... ... i:1')
... class RegInd(Operand): i: int # @R0, @R1
>>> @bitspec.dataclass('.:8 val:8')
... class Imm(Operand): val: int # #val
>>> @bitspec.dataclass('.:16 val:8')
... class Imm_(Imm): pass # re-use lift semantics on a new encoding!
>>> @bitspec.dataclass('0100 0100', name='ORL', dst=Acc, src=Imm)
... @bitspec.dataclass('0100 0101', name='ORL', dst=Acc, src=Dir)
... @bitspec.dataclass('0100 011.', name='ORL', dst=Acc, src=RegInd)
... @bitspec.dataclass('0100 1...', name='ORL', dst=Acc, src=Reg)
... @bitspec.dataclass('0100 0010', name='ORL', dst=Dir, src=Acc)
... @bitspec.dataclass('0100 0011', name='ORL', dst=Dir, src=Imm_)
... @bitspec.dataclass('0101 0100', name='ANL', dst=Acc, src=Imm)
... @bitspec.dataclass('0101 0101', name='ANL', dst=Acc, src=Dir)
... @bitspec.dataclass('0101 011.', name='ANL', dst=Acc, src=RegInd)
... @bitspec.dataclass('0101 1...', name='ANL', dst=Acc, src=Reg)
... @bitspec.dataclass('0101 0010', name='ANL', dst=Dir, src=Acc)
... @bitspec.dataclass('0101 0011', name='ANL', dst=Dir, src=Imm_)
... # [...] repetitive code for XRL, ADD, ADDC, etc.
... class MCS51ALU:
... name: str
... dst: Operand
... src: Operand
The resulting `__len__` is adjusted based on any arguments `from_bytes` was
called on, so variable-length encodings work as you'd expect.
>>> code = bytes.fromhex('438010 58 52ff')
>>> for i in MCS51ALU.iter_bytes(code, addr=0):
... print(f'{hex(i.addr)}({len(i)}): {i}')
0x0(3): MCS51ALU(name='ORL', dst=Dir(addr=128), src=Imm_(val=16))
0x3(1): MCS51ALU(name='ANL', dst=Acc(), src=Reg(reg=0))
0x4(2): MCS51ALU(name='ANL', dst=Dir(addr=255), src=Acc())
## Incremental Development
What if we're implementing tools for an unfamiliar architecture, and it's not
obvious how addressing modes are encoded?
Suppose we thought the MCS-51 instruction set was orthogonal and operand
addressing mode encodings were independent. The preceding code could be
code-golfed by having the operand arguments be an orthogonal pattern match.
But wait - MCS-51 isn't orthogonal! One of the MOV encodings that would be a
NOP on an architecture with bits to spare is actually a JMP, and another had a
bit-level ORL variant squeezed in.
>>> @bitspec.dataclass('.... 0100 n:8', dst='A', src='#{n}') # immediate
... @bitspec.dataclass('.... 0101 n:8', dst='A', src='0x{n:x}') # direct
... @bitspec.dataclass('.... 011 n:1', dst='A', src='@R{n}' ) # I-ram
... @bitspec.dataclass('.... 1 n:3', dst='A', src='R{n}') # register
... @bitspec.dataclass('0111 0010 n:8', dst='C', src='{n}') # bit addr (hack)
... @bitspec.dataclass('.... 0011 n:8 m:8', dst='0x{n:x}', src='#{m}')
... @bitspec.dataclass('.... 0010 n:8', dst='0x{n:x}', src='A')
... class Operands:
... src:str; dst:str; n:int = -1; m:int = -1
... def __str__(self):
... return f'{self.dst},{self.src}'.format(**self.__dict__)
... def __repr__(self): return f'<{len(self)}: {self!s}>'
>>> @bitspec.dataclass('0100 ....', name='ORL', ops=Operands)
... @bitspec.dataclass('0101 ....', name='ANL', ops=Operands)
... @bitspec.dataclass('0110 ....', name='XRL', ops=Operands)
... @bitspec.dataclass('0111 ....', name='MOV', ops=Operands)
... # [...]
... @bitspec.dataclass('0111 0011', name='JMP', ops='@A + DPTR') # (hack)
... class OrthMCS51ALU:
... name:str; ops:Operands
... def __repr__(self): return f'<{len(self)}: {self.name} {self.ops!s}>'
And that's fine. Since `Bitspec.from_bytes` only directly pattern matches on
the class hierarchy it is called on, with any operands being pattern matched
only after the first-level match succeeds, some conflicts and overlaps with
bitspec arguments are okay.
>>> OrthMCS51ALU.from_bytes(b'C'*3)
<3: ORL 0x43,#67>
>>> jmp_indirect = b'\\x73' + b'C' * 10
>>> OrthMCS51ALU.from_bytes(jmp_indirect) # correct disassembly
<1: JMP @A + DPTR>
>>> Operands.from_bytes(jmp_indirect) # top-level match takes priority!
<3: 0x43,#67>
A disassembler and lifter can be quickly brought up based on common encoding
structures. Edge cases can be filled in later as they're figured out.
## Debugging and Partial Decoding
Bringing up a new ISA target will often raise questions:
* "Am I looking at code or data? The disassembly doesn't make sense."
* "Did I typo this instruction's encoding or is the reference manual wrong?"
* On bad days, "Does my memory dump contain semi-random bitflips?"
This library can't fix signal integrity in 1' of 0.1" jumper wire spaghetti. It
*can* disassemble just one class of instructions at a time, which helps debug
encoding typos from an interpreter.
>>> NOP.from_bytes(b'\\x08\\x1f\\x02\\x41')
COPY(r1=31, rt=1)
You can even peek under the hood for a quick encoding reference while doing it.
>>> from pprint import pprint as pp
>>> pp(COPY.__bitspec__)
{<Match 000010.....0000000000010010..... /4>: {'r1': <Slice 0:6 u:5 _:21 /4>,
'rt': <Slice 0:27 u:5 /4>},
<Match 00001000000.....00000010010..... /4>: {'r1': <Slice 0:11 u:5 _:16 /4>,
'rt': <Slice 0:27 u:5 /4>}}
>>> pp(NOP.__bitspec__)
{<Match 000010..........0000001001000000 /4>: {}}
There's also a sometimes-useful (sometimes-annoying) edge case when decoding
short bytestrings on variable length architectures: it's possible to decode an
instruction prefix, but not have enough bytes left for the operand.
>>> OrthMCS51ALU.from_bytes(b'\\x72' + b'C') # ok
<2: MOV C,67>
>>> OrthMCS51ALU.from_bytes(b'\x72') # no operand!
<1: MOV None>
Usually that's a sign the ISA specification is missing an instruction or needs
the top-level match padded out to a full instruction length. However, at the
end of a basic block or data section it's a good sign you're disassembling
data.
# Motivation and Similar Tools
Isolating bit-level encoding details to a flexible mini-language allows the
low-level IR to be designed entirely around the target ISA's semantic quirks
rather than its encoding quirks. Building around addressing modes, ALU design,
memory banking schemes, or significant ISA extensions greatly speeds up lifter
bringup, debugging, and maintenance.
Q: But I wanna go *fast*! Why not just write table-based (dis)assemblers? <br>
A: You should - in Rust. Python's GIL and ctypes back-and-forth to your
binary analysis framework of choice will limit scalability anyway.
Q: Isn't this overthinking a problem that doesn't exist? <br>
A: In theory the number of ISAs in this world is finite and we can
brute-force our way to nice tool support for all of them.
In practice appearance of interesting targets, weird bytecode machines,
and binary analysis platforms seems to outpace tool publications.
Q: The DSL style looks familiar. <br>
A: See GDB's opc2c.c, QEMU's decodetree.py, or Amoco's @ispec as other examples.
Q: Do all sufficiently complex binary analysis projects really
contain an ad-hoc implementation of LLVM's MCInst? <br>
A: Yes, but only touch LLVM when paid to.
"""
## read docs locally with `python -m pydoc -b bitspec`
## (or ideally with the `pdoc3` package, but keep it out of CI? hmm)
__version__ = '0.4.4'
__author__ = 'amtal'
__license__ = 'MIT' # https://opensource.org/licenses/MIT
__all__ = ['dataclass', 'bitspec', 'is_bitspec', 'Bitspec']
# TODO top-level from_bytes API rework
# addr=n argument to iter_bytes not present on from_bytes, weird
# maybe drop from_bytes, add a fromhex to keep examples next()-free?
# TODO want a len_iter_bytes length-decoder fastpath?
# - need a way to signal full-decoding need though
# since you usually want branch decoding not just length
# - also very inconvenient to do with current arglen behavior,
# arguments might expand final matched length so whole match tree is
# too dynamic to try and extract a closed-form solution
# - __bitspec_match__ is also a hack currently breaks "frozen" objects,
# maybe dynamic length isn't worth it even in Python
# TODO examples of NamedTuple / other __slots__-based IRs
# TODO re-examine python version high watermark
# TODO changelog in comments?
from itertools import groupby
# Python >= 3:
import inspect
from types import FunctionType
import typing # Python >= 3.5 for NamedTuple subclass
import abc, collections.abc
# Attribute name for storing bitfield spec, canonical indicator for is_bitspec.
_SPEC = '__bitspec__'
# Cache of all subclasses, updated when __hash__ changes or on first use?
_CACHE = '__bitspec_cache__'
# Which pattern an object was matched from. (Used only in re-assembly.)
_MATCH = '__bitspec_match__'
class Bitspec(collections.abc.Sized, metaclass=abc.ABCMeta):
"""ABC[^1] for type-annotating `bitspec`-decorated classes.
>>> import typing
>>> @bitspec.dataclass('0x414141')
... class Aaa: pass
>>> def decode(bs: bytes) -> typing.List[bitspec.Bitspec]:
... return list(Aaa.iter_bytes(bs))
The library follows the same extension pattern as PEP 557 dataclasses. No
new class is created. As a result, inheritance is untouched!
[^1]: Not a real PEP 3119 ABC, just here for annotations and docs. :)
>>> xs = decode(b'A' * 9); xs
[Aaa(), Aaa(), Aaa()]
>>> b''.join(x.to_bytes() for x in xs) # this'll typecheck nicely, though
b'AAAAAAAAA'
>>> assert not any(isinstance(x, bitspec.Bitspec) for x in xs)
*[ISA]: Instruction Set Architecture
*[IR]: Intermediate Representation
*[ALU]: Arithmetic Logic Unit
*[DSL]: Domain Specific Language
*[GIL]: Global Interpreter Lock
*[ABC]: Abstract Base Class
*[PEP 557]: dataclasses module added in Py3.7
*[PEP 3119]: Introducing Abstract Base Classes
"""
# Dataclass-style extension doesn't actually re-create the class, so we
# can't add an extra parent. This is purely a pydoc / mypy hallucination.
# Could add it as a decoration?
#
# For type annotation reasons, it has to be declared here and defined at
# the bottom. Every release of Python strays further from God's light.
__slots__ = _SPEC, _MATCH
from_bytes:classmethod
iter_bytes:classmethod
addr:typing.Optional[int]
"""Address of decoded instruction.
Optional, only set if a base `addr` is passed to `Bitspec.iter_bytes`.
"""
to_bytes:typing.Callable[[typing.Any, int], bytes]
__len__:typing.Callable[[typing.Any], int]
def bitspec(specification='', **const_fields):
"""Class decorator which adds `Bitspec` methods to an existing class.
Mini-language grammar, with whitespace separating constants and fields:
specification := (const_bits | const_bytes | variable)+
const_bits := ('.' | '0' | '1')+
const_bytes := '0' 'x' hex_value
variable := (py_identifier | '.') ':' 's'? int_size
Bits and bytes are indexed in big-endian order, with the most significant
bit at the start of the specification and bit 0 at the end. The byte order
can be flipped for little-endian memory platforms, but bit order remains
the same.
Any bitspec argument fields are part of the full width; don't-care bits
will be appended to the end of any too-short declarations.
>>> # byte-endianness: [ 3] [ 2] [ 1] [0]
>>> # bit-endianness: 31-----24 23-----16 15----8 7-0
>>> @bitspec.dataclass('.... .... .... .... a:4 b:4 c:8')
... class Op:
... a: int; b: int; c: int
>>> @bitspec.dataclass('1000 0100 0010 0001', op=Op)
... class BitOrder:
... op: Op
>>> BitOrder.from_bytes(b'\\x84\\x21\\x53\\x10')
BitOrder(op=Op(a=5, b=3, c=16))
>>> BitOrder.from_bytes(b'\\x21\\x84\\x10\\x53', byteswap=2)
BitOrder(op=Op(a=5, b=3, c=16))
Total size should be aligned on byte boundaries, but internal bit fields
can have arbitrary widths and alignments.
Syntax sugar includes:
- An empty spec (default) can't be matched, but still gets all the extra
methods. This is often the case for top-level "instruction" classes that
anchor multiple subclassed instruction types. Either `@bitspec()` or
`@bitspec` decorator syntax can be used.
- Long runs of ..... don't-care bits can be listed as a '.' or '-' variable.
- Wildcard characters '.' and '-' are completely interchangeable. This can
be handy to differentiate between actual don't-care bits and parts of the
pattern match that are matched/extracted in an argument, but is purely a
hint to the reader and isn't validated in any way.
- Byte fields for instruction sets with known prefixes or big constants.
They have to be byte-aligned and a multiple of 8 bits wide.
>>> @bitspec.dataclass('-:7 .:9 a:4 b:4 c:8')
... class ShortOp(Op): pass
>>> @bitspec.dataclass('0x8421', op=ShortOp)
... class ShortBitOrder(BitOrder): pass
>>> ShortBitOrder.from_bytes(b'\\x84\\x21\\x53\\x10')
ShortBitOrder(op=ShortOp(a=5, b=3, c=16))
Ambiguities are resolved by:
1. Maximizing the number of constant bits matched.
2. Prioritizing the shallowest reachable class. (This is why the above
example isn't ambiguous, even though BitOrder is part of its pattern
match.)
Prefixing field size with 's' will read and sign-extend a 2s complement
signed value.
>>> @bitspec.dataclass('imm:32 off:s16 src:4 dst:4 op:8')
... class EBPF:
... imm:int; off:int; src:int; dst:int; op:int
>>> ja_neg4 = bytes.fromhex('05 00 fcff 00000000')
>>> EBPF.from_bytes(ja_neg4, byteswap=8)
EBPF(imm=0, off=-4, src=0, dst=0, op=5)
.. todo:: Multiple fields with the same name will be concatenated.
Detection of dead code due to ambiguous or over-constrained specifications
is best-effort and not guaranteed; this is an instruction decoder not a
general-purpose parser. That said, the load-time checks provide a bit more
assurance than usual for a statically unityped language like Python.
Raises:
SyntaxError: certain bugs (e.g. field name not a valid variable)
SyntaxWarning: suspected bugs (e.g. ambiguous or overconstrained specs)
IndexError: top-level byte alignment violated
NameError: field names don't match constructor arguments
"""
if callable(specification) and len(const_fields) == 0: # @bitspec
return install_methods(specification, None, {}, {})
match, var_fields = load_time_parse(specification)
check_duplicate_args(var_fields, const_fields)
def add_bitspec(cls):
check_class_args(cls, var_fields, const_fields, specification)
return install_methods(cls, match, var_fields, const_fields)
return add_bitspec
def test_bitspec(): # hack to make doctest see tests despite function
pass # clashing with "import" glob (breaks line #s, oh well)
test_bitspec.__doc__ = bitspec.__doc__
import dataclasses # Python >= 3.7 (maybe ImportError gate @dataclass?)
def dataclass(specification='', **const_fields):
"""Same class decorator as @`bitspec` but with a PEP 557 @dataclass inside.
You'll still need the extra line if using non-default dataclass arguments.
>>> import dataclasses
>>> @bitspec.bitspec('0xf000')
... @dataclasses.dataclass # or just do @bitspec.dataclass('0xf000')
... class Foo: pass
"""
if callable(specification) and len(const_fields) == 0: # @dataclass
return install_methods(install_dataclass(specification), None, {}, {})
match, var_fields = load_time_parse(specification)
check_duplicate_args(var_fields, const_fields)
def add_bitspec_with_dataclass(cls):
cls = install_dataclass(cls)
check_class_args(cls, var_fields, const_fields, specification)
return install_methods(cls, match, var_fields, const_fields)
return add_bitspec_with_dataclass
def install_dataclass(cls):
# dataclasses.is_dataclass returns true for *inherited* dataclasses due
# to using `hasattr`, we're just looking to see if @dataclass has been
# applied already to avoid re-running it.
is_already = dataclasses._FIELDS in cls.__dict__
return cls if is_already else dataclasses.dataclass(cls)
# this was the only use of the module, could drop dep for py2.7
def is_bitspec(cls_or_obj: typing.Any) -> bool:
"""True if `cls_or_obj` has been directly decorated with a `bitspec`.
>>> @bitspec.bitspec
... class Foo: pass # can be matched, has extra methods
>>> class Bar(Foo): pass # can't be matched, no extra methods
>>> @bitspec.bitspec("0x0badf00d")
... class Baz(Bar): pass # part of the match for Foo
>>> [bitspec.is_bitspec(x) for x in [Foo, Bar, Baz]]
[True, False, True]
>>> [cls.from_bytes(b'\\x0b\\xad\\xf0\\x0d') for cls in Baz.__mro__
... if bitspec.is_bitspec(cls)]
[<__main__.Baz object at ...>, <__main__.Baz object at ...>]
Since `Bitspec.from_bytes` returns instances of the specific class that's
been decorated, there's no clear meaning for calling it on a non-decorated
subclass. As such while bitspec methods may resolve on a subclass, calling
them is currently an error.
>>> try: Bar.from_bytes(b'\\xde\\xad\\xbe\\xef')
... except TypeError: True
True
"""
cls = cls_or_obj if isinstance(cls_or_obj, type) else type(cls_or_obj)
return _SPEC in cls.__dict__
def iter_bytes(cls, bytes:bytes, byteswap=0, addr=None) -> typing.Iterable[Bitspec]:
"""Generate a sequence of objects pattern-matched from bytes.
Yields results until a match fails. Un-decoded bytes can be identified
based on last-decoded instruction address:
>>> @bitspec.bitspec('0x41414141')
... class AAA: pass
>>> mash, start = b'A'*1024 + b'\\xde\\xad\\xbe\\xef', 0x8000
>>> for a in AAA.iter_bytes(mash, addr=start):
... pass
>>> mash[a.addr - start + len(a):]
b'\\xde\\xad\\xbe\\xef'
Args:
byteswap: little-endian word width in bytes, 0 for big-endian
addr: set an `addr` attribute on generated objects, incrementing it as
generator advances
Raises:
TypeError: no bitspec decorator found on cls
"""
# perform byte-endianness swap ASAP, if matches aren't word-aligned at
# least the resulting bugs won't be confusing
bytes = swap_endianness(bytes, byteswap)
remaining = len(bytes)
while remaining:
if obj := from_bytes(cls, bytes[-remaining:]):
if addr != None:
obj.addr = addr
addr += len(obj)
remaining -= len(obj)
yield obj
else: # remaining length can be reconstructed from obj.addr
return
def reachable_bitspec_classes(root):
# enumerate all possible subclasses that are part of pattern match
# breadth-first traversal storing shortest path as later tie-breaker
#
# if A->B->C and is_bitspec(x) is True,False,True then C isn't reached...
# document? full traverse?
cls_tree = {}
level = {root,}
next_level = set()
depth = 0
while level:
for cls in level:
# Subtle: traverse *entire* class tree, but only return bitspec
# matches. This means A->B->C where only A and C are decorated will
# match both in A.from_bytes, but ignores B.
#
# This can come up when doing complicated things with operands for
# the sake of code-golfing a lifter into something legible.
if cls not in cls_tree and is_bitspec(cls):
cls_tree[cls] = depth
{next_level.add(sub_cls) for sub_cls in cls.__subclasses__()}
level, next_level = next_level, set()
depth += 1
max_depth = depth
return cls_tree, max_depth
import functools
@functools.lru_cache(maxsize=512)
def __precompute(cls):
cls_tree, max_depth = reachable_bitspec_classes(cls)
possible_matches = []
for a_cls, depth in cls_tree.items():
for match in a_cls.__dict__[_SPEC]:
score = bin(match.mask).count('1') # popcnt
result = (score, max_depth - depth, match, a_cls)
possible_matches.append((match, result))
opaque = Match.multimatch_precompute(possible_matches)
return opaque
@functools.lru_cache(maxsize=4096)
def from_bytes(cls, bytes: bytes, byteswap=0) -> typing.Optional[Bitspec]:
"""Constructor classmethod.
Args:
byteswap: little-endian word width in bytes, 0 for big-endian
Returns:
`None` if match unsuccessful due to insufficient bytes or wrong prefix.
Raises:
TypeError: no bitspec decorator found on cls
"""
if not is_bitspec(cls):
msg = f'''{cls} has not been decorated by @bitspec directly
Either the intent was to match subclasses ({cls.__subclasses__()})
in which case {cls} needs a trivial decorator, or a decorated class
(one of {cls.__mro__[1:-1]})
has been subclassed. Since bitspec from_bytes returns very specific classes on
successful matches, deserializing non-bitspec subclasses do not make sense.
'''
raise TypeError(msg)
opaque = __precompute(cls)
bytes = swap_endianness(bytes, byteswap)
if not (possible_matches := Match.multimatch_execute(opaque, bytes)):
return None
_, _, match, matched_cls = max(possible_matches)
# build object
def slice_off_argument(val):
if isinstance(val, Slice):
return val.from_bytes(bytes)
elif isinstance(val, type) and is_bitspec(val):
# ^ Check that it was an uninstantiated class, not an instance.
# Otherwise passing an initialized object will silently
# replace it with a brand-new one, initialized from the bytes.
return val.from_bytes(bytes)
else:
return val
spec_args = matched_cls.__dict__[_SPEC][match]
kwargs = {name:slice_off_argument(spec_args[name]) for name in spec_args}
obj = matched_cls(**kwargs)
# adjust length for matched args; they're not part of the pattern match,
# but not propagating length up means user would have to max(len(i),
# len(i.src), len(i.dst)) or something dumb
matched_length = max([match.byte_length] + [len(k) for k in kwargs.values()
if is_bitspec(k)])
if matched_length != match.byte_length:
match = match.expand_by(matched_length - match.byte_length)
setattr(obj, _MATCH, match) # for easy `to_bytes`, not slots-compatible
return obj
# Matches are prioritized on number of constant bits first, matched class
# distance from root second.
#
# **Design Rationale:**
#
# Class depth is a clean tie-breaker for slicing out operand fields, which
# usually has no constant-bits, only don't-care bits and field slices. As
# such it's common to have a complex class hierarchy you want to decode
# from bytes, but none of the classes actually have constant bit matches to
# disambiguate them. This behavior guarantees the "nearest" class gets
# decoded, which usually ends up being the one the decode classmethod
# was called on.
#
# It's an ugly tie-breaker for rare cases where you might want to decode
# macroinstructions or special-cased instruction aliases. The PA-RISC
# example in the module docs does this to decode a no-op encoding as
# a specific NOP, although practically there are multiple no-op encodings
# and which one gets used by a given target's assembler isn't guaranteed.
#
# There might be CISC var-length architectures where the max-number of
# constant bits heuristic isn't valid and you need a proper parser. Can't
# think of any off the top of my head, easily solved via a first-pass
# decoder that looks for prefix bytes or whatever and dispatches to three
# or four appropriate instruction types.
#
# **Alternatives Considered:**
#
# * manually prioritizing conflicts via foo.decorator, too much effort
# for common case
# * passing a floating point weight-adjustment as an extra arg, too much
# internal details leakage + might collide with field names
#
# tl;dr this is where not treating this as a proper parser bites us,
# but afaict it's still worth it and full LR(8) bitparsing or w/e is
# complete overkill
# Bitspec arguments affect the pattern match by increasing its byte length.
# Bitspec arguments do not affect the matched constant values, failure to match
# argument constants just results in that argument returning None.
#
# This should be fairly easy to debug (AttributeError: `NoneType` object has
# no attribute 'lift`) and should only really happen if the argument
# does some complex operand dispatch... But it's usually easier to
# specify operand types in a top-level instruction pattern, than to
# figure out operand encoding invariants that hold across all
# instruction classes and push them down to an operand argument.
#
# tl;dr Bitspec pattern matches traverse subclasses, but not arguments.
# This is an implementation detail that also simplifies fast matching,
# but should still be documented somewhere?
import array
def swap_endianness(bytes, word_length):
"""Silently truncates non-word-sized inputs."""
if not word_length:
return bytes
elif word_length in [2,4,8]:
if tail := len(bytes) % word_length:
bytes, tail = bytes[:-tail], bytes[-tail:]
arr = array.array({2:'H',4:'I',8:'Q'}[word_length])
arr.frombytes(bytes)
arr.byteswap()
return arr.tobytes() # Py>=3.2 lol
else:
msg = f'{word_length*8}-bit byte endianness not implemented'
raise NotImplementedError(msg)
def to_bytes(self: Bitspec, byteswap=0) -> bytes:
"""Assemble IR result of `Bitspec.from_bytes`.
Only works if object has fields that exactly match its constructor
arguments. This is a common Python convention and is true for PEP 557
dataclasses, but if you don't intend to use this method you can completely
ignore the convention.
Ambiguities are resolved as follows:
- Don't-care bit positions will be set to zero.
- Fixed bits will be set to whatever they were originally decoded from.
- If the object was constructed manually rather than via from_bytes,
fixed bits will be chosen from an arbitrary match decorator. If there are
multiple decorators, exact one chosen is undefined.
As an added benefit, this means instantiated bitspec arguments can be
passed to pattern matches. A common example would be specializing a
particular encoding. Doing so isn't a meaningful optimization, but it's
nice to have it "just work"[^1] rather than throw errors.
[^1]: This is probably a nitpicky implementation detail, but the provided
example *could* return a length of 1 since `SReg(0)` was never pattern
matched and might have valid definitions with other lengths? Current
solution is dead simple and just treats len(arg.to_bytes()) as a lower
bound on length. FIXME rm docs leave test
>>> @bitspec.dataclass('.:8 n:4 ....')
... class SReg: n:int
>>> @bitspec.dataclass('0x01 .... imm:s4', r=SReg, op='load-rel')
... @bitspec.dataclass('0x01 0000 imm:4', r=SReg(0), op='load-abs')
... class SIns: r:SReg; op: str; imm:int
>>> SIns.from_bytes(b'\\x01\\x0f')
SIns(r=SReg(n=0), op='load-abs', imm=15)
>>> len(_.r) # re-calculated off of .to_bytes(), not cached!
2
Disassembly is expected to be more performance-sensitive than re-assembly, so
the implementation is likely to be slower than from_bytes.
"""
try:
match = getattr(self, _MATCH)
except AttributeError:
# fully synthetic object constructed w/o matching
# FIXME actually walk down matches to find correct match
match = list(getattr(self.__class__, _SPEC).keys())[0] # lol guess
fields = getattr(self.__class__, _SPEC)[match]
acc = match.const
for name in fields:
field = fields[name]
if isinstance(field, Slice):
val = getattr(self, name) # assume field names == __init__ args
val <<= field.shift
acc |= val
big_endian = acc.to_bytes(length=match.byte_length, byteorder='big')
return swap_endianness(big_endian, byteswap)
def byte_length(self: Bitspec) -> int:
"""Byte length of matched value.
If the object wasn't built by calling `from_bytes` or `iter_bytes`, length
should still be correct in simple cases. Multiple matches of variable
lengths *might* result in wrong length being returned.
"""
if match := getattr(self, _MATCH, None):
return match.byte_length
else:
# Synthetic-constructed instance; realistically, most likely reason to
# check len() on this is the default truthiness implementation. e.g.
# testing an operand with a default value of None will call __len__ if
# it's present.
# So, actual accurate length probably doesn't matter so just guess :)
# If multiple lengths are possible lol good luck.
# FIXME actually walk down matches to find correct match
assert type(self) != type
match = list(getattr(self.__class__, _SPEC).keys())[0] # lol guess
return match.byte_length
BIT_CONST_CHARS = set('01.-')
def load_time_parse(specification):
"""Parses spec into a match pattern and dict of variables.
The hand-rolled parser implementation is super-gnarly, but that's okay if
it never leaks out of this function. This is a key source of user-facing
error messages so it's worth keeping the exception call stack minimal.
Gnarly implementation regression-test hall of shame:
>>> parse = bitspec.load_time_parse
>>> assert (x := parse('....0001')) == parse('.... 0001'); x
(<Match ....0001 /1>, {})
>>> assert (x := parse('0x0008')) != parse('0x08'); x
(<Match 0000000000001000 /2>, {})
"""
spec_width = 0 # running total in bits
fields = [] # tagged-tuple IR := ('c' index mask const bit_width)
# | ('v' index mask name is_signed)
# parse spec, indexing bit-offsets starting from zero @ most significant
# (will get re-indexed later once full width is known)
for token in specification.split():
if set(token).issubset(BIT_CONST_CHARS):
const_value = int('0b' + token.replace('.','0')
.replace('-','0'), 2)
const_mask = int('0b' + token.replace('0','1')
.replace('.','0')
.replace('-','0'), 2)
const_width = len(token)
fields.append(('c', spec_width, const_mask, const_value, const_width))
spec_width += const_width
elif token.startswith('0x'):
const_value = int(token, 16)
const_width = len(token[2:]) * 4
const_mask = (1 << const_width) - 1
if len(token) == 3:
suggest_a = token[:2] + '0' + token[2]
suggest_b = bin(const_value)[2:]
suggest_b = '0' * (const_width - len(suggest_b)) + suggest_b
msg = f'''byte constant {token} may be confused for 4-bit nibble
Please either explicitly pad byte values smaller than 16 (e.g. {suggest_a}) or
write nibble-sized constants in bit form (e.g. {suggest_b}.)'''
raise SyntaxWarning(msg)
if spec_width % 8 != 0:
suggest = bin(const_value)[2:]
suggest = '0' * (const_width - len(suggest)) + suggest
msg = f'''byte constant {token} is not byte-aligned
Check declaration for bugs, or re-write constant as {suggest} bits.'''
raise IndexError(msg)
fields.append(('c', spec_width, const_mask, const_value, const_width))
spec_width += const_width
elif ':' in token:
var_name, var_kind = token.split(':')
if not var_name.isidentifier() and var_name not in ('.', '-'):
msg = f"{var_name} in {token} isn't a Python identifier or . or -"
raise SyntaxError(msg)
if is_signed := var_kind.startswith('s'):
var_kind = var_kind[1:]
var_width = int(var_kind)
if var_name in ('.', '-'):
const_value = 0
const_mask = 0
fields.append(('c', spec_width, const_mask, const_value, var_width))
else:
var_mask = (1 << var_width) - 1
fields.append(('v', spec_width, var_mask, var_name, is_signed))
spec_width += var_width
else:
# could add a opc2c-style name-length-encoding here, but it's more
# cutesy code golf than a real space saver
raise SyntaxError(f'''unidentified field {token!r}
Valid fields should be bytes (0xf00f) bits (010..11. where . is a "don't-care"
wildcard) or variable bindings (signed_jump:s24, imm:16, etc.)''')
if spec_width % 8 != 0:
msg = f'''{spec_width}-bit pattern width isn't a multiple of 8
Most encodings are byte-aligned. There might be a subtle error in the
specification, or just some forgotten '....' don't-care padding.'''
raise IndexError(msg)
# accumulate constant fields (remember that index is wrong bit-endianness)
const_bits = [(mask, const, spec_width - index - width)
for ty,index,mask,const,width in fields if ty == 'c']
mask, const = 0, 0
for m,c,shift in const_bits:
mask |= m << shift
const |= c << shift
match = Match(mask, const, spec_width // 8)
# accumulate variable fields
var_fields = [(name,Slice(mask << (spec_width - index - mask.bit_length()),
spec_width - index - mask.bit_length(),
signed,
spec_width // 8))
for ty,index,mask,name,signed in fields if ty == 'v']
assert len(var_fields) == len(set(v[0] for v in var_fields)) # TODO dups
var_fields = {name:slicer for name,slicer in var_fields}
return match, var_fields
class Match(typing.NamedTuple):
"""Match some constant bits inside an exact length of bytes."""
mask: int
const: int
byte_length: int
def __repr__(self):
fmt = f'{{0:0{self.byte_length * 8}b}}'
lut = {('0','0'):'.', ('0','1'):'.',
('1','0'):'0', ('1','1'):'1'}
wildcard = ''.join([lut[w] for w in zip(fmt.format(self.mask),
fmt.format(self.const))])
return f'<Match {wildcard} /{self.byte_length}>'
def matches(self, bytes): # slow path
"""Exact-length pattern match.
>>> m = bitspec.Match(0xffff00, 0xdead00, 4)
>>> [m.matches(bs) for bs in [b'\\xff\\xde\\xad\\xff',
... b'\\xff\\xde\\xad\\xffAAA']]
[True, False]
"""
if len(bytes) != self.byte_length:
return False
n = int.from_bytes(bytes, byteorder='big')
return (n & self.mask) == self.const
def expand_by(self, byte_length):
"""Don't change pattern, but add some don't-care bytes after it."""
if byte_length == 0:
return self
elif byte_length > 0:
bits = byte_length * 8
return Match(self.mask << bits, self.const << bits,
self.byte_length + byte_length)
else:
raise NotImplementedError(f'{byte_length} on {self}')
@staticmethod
def multimatch_precompute(matches) -> object:
"""Match lots of stuff at slightly better than O(n)
[(Match, any)] -> bytes -> [any]
"""
unique_masks = set((m.byte_length, m.mask) for m,_ in matches)
first = lambda t:t[0]
lut = {}
for _, mask in unique_masks:
const_lut = []
for match,result in matches:
const_lut += [(m.const, result) for m,result in matches
if m.mask == mask]
lut[mask] = {const: list(t[1] for t in group)
for const,group in groupby(sorted(const_lut, key=first),
key=first)}
return unique_masks,lut
@staticmethod
def multimatch_execute(opaque, bytes) -> list:
unique_masks,lut = opaque
acc = []
for byte_length, mask in unique_masks:
if len(bytes) < byte_length:
continue
n = int.from_bytes(bytes[:byte_length], byteorder='big')
acc += lut[mask].get(n & mask, [])
return acc
class Slice(typing.NamedTuple):
"""Extract a contiguous region of bits from fixed length of bytes.
>>> s = bitspec.Slice(0x00ff00, 8, True, 3)
>>> [s.from_bytes(bs) for bs in [b'\\xfa\\xfe\\xaf', b'\\xaf\\xff\\xfa',
... b'\\xaf\\x00\\xfa', b'\\xfa\\x01\\xaf',
... b'A ABBB']]
[-2, -1, 0, 1, 32]