Win32.Traps.Mod 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. (* Aos, Copyright 2001, Pieter Muller, ETH Zurich *)
  2. MODULE Traps; (** AUTHOR "pjm"; PURPOSE "Trap handling and symbolic debugging"; *)
  3. IMPORT SYSTEM, Kernel32, Machine, TrapWriters, KernelLog, Streams, Modules, Objects, Kernel, Reflection, SystemVersion;
  4. CONST
  5. RecursiveLimit = 16; (* normally 1 or 2 - how many recursive traps to display before stopping *)
  6. TraceVerbose = FALSE; TestTrap = TRUE;
  7. TrapMaxCharacters = 32*1024;
  8. (* Process termination halt codes *)
  9. halt* = Objects.halt; haltUnbreakable* = Objects.haltUnbreakable;
  10. TYPE
  11. VAR
  12. modes: ARRAY 25 OF CHAR;
  13. flags: ARRAY 13 OF CHAR;
  14. trapState: LONGINT;
  15. check: Objects.Process;
  16. (* Get a compressed refblk number. *)
  17. (** Display trap state. *)
  18. PROCEDURE Show*( p: Objects.Process; VAR int: Kernel32.Context; VAR exc: Kernel32.ExceptionRecord; long: BOOLEAN );
  19. VAR overflow: BOOLEAN;
  20. desc: ARRAY 128 OF CHAR;
  21. code: LONGINT;
  22. pc: LONGINT; (*ALEX 2005.12.08*)
  23. w: Streams.Writer;
  24. (* Write flag values. *)
  25. PROCEDURE Flags( w: Streams.Writer; s: SET );
  26. VAR i: SHORTINT; ch: CHAR;
  27. BEGIN
  28. FOR i := 0 TO 11 DO
  29. ch := flags[i];
  30. IF ch # "!" THEN
  31. IF i IN s THEN ch := CAP( ch ) END;
  32. w.Char( ch )
  33. END
  34. END;
  35. w.String( " iopl" ); w.Int( ASH( SYSTEM.VAL( LONGINT, s * {12, 13} ), -12 ), 1 )
  36. END Flags;
  37. PROCEDURE Val( CONST s: ARRAY OF CHAR; val: LONGINT );
  38. BEGIN
  39. w.Char( " " ); w.String( s ); w.Char( "=" ); w.Hex( val, -8 )
  40. END Val;
  41. (** Append this to to. *)
  42. PROCEDURE StrAppend( VAR to (** in/out *) : ARRAY OF CHAR; CONST this: ARRAY OF CHAR );
  43. VAR i, j, l: LONGINT;
  44. BEGIN
  45. i := 0;
  46. WHILE to[i] # 0X DO INC( i ) END;
  47. l := LEN( to ) - 1; j := 0;
  48. WHILE (i < l) & (this[j] # 0X) DO to[i] := this[j]; INC( i ); INC( j ) END;
  49. to[i] := 0X
  50. END StrAppend;
  51. (** Convert an integer into a string. *)
  52. PROCEDURE StrIntToStr( val: LONGINT; VAR str: ARRAY OF CHAR );
  53. VAR i, j: LONGINT;
  54. digits: ARRAY 16 OF LONGINT;
  55. BEGIN
  56. IF val = MIN( LONGINT ) THEN COPY( "-2147483648", str ); RETURN END;
  57. IF val < 0 THEN val := -val; str[0] := "-"; j := 1 ELSE j := 0 END;
  58. i := 0;
  59. REPEAT digits[i] := val MOD 10; INC( i ); val := val DIV 10 UNTIL val = 0;
  60. DEC( i );
  61. WHILE i >= 0 DO str[j] := CHR( digits[i] + ORD( "0" ) ); INC( j ); DEC( i ) END;
  62. str[j] := 0X
  63. END StrIntToStr;
  64. PROCEDURE GetDescription;
  65. VAR code : LONGINT; arg: ARRAY 16 OF CHAR;
  66. BEGIN
  67. IF exc.ExceptionCode = Kernel32.ExceptionGuardPage THEN COPY( "guard page violation", desc )
  68. ELSIF exc.ExceptionCode = Kernel32.ExceptionBreakPoint THEN
  69. SYSTEM.GET( int.SP, code ); StrIntToStr( code, desc ); StrAppend( desc, " " );
  70. IF code = 1 THEN StrAppend( desc, "WITH guard failed" )
  71. ELSIF code = 2 THEN StrAppend( desc, "CASE invalid" )
  72. ELSIF code = 3 THEN StrAppend( desc, "RETURN missing" )
  73. ELSIF code = 5 THEN StrAppend( desc, "Implicit type guard failed" )
  74. ELSIF code = 6 THEN StrAppend( desc, "Type guard failed" )
  75. ELSIF code = 7 THEN StrAppend( desc, "Index out of range" )
  76. ELSIF code = 8 THEN StrAppend( desc, "ASSERT failed" )
  77. ELSIF code = 9 THEN StrAppend( desc, "Array dimension error" )
  78. ELSIF code=10 THEN StrAppend(desc, "Array allocation error" ); (* fof *)
  79. ELSIF code = 13 THEN StrAppend( desc, "Keyboard interrupt" )
  80. ELSIF code = 14 THEN StrAppend( desc, "Out of memory" )
  81. ELSIF code = 15 THEN StrAppend( desc, "Deadlock (active objects)" );
  82. ELSIF code = 16 THEN StrAppend( desc, "Procedure returned" );
  83. ELSIF code = 23 THEN StrAppend( desc, "Exceptions.Raise" )
  84. ELSE StrAppend( desc, "HALT statement" )
  85. END
  86. ELSIF exc.ExceptionCode = Kernel32.ExceptionSingleStep THEN COPY( "single step", desc )
  87. ELSIF exc.ExceptionCode = Kernel32.ExceptionAccessViolation THEN COPY( "access violation", desc )
  88. ELSIF exc.ExceptionCode = Kernel32.ExceptionIllegalInstruction THEN COPY( "illegal instruction", desc )
  89. ELSIF exc.ExceptionCode = Kernel32.ExceptionArrayBoundsExceeded THEN COPY( "index out of range", desc )
  90. ELSIF exc.ExceptionCode = Kernel32.ExceptionFltDenormalOperand THEN COPY( "FPU: denormal operand", desc )
  91. ELSIF exc.ExceptionCode = Kernel32.ExceptionFltDivideByZero THEN COPY( "FPU: divide by zero", desc )
  92. ELSIF exc.ExceptionCode = Kernel32.ExceptionFltInexactResult THEN COPY( "FPU: inexact result", desc )
  93. ELSIF exc.ExceptionCode = Kernel32.ExceptionFltInvalidOperation THEN COPY( "FPU: invalid operation", desc )
  94. ELSIF exc.ExceptionCode = Kernel32.ExceptionFltOverflow THEN COPY( "FPU: overflow", desc )
  95. ELSIF exc.ExceptionCode = Kernel32.ExceptionFltStackCheck THEN COPY( "FPU: stack check", desc )
  96. ELSIF exc.ExceptionCode = Kernel32.ExceptionFltUndeflow THEN COPY( "FPU: undeflow", desc )
  97. ELSIF exc.ExceptionCode = Kernel32.ExceptionIntDivideByZero THEN COPY( "integer division by zero", desc )
  98. ELSIF exc.ExceptionCode = Kernel32.ExceptionIntOverflow THEN COPY( "integer overflow", desc )
  99. ELSIF exc.ExceptionCode = Kernel32.ExceptionPrivInstruction THEN COPY( "privileged instruction", desc )
  100. ELSIF exc.ExceptionCode = Kernel32.ExceptionStackOverflow THEN COPY( "stack overflow", desc )
  101. ELSE StrIntToStr( exc.ExceptionCode, arg ); COPY( "exception ", desc ); StrAppend( desc, arg )
  102. END
  103. END GetDescription;
  104. BEGIN
  105. overflow := FALSE;
  106. (*! we need a reentrant lock here, use Kernel32 !! *)
  107. Machine.Acquire( Machine.KernelLog ); (* like KernelLog.Enter, but without output *)
  108. w := TrapWriters.GetWriter();
  109. w.Update; (* flush previous output stuck in global writer w *)
  110. w.Char( 1X ); (* "start of trap" *)
  111. INC( trapState );
  112. IF trapState > RecursiveLimit THEN w.String( " [Recursive TRAP]" );
  113. trapState := 0;
  114. ELSE
  115. (* output first line *)
  116. SYSTEM.GET( int.SP, code );
  117. w.String( "TRAP " ); w.Int( code, 1 ); w.String( " [" ); w.Int( trapState, 1 ); w.String( "]" ); w.String( " PL" );
  118. w.Int( int.CS MOD 4, 2 ); w.Char( " " ); GetDescription();
  119. (*
  120. CASE exc.halt OF
  121. -14: (* page fault *)
  122. IF (int.CS MOD 4 > Machine.KernelLevel) & (exc.pf+4 = int.ESP) THEN
  123. w.String("stack overflow"); overflow := TRUE
  124. END
  125. |0: w.String("division error")
  126. |1: w.String("WITH guard failed")
  127. |2: w.String("CASE invalid")
  128. |3: w.String("RETURN missing")
  129. |4: w.String("integer overflow")
  130. |5: w.String("implicit type guard failed")
  131. |6: w.String("type guard failed")
  132. |7: w.String("index out of range")
  133. |8: w.String("ASSERT failed")
  134. |9: w.String("array dimension error")
  135. |14: w.String("out of memory")
  136. ELSE
  137. IF (exc.halt > MAX(INTEGER)+1) OR (exc.halt < MIN(INTEGER)) THEN
  138. w.String("module freed?")
  139. END
  140. END;
  141. *)
  142. w.String( desc ); w.Ln; w.Update;
  143. (*
  144. IF exc.locks # {} THEN
  145. w.String(", Locks: "); w.Set(exc.locks)
  146. END;
  147. *)
  148. w.String( "System: " ); w.String( Machine.version );
  149. w.String(" Kernel_CRC="); w.Hex(SystemVersion.BootCRC,8);
  150. w.String(" Uptime="); w.Hex(Machine.GetTimer()- Machine.boottime, 8);
  151. (*w.String( " Uptime=" ); w.Hex(Machine.GetTimer()-Machine.boottime, -8);*)
  152. IF long THEN
  153. w.Char( 0EX ); (* "fixed font" *)
  154. w.Ln;
  155. w.String("Processor:");
  156. (* output values *)
  157. Val( "CS", int.CS ); Val( "DS", int.DS ); Val( "ES", int.ES ); Val( "SS", int.SS ); (* Val("CR0", int.CR[0]);*)
  158. (*
  159. Val("FPU", SYSTEM.VAL(LONGINT, int.FPU[1] * {0..15} + LSH(int.FPU[2], 16))); w.Ln;
  160. *)
  161. Val( "PC", int.PC ); Val( "ESI", int.ESI ); Val( "EDI", int.EDI ); Val( "ESP", int.SP );
  162. (*
  163. Val("CR2", int.CR[2]);
  164. *)
  165. Val( "PID", p.id ); Val( "EAX", int.EAX ); Val( "EBX", int.EBX ); Val( "ECX", int.ECX ); Val( "EDX", int.EDX );
  166. (*
  167. Val("CR3", int.CR[3]);
  168. Val("LCK", SYSTEM.VAL(LONGINT, int.locks)); w.Ln;
  169. *)
  170. Val( "EBP", int.BP ); Val( "FS", int.FS ); Val( "GS", int.GS ); (* Val("ERR", int.ERR); Val("CR4", int.CR[4]); *)
  171. Val( "TMR", Kernel.GetTicks() ); (* w.Ln;*)
  172. IF SYSTEM.VAL( CHAR, int.DR7 ) # 0X THEN (* some breakpoints enabled *)
  173. Val( "DR0", int.DR0 ); Val( "DR1", int.DR1 ); Val( "DR2", int.DR2 ); Val( "DR3", int.DR3 ); Val( "DR6", int.DR6 );
  174. Val( "DR7", int.DR7 ); w.Ln
  175. END;
  176. w.Ln; w.String( " FLAGS: " ); Flags( w, SYSTEM.VAL( SET, int.FLAGS ) );
  177. w.Char( 0FX ); (* "proportional font" *)
  178. w.Char( " " ); w.Set( SYSTEM.VAL( SET, int.FLAGS ) ); w.Ln;
  179. w.String(" Features="); w.Set(Machine.features); w.Set(Machine.features2); w.Ln;
  180. (*IF int.INT = Machine.UD THEN KernelLog.Memory(int.PC, 16) END*) (* show bad instruction *)
  181. ELSE w.Ln
  182. END;
  183. w.Update;
  184. w.String( "Process:" ); Reflection.WriteProcess( w, p ); w.Ln;
  185. (*IF exc.halt = 1301 THEN (* lock timeout - see Machine *)
  186. KernelLog.Memory(ADDRESSOF(Machine.trapState[0]), LEN(Machine.trapState) *
  187. (ADDRESSOF(Machine.trapState[1]) - ADDRESSOF(Machine.trapState[0])));
  188. w.Hex(SYSTEM.VAL(LONGINT, Machine.trapLocksBusy), 8); w.Ln
  189. END;
  190. IF (int.INT = Machine.PF) & (ABS(int.PC-exc.CR[2]) < 100H) THEN (* PF close to EIP *)
  191. KernelLog.Memory(int.ESP-16, 64) (* show stack *)
  192. END;*)
  193. (*ALEX 2005.12.08 when calling a pointer to a function and the pointer is NULL meaning eip=NULL*)
  194. IF int.PC = 0 THEN SYSTEM.GET( int.SP, pc ) ELSE pc := int.PC END;
  195. w.String( "StackTraceBack:" ); w.Ln;
  196. Reflection.StackTraceBack( w, pc, int.BP, int.SP, Objects.GetStackBottom(p), long, overflow )
  197. END;
  198. w.String("---------------------------------"); w.Ln;
  199. w.Char(02X); (* "end of trap" *)
  200. w.Update;
  201. TrapWriters.Trapped;
  202. FINALLY
  203. Machine.Release( Machine.KernelLog ); (* like KernelLog.Exit, but without output *)
  204. trapState := 0
  205. END Show;
  206. PROCEDURE SetLastExceptionState( ex: Kernel32.Context );
  207. (*
  208. VAR id: LONGINT;
  209. BEGIN
  210. id := Machine.AcquirePreemption();
  211. Objects.running[id].exp := ex;
  212. Machine.ReleasePreemption();
  213. *)
  214. END SetLastExceptionState;
  215. PROCEDURE CheckBP(fp: ADDRESS): ADDRESS;
  216. VAR n: ADDRESS;
  217. BEGIN
  218. IF (fp # NIL) THEN
  219. SYSTEM.GET(fp, n);
  220. IF ODD(n) THEN RETURN fp + SIZEOF(ADDRESS) END;
  221. END;
  222. RETURN fp;
  223. END CheckBP;
  224. (** Handles an exception. Interrupts are on during this procedure. *)
  225. PROCEDURE HandleException( VAR int: Kernel32.Context; VAR exc: Kernel32.ExceptionRecord; VAR handled: BOOLEAN );
  226. VAR fp, newFP, sp, pc, handler: ADDRESS;
  227. BEGIN
  228. fp := int.BP; sp := int.SP; pc := int.PC; handler := Modules.GetExceptionHandler( pc );
  229. IF handler # -1 THEN (* Handler in the current PAF *)
  230. int.PC := handler; handled := TRUE; SetTrapVariable( pc, fp ); SetLastExceptionState( int )
  231. ELSE
  232. WHILE (fp # 0) & (handler = -1) DO
  233. fp := CheckBP(fp);
  234. SYSTEM.GET( fp + 4, pc );
  235. pc := pc - 1; (* CALL instruction, machine dependant!!! *)
  236. handler := Modules.GetExceptionHandler( pc );
  237. sp := fp; (* Save the old framepointer into the stack pointer *)
  238. SYSTEM.GET( fp, fp ) (* Unwind PAF *)
  239. END;
  240. IF handler = -1 THEN handled := FALSE;
  241. ELSE
  242. int.PC := handler; int.BP := fp; int.SP := sp; SetTrapVariable( pc, fp );
  243. SetLastExceptionState( int ); handled := TRUE
  244. END
  245. END
  246. END HandleException;
  247. PROCEDURE SetTrapVariable( pc, fp: LONGINT );
  248. VAR varadr: LONGINT;
  249. BEGIN
  250. varadr := Reflection.GetVariableAdr( pc, fp, "trap" );
  251. IF varadr # -1 THEN SYSTEM.PUT8( varadr, 1 ) END
  252. END SetTrapVariable;
  253. (* Unbreakable stack trace back with regard to every FINALLY on the way *)
  254. PROCEDURE Unbreakable( p: Objects.Process; VAR int: Kernel32.Context; VAR exc: Kernel32.ExceptionRecord;
  255. VAR handled: BOOLEAN );
  256. VAR ebp, ebpSave, pc, handler, ebpBottom: ADDRESS; checkedBP: ADDRESS; hasFinally: BOOLEAN;
  257. BEGIN
  258. ebp := int.BP; pc := int.PC; hasFinally := FALSE;
  259. handler := Modules.GetExceptionHandler( pc );
  260. (* Handler in the current PAF *)
  261. IF handler # -1 THEN int.PC := handler; hasFinally := TRUE; SetTrapVariable( pc, ebp ); END;
  262. (* The first waypoint is the ebp of the top PAF *)
  263. ebpSave := CheckBP(ebp);
  264. WHILE (ebp # 0) DO
  265. (* Did we reach the last PAF? *)
  266. checkedBP := CheckBP(ebp);
  267. SYSTEM.GET( checkedBP, pc );
  268. IF (pc = 0) THEN
  269. ebpBottom := ebp; (* Save the FP of the last PAF *)
  270. END;
  271. (* Get the return pc *)
  272. SYSTEM.GET( checkedBP + 4, pc );
  273. handler := Modules.GetExceptionHandler( pc );
  274. (* Save the last framepointer as stackpointer *)
  275. IF ~hasFinally THEN int.SP := ebp; END;
  276. SYSTEM.GET( checkedBP, ebp );
  277. (* Here ebp may be 0. *)
  278. IF (handler # -1) & (ebp # 0) THEN (* If Objects.Terminate has a FINALLY this doesn't work !!! *)
  279. IF hasFinally THEN
  280. (* Connect Finally to Finally *)
  281. SYSTEM.PUT( ebpSave + 4, handler ); (* Adapt the return pc *)
  282. SYSTEM.PUT( ebpSave, ebp ); (* Adapt the dynamic link *)
  283. ebpSave := checkedBP;
  284. ELSE
  285. int.PC := handler; int.BP := ebp; ebpSave := checkedBP; hasFinally := TRUE;
  286. END;
  287. SetTrapVariable( pc, ebp )
  288. END
  289. END;
  290. (* Now ebp = 0, bottom of the stack, so link the last known return PC to the Termination *)
  291. IF ~hasFinally THEN
  292. SYSTEM.GET( ebpBottom + 4, pc ); (* PC of the Terminate *)
  293. int.PC := pc; int.BP := ebpBottom;
  294. ELSIF ebpSave # ebpBottom THEN
  295. SYSTEM.GET( ebpBottom + 4, pc ); (* PC of the Terminate *)
  296. SYSTEM.PUT( ebpSave + 4, pc ); SetLastExceptionState( int )
  297. END;
  298. handled := TRUE; (* If FALSE the process could be restarted, may be this is the meaning? *)
  299. END Unbreakable;
  300. (* General exception handler. *)
  301. PROCEDURE Exception( VAR int: Kernel32.Context; VAR exc: Kernel32.ExceptionRecord; VAR handled: BOOLEAN );
  302. VAR t: Objects.Process; user, traceTrap: BOOLEAN; exchalt: LONGINT;
  303. BEGIN (* interrupts off *)
  304. t := Objects.CurrentProcess();
  305. check := t;
  306. (*
  307. t := Objects.running[Machine.ID()]; (* t is running process *)
  308. *)
  309. handled := FALSE;
  310. (*
  311. Machine.GetExceptionState(int, exc);
  312. *)
  313. user := (int.CS MOD 4 > 0 (* Machine.KernelLevel*) ); SYSTEM.GET( int.SP, exchalt );
  314. (*
  315. traceTrap := (exc.locks = {}) & (exc.halt >= MAX(INTEGER)) & (exc.halt <= MAX(INTEGER)+1);
  316. *)
  317. traceTrap := FALSE;
  318. Show( t, int, exc, (* exc.halt # MAX(INTEGER)+1*) TRUE ); (* Always show the trap info!*)
  319. IF exchalt = haltUnbreakable THEN Unbreakable( t, int, exc, handled )
  320. ELSIF ~traceTrap THEN HandleException( int, exc, handled )
  321. END;
  322. IF ~handled THEN
  323. (* Taken from Machine to allow the FINALLY in the kernel *)
  324. (*
  325. locks := Machine.BreakAll();
  326. SYSTEM.STI();
  327. *)
  328. IF ~traceTrap THEN (* trap *)
  329. IF user THEN (* return to outer level *)
  330. IF TraceVerbose THEN
  331. KernelLog.Enter; KernelLog.String( "Jump" ); KernelLog.Hex( t.restartPC, 9 );
  332. KernelLog.Hex( t.restartSP, 9 ); (* KernelLog.Hex(t.stack.high, 9);*)
  333. KernelLog.Exit
  334. END;
  335. (*
  336. INCL(SYSTEM.VAL(SET,int.EFLAGS), Machine.IFBit); (* enable interrupts *)
  337. *)
  338. int.BP := 0; int.SP := t.restartSP; (* reset stack *)
  339. int.PC := t.restartPC; (* restart object body or terminate *)
  340. ELSE (* trap was in kernel (interrupt handler) *) (* fixme: recover from trap in stack traceback *)
  341. KernelLog.Enter; KernelLog.String( "Kernel halt" ); KernelLog.Exit; Machine.Shutdown( FALSE )
  342. END
  343. END
  344. END;
  345. IF Objects.PleaseHalt IN t.flags THEN
  346. EXCL( t.flags, Objects.PleaseHalt );
  347. IF Objects.Unbreakable IN t.flags THEN EXCL( t.flags, Objects.Unbreakable ) END;
  348. IF Objects.SelfTermination IN t.flags THEN EXCL( t.flags, Objects.SelfTermination ) END
  349. END;
  350. check := NIL;
  351. FINALLY
  352. (* if trap occurs in this procedure, then go on working right here *)
  353. END Exception;
  354. PROCEDURE Init;
  355. VAR
  356. s: ARRAY 8 OF CHAR;
  357. BEGIN
  358. IF TestTrap THEN
  359. Machine.GetConfig( "TestTrap", s );
  360. IF s[0] = "1" THEN HALT( 98 ) END
  361. END;
  362. IF TestTrap & (s[0] = "2") THEN HALT( 99 ) END;
  363. Objects.InstallExceptionHandler( Exception );
  364. END Init;
  365. PROCEDURE Install*; (* for loading this module *)
  366. BEGIN
  367. TrapWriters.InstallTraceWriter
  368. END Install;
  369. BEGIN
  370. modes := " rdy run awl awc awe rip"; (* 4 characters per mode from Objects.Ready to Objects.Terminated *)
  371. flags := "c!p!a!zstido"; (* bottom flags, !=reserved *)
  372. Init
  373. END Traps.
  374. SystemTools.FreeDownTo Traps ~
  375. (*
  376. 12.03.1998 pjm Started
  377. 06.08.1998 pjm Exported Show and removed AosException upcall installation & Modules lock
  378. 10.12.1998 pjm New refblk
  379. 23.06.1999 pjm State added
  380. *)
  381. (*
  382. to do:
  383. o stack overflow message is not correctly displayed in case of dynamic arrays (EDI = CR2, ESP # CR2)
  384. o fix KernelLog.Memory calls removed when switching to Streams
  385. o fix use of KernelLog lock in Show
  386. o if allowing modification of variables using their descriptors, it should also have reference to module to avoid gc after free.
  387. *)