codescan.inc 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. // Copyright (C) 2016 Y_Less
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the "Software"),
  5. // to deal in the Software without restriction, including without limitation
  6. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  7. // and/or sell copies of the Software, and to permit persons to whom the
  8. // Software is furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  14. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  19. // DEALINGS IN THE SOFTWARE.
  20. #if defined CODESCAN_INC
  21. #endinput
  22. #endif
  23. #define CODESCAN_INC
  24. /*
  25. // Example:
  26. forward TailCall_FoundCallback(m[CodeScanner])
  27. main() {
  28. new scanner[CodeScanner];
  29. CodeScanInit(scanner);
  30. new csm0[CodeScanMatcher];
  31. CodeScanMatcherInit(csm0, &TailCall_FoundCallback);
  32. CodeScanMatcherPattern(csm0,
  33. OP(PUSH_C, ???)
  34. OP(CALL, &MyFunc)
  35. OP(RETN)
  36. );
  37. CodeScanAddMatcher(scanner, csm0);
  38. // Add other matcher patterns here.
  39. // Run all the scanners in parallel.
  40. CodeScanRun(scanner);
  41. }
  42. public TailCall_FoundCallback(m[CodeScanner]) {
  43. // Do something with the found address (of the START of the match), and the
  44. // stack size (of the END of the match) - different for reasons...
  45. }
  46. // Create a default call for this function, so that we can include it in the AMX
  47. // and take the address in "OP". Note that you do NOT need to do this for
  48. // scanner callbacks if you only use their address in "CodeScanMatcherInit".
  49. #define CALL@MyFunc MyFunc(0, "")
  50. stock MyFunc(a, b[], ...) {
  51. // Normal function.
  52. }
  53. */
  54. #include <core>
  55. #include "frame_info"
  56. #include "disasm"
  57. #include "asm"
  58. #include "addressof"
  59. #define SCANNER_FAIL_ON_INVALID (1)
  60. #define SCANNER_IGNORE_NOP (2)
  61. #define SCANNER_IGNORE_BREAK (4)
  62. #define SCANNER_NAME_FUNCTIONS (8)
  63. #define SCANNER_IGNORE_HALT (16)
  64. #define SCANNER_IGNORE_BOUNDS (32)
  65. #define SCANNER_HAS_USER_DATA (64)
  66. #define O@I_ (0) // Type integer.
  67. #define O@U_ (1) // Type unknown (???).
  68. #define O@F_ (2) // Type function (&func).
  69. #define O@O_ (4) // Type opcode.
  70. #define O@S_ (5) // Type skipped.
  71. #define OP_TYPE_INTEGER_ (O@I_) // Type integer.
  72. #define OP_TYPE_UNKNOWN_ (O@U_) // Type unknown (???).
  73. #define OP_TYPE_FUNCTION_ (O@F_) // Type function (&func).
  74. #define OP_TYPE_OPCODE_ (O@O_) // Type opcode.
  75. #define OP_TYPE_SKIP_ (O@S_) // Type skipped.
  76. // If we can determine a function's name, we can determine if it is a public or
  77. // not. If we can't name it, it is a normal one. However, if naming is skipped
  78. // then we will have no idea what type it is.
  79. #define SCANNER_FUNC_PUBLIC (1)
  80. #define SCANNER_FUNC_OTHER (2)
  81. #define SCANNER_FUNC_AUTOMATA (3)
  82. #define SCANNER_FUNC_HALT (4)
  83. #define SCANNER_FUNC_UNKNOWN (5)
  84. #define SCANNER_FUNC_AUTOMATA_NO_NAME (7)
  85. #define SCANNER_FUNC_HALT_NO_NAME (8)
  86. // The "OP()" macro is used to easilly define code patterns to scan for:
  87. //
  88. // new csm[CodeScanMatcher];
  89. // CodeScanMatcherInit(csm, &callback);
  90. // CodeScanMatcherPattern(csm,
  91. // OP(CONST_PRI, 42)
  92. // OP(ADD_C, ???)
  93. // OP(CALL, &my_func)
  94. // )
  95. //
  96. // Any function that you want to take the address of in this way must have its
  97. // call pattern defined as:
  98. //
  99. // #define CALL@my_func my_func(0, "hi", false)
  100. //
  101. // Because otherwise a) the code can't guarantee that the function will be in
  102. // the final amx, and b) we need a call to it from which to extract the addr.
  103. //
  104. // You can use this style explcitly within an "OP" scanner, or there is a new
  105. // dedicated keyword for it - "addressof(func)" (note the lack of "&" there).
  106. //
  107. #define OP(%0) ,(_:O@T_:O@O_),(Opcode:O@X_:O@Y_:O@W_:$OP_%0)
  108. #define OP_%0\32;%1) OP_%0%1)
  109. #define O@X_:%9$%0,%1,%2) %0),(_:O@1_:O@2_:O@3_:$%1|||,%2)
  110. #define O@Y_:%9$%0,%1) %0),(_:O@1_:O@2_:O@3_:$%1|||)
  111. #define O@Z_:%9$%0) %0)
  112. #define O@W_:%9$%0) %0)
  113. #define O@T_:O@O_),(Opcode:O@X_:O@Y_:O@W_:$OP_???%0) O@S_),(0)
  114. #define O@1_:%9$%0???%1|||%2) O@U_ ),(_:O@X_:O@Y_:O@Z_:$0%2)
  115. #define O@2_:%9$%0&%1|||%2) O@F_),(O@A_()?(((CALL@%1),O@V_)?1:2):_:O@X_:O@Y_:O@Z_:$(O@V_)%2)
  116. #define O@3_:%9$%1|||%2) O@I_ ),(_:O@X_:O@Y_:O@Z_:$(%1)%2)
  117. #if !defined cellbytes
  118. #define cellbytes (cellbits / 8)
  119. #endif
  120. #if !defined CODE_SCAN_MAX_PATTERN
  121. #define CODE_SCAN_MAX_PATTERN (16)
  122. #endif
  123. #define CODE_SCAN_MAX_PATTERN_ARRAY (CODE_SCAN_MAX_PATTERN * 4)
  124. #define CODE_SCAN_MAX_HOLES (CODE_SCAN_MAX_PATTERN / 2)
  125. #if !defined CODE_SCAN_MAX_PARALLEL
  126. #define CODE_SCAN_MAX_PARALLEL (2)
  127. #endif
  128. #if !defined CODE_SCAN_MAX_JUMP_TARGETS
  129. #define CODE_SCAN_MAX_JUMP_TARGETS (32)
  130. #endif
  131. // All the information for scanning through an AMX and extracting lots of nice
  132. // information about it.
  133. enum CodeScanner {
  134. CodeScanMatch_func, // Start of the containing function.
  135. CodeScanMatch_size, // Size of the match.
  136. CodeScanMatch_type, // Public, normal, automata, etc.
  137. CodeScanMatch_heap, // At the point of this scanner.
  138. CodeScanMatch_stack, // At the point of this scanner.
  139. CodeScanMatch_params, // Likely unknown statically.
  140. CodeScanMatch_cip, // The point of the pattern match.
  141. CodeScanMatch_holes[CODE_SCAN_MAX_HOLES], // Results of "???"s.
  142. CodeScanMatch_hole_count, // How many holes were seen.
  143. CodeScanMatch_name[32 char],
  144. CodeScanner_first,
  145. CodeScanner_minn,
  146. CodeScanner_jump_switch[CODE_SCAN_MAX_JUMP_TARGETS], // For "CASETBL" not regular jumps.
  147. CodeScanner_jump_target[CODE_SCAN_MAX_JUMP_TARGETS], // Zero when this slot is available.
  148. CodeScanner_jump_stack [CODE_SCAN_MAX_JUMP_TARGETS], // Sizes at the time of the jump.
  149. CodeScanner_jump_heap [CODE_SCAN_MAX_JUMP_TARGETS], // Sizes at the time of the jump.
  150. CodeScanner_state,
  151. CodeScanner_param
  152. }
  153. enum CodeScanMatcher {
  154. CodeScanMatcher_func, // A pointer to the callback.
  155. CodeScanMatcher_user_data, // User data to pass to their callback.
  156. CodeScanMatcher_code[CODE_SCAN_MAX_PATTERN_ARRAY], // The code to look for.
  157. CodeScanMatcher_len,
  158. CodeScanMatcher_offset[CODE_SCAN_MAX_PARALLEL], // Where the current scanner is in this code.
  159. CodeScanMatcher_start[CODE_SCAN_MAX_PARALLEL],
  160. CodeScanMatcher_holeidx[CODE_SCAN_MAX_PARALLEL],
  161. CodeScanMatcher_holes[CODE_SCAN_MAX_PARALLEL * CODE_SCAN_MAX_HOLES],
  162. CodeScanMatcher_next, // The next match array.
  163. CodeScanMatcher_flags // Customisation.
  164. }
  165. // This macro is to let anyone use `&callback` for a scanner callback without
  166. // having to define the `CALL@...` macro for the required parameters (since we
  167. // know to call scanner callbacks in this code).
  168. #define addressof_ScannerCallback_(%1) (O@A_()?(((%1((gCodeScanCallback_match))),O@V_)?1:2):(O@V_))
  169. stock
  170. gCodeScanCallback_match[CodeScanner];
  171. static stock
  172. gOP_NOP,
  173. gOP_CASETBL,
  174. gHdr[AMX_HDR],
  175. gBase,
  176. gCodBase,
  177. gDat;
  178. static stock const
  179. gOpArgCount[Opcode:NUM_OPCODES] = {
  180. 4, 8, 8, 8, 8, 8, 8, 8, 8, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, 8, 4, 8, 4, 8, 8, 8,
  181. 8, 8, 4, 4, 4, 4, 4, 8, 8, 8, 8, 4, 4, 8, 8, 4, 4, 4, 8, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  182. 8, 8, 8, 4, 4, 4, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 4, 4, 8, 8,
  183. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 4, 4, 8, 8, 4, 4, 4, 8, 8, 4, 8, 8, 8, 8, 8, 4, 8,
  184. -1, -1, -1, -1, 4, 8, -1, 4, 4, 8, 4, 8, 8, 4
  185. };
  186. static stock bool:CodeScanCheckJumpTarget(cip, deloc, &stk, &hea, jumpTargets[CodeScanner], num = CODE_SCAN_MAX_JUMP_TARGETS) {
  187. // Use "minn" to restrict the number of jump targets that we check. Returns
  188. // "true" if the current address is equal to an address that any jump goes
  189. // to.
  190. new
  191. minn = jumpTargets[CodeScanner_minn],
  192. sip;
  193. while (num-- > minn) {
  194. if (jumpTargets[CodeScanner_jump_target][num]) {
  195. if ((sip = jumpTargets[CodeScanner_jump_switch][num])) {
  196. for (new count = ReadAmxMemory(sip + cellbytes) + 1; count; --count) {
  197. if (ReadAmxMemory(sip + (count << 3)) == deloc) {
  198. return
  199. --jumpTargets[CodeScanner_jump_target][num],
  200. stk = jumpTargets[CodeScanner_jump_stack][num],
  201. hea = jumpTargets[CodeScanner_jump_heap][num],
  202. true;
  203. }
  204. }
  205. } else if (jumpTargets[CodeScanner_jump_target][num] == cip) {
  206. return
  207. jumpTargets[CodeScanner_jump_target][num] = 0,
  208. stk = jumpTargets[CodeScanner_jump_stack][num],
  209. hea = jumpTargets[CodeScanner_jump_heap][num],
  210. true;
  211. }
  212. }
  213. }
  214. return false;
  215. }
  216. static stock CodeScanResetJumpTargets(jumpTargets[CodeScanner], num = CODE_SCAN_MAX_JUMP_TARGETS) {
  217. jumpTargets[CodeScanner_minn] = num;
  218. while (num--) {
  219. jumpTargets[CodeScanner_jump_target][num] = 0;
  220. }
  221. }
  222. static stock CodeScanAddJumpTarget(cip, stk, hea, jumpTargets[CodeScanner], num = CODE_SCAN_MAX_JUMP_TARGETS) {
  223. while (num--) {
  224. // Multiple jumps to the same place?
  225. if (jumpTargets[CodeScanner_jump_target][num] == cip) {
  226. return;
  227. } else if (!jumpTargets[CodeScanner_jump_target][num]) {
  228. jumpTargets[CodeScanner_jump_switch][num] = 0;
  229. jumpTargets[CodeScanner_jump_target][num] = cip;
  230. jumpTargets[CodeScanner_jump_stack][num] = stk;
  231. jumpTargets[CodeScanner_jump_heap][num] = hea;
  232. jumpTargets[CodeScanner_minn] = min(jumpTargets[CodeScanner_minn], num);
  233. return;
  234. }
  235. }
  236. }
  237. static stock CodeScanAddSwitchTarget(const dctx[DisasmContext], stk, hea, jumpTargets[CodeScanner], num = CODE_SCAN_MAX_JUMP_TARGETS) {
  238. new
  239. sip = DisasmGetOperand(dctx) - gBase,
  240. codepos = sip + gHdr[AMX_HDR_DAT] - gHdr[AMX_HDR_COD];
  241. if (codepos < 0 || codepos > gHdr[AMX_HDR_DAT] || ReadAmxMemory(sip) != gOP_CASETBL) {
  242. // Can happen when we parse "RelocateOpcodeNow" because it has an
  243. // explicit "#emit switch 0" in.
  244. return;
  245. }
  246. while (num--) {
  247. // Multiple jumps to the same place?
  248. if (!jumpTargets[CodeScanner_jump_target][num]) {
  249. jumpTargets[CodeScanner_jump_switch][num] = sip,
  250. jumpTargets[CodeScanner_jump_target][num] = ReadAmxMemory(sip + cellbytes) + 1,
  251. jumpTargets[CodeScanner_jump_stack][num] = stk,
  252. jumpTargets[CodeScanner_jump_heap][num] = hea,
  253. jumpTargets[CodeScanner_minn] = min(jumpTargets[CodeScanner_minn], num);
  254. return;
  255. }
  256. }
  257. }
  258. static stock CodeScanReset(cs[CodeScanMatcher], &next) {
  259. static
  260. lReset[CODE_SCAN_MAX_PARALLEL];
  261. next = cs[CodeScanMatcher_next],
  262. cs[CodeScanMatcher_offset] = lReset,
  263. cs[CodeScanMatcher_holeidx] = lReset;
  264. if (!cs[CodeScanMatcher_func]) {
  265. cs[CodeScanMatcher_len] = 0;
  266. }
  267. }
  268. stock CodeScanAddMatcher(scanner[CodeScanner], searcher[CodeScanMatcher]) {
  269. searcher[CodeScanMatcher_next] = scanner[CodeScanner_first],
  270. scanner[CodeScanner_first] = ref(searcher);
  271. }
  272. stock CodeScanMatcherInit_(searcher[CodeScanMatcher], address, flags = SCANNER_IGNORE_NOP | SCANNER_IGNORE_BOUNDS | SCANNER_IGNORE_BREAK | SCANNER_IGNORE_HALT) {
  273. // This used to look the function up by name from the public functions
  274. // table, but that was very silly since we already had code in this exact
  275. // file to get the address of ANY function at run-time (`addressof`). The
  276. // only difference between the normal `addressof` and the one used here is
  277. // that because we know exactly what sort of functions we are expecting, we
  278. // know exactly what parameters they require to construct the fake call, so
  279. // we can do away with the `CALL@...` macro requirement for defining the
  280. // standard call pattern. This also means that we actually ensure that the
  281. // passed function has the correct shape.
  282. searcher[CodeScanMatcher_func] = address,
  283. searcher[CodeScanMatcher_flags] = flags & ~SCANNER_HAS_USER_DATA,
  284. searcher[CodeScanMatcher_next] = -1,
  285. searcher[CodeScanMatcher_len] = 0,
  286. searcher[CodeScanMatcher_user_data] = 0,
  287. CodeScanReset(searcher, flags);
  288. }
  289. stock CodeScanMatcherData(searcher[CodeScanMatcher], val) {
  290. // Use `ref()` to pass an array.
  291. searcher[CodeScanMatcher_flags] |= SCANNER_HAS_USER_DATA,
  292. searcher[CodeScanMatcher_user_data] = val;
  293. }
  294. // Will not call the function because the check will fail, but will not compile
  295. // if the function doesn't exist, while still passing it in as a string.
  296. #define CodeScanMatcherInit(%0,&%1) CodeScanMatcherInit_((%0),addressof_ScannerCallback_(%1))
  297. #define CodeScanMatcherInit_(%0,addressof_ScannerCallback_(%1,%2)) CodeScanMatcherInit_(%0,addressof_ScannerCallback_(%1),%2)
  298. stock CodeScanMatcherPattern_(searcher[CodeScanMatcher], {Opcode, Float, _}:...) {
  299. new
  300. len = numargs() - 1;
  301. if (len > CODE_SCAN_MAX_PATTERN_ARRAY) {
  302. return -1;
  303. }
  304. if (len & 0x01) {
  305. // Not a multiple of 2 in the scanner.
  306. return -2;
  307. }
  308. for (new i = 0; i != len; ) {
  309. new
  310. optype = getarg(i + 1),
  311. Opcode:op = Opcode:getarg(i + 2);
  312. searcher[CodeScanMatcher_code][i + 0] = optype;
  313. searcher[CodeScanMatcher_code][i + 1] = _:op;
  314. i += 2;
  315. switch (optype) {
  316. case OP_TYPE_OPCODE_: {
  317. new opcount = GetOpcodeInstructionParameters(op);
  318. for (new partype; i != len; )
  319. {
  320. partype = getarg(i + 1);
  321. switch (partype) {
  322. case OP_TYPE_INTEGER_, OP_TYPE_UNKNOWN_, OP_TYPE_FUNCTION_: {
  323. // Got an unexpected parameter.
  324. if (opcount == 0) {
  325. return i / 2 + 1;
  326. }
  327. // Decrement the remaining number of parameters.
  328. // The variable OPs like `CASETBL` have negative
  329. // parameter counts, so will never not match.
  330. --opcount;
  331. searcher[CodeScanMatcher_code][i + 0] = partype;
  332. searcher[CodeScanMatcher_code][i + 1] = getarg(i + 2);
  333. i += 2;
  334. }
  335. default: {
  336. break;
  337. }
  338. }
  339. }
  340. // Missing a required (non-optional) parameter.
  341. if (opcount > 0) {
  342. return i / 2 + 1;
  343. }
  344. }
  345. case OP_TYPE_SKIP_: {
  346. }
  347. default: {
  348. // Incorrect parameter type. Return the op where it happened.
  349. return i / 2 + 1;
  350. }
  351. }
  352. }
  353. searcher[CodeScanMatcher_len] = len;
  354. // No error.
  355. return 0;
  356. }
  357. // Note the lack of trailing comma. This is to make the code patterns work.
  358. #define CodeScanMatcherPattern(%0, CodeScanMatcherPattern_(%0
  359. static stock CodeScanDeref(v) {
  360. static
  361. lFakeMatcher[CodeScanMatcher];
  362. #pragma unused v
  363. #emit load.s.pri 12 // First argument.
  364. #emit stor.s.pri 16 // Secret argument.
  365. #emit retn
  366. return lFakeMatcher; // Make compiler happy, and teach it the array return.
  367. }
  368. static stock bool:CodeScanCheck(Opcode:op, const dctx[DisasmContext], cs[CodeScanMatcher], fctx[CodeScanner], &next) {
  369. // Returns an address of a callback if it passes.
  370. if (!cs[CodeScanMatcher_len]) {
  371. return
  372. next = cs[CodeScanMatcher_next],
  373. false;
  374. }
  375. new
  376. bool:zero = true,
  377. off = cs[CodeScanMatcher_flags];
  378. if (off) {
  379. // To deal with differences in different compilation modes, we just mark
  380. // these opcodes as fully ignorable (because they are mostly used for
  381. // debugging and not real user code).
  382. switch (op) {
  383. case OP_NOP: {
  384. if (off & SCANNER_IGNORE_NOP) {
  385. return
  386. next = cs[CodeScanMatcher_next],
  387. false;
  388. }
  389. }
  390. case OP_BOUNDS: {
  391. if (off & SCANNER_IGNORE_BOUNDS) {
  392. return
  393. next = cs[CodeScanMatcher_next],
  394. false;
  395. }
  396. }
  397. case OP_BREAK: {
  398. if (off & SCANNER_IGNORE_BREAK) {
  399. return
  400. next = cs[CodeScanMatcher_next],
  401. false;
  402. }
  403. }
  404. case OP_HALT: {
  405. if (off & SCANNER_IGNORE_HALT) {
  406. return
  407. next = cs[CodeScanMatcher_next],
  408. false;
  409. }
  410. }
  411. }
  412. }
  413. new
  414. cnt = DisasmGetNumOperands(dctx),
  415. len = cs[CodeScanMatcher_len];
  416. for (new idx = 0; idx != CODE_SCAN_MAX_PARALLEL; ++idx) {
  417. off = cs[CodeScanMatcher_offset][idx];
  418. // Ensure that only one of the parallel scanners starts from the
  419. // beginning on each instruction.
  420. if (off) {
  421. } else if (zero) {
  422. // Get the start point of this match.
  423. cs[CodeScanMatcher_start][idx] = DisasmGetCurIp(dctx),
  424. zero = false;
  425. } else {
  426. continue;
  427. }
  428. if (cs[CodeScanMatcher_code][off] == OP_TYPE_SKIP_) {
  429. off += 2;
  430. if (off == len) {
  431. return
  432. memcpy(fctx[CodeScanMatch_holes], cs[CodeScanMatcher_holes], idx * CODE_SCAN_MAX_HOLES, cs[CodeScanMatcher_holeidx][idx] * cellbytes, CODE_SCAN_MAX_HOLES),
  433. fctx[CodeScanMatch_hole_count] = cs[CodeScanMatcher_holeidx][idx],
  434. fctx[CodeScanMatch_cip] = cs[CodeScanMatcher_start][idx],
  435. fctx[CodeScanMatch_size] = DisasmGetNextIp(dctx) - cs[CodeScanMatcher_start][idx],
  436. true;
  437. } else if (cs[CodeScanMatcher_code][off] == OP_TYPE_OPCODE_ && Opcode:cs[CodeScanMatcher_code][off + 1] == op) {
  438. // Found the match after the current "missing" instruction.
  439. goto CodeScanCheck_pass;
  440. } else {
  441. // The "== op" check is done twice because in this case we don't
  442. // want to fail the scanner if it doesn't match.
  443. continue;
  444. }
  445. }
  446. if (cs[CodeScanMatcher_code][off] == OP_TYPE_OPCODE_ && Opcode:cs[CodeScanMatcher_code][off + 1] == op) {
  447. CodeScanCheck_pass:
  448. // Check if there are enough parameters for this opcode.
  449. off += 2;
  450. for (new i = 0; i != cnt; ++i) {
  451. switch (cs[CodeScanMatcher_code][off++]) {
  452. // Because we now abstract relocations to the disasm system,
  453. // we don't need to differentiate between fixed parameters
  454. // and function parameters any more - they are always fully
  455. // resolved.
  456. case OP_TYPE_INTEGER_, OP_TYPE_FUNCTION_: {
  457. if (cs[CodeScanMatcher_code][off++] != DisasmGetOperandReloc(dctx, i)) {
  458. goto CodeScanCheck_fail;
  459. }
  460. }
  461. case OP_TYPE_UNKNOWN_: {
  462. // Save the parameter.
  463. ++off,
  464. cs[CodeScanMatcher_holes][idx * CODE_SCAN_MAX_HOLES + cs[CodeScanMatcher_holeidx][idx]++] = DisasmGetOperandReloc(dctx, i);
  465. }
  466. case OP_TYPE_OPCODE_, OP_TYPE_SKIP_: {
  467. goto CodeScanCheck_fail;
  468. }
  469. }
  470. }
  471. if (off == len) {
  472. // Get the address of the START of the match.
  473. return
  474. memcpy(fctx[CodeScanMatch_holes], cs[CodeScanMatcher_holes][idx * CODE_SCAN_MAX_HOLES], 0, cs[CodeScanMatcher_holeidx][idx] * cellbytes, CODE_SCAN_MAX_HOLES),
  475. fctx[CodeScanMatch_hole_count] = cs[CodeScanMatcher_holeidx][idx],
  476. fctx[CodeScanMatch_cip] = cs[CodeScanMatcher_start][idx],
  477. fctx[CodeScanMatch_size] = DisasmGetNextIp(dctx) - cs[CodeScanMatcher_start][idx],
  478. true;
  479. } else switch (cs[CodeScanMatcher_code][off]) {
  480. case OP_TYPE_INTEGER_, OP_TYPE_FUNCTION_, OP_TYPE_UNKNOWN_: {
  481. // Parameters remaining, none expected.
  482. goto CodeScanCheck_fail;
  483. }
  484. default: {
  485. // Out of parameters to check but still looking correct.
  486. cs[CodeScanMatcher_offset][idx] = off;
  487. continue;
  488. }
  489. }
  490. }
  491. CodeScanCheck_fail:
  492. // The parameter is wrong.
  493. cs[CodeScanMatcher_holeidx][idx] = cs[CodeScanMatcher_offset][idx] = 0;
  494. }
  495. return
  496. next = cs[CodeScanMatcher_next],
  497. false;
  498. }
  499. static stock bool:CodeScanGetFuncName(addr, name[]) {
  500. // The "name" parameter is longer than 32 (which is the maximum function
  501. // name length normally) beacause we append states to some.
  502. // Name not found.
  503. new
  504. index = GetPublicIndexFromAddress(addr);
  505. if (index < 0) {
  506. return
  507. name[0] = 0,
  508. false;
  509. }
  510. // This code will not return great results for public functions with states.
  511. return
  512. GetPublicNameFromIndex(index, name, 32),
  513. true;
  514. }
  515. static stock bool:CodeScanStepInternal(dctx[DisasmContext], csState[CodeScanner], &parseState, &parseParam) {
  516. // Loop over the data. Since our end condition is "out of data", we know
  517. // that any "false" returns are because of invalid data since the "< 0"
  518. // check is also the only other way that "false" can be returned and we pre-
  519. // empt that one.
  520. switch (DisasmNext(dctx)) {
  521. case DISASM_OK: {
  522. new
  523. stk = csState[CodeScanMatch_stack],
  524. hea = csState[CodeScanMatch_heap],
  525. cip = DisasmGetCurIp(dctx),
  526. Opcode:op = DisasmGetOpcode(dctx);
  527. // The compiler sometimes inserts extra instructions like "NOP" and
  528. // "BREAK" for debugging and padding (as do we) - maybe ignore them.
  529. CodeScanCheckJumpTarget(cip, cip + gBase, stk, hea, csState);
  530. switch (op) {
  531. case OP_HALT: {
  532. if (parseState == 4) {
  533. csState[CodeScanMatch_type] = SCANNER_FUNC_HALT_NO_NAME,
  534. csState[CodeScanMatch_func] = cip,
  535. stk = hea = 0,
  536. CodeScanResetJumpTargets(csState);
  537. }
  538. }
  539. case OP_PROC: {
  540. // This is the start of a new function. The only functions
  541. // that don't start like this are the automata stubs.
  542. csState[CodeScanMatch_type] = SCANNER_FUNC_UNKNOWN,
  543. csState[CodeScanMatch_func] = cip,
  544. CodeScanResetJumpTargets(csState),
  545. stk = hea = parseState = 0;
  546. }
  547. case OP_LOAD_PRI: {
  548. // If we are not in the main functions yet and this is the
  549. // first instruction seen, then it is the start of an
  550. // automata function stub.
  551. if (parseState == 4) {
  552. csState[CodeScanMatch_type] = SCANNER_FUNC_AUTOMATA_NO_NAME,
  553. csState[CodeScanMatch_func] = cip,
  554. stk = hea = 0,
  555. CodeScanResetJumpTargets(csState);
  556. }
  557. }
  558. case OP_PUSH_PRI, OP_PUSH_ALT, OP_PUSH_R, OP_PUSH_S, OP_PUSH, OP_PUSH_ADR: {
  559. if (stk != cellmin) {
  560. stk += cellbytes;
  561. }
  562. parseState = 0;
  563. }
  564. case OP_STACK: {
  565. // The stack grows down, but our count is positive.
  566. if (stk != cellmin) {
  567. stk -= DisasmGetOperand(dctx);
  568. }
  569. parseState = 0;
  570. }
  571. case OP_HEAP: {
  572. if (hea != cellmin) {
  573. hea += DisasmGetOperand(dctx);
  574. }
  575. parseState = 0;
  576. }
  577. case OP_POP_PRI, OP_POP_ALT: {
  578. if (stk != cellmin) {
  579. stk -= cellbytes;
  580. }
  581. parseState = 0;
  582. }
  583. case OP_CALL, OP_CALL_PRI: {
  584. // Remove all the function parameters.
  585. if (parseState == 3) {
  586. stk -= parseParam;
  587. }
  588. parseState = 0;
  589. }
  590. case OP_PUSH_C: {
  591. // The "+ cellbytes" is because when calling a function, the
  592. // parameter is the number of bytes pushed, not including
  593. // this one, with that one implicitly popped on return.
  594. parseParam = DisasmGetOperand(dctx) + cellbytes;
  595. if (stk != cellmin) {
  596. stk += cellbytes,
  597. parseState = 3;
  598. }
  599. }
  600. // There is a code-get pattern of:
  601. //
  602. // LCTRL 5
  603. // ADD.C n
  604. // SCTRL 4
  605. //
  606. // Which adjusts the stack to the correct size after "goto". We
  607. // have to deal with that explcitly. Note that the "ADD.C" may
  608. // be missing if there are no variables currently in scope.
  609. case OP_LCTRL: {
  610. if (DisasmGetOperand(dctx) == 5) {
  611. parseParam = 0;
  612. parseState = 1;
  613. } else {
  614. parseState = 0;
  615. }
  616. }
  617. case OP_ADD_C: {
  618. if (parseState == 1) {
  619. parseParam = -DisasmGetOperand(dctx),
  620. parseState = 2;
  621. } else {
  622. parseState = 0;
  623. }
  624. }
  625. case OP_SCTRL: {
  626. // This is the tricky one, since it can mess up the stack in
  627. // strange ways. Deal with the case where it comes from
  628. // "goto", even though that is generally considered bad.
  629. switch (DisasmGetOperand(dctx)) {
  630. case 2: {
  631. hea = cellmin;
  632. }
  633. case 4: {
  634. switch (parseState) {
  635. case 1: {
  636. stk = 0;
  637. }
  638. case 2: {
  639. stk = parseParam;
  640. }
  641. default: {
  642. stk = cellmin;
  643. }
  644. }
  645. }
  646. case 5: {
  647. stk = cellmin;
  648. }
  649. }
  650. parseState = 0;
  651. }
  652. case OP_JUMP, OP_JZER, OP_JNZ, OP_JEQ, OP_JNEQ, OP_JLESS, OP_JLEQ, OP_JGRTR, OP_JGEQ, OP_JSLESS, OP_JSLEQ, OP_JSGRTR, OP_JSGEQ: {
  653. // Add a jump target. These require relocation as they are
  654. // translated to absolute RAM locations. "DisasmNeedReloc"
  655. // will return "true", but we don't need to call it.
  656. // Relocate it relative to "dat" not "cod" for simpler
  657. // comparisons - just see if the read address matches
  658. // instead of the true code address.
  659. //
  660. // val = val - (base + cod) + (cod - dat);
  661. // val = val - base - cod + cod - dat;
  662. // val = val - base - dat;
  663. // val = val - (base + dat);
  664. // base = base + dat;
  665. // val = val - base;
  666. //
  667. // Only jumps that go forwards.
  668. parseParam = DisasmGetOperand(dctx) - gBase,
  669. parseState = 0;
  670. if (parseParam > cip) {
  671. CodeScanAddJumpTarget(parseParam, stk, hea, csState);
  672. }
  673. }
  674. case OP_JREL: {
  675. // Add a jump target. Only jumps that go forwards.
  676. parseParam = DisasmGetOperand(dctx) + cip,
  677. parseState = 0;
  678. if (parseParam > cip) {
  679. CodeScanAddJumpTarget(parseParam, stk, hea, csState);
  680. }
  681. }
  682. case OP_SWITCH: {
  683. // Add a jump target. These are always forwards.
  684. CodeScanAddSwitchTarget(dctx, stk, hea, csState),
  685. parseState = 0;
  686. }
  687. default: {
  688. parseState = 0;
  689. }
  690. }
  691. csState[CodeScanMatch_stack] = stk,
  692. csState[CodeScanMatch_heap] = hea;
  693. }
  694. case DISASM_DONE: {
  695. return false;
  696. }
  697. case DISASM_NOP: {
  698. parseState = 0;
  699. }
  700. }
  701. return true;
  702. }
  703. stock bool:CodeScanStep(dctx[DisasmContext], csState[CodeScanner]) {
  704. return CodeScanStepInternal(dctx, csState, csState[CodeScanner_state], csState[CodeScanner_param]);
  705. }
  706. static stock CodeScanCall(const cs[CodeScanMatcher], csState[CodeScanner]) {
  707. // If I wrote way more assembly I could get away with not calling
  708. // `CodeScanDeref(cur)` below, and not need to assign `param` to a variable
  709. // before pushing it. But I'm not going to - it isn't worth the effort.
  710. new
  711. func = cs[CodeScanMatcher_func];
  712. if (cs[CodeScanMatcher_flags] & SCANNER_HAS_USER_DATA) {
  713. new
  714. param = cs[CodeScanMatcher_user_data];
  715. #emit PUSH.S param
  716. #emit PUSH.S csState
  717. #emit PUSH.C 8
  718. #emit LCTRL 6
  719. #emit ADD.C 36
  720. #emit LCTRL 8
  721. #emit PUSH.pri
  722. #emit LOAD.S.pri func
  723. #emit SCTRL 6
  724. #emit STOR.S.pri func
  725. } else {
  726. #emit PUSH.S csState
  727. #emit PUSH.C 4
  728. #emit LCTRL 6
  729. #emit ADD.C 36
  730. #emit LCTRL 8
  731. #emit PUSH.pri
  732. #emit LOAD.S.pri func
  733. #emit SCTRL 6
  734. #emit STOR.S.pri func
  735. }
  736. return func;
  737. }
  738. static stock bool:CodeScanFindOneFastPattern3(const matcher[CodeScanMatcher], addr, &cur) {
  739. // Check if the current matcher has this exact function call in it as well.
  740. cur = matcher[CodeScanMatcher_next];
  741. for (new i = 0, j = matcher[CodeScanMatcher_len]; i != j; i += 2) {
  742. if (matcher[CodeScanMatcher_code][i + 0] == OP_TYPE_OPCODE_ && Opcode:matcher[CodeScanMatcher_code][i + 1] == OP_CALL && matcher[CodeScanMatcher_code][i + 2] == OP_TYPE_FUNCTION_ && matcher[CodeScanMatcher_code][i + 3] == addr) {
  743. return true;
  744. }
  745. }
  746. return false;
  747. }
  748. static stock bool:CodeScanFindOneFastPattern2(const matcher[CodeScanMatcher], &addr) {
  749. // Loop over all the function calls in the first matcher, and check that at least one exists in
  750. // all the others. We only need to loop over the first, because it must exist in them all, so
  751. // even if it is in all but the first it isn't good enough.
  752. for (new i = 0, j = matcher[CodeScanMatcher_len]; i != j; i += 2) {
  753. if (matcher[CodeScanMatcher_code][i + 0] == OP_TYPE_OPCODE_ && Opcode:matcher[CodeScanMatcher_code][i + 1] == OP_CALL && matcher[CodeScanMatcher_code][i + 2] == OP_TYPE_FUNCTION_) {
  754. // Found a candidate for the fast scan.
  755. addr = matcher[CodeScanMatcher_code][i + 3];
  756. new cur = matcher[CodeScanMatcher_next];
  757. do {
  758. if (cur == -1) {
  759. // Ran out of matchers, so this function call will do.
  760. return true;
  761. }
  762. // Check that all other matchers have the same function call.
  763. } while (CodeScanFindOneFastPattern3(CodeScanDeref(cur), addr, cur));
  764. }
  765. }
  766. return false;
  767. }
  768. forward bool:CodeScanRun(csState[CodeScanner]);
  769. static stock CodeScanRunFastPrescan(&proc, &nextaddr, const searchFuncAddr) {
  770. // Do a fast code scan for functions containing something vaguely similar to the pattern. Once
  771. // that's found we can reparse the function with the full analysis mode.
  772. new Opcode:o;
  773. new addr = nextaddr;
  774. while (addr < 0) {
  775. switch (o = UnrelocateOpcode(Opcode:ReadAmxMemory(addr))) {
  776. case OP_PROC: {
  777. proc = addr;
  778. addr += 4;
  779. }
  780. case OP_CASETBL: {
  781. addr += (2 * ReadAmxMemory(addr + 4) + 3) * 4;
  782. }
  783. case OP_CALL: {
  784. if (ReadAmxMemory(addr + 4) - gCodBase == searchFuncAddr) {
  785. nextaddr = addr;
  786. return true;
  787. }
  788. addr += 8;
  789. }
  790. default: {
  791. addr += gOpArgCount[o];
  792. }
  793. }
  794. }
  795. return false;
  796. }
  797. stock bool:CodeScanRunFast(csState[CodeScanner], searchFuncAddr = 0) {
  798. if (csState[CodeScanner_first] == -1) {
  799. return true;
  800. }
  801. if (searchFuncAddr || CodeScanFindOneFastPattern2(CodeScanDeref(csState[CodeScanner_first]), searchFuncAddr)) {
  802. new
  803. proc,
  804. addr = csState[CodeScanMatch_cip],
  805. dctx[DisasmContext],
  806. cur,
  807. bool:second,
  808. Opcode:op,
  809. parseState = 4,
  810. parseParam;
  811. // Enable scans to start at a non-zero position.
  812. DisasmInit(dctx);
  813. while (CodeScanRunFastPrescan(proc, addr, searchFuncAddr)) {
  814. addr += 8,
  815. second = false,
  816. dctx[DisasmContext_nip] = proc;
  817. for (cur = csState[CodeScanner_first]; cur != -1; CodeScanReset(CodeScanDeref(cur), cur)) { }
  818. while (CodeScanStepInternal(dctx, csState, parseState, parseParam)) {
  819. if (second && dctx[DisasmContext_opcode] == OP_PROC) {
  820. // Finished the function we know has relevant code in. Move on.
  821. addr = (proc = dctx[DisasmContext_cip]) + 4;
  822. break;
  823. }
  824. second = true;
  825. // Check the address - if it is a jump target that changes the stack
  826. // size BEFORE the instruction, while the instruction itself changes
  827. // it after.
  828. // Found a valid instruction that we don't want to ignore. Finally
  829. // do the actual comparisons to various defined scanners.
  830. for (cur = csState[CodeScanner_first], op = DisasmGetOpcode(dctx); cur != -1; ) {
  831. if (CodeScanCheck(op, dctx, CodeScanDeref(cur), csState, cur)) {
  832. switch (CodeScanCall(CodeScanDeref(cur), csState)) {
  833. case cellmin: {
  834. // End right now.
  835. return true;
  836. }
  837. case -1: {
  838. // Want to skip this match. However, it was a full
  839. // match so does need resetting.
  840. CodeScanReset(CodeScanDeref(cur), cur);
  841. continue;
  842. }
  843. case 0: {
  844. // Do nothing except ignore.
  845. }
  846. default: {
  847. // If code was written, reparse this function.
  848. dctx[DisasmContext_nip] = proc;
  849. }
  850. }
  851. // Reset to the start of the function, to reparse.
  852. for (cur = csState[CodeScanner_first]; cur != -1; CodeScanReset(CodeScanDeref(cur), cur)) { }
  853. break;
  854. }
  855. }
  856. }
  857. }
  858. return true;
  859. } else {
  860. printf("Could not find common `CALL <func>` for the common scanner. Falling back to the slow scanner.");
  861. return CodeScanRun(csState);
  862. }
  863. }
  864. #define CodeScanRunFast(%0,&%1) CodeScanRunFast(%0,addressof(%1))
  865. stock bool:CodeScanRun(csState[CodeScanner]) {
  866. if (csState[CodeScanner_first] == -1) {
  867. return true;
  868. }
  869. new
  870. dctx[DisasmContext],
  871. cur,
  872. Opcode:op,
  873. parseState = 4,
  874. parseParam;
  875. // Enable scans to start at a non-zero position.
  876. DisasmInit(dctx, csState[CodeScanMatch_cip] - gDat);
  877. for (cur = csState[CodeScanner_first]; cur != -1; CodeScanReset(CodeScanDeref(cur), cur)) { }
  878. while (CodeScanStepInternal(dctx, csState, parseState, parseParam)) {
  879. // Check the address - if it is a jump target that changes the stack
  880. // size BEFORE the instruction, while the instruction itself changes
  881. // it after.
  882. // Found a valid instruction that we don't want to ignore. Finally
  883. // do the actual comparisons to various defined scanners.
  884. for (cur = csState[CodeScanner_first], op = DisasmGetOpcode(dctx); cur != -1; ) {
  885. if (CodeScanCheck(op, dctx, CodeScanDeref(cur), csState, cur)) {
  886. switch (CodeScanCall(CodeScanDeref(cur), csState)) {
  887. case cellmin: {
  888. // End right now.
  889. return true;
  890. }
  891. case -1: {
  892. // Want to skip this match. However, it was a full
  893. // match so does need resetting.
  894. CodeScanReset(CodeScanDeref(cur), cur);
  895. continue;
  896. }
  897. case 0: {
  898. // Do nothing except ignore.
  899. }
  900. default: {
  901. // If code was written, reparse this function.
  902. dctx[DisasmContext_nip] = csState[CodeScanMatch_func];
  903. }
  904. }
  905. // Reset to the start of the function, to reparse.
  906. for (cur = csState[CodeScanner_first]; cur != -1; CodeScanReset(CodeScanDeref(cur), cur)) { }
  907. break;
  908. }
  909. }
  910. }
  911. return true;
  912. }
  913. stock CodeScanInit(scanner[CodeScanner]) {
  914. if (gDat == 0) {
  915. GetAmxHeader(gHdr),
  916. gBase = GetAmxBaseAddress() + gHdr[AMX_HDR_DAT],
  917. gCodBase = GetAmxBaseAddress() + gHdr[AMX_HDR_COD],
  918. gDat = gHdr[AMX_HDR_COD] - gHdr[AMX_HDR_DAT],
  919. gOP_NOP = _:RelocateOpcode(OP_NOP);
  920. gOP_CASETBL = _:RelocateOpcode(OP_CASETBL);
  921. }
  922. CodeScanResetJumpTargets(scanner),
  923. scanner[CodeScanMatch_cip] = gDat,
  924. scanner[CodeScanMatch_type] =
  925. scanner[CodeScanMatch_name] =
  926. scanner[CodeScanner_param] =
  927. scanner[CodeScanner_state] =
  928. scanner[CodeScanMatch_heap] =
  929. scanner[CodeScanMatch_stack] = 0,
  930. scanner[CodeScanMatch_params] = cellmin,
  931. scanner[CodeScanner_first] = -1;
  932. }
  933. stock CodeScanClone(dest[CodeScanner], const src[CodeScanner]) {
  934. dest = src,
  935. dest[CodeScanner_first] = -1;
  936. }
  937. stock CodeScanGetFunctionScanner(const csm[CodeScanner], ret[CodeScanner], ctx[DisasmContext]) {
  938. // Doesn't do any decompilation, just gets the information for decompiling
  939. // the whole of the current function.
  940. CodeScanInit(ret),
  941. ctx[DisasmContext_end_ip] = 0,
  942. ctx[DisasmContext_start_ip] = ctx[DisasmContext_nip] = ctx[DisasmContext_cip] = csm[CodeScanMatch_func];
  943. switch (csm[CodeScanMatch_type]) {
  944. case 0, SCANNER_FUNC_AUTOMATA, SCANNER_FUNC_HALT, SCANNER_FUNC_AUTOMATA_NO_NAME, SCANNER_FUNC_HALT_NO_NAME: {
  945. ret[CodeScanner_state] = 4;
  946. }
  947. default: {
  948. ret[CodeScanner_state] = 0;
  949. }
  950. }
  951. }
  952. stock CodeScanGetMatchScanner(const csm[CodeScanner], ret[CodeScanner], ctx[DisasmContext], bool:accurate = false) {
  953. // Doesn't do any decompilation, just gets the information for decompiling
  954. // the currently found match.
  955. CodeScanGetFunctionScanner(csm, ret, ctx);
  956. if (accurate) {
  957. // To be accurate in terms of jump targets, we re-run the scanner over
  958. // the function back up to this point.
  959. while (ctx[DisasmContext_nip] < csm[CodeScanMatch_cip]) {
  960. CodeScanStepInternal(ctx, ret, ret[CodeScanner_state], ret[CodeScanner_param]);
  961. }
  962. } else {
  963. // For speed, we just change the current instruction pointers.
  964. ctx[DisasmContext_start_ip] = ctx[DisasmContext_nip] = ctx[DisasmContext_cip] = csm[CodeScanMatch_cip];
  965. }
  966. }
  967. stock CodeScanGetFunctionDisasm(const csm[CodeScanner], ctx[DisasmContext], offset = 0) {
  968. // Doesn't do any decompilation, just gets the information for decompiling
  969. // the whole of the current function.
  970. ctx[DisasmContext_end_ip] = 0,
  971. ctx[DisasmContext_start_ip] = ctx[DisasmContext_nip] = ctx[DisasmContext_cip] = csm[CodeScanMatch_func] + offset;
  972. }
  973. stock CodeScanGetMatchDisasm(const csm[CodeScanner], ctx[DisasmContext], offset = 0) {
  974. // Doesn't do any decompilation, just gets the information for decompiling
  975. // the currently found match.
  976. ctx[DisasmContext_end_ip] = 0,
  977. ctx[DisasmContext_start_ip] = ctx[DisasmContext_nip] = ctx[DisasmContext_cip] = csm[CodeScanMatch_cip] + offset;
  978. }
  979. stock CodeScanGetFunctionAsm(const csm[CodeScanner], ctx[AsmContext], offset = 0) {
  980. // Doesn't do any decompilation, just gets the information for writing to
  981. // the whole of the current function.
  982. AsmInitPtr(ctx, csm[CodeScanMatch_func] + offset, cellmax);
  983. }
  984. stock CodeScanGetMatchAsm(const csm[CodeScanner], ctx[AsmContext], offset = 0) {
  985. // Doesn't do any decompilation, just gets the information for writing to
  986. // the currently found match.
  987. AsmInitPtr(ctx, csm[CodeScanMatch_cip] + offset, cellmax);
  988. }
  989. stock CodeScanGetMatchFunc(const csm[CodeScanner]) {
  990. // The stored value is relative to "DAT", return relative to "COD".
  991. return csm[CodeScanMatch_func] - gDat;
  992. }
  993. stock CodeScanGetMatchAddress(const csm[CodeScanner]) {
  994. // The stored value is relative to "DAT", return relative to "COD".
  995. return csm[CodeScanMatch_cip] - gDat;
  996. }
  997. stock CodeScanGetMatchFuncData(const csm[CodeScanner]) {
  998. // Return relative to "DAT".
  999. return csm[CodeScanMatch_func];
  1000. }
  1001. stock CodeScanGetMatchAddressData(const csm[CodeScanner]) {
  1002. // Return relative to "DAT".
  1003. return csm[CodeScanMatch_cip];
  1004. }
  1005. stock CodeScanGetMatchLength(const csm[CodeScanner]) {
  1006. return csm[CodeScanMatch_size];
  1007. }
  1008. stock CodeScanNOPMatch(const csm[CodeScanner]) {
  1009. for (new dest = csm[CodeScanMatch_cip], end = dest + csm[CodeScanMatch_size]; dest != end; dest += cellbytes) {
  1010. #emit LOAD.pri gOP_NOP
  1011. #emit SREF.S.pri dest
  1012. }
  1013. {}
  1014. }
  1015. stock CodeScanGetMatchType(csm[CodeScanner]) {
  1016. // Lazilly get the names and types of functions when requested.
  1017. if (csm[CodeScanMatch_type] >= SCANNER_FUNC_UNKNOWN) {
  1018. csm[CodeScanMatch_name][0] = '\0';
  1019. if (CodeScanGetFuncName(csm[CodeScanMatch_func], csm[CodeScanMatch_name])) {
  1020. csm[CodeScanMatch_type] -= 4;
  1021. } else {
  1022. csm[CodeScanMatch_type] /= 2;
  1023. // We could check for functions that are state implementations.
  1024. // Currently public functions with states will only get their names
  1025. // for the state stub, not for the various implementations.
  1026. }
  1027. }
  1028. // There are four types:
  1029. //
  1030. // PUBLIC - Public functions.
  1031. // HALT - The "halt" instructions at the very start.
  1032. // AUTOMATA - A state determining stub.
  1033. // OTHER - A normal function.
  1034. //
  1035. // These names are always prefixed by "SCANNER_FUNC_", and only "PUBLIC" is
  1036. // guaranteed to have a name - the types are partially determined in other
  1037. // ways ("OTHER" will never have a name).
  1038. //
  1039. // There is also "0", which just means that nothing has been scanned yet.
  1040. return csm[CodeScanMatch_type];
  1041. }
  1042. stock CodeScanGetMatchHeap(const csm[CodeScanner]) {
  1043. return csm[CodeScanMatch_heap];
  1044. }
  1045. stock CodeScanGetMatchStack(const csm[CodeScanner]) {
  1046. return csm[CodeScanMatch_stack];
  1047. }
  1048. stock CodeScanGetMatchHole(const csm[CodeScanner], idx) {
  1049. return csm[CodeScanMatch_holes][idx];
  1050. }
  1051. stock CodeScanGetHoleCount(const csm[CodeScanner]) {
  1052. return csm[CodeScanMatch_hole_count];
  1053. }
  1054. stock CodeScanGetMatchName(const csm[CodeScanner], name[]) {
  1055. if (csm[CodeScanMatch_type] >= SCANNER_FUNC_UNKNOWN) {
  1056. // We get the type, because the type is based on the name.
  1057. CodeScanGetMatchType(csm);
  1058. }
  1059. name[0] = '\0',
  1060. strcat(name, csm[CodeScanMatch_name], 32);
  1061. }