rsbinder 0.9.0

rsbinder provides crates implemented in pure Rust that make Binder IPC available on both Android and Linux.
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
// Copyright 2022 Jeff Kim <hiking90@gmail.com>
// SPDX-License-Identifier: Apache-2.0

//! `Android13PlusCodec` — the android-13+ *versioned* RPC wire, an
//! **additive** [`super::wire::WireCodec`] impl behind the same trait
//! as [`super::wire::R34Codec`].
//! `R34Codec` is byte-unchanged; nothing here touches the kernel path.
//!
//! One codec, **version-keyed** — exactly AOSP's own design
//! (`RpcWireReply::wireSize(protocolVersion)`):
//!
//! * **v0** = android-13 (`RPC_WIRE_PROTOCOL_VERSION = 0`)
//! * **v1** = android-14 **and** android-15
//!   (`RPC_WIRE_PROTOCOL_VERSION = 1`; their `RpcWireFormat.h` is
//!   byte-identical to each other)
//! * **v2** = android-16 (`RPC_WIRE_PROTOCOL_VERSION = 2`,
//!   `RPC_HEADER_INCLUDES_BINDER_POSITIONS`)
//!
//! The negotiated version is selected at runtime by the connection
//! handshake (`RpcConnectionHeader`/`RpcNewSessionResponse`). r34
//! (android-12, pre-versioning, 32-byte address, no handshake) stays a
//! separate codec.
//!
//! # v1 ≡ v2 framing (verified vs `android-16.0.0_r4`)
//!
//! v2's wire-protocol delta is **not** a framing change. A full sweep
//! of `RpcState.cpp` + `RpcWireFormat.h` (android-16.0.0_r4) shows
//! **zero** `>= RPC_HEADER_INCLUDES_BINDER_POSITIONS` branches in the
//! framing layer — every wire branch is v0-vs-v1+
//! (`RpcWireReply::wireSize` = `v0?4:20`, the `>= EXPLICIT_PARCEL_SIZE`
//! object-table split). The only `INCLUDES_BINDER_POSITIONS` uses are
//! in `Parcel.cpp` (`flattenBinder`/`unflattenBinder`/
//! `rpcSetDataReference`): whether a *binder* (vs only an FD) position
//! is recorded in `mObjectPositions`. So v1 and v2 share **one
//! framing path**; the codec is version-agnostic about the object
//! table's *contents* (it just frames the trailing `u32[]`), and the
//! v1↔v2 distinction lives entirely in the Parcel position producer.
//! A no-object parcel ⇒ empty table ⇒ byte-identical v1/v2 wire
//! (structural v1 no-regression).
//!
//! # Spec of record
//!
//! Extracted byte-exact from AOSP `frameworks/native`
//! (`android-13.0.0_r84` for v0, `android-15.0.0_r36` == v1 ==
//! `android-14.0.0_r75`), device-free — the same route used for r34.
//! Source files: `libs/binder/RpcWireFormat.h`,
//! `include/binder/RpcSession.h`, `RpcSession.cpp`, `RpcState.cpp`,
//! `tests/binderRpcWireProtocolTest.cpp`.
//!
//! ## Unchanged across r34 / v0 / v1
//!
//! `RpcWireHeader` (16 B `{u32 command; u32 bodySize;
//! u32 reserved[2]}`), the command enum
//! (`TRANSACT=0/REPLY=1/DEC_STRONG=2`), the zero-address special
//! transacts (`GET_ROOT=0/GET_MAX_THREADS=1/GET_SESSION_ID=2`).
//!
//! ## r34 → v0 (android-13)
//!
//! * `RpcWireAddress`: `u8[32]` → `{u32 options; u32 address}` = **8 B**
//!   (`CREATED=1<<0`, `FOR_SERVER=1<<1`).
//! * `RpcWireTransaction`: 64 B fixed → **40 B**
//!   (`addr(8)|code|flags|asyncNumber|reserved[4]`).
//! * `RpcDecStrong`: address-only → **16 B**
//!   (`addr(8)|u32 amount|u32 reserved`).
//! * bare `int32 RPC_SESSION_ID_NEW=-1` preamble → versioned
//!   connection handshake.
//!
//! ## v0 → v1 (android-14 / android-15)
//!
//! * `RpcWireReply`: v0 = **4 B** (`i32 status`) → v1 = **20 B**
//!   (`i32 status; u32 parcelDataSize; u32 reserved[3]`). AOSP's
//!   `RpcWireReply::wireSize(version)` is `v0 ⇒ 4`, `v1 ⇒ 20`.
//! * `RpcWireTransaction` (still 40 B): the first `reserved` word
//!   becomes `u32 parcelDataSize` (size of the Parcel data that
//!   follows), then `reserved[3]`.
//! * `RpcConnectionHeader` (still 16 B): a reserved byte becomes
//!   `u8 fileDescriptorTransportMode` (`NONE=0/UNIX=1/TRUSTY=2`) —
//!   FD mode is negotiated *in the connection header* at v1, not via
//!   the separate `GET_FD_MODE` special transact rsbinder uses.
//!
//! ## v1 → v2 (android-16)
//!
//! **No struct/framing change.** `RpcWireFormat.h` is byte-identical to
//! v1 (`RpcWireReply::wireSize` still `v0?4:20`; `RpcWireTransaction`
//! still 40 B with `parcelDataSize`). The only delta is that the
//! trailing object table (`mObjectPositions`, framed as a `u32[]` after
//! the parcel data — already present at v1 for FD positions) now also
//! carries **binder** positions. That is a Parcel-producer concern,
//! not a codec/framing concern: the codec frames the `u32[]`
//! identically for v1 and v2.
//!
//! Version constants (`include/binder/RpcSession.h` @
//! **android-16.0.0_r4**): `RPC_WIRE_PROTOCOL_VERSION = 2`,
//! `RPC_WIRE_PROTOCOL_VERSION_NEXT = 3`,
//! `..._EXPERIMENTAL = 0xF000_0000`,
//! `..._RPC_HEADER_FEATURE_EXPLICIT_PARCEL_SIZE = 1`,
//! `..._RPC_HEADER_INCLUDES_BINDER_POSITIONS = 2`.
//! `setProtocolVersion` rejects `version >= _NEXT && version !=
//! _EXPERIMENTAL` — so a peer that supports up to v2 accepts
//! {0, 1, 2, EXPERIMENTAL}.
//!
//! ## Scope
//!
//! Delivers the **spec-conformance** half (byte-exact codec + golden
//! vectors from `RpcWireFormat.h`), the same device-free route used for
//! r34. Matching a real peer's `RpcState` node-id / `FOR_SERVER`
//! address semantics is a separate refinement, as is the Parcel-body
//! layer (AOSP `kCurrentRepr`).

use std::io::{Read, Write};

use super::address::RpcAddress;
use super::transport::MAX_FRAME_LEN;
use super::wire::{WireCodec, WireMessage, WireReply, WireTransaction};
use super::{RpcError, RpcResult};

/// `RpcWireHeader` size (unchanged r34 / v0 / v1).
const WIRE_HEADER_LEN: usize = 16;
/// android-13+ `RpcWireAddress` size (`u32 options; u32 address`).
pub const A13_ADDR_LEN: usize = 8;
/// `RpcWireTransaction` fixed prefix (v0 & v1 are both 40 B; only the
/// first reserved word's *meaning* differs — `parcelDataSize` at v1).
pub const A13_TXN_FIXED_LEN: usize = A13_ADDR_LEN + 4 + 4 + 8 + 16; // = 40
/// `RpcDecStrong` size (`addr(8) + amount(4) + reserved(4)`).
pub const A13_DEC_STRONG_LEN: usize = A13_ADDR_LEN + 4 + 4; // = 16
/// `RpcConnectionHeader` size (v0 & v1 both 16 B).
pub const A13_CONN_HEADER_LEN: usize = 16;
/// `RpcNewSessionResponse` size.
pub const A13_NEW_SESSION_RESP_LEN: usize = 8;
/// `RpcOutgoingConnectionInit` size.
pub const A13_CONN_INIT_LEN: usize = 8;

/// `RpcWireHeader.command` (unchanged).
const CMD_TRANSACT: u32 = 0;
const CMD_REPLY: u32 = 1;
const CMD_DEC_STRONG: u32 = 2;

/// `RPC_WIRE_ADDRESS_OPTION_*` (RpcWireFormat.h).
const ADDR_OPTION_CREATED: u32 = 1 << 0;
const ADDR_OPTION_FOR_SERVER: u32 = 1 << 1;

/// `RPC_CONNECTION_OPTION_INCOMING` (default is outgoing).
pub const CONN_OPTION_INCOMING: u8 = 0x1;

/// `RPC_CONNECTION_INIT_OKAY = "cci"` (+ NUL ⇒ 4 wire bytes).
pub const CONN_INIT_OKAY: [u8; 4] = *b"cci\0";

/// android-13 stable wire protocol version (`RPC_WIRE_PROTOCOL_VERSION`).
pub const PROTOCOL_V0: u32 = 0;
/// android-14 / android-15 stable wire protocol version
/// (`RPC_WIRE_PROTOCOL_VERSION_RPC_HEADER_FEATURE_EXPLICIT_PARCEL_SIZE`).
pub const PROTOCOL_V1: u32 = 1;
/// android-16 stable wire protocol version
/// (`RPC_WIRE_PROTOCOL_VERSION_RPC_HEADER_INCLUDES_BINDER_POSITIONS`).
/// Framing is byte-identical to [`PROTOCOL_V1`]; the only delta is that
/// binder positions also enter the object table (a Parcel-producer
/// concern).
pub const PROTOCOL_V2: u32 = 2;
/// Highest version this codec implements (android-16.0.0_r4
/// `RPC_WIRE_PROTOCOL_VERSION`).
pub const SUPPORTED_MAX_VERSION: u32 = PROTOCOL_V2;
/// `RPC_WIRE_PROTOCOL_VERSION_NEXT` (android-16.0.0_r4) — the first
/// version rsbinder cannot speak; `setProtocolVersion` rejects
/// `>= _NEXT` (unless `_EXPERIMENTAL`).
pub const RPC_WIRE_PROTOCOL_VERSION_NEXT: u32 = 3;
/// `RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL`.
pub const RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL: u32 = 0xF000_0000;

/// `RpcSession::FileDescriptorTransportMode` (u8) — the v1
/// `RpcConnectionHeader.fileDescriptorTransportMode` byte. rsbinder
/// only ever advertises `NONE`/`UNIX`; `TRUSTY` is out of scope but
/// defined for wire fidelity.
pub const FD_MODE_NONE: u8 = 0;
pub const FD_MODE_UNIX: u8 = 1;
pub const FD_MODE_TRUSTY: u8 = 2;

/// AOSP `RpcFields::ObjectType::TYPE_NATIVE_FILE_DESCRIPTOR`
/// (`Parcel.h`, `int32_t`) — the first int32 of an RPC-Parcel fd
/// object body. Pinned byte-exact android-14.0.0_r75…android-16.0.0_r4
/// (`TYPE_BINDER_NULL=0, TYPE_BINDER=1, TYPE_NATIVE_FILE_DESCRIPTOR=2`).
/// Load-bearing for the AOSP-faithful FD-over-RPC v1+ Parcel body
/// (`[not-null|hasComm|TYPE|fdIndex]`) that replaces rsbinder's
/// internal `[present|idx]` shape.
pub const TYPE_NATIVE_FILE_DESCRIPTOR: i32 = 2;

/// `setProtocolVersion` acceptance for a peer that supports up to
/// [`SUPPORTED_MAX_VERSION`] (AOSP rule, android-16.0.0_r4 `_NEXT = 3`):
/// accept `0..=2`, or exactly `_EXPERIMENTAL`. Equivalent to AOSP's
/// `version < _NEXT || version == _EXPERIMENTAL` since
/// `SUPPORTED_MAX_VERSION == _NEXT - 1`.
pub fn is_supported_protocol_version(version: u32) -> bool {
    version <= SUPPORTED_MAX_VERSION || version == RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL
}

/// `RpcWireReply::wireSize(protocolVersion)` (RpcWireFormat.h): v0 is
/// just `i32 status` (4 B); v1+ (incl. v2 — byte-identical) adds
/// `parcelDataSize + reserved[3]` (20 B). The Parcel data follows
/// this fixed prefix.
fn reply_fixed_len(version: u32) -> usize {
    if version == PROTOCOL_V0 {
        4
    } else {
        20
    }
}

/// `true` once the wire carries a trailing object table — i.e. v1+
/// (`>= RPC_WIRE_PROTOCOL_VERSION_RPC_HEADER_FEATURE_EXPLICIT_PARCEL_SIZE`).
/// v0 has no `parcelDataSize` and no object table at all. (v1 and v2
/// are identical here — the v1↔v2 distinction is purely *which*
/// objects the Parcel producer records.)
fn has_object_table(version: u32) -> bool {
    version >= PROTOCOL_V1
}

// --- bounds-checked LE readers (local — keeps wire.rs byte-unchanged) -

fn rd_u32(buf: &[u8], off: usize) -> RpcResult<u32> {
    let end = off
        .checked_add(4)
        .ok_or(RpcError::Protocol("offset overflow"))?;
    let s = buf
        .get(off..end)
        .ok_or(RpcError::Protocol("truncated u32"))?;
    Ok(u32::from_le_bytes(s.try_into().unwrap()))
}

fn rd_u64(buf: &[u8], off: usize) -> RpcResult<u64> {
    let end = off
        .checked_add(8)
        .ok_or(RpcError::Protocol("offset overflow"))?;
    let s = buf
        .get(off..end)
        .ok_or(RpcError::Protocol("truncated u64"))?;
    Ok(u64::from_le_bytes(s.try_into().unwrap()))
}

/// The android-13+ versioned RPC wire codec (additive).
/// `version` is the negotiated `RPC_WIRE_PROTOCOL_VERSION`
/// ([`PROTOCOL_V0`] = android-13, [`PROTOCOL_V1`] = android-14/15).
#[derive(Debug, Clone, Copy)]
pub struct Android13PlusCodec {
    version: u32,
}

impl Default for Android13PlusCodec {
    /// Defaults to v0 (android-13) — the lowest, always-safe version.
    fn default() -> Self {
        Self::android13()
    }
}

impl Android13PlusCodec {
    /// v0 — android-13.
    pub fn android13() -> Self {
        Self {
            version: PROTOCOL_V0,
        }
    }

    /// v1 — android-14 and android-15 (identical wire).
    pub fn android14_15() -> Self {
        Self {
            version: PROTOCOL_V1,
        }
    }

    /// v2 — android-16. Framing byte-identical to v1; differs only in
    /// that the Parcel producer also records binder positions in the
    /// object table.
    pub fn android16() -> Self {
        Self {
            version: PROTOCOL_V2,
        }
    }

    /// Build for an explicit negotiated version; rejects an
    /// unsupported one (mirrors `setProtocolVersion`).
    ///
    /// **`RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL` (`0xF000_0000`) is
    /// stored verbatim**. AOSP `RpcState::validateProtocolVersion` and
    /// `RpcSession::setProtocolVersionInternal` (android-16.0.0_r4)
    /// store the sentinel as-is, advertise it on the wire, and drive
    /// v1+ framing from it (because `version >= PROTOCOL_V1` holds for
    /// `0xF000_0000`). rsbinder mirrors that exactly — do **not**
    /// normalize EXPERIMENTAL to `SUPPORTED_MAX_VERSION`.
    pub fn with_version(version: u32) -> RpcResult<Self> {
        if !is_supported_protocol_version(version) {
            return Err(RpcError::Protocol("unsupported RPC wire protocol version"));
        }
        Ok(Self { version })
    }

    /// The negotiated protocol version this codec encodes/decodes.
    pub fn version(&self) -> u32 {
        self.version
    }

    fn header(command: u32, body_size: usize) -> RpcResult<[u8; WIRE_HEADER_LEN]> {
        // Encoder/decoder symmetry: the decoder rejects
        // `body_size > MAX_FRAME_LEN` at every entry
        // (see `decode_message`, `read_aosp_message`,
        // `write_aosp_message`); without this guard the encoder would
        // silently truncate a `body_size > u32::MAX` via `as u32` on
        // 64-bit hosts, emitting a header whose `bodySize` disagrees
        // with the actual payload — a peer reading per `bodySize`
        // would misframe the next message.
        if body_size > MAX_FRAME_LEN {
            return Err(RpcError::FrameTooLarge {
                declared: body_size,
                max: MAX_FRAME_LEN,
            });
        }
        let mut h = [0u8; WIRE_HEADER_LEN];
        h[0..4].copy_from_slice(&command.to_le_bytes());
        h[4..8].copy_from_slice(&(body_size as u32).to_le_bytes());
        // reserved[2] stays zero.
        Ok(h)
    }

    /// Project rsbinder's 32-byte [`RpcAddress`] onto the android-13+
    /// 8-byte `RpcWireAddress { options, address }` (unchanged v0↔v1).
    ///
    /// rsbinder's address is `counter:le_u64 @0..8 | role_tag @8`
    /// ([`RpcAddress::unique`]): zero → `{0, 0}` (the special all-zero
    /// address; `CREATED` unset); else `address = low 32 bits of the
    /// counter`, `options = CREATED | (FOR_SERVER if Acceptor-minted)`.
    /// Documented + internally consistent (round-trips within
    /// rsbinder); matching a live peer's `RpcState` node-id semantics
    /// is a separate refinement.
    ///
    /// `pub(crate)` because the *in-parcel* binder encoding
    /// (`flattenBinder` RPC branch: `i32 present` + `writeUint64`) must
    /// use this same 8-byte `RpcWireAddress` — the session's
    /// `write_binder`/`read_binder` route through it for the
    /// android-13+ profile (a real libbinder peer rejects r34's 32-byte
    /// in-parcel address).
    pub(crate) fn encode_addr(addr: &RpcAddress) -> [u8; A13_ADDR_LEN] {
        let mut out = [0u8; A13_ADDR_LEN];
        if addr.is_zero() {
            return out;
        }
        let raw = addr.as_wire_bytes();
        let address = u32::from_le_bytes([raw[0], raw[1], raw[2], raw[3]]);
        let mut options = ADDR_OPTION_CREATED;
        if raw[8] == 2 {
            options |= ADDR_OPTION_FOR_SERVER;
        }
        out[0..4].copy_from_slice(&options.to_le_bytes());
        out[4..8].copy_from_slice(&address.to_le_bytes());
        out
    }

    /// Inverse of [`Android13PlusCodec::encode_addr`]; `pub(crate)` for
    /// the in-parcel binder decode (`unflattenBinder` RPC branch).
    pub(crate) fn decode_addr(buf: &[u8], off: usize) -> RpcResult<RpcAddress> {
        let options = rd_u32(buf, off)?;
        let address = rd_u32(buf, off + 4)?;
        if options & ADDR_OPTION_CREATED == 0 && address == 0 {
            return Ok(RpcAddress::zero());
        }
        let mut bytes = [0u8; super::address::RPC_ADDR_LEN];
        bytes[0..4].copy_from_slice(&address.to_le_bytes());
        bytes[8] = if options & ADDR_OPTION_FOR_SERVER != 0 {
            2
        } else {
            1
        };
        Ok(RpcAddress::from_wire_bytes(bytes))
    }

    // --- connection handshake --------------------------------------

    /// `RpcConnectionHeader` (16 B) + `session_id` bytes
    /// (empty ⇒ request a new session). `fd_mode` is written into the
    /// v1 `fileDescriptorTransportMode` byte; at v0 that byte is part
    /// of `reserved` (so `fd_mode` is ignored — v0 negotiates FD mode
    /// via the separate `GET_FD_MODE` transact).
    pub fn encode_connection_header(
        &self,
        incoming: bool,
        fd_mode: u8,
        session_id: &[u8],
    ) -> RpcResult<Vec<u8>> {
        // Explicit `u16` bound on `sessionIdSize`. AOSP
        // `RpcConnectionHeader.sessionIdSize` is a `uint16_t`; this is a
        // `pub fn` and a caller passing a 64 KiB+ slice would otherwise
        // wrap the on-wire size while the full body was still appended
        // — a peer reading per `sessionIdSize` would misframe the
        // following message.
        let id_size: u16 = session_id
            .len()
            .try_into()
            .map_err(|_| RpcError::Protocol("session_id length exceeds u16"))?;
        let mut out = Vec::with_capacity(A13_CONN_HEADER_LEN + session_id.len());
        out.extend_from_slice(&self.version.to_le_bytes()); // u32 version
        out.push(if incoming { CONN_OPTION_INCOMING } else { 0 }); // u8 options
        if self.version == PROTOCOL_V0 {
            out.extend_from_slice(&[0u8; 9]); // u8 reserved[9]
        } else {
            out.push(fd_mode); // u8 fileDescriptorTransportMode
            out.extend_from_slice(&[0u8; 8]); // u8 reserved[8]
        }
        out.extend_from_slice(&id_size.to_le_bytes()); // u16 sessionIdSize
        out.extend_from_slice(session_id);
        Ok(out)
    }

    /// Parse an `RpcConnectionHeader`; returns `(version, options,
    /// fd_mode, session_id)`. `fd_mode` is `0` for a v0 header.
    pub fn decode_connection_header(&self, buf: &[u8]) -> RpcResult<(u32, u8, u8, Vec<u8>)> {
        if buf.len() < A13_CONN_HEADER_LEN {
            return Err(RpcError::Protocol("RpcConnectionHeader truncated"));
        }
        let version = rd_u32(buf, 0)?;
        let options = buf[4];
        let fd_mode = if version == PROTOCOL_V0 { 0 } else { buf[5] };
        // Symmetric with `server_accept`.
        if !matches!(fd_mode, FD_MODE_NONE | FD_MODE_UNIX | FD_MODE_TRUSTY) {
            return Err(RpcError::Protocol(
                "unknown RpcConnectionHeader.fileDescriptorTransportMode",
            ));
        }
        let id_size = u16::from_le_bytes([buf[14], buf[15]]) as usize;
        let end = A13_CONN_HEADER_LEN
            .checked_add(id_size)
            .ok_or(RpcError::Protocol("sessionIdSize overflow"))?;
        let session_id = buf
            .get(A13_CONN_HEADER_LEN..end)
            .ok_or(RpcError::Protocol("session id truncated"))?
            .to_vec();
        Ok((version, options, fd_mode, session_id))
    }

    /// `RpcNewSessionResponse` (8 B).
    pub fn encode_new_session_response(&self, version: u32) -> [u8; A13_NEW_SESSION_RESP_LEN] {
        let mut out = [0u8; A13_NEW_SESSION_RESP_LEN];
        out[0..4].copy_from_slice(&version.to_le_bytes());
        out
    }

    /// Parse `RpcNewSessionResponse`; rejects an unsupported version
    /// per `setProtocolVersion`.
    pub fn decode_new_session_response(&self, buf: &[u8]) -> RpcResult<u32> {
        if buf.len() < A13_NEW_SESSION_RESP_LEN {
            return Err(RpcError::Protocol("RpcNewSessionResponse truncated"));
        }
        let version = rd_u32(buf, 0)?;
        if !is_supported_protocol_version(version) {
            return Err(RpcError::Protocol("unsupported RPC wire protocol version"));
        }
        Ok(version)
    }

    /// `RpcOutgoingConnectionInit` (8 B, `msg="cci\0"`).
    pub fn encode_connection_init(&self) -> [u8; A13_CONN_INIT_LEN] {
        let mut out = [0u8; A13_CONN_INIT_LEN];
        out[0..4].copy_from_slice(&CONN_INIT_OKAY);
        out
    }

    /// Verify an `RpcOutgoingConnectionInit` (`strncmp(msg,"cci",4)`).
    pub fn decode_connection_init(&self, buf: &[u8]) -> RpcResult<()> {
        if buf.len() < A13_CONN_INIT_LEN {
            return Err(RpcError::Protocol("RpcOutgoingConnectionInit truncated"));
        }
        if buf[0..4] != CONN_INIT_OKAY {
            return Err(RpcError::Protocol("bad RpcOutgoingConnectionInit msg"));
        }
        Ok(())
    }
}

/// AOSP `RpcState::sendTransaction`/`reply`: append the parcel data
/// then the object table as a trailing LE `u32[]` (`objectTableSpan
/// .toIovec()`). The wire body is `[fixed prefix][parcel data
/// (parcelDataSize bytes)][object table (4·N bytes)]`. v0 has no
/// object table (`validateParcel` rejects v0 + non-empty positions);
/// v1 and v2 are byte-identical here (the table is just a `u32[]`,
/// version-agnostic).
fn encode_data_and_table(
    out: &mut Vec<u8>,
    version: u32,
    data: &[u8],
    object_positions: &[u32],
) -> RpcResult<()> {
    if !has_object_table(version) {
        if !object_positions.is_empty() {
            // AOSP `RpcState::validateParcel` (RpcState.cpp:1469):
            // `protocolVersion < EXPLICIT_PARCEL_SIZE && !mObjectPositions
            // .empty()` ⇒ BAD_VALUE.
            return Err(RpcError::Protocol(
                "v0 wire has no object table (objects need protocol version >= 1)",
            ));
        }
        out.extend_from_slice(data);
        return Ok(());
    }
    out.extend_from_slice(data);
    for pos in object_positions {
        out.extend_from_slice(&pos.to_le_bytes());
    }
    Ok(())
}

/// Inverse of [`encode_data_and_table`]: AOSP's `parcelSpan.splitOff(
/// parcelDataSize)` + `objectTableBytes->reinterpret<uint32_t>()`
/// (`RpcState.cpp:840-866`/`1144-1176`). `rest` is the body after the
/// fixed prefix. This does **length + %4 validation only**; strict v2
/// position-content validation (`binary_search`/range) is a separate
/// step — a lenient decoder still interops.
fn split_data_and_table(
    version: u32,
    rest: &[u8],
    parcel_data_size: usize,
) -> RpcResult<(Vec<u8>, Vec<u32>)> {
    if !has_object_table(version) {
        // v0: no parcelDataSize, no object table — the whole `rest`
        // is parcel data (bodySize authoritative).
        return Ok((rest.to_vec(), Vec::new()));
    }
    // `splitOff(parcelDataSize)` ⇒ nullopt (⇒ BAD_VALUE) if it runs
    // past the available bytes.
    if parcel_data_size > rest.len() {
        return Err(RpcError::Protocol(
            "parcelDataSize larger than available bytes",
        ));
    }
    let (data, table_bytes) = rest.split_at(parcel_data_size);
    // `reinterpret<uint32_t>()` ⇒ nullopt (⇒ BAD_VALUE) if the object
    // table byte length isn't a whole number of u32.
    if table_bytes.len() % 4 != 0 {
        return Err(RpcError::Protocol(
            "object table byte size not a multiple of 4",
        ));
    }
    let positions = table_bytes
        .chunks_exact(4)
        .map(|c| u32::from_le_bytes([c[0], c[1], c[2], c[3]]))
        .collect();
    Ok((data.to_vec(), positions))
}

impl WireCodec for Android13PlusCodec {
    fn encode_transact(&self, txn: &WireTransaction) -> RpcResult<Vec<u8>> {
        // 40 B fixed at all versions; at v1+ the first reserved word
        // carries parcelDataSize (size of the Parcel data following),
        // then the object table is appended as a trailing LE u32[]
        // (bodySize = 40 + parcelDataSize + 4·N). v0: no table.
        let table_bytes = if has_object_table(self.version) {
            4 * txn.object_positions.len()
        } else {
            0
        };
        let body_len = A13_TXN_FIXED_LEN + txn.data.len() + table_bytes;
        let header = Self::header(CMD_TRANSACT, body_len)?;
        let mut out = Vec::with_capacity(WIRE_HEADER_LEN + body_len);
        out.extend_from_slice(&header);
        out.extend_from_slice(&Self::encode_addr(&txn.address)); // 8
        out.extend_from_slice(&txn.code.to_le_bytes()); // 4
        out.extend_from_slice(&txn.flags.to_le_bytes()); // 4
        out.extend_from_slice(&txn.async_number.to_le_bytes()); // 8
        let parcel_size = if self.version == PROTOCOL_V0 {
            0u32
        } else {
            txn.data.len() as u32
        };
        out.extend_from_slice(&parcel_size.to_le_bytes()); // 4 (v0: reserved)
        out.extend_from_slice(&[0u8; 12]); // reserved[3]
        encode_data_and_table(&mut out, self.version, &txn.data, &txn.object_positions)?;
        Ok(out)
    }

    fn encode_reply(&self, reply: &WireReply) -> RpcResult<Vec<u8>> {
        let fixed = reply_fixed_len(self.version);
        let table_bytes = if has_object_table(self.version) {
            4 * reply.object_positions.len()
        } else {
            0
        };
        let body_len = fixed + reply.data.len() + table_bytes;
        let header = Self::header(CMD_REPLY, body_len)?;
        let mut out = Vec::with_capacity(WIRE_HEADER_LEN + body_len);
        out.extend_from_slice(&header);
        out.extend_from_slice(&reply.status.to_le_bytes()); // i32 status
        if self.version != PROTOCOL_V0 {
            out.extend_from_slice(&(reply.data.len() as u32).to_le_bytes()); // parcelDataSize
            out.extend_from_slice(&[0u8; 12]); // reserved[3]
        }
        encode_data_and_table(&mut out, self.version, &reply.data, &reply.object_positions)?;
        Ok(out)
    }

    fn encode_dec_strong(&self, addr: &RpcAddress) -> Vec<u8> {
        // RpcDecStrong { addr(8); u32 amount; u32 reserved } — 16 B,
        // unchanged v0↔v1. rsbinder sends one decrement per drop.
        // `A13_DEC_STRONG_LEN` is a const ≪ `MAX_FRAME_LEN`, so the
        // frame-size guard inside `Self::header` is structurally satisfied.
        let header = Self::header(CMD_DEC_STRONG, A13_DEC_STRONG_LEN)
            .expect("DEC_STRONG body length is a const ≪ MAX_FRAME_LEN");
        let mut out = Vec::with_capacity(WIRE_HEADER_LEN + A13_DEC_STRONG_LEN);
        out.extend_from_slice(&header);
        out.extend_from_slice(&Self::encode_addr(addr)); // 8
        out.extend_from_slice(&1u32.to_le_bytes()); // amount
        out.extend_from_slice(&0u32.to_le_bytes()); // reserved
        out
    }

    fn decode_message(&self, frame: &[u8]) -> RpcResult<WireMessage> {
        if frame.len() < WIRE_HEADER_LEN {
            return Err(RpcError::Protocol("frame shorter than RpcWireHeader"));
        }
        let command = rd_u32(frame, 0)?;
        let body_size = rd_u32(frame, 4)? as usize;
        if body_size > MAX_FRAME_LEN {
            return Err(RpcError::FrameTooLarge {
                declared: body_size,
                max: MAX_FRAME_LEN,
            });
        }
        let expected = WIRE_HEADER_LEN
            .checked_add(body_size)
            .ok_or(RpcError::Protocol("body size overflow"))?;
        if frame.len() != expected {
            return Err(RpcError::Protocol("frame length != header + bodySize"));
        }
        let body = &frame[WIRE_HEADER_LEN..];

        match command {
            CMD_TRANSACT => {
                if body.len() < A13_TXN_FIXED_LEN {
                    return Err(RpcError::Protocol("RpcWireTransaction truncated"));
                }
                let address = Self::decode_addr(body, 0)?;
                let code = rd_u32(body, A13_ADDR_LEN)?;
                let flags = rd_u32(body, A13_ADDR_LEN + 4)?;
                let async_number = rd_u64(body, A13_ADDR_LEN + 8)?;
                // body[24..28] = parcelDataSize (v1+) / reserved (v0);
                // body[28..40] = reserved[3]. At v1+ parcelDataSize is
                // **authoritative** for the data/table split (AOSP
                // `parcelSpan.splitOff(parcelDataSize)`); at v0 there is
                // no table and the whole body tail is the parcel data.
                let parcel_data_size = rd_u32(body, A13_ADDR_LEN + 16)? as usize;
                let (data, object_positions) = split_data_and_table(
                    self.version,
                    &body[A13_TXN_FIXED_LEN..],
                    parcel_data_size,
                )?;
                Ok(WireMessage::Transact(WireTransaction {
                    address,
                    code,
                    flags,
                    async_number,
                    data,
                    object_positions,
                }))
            }
            CMD_REPLY => {
                let fixed = reply_fixed_len(self.version);
                if body.len() < fixed {
                    return Err(RpcError::Protocol("RpcWireReply truncated"));
                }
                let status = rd_u32(body, 0)? as i32;
                // v1+: parcelDataSize @4 is authoritative for the
                // data/table split; reserved[3] @8..20. v0: 4 B fixed,
                // no table.
                let parcel_data_size = if has_object_table(self.version) {
                    rd_u32(body, 4)? as usize
                } else {
                    0
                };
                let (data, object_positions) =
                    split_data_and_table(self.version, &body[fixed..], parcel_data_size)?;
                Ok(WireMessage::Reply(WireReply {
                    status,
                    data,
                    object_positions,
                }))
            }
            CMD_DEC_STRONG => {
                if body.len() != A13_DEC_STRONG_LEN {
                    return Err(RpcError::Protocol("RpcDecStrong body != 16 bytes"));
                }
                let address = Self::decode_addr(body, 0)?;
                // amount @8 — rsbinder applies one decrement per
                // DEC_STRONG; honoring amount>1 from a live peer is a
                // separate refinement.
                Ok(WireMessage::DecStrong(address))
            }
            _ => Err(RpcError::Protocol("unknown RpcWireHeader.command")),
        }
    }

    fn encode_session_preamble(&self, session_id: i32) -> Vec<u8> {
        // Empty `session_id` ⇒ the `u16` sessionIdSize bound is
        // structurally satisfied; the inner `expect` can never fire.
        // android-13+ replaced the bare int32 preamble with the
        // versioned RpcConnectionHeader. rsbinder only opens a new
        // session here (session_id == RPC_SESSION_ID_NEW) and defaults
        // to FD mode NONE; the richer handshake (RpcNewSessionResponse
        // / "cci") uses the inherent methods.
        let _ = session_id;
        self.encode_connection_header(false, FD_MODE_NONE, &[])
            .expect("preamble passes empty session_id ⇒ u16 bound trivially satisfied")
    }

    fn decode_session_preamble(&self, buf: &[u8]) -> RpcResult<i32> {
        // The version-bearing reply is RpcNewSessionResponse; return
        // the negotiated protocol version (the meaningful preamble
        // datum for android-13+; the trait's i32 slot is reinterpreted).
        Ok(self.decode_new_session_response(buf)? as i32)
    }
}

// ---------------------------------------------------------------------
// AOSP-faithful framing + connection handshake
//
// The real android RPC wire has **no length prefix**: a peer writes the
// 16-byte `RpcWireHeader` (whose `bodySize` field is authoritative)
// followed by the body, and the handshake structs are written as raw
// fixed-size structs (AOSP `RpcState::rpcSend`/`rpcRec` —
// `interruptableWriteFully`/`ReadFully` of iovecs, no framing). This is
// distinct from rsbinder's own `RpcTransport` framing, which prepends a
// `u32` length (`transport::write_frame`) — that extra prefix is an
// rsbinder-ism a real android peer neither writes nor expects.
//
// These helpers operate directly on a byte stream (`Read + Write`), so
// they are wire-identical to a genuine android-13/14/15 RPC peer. They
// are the reusable primitives the opt-in `RpcSession` android-13+
// profile wires in; nothing here touches the existing R34
// `RpcSession`/`RpcTransport` path (additive, R34 byte-unchanged).
// ---------------------------------------------------------------------

fn map_io(e: std::io::Error) -> RpcError {
    RpcError::from(e)
}

/// Read exactly `n` bytes. Zero bytes before any progress ⇒ a clean
/// [`RpcError::PeerClosed`]; a short read after partial progress ⇒
/// [`RpcError::Truncated`] (mirrors `transport::read_frame`).
fn read_exact_raw<R: Read>(r: &mut R, n: usize) -> RpcResult<Vec<u8>> {
    let mut buf = vec![0u8; n];
    let mut got = 0;
    while got < n {
        match r.read(&mut buf[got..]) {
            Ok(0) => {
                return Err(if got == 0 {
                    RpcError::PeerClosed
                } else {
                    RpcError::Truncated
                })
            }
            Ok(k) => got += k,
            // A read deadline elapsed (`RawTransportIo` surfaces
            // `recv_raw`'s `Timeout` as `ErrorKind::TimedOut`): a clean
            // `Timeout` before any byte of this read, else mid-frame
            // `Truncated` — honoring the `set_read_timeout` contract rather
            // than collapsing to a generic `Io` via `map_io`.
            Err(ref e) if e.kind() == std::io::ErrorKind::TimedOut => {
                return Err(if got == 0 {
                    RpcError::Timeout
                } else {
                    RpcError::Truncated
                });
            }
            Err(e) => return Err(map_io(e)),
        }
    }
    Ok(buf)
}

/// Write one message **AOSP-faithfully**: `msg` is the codec output
/// (`[RpcWireHeader(16) | body]`, `bodySize` already correct), emitted
/// raw with no length prefix — exactly what a real android peer reads.
pub fn write_aosp_message<W: Write>(w: &mut W, msg: &[u8]) -> RpcResult<()> {
    if msg.len() < WIRE_HEADER_LEN {
        return Err(RpcError::Protocol("message shorter than RpcWireHeader"));
    }
    if msg.len() - WIRE_HEADER_LEN > MAX_FRAME_LEN {
        return Err(RpcError::FrameTooLarge {
            declared: msg.len() - WIRE_HEADER_LEN,
            max: MAX_FRAME_LEN,
        });
    }
    w.write_all(msg).map_err(map_io)?;
    w.flush().map_err(map_io)?;
    Ok(())
}

/// Read one message AOSP-faithfully: read the 16-byte `RpcWireHeader`,
/// take `bodySize` (LE @ offset 4, capped vs [`MAX_FRAME_LEN`] before
/// allocation), then read exactly that many body bytes. Returns
/// `[header | body]`, exactly what [`Android13PlusCodec::decode_message`]
/// expects.
pub fn read_aosp_message<R: Read>(r: &mut R) -> RpcResult<Vec<u8>> {
    let header = read_exact_raw(r, WIRE_HEADER_LEN)?;
    let body_size = u32::from_le_bytes([header[4], header[5], header[6], header[7]]) as usize;
    if body_size > MAX_FRAME_LEN {
        return Err(RpcError::FrameTooLarge {
            declared: body_size,
            max: MAX_FRAME_LEN,
        });
    }
    let mut out = Vec::with_capacity(WIRE_HEADER_LEN + body_size);
    out.extend_from_slice(&header);
    if body_size > 0 {
        // The header is already consumed, so a deadline that elapses at the
        // start of the body is mid-message, not frame-synchronized: report
        // `Truncated`, not `Timeout` (matches `read_aosp_message_with_fds`,
        // which carries `total_read` across header+body).
        let body = read_exact_raw(r, body_size).map_err(|e| match e {
            RpcError::Timeout => RpcError::Truncated,
            other => other,
        })?;
        out.extend_from_slice(&body);
    }
    Ok(out)
}

/// [`write_aosp_message`] + out-of-band `SCM_RIGHTS` fds (the
/// android-13+ v1+ `Unix` FD-over-RPC path). `msg` is
/// the codec output (`[RpcWireHeader(16) | body]`, `bodySize` correct),
/// emitted raw with **no length prefix**; `fds` ride the first
/// `sendmsg` (AOSP `RpcTransportRaw`). With `fds` empty this is exactly
/// [`write_aosp_message`] on the transport's raw channel (byte-identical
/// to the no-FD android-13+ path).
pub fn write_aosp_message_with_fds(
    t: &dyn super::transport::RpcTransport,
    msg: &[u8],
    fds: &[std::os::fd::BorrowedFd<'_>],
) -> RpcResult<()> {
    if msg.len() < WIRE_HEADER_LEN {
        return Err(RpcError::Protocol("message shorter than RpcWireHeader"));
    }
    if msg.len() - WIRE_HEADER_LEN > MAX_FRAME_LEN {
        return Err(RpcError::FrameTooLarge {
            declared: msg.len() - WIRE_HEADER_LEN,
            max: MAX_FRAME_LEN,
        });
    }
    t.send_raw_with_fds(msg, fds)
}

/// [`read_aosp_message`] + the `SCM_RIGHTS` fds delivered with it.
/// Reads the 16-byte `RpcWireHeader`, then
/// exactly `bodySize` body bytes (capped vs [`MAX_FRAME_LEN`] before
/// allocation), **accumulating ancillary fds across every
/// `recvmsg`** that read the message (AOSP
/// `RpcTransportRaw::interruptableReadFully`: the kernel delivers
/// `SCM_RIGHTS` with the first byte of the sender's `sendmsg`, i.e. on
/// the header read). Clean EOF before any byte ⇒ [`RpcError::PeerClosed`];
/// a short read after partial progress ⇒ [`RpcError::Truncated`]
/// (mirrors [`read_aosp_message`]).
pub fn read_aosp_message_with_fds(
    t: &dyn super::transport::RpcTransport,
) -> RpcResult<(Vec<u8>, Vec<std::os::fd::OwnedFd>)> {
    let mut fds: Vec<std::os::fd::OwnedFd> = Vec::new();
    let mut total_read = 0usize;
    // Read exactly `want` bytes via `recvmsg`, accumulating any fds
    // into `fds`. `total_read` tracks progress across header+body so a
    // 0-byte recv distinguishes a clean pre-message close (PeerClosed)
    // from a mid-message truncation (Truncated) — same contract as
    // `read_exact_raw`.
    let mut fill = |want: usize| -> RpcResult<Vec<u8>> {
        let mut buf = vec![0u8; want];
        let mut got = 0;
        while got < want {
            let (n, mut more) = match t.recv_raw_with_fds(&mut buf[got..]) {
                Ok(v) => v,
                // A read deadline elapsed: a clean `Timeout` only before any
                // byte of the message was read; otherwise the message is
                // mid-flight ⇒ `Truncated` (same split as the EOF case below).
                Err(RpcError::Timeout) => {
                    return Err(if total_read == 0 {
                        RpcError::Timeout
                    } else {
                        RpcError::Truncated
                    });
                }
                Err(e) => return Err(e),
            };
            fds.append(&mut more);
            // Enforce the per-message `MAX_FDS_PER_FRAME` cap *across*
            // the multiple recvmsgs that read one message. The
            // transport's per-call cap (`recv_raw_with_fds` rejects
            // > 64 per single recvmsg) is per-recvmsg, not per-message
            // — without this accumulator-side check, a hostile peer
            // that fragments a message across N recvmsgs each carrying
            // 64 fds would accumulate 64·N ancillary fds and walk the
            // process toward `RLIMIT_NOFILE` (DoS bound).
            if fds.len() > super::transport::unix::MAX_FDS_PER_FRAME {
                return Err(RpcError::Protocol(
                    "RPC message exceeded MAX_FDS_PER_FRAME (ancillary fd budget)",
                ));
            }
            if n == 0 {
                return Err(if total_read == 0 {
                    RpcError::PeerClosed
                } else {
                    RpcError::Truncated
                });
            }
            got += n;
            total_read += n;
        }
        Ok(buf)
    };

    let header = fill(WIRE_HEADER_LEN)?;
    let body_size = u32::from_le_bytes([header[4], header[5], header[6], header[7]]) as usize;
    if body_size > MAX_FRAME_LEN {
        return Err(RpcError::FrameTooLarge {
            declared: body_size,
            max: MAX_FRAME_LEN,
        });
    }
    let mut out = Vec::with_capacity(WIRE_HEADER_LEN + body_size);
    out.extend_from_slice(&header);
    if body_size > 0 {
        out.extend_from_slice(&fill(body_size)?);
    }
    Ok((out, fds))
}

/// Client side of the android-13+ connection handshake (new session).
///
/// **Wire order is byte-exact to AOSP `RpcSession::setupClient`
/// (android-13.0.0_r84), validated against a *real* compiled libbinder
/// peer on the Android 13 emulator:**
///
/// 1. write `RpcConnectionHeader` (caller's max version, raw — no framing);
/// 2. write `RpcOutgoingConnectionInit` (`"cci"`) — the *outgoing*
///    connection **initiator sends** this (`addOutgoingConnection(init=
///    true)` → `RpcState::sendConnectionInit`); the acceptor reads it;
/// 3. read `RpcNewSessionResponse` (negotiated version, validated) —
///    AOSP reads this *after* sending `"cci"` (`setupClient` order).
///
/// Returns the [`Android13PlusCodec`] for the **negotiated** version.
pub fn client_connect<S: Read + Write>(
    stream: &mut S,
    max_version: u32,
    incoming: bool,
    fd_mode: u8,
) -> RpcResult<Android13PlusCodec> {
    // Empty id ⇒ request a new session — byte-identical to a
    // single-connection session (the `session_id` slot already
    // existed).
    client_connect_with_id(stream, max_version, incoming, fd_mode, &[])
}

/// Like [`client_connect`] but echoes a server-minted 32-byte
/// `session_id` in the `RpcConnectionHeader`
/// (AOSP `RpcSession::setupClient`: the first connection sends an empty
/// id and reads the server-minted one; the remaining connections echo
/// it). An **empty** `session_id` is byte-for-byte identical to
/// [`client_connect`] (additive: the default path is unchanged).
///
/// **Wire is the mirror of [`server_accept`] across the 4 (new vs.
/// attach) × (outgoing vs. incoming) cells (AOSP `RpcSession.cpp`
/// `initAndAddConnection` + `setupClient` + `addOutgoing/Incoming
/// Connection`):**
///
/// 1. write `RpcConnectionHeader` (`max_version`, optional `INCOMING`
///    bit, optional 32-byte session id);
/// 2. **direction-aware `"cci"` exchange**:
///    - **outgoing**: client writes `"cci"` (`addOutgoingConnection
///      (init=true)` → `sendConnectionInit`);
///    - **incoming**: client reads `"cci"` (`addIncomingConnection` →
///      `preJoinSetup` → `readConnectionInit`).
/// 3. **new session only** (empty `session_id`, outgoing only — the
///    AOSP `RpcSession::setupClient` outer flow reads
///    `RpcNewSessionResponse` after the first `connectAndInit({},
///    false)`): client reads `RpcNewSessionResponse`. Attach (echoed
///    id) does NOT read one (AOSP attaches don't get a new-session
///    response — the founding connection already pinned the version).
///
/// Returns the [`Android13PlusCodec`] for the version this connection
/// negotiated: for a new session, the server-confirmed `min(max,
/// server_max)`; for an attach, `max_version` itself. Attach trusts
/// the founding session's pinned version because the wire carries no
/// per-connection version on attach (AOSP same: `RpcServer.cpp` only
/// writes `RpcNewSessionResponse` for `requestingNewSession`). The
/// caller enforces uniformity by clamping `max_version =
/// min(caller_max, session_version)` *before* the handshake (see
/// [`super::session::RpcSession::add_outgoing_connection_android13plus`]); a peer
/// running a different actual version on an id-echoing attach is an
/// unverifiable wire condition on both sides.
pub fn client_connect_with_id<S: Read + Write>(
    stream: &mut S,
    max_version: u32,
    incoming: bool,
    fd_mode: u8,
    session_id: &[u8],
) -> RpcResult<Android13PlusCodec> {
    let hdr_codec = Android13PlusCodec::with_version(max_version)?;
    let header = hdr_codec.encode_connection_header(incoming, fd_mode, session_id)?;
    write_all_raw(stream, &header)?;
    let requesting_new_session = session_id.is_empty();
    if incoming {
        // attach + incoming (new + incoming is rejected server-side):
        // client reads server-sent init okay.
        let init = read_exact_raw(stream, A13_CONN_INIT_LEN)?;
        hdr_codec.decode_connection_init(&init)?;
    } else {
        // outgoing (both new and attach): client writes init.
        write_all_raw(stream, &hdr_codec.encode_connection_init())?;
    }
    if requesting_new_session {
        let resp = read_exact_raw(stream, A13_NEW_SESSION_RESP_LEN)?;
        let negotiated = hdr_codec.decode_new_session_response(&resp)?;
        if negotiated == hdr_codec.version() {
            Ok(hdr_codec)
        } else {
            Android13PlusCodec::with_version(negotiated)
        }
    } else {
        // Attach: no NewSessionResponse on the wire (AOSP same).
        Ok(hdr_codec)
    }
}

/// Server side of the android-13+ connection handshake.
///
/// Wire order is byte-exact to AOSP `RpcServer::establishConnection`
/// (and `RpcSession::preJoinSetup` / `addOutgoingConnection(init=true)`)
/// in android-16.0.0_r4 across the 4 cells of `(new vs attach)` ×
/// `(outgoing vs incoming)`:
///
/// 1. read `RpcConnectionHeader` (+ variable session id) — parse the
///    `RPC_CONNECTION_OPTION_INCOMING` bit at `head[4]`.
/// 2. **new session only** (empty session id): write
///    `RpcNewSessionResponse` (`min(server_max, client_max)`). AOSP
///    only writes this for `requestingNewSession` (`RpcServer.cpp`
///    `if (requestingNewSession)`); attach echoes don't get one.
/// 3. **direction-aware `"cci"` exchange**:
///    - **outgoing-from-client** (server reads transacts from the
///      client on this slot): server **reads** the client's
///      `RpcOutgoingConnectionInit` (`preJoinSetup` →
///      `readConnectionInit`).
///    - **incoming-from-client** (server *sends* callbacks /
///      `DEC_STRONG` to the client on this slot): server **writes**
///      `"cci"` (`RpcServer.cpp` calls `addOutgoingConnection(client,
///      true /*init*/)` for `incoming` → `sendConnectionInit`).
/// 4. **new + incoming** is rejected (`RpcServer.cpp`: *"Cannot create
///    a new session with an incoming connection, would leak"*).
///
/// Returns the negotiated [`Android13PlusCodec`] plus the client's
/// requested FD mode, session-id, and incoming flag.
pub fn server_accept<S: Read + Write>(
    stream: &mut S,
    server_max_version: u32,
) -> RpcResult<(Android13PlusCodec, u8, Vec<u8>, bool)> {
    // Fixed 16-byte header first, then the variable session id.
    let head = read_exact_raw(stream, A13_CONN_HEADER_LEN)?;
    let client_version = u32::from_le_bytes([head[0], head[1], head[2], head[3]]);
    let options = head[4];
    let incoming = (options & CONN_OPTION_INCOMING) != 0;
    let id_size = u16::from_le_bytes([head[14], head[15]]) as usize;
    let session_id = if id_size > 0 {
        read_exact_raw(stream, id_size)?
    } else {
        Vec::new()
    };
    let fd_mode = if client_version == PROTOCOL_V0 {
        FD_MODE_NONE
    } else {
        head[5]
    };
    // Reject out-of-enum
    // `RpcConnectionHeader.fileDescriptorTransportMode`. AOSP defines
    // the field as an enum {NONE, UNIX, TRUSTY}; an unknown value is
    // malformed input that must not flow to downstream consumers as a
    // u8 caller has to re-validate.
    if !matches!(fd_mode, FD_MODE_NONE | FD_MODE_UNIX | FD_MODE_TRUSTY) {
        return Err(RpcError::Protocol(
            "unknown RpcConnectionHeader.fileDescriptorTransportMode",
        ));
    }
    // min(serverMax, callerMax) — and it must be one we implement.
    let negotiated = client_version.min(server_max_version);
    let codec = Android13PlusCodec::with_version(negotiated)?;
    let requesting_new_session = session_id.is_empty();
    // AOSP order (`RpcServer.cpp` lines 488-507 then 530-534): for any
    // `requesting_new_session` header the response is written first;
    // the incoming-direction "would leak" reject happens *after*.
    if requesting_new_session {
        write_all_raw(stream, &codec.encode_new_session_response(negotiated))?;
    }
    if requesting_new_session && incoming {
        return Err(RpcError::Protocol(
            "new-session request set RPC_CONNECTION_OPTION_INCOMING",
        ));
    }
    if incoming {
        // attach + incoming: server-driven send of the init okay
        // (`addOutgoingConnection(init=true)`).
        write_all_raw(stream, &codec.encode_connection_init())?;
    } else {
        // outgoing-from-client (both new and attach): server reads the
        // client's init (`preJoinSetup` → `readConnectionInit`).
        let init = read_exact_raw(stream, A13_CONN_INIT_LEN)?;
        codec.decode_connection_init(&init)?;
    }
    Ok((codec, fd_mode, session_id, incoming))
}

fn write_all_raw<W: Write>(w: &mut W, buf: &[u8]) -> RpcResult<()> {
    w.write_all(buf).map_err(map_io)?;
    w.flush().map_err(map_io)?;
    Ok(())
}

/// Bridges a [`RpcTransport`](super::transport::RpcTransport) to
/// `std::io::{Read, Write}` so the AOSP-faithful framing + handshake
/// helpers above run over any transport with raw byte access
/// (currently `unix`). EOF (`recv_raw` ⇒ `Ok(0)`) is preserved as
/// `Read` returning `Ok(0)`, so `read_exact_raw` still yields the
/// correct `PeerClosed`/`Truncated`. This is the bridge the opt-in
/// android-13+ `RpcSession` profile uses; the R34 path never touches
/// it.
pub struct RawTransportIo<'a>(pub &'a dyn super::transport::RpcTransport);

impl Read for RawTransportIo<'_> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        self.0.recv_raw(buf).map_err(|e| match e {
            // Preserve the timeout kind across the `Read` boundary so
            // `read_exact_raw` can honor the `Timeout`/`Truncated`
            // contract; other errors keep their string form.
            RpcError::Timeout => std::io::Error::from(std::io::ErrorKind::TimedOut),
            other => std::io::Error::other(other.to_string()),
        })
    }
}

impl Write for RawTransportIo<'_> {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.write_all(buf)?;
        Ok(buf.len())
    }
    fn write_all(&mut self, buf: &[u8]) -> std::io::Result<()> {
        self.0
            .send_raw(buf)
            .map_err(|e| std::io::Error::other(e.to_string()))
    }
    fn flush(&mut self) -> std::io::Result<()> {
        Ok(()) // `send_raw` already flushes the underlying stream.
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::rpc::address::RPC_SESSION_ID_NEW;
    use crate::rpc::wire::{R34Codec, WIRE_TXN_FIXED_LEN};
    use crate::rpc::AddressSpace;

    fn txn(addr: RpcAddress, data: Vec<u8>) -> WireTransaction {
        WireTransaction {
            address: addr,
            code: 0xdead_beef,
            flags: 1,
            async_number: 0x0102_0304_0506_0708,
            data,
            ..Default::default()
        }
    }

    /// A `UnixStream::pair()` with read/write deadlines on both ends.
    /// The handshake/e2e exchanges below complete in microseconds, so
    /// the deadline never trips in normal operation; it bounds a
    /// macOS-specific EOF-wakeup race (see [`drain_leftover`]) and any
    /// stray stall so the (default, 5×-soak) CI job can never hang to
    /// its wall-clock timeout.
    fn timed_socketpair() -> (
        std::os::unix::net::UnixStream,
        std::os::unix::net::UnixStream,
    ) {
        let (c, s) = std::os::unix::net::UnixStream::pair().expect("socketpair");
        let timeout = std::time::Duration::from_secs(10);
        for sock in [&c, &s] {
            sock.set_read_timeout(Some(timeout))
                .expect("set read timeout");
            sock.set_write_timeout(Some(timeout))
                .expect("set write timeout");
        }
        (c, s)
    }

    /// Drain everything the peer sent after the handshake, returning all
    /// bytes read. Stops at a clean EOF (the peer's write-half shutdown —
    /// the Linux fast path) **or** at the [`timed_socketpair`] read
    /// deadline.
    ///
    /// The deadline arm is load-bearing on macOS: a peer's
    /// `shutdown(SHUT_WR)` does *not* reliably wake a `recv` already
    /// parked in the kernel on an `AF_UNIX` `socketpair` (reproduced
    /// ~1-in-50 full `rpc::` soak runs locally — the stuck thread sits
    /// forever in `read_to_end` → `recvfrom`), so a plain blocking
    /// `read_to_end` can wait for an EOF that never arrives. Treating
    /// the deadline as end-of-stream is correct for the leftover-byte
    /// assertions here: any *real* trailing byte was written by the peer
    /// during the handshake and is already in the socket buffer, so it
    /// comes back on the first `recv`; a timeout with nothing buffered
    /// genuinely means the peer sent nothing.
    fn drain_leftover(stream: &mut std::os::unix::net::UnixStream) -> Vec<u8> {
        use std::io::Read;
        let mut out = Vec::new();
        let mut chunk = [0u8; 64];
        loop {
            match stream.read(&mut chunk) {
                Ok(0) => break,
                Ok(n) => out.extend_from_slice(&chunk[..n]),
                Err(e)
                    if matches!(
                        e.kind(),
                        std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut
                    ) =>
                {
                    break
                }
                Err(e) => panic!("drain_leftover: unexpected error: {e}"),
            }
        }
        out
    }

    /// Spec-conformance golden — byte-exact against AOSP
    /// `RpcWireFormat.h` (v0 = android-13.0.0_r84, v1 =
    /// android-15.0.0_r36 == android-14.0.0_r75). Device-free.
    #[test]
    fn android13plus_spec_golden_vectors() {
        // ---- v0 (android-13) ----
        let c0 = Android13PlusCodec::android13();
        assert_eq!(c0.version(), 0);

        // TRANSACT(zero, GET_ROOT, no data): 16B header + 40B body, all
        // zero (v0: bytes 24..40 are reserved[4] = 0).
        let enc = c0
            .encode_transact(&WireTransaction {
                address: RpcAddress::zero(),
                code: 0,
                flags: 0,
                async_number: 0,
                data: vec![],
                ..Default::default()
            })
            .unwrap();
        let mut want = Vec::new();
        want.extend_from_slice(&0u32.to_le_bytes()); // TRANSACT
        want.extend_from_slice(&40u32.to_le_bytes()); // bodySize = 40
        want.extend_from_slice(&[0u8; 8]); // reserved[2]
        want.extend_from_slice(&[0u8; 40]); // addr8|code|flags|async|rsv16
        assert_eq!(enc, want, "v0 TRANSACT layout");
        assert_eq!(A13_TXN_FIXED_LEN, 40);

        // REPLY v0 = 4B fixed (i32 status) + data.
        let enc = c0
            .encode_reply(&WireReply {
                status: 0,
                data: vec![0xAA, 0xBB],
                ..Default::default()
            })
            .unwrap();
        let mut want = Vec::new();
        want.extend_from_slice(&1u32.to_le_bytes()); // REPLY
        want.extend_from_slice(&6u32.to_le_bytes()); // bodySize = 4 + 2
        want.extend_from_slice(&[0u8; 8]); // reserved[2]
        want.extend_from_slice(&0i32.to_le_bytes()); // status
        want.extend_from_slice(&[0xAA, 0xBB]);
        assert_eq!(enc, want, "v0 RpcWireReply = 4B fixed");

        // ---- v1 (android-14 / android-15) ----
        let c1 = Android13PlusCodec::android14_15();
        assert_eq!(c1.version(), 1);

        // TRANSACT v1: bytes 24..28 = parcelDataSize = data.len().
        let data = vec![1u8, 2, 3, 4, 5];
        let enc = c1
            .encode_transact(&txn(RpcAddress::zero(), data.clone()))
            .unwrap();
        // body[24..28] is parcelDataSize.
        let body = &enc[WIRE_HEADER_LEN..];
        assert_eq!(
            &body[24..28],
            &(data.len() as u32).to_le_bytes(),
            "v1 RpcWireTransaction.parcelDataSize"
        );
        assert_eq!(&body[28..40], &[0u8; 12]); // reserved[3] (12 B) zero
        assert_eq!(&body[40..], &data[..]);

        // REPLY v1 = 20B fixed (status|parcelDataSize|reserved[3]) + data.
        let enc = c1
            .encode_reply(&WireReply {
                status: 7,
                data: vec![0xCC, 0xDD, 0xEE],
                ..Default::default()
            })
            .unwrap();
        let mut want = Vec::new();
        want.extend_from_slice(&1u32.to_le_bytes()); // REPLY
        want.extend_from_slice(&23u32.to_le_bytes()); // bodySize = 20 + 3
        want.extend_from_slice(&[0u8; 8]); // reserved[2]
        want.extend_from_slice(&7i32.to_le_bytes()); // status
        want.extend_from_slice(&3u32.to_le_bytes()); // parcelDataSize
        want.extend_from_slice(&[0u8; 12]); // reserved[3]
        want.extend_from_slice(&[0xCC, 0xDD, 0xEE]);
        assert_eq!(enc, want, "v1 RpcWireReply = 20B fixed");

        // DEC_STRONG (unchanged v0↔v1): { addr8; amount=1; reserved }.
        for c in [c0, c1] {
            let mut ctr = 0u64;
            let a = RpcAddress::unique(&mut ctr, AddressSpace::Initiator);
            let enc = c.encode_dec_strong(&a);
            let mut want = Vec::new();
            want.extend_from_slice(&2u32.to_le_bytes()); // DEC_STRONG
            want.extend_from_slice(&16u32.to_le_bytes()); // bodySize
            want.extend_from_slice(&[0u8; 8]); // reserved[2]
            want.extend_from_slice(&ADDR_OPTION_CREATED.to_le_bytes()); // options
            want.extend_from_slice(&1u32.to_le_bytes()); // address = 1
            want.extend_from_slice(&1u32.to_le_bytes()); // amount = 1
            want.extend_from_slice(&0u32.to_le_bytes()); // reserved
            assert_eq!(enc, want, "DEC_STRONG layout (v{})", c.version());
        }

        // Connection header: v0 has reserved[9]; v1 has fdMode@5.
        let h0 = c0
            .encode_connection_header(false, FD_MODE_UNIX, &[])
            .unwrap();
        assert_eq!(h0.len(), A13_CONN_HEADER_LEN);
        assert_eq!(&h0[0..4], &0u32.to_le_bytes()); // version 0
        assert_eq!(h0[5], 0, "v0 byte 5 is reserved, not fdMode");
        let h1 = c1
            .encode_connection_header(false, FD_MODE_UNIX, &[])
            .unwrap();
        assert_eq!(&h1[0..4], &1u32.to_le_bytes()); // version 1
        assert_eq!(h1[5], FD_MODE_UNIX, "v1 fileDescriptorTransportMode");
        assert_eq!(&h1[14..16], &0u16.to_le_bytes()); // sessionIdSize = 0
        let h1i = c1
            .encode_connection_header(true, FD_MODE_NONE, &[])
            .unwrap();
        assert_eq!(h1i[4], CONN_OPTION_INCOMING);
        assert_eq!(
            c1.decode_connection_header(&h1).unwrap(),
            (1, 0, FD_MODE_UNIX, Vec::new())
        );
        assert_eq!(
            c0.decode_connection_header(&h0).unwrap(),
            (0, 0, 0, Vec::new())
        );

        // New-session response + "cci" (unchanged v0↔v1).
        let r = c1.encode_new_session_response(PROTOCOL_V1);
        assert_eq!(c1.decode_new_session_response(&r).unwrap(), 1);
        let init = c1.encode_connection_init();
        assert_eq!(&init[0..4], b"cci\0");
        c1.decode_connection_init(&init).expect("\"cci\"");

        // Version acceptance (AOSP rule @ android-16.0.0_r4, _NEXT = 3):
        // accept 0,1,2,EXPERIMENTAL; reject 3 and above.
        assert!(is_supported_protocol_version(0));
        assert!(is_supported_protocol_version(1));
        assert!(is_supported_protocol_version(2));
        assert!(is_supported_protocol_version(
            RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL
        ));
        assert!(!is_supported_protocol_version(3));
        assert!(!is_supported_protocol_version(
            RPC_WIRE_PROTOCOL_VERSION_NEXT
        ));
        assert!(Android13PlusCodec::with_version(3).is_err());
        assert_eq!(Android13PlusCodec::with_version(2).unwrap().version(), 2);
        assert_eq!(SUPPORTED_MAX_VERSION, 2);
        assert_eq!(RPC_WIRE_PROTOCOL_VERSION_NEXT, 3);
    }

    /// encode∘decode == identity, all versions (incl. android-16 v2),
    /// both address spaces. v2 also round-trips a non-empty object
    /// table (synthetic positions).
    #[test]
    fn android13plus_roundtrip_all_commands() {
        for c in [
            Android13PlusCodec::android13(),
            Android13PlusCodec::android14_15(),
            Android13PlusCodec::android16(),
        ] {
            for size in [0usize, 1, 17, 4096, 1 << 20] {
                let data: Vec<u8> = (0..size).map(|i| (i % 251) as u8).collect();
                // v1+ may carry an object table; synthesize sorted
                // positions within the parcel data (v0 must stay empty
                // — exercised separately by the negative test).
                let positions: Vec<u32> = if c.version() >= PROTOCOL_V1 && size >= 8 {
                    vec![0, (size / 2) as u32, (size - 4) as u32]
                } else {
                    Vec::new()
                };
                for space in [AddressSpace::Initiator, AddressSpace::Acceptor] {
                    let mut ctr = 0x4142u64;
                    let addr = RpcAddress::unique(&mut ctr, space);
                    let mut t = txn(addr, data.clone());
                    t.object_positions = positions.clone();
                    match c.decode_message(&c.encode_transact(&t).unwrap()).unwrap() {
                        WireMessage::Transact(d) => {
                            assert_eq!(d.address, addr, "addr rt (v{}, {space:?})", c.version());
                            assert_eq!(d.code, 0xdead_beef);
                            assert_eq!(d.flags, 1);
                            assert_eq!(d.async_number, 0x0102_0304_0506_0708);
                            assert_eq!(d.data, data);
                            assert_eq!(
                                d.object_positions,
                                positions,
                                "object table rt (v{})",
                                c.version()
                            );
                        }
                        other => panic!("expected Transact, got {other:?}"),
                    }
                }
                match c
                    .decode_message(
                        &c.encode_reply(&WireReply {
                            status: -42,
                            data: data.clone(),
                            object_positions: positions.clone(),
                        })
                        .unwrap(),
                    )
                    .unwrap()
                {
                    WireMessage::Reply(r) => {
                        assert_eq!(r.status, -42, "v{}", c.version());
                        assert_eq!(r.data, data);
                        assert_eq!(r.object_positions, positions, "reply table rt");
                    }
                    other => panic!("expected Reply, got {other:?}"),
                }
            }
            let mut ctr = 9u64;
            let addr = RpcAddress::unique(&mut ctr, AddressSpace::Initiator);
            match c.decode_message(&c.encode_dec_strong(&addr)).unwrap() {
                WireMessage::DecStrong(a) => assert_eq!(a, addr),
                other => panic!("expected DecStrong, got {other:?}"),
            }
        }
    }

    /// Additive invariant: distinct from `R34Codec`; r34 byte-unchanged
    /// (32B addr / 64B txn). v0 and v1 differ exactly in the reply
    /// fixed size (4 vs 20).
    #[test]
    fn android13plus_distinct_and_r34_unchanged() {
        let t = WireTransaction {
            address: RpcAddress::zero(),
            code: 0,
            flags: 0,
            async_number: 0,
            data: vec![],
            ..Default::default()
        };
        let r34 = R34Codec.encode_transact(&t).unwrap();
        assert_eq!(r34.len(), WIRE_HEADER_LEN + WIRE_TXN_FIXED_LEN); // 16 + 64
        assert_eq!(&r34[4..8], &64u32.to_le_bytes());

        let v0 = Android13PlusCodec::android13().encode_transact(&t).unwrap();
        let v1 = Android13PlusCodec::android14_15()
            .encode_transact(&t)
            .unwrap();
        let v2 = Android13PlusCodec::android16().encode_transact(&t).unwrap();
        assert_eq!(v0.len(), WIRE_HEADER_LEN + 40);
        assert_eq!(v1.len(), WIRE_HEADER_LEN + 40);
        assert_ne!(v0, r34, "android-13+ must differ from r34");
        // No-object parcel ⇒ v1 and v2 wire are byte-identical
        // (the structural v1 no-regression invariant).
        assert_eq!(v1, v2, "no-object v1 ≡ v2 (AC-8.2)");

        // Reply: the load-bearing v0/v1 divergence (4B vs 20B fixed);
        // v1 ≡ v2.
        let rep = WireReply {
            status: 0,
            data: vec![],
            ..Default::default()
        };
        assert_eq!(
            Android13PlusCodec::android13()
                .encode_reply(&rep)
                .unwrap()
                .len(),
            WIRE_HEADER_LEN + 4
        );
        assert_eq!(
            Android13PlusCodec::android14_15()
                .encode_reply(&rep)
                .unwrap()
                .len(),
            WIRE_HEADER_LEN + 20
        );
        assert_eq!(
            Android13PlusCodec::android14_15()
                .encode_reply(&rep)
                .unwrap(),
            Android13PlusCodec::android16().encode_reply(&rep).unwrap(),
            "no-object reply v1 ≡ v2 (AC-8.2)"
        );
    }

    /// Malformed input never panics/OOMs (both versions).
    #[test]
    fn android13plus_decoder_rejects_hostile_input_safely() {
        for c in [
            Android13PlusCodec::android13(),
            Android13PlusCodec::android14_15(),
            Android13PlusCodec::android16(),
        ] {
            assert!(c.decode_message(&[]).is_err());
            assert!(c.decode_message(&[0u8; 15]).is_err());

            let mut f = Vec::new();
            f.extend_from_slice(&0u32.to_le_bytes());
            f.extend_from_slice(&u32::MAX.to_le_bytes());
            f.extend_from_slice(&[0u8; 8]);
            assert!(matches!(
                c.decode_message(&f),
                Err(RpcError::FrameTooLarge { .. })
            ));

            // TRANSACT bodySize < 40 fixed prefix.
            let mut f = Vec::new();
            f.extend_from_slice(&0u32.to_le_bytes());
            f.extend_from_slice(&20u32.to_le_bytes());
            f.extend_from_slice(&[0u8; 8]);
            f.extend_from_slice(&[0u8; 20]);
            assert!(matches!(c.decode_message(&f), Err(RpcError::Protocol(_))));

            // DEC_STRONG wrong body length.
            let mut f = Vec::new();
            f.extend_from_slice(&2u32.to_le_bytes());
            f.extend_from_slice(&8u32.to_le_bytes());
            f.extend_from_slice(&[0u8; 8]);
            f.extend_from_slice(&[0u8; 8]);
            assert!(matches!(c.decode_message(&f), Err(RpcError::Protocol(_))));

            // Unknown command.
            let mut f = Vec::new();
            f.extend_from_slice(&999u32.to_le_bytes());
            f.extend_from_slice(&0u32.to_le_bytes());
            f.extend_from_slice(&[0u8; 8]);
            assert!(matches!(c.decode_message(&f), Err(RpcError::Protocol(_))));

            assert!(c.decode_connection_header(&[0u8; 8]).is_err());
            assert!(c.decode_new_session_response(&[0u8; 4]).is_err());
            assert!(c.decode_connection_init(&[0u8; 4]).is_err());

            let pre = c.encode_session_preamble(RPC_SESSION_ID_NEW);
            assert_eq!(pre.len(), A13_CONN_HEADER_LEN);
            assert_eq!(
                c.decode_session_preamble(&c.encode_new_session_response(0))
                    .unwrap(),
                0
            );
        }
        // v1 REPLY shorter than its 20B fixed prefix is rejected.
        let c1 = Android13PlusCodec::android14_15();
        let mut f = Vec::new();
        f.extend_from_slice(&1u32.to_le_bytes()); // REPLY
        f.extend_from_slice(&4u32.to_le_bytes()); // bodySize = 4 (< 20)
        f.extend_from_slice(&[0u8; 8]);
        f.extend_from_slice(&[0u8; 4]);
        assert!(matches!(c1.decode_message(&f), Err(RpcError::Protocol(_))));
    }

    /// The full android-13+ RPC protocol (versioned connection
    /// **handshake** + **AOSP-faithful framing** + `Android13PlusCodec`)
    /// driven end-to-end over a **raw `UnixStream`** (no rsbinder
    /// `RpcTransport` u32 prefix — wire-identical to a genuine
    /// android-13/14/15 RPC peer). Proves all three protocol layers
    /// interoperate, hermetically, over both v0 and v1 and across
    /// version negotiation.
    #[test]
    fn android13plus_live_protocol_e2e_over_raw_socket() {
        use std::thread;

        // (client_max, server_max, expected_negotiated) — incl.
        // android-16 v2 and v2↔v1 / v2↔v0 downgrade negotiation.
        for (cmax, smax, expect) in [
            (0u32, 0u32, 0u32),
            (1, 1, 1),
            (1, 0, 0),
            (0, 1, 0),
            (2, 2, 2),
            (2, 1, 1),
            (1, 2, 1),
            (2, 0, 0),
        ] {
            let (mut c, mut s) = timed_socketpair();

            let srv = thread::spawn(move || -> u32 {
                let (codec, fd_mode, sid, incoming) =
                    server_accept(&mut s, smax).expect("server_accept");
                assert_eq!(fd_mode, FD_MODE_NONE);
                assert!(sid.is_empty(), "new-session ⇒ empty session id");
                assert!(!incoming, "new-session is always outgoing");

                // Read the client's GET_ROOT TRANSACT (AOSP framing).
                let raw = read_aosp_message(&mut s).expect("read transact");
                match codec.decode_message(&raw).expect("decode transact") {
                    WireMessage::Transact(t) => {
                        assert!(t.address.is_zero());
                        assert_eq!(t.code, 0); // GET_ROOT
                        assert!(t.data.is_empty());
                    }
                    other => panic!("expected Transact, got {other:?}"),
                }
                // Reply.
                write_aosp_message(
                    &mut s,
                    &codec
                        .encode_reply(&WireReply {
                            status: 0,
                            data: b"root!".to_vec(),
                            ..Default::default()
                        })
                        .unwrap(),
                )
                .expect("write reply");

                // Read a DEC_STRONG.
                let raw = read_aosp_message(&mut s).expect("read dec_strong");
                match codec.decode_message(&raw).expect("decode dec_strong") {
                    WireMessage::DecStrong(_) => {}
                    other => panic!("expected DecStrong, got {other:?}"),
                }
                codec.version()
            });

            let codec = client_connect(&mut c, cmax, false, FD_MODE_NONE).expect("client_connect");
            assert_eq!(codec.version(), expect, "negotiated min({cmax},{smax})");

            // GET_ROOT TRANSACT. Capture the exact wire bytes to prove
            // AOSP-faithful framing (no u32 length prefix).
            let txn = WireTransaction {
                address: RpcAddress::zero(),
                code: 0,
                flags: 0,
                async_number: 0,
                data: vec![],
                ..Default::default()
            };
            let encoded = codec.encode_transact(&txn).unwrap();
            // First 4 wire bytes are the RpcWireHeader.command
            // (TRANSACT=0), NOT an rsbinder u32 frame length.
            assert_eq!(&encoded[0..4], &0u32.to_le_bytes());
            assert_eq!(
                encoded.len(),
                WIRE_HEADER_LEN + A13_TXN_FIXED_LEN,
                "v{} GET_ROOT = 16 + 40, no length prefix",
                codec.version()
            );
            write_aosp_message(&mut c, &encoded).expect("write transact");

            let raw = read_aosp_message(&mut c).expect("read reply");
            match codec.decode_message(&raw).expect("decode reply") {
                WireMessage::Reply(r) => {
                    assert_eq!(r.status, 0);
                    assert_eq!(r.data, b"root!");
                }
                other => panic!("expected Reply, got {other:?}"),
            }

            let mut ctr = 7u64;
            let addr = RpcAddress::unique(&mut ctr, AddressSpace::Initiator);
            write_aosp_message(&mut c, &codec.encode_dec_strong(&addr)).expect("write dec_strong");

            assert_eq!(srv.join().expect("server thread"), expect);
        }
    }

    /// The same full android-13+ protocol, but driven over a real
    /// [`UnixTransport`](crate::rpc::transport::UnixTransport)
    /// through the [`RawTransportIo`] bridge (not a bare `UnixStream`)
    /// — i.e. over the actual `RpcTransport` abstraction the opt-in
    /// `RpcSession` profile will use. v0 and v1.
    #[test]
    fn android13plus_e2e_over_unix_transport_bridge() {
        use crate::rpc::transport::UnixTransport;
        use std::thread;

        for (cmax, expect) in [(0u32, 0u32), (1, 1), (2, 2)] {
            let (a, b) = UnixTransport::pair().expect("unix pair");

            let srv = thread::spawn(move || -> u32 {
                let mut io = RawTransportIo(&b);
                let (codec, fd, sid, incoming) = server_accept(&mut io, 2).expect("server_accept");
                assert_eq!(fd, FD_MODE_NONE);
                assert!(sid.is_empty());
                assert!(!incoming);
                let raw = read_aosp_message(&mut io).expect("read transact");
                match codec.decode_message(&raw).expect("decode") {
                    WireMessage::Transact(t) => assert_eq!(t.code, 0),
                    o => panic!("expected Transact, got {o:?}"),
                }
                write_aosp_message(
                    &mut io,
                    &codec
                        .encode_reply(&WireReply {
                            status: 0,
                            data: b"via-transport".to_vec(),
                            ..Default::default()
                        })
                        .unwrap(),
                )
                .expect("write reply");
                codec.version()
            });

            let mut io = RawTransportIo(&a);
            let codec = client_connect(&mut io, cmax, false, FD_MODE_NONE).expect("client_connect");
            assert_eq!(codec.version(), expect);
            write_aosp_message(
                &mut io,
                &codec
                    .encode_transact(&WireTransaction {
                        address: RpcAddress::zero(),
                        code: 0,
                        flags: 0,
                        async_number: 0,
                        data: vec![],
                        ..Default::default()
                    })
                    .unwrap(),
            )
            .expect("write transact");
            let raw = read_aosp_message(&mut io).expect("read reply");
            match codec.decode_message(&raw).expect("decode reply") {
                WireMessage::Reply(r) => {
                    assert_eq!(r.status, 0);
                    assert_eq!(r.data, b"via-transport");
                }
                o => panic!("expected Reply, got {o:?}"),
            }
            assert_eq!(srv.join().expect("server thread"), expect);
        }

        // Default transports have no raw access (additive, by type).
        use crate::rpc::transport::RpcTransport;
        let (m, _m2) = crate::rpc::transport::MemTransport::pair();
        assert!(m.send_raw(b"x").is_err(), "mem has no raw byte access");
        assert!(m.recv_raw(&mut [0u8; 4]).is_err());
    }

    /// Attach handshake (non-empty session id) is byte-exact to AOSP
    /// `RpcSession::initAndAddConnection` + `RpcServer::establish
    /// Connection`: no `RpcNewSessionResponse` on the wire, "cci"
    /// direction follows the `RPC_CONNECTION_OPTION_INCOMING` bit.
    /// The mutant catch — server spuriously writing `RpcNewSession
    /// Response` on attach + client spuriously reading it — would be
    /// invisible to a success-only assertion (both sides write+read
    /// 8 stale bytes in lockstep) but surfaces here as leftover bytes
    /// after both shutdown their write halves.
    #[test]
    fn android13plus_attach_handshake_wire_byte_exact() {
        use std::net::Shutdown;
        use std::thread;

        let session_id = [0x42u8; 32];

        for (incoming, version) in [
            (false, PROTOCOL_V0),
            (false, PROTOCOL_V1),
            (false, PROTOCOL_V2),
            (true, PROTOCOL_V0),
            (true, PROTOCOL_V1),
            (true, PROTOCOL_V2),
        ] {
            let (mut c, mut s) = timed_socketpair();
            let sid = session_id;
            let srv = thread::spawn(move || -> (u32, bool, Vec<u8>, Vec<u8>) {
                let (codec, _fd, got_sid, srv_incoming) =
                    server_accept(&mut s, version).expect("server_accept");
                s.shutdown(Shutdown::Write).expect("srv shutdown w");
                let leftover = drain_leftover(&mut s);
                (codec.version(), srv_incoming, got_sid, leftover)
            });

            let codec = client_connect_with_id(&mut c, version, incoming, FD_MODE_NONE, &sid)
                .expect("client_connect_with_id");
            c.shutdown(Shutdown::Write).expect("cli shutdown w");
            let client_leftover = drain_leftover(&mut c);
            assert!(
                client_leftover.is_empty(),
                "(attach, incoming={incoming}, v={version}): server wrote {} unexpected \
                 byte(s) after handshake — regression of NewSessionResponse-on-attach",
                client_leftover.len()
            );

            let (sv, sincoming, ssid, srv_leftover) = srv.join().expect("srv");
            assert_eq!(sv, version);
            assert_eq!(sincoming, incoming);
            assert_eq!(ssid, sid.to_vec());
            assert_eq!(codec.version(), version);
            assert!(
                srv_leftover.is_empty(),
                "(attach, incoming={incoming}, v={version}): client wrote {} unexpected \
                 byte(s) after handshake",
                srv_leftover.len()
            );
        }
    }

    /// AOSP-faithful: `RpcServer.cpp` writes `RpcNewSessionResponse`
    /// for *any* `requestingNewSession` header (line 494-506), *then*
    /// at line 530-534 rejects the incoming-direction request because
    /// "Cannot create a new session with an incoming connection,
    /// would leak". The client therefore sees the response on the
    /// wire before EOF — this test pins both the rejection and the
    /// wire order.
    #[test]
    fn android13plus_new_session_incoming_rejected() {
        use std::thread;

        let (mut c, mut s) = timed_socketpair();
        let srv = thread::spawn(move || -> RpcResult<_> {
            let result = server_accept(&mut s, PROTOCOL_V1);
            drop(s);
            result
        });

        let codec = Android13PlusCodec::with_version(PROTOCOL_V1).unwrap();
        let header = codec
            .encode_connection_header(true, FD_MODE_NONE, &[])
            .unwrap();
        write_all_raw(&mut c, &header).expect("write header");

        let r = srv.join().expect("srv");
        assert!(
            matches!(r, Err(RpcError::Protocol(_))),
            "server must reject new-session + incoming, got {r:?}"
        );

        let buf = drain_leftover(&mut c);
        assert_eq!(
            buf.len(),
            A13_NEW_SESSION_RESP_LEN,
            "AOSP order: write RpcNewSessionResponse, then close on \
             incoming. Got {} bytes: {buf:?}",
            buf.len()
        );
    }

    // ===== android-16 RPC wire v2 ==================================

    /// android-16.0.0_r4 version constants golden.
    /// `RpcSession.h`: `RPC_WIRE_PROTOCOL_VERSION = 2`, `_NEXT = 3`,
    /// `_EXPLICIT_PARCEL_SIZE = 1`, `_INCLUDES_BINDER_POSITIONS = 2`.
    /// `setProtocolVersion` accepts {0,1,2,EXPERIMENTAL}, rejects ≥3.
    #[test]
    fn android16_v2_version_constants_golden() {
        assert_eq!(PROTOCOL_V0, 0);
        assert_eq!(PROTOCOL_V1, 1);
        assert_eq!(PROTOCOL_V2, 2);
        assert_eq!(
            SUPPORTED_MAX_VERSION, 2,
            "android-16 RPC_WIRE_PROTOCOL_VERSION"
        );
        assert_eq!(RPC_WIRE_PROTOCOL_VERSION_NEXT, 3);
        assert_eq!(RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL, 0xF000_0000);
        for v in [0, 1, 2] {
            assert!(is_supported_protocol_version(v));
            assert_eq!(Android13PlusCodec::with_version(v).unwrap().version(), v);
        }
        assert!(is_supported_protocol_version(
            RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL
        ));
        for v in [3u32, 4, 100, RPC_WIRE_PROTOCOL_VERSION_NEXT] {
            assert!(!is_supported_protocol_version(v), "reject v{v}");
            assert!(Android13PlusCodec::with_version(v).is_err());
        }
        assert_eq!(Android13PlusCodec::android16().version(), 2);
        // `RpcWireReply::wireSize` is byte-identical v1≡v2 (4 vs 20 is
        // strictly the v0-vs-v1+ split — no v2 framing change).
        assert_eq!(reply_fixed_len(0), 4);
        assert_eq!(reply_fixed_len(1), 20);
        assert_eq!(reply_fixed_len(2), 20);
        assert!(!has_object_table(0));
        assert!(has_object_table(1));
        assert!(has_object_table(2));
    }

    /// A no-object parcel encodes **byte-identically** at v1 and v2
    /// (TRANSACT *and* REPLY, several payload sizes). This is the
    /// structural v1-no-regression guarantee: an empty object table is
    /// 0 wire bytes ⇒ `bodySize` unchanged ⇒ a v2-capable rsbinder's
    /// no-object traffic is wire-identical to its v1 traffic.
    #[test]
    fn android16_no_object_v1_eq_v2_byte_identical() {
        let v1 = Android13PlusCodec::android14_15();
        let v2 = Android13PlusCodec::android16();
        for size in [0usize, 1, 4, 5, 64, 4096] {
            let data: Vec<u8> = (0..size).map(|i| (i % 251) as u8).collect();
            let mut ctr = 0xABCDu64;
            let addr = RpcAddress::unique(&mut ctr, AddressSpace::Initiator);
            let t = txn(addr, data.clone()); // object_positions empty
            assert_eq!(
                v1.encode_transact(&t).unwrap(),
                v2.encode_transact(&t).unwrap(),
                "no-object TRANSACT v1≡v2 (size {size})"
            );
            let r = WireReply {
                status: -7,
                data: data.clone(),
                object_positions: Vec::new(),
            };
            assert_eq!(
                v1.encode_reply(&r).unwrap(),
                v2.encode_reply(&r).unwrap(),
                "no-object REPLY v1≡v2 (size {size})"
            );
        }
    }

    /// Object-table framing golden vs AOSP
    /// `RpcState.cpp`: `bodySize = fixed + parcelDataSize + 4·N`; the
    /// table is the trailing LE `u32[]` after the parcel data;
    /// `parcelDataSize` is the data length (unchanged); encode→decode
    /// round-trips the positions; and the android-16 `splitOff` /
    /// `reinterpret<uint32_t>` receive rules (parcelDataSize past the
    /// body, or a table byte-size not a multiple of 4) are rejected.
    #[test]
    fn android16_v2_object_table_framing_golden() {
        let c = Android13PlusCodec::android16();
        let data = vec![0x11u8, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88];
        let positions = vec![0u32, 4];

        // ---- TRANSACT ----
        let t = WireTransaction {
            address: RpcAddress::zero(),
            code: 0xCAFE,
            flags: 0,
            async_number: 0,
            data: data.clone(),
            object_positions: positions.clone(),
        };
        let enc = c.encode_transact(&t).unwrap();
        // bodySize = 40 + 8 (data) + 4*2 (table) = 56.
        assert_eq!(&enc[4..8], &56u32.to_le_bytes(), "bodySize = 40+pds+4N");
        let body = &enc[WIRE_HEADER_LEN..];
        // parcelDataSize @24 = data.len() (UNCHANGED by the table).
        assert_eq!(&body[24..28], &(data.len() as u32).to_le_bytes());
        // [40 .. 40+8] = parcel data, then the LE u32[] table.
        assert_eq!(&body[40..48], &data[..]);
        assert_eq!(&body[48..52], &0u32.to_le_bytes());
        assert_eq!(&body[52..56], &4u32.to_le_bytes());
        assert_eq!(enc.len(), WIRE_HEADER_LEN + 56);
        match c.decode_message(&enc).unwrap() {
            WireMessage::Transact(d) => {
                assert_eq!(d.data, data);
                assert_eq!(d.object_positions, positions);
            }
            o => panic!("expected Transact, got {o:?}"),
        }

        // ---- REPLY ----
        let r = WireReply {
            status: 0,
            data: data.clone(),
            object_positions: positions.clone(),
        };
        let enc = c.encode_reply(&r).unwrap();
        // bodySize = 20 + 8 + 8 = 36.
        assert_eq!(&enc[4..8], &36u32.to_le_bytes());
        let body = &enc[WIRE_HEADER_LEN..];
        assert_eq!(&body[4..8], &(data.len() as u32).to_le_bytes()); // parcelDataSize
        assert_eq!(&body[20..28], &data[..]);
        assert_eq!(&body[28..32], &0u32.to_le_bytes());
        assert_eq!(&body[32..36], &4u32.to_le_bytes());
        match c.decode_message(&enc).unwrap() {
            WireMessage::Reply(d) => {
                assert_eq!(d.data, data);
                assert_eq!(d.object_positions, positions);
            }
            o => panic!("expected Reply, got {o:?}"),
        }

        // android-16 `parcelSpan.splitOff(parcelDataSize)` ⇒ nullopt
        // ⇒ BAD_VALUE: forge a TRANSACT whose parcelDataSize exceeds
        // the available body tail.
        let mut bad = c
            .encode_transact(&WireTransaction {
                address: RpcAddress::zero(),
                code: 1,
                flags: 0,
                async_number: 0,
                data: vec![1, 2, 3, 4],
                object_positions: vec![0],
            })
            .unwrap();
        // body[24..28] = parcelDataSize; bump it past the tail.
        let pds_off = WIRE_HEADER_LEN + 24;
        bad[pds_off..pds_off + 4].copy_from_slice(&0xFFFF_FFFFu32.to_le_bytes());
        assert!(matches!(c.decode_message(&bad), Err(RpcError::Protocol(_))));

        // `objectTableBytes->reinterpret<uint32_t>()` ⇒ nullopt if the
        // table byte-size isn't a multiple of 4: parcelDataSize that
        // leaves a 2-byte tail.
        let mut bad = c
            .encode_transact(&WireTransaction {
                address: RpcAddress::zero(),
                code: 1,
                flags: 0,
                async_number: 0,
                data: vec![0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x01],
                object_positions: Vec::new(),
            })
            .unwrap();
        // data.len()=6, no table ⇒ tail after pds is 0 (ok). Now claim
        // parcelDataSize=4 ⇒ 2-byte trailing "table" ⇒ %4 != 0.
        bad[pds_off..pds_off + 4].copy_from_slice(&4u32.to_le_bytes());
        assert!(matches!(c.decode_message(&bad), Err(RpcError::Protocol(_))));
    }

    /// `validateParcel` analogue: a v0 (android-13) or r34 wire can
    /// carry **no** object table; a non-empty `object_positions`
    /// on those is a protocol error, not a silently-dropped table
    /// (AOSP `RpcState::validateParcel` ⇒ `BAD_VALUE`).
    #[test]
    fn android16_v0_and_r34_reject_object_positions() {
        let t = WireTransaction {
            address: RpcAddress::zero(),
            code: 1,
            flags: 0,
            async_number: 0,
            data: vec![1, 2, 3, 4],
            object_positions: vec![0],
        };
        let r = WireReply {
            status: 0,
            data: vec![1, 2, 3, 4],
            object_positions: vec![0],
        };
        // v0 (android-13) — no parcelDataSize, no table.
        let v0 = Android13PlusCodec::android13();
        assert!(matches!(v0.encode_transact(&t), Err(RpcError::Protocol(_))));
        assert!(matches!(v0.encode_reply(&r), Err(RpcError::Protocol(_))));
        // r34 (android-12, pre-versioning) — no object table concept.
        assert!(matches!(
            R34Codec.encode_transact(&t),
            Err(RpcError::Protocol(_))
        ));
        assert!(matches!(
            R34Codec.encode_reply(&r),
            Err(RpcError::Protocol(_))
        ));
        // …and empty positions on v0/r34 still encode fine (unchanged).
        let t0 = txn(RpcAddress::zero(), vec![9, 9, 9, 9]);
        assert!(v0.encode_transact(&t0).is_ok());
        assert!(R34Codec.encode_transact(&t0).is_ok());
    }
}