2626using namespace mozilla ;
2727using namespace mozilla ::interceptor::arm64;
2828
29+ constexpr uintptr_t kExamplePCValue = 0x7ff959a7ea80 ;
30+
2931bool TestCheckForPCRelAdrp () {
3032 // A real-world example from bug 1964688 comment 5:
3133 // 00007ff9`59a7ea80 d0dfff11 adrp xip1,00007ff9`19a60000
3234 Result<LoadOrBranch, PCRelCheckError> result =
33- CheckForPCRel (0x7ff959a7ea80 , 0xd0dfff11 );
35+ CheckForPCRel (kExamplePCValue , 0xd0dfff11 );
3436 if (result.isErr ()) {
3537 auto error = result.unwrapErr ();
3638 TEST_FAILED (
@@ -65,7 +67,7 @@ bool TestCheckForPCRelAdr() {
6567 // Fictional example with adr:
6668 // 00007ff959a7ea80 50dfff11 adr x17, #0x7ff959a3ea62
6769 Result<LoadOrBranch, PCRelCheckError> result =
68- CheckForPCRel (0x7ff959a7ea80 , 0x50dfff11 );
70+ CheckForPCRel (kExamplePCValue , 0x50dfff11 );
6971
7072 // For the moment we expect to recognize adr instructions but we don't have
7173 // a decoder
@@ -90,8 +92,56 @@ bool TestCheckForPCRelAdr() {
9092 return true ;
9193}
9294
95+ bool TestCheckForPCRelBlr () {
96+ // blr x16
97+ Result<LoadOrBranch, PCRelCheckError> result =
98+ CheckForPCRel (kExamplePCValue , 0xd63f0200 );
99+
100+ if (!result.isErr ()) {
101+ TEST_FAILED (
102+ " Unexpectedly recognized blr as a PC-relative instruction with a "
103+ " decoder. If you have implemented a decoder for this instruction, "
104+ " please update TestArm64Disassembler.cpp.\n " );
105+ }
106+
107+ auto error = result.unwrapErr ();
108+ if (error != PCRelCheckError::NoDecoderAvailable) {
109+ TEST_FAILED (
110+ " Failed to recognize blr as a PC-relative instruction, got "
111+ " PCRelCheckError %d.\n " ,
112+ error);
113+ }
114+
115+ TEST_PASS (
116+ " Properly recognized blr as a PC-relative instruction without a "
117+ " decoder.\n " );
118+ return true ;
119+ }
120+
121+ bool TestCheckForPCRelBr () {
122+ // br x16
123+ Result<LoadOrBranch, PCRelCheckError> result =
124+ CheckForPCRel (kExamplePCValue , 0xd61f0200 );
125+
126+ if (result.isErr ()) {
127+ auto error = result.unwrapErr ();
128+ if (error != PCRelCheckError::InstructionNotPCRel) {
129+ TEST_FAILED (
130+ " Failed to recognize br as a non-PC-relative instruction, got "
131+ " PCRelCheckError %d.\n " ,
132+ error);
133+ }
134+ } else {
135+ TEST_FAILED (" Incorrectly recognized br as a PC-relative instruction.\n " );
136+ }
137+
138+ TEST_PASS (" Properly recognized br as a non-PC-relative instruction.\n " );
139+ return true ;
140+ }
141+
93142int wmain (int argc, wchar_t * argv[]) {
94- if (!TestCheckForPCRelAdrp () || !TestCheckForPCRelAdr ()) {
143+ if (!TestCheckForPCRelAdrp () || !TestCheckForPCRelAdr () ||
144+ !TestCheckForPCRelBlr () || !TestCheckForPCRelBr ()) {
95145 return -1 ;
96146 }
97147 return 0 ;
0 commit comments