Skip to content

Commit dac3b90

Browse files
committed
Return for failing range asserts to enforce the assumptions.
1 parent 9a0a160 commit dac3b90

File tree

2 files changed

+113
-74
lines changed

2 files changed

+113
-74
lines changed

arch/Xtensa/XtensaDisassembler.c

Lines changed: 81 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -528,15 +528,17 @@ static bool tryAddingSymbolicOperand(int64_t Value, bool isBranch,
528528
static DecodeStatus decodeCallOperand(MCInst *Inst, uint64_t Imm,
529529
int64_t Address, const void *Decoder)
530530
{
531-
CS_ASSERT(isUIntN(18, Imm) && "Invalid immediate");
531+
CS_ASSERT_RET_VAL(isUIntN(18, Imm) && "Invalid immediate",
532+
MCDisassembler_Fail);
532533
MCOperand_CreateImm0(Inst, (SignExtend64((Imm << 2), 20)));
533534
return MCDisassembler_Success;
534535
}
535536

536537
static DecodeStatus decodeJumpOperand(MCInst *Inst, uint64_t Imm,
537538
int64_t Address, const void *Decoder)
538539
{
539-
CS_ASSERT(isUIntN(18, Imm) && "Invalid immediate");
540+
CS_ASSERT_RET_VAL(isUIntN(18, Imm) && "Invalid immediate",
541+
MCDisassembler_Fail);
540542
MCOperand_CreateImm0(Inst, (SignExtend64((Imm), 18)));
541543
return MCDisassembler_Success;
542544
}
@@ -549,14 +551,16 @@ static DecodeStatus decodeBranchOperand(MCInst *Inst, uint64_t Imm,
549551
case Xtensa_BGEZ:
550552
case Xtensa_BLTZ:
551553
case Xtensa_BNEZ:
552-
CS_ASSERT(isUIntN(12, Imm) && "Invalid immediate");
554+
CS_ASSERT_RET_VAL(isUIntN(12, Imm) && "Invalid immediate",
555+
MCDisassembler_Fail);
553556
if (!tryAddingSymbolicOperand(
554557
SignExtend64((Imm), 12) + 4 + Address, true,
555558
Address, 0, 3, Inst, Decoder))
556559
MCOperand_CreateImm0(Inst, (SignExtend64((Imm), 12)));
557560
break;
558561
default:
559-
CS_ASSERT(isUIntN(8, Imm) && "Invalid immediate");
562+
CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
563+
MCDisassembler_Fail);
560564
if (!tryAddingSymbolicOperand(
561565
SignExtend64((Imm), 8) + 4 + Address, true, Address,
562566
0, 3, Inst, Decoder))
@@ -568,7 +572,8 @@ static DecodeStatus decodeBranchOperand(MCInst *Inst, uint64_t Imm,
568572
static DecodeStatus decodeLoopOperand(MCInst *Inst, uint64_t Imm,
569573
int64_t Address, const void *Decoder)
570574
{
571-
CS_ASSERT(isUIntN(8, Imm) && "Invalid immediate");
575+
CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
576+
MCDisassembler_Fail);
572577
if (!tryAddingSymbolicOperand(Imm + 4 + Address, true, Address, 0, 3,
573578
Inst, Decoder))
574579
MCOperand_CreateImm0(Inst, (Imm));
@@ -578,64 +583,72 @@ static DecodeStatus decodeLoopOperand(MCInst *Inst, uint64_t Imm,
578583
static DecodeStatus decodeL32ROperand(MCInst *Inst, uint64_t Imm,
579584
int64_t Address, const void *Decoder)
580585
{
581-
CS_ASSERT(isUIntN(16, Imm) && "Invalid immediate");
586+
CS_ASSERT_RET_VAL(isUIntN(16, Imm) && "Invalid immediate",
587+
MCDisassembler_Fail);
582588
MCOperand_CreateImm0(Inst, OneExtend64(Imm << 2, 18));
583589
return MCDisassembler_Success;
584590
}
585591

586592
static DecodeStatus decodeImm8Operand(MCInst *Inst, uint64_t Imm,
587593
int64_t Address, const void *Decoder)
588594
{
589-
CS_ASSERT(isUIntN(8, Imm) && "Invalid immediate");
595+
CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
596+
MCDisassembler_Fail);
590597
MCOperand_CreateImm0(Inst, (SignExtend64((Imm), 8)));
591598
return MCDisassembler_Success;
592599
}
593600

594601
static DecodeStatus decodeImm8_sh8Operand(MCInst *Inst, uint64_t Imm,
595602
int64_t Address, const void *Decoder)
596603
{
597-
CS_ASSERT(isUIntN(16, Imm) && ((Imm & 0xff) == 0) &&
598-
"Invalid immediate");
604+
CS_ASSERT_RET_VAL(isUIntN(16, Imm) && ((Imm & 0xff) == 0) &&
605+
"Invalid immediate",
606+
MCDisassembler_Fail);
599607
MCOperand_CreateImm0(Inst, (SignExtend64((Imm), 16)));
600608
return MCDisassembler_Success;
601609
}
602610

603611
static DecodeStatus decodeImm12Operand(MCInst *Inst, uint64_t Imm,
604612
int64_t Address, const void *Decoder)
605613
{
606-
CS_ASSERT(isUIntN(12, Imm) && "Invalid immediate");
614+
CS_ASSERT_RET_VAL(isUIntN(12, Imm) && "Invalid immediate",
615+
MCDisassembler_Fail);
607616
MCOperand_CreateImm0(Inst, (SignExtend64((Imm), 12)));
608617
return MCDisassembler_Success;
609618
}
610619

611620
static DecodeStatus decodeUimm4Operand(MCInst *Inst, uint64_t Imm,
612621
int64_t Address, const void *Decoder)
613622
{
614-
CS_ASSERT(isUIntN(4, Imm) && "Invalid immediate");
623+
CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
624+
MCDisassembler_Fail);
615625
MCOperand_CreateImm0(Inst, (Imm));
616626
return MCDisassembler_Success;
617627
}
618628

619629
static DecodeStatus decodeUimm5Operand(MCInst *Inst, uint64_t Imm,
620630
int64_t Address, const void *Decoder)
621631
{
622-
CS_ASSERT(isUIntN(5, Imm) && "Invalid immediate");
632+
CS_ASSERT_RET_VAL(isUIntN(5, Imm) && "Invalid immediate",
633+
MCDisassembler_Fail);
623634
MCOperand_CreateImm0(Inst, (Imm));
624635
return MCDisassembler_Success;
625636
}
626637

627638
static DecodeStatus decodeImm1_16Operand(MCInst *Inst, uint64_t Imm,
628639
int64_t Address, const void *Decoder)
629640
{
630-
CS_ASSERT(isUIntN(4, Imm) && "Invalid immediate");
641+
CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
642+
MCDisassembler_Fail);
631643
MCOperand_CreateImm0(Inst, (Imm + 1));
632644
return MCDisassembler_Success;
633645
}
634646

635647
static DecodeStatus decodeImm1n_15Operand(MCInst *Inst, uint64_t Imm,
636648
int64_t Address, const void *Decoder)
637649
{
638-
CS_ASSERT(isUIntN(4, Imm) && "Invalid immediate");
650+
CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
651+
MCDisassembler_Fail);
639652
if (!Imm)
640653
MCOperand_CreateImm0(Inst, (-1));
641654
else
@@ -646,7 +659,8 @@ static DecodeStatus decodeImm1n_15Operand(MCInst *Inst, uint64_t Imm,
646659
static DecodeStatus decodeImm32n_95Operand(MCInst *Inst, uint64_t Imm,
647660
int64_t Address, const void *Decoder)
648661
{
649-
CS_ASSERT(isUIntN(7, Imm) && "Invalid immediate");
662+
CS_ASSERT_RET_VAL(isUIntN(7, Imm) && "Invalid immediate",
663+
MCDisassembler_Fail);
650664
if ((Imm & 0x60) == 0x60)
651665
MCOperand_CreateImm0(Inst, ((~0x1f) | Imm));
652666
else
@@ -657,7 +671,8 @@ static DecodeStatus decodeImm32n_95Operand(MCInst *Inst, uint64_t Imm,
657671
static DecodeStatus decodeImm8n_7Operand(MCInst *Inst, uint64_t Imm,
658672
int64_t Address, const void *Decoder)
659673
{
660-
CS_ASSERT(isUIntN(4, Imm) && "Invalid immediate");
674+
CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
675+
MCDisassembler_Fail);
661676
if (Imm > 7)
662677
MCOperand_CreateImm0(Inst, (Imm - 16));
663678
else
@@ -668,7 +683,9 @@ static DecodeStatus decodeImm8n_7Operand(MCInst *Inst, uint64_t Imm,
668683
static DecodeStatus decodeImm64n_4nOperand(MCInst *Inst, uint64_t Imm,
669684
int64_t Address, const void *Decoder)
670685
{
671-
CS_ASSERT(isUIntN(6, Imm) && ((Imm & 0x3) == 0) && "Invalid immediate");
686+
CS_ASSERT_RET_VAL(isUIntN(6, Imm) && ((Imm & 0x3) == 0) &&
687+
"Invalid immediate",
688+
MCDisassembler_Fail);
672689
MCOperand_CreateImm0(Inst, ((~0x3f) | (Imm)));
673690
return MCDisassembler_Success;
674691
}
@@ -677,8 +694,9 @@ static DecodeStatus decodeOffset8m32Operand(MCInst *Inst, uint64_t Imm,
677694
int64_t Address,
678695
const void *Decoder)
679696
{
680-
CS_ASSERT(isUIntN(10, Imm) && ((Imm & 0x3) == 0) &&
681-
"Invalid immediate");
697+
CS_ASSERT_RET_VAL(isUIntN(10, Imm) && ((Imm & 0x3) == 0) &&
698+
"Invalid immediate",
699+
MCDisassembler_Fail);
682700
MCOperand_CreateImm0(Inst, (Imm));
683701
return MCDisassembler_Success;
684702
}
@@ -687,64 +705,71 @@ static DecodeStatus decodeEntry_Imm12OpValue(MCInst *Inst, uint64_t Imm,
687705
int64_t Address,
688706
const void *Decoder)
689707
{
690-
CS_ASSERT(isUIntN(15, Imm) && ((Imm & 0x7) == 0) &&
691-
"Invalid immediate");
708+
CS_ASSERT_RET_VAL(isUIntN(15, Imm) && ((Imm & 0x7) == 0) &&
709+
"Invalid immediate",
710+
MCDisassembler_Fail);
692711
MCOperand_CreateImm0(Inst, (Imm));
693712
return MCDisassembler_Success;
694713
}
695714

696715
static DecodeStatus decodeShimm1_31Operand(MCInst *Inst, uint64_t Imm,
697716
int64_t Address, const void *Decoder)
698717
{
699-
CS_ASSERT(isUIntN(5, Imm) && "Invalid immediate");
718+
CS_ASSERT_RET_VAL(isUIntN(5, Imm) && "Invalid immediate",
719+
MCDisassembler_Fail);
700720
MCOperand_CreateImm0(Inst, (32 - Imm));
701721
return MCDisassembler_Success;
702722
}
703723

704724
//static DecodeStatus decodeShimm0_31Operand(MCInst *Inst, uint64_t Imm,
705725
// int64_t Address, const void *Decoder)
706726
//{
707-
// CS_ASSERT(isUIntN(5, Imm) && "Invalid immediate");
727+
// CS_ASSERT_RET_VAL(isUIntN(5, Imm) && "Invalid immediate", MCDisassembler_Fail);
708728
// MCOperand_CreateImm0(Inst, (32 - Imm));
709729
// return MCDisassembler_Success;
710730
//}
711731

712732
static DecodeStatus decodeImm7_22Operand(MCInst *Inst, uint64_t Imm,
713733
int64_t Address, const void *Decoder)
714734
{
715-
CS_ASSERT(isUIntN(4, Imm) && "Invalid immediate");
735+
CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
736+
MCDisassembler_Fail);
716737
MCOperand_CreateImm0(Inst, (Imm + 7));
717738
return MCDisassembler_Success;
718739
}
719740

720741
static DecodeStatus decodeSelect_2Operand(MCInst *Inst, uint64_t Imm,
721742
int64_t Address, const void *Decoder)
722743
{
723-
CS_ASSERT(isUIntN(8, Imm) && "Invalid immediate");
744+
CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
745+
MCDisassembler_Fail);
724746
MCOperand_CreateImm0(Inst, (Imm));
725747
return MCDisassembler_Success;
726748
}
727749

728750
static DecodeStatus decodeSelect_4Operand(MCInst *Inst, uint64_t Imm,
729751
int64_t Address, const void *Decoder)
730752
{
731-
CS_ASSERT(isUIntN(8, Imm) && "Invalid immediate");
753+
CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
754+
MCDisassembler_Fail);
732755
MCOperand_CreateImm0(Inst, (Imm));
733756
return MCDisassembler_Success;
734757
}
735758

736759
static DecodeStatus decodeSelect_8Operand(MCInst *Inst, uint64_t Imm,
737760
int64_t Address, const void *Decoder)
738761
{
739-
CS_ASSERT(isUIntN(8, Imm) && "Invalid immediate");
762+
CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
763+
MCDisassembler_Fail);
740764
MCOperand_CreateImm0(Inst, (Imm));
741765
return MCDisassembler_Success;
742766
}
743767

744768
static DecodeStatus decodeSelect_16Operand(MCInst *Inst, uint64_t Imm,
745769
int64_t Address, const void *Decoder)
746770
{
747-
CS_ASSERT(isUIntN(8, Imm) && "Invalid immediate");
771+
CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
772+
MCDisassembler_Fail);
748773
MCOperand_CreateImm0(Inst, (Imm));
749774
return MCDisassembler_Success;
750775
}
@@ -753,7 +778,8 @@ static DecodeStatus decodeSelect_256Operand(MCInst *Inst, uint64_t Imm,
753778
int64_t Address,
754779
const void *Decoder)
755780
{
756-
CS_ASSERT(isUIntN(8, Imm) && "Invalid immediate");
781+
CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
782+
MCDisassembler_Fail);
757783
MCOperand_CreateImm0(Inst, (Imm));
758784
return MCDisassembler_Success;
759785
}
@@ -762,7 +788,8 @@ static DecodeStatus decodeOffset_16_16Operand(MCInst *Inst, uint64_t Imm,
762788
int64_t Address,
763789
const void *Decoder)
764790
{
765-
CS_ASSERT(isIntN(Imm, 8) && "Invalid immediate");
791+
CS_ASSERT_RET_VAL(isIntN(Imm, 8) && "Invalid immediate",
792+
MCDisassembler_Fail);
766793
if ((Imm & 0xf) != 0)
767794
MCOperand_CreateImm0(Inst, (Imm << 4));
768795
else
@@ -774,7 +801,8 @@ static DecodeStatus decodeOffset_256_8Operand(MCInst *Inst, uint64_t Imm,
774801
int64_t Address,
775802
const void *Decoder)
776803
{
777-
CS_ASSERT(isIntN(16, Imm) && "Invalid immediate");
804+
CS_ASSERT_RET_VAL(isIntN(16, Imm) && "Invalid immediate",
805+
MCDisassembler_Fail);
778806
if ((Imm & 0x7) != 0)
779807
MCOperand_CreateImm0(Inst, (Imm << 3));
780808
else
@@ -786,7 +814,8 @@ static DecodeStatus decodeOffset_256_16Operand(MCInst *Inst, uint64_t Imm,
786814
int64_t Address,
787815
const void *Decoder)
788816
{
789-
CS_ASSERT(isIntN(16, Imm) && "Invalid immediate");
817+
CS_ASSERT_RET_VAL(isIntN(16, Imm) && "Invalid immediate",
818+
MCDisassembler_Fail);
790819
if ((Imm & 0xf) != 0)
791820
MCOperand_CreateImm0(Inst, (Imm << 4));
792821
else
@@ -798,7 +827,8 @@ static DecodeStatus decodeOffset_256_4Operand(MCInst *Inst, uint64_t Imm,
798827
int64_t Address,
799828
const void *Decoder)
800829
{
801-
CS_ASSERT(isIntN(16, Imm) && "Invalid immediate");
830+
CS_ASSERT_RET_VAL(isIntN(16, Imm) && "Invalid immediate",
831+
MCDisassembler_Fail);
802832
if ((Imm & 0x2) != 0)
803833
MCOperand_CreateImm0(Inst, (Imm << 2));
804834
else
@@ -810,7 +840,8 @@ static DecodeStatus decodeOffset_128_2Operand(MCInst *Inst, uint64_t Imm,
810840
int64_t Address,
811841
const void *Decoder)
812842
{
813-
CS_ASSERT(isUIntN(8, Imm) && "Invalid immediate");
843+
CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
844+
MCDisassembler_Fail);
814845
if ((Imm & 0x1) != 0)
815846
MCOperand_CreateImm0(Inst, (Imm << 1));
816847
else
@@ -822,7 +853,8 @@ static DecodeStatus decodeOffset_128_1Operand(MCInst *Inst, uint64_t Imm,
822853
int64_t Address,
823854
const void *Decoder)
824855
{
825-
CS_ASSERT(isUIntN(8, Imm) && "Invalid immediate");
856+
CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
857+
MCDisassembler_Fail);
826858
MCOperand_CreateImm0(Inst, (Imm));
827859
return MCDisassembler_Success;
828860
}
@@ -831,7 +863,8 @@ static DecodeStatus decodeOffset_64_16Operand(MCInst *Inst, uint64_t Imm,
831863
int64_t Address,
832864
const void *Decoder)
833865
{
834-
CS_ASSERT(isIntN(16, Imm) && "Invalid immediate");
866+
CS_ASSERT_RET_VAL(isIntN(16, Imm) && "Invalid immediate",
867+
MCDisassembler_Fail);
835868
if ((Imm & 0xf) != 0)
836869
MCOperand_CreateImm0(Inst, (Imm << 4));
837870
else
@@ -844,7 +877,8 @@ static int64_t TableB4const[16] = { -1, 1, 2, 3, 4, 5, 6, 7,
844877
static DecodeStatus decodeB4constOperand(MCInst *Inst, uint64_t Imm,
845878
int64_t Address, const void *Decoder)
846879
{
847-
CS_ASSERT(isUIntN(4, Imm) && "Invalid immediate");
880+
CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
881+
MCDisassembler_Fail);
848882

849883
MCOperand_CreateImm0(Inst, (TableB4const[Imm]));
850884
return MCDisassembler_Success;
@@ -855,7 +889,8 @@ static int64_t TableB4constu[16] = { 32768, 65536, 2, 3, 4, 5, 6, 7,
855889
static DecodeStatus decodeB4constuOperand(MCInst *Inst, uint64_t Imm,
856890
int64_t Address, const void *Decoder)
857891
{
858-
CS_ASSERT(isUIntN(4, Imm) && "Invalid immediate");
892+
CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
893+
MCDisassembler_Fail);
859894

860895
MCOperand_CreateImm0(Inst, (TableB4constu[Imm]));
861896
return MCDisassembler_Success;
@@ -864,7 +899,8 @@ static DecodeStatus decodeB4constuOperand(MCInst *Inst, uint64_t Imm,
864899
static DecodeStatus decodeMem8Operand(MCInst *Inst, uint64_t Imm,
865900
int64_t Address, const void *Decoder)
866901
{
867-
CS_ASSERT(isUIntN(12, Imm) && "Invalid immediate");
902+
CS_ASSERT_RET_VAL(isUIntN(12, Imm) && "Invalid immediate",
903+
MCDisassembler_Fail);
868904
DecodeARRegisterClass(Inst, Imm & 0xf, Address, Decoder);
869905
MCOperand_CreateImm0(Inst, ((Imm >> 4) & 0xff));
870906
return MCDisassembler_Success;
@@ -873,7 +909,8 @@ static DecodeStatus decodeMem8Operand(MCInst *Inst, uint64_t Imm,
873909
static DecodeStatus decodeMem16Operand(MCInst *Inst, uint64_t Imm,
874910
int64_t Address, const void *Decoder)
875911
{
876-
CS_ASSERT(isUIntN(12, Imm) && "Invalid immediate");
912+
CS_ASSERT_RET_VAL(isUIntN(12, Imm) && "Invalid immediate",
913+
MCDisassembler_Fail);
877914
DecodeARRegisterClass(Inst, Imm & 0xf, Address, Decoder);
878915
MCOperand_CreateImm0(Inst, ((Imm >> 3) & 0x1fe));
879916
return MCDisassembler_Success;
@@ -882,7 +919,8 @@ static DecodeStatus decodeMem16Operand(MCInst *Inst, uint64_t Imm,
882919
static DecodeStatus decodeMem32Operand(MCInst *Inst, uint64_t Imm,
883920
int64_t Address, const void *Decoder)
884921
{
885-
CS_ASSERT(isUIntN(12, Imm) && "Invalid immediate");
922+
CS_ASSERT_RET_VAL(isUIntN(12, Imm) && "Invalid immediate",
923+
MCDisassembler_Fail);
886924
DecodeARRegisterClass(Inst, Imm & 0xf, Address, Decoder);
887925
MCOperand_CreateImm0(Inst, ((Imm >> 2) & 0x3fc));
888926
return MCDisassembler_Success;
@@ -891,7 +929,8 @@ static DecodeStatus decodeMem32Operand(MCInst *Inst, uint64_t Imm,
891929
static DecodeStatus decodeMem32nOperand(MCInst *Inst, uint64_t Imm,
892930
int64_t Address, const void *Decoder)
893931
{
894-
CS_ASSERT(isUIntN(8, Imm) && "Invalid immediate");
932+
CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
933+
MCDisassembler_Fail);
895934
DecodeARRegisterClass(Inst, Imm & 0xf, Address, Decoder);
896935
MCOperand_CreateImm0(Inst, ((Imm >> 2) & 0x3c));
897936
return MCDisassembler_Success;

0 commit comments

Comments
 (0)