heapalloc.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. Legal:
  3. Version: MPL 1.1
  4. The contents of this file are subject to the Mozilla Public License Version
  5. 1.1 the "License"; you may not use this file except in compliance with
  6. the License. You may obtain a copy of the License at
  7. http://www.mozilla.org/MPL/
  8. Software distributed under the License is distributed on an "AS IS" basis,
  9. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  10. for the specific language governing rights and limitations under the
  11. License.
  12. The Original Code is the YSI framework.
  13. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  14. Portions created by the Initial Developer are Copyright C 2011
  15. the Initial Developer. All Rights Reserved.
  16. Contributors:
  17. Y_Less
  18. koolk
  19. JoeBullet/Google63
  20. g_aSlice/Slice
  21. Misiur
  22. samphunter
  23. tianmeta
  24. maddinat0r
  25. spacemud
  26. Crayder
  27. Dayvison
  28. Ahmad45123
  29. Zeex
  30. irinel1996
  31. Yiin-
  32. Chaprnks
  33. Konstantinos
  34. Masterchen09
  35. Southclaws
  36. PatchwerkQWER
  37. m0k1
  38. paulommu
  39. udan111
  40. Thanks:
  41. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  42. ZeeX - Very productive conversations.
  43. koolk - IsPlayerinAreaEx code.
  44. TheAlpha - Danish translation.
  45. breadfish - German translation.
  46. Fireburn - Dutch translation.
  47. yom - French translation.
  48. 50p - Polish translation.
  49. Zamaroht - Spanish translation.
  50. Los - Portuguese translation.
  51. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes for
  52. me to strive to better.
  53. Pixels^ - Running XScripters where the idea was born.
  54. Matite - Pestering me to release it and using it.
  55. Very special thanks to:
  56. Thiadmer - PAWN, whose limits continue to amaze me!
  57. Kye/Kalcor - SA:MP.
  58. SA:MP Team past, present and future - SA:MP.
  59. Optional plugins:
  60. Gamer_Z - GPS.
  61. Incognito - Streamer.
  62. Me - sscanf2, fixes2, Whirlpool.
  63. */
  64. main()
  65. {
  66. P:1("Malloc_main called");
  67. Malloc_TrySetup();
  68. #if defined Malloc_main
  69. Malloc_main();
  70. #endif
  71. return 1;
  72. //return MALLOC_SOLIDIFYHEAP(3);
  73. }
  74. #if defined _ALS_main
  75. #undef main
  76. #else
  77. #define _ALS_main
  78. #endif
  79. #define main() forward Malloc_main();public Malloc_main()
  80. /*-------------------------------------------------------------------------*//**
  81. * <remarks>
  82. * Loop back up through the stack and find the start of the current stack. If
  83. * it doesn't equal the top of the true stack then we've been called via
  84. * "CallLocalFunction" at some point and thus MAY get some memory corruption.
  85. *
  86. * Based on ZeeX's GetStackTrace, but gets frames instead of returns.
  87. * </remarks>
  88. *//*------------------------------------------------------------------------**/
  89. static Malloc_FindStackTop()
  90. {
  91. new
  92. frm_addr;
  93. #emit LCTRL 5
  94. #emit STOR.S.pri frm_addr
  95. // Loop until we hit a return address of 0.
  96. while (GetFrameReturn(frm_addr))
  97. {
  98. #emit LREF.S.pri frm_addr
  99. #emit STOR.S.pri frm_addr
  100. }
  101. // We can't accurately get the number or parameters because of y_hooks :(.
  102. return frm_addr + 12;
  103. }
  104. /*-------------------------------------------------------------------------*//**
  105. * <remarks>
  106. * Finds all "BOUNDS 0" OpCodes in the AMX and rewrites them to "NOP NOP". The
  107. * byte pattern for this code is "OP_BOUNDS 0", which (AFAIK) can not appear
  108. * anywhere else in the DAT segment. You can have "OP_BOUNDS" as a parameter
  109. * to something, but it would then be followed by an OpCode of "0", which is
  110. * never valid (OP_NONE).
  111. *
  112. * I've tried to make this as resiliant as possible to being called via
  113. * "CallLocalFunction" as not the first callback in the script but there may
  114. * still be a few problems - we won't see till people start testing I guess...
  115. * </remarks>
  116. *//*------------------------------------------------------------------------**/
  117. public OnScriptInit()
  118. {
  119. P:1("Malloc_OnScriptInit called");
  120. // First ever LEGITIMATE call to "heapspace"!
  121. if (heapspace() < MALLOC_MEMORY + 4 * 1024)
  122. {
  123. P:E("heapspace too low for y_malloc: Did you use \"#pragma dynamic\"?");
  124. }
  125. new
  126. Opcode:bounds = RelocateOpcode(OP_BOUNDS),
  127. nop = _:RelocateOpcode(OP_NOP);
  128. for (new i = AMX_HEADER_COD, end = AMX_HEADER_DAT - 4; i < end; i += 4)
  129. {
  130. // Make sure this isn't just something that LOOKS like "bounds 0",
  131. // although I don't think there can be because "0" is not a valid
  132. // opcode.
  133. if (AMX_Read(i) == _:bounds && AMX_Read(i + 4) == 0)
  134. {
  135. // Found a bounds code.
  136. AMX_Write(i, nop);
  137. AMX_Write(i += 4, nop);
  138. }
  139. }
  140. Malloc_TrySetup();
  141. #if defined Malloc_OnScriptInit
  142. Malloc_OnScriptInit();
  143. #endif
  144. SetTimer("Malloc_SolidifyTimer", 0, 0);
  145. SetTimer("Malloc_SolidifyTimer", 0, 0);
  146. return 1;
  147. }
  148. #undef OnScriptInit
  149. #define OnScriptInit Malloc_OnScriptInit
  150. #if defined Malloc_OnScriptInit
  151. forward Malloc_OnScriptInit();
  152. #endif
  153. /*-------------------------------------------------------------------------*//**
  154. * <remarks>
  155. * Move the heap pointer up a load. This is called multiple times at the start
  156. * of the mode because we need to beat protections added in by the virtual
  157. * machine to steal away its heap area.
  158. * </remarks>
  159. *//*------------------------------------------------------------------------**/
  160. static Malloc_TrySetup()
  161. {
  162. P:1("Malloc_TrySetup called");
  163. new
  164. temp;
  165. #emit LCTRL 3
  166. #emit STOR.S.pri temp
  167. if (temp > Malloc_FindStackTop() + 4)
  168. {
  169. P:W("y_malloc set up via \"CallLocalFunction\", memory corruption is a remote possibility");
  170. }
  171. temp = MALLOC_MEMORY * 4;
  172. // Allocate a ton of space on the heap.
  173. #emit LCTRL 2
  174. #emit LOAD.S.alt temp
  175. #emit ADD
  176. #emit STOR.S.pri temp
  177. // Now there's only the normal bit of the stack and heap left.
  178. if (temp == AMX_HEADER_HEA + MALLOC_MEMORY * 4 * 2)
  179. {
  180. // Already allocated and now trying to double allocate.
  181. return;
  182. }
  183. if (temp != AMX_HEADER_HEA + MALLOC_MEMORY * 4)
  184. {
  185. P:F("y_malloc: Not the first HEAP allocation!");
  186. return;
  187. }
  188. else
  189. {
  190. #emit LOAD.S.pri temp
  191. #emit SCTRL 2
  192. if (YSI_g_sHeapStart) return;
  193. }
  194. YSI_g_sHeapStart = temp - MALLOC_MEMORY * 4;
  195. P:2("Malloc_OnScriptInit: %d %d %d", YSI_g_sHeapStart, MALLOC_MEMORY * 4, AMX_HEADER_HEA);
  196. #emit CONST.alt YSI_gMallocMemory
  197. #emit LOAD.pri __YSI_g_sHeapStart
  198. #emit SUB
  199. #emit SHR.C.pri 2 // Divide by 4 to get cells.
  200. #emit STOR.pri __YSI_g_sHeapStart
  201. YSI_gMallocMemory[YSI_g_sHeapStart] = MALLOC_MEMORY - 1;
  202. YSI_g_sUnusedStart = YSI_g_sHeapStart + 1;
  203. // Blank the whole memory. Maybe required if the heap has been used
  204. // already (better to be safe than sorry).
  205. memset(YSI_gMallocMemory[YSI_g_sHeapStart + 1], 0, MALLOC_MEMORY - 1);
  206. #if _DEBUG > 3
  207. // The "#if" is actually ignored by these "#emit" codes, as always.
  208. #emit CONST.alt YSI_gMallocMemory
  209. #emit STOR.S.alt temp
  210. printf("Malloc_OnScriptInit: AMX_HEADER_HEA = %d, YSI_gMallocMemory = %d, YSI_g_sHeapStart = %d", _:AMX_HEADER_HEA, temp, YSI_g_sHeapStart);
  211. printf("Malloc_OnScriptInit: YSI_gMallocMemory + 4 * YSI_g_sHeapStart = %d", temp + 4 * YSI_g_sHeapStart);
  212. #endif
  213. // This is never read, but we now have a spare cell because we HAD to
  214. // allocate an array of some size.
  215. YSI_gMallocMemory[0] = MALLOC_MEMORY;
  216. }
  217. static
  218. //YSI_g_sSolidifyHeap,
  219. YSI_g_sHeapSetup = 0;
  220. static const
  221. // Split in to three because of line length limits.
  222. YSI_g_scErrorMessage1[] =
  223. " \n" \
  224. " \n" \
  225. " ============================================================================\n" \
  226. " | |\n" \
  227. " | ****************** |\n" \
  228. " | * VERY IMPORTANT * |\n" \
  229. " | ****************** |",
  230. YSI_g_scErrorMessage2[] =
  231. " | |\n" \
  232. " | Your \"crashdetect\" plugin is out of date, ignore the \"error 12\" above! |\n" \
  233. " | |\n" \
  234. " | [debug] Run time error 12: \"(sleep mode)\" |\n" \
  235. " | [debug] AMX backtrace: |",
  236. //" | [debug] #0 <SOME ADDRESS> in %s () from <FILE>%s|\n"
  237. YSI_g_scErrorMessage3[] =
  238. " | [debug] #0 <HEX NUMBER> in public Malloc_SolidifyTimer () |\n" \
  239. " | |\n" \
  240. " | For more information, see the YSI.tl release topic. |\n" \
  241. " | |\n" \
  242. " ============================================================================\n" \
  243. " \n";
  244. forward OnRuntimeError(code, &bool:suppress);
  245. public OnRuntimeError(code, &bool:suppress)
  246. {
  247. // This is called if the "crashdetect" plugin is installed.
  248. if (code == 12)
  249. {
  250. P:0(YSI_g_scErrorMessage1);
  251. P:0(YSI_g_scErrorMessage2);
  252. P:0(YSI_g_scErrorMessage3);
  253. }
  254. #if defined Malloc_OnRuntimeError
  255. Malloc_OnRuntimeError(code, suppress);
  256. #endif
  257. if (code == 12)
  258. {
  259. // Make sure our heap is correctly set still...
  260. return Malloc_SolidifyHeap();
  261. }
  262. else return 0;
  263. }
  264. #if defined _ALS_OnRuntimeError
  265. #undef OnRuntimeError
  266. #else
  267. #define _ALS_OnRuntimeError
  268. #endif
  269. #define OnRuntimeError Malloc_OnRuntimeError
  270. #if defined Malloc_OnRuntimeError
  271. forward Malloc_OnRuntimeError(code, &bool:suppress);
  272. #endif
  273. forward Malloc_SolidifyTimer();
  274. public Malloc_SolidifyTimer()
  275. {
  276. P:1("Malloc_SolidifyTimer called");
  277. Malloc_TrySetup();
  278. Malloc_SolidifyHeap();
  279. }
  280. static Malloc_SolidifyHeap()
  281. {
  282. if (YSI_g_sHeapSetup == 2) return 1;
  283. ++YSI_g_sHeapSetup;
  284. #emit LCTRL 3
  285. #emit MOVE.alt
  286. #emit SCTRL 5
  287. #emit SCTRL 4 // Set the original stack pointer.
  288. // Call to save "stk" and "frm", since they aren't in early builds.
  289. #emit PUSH.C 0
  290. #emit SYSREQ.C heapspace // The pre-processor can't touch this.
  291. #emit STACK 4
  292. // Unfortunately, "heapspace" has a parameter pushed first so it saves the
  293. // wrong stack value (the value 4 below where it should be). The only other
  294. // opcode that reliably saves "stk" in "amx->stk" without trying to call a
  295. // native function is "OP_RETN", so let's BADLY invoke it! Note that we
  296. // still need the "heapspace" call (or any function call) to save "hea" and
  297. // "frm", which are NOT saved by "OP_RETN" but are by "OP_SYSREQ_C".
  298. #emit PUSH.C 0 // No parameters
  299. #emit LCTRL 6 // Return to the next instruction...
  300. #emit ADD.C 20
  301. #emit PUSH.pri
  302. #emit PUSH.alt // Same frame.
  303. #emit RETN
  304. // "return" to here...
  305. #emit HALT 12
  306. return 0;
  307. }
  308. /*-------------------------------------------------------------------------*//**
  309. * <param name="playerid">Player that just connected.</param>
  310. * <remarks>
  311. * This is the only callback that can be called before our timers when the mode
  312. * starts. Make sure the heap is set up correctly.
  313. * </remarks>
  314. *//*------------------------------------------------------------------------**/
  315. forward Malloc_DoPlayerConnect(playerid);
  316. public Malloc_DoPlayerConnect(playerid)
  317. {
  318. if (YSI_g_sHeapSetup != 2) Malloc_TrySetup();
  319. // Ensure we only call the originsl.
  320. return call OnPlayerConnect(playerid);
  321. }