chrome_unwinder_android_v2_unittest.cc 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/profiler/chrome_unwinder_android_v2.h"
  5. #include "base/memory/aligned_memory.h"
  6. #include "base/profiler/chrome_unwind_info_android.h"
  7. #include "base/profiler/stack_sampling_profiler_test_util.h"
  8. #include "base/test/gtest_util.h"
  9. #include "build/build_config.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. namespace base {
  12. TEST(ChromeAndroidUnwindInstructionTest,
  13. TestSmallStackPointerIncrementMinValue) {
  14. RegisterContext thread_context = {};
  15. const uint8_t instruction = 0b00000000;
  16. const uint8_t* current_instruction = &instruction;
  17. thread_context.arm_sp = 0x10000000;
  18. bool pc_was_updated = false;
  19. ASSERT_EQ(ExecuteUnwindInstruction(current_instruction, pc_was_updated,
  20. &thread_context),
  21. UnwindInstructionResult::kInstructionPending);
  22. EXPECT_FALSE(pc_was_updated);
  23. ASSERT_EQ(current_instruction, &instruction + 1);
  24. EXPECT_EQ(0x10000004ul, thread_context.arm_sp);
  25. }
  26. TEST(ChromeAndroidUnwindInstructionTest,
  27. TestSmallStackPointerIncrementMidValue) {
  28. // xxxxxx = 4; vsp = vsp + (4 << 2) + 4 = vsp + 16 + 4 = vsp + 0x14.
  29. RegisterContext thread_context = {};
  30. const uint8_t instruction = 0b00000100;
  31. const uint8_t* current_instruction = &instruction;
  32. thread_context.arm_sp = 0x10000000;
  33. bool pc_was_updated = false;
  34. ASSERT_EQ(ExecuteUnwindInstruction(current_instruction, pc_was_updated,
  35. &thread_context),
  36. UnwindInstructionResult::kInstructionPending);
  37. EXPECT_FALSE(pc_was_updated);
  38. ASSERT_EQ(current_instruction, &instruction + 1);
  39. EXPECT_EQ(0x10000014ul, thread_context.arm_sp);
  40. }
  41. TEST(ChromeAndroidUnwindInstructionTest,
  42. TestSmallStackPointerIncrementMaxValue) {
  43. RegisterContext thread_context = {};
  44. const uint8_t instruction = 0b00111111;
  45. const uint8_t* current_instruction = &instruction;
  46. thread_context.arm_sp = 0x10000000;
  47. bool pc_was_updated = false;
  48. ASSERT_EQ(ExecuteUnwindInstruction(current_instruction, pc_was_updated,
  49. &thread_context),
  50. UnwindInstructionResult::kInstructionPending);
  51. EXPECT_FALSE(pc_was_updated);
  52. ASSERT_EQ(current_instruction, &instruction + 1);
  53. EXPECT_EQ(0x10000100ul, thread_context.arm_sp);
  54. }
  55. TEST(ChromeAndroidUnwindInstructionTest,
  56. TestSmallStackPointerIncrementOverflow) {
  57. RegisterContext thread_context = {};
  58. const uint8_t instruction = 0b00111111;
  59. const uint8_t* current_instruction = &instruction;
  60. thread_context.arm_sp = 0xffffffff;
  61. bool pc_was_updated = false;
  62. ASSERT_EQ(ExecuteUnwindInstruction(current_instruction, pc_was_updated,
  63. &thread_context),
  64. UnwindInstructionResult::kAborted);
  65. ASSERT_EQ(current_instruction, &instruction + 1);
  66. EXPECT_EQ(0xffffffff, thread_context.arm_sp);
  67. }
  68. TEST(ChromeAndroidUnwindInstructionTest,
  69. TestSmallStackPointerDecrementMinValue) {
  70. RegisterContext thread_context = {};
  71. const uint8_t instruction = 0b01000000;
  72. const uint8_t* current_instruction = &instruction;
  73. thread_context.arm_sp = 0x10000000;
  74. bool pc_was_updated = false;
  75. ASSERT_EQ(ExecuteUnwindInstruction(current_instruction, pc_was_updated,
  76. &thread_context),
  77. UnwindInstructionResult::kInstructionPending);
  78. EXPECT_FALSE(pc_was_updated);
  79. ASSERT_EQ(current_instruction, &instruction + 1);
  80. EXPECT_EQ(0x0ffffffcul, thread_context.arm_sp);
  81. }
  82. TEST(ChromeAndroidUnwindInstructionTest,
  83. TestSmallStackPointerDecrementMidValue) {
  84. // xxxxxx = 4; vsp = vsp - (4 << 2) - 4 = vsp - 16 - 4 = vsp - 0x14.
  85. RegisterContext thread_context = {};
  86. const uint8_t instruction = 0b01000100;
  87. const uint8_t* current_instruction = &instruction;
  88. thread_context.arm_sp = 0x10000000;
  89. bool pc_was_updated = false;
  90. ASSERT_EQ(ExecuteUnwindInstruction(current_instruction, pc_was_updated,
  91. &thread_context),
  92. UnwindInstructionResult::kInstructionPending);
  93. EXPECT_FALSE(pc_was_updated);
  94. ASSERT_EQ(current_instruction, &instruction + 1);
  95. EXPECT_EQ(0x0fffffecul, thread_context.arm_sp);
  96. }
  97. TEST(ChromeAndroidUnwindInstructionTest,
  98. TestSmallStackPointerDecrementMaxValue) {
  99. RegisterContext thread_context = {};
  100. const uint8_t instruction = 0b01111111;
  101. const uint8_t* current_instruction = &instruction;
  102. thread_context.arm_sp = 0x10000000;
  103. bool pc_was_updated = false;
  104. ASSERT_EQ(ExecuteUnwindInstruction(current_instruction, pc_was_updated,
  105. &thread_context),
  106. UnwindInstructionResult::kInstructionPending);
  107. EXPECT_FALSE(pc_was_updated);
  108. ASSERT_EQ(current_instruction, &instruction + 1);
  109. EXPECT_EQ(0x0fffff00ul, thread_context.arm_sp);
  110. }
  111. TEST(ChromeAndroidUnwindInstructionTest,
  112. TestSmallStackPointerDecrementUnderflow) {
  113. RegisterContext thread_context = {};
  114. const uint8_t instruction = 0b01111111;
  115. const uint8_t* current_instruction = &instruction;
  116. thread_context.arm_sp = 0x00000000;
  117. bool pc_was_updated = false;
  118. ASSERT_EQ(ExecuteUnwindInstruction(current_instruction, pc_was_updated,
  119. &thread_context),
  120. UnwindInstructionResult::kAborted);
  121. EXPECT_FALSE(pc_was_updated);
  122. ASSERT_EQ(current_instruction, &instruction + 1);
  123. EXPECT_EQ(0x0ul, thread_context.arm_sp);
  124. }
  125. using ChromeAndroidUnwindSetStackPointerFromRegisterValueTest =
  126. ::testing::TestWithParam<uint8_t>;
  127. INSTANTIATE_TEST_SUITE_P(
  128. All,
  129. ChromeAndroidUnwindSetStackPointerFromRegisterValueTest,
  130. // The function should set all registers except
  131. // - callee saved registers (r0, r1, r2, r3)
  132. // - sp (r13)
  133. // - pc (r15)
  134. ::testing::Values(4, 5, 6, 7, 8, 9, 10, 11, 12, 14));
  135. TEST_P(ChromeAndroidUnwindSetStackPointerFromRegisterValueTest,
  136. TestSetStackPointerFromRegisterValue) {
  137. const uint8_t register_index = GetParam();
  138. RegisterContext thread_context = {};
  139. thread_context.arm_r0 = 100;
  140. thread_context.arm_r1 = 101;
  141. thread_context.arm_r2 = 102;
  142. thread_context.arm_r3 = 103;
  143. thread_context.arm_r4 = 104;
  144. thread_context.arm_r5 = 105;
  145. thread_context.arm_r6 = 106;
  146. thread_context.arm_r7 = 107;
  147. thread_context.arm_r8 = 108;
  148. thread_context.arm_r9 = 109;
  149. thread_context.arm_r10 = 110;
  150. thread_context.arm_fp = 111; // r11
  151. thread_context.arm_ip = 112; // r12
  152. thread_context.arm_lr = 114; // r14
  153. const uint8_t instruction = 0b10010000 + register_index;
  154. const uint8_t* current_instruction = &instruction;
  155. bool pc_was_updated = false;
  156. ASSERT_EQ(ExecuteUnwindInstruction(current_instruction, pc_was_updated,
  157. &thread_context),
  158. UnwindInstructionResult::kInstructionPending);
  159. EXPECT_FALSE(pc_was_updated);
  160. ASSERT_EQ(current_instruction, &instruction + 1);
  161. EXPECT_EQ(100ul + register_index, thread_context.arm_sp);
  162. }
  163. TEST(ChromeAndroidUnwindInstructionTest, TestCompleteWithNoPriorPCUpdate) {
  164. RegisterContext thread_context = {};
  165. thread_context.arm_lr = 114; // r14
  166. thread_context.arm_pc = 115; // r15
  167. const uint8_t instruction = 0b10110000;
  168. const uint8_t* current_instruction = &instruction;
  169. bool pc_was_updated = false;
  170. ASSERT_EQ(ExecuteUnwindInstruction(current_instruction, pc_was_updated,
  171. &thread_context),
  172. UnwindInstructionResult::kCompleted);
  173. ASSERT_EQ(current_instruction, &instruction + 1);
  174. EXPECT_EQ(114ul, thread_context.arm_pc);
  175. }
  176. TEST(ChromeAndroidUnwindInstructionTest, TestCompleteWithPriorPCUpdate) {
  177. RegisterContext thread_context = {};
  178. thread_context.arm_lr = 114; // r14
  179. thread_context.arm_pc = 115; // r15
  180. const uint8_t instruction = 0b10110000;
  181. const uint8_t* current_instruction = &instruction;
  182. bool pc_was_updated = true;
  183. ASSERT_EQ(ExecuteUnwindInstruction(current_instruction, pc_was_updated,
  184. &thread_context),
  185. UnwindInstructionResult::kCompleted);
  186. ASSERT_EQ(current_instruction, &instruction + 1);
  187. EXPECT_EQ(115ul, thread_context.arm_pc);
  188. }
  189. TEST(ChromeAndroidUnwindInstructionTest,
  190. TestPopDiscontinuousRegistersIncludingPC) {
  191. RegisterContext thread_context = {};
  192. thread_context.arm_r0 = 100;
  193. thread_context.arm_r1 = 101;
  194. thread_context.arm_r2 = 102;
  195. thread_context.arm_r3 = 103;
  196. thread_context.arm_r4 = 104;
  197. thread_context.arm_r5 = 105;
  198. thread_context.arm_r6 = 106;
  199. thread_context.arm_r7 = 107;
  200. thread_context.arm_r8 = 108;
  201. thread_context.arm_r9 = 109;
  202. thread_context.arm_r10 = 110;
  203. thread_context.arm_fp = 111;
  204. thread_context.arm_ip = 112;
  205. thread_context.arm_lr = 113;
  206. thread_context.arm_pc = 114;
  207. // Pop up to 12 integer registers under masks {r15-r12}, {r11-r4}.
  208. const uintptr_t stack[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  209. thread_context.arm_sp = reinterpret_cast<uintptr_t>(&stack[0]);
  210. // Pop r15, r12, r8, r4.
  211. const uint8_t instruction[] = {0b10001001, 0b00010001};
  212. const uint8_t* current_instruction = instruction;
  213. bool pc_was_updated = false;
  214. ASSERT_EQ(ExecuteUnwindInstruction(current_instruction, pc_was_updated,
  215. &thread_context),
  216. UnwindInstructionResult::kInstructionPending);
  217. EXPECT_TRUE(pc_was_updated);
  218. ASSERT_EQ(current_instruction, instruction + 2);
  219. EXPECT_EQ(reinterpret_cast<uintptr_t>(&stack[0] + 4), thread_context.arm_sp);
  220. EXPECT_EQ(100ul, thread_context.arm_r0);
  221. EXPECT_EQ(101ul, thread_context.arm_r1);
  222. EXPECT_EQ(102ul, thread_context.arm_r2);
  223. EXPECT_EQ(103ul, thread_context.arm_r3);
  224. EXPECT_EQ(1ul, thread_context.arm_r4);
  225. EXPECT_EQ(105ul, thread_context.arm_r5);
  226. EXPECT_EQ(106ul, thread_context.arm_r6);
  227. EXPECT_EQ(107ul, thread_context.arm_r7);
  228. EXPECT_EQ(2ul, thread_context.arm_r8);
  229. EXPECT_EQ(109ul, thread_context.arm_r9);
  230. EXPECT_EQ(110ul, thread_context.arm_r10);
  231. EXPECT_EQ(111ul, thread_context.arm_fp);
  232. EXPECT_EQ(3ul, thread_context.arm_ip);
  233. EXPECT_EQ(113ul, thread_context.arm_lr);
  234. EXPECT_EQ(4ul, thread_context.arm_pc);
  235. }
  236. TEST(ChromeAndroidUnwindInstructionTest, TestPopDiscontinuousRegisters) {
  237. RegisterContext thread_context = {};
  238. thread_context.arm_r0 = 100;
  239. thread_context.arm_r1 = 101;
  240. thread_context.arm_r2 = 102;
  241. thread_context.arm_r3 = 103;
  242. thread_context.arm_r4 = 104;
  243. thread_context.arm_r5 = 105;
  244. thread_context.arm_r6 = 106;
  245. thread_context.arm_r7 = 107;
  246. thread_context.arm_r8 = 108;
  247. thread_context.arm_r9 = 109;
  248. thread_context.arm_r10 = 110;
  249. thread_context.arm_fp = 111;
  250. thread_context.arm_ip = 112;
  251. thread_context.arm_lr = 113;
  252. thread_context.arm_pc = 114;
  253. // Pop up to 12 integer registers under masks {r15-r12}, {r11-r4}.
  254. const uintptr_t stack[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  255. thread_context.arm_sp = reinterpret_cast<uintptr_t>(&stack[0]);
  256. // Pop r12, r8, r4.
  257. const uint8_t instruction[] = {0b10000001, 0b00010001};
  258. const uint8_t* current_instruction = instruction;
  259. bool pc_was_updated = false;
  260. ASSERT_EQ(ExecuteUnwindInstruction(current_instruction, pc_was_updated,
  261. &thread_context),
  262. UnwindInstructionResult::kInstructionPending);
  263. EXPECT_FALSE(pc_was_updated);
  264. ASSERT_EQ(current_instruction, instruction + 2);
  265. EXPECT_EQ(reinterpret_cast<uintptr_t>(&stack[0] + 3), thread_context.arm_sp);
  266. EXPECT_EQ(100ul, thread_context.arm_r0);
  267. EXPECT_EQ(101ul, thread_context.arm_r1);
  268. EXPECT_EQ(102ul, thread_context.arm_r2);
  269. EXPECT_EQ(103ul, thread_context.arm_r3);
  270. EXPECT_EQ(1ul, thread_context.arm_r4);
  271. EXPECT_EQ(105ul, thread_context.arm_r5);
  272. EXPECT_EQ(106ul, thread_context.arm_r6);
  273. EXPECT_EQ(107ul, thread_context.arm_r7);
  274. EXPECT_EQ(2ul, thread_context.arm_r8);
  275. EXPECT_EQ(109ul, thread_context.arm_r9);
  276. EXPECT_EQ(110ul, thread_context.arm_r10);
  277. EXPECT_EQ(111ul, thread_context.arm_fp);
  278. EXPECT_EQ(3ul, thread_context.arm_ip);
  279. EXPECT_EQ(113ul, thread_context.arm_lr);
  280. EXPECT_EQ(114ul, thread_context.arm_pc);
  281. }
  282. TEST(ChromeAndroidUnwindInstructionTest,
  283. TestPopDiscontinuousRegistersOverflow) {
  284. RegisterContext thread_context = {};
  285. thread_context.arm_r0 = 100;
  286. thread_context.arm_r1 = 101;
  287. thread_context.arm_r2 = 102;
  288. thread_context.arm_r3 = 103;
  289. thread_context.arm_r4 = 104;
  290. thread_context.arm_r5 = 105;
  291. thread_context.arm_r6 = 106;
  292. thread_context.arm_r7 = 107;
  293. thread_context.arm_r8 = 108;
  294. thread_context.arm_r9 = 109;
  295. thread_context.arm_r10 = 110;
  296. thread_context.arm_fp = 111;
  297. thread_context.arm_ip = 112;
  298. thread_context.arm_lr = 113;
  299. thread_context.arm_pc = 114;
  300. // Pop up to 12 integer registers under masks {r15-r12}, {r11-r4}.
  301. thread_context.arm_sp = 0xffffffff;
  302. // Pop r15, r12, r8, r4.
  303. const uint8_t instruction[] = {0b10001001, 0b00010001};
  304. const uint8_t* current_instruction = instruction;
  305. bool pc_was_updated = false;
  306. ASSERT_EQ(ExecuteUnwindInstruction(current_instruction, pc_was_updated,
  307. &thread_context),
  308. UnwindInstructionResult::kAborted);
  309. EXPECT_FALSE(pc_was_updated);
  310. ASSERT_EQ(current_instruction, instruction + 2);
  311. EXPECT_EQ(0xffffffff, thread_context.arm_sp);
  312. EXPECT_EQ(100ul, thread_context.arm_r0);
  313. EXPECT_EQ(101ul, thread_context.arm_r1);
  314. EXPECT_EQ(102ul, thread_context.arm_r2);
  315. EXPECT_EQ(103ul, thread_context.arm_r3);
  316. EXPECT_EQ(104ul, thread_context.arm_r4);
  317. EXPECT_EQ(105ul, thread_context.arm_r5);
  318. EXPECT_EQ(106ul, thread_context.arm_r6);
  319. EXPECT_EQ(107ul, thread_context.arm_r7);
  320. EXPECT_EQ(108ul, thread_context.arm_r8);
  321. EXPECT_EQ(109ul, thread_context.arm_r9);
  322. EXPECT_EQ(110ul, thread_context.arm_r10);
  323. EXPECT_EQ(111ul, thread_context.arm_fp);
  324. EXPECT_EQ(112ul, thread_context.arm_ip);
  325. EXPECT_EQ(113ul, thread_context.arm_lr);
  326. EXPECT_EQ(114ul, thread_context.arm_pc);
  327. }
  328. TEST(ChromeAndroidUnwindInstructionTest, TestRefuseToUnwind) {
  329. RegisterContext thread_context = {};
  330. const uint8_t instruction[] = {0b10000000, 0b0};
  331. const uint8_t* current_instruction = instruction;
  332. bool pc_was_updated = false;
  333. ASSERT_EQ(ExecuteUnwindInstruction(current_instruction, pc_was_updated,
  334. &thread_context),
  335. UnwindInstructionResult::kAborted);
  336. EXPECT_FALSE(pc_was_updated);
  337. ASSERT_EQ(current_instruction, instruction + 2);
  338. }
  339. TEST(ChromeAndroidUnwindInstructionTest,
  340. TestPopRegistersIncludingR14MinRegisters) {
  341. RegisterContext thread_context = {};
  342. thread_context.arm_r0 = 100;
  343. thread_context.arm_r1 = 101;
  344. thread_context.arm_r2 = 102;
  345. thread_context.arm_r3 = 103;
  346. thread_context.arm_r4 = 104;
  347. thread_context.arm_r5 = 105;
  348. thread_context.arm_r6 = 106;
  349. thread_context.arm_r7 = 107;
  350. thread_context.arm_r8 = 108;
  351. thread_context.arm_r9 = 109;
  352. thread_context.arm_r10 = 110;
  353. thread_context.arm_fp = 111;
  354. thread_context.arm_ip = 112;
  355. thread_context.arm_lr = 113;
  356. // Popping r4 - r[4 + nnn], r14, at most 9 registers.
  357. // r14 = lr
  358. const uintptr_t stack[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  359. thread_context.arm_sp = reinterpret_cast<uintptr_t>(&stack[0]);
  360. const uint8_t instruction = 0b10101000;
  361. const uint8_t* current_instruction = &instruction;
  362. bool pc_was_updated = false;
  363. ASSERT_EQ(ExecuteUnwindInstruction(current_instruction, pc_was_updated,
  364. &thread_context),
  365. UnwindInstructionResult::kInstructionPending);
  366. EXPECT_FALSE(pc_was_updated);
  367. ASSERT_EQ(current_instruction, &instruction + 1);
  368. EXPECT_EQ(reinterpret_cast<uintptr_t>(&stack[0] + 2), thread_context.arm_sp);
  369. EXPECT_EQ(100ul, thread_context.arm_r0);
  370. EXPECT_EQ(101ul, thread_context.arm_r1);
  371. EXPECT_EQ(102ul, thread_context.arm_r2);
  372. EXPECT_EQ(103ul, thread_context.arm_r3);
  373. EXPECT_EQ(1ul, thread_context.arm_r4);
  374. EXPECT_EQ(105ul, thread_context.arm_r5);
  375. EXPECT_EQ(106ul, thread_context.arm_r6);
  376. EXPECT_EQ(107ul, thread_context.arm_r7);
  377. EXPECT_EQ(108ul, thread_context.arm_r8);
  378. EXPECT_EQ(109ul, thread_context.arm_r9);
  379. EXPECT_EQ(110ul, thread_context.arm_r10);
  380. EXPECT_EQ(111ul, thread_context.arm_fp);
  381. EXPECT_EQ(112ul, thread_context.arm_ip);
  382. EXPECT_EQ(2ul, thread_context.arm_lr);
  383. }
  384. TEST(ChromeAndroidUnwindInstructionTest,
  385. TestPopRegistersIncludingR14MidRegisters) {
  386. RegisterContext thread_context = {};
  387. thread_context.arm_r0 = 100;
  388. thread_context.arm_r1 = 101;
  389. thread_context.arm_r2 = 102;
  390. thread_context.arm_r3 = 103;
  391. thread_context.arm_r4 = 104;
  392. thread_context.arm_r5 = 105;
  393. thread_context.arm_r6 = 106;
  394. thread_context.arm_r7 = 107;
  395. thread_context.arm_r8 = 108;
  396. thread_context.arm_r9 = 109;
  397. thread_context.arm_r10 = 110;
  398. thread_context.arm_fp = 111;
  399. thread_context.arm_ip = 112;
  400. thread_context.arm_lr = 113;
  401. // Popping r4 - r[4 + nnn], r14, at most 9 registers.
  402. // r14 = lr
  403. const uintptr_t stack[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  404. thread_context.arm_sp = reinterpret_cast<uintptr_t>(&stack[0]);
  405. const uint8_t instruction = 0b10101100; // Pop r4-r8, r14.
  406. const uint8_t* current_instruction = &instruction;
  407. bool pc_was_updated = false;
  408. ASSERT_EQ(ExecuteUnwindInstruction(current_instruction, pc_was_updated,
  409. &thread_context),
  410. UnwindInstructionResult::kInstructionPending);
  411. EXPECT_FALSE(pc_was_updated);
  412. ASSERT_EQ(current_instruction, &instruction + 1);
  413. EXPECT_EQ(reinterpret_cast<uintptr_t>(&stack[0] + 6), thread_context.arm_sp);
  414. EXPECT_EQ(100ul, thread_context.arm_r0);
  415. EXPECT_EQ(101ul, thread_context.arm_r1);
  416. EXPECT_EQ(102ul, thread_context.arm_r2);
  417. EXPECT_EQ(103ul, thread_context.arm_r3);
  418. EXPECT_EQ(1ul, thread_context.arm_r4);
  419. EXPECT_EQ(2ul, thread_context.arm_r5);
  420. EXPECT_EQ(3ul, thread_context.arm_r6);
  421. EXPECT_EQ(4ul, thread_context.arm_r7);
  422. EXPECT_EQ(5ul, thread_context.arm_r8);
  423. EXPECT_EQ(109ul, thread_context.arm_r9);
  424. EXPECT_EQ(110ul, thread_context.arm_r10);
  425. EXPECT_EQ(111ul, thread_context.arm_fp);
  426. EXPECT_EQ(112ul, thread_context.arm_ip);
  427. EXPECT_EQ(6ul, thread_context.arm_lr);
  428. }
  429. TEST(ChromeAndroidUnwindInstructionTest,
  430. TestPopRegistersIncludingR14MaxRegisters) {
  431. RegisterContext thread_context = {};
  432. thread_context.arm_r0 = 100;
  433. thread_context.arm_r1 = 101;
  434. thread_context.arm_r2 = 102;
  435. thread_context.arm_r3 = 103;
  436. thread_context.arm_r4 = 104;
  437. thread_context.arm_r5 = 105;
  438. thread_context.arm_r6 = 106;
  439. thread_context.arm_r7 = 107;
  440. thread_context.arm_r8 = 108;
  441. thread_context.arm_r9 = 109;
  442. thread_context.arm_r10 = 110;
  443. thread_context.arm_fp = 111;
  444. thread_context.arm_ip = 112;
  445. thread_context.arm_lr = 113;
  446. // Popping r4 - r[4 + nnn], r14, at most 9 registers.
  447. // r14 = lr
  448. const uintptr_t stack[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  449. thread_context.arm_sp = reinterpret_cast<uintptr_t>(&stack[0]);
  450. const uint8_t instruction = 0b10101111; // Pop r4 - r11, r14.
  451. const uint8_t* current_instruction = &instruction;
  452. bool pc_was_updated = false;
  453. ASSERT_EQ(ExecuteUnwindInstruction(current_instruction, pc_was_updated,
  454. &thread_context),
  455. UnwindInstructionResult::kInstructionPending);
  456. EXPECT_FALSE(pc_was_updated);
  457. ASSERT_EQ(current_instruction, &instruction + 1);
  458. EXPECT_EQ(reinterpret_cast<uintptr_t>(&stack[0] + 9), thread_context.arm_sp);
  459. EXPECT_EQ(100ul, thread_context.arm_r0);
  460. EXPECT_EQ(101ul, thread_context.arm_r1);
  461. EXPECT_EQ(102ul, thread_context.arm_r2);
  462. EXPECT_EQ(103ul, thread_context.arm_r3);
  463. EXPECT_EQ(1ul, thread_context.arm_r4);
  464. EXPECT_EQ(2ul, thread_context.arm_r5);
  465. EXPECT_EQ(3ul, thread_context.arm_r6);
  466. EXPECT_EQ(4ul, thread_context.arm_r7);
  467. EXPECT_EQ(5ul, thread_context.arm_r8);
  468. EXPECT_EQ(6ul, thread_context.arm_r9);
  469. EXPECT_EQ(7ul, thread_context.arm_r10);
  470. EXPECT_EQ(8ul, thread_context.arm_fp);
  471. EXPECT_EQ(112ul, thread_context.arm_ip);
  472. EXPECT_EQ(9ul, thread_context.arm_lr);
  473. }
  474. TEST(ChromeAndroidUnwindInstructionTest, TestPopRegistersIncludingR14Overflow) {
  475. RegisterContext thread_context = {};
  476. thread_context.arm_r0 = 100;
  477. thread_context.arm_r1 = 101;
  478. thread_context.arm_r2 = 102;
  479. thread_context.arm_r3 = 103;
  480. thread_context.arm_r4 = 104;
  481. thread_context.arm_r5 = 105;
  482. thread_context.arm_r6 = 106;
  483. thread_context.arm_r7 = 107;
  484. thread_context.arm_r8 = 108;
  485. thread_context.arm_r9 = 109;
  486. thread_context.arm_r10 = 110;
  487. thread_context.arm_fp = 111;
  488. thread_context.arm_ip = 112;
  489. thread_context.arm_lr = 113;
  490. // Popping r4 - r[4 + nnn], r14, at most 9 registers.
  491. // r14 = lr
  492. thread_context.arm_sp = 0xffffffff;
  493. const uint8_t instruction = 0b10101111; // Pop r4 - r11, r14.
  494. const uint8_t* current_instruction = &instruction;
  495. bool pc_was_updated = false;
  496. ASSERT_EQ(ExecuteUnwindInstruction(current_instruction, pc_was_updated,
  497. &thread_context),
  498. UnwindInstructionResult::kAborted);
  499. EXPECT_FALSE(pc_was_updated);
  500. ASSERT_EQ(current_instruction, &instruction + 1);
  501. EXPECT_EQ(0xffffffff, thread_context.arm_sp);
  502. EXPECT_EQ(100ul, thread_context.arm_r0);
  503. EXPECT_EQ(101ul, thread_context.arm_r1);
  504. EXPECT_EQ(102ul, thread_context.arm_r2);
  505. EXPECT_EQ(103ul, thread_context.arm_r3);
  506. EXPECT_EQ(104ul, thread_context.arm_r4);
  507. EXPECT_EQ(105ul, thread_context.arm_r5);
  508. EXPECT_EQ(106ul, thread_context.arm_r6);
  509. EXPECT_EQ(107ul, thread_context.arm_r7);
  510. EXPECT_EQ(108ul, thread_context.arm_r8);
  511. EXPECT_EQ(109ul, thread_context.arm_r9);
  512. EXPECT_EQ(110ul, thread_context.arm_r10);
  513. EXPECT_EQ(111ul, thread_context.arm_fp);
  514. EXPECT_EQ(112ul, thread_context.arm_ip);
  515. EXPECT_EQ(113ul, thread_context.arm_lr);
  516. }
  517. TEST(ChromeAndroidUnwindInstructionTest, TestBigStackPointerIncrementMinValue) {
  518. RegisterContext thread_context = {};
  519. thread_context.arm_sp = 0x10000000;
  520. const uint8_t increment_0[] = {
  521. 0b10110010,
  522. 0b00000000,
  523. };
  524. const uint8_t* current_instruction = &increment_0[0];
  525. bool pc_was_updated = false;
  526. ASSERT_EQ(ExecuteUnwindInstruction(current_instruction, pc_was_updated,
  527. &thread_context),
  528. UnwindInstructionResult::kInstructionPending);
  529. EXPECT_FALSE(pc_was_updated);
  530. ASSERT_EQ(current_instruction, increment_0 + sizeof(increment_0));
  531. // vsp + 0x204 + (0 << 2)
  532. // = vsp + 0x204
  533. EXPECT_EQ(0x10000204ul, thread_context.arm_sp);
  534. }
  535. TEST(ChromeAndroidUnwindInstructionTest, TestBigStackPointerIncrementMidValue) {
  536. RegisterContext thread_context = {};
  537. thread_context.arm_sp = 0x10000000;
  538. const uint8_t increment_4[] = {
  539. 0b10110010,
  540. 0b00000100,
  541. };
  542. const uint8_t* current_instruction = &increment_4[0];
  543. // vsp + 0x204 + (4 << 2)
  544. // = vsp + 0x204 + 0x10
  545. // = vsp + 0x214
  546. bool pc_was_updated = false;
  547. ASSERT_EQ(ExecuteUnwindInstruction(current_instruction, pc_was_updated,
  548. &thread_context),
  549. UnwindInstructionResult::kInstructionPending);
  550. EXPECT_FALSE(pc_was_updated);
  551. ASSERT_EQ(current_instruction, increment_4 + sizeof(increment_4));
  552. EXPECT_EQ(0x10000214ul, thread_context.arm_sp);
  553. }
  554. TEST(ChromeAndroidUnwindInstructionTest,
  555. TestBigStackPointerIncrementLargeValue) {
  556. RegisterContext thread_context = {};
  557. thread_context.arm_sp = 0x10000000;
  558. const uint8_t increment_128[] = {
  559. 0b10110010,
  560. 0b10000000,
  561. 0b00000001,
  562. };
  563. const uint8_t* current_instruction = &increment_128[0];
  564. // vsp + 0x204 + (128 << 2)
  565. // = vsp + 0x204 + 512
  566. // = vsp + 0x204 + 0x200
  567. // = vsp + 0x404
  568. bool pc_was_updated = false;
  569. ASSERT_EQ(ExecuteUnwindInstruction(current_instruction, pc_was_updated,
  570. &thread_context),
  571. UnwindInstructionResult::kInstructionPending);
  572. EXPECT_FALSE(pc_was_updated);
  573. ASSERT_EQ(current_instruction, increment_128 + sizeof(increment_128));
  574. EXPECT_EQ(0x10000404ul, thread_context.arm_sp);
  575. }
  576. TEST(ChromeAndroidUnwindInstructionTest, TestBigStackPointerIncrementOverflow) {
  577. RegisterContext thread_context = {};
  578. thread_context.arm_sp = 0xffffffff;
  579. const uint8_t increment_overflow[] = {
  580. 0b10110010,
  581. 0b10000000,
  582. 0b00000001,
  583. }; // ULEB128 = 128
  584. const uint8_t* current_instruction = &increment_overflow[0];
  585. bool pc_was_updated = false;
  586. ASSERT_EQ(ExecuteUnwindInstruction(current_instruction, pc_was_updated,
  587. &thread_context),
  588. UnwindInstructionResult::kAborted);
  589. EXPECT_FALSE(pc_was_updated);
  590. ASSERT_EQ(current_instruction,
  591. increment_overflow + sizeof(increment_overflow));
  592. EXPECT_EQ(0xfffffffful, thread_context.arm_sp);
  593. }
  594. TEST(ChromeUnwinderAndroidV2Test,
  595. TestFunctionOffsetTableLookupExactMatchingOffset) {
  596. const uint8_t function_offset_table[] = {
  597. // Function 1: [(130, 2), (128, 3), (0, 4)]
  598. // offset = 130
  599. 0b10000010,
  600. 0b00000001,
  601. // unwind index = 2
  602. 0b00000010,
  603. // offset = 128
  604. 0b10000000,
  605. 0b00000001,
  606. // unwind index = 3
  607. 0b00000011,
  608. // offset = 0
  609. 0b00000000,
  610. // unwind index = 4
  611. 0b00000100,
  612. };
  613. EXPECT_EQ(3ul, GetFirstUnwindInstructionIndexFromFunctionOffsetTableEntry(
  614. &function_offset_table[0],
  615. /* instruction_offset_from_function_start */ 128));
  616. }
  617. TEST(ChromeUnwinderAndroidV2Test,
  618. TestFunctionOffsetTableLookupNonExactMatchingOffset) {
  619. const uint8_t function_offset_table[] = {
  620. // Function 1: [(130, 2), (128, 3), (0, 4)]
  621. // offset = 130
  622. 0b10000010,
  623. 0b00000001,
  624. // unwind index = 2
  625. 0b00000010,
  626. // offset = 128
  627. 0b10000000,
  628. 0b00000001,
  629. // unwind index = 3
  630. 0b00000011,
  631. // offset = 0
  632. 0b00000000,
  633. // unwind index = 4
  634. 0b00000100,
  635. };
  636. EXPECT_EQ(3ul, GetFirstUnwindInstructionIndexFromFunctionOffsetTableEntry(
  637. &function_offset_table[0],
  638. /* instruction_offset_from_function_start */ 129));
  639. }
  640. TEST(ChromeUnwinderAndroidV2Test, TestFunctionOffsetTableLookupZeroOffset) {
  641. const uint8_t function_offset_table[] = {
  642. // Function 1: [(130, 2), (128, 3), (0, 4)]
  643. // offset = 130
  644. 0b10000010,
  645. 0b00000001,
  646. // unwind index = 2
  647. 0b00000010,
  648. // offset = 128
  649. 0b10000000,
  650. 0b00000001,
  651. // unwind index = 3
  652. 0b00000011,
  653. // offset = 0
  654. 0b00000000,
  655. // unwind index = 4
  656. 0b00000100,
  657. };
  658. EXPECT_EQ(4ul, GetFirstUnwindInstructionIndexFromFunctionOffsetTableEntry(
  659. &function_offset_table[0],
  660. /* instruction_offset_from_function_start */ 0));
  661. }
  662. TEST(ChromeUnwinderAndroidV2Test, TestAddressTableLookupEntryInPage) {
  663. const uint32_t page_start_instructions[] = {0, 2};
  664. const FunctionTableEntry function_offset_table_indices[] = {
  665. // Page 0
  666. {
  667. /* function_start_address_page_instruction_offset */ 0,
  668. /* function_offset_table_byte_index */ 20,
  669. },
  670. {
  671. /* function_start_address_page_instruction_offset */ 4,
  672. /* function_offset_table_byte_index */ 40,
  673. },
  674. // Page 1
  675. {
  676. /* function_start_address_page_instruction_offset */ 6,
  677. /* function_offset_table_byte_index */ 70,
  678. },
  679. };
  680. {
  681. const uint32_t page_number = 0;
  682. const uint32_t page_instruction_offset = 4;
  683. const auto entry_found = GetFunctionTableIndexFromInstructionOffset(
  684. page_start_instructions, function_offset_table_indices,
  685. /* instruction_offset */ (page_instruction_offset << 1) +
  686. (page_number << 17));
  687. ASSERT_NE(absl::nullopt, entry_found);
  688. EXPECT_EQ(0, entry_found->instruction_offset_from_function_start);
  689. EXPECT_EQ(40ul, entry_found->function_offset_table_byte_index);
  690. }
  691. {
  692. const uint32_t page_number = 0;
  693. const uint32_t page_instruction_offset = 50;
  694. const auto entry_found = GetFunctionTableIndexFromInstructionOffset(
  695. page_start_instructions, function_offset_table_indices,
  696. /* instruction_offset */ (page_instruction_offset << 1) +
  697. (page_number << 17));
  698. ASSERT_NE(absl::nullopt, entry_found);
  699. EXPECT_EQ(46, entry_found->instruction_offset_from_function_start);
  700. EXPECT_EQ(40ul, entry_found->function_offset_table_byte_index);
  701. }
  702. // Lookup last instruction in last function.
  703. {
  704. const uint32_t page_number = 1;
  705. const uint32_t page_instruction_offset = 0xffff;
  706. const auto entry_found = GetFunctionTableIndexFromInstructionOffset(
  707. page_start_instructions, function_offset_table_indices,
  708. /* instruction_offset */ (page_instruction_offset << 1) +
  709. (page_number << 17));
  710. ASSERT_NE(absl::nullopt, entry_found);
  711. // 0xffff - 6 = 0xfff9.
  712. EXPECT_EQ(0xfff9, entry_found->instruction_offset_from_function_start);
  713. EXPECT_EQ(70ul, entry_found->function_offset_table_byte_index);
  714. }
  715. }
  716. TEST(ChromeUnwinderAndroidV2Test, TestAddressTableLookupEmptyPage) {
  717. const uint32_t page_start_instructions[] = {0, 1, 1};
  718. const FunctionTableEntry function_offset_table_indices[] = {
  719. // Page 0
  720. {
  721. /* function_start_address_page_instruction_offset */ 0,
  722. /* function_offset_table_byte_index */ 20,
  723. },
  724. // Page 1 is empty
  725. // Page 2
  726. {
  727. /* function_start_address_page_instruction_offset */ 6,
  728. /* function_offset_table_byte_index */ 70,
  729. },
  730. };
  731. const uint32_t page_number = 1;
  732. const uint32_t page_instruction_offset = 4;
  733. const auto entry_found = GetFunctionTableIndexFromInstructionOffset(
  734. page_start_instructions, function_offset_table_indices,
  735. /* instruction_offset */ (page_instruction_offset << 1) +
  736. (page_number << 17));
  737. ASSERT_NE(absl::nullopt, entry_found);
  738. EXPECT_EQ(0x10004, entry_found->instruction_offset_from_function_start);
  739. EXPECT_EQ(20ul, entry_found->function_offset_table_byte_index);
  740. }
  741. TEST(ChromeUnwinderAndroidV2Test,
  742. TestAddressTableLookupInvalidIntructionOffset) {
  743. const uint32_t page_start_instructions[] = {0, 1};
  744. const FunctionTableEntry function_offset_table_indices[] = {
  745. // Page 0
  746. // This function spans from page 0 offset 0 to page 1 offset 5.
  747. {
  748. /* function_start_address_page_instruction_offset */ 0,
  749. /* function_offset_table_byte_index */ 20,
  750. },
  751. // Page 1
  752. {
  753. /* function_start_address_page_instruction_offset */ 6,
  754. /* function_offset_table_byte_index */ 70,
  755. },
  756. };
  757. // Instruction offset lies after last page on page table.
  758. {
  759. const uint32_t page_number = 50;
  760. const uint32_t page_instruction_offset = 6;
  761. const auto entry_found = GetFunctionTableIndexFromInstructionOffset(
  762. page_start_instructions, function_offset_table_indices,
  763. /* instruction_offset */ (page_instruction_offset << 1) +
  764. (page_number << 17));
  765. ASSERT_EQ(absl::nullopt, entry_found);
  766. }
  767. {
  768. const uint32_t page_number = 2;
  769. const uint32_t page_instruction_offset = 0;
  770. const auto entry_found = GetFunctionTableIndexFromInstructionOffset(
  771. page_start_instructions, function_offset_table_indices,
  772. /* instruction_offset */ (page_instruction_offset << 1) +
  773. (page_number << 17));
  774. ASSERT_EQ(absl::nullopt, entry_found);
  775. }
  776. }
  777. TEST(ChromeUnwinderAndroidV2Test,
  778. TestAddressTableLookupOnSecondPageOfFunctionSpanningPageBoundary) {
  779. const uint32_t page_start_instructions[] = {0, 1, 2};
  780. const FunctionTableEntry function_offset_table_indices[] = {
  781. // Page 0
  782. {
  783. /* function_start_address_page_instruction_offset */ 0,
  784. /* function_offset_table_byte_index */ 20,
  785. },
  786. // Page 1
  787. {
  788. /* function_start_address_page_instruction_offset */ 6,
  789. /* function_offset_table_byte_index */ 70,
  790. },
  791. // Page 2
  792. {
  793. /* function_start_address_page_instruction_offset */ 10,
  794. /* function_offset_table_byte_index */ 80,
  795. }};
  796. const uint32_t page_number = 1;
  797. const uint32_t page_instruction_offset = 4;
  798. const auto entry_found = GetFunctionTableIndexFromInstructionOffset(
  799. page_start_instructions, function_offset_table_indices,
  800. /* instruction_offset */ (page_instruction_offset << 1) +
  801. (page_number << 17));
  802. ASSERT_NE(absl::nullopt, entry_found);
  803. EXPECT_EQ(0x10004, entry_found->instruction_offset_from_function_start);
  804. EXPECT_EQ(20ul, entry_found->function_offset_table_byte_index);
  805. }
  806. TEST(ChromeUnwinderAndroidV2Test,
  807. TestAddressTableLookupWithinFunctionSpanningMultiplePages) {
  808. const uint32_t page_start_instructions[] = {0, 1, 1, 1};
  809. const FunctionTableEntry function_offset_table_indices[] = {
  810. // Page 0
  811. // This function spans from page 0 offset 0 to page 3 offset 5.
  812. {
  813. /* function_start_address_page_instruction_offset */ 0,
  814. /* function_offset_table_byte_index */ 20,
  815. },
  816. // Page 1 is empty
  817. // Page 2 is empty
  818. // Page 3
  819. {
  820. /* function_start_address_page_instruction_offset */ 6,
  821. /* function_offset_table_byte_index */ 70,
  822. },
  823. };
  824. {
  825. const uint32_t page_number = 0;
  826. const uint32_t page_instruction_offset = 4;
  827. const auto entry_found = GetFunctionTableIndexFromInstructionOffset(
  828. page_start_instructions, function_offset_table_indices,
  829. /* instruction_offset */ (page_instruction_offset << 1) +
  830. (page_number << 17));
  831. ASSERT_NE(absl::nullopt, entry_found);
  832. EXPECT_EQ(0x4, entry_found->instruction_offset_from_function_start);
  833. EXPECT_EQ(20ul, entry_found->function_offset_table_byte_index);
  834. }
  835. {
  836. const uint32_t page_number = 1;
  837. const uint32_t page_instruction_offset = 4;
  838. const auto entry_found = GetFunctionTableIndexFromInstructionOffset(
  839. page_start_instructions, function_offset_table_indices,
  840. /* instruction_offset */ (page_instruction_offset << 1) +
  841. (page_number << 17));
  842. ASSERT_NE(absl::nullopt, entry_found);
  843. EXPECT_EQ(0x10004, entry_found->instruction_offset_from_function_start);
  844. EXPECT_EQ(20ul, entry_found->function_offset_table_byte_index);
  845. }
  846. {
  847. const uint32_t page_number = 2;
  848. const uint32_t page_instruction_offset = 4;
  849. const auto entry_found = GetFunctionTableIndexFromInstructionOffset(
  850. page_start_instructions, function_offset_table_indices,
  851. /* instruction_offset */ (page_instruction_offset << 1) +
  852. (page_number << 17));
  853. ASSERT_NE(absl::nullopt, entry_found);
  854. EXPECT_EQ(0x20004, entry_found->instruction_offset_from_function_start);
  855. EXPECT_EQ(20ul, entry_found->function_offset_table_byte_index);
  856. }
  857. {
  858. const uint32_t page_number = 3;
  859. const uint32_t page_instruction_offset = 4;
  860. const auto entry_found = GetFunctionTableIndexFromInstructionOffset(
  861. page_start_instructions, function_offset_table_indices,
  862. /* instruction_offset */ (page_instruction_offset << 1) +
  863. (page_number << 17));
  864. ASSERT_NE(absl::nullopt, entry_found);
  865. EXPECT_EQ(0x30004, entry_found->instruction_offset_from_function_start);
  866. EXPECT_EQ(20ul, entry_found->function_offset_table_byte_index);
  867. }
  868. }
  869. // Utility function to add a single native module during test setup. Returns
  870. // a pointer to the provided module.
  871. const ModuleCache::Module* AddNativeModule(
  872. ModuleCache* cache,
  873. std::unique_ptr<const ModuleCache::Module> module) {
  874. const ModuleCache::Module* module_ptr = module.get();
  875. cache->AddCustomNativeModule(std::move(module));
  876. return module_ptr;
  877. }
  878. TEST(ChromeUnwinderAndroidV2Test, CanUnwindFrom) {
  879. const uint32_t page_table[] = {0};
  880. const FunctionTableEntry function_table[] = {{0, 0}};
  881. const uint8_t function_offset_table[] = {0};
  882. const uint8_t unwind_instruction_table[] = {0};
  883. auto dummy_unwind_info = ChromeUnwindInfoAndroid{
  884. make_span(unwind_instruction_table, 1ul),
  885. make_span(function_offset_table, 1ul),
  886. make_span(function_table, 1ul),
  887. make_span(page_table, 1ul),
  888. };
  889. auto chrome_module = std::make_unique<TestModule>(0x1000, 0x500);
  890. auto non_chrome_module = std::make_unique<TestModule>(0x2000, 0x500);
  891. ModuleCache module_cache;
  892. ChromeUnwinderAndroidV2 unwinder(dummy_unwind_info,
  893. chrome_module->GetBaseAddress(),
  894. /* text_section_start_address */
  895. chrome_module->GetBaseAddress() + 4);
  896. unwinder.Initialize(&module_cache);
  897. EXPECT_TRUE(unwinder.CanUnwindFrom({0x1100, chrome_module.get()}));
  898. EXPECT_TRUE(unwinder.CanUnwindFrom({0x1000, chrome_module.get()}));
  899. EXPECT_FALSE(unwinder.CanUnwindFrom({0x2100, non_chrome_module.get()}));
  900. EXPECT_FALSE(unwinder.CanUnwindFrom({0x400, nullptr}));
  901. }
  902. namespace {
  903. void ExpectFramesEq(const std::vector<Frame>& expected,
  904. const std::vector<Frame>& actual) {
  905. EXPECT_EQ(actual.size(), expected.size());
  906. if (actual.size() != expected.size())
  907. return;
  908. for (size_t i = 0; i < actual.size(); i++) {
  909. EXPECT_EQ(expected[i].module, actual[i].module);
  910. EXPECT_EQ(expected[i].instruction_pointer, actual[i].instruction_pointer);
  911. }
  912. }
  913. class AlignedStackMemory {
  914. public:
  915. AlignedStackMemory(std::initializer_list<uintptr_t> values)
  916. : size_(values.size()),
  917. stack_memory_(static_cast<uintptr_t*>(
  918. AlignedAlloc(size_ * sizeof(uintptr_t), 2 * sizeof(uintptr_t)))) {
  919. DCHECK_EQ(size_ % 2, 0u);
  920. std::copy(values.begin(), values.end(), stack_memory_.get());
  921. }
  922. uintptr_t stack_start_address() const {
  923. return reinterpret_cast<uintptr_t>(stack_memory_.get());
  924. }
  925. uintptr_t stack_end_address() const {
  926. return reinterpret_cast<uintptr_t>(stack_memory_.get() + size_);
  927. }
  928. private:
  929. const uintptr_t size_;
  930. const std::unique_ptr<uintptr_t, AlignedFreeDeleter> stack_memory_;
  931. };
  932. } // namespace
  933. TEST(ChromeUnwinderAndroidV2Test, TryUnwind) {
  934. const uint32_t page_table[] = {0, 2};
  935. const size_t number_of_pages = std::size(page_table);
  936. const size_t page_size = 1 << 17;
  937. const FunctionTableEntry function_table[] = {
  938. // Page 0.
  939. {0, 0}, // Function 0.
  940. {0x10, 4}, // Function 1. The function to unwind 2 times.
  941. // Page 1.
  942. {0x5, 8}, // Function 2.
  943. {0x20, 12}, // Function 3.
  944. };
  945. const uint8_t function_offset_table[] = {
  946. // Function 0.
  947. 0x2,
  948. 0,
  949. 0x0,
  950. 1,
  951. // Function 1.
  952. 0x7f,
  953. 0,
  954. 0x0,
  955. 1,
  956. // Function 2.
  957. 0x78,
  958. 0,
  959. 0x0,
  960. 1,
  961. // Function 3.
  962. 0x2,
  963. 0,
  964. 0x0,
  965. 1,
  966. };
  967. const uint8_t unwind_instruction_table[] = {
  968. // Offset 0: Pop r4, r14 from stack top.
  969. // Need to pop 2 registers to keep SP aligned.
  970. 0b10101000,
  971. // Offset 1: COMPLETE.
  972. 0b10110000,
  973. };
  974. auto unwind_info = ChromeUnwindInfoAndroid{
  975. make_span(unwind_instruction_table, std::size(unwind_instruction_table)),
  976. make_span(function_offset_table, std::size(function_offset_table)),
  977. make_span(function_table, std::size(function_table)),
  978. make_span(page_table, std::size(page_table)),
  979. };
  980. ModuleCache module_cache;
  981. const ModuleCache::Module* chrome_module = AddNativeModule(
  982. &module_cache, std::make_unique<TestModule>(
  983. 0x1000, number_of_pages * page_size, "ChromeModule"));
  984. uintptr_t text_section_start_address = 0x1100;
  985. ChromeUnwinderAndroidV2 unwinder(unwind_info, chrome_module->GetBaseAddress(),
  986. text_section_start_address);
  987. unwinder.Initialize(&module_cache);
  988. // Both first_pc and second_pc lie in Function 1's address range.
  989. uintptr_t first_pc = text_section_start_address + 0x20;
  990. uintptr_t second_pc = text_section_start_address + page_size + 0x4;
  991. // third_pc lies outside chrome_module's address range.
  992. uintptr_t third_pc = text_section_start_address + 3 * page_size;
  993. AlignedStackMemory stack_memory = {
  994. 0x0,
  995. third_pc,
  996. 0xFFFF,
  997. 0xFFFF,
  998. };
  999. std::vector<Frame> unwound_frames = {{first_pc, chrome_module}};
  1000. RegisterContext context;
  1001. RegisterContextInstructionPointer(&context) = first_pc;
  1002. RegisterContextStackPointer(&context) = stack_memory.stack_start_address();
  1003. context.arm_lr = second_pc;
  1004. EXPECT_EQ(UnwindResult::kUnrecognizedFrame,
  1005. unwinder.TryUnwind(&context, stack_memory.stack_end_address(),
  1006. &unwound_frames));
  1007. ExpectFramesEq(std::vector<Frame>({{first_pc, chrome_module},
  1008. {second_pc, chrome_module},
  1009. {third_pc, nullptr}}),
  1010. unwound_frames);
  1011. }
  1012. TEST(ChromeUnwinderAndroidV2Test, TryUnwindInfiniteLoopSingleFrame) {
  1013. const uint32_t page_table[] = {0, 2};
  1014. const size_t number_of_pages = std::size(page_table);
  1015. const size_t page_size = 1 << 17;
  1016. const FunctionTableEntry function_table[] = {
  1017. // Page 0.
  1018. {0x0, 0}, // Refuse to unwind filler function.
  1019. {0x10, 2}, // Function 0. The function to unwind.
  1020. // Page 1.
  1021. {0x5, 0}, // Refuse to unwind filler function.
  1022. };
  1023. const uint8_t function_offset_table[] = {
  1024. // Refuse to unwind filler function.
  1025. 0x0,
  1026. 0,
  1027. // Function 0.
  1028. 0x0,
  1029. 2,
  1030. };
  1031. const uint8_t unwind_instruction_table[] = {
  1032. // Offset 0: REFUSE_TO_UNWIND.
  1033. 0b10000000,
  1034. 0b00000000,
  1035. // Offset 2: COMPLETE.
  1036. 0b10110000,
  1037. };
  1038. auto unwind_info = ChromeUnwindInfoAndroid{
  1039. make_span(unwind_instruction_table, std::size(unwind_instruction_table)),
  1040. make_span(function_offset_table, std::size(function_offset_table)),
  1041. make_span(function_table, std::size(function_table)),
  1042. make_span(page_table, std::size(page_table)),
  1043. };
  1044. ModuleCache module_cache;
  1045. const ModuleCache::Module* chrome_module = AddNativeModule(
  1046. &module_cache, std::make_unique<TestModule>(
  1047. 0x1000, number_of_pages * page_size, "ChromeModule"));
  1048. uintptr_t text_section_start_address = 0x1100;
  1049. ChromeUnwinderAndroidV2 unwinder(unwind_info, chrome_module->GetBaseAddress(),
  1050. text_section_start_address);
  1051. unwinder.Initialize(&module_cache);
  1052. uintptr_t pc = text_section_start_address + 0x20;
  1053. AlignedStackMemory stack_memory = {
  1054. 0xFFFF,
  1055. 0xFFFF,
  1056. };
  1057. std::vector<Frame> unwound_frames = {{pc, chrome_module}};
  1058. RegisterContext context;
  1059. RegisterContextInstructionPointer(&context) = pc;
  1060. RegisterContextStackPointer(&context) = stack_memory.stack_start_address();
  1061. // Set lr = pc so that both sp and pc stays the same after first round of
  1062. // unwind.
  1063. context.arm_lr = pc;
  1064. EXPECT_EQ(UnwindResult::kAborted,
  1065. unwinder.TryUnwind(&context, stack_memory.stack_end_address(),
  1066. &unwound_frames));
  1067. ExpectFramesEq(std::vector<Frame>({{pc, chrome_module}}), unwound_frames);
  1068. }
  1069. TEST(ChromeUnwinderAndroidV2Test, TryUnwindInfiniteLoopMultipleFrames) {
  1070. // This test aims to produce a scenario, where after the unwind of a number
  1071. // of frames, the sp and pc get to their original state before the unwind.
  1072. // Function 1 (pc1, sp1):
  1073. // - set pc = lr(pc2)
  1074. // Function 2 (pc2, sp1):
  1075. // - pop r14(pc2), r15(pc1) off stack
  1076. // - vsp = r4 (reset vsp to frame initial vsp)
  1077. const uint32_t page_table[] = {0, 3};
  1078. const size_t number_of_pages = std::size(page_table);
  1079. const size_t page_size = 1 << 17;
  1080. const FunctionTableEntry function_table[] = {
  1081. // Page 0.
  1082. {0x0, 0}, // Refuse to unwind filler function.
  1083. {0x10, 2}, // Function 1. The function to unwind.
  1084. {0x100, 2}, // Function 2. The function to unwind.
  1085. // Page 1.
  1086. {0x5, 0}, // Refuse to unwind filler function.
  1087. };
  1088. const uint8_t function_offset_table[] = {
  1089. // Refuse to unwind filler function.
  1090. 0x0,
  1091. 0,
  1092. // Function 0.
  1093. 0x0,
  1094. 2,
  1095. // Function 1.
  1096. 0x2,
  1097. 3,
  1098. 0x1,
  1099. 5,
  1100. 0x0,
  1101. 6,
  1102. };
  1103. const uint8_t unwind_instruction_table[] = {
  1104. // Offset 0: REFUSE_TO_UNWIND.
  1105. 0b10000000,
  1106. 0b00000000,
  1107. // Offset 2: COMPLETE.
  1108. 0b10110000,
  1109. // Offset 3: POP r14, r15 off the stack.
  1110. 0b10001100,
  1111. 0b00000000,
  1112. // Offset 5: vsp = r4.
  1113. 0b10010100,
  1114. // Offset 6: COMPLETE.
  1115. 0b10110000,
  1116. };
  1117. auto unwind_info = ChromeUnwindInfoAndroid{
  1118. make_span(unwind_instruction_table, std::size(unwind_instruction_table)),
  1119. make_span(function_offset_table, std::size(function_offset_table)),
  1120. make_span(function_table, std::size(function_table)),
  1121. make_span(page_table, std::size(page_table)),
  1122. };
  1123. ModuleCache module_cache;
  1124. const ModuleCache::Module* chrome_module = AddNativeModule(
  1125. &module_cache, std::make_unique<TestModule>(
  1126. 0x1000, number_of_pages * page_size, "ChromeModule"));
  1127. uintptr_t text_section_start_address = 0x1100;
  1128. ChromeUnwinderAndroidV2 unwinder(unwind_info, chrome_module->GetBaseAddress(),
  1129. text_section_start_address);
  1130. unwinder.Initialize(&module_cache);
  1131. uintptr_t first_pc = text_section_start_address + 0x20; // Function 1.
  1132. uintptr_t second_pc = text_section_start_address + 0x110; // Function 2.
  1133. AlignedStackMemory stack_memory = {
  1134. second_pc,
  1135. first_pc,
  1136. 0xFFFF,
  1137. 0xFFFF,
  1138. };
  1139. std::vector<Frame> unwound_frames = {{first_pc, chrome_module}};
  1140. RegisterContext context;
  1141. RegisterContextInstructionPointer(&context) = first_pc;
  1142. RegisterContextStackPointer(&context) = stack_memory.stack_start_address();
  1143. context.arm_lr = second_pc;
  1144. context.arm_r4 = stack_memory.stack_start_address();
  1145. EXPECT_EQ(UnwindResult::kAborted,
  1146. unwinder.TryUnwind(&context, stack_memory.stack_end_address(),
  1147. &unwound_frames));
  1148. ExpectFramesEq(std::vector<Frame>(
  1149. {{first_pc, chrome_module}, {second_pc, chrome_module}}),
  1150. unwound_frames);
  1151. }
  1152. TEST(ChromeUnwinderAndroidV2Test, TryUnwindUnalignedSPFrameUnwind) {
  1153. // SP should be 2-uintptr_t aligned before/after each frame unwind.
  1154. const uint32_t page_table[] = {0, 2};
  1155. const size_t number_of_pages = std::size(page_table);
  1156. const size_t page_size = 1 << 17;
  1157. const FunctionTableEntry function_table[] = {
  1158. // Page 0.
  1159. {0x0, 0}, // Refuse to unwind filler function.
  1160. {0x10, 2}, // Function 0. The function to unwind.
  1161. // Page 1.
  1162. {0x5, 0}, // Refuse to unwind filler function.
  1163. };
  1164. const uint8_t function_offset_table[] = {
  1165. // Refuse to unwind filler function.
  1166. 0x0,
  1167. 0,
  1168. // Function 0.
  1169. 0x0,
  1170. 2,
  1171. };
  1172. const uint8_t unwind_instruction_table[] = {
  1173. // Offset 0: REFUSE_TO_UNWIND.
  1174. 0b10000000,
  1175. 0b00000000,
  1176. // Offset 2: COMPLETE.
  1177. 0b10110000,
  1178. };
  1179. auto unwind_info = ChromeUnwindInfoAndroid{
  1180. make_span(unwind_instruction_table, std::size(unwind_instruction_table)),
  1181. make_span(function_offset_table, std::size(function_offset_table)),
  1182. make_span(function_table, std::size(function_table)),
  1183. make_span(page_table, std::size(page_table)),
  1184. };
  1185. ModuleCache module_cache;
  1186. const ModuleCache::Module* chrome_module = AddNativeModule(
  1187. &module_cache, std::make_unique<TestModule>(
  1188. 0x1000, number_of_pages * page_size, "ChromeModule"));
  1189. uintptr_t text_section_start_address = 0x1100;
  1190. ChromeUnwinderAndroidV2 unwinder(unwind_info, chrome_module->GetBaseAddress(),
  1191. text_section_start_address);
  1192. unwinder.Initialize(&module_cache);
  1193. uintptr_t pc = text_section_start_address + 0x20;
  1194. AlignedStackMemory stack_memory = {
  1195. 0xFFFF,
  1196. 0xFFFF,
  1197. };
  1198. std::vector<Frame> unwound_frames = {{pc, chrome_module}};
  1199. RegisterContext context;
  1200. RegisterContextInstructionPointer(&context) = pc;
  1201. // Make stack memory not aligned to 2 * sizeof(uintptr_t);
  1202. RegisterContextStackPointer(&context) =
  1203. stack_memory.stack_start_address() + sizeof(uintptr_t);
  1204. // The address is outside chrome module, which will result the unwind to
  1205. // stop with result kUnrecognizedFrame if SP alignment issue was not detected.
  1206. context.arm_lr =
  1207. text_section_start_address + (number_of_pages + 1) * page_size;
  1208. EXPECT_EQ(UnwindResult::kAborted,
  1209. unwinder.TryUnwind(&context, stack_memory.stack_end_address(),
  1210. &unwound_frames));
  1211. ExpectFramesEq(std::vector<Frame>({{pc, chrome_module}}), unwound_frames);
  1212. }
  1213. TEST(ChromeUnwinderAndroidV2Test, TryUnwindUnalignedSPInstructionUnwind) {
  1214. // SP should be uintptr_t aligned before/after each unwind instruction
  1215. // execution.
  1216. const uint32_t page_table[] = {0, 2};
  1217. const size_t number_of_pages = std::size(page_table);
  1218. const size_t page_size = 1 << 17;
  1219. const FunctionTableEntry function_table[] = {
  1220. // Page 0.
  1221. {0x0, 0}, // Refuse to unwind filler function.
  1222. {0x10, 2}, // Function 0. The function to unwind.
  1223. // Page 1.
  1224. {0x5, 0}, // Refuse to unwind filler function.
  1225. };
  1226. const uint8_t function_offset_table[] = {
  1227. // Refuse to unwind filler function.
  1228. 0x0,
  1229. 0,
  1230. // Function 0.
  1231. 0x0,
  1232. 2,
  1233. };
  1234. const uint8_t unwind_instruction_table[] = {
  1235. // Offset 0: REFUSE_TO_UNWIND.
  1236. 0b10000000, 0b00000000,
  1237. // Offset 2:
  1238. 0b10010100, // vsp = r4, where r4 = stack + (sizeof(uintptr_t) / 2)
  1239. 0b10110000, // COMPLETE.
  1240. };
  1241. auto unwind_info = ChromeUnwindInfoAndroid{
  1242. make_span(unwind_instruction_table, std::size(unwind_instruction_table)),
  1243. make_span(function_offset_table, std::size(function_offset_table)),
  1244. make_span(function_table, std::size(function_table)),
  1245. make_span(page_table, std::size(page_table)),
  1246. };
  1247. ModuleCache module_cache;
  1248. const ModuleCache::Module* chrome_module = AddNativeModule(
  1249. &module_cache, std::make_unique<TestModule>(
  1250. 0x1000, number_of_pages * page_size, "ChromeModule"));
  1251. uintptr_t text_section_start_address = 0x1100;
  1252. ChromeUnwinderAndroidV2 unwinder(unwind_info, chrome_module->GetBaseAddress(),
  1253. text_section_start_address);
  1254. unwinder.Initialize(&module_cache);
  1255. uintptr_t pc = text_section_start_address + 0x20;
  1256. AlignedStackMemory stack_memory = {
  1257. 0xFFFF,
  1258. 0xFFFF,
  1259. };
  1260. std::vector<Frame> unwound_frames = {{pc, chrome_module}};
  1261. RegisterContext context;
  1262. RegisterContextInstructionPointer(&context) = pc;
  1263. RegisterContextStackPointer(&context) = stack_memory.stack_start_address();
  1264. // The address is outside chrome module, which will result the unwind to
  1265. // stop with result kUnrecognizedFrame if SP alignment issue was not detected.
  1266. context.arm_lr =
  1267. text_section_start_address + (number_of_pages + 1) * page_size;
  1268. context.arm_r4 = stack_memory.stack_start_address() + sizeof(uintptr_t) / 2;
  1269. EXPECT_EQ(UnwindResult::kAborted,
  1270. unwinder.TryUnwind(&context, stack_memory.stack_end_address(),
  1271. &unwound_frames));
  1272. ExpectFramesEq(std::vector<Frame>({{pc, chrome_module}}), unwound_frames);
  1273. }
  1274. TEST(ChromeUnwinderAndroidV2Test, TryUnwindSPOverflow) {
  1275. const uint32_t page_table[] = {0, 2};
  1276. const size_t number_of_pages = std::size(page_table);
  1277. const size_t page_size = 1 << 17;
  1278. const FunctionTableEntry function_table[] = {
  1279. // Page 0.
  1280. {0x0, 0}, // Refuse to unwind filler function.
  1281. {0x10, 2}, // Function 0. The function to unwind.
  1282. // Page 1.
  1283. {0x5, 0}, // Refuse to unwind filler function.
  1284. };
  1285. const uint8_t function_offset_table[] = {
  1286. // Refuse to unwind filler function.
  1287. 0x0,
  1288. 0,
  1289. // Function 0.
  1290. 0x0,
  1291. 2,
  1292. };
  1293. const uint8_t unwind_instruction_table[] = {
  1294. // Offset 0: REFUSE_TO_UNWIND.
  1295. 0b10000000, 0b00000000,
  1296. // Offset 2.
  1297. 0b10010100, // vsp = r4.
  1298. 0b10101000, // Pop r4, r14.
  1299. 0b10110000, // COMPLETE.
  1300. };
  1301. auto unwind_info = ChromeUnwindInfoAndroid{
  1302. make_span(unwind_instruction_table, std::size(unwind_instruction_table)),
  1303. make_span(function_offset_table, std::size(function_offset_table)),
  1304. make_span(function_table, std::size(function_table)),
  1305. make_span(page_table, std::size(page_table)),
  1306. };
  1307. ModuleCache module_cache;
  1308. const ModuleCache::Module* chrome_module = AddNativeModule(
  1309. &module_cache, std::make_unique<TestModule>(
  1310. 0x1000, number_of_pages * page_size, "ChromeModule"));
  1311. uintptr_t text_section_start_address = 0x1100;
  1312. ChromeUnwinderAndroidV2 unwinder(unwind_info, chrome_module->GetBaseAddress(),
  1313. text_section_start_address);
  1314. unwinder.Initialize(&module_cache);
  1315. uintptr_t pc = text_section_start_address + 0x20;
  1316. AlignedStackMemory stack_memory = {
  1317. 0xFFFF,
  1318. 0xFFFF,
  1319. };
  1320. std::vector<Frame> unwound_frames = {{pc, chrome_module}};
  1321. RegisterContext context;
  1322. RegisterContextInstructionPointer(&context) = pc;
  1323. RegisterContextStackPointer(&context) = stack_memory.stack_start_address();
  1324. // Setting vsp = 0xffffffff should cause SP overflow.
  1325. context.arm_r4 = 0xffffffff;
  1326. // The address is outside chrome module, which will result the unwind to
  1327. // stop with result kUnrecognizedFrame if the unwinder did not abort for other
  1328. // reasons.
  1329. context.arm_lr =
  1330. text_section_start_address + (number_of_pages + 1) * page_size;
  1331. EXPECT_EQ(UnwindResult::kAborted,
  1332. unwinder.TryUnwind(&context, stack_memory.stack_end_address(),
  1333. &unwound_frames));
  1334. ExpectFramesEq(std::vector<Frame>({{pc, chrome_module}}), unwound_frames);
  1335. }
  1336. TEST(ChromeUnwinderAndroidV2Test, TryUnwindNullSP) {
  1337. const uint32_t page_table[] = {0, 2};
  1338. const size_t number_of_pages = std::size(page_table);
  1339. const size_t page_size = 1 << 17;
  1340. const FunctionTableEntry function_table[] = {
  1341. // Page 0.
  1342. {0x0, 0}, // Refuse to unwind filler function.
  1343. {0x10, 2}, // Function 0. The function to unwind.
  1344. // Page 1.
  1345. {0x5, 0}, // Refuse to unwind filler function.
  1346. };
  1347. const uint8_t function_offset_table[] = {
  1348. // Refuse to unwind filler function.
  1349. 0x0,
  1350. 0,
  1351. // Function 0.
  1352. 0x0,
  1353. 2,
  1354. };
  1355. const uint8_t unwind_instruction_table[] = {
  1356. // Offset 0: REFUSE_TO_UNWIND.
  1357. 0b10000000, 0b00000000,
  1358. // Offset 2.
  1359. 0b10010100, // vsp = r4.
  1360. 0b10101000, // Pop r4, r14.
  1361. 0b10110000, // COMPLETE.
  1362. };
  1363. auto unwind_info = ChromeUnwindInfoAndroid{
  1364. make_span(unwind_instruction_table, std::size(unwind_instruction_table)),
  1365. make_span(function_offset_table, std::size(function_offset_table)),
  1366. make_span(function_table, std::size(function_table)),
  1367. make_span(page_table, std::size(page_table)),
  1368. };
  1369. ModuleCache module_cache;
  1370. const ModuleCache::Module* chrome_module = AddNativeModule(
  1371. &module_cache, std::make_unique<TestModule>(
  1372. 0x1000, number_of_pages * page_size, "ChromeModule"));
  1373. uintptr_t text_section_start_address = 0x1100;
  1374. ChromeUnwinderAndroidV2 unwinder(unwind_info, chrome_module->GetBaseAddress(),
  1375. text_section_start_address);
  1376. unwinder.Initialize(&module_cache);
  1377. uintptr_t pc = text_section_start_address + 0x20;
  1378. AlignedStackMemory stack_memory = {
  1379. 0xFFFF,
  1380. 0xFFFF,
  1381. };
  1382. std::vector<Frame> unwound_frames = {{pc, chrome_module}};
  1383. RegisterContext context;
  1384. RegisterContextInstructionPointer(&context) = pc;
  1385. RegisterContextStackPointer(&context) = stack_memory.stack_start_address();
  1386. // Setting vsp = 0x0 should cause the unwinder to abort.
  1387. context.arm_r4 = 0x0;
  1388. // The address is outside chrome module, which will result the unwind to
  1389. // stop with result kUnrecognizedFrame if the unwinder did not abort for other
  1390. // reasons.
  1391. context.arm_lr =
  1392. text_section_start_address + (number_of_pages + 1) * page_size;
  1393. EXPECT_EQ(UnwindResult::kAborted,
  1394. unwinder.TryUnwind(&context, stack_memory.stack_end_address(),
  1395. &unwound_frames));
  1396. ExpectFramesEq(std::vector<Frame>({{pc, chrome_module}}), unwound_frames);
  1397. }
  1398. TEST(ChromeUnwinderAndroidV2Test, TryUnwindInvalidSPOperation) {
  1399. // This test aims to verify that for each unwind instruction executed, it is
  1400. // always true that sp > frame initial sp.
  1401. const uint32_t page_table[] = {0, 2};
  1402. const size_t number_of_pages = std::size(page_table);
  1403. const size_t page_size = 1 << 17;
  1404. const FunctionTableEntry function_table[] = {
  1405. // Page 0.
  1406. {0x0, 0}, // Refuse to unwind filler function.
  1407. {0x10, 2}, // Function 0. The function to unwind.
  1408. // Page 1.
  1409. {0x5, 0}, // Refuse to unwind filler function.
  1410. };
  1411. const uint8_t function_offset_table[] = {
  1412. // Refuse to unwind filler function.
  1413. 0x0,
  1414. 0,
  1415. // Function 0.
  1416. 0x0,
  1417. 2,
  1418. };
  1419. const uint8_t unwind_instruction_table[] = {
  1420. // Offset 0: REFUSE_TO_UNWIND.
  1421. 0b10000000, 0b00000000,
  1422. // Offset 2.
  1423. 0b10010100, // vsp = r4 (r4 < frame initial sp).
  1424. 0b10010101, // vsp = r5 (r5 > frame initial sp).
  1425. 0b10110000, // COMPLETE.
  1426. };
  1427. auto unwind_info = ChromeUnwindInfoAndroid{
  1428. make_span(unwind_instruction_table, std::size(unwind_instruction_table)),
  1429. make_span(function_offset_table, std::size(function_offset_table)),
  1430. make_span(function_table, std::size(function_table)),
  1431. make_span(page_table, std::size(page_table)),
  1432. };
  1433. ModuleCache module_cache;
  1434. const ModuleCache::Module* chrome_module = AddNativeModule(
  1435. &module_cache, std::make_unique<TestModule>(
  1436. 0x1000, number_of_pages * page_size, "ChromeModule"));
  1437. uintptr_t text_section_start_address = 0x1100;
  1438. ChromeUnwinderAndroidV2 unwinder(unwind_info, chrome_module->GetBaseAddress(),
  1439. text_section_start_address);
  1440. unwinder.Initialize(&module_cache);
  1441. uintptr_t pc = text_section_start_address + 0x20;
  1442. AlignedStackMemory stack_memory = {
  1443. 0xFFFF,
  1444. 0xFFFF,
  1445. };
  1446. std::vector<Frame> unwound_frames = {{pc, chrome_module}};
  1447. RegisterContext context;
  1448. RegisterContextInstructionPointer(&context) = pc;
  1449. RegisterContextStackPointer(&context) = stack_memory.stack_start_address();
  1450. context.arm_r4 = stack_memory.stack_start_address() - 2 * sizeof(uintptr_t);
  1451. context.arm_r5 = stack_memory.stack_start_address() + 2 * sizeof(uintptr_t);
  1452. // The address is outside chrome module, which will result the unwind to
  1453. // stop with result kUnrecognizedFrame if the unwinder did not abort for other
  1454. // reasons.
  1455. context.arm_lr =
  1456. text_section_start_address + (number_of_pages + 1) * page_size;
  1457. EXPECT_EQ(UnwindResult::kAborted,
  1458. unwinder.TryUnwind(&context, stack_memory.stack_end_address(),
  1459. &unwound_frames));
  1460. ExpectFramesEq(std::vector<Frame>({{pc, chrome_module}}), unwound_frames);
  1461. }
  1462. } // namespace base