home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 191.lha / MungeII / munge2.asm < prev    next >
Assembly Source File  |  1988-04-28  |  8KB  |  389 lines

  1. ; TITLE: Source code for Bryce's Deadly Memory Munger (with enhancements)
  2. ;
  3. ; Bryce's deadly memory munger
  4. ;
  5. ; Munge2: Enhancements by Joe Pearce
  6. ;
  7. ; Wipes all free (unused) memory at program startup to $01.
  8. ; Also, wipes memory just before FreeMem().
  9. ;
  10. ; If run from CLI, use "break" to kill it off.
  11. ; Use "break # d" to toggle between setting freed memory to 1's (the
  12. ; default) and clearing to zero instead.
  13. ; Use "break # e" to force munge to "remunge" all of memory.
  14. ; Use "break # f" to toggle on/off state.
  15. ;
  16. ; If run from the workbench, run it again to kill it.
  17. ;
  18. ; Control of munge can be done from another program as shown:
  19. ;    port = FindPort("munge.port");
  20. ;    if (port) Signal(port->mp_SigTask,signal#);
  21. ;
  22. ; You can check the state of the munger by checking the WORD after the port
  23. ; in memory. Bit 0 set indicated munging to zero. Bit 1 set indicates
  24. ; munger off. For example:
  25. ;    WORD state;
  26. ;    state = *((WORD *)port + sizeof(struct MsgPort) / 2);
  27. ;    if (state & 1) puts("Munging to zero.");
  28. ;    if (state & 2) puts("Munger off");
  29. ;
  30. ; This is a very crude hack... breaks all the rules.
  31. ;
  32. ; Sorry it does not pinpoint your bug.    They never said finding
  33. ; bugs was easy. :-)
  34. ;
  35. ; This source doesn't have "CloseLock" commented out.
  36. ;
  37. ; Note from Joe: If the symbol MUNGELAYER is defined, munge will catch the
  38. ; "layers library bug" at a lower level than CloseWindow. This works until
  39. ; munge is exited at which time Intuition acts very strange and eventually
  40. ; crashes. Therefore, if you want to munge at the layer level, I suggest
  41. ; you turn off munge with a CTRL_F signal instead of terminating it.
  42. ; Running SetPatch (from WB1.3) on your system fixes the layer bug.
  43. ; Define the symbol PATCHED to remove all bug handling by munge.
  44. ;
  45. ; Assembly note for Manx users (from Joe):
  46. ;    as -n -c -d (-eMUNGELAYER) (-ePATCHED) munge2.asm
  47. ;    ln munge2.o -lc32 (although -lc might work just as well)
  48. ;
  49.     NOLIST
  50.     INCLUDE "exec/types.i"
  51.     INCLUDE "exec/lists.i"
  52.     INCLUDE "exec/execbase.i"
  53.     INCLUDE "exec/memory.i"
  54.     INCLUDE "exec/ables.i"
  55.     INCLUDE "exec/ports.i"
  56.     INCLUDE "exec/alerts.i"
  57.     INCLUDE "libraries/dos.i"
  58.     INCLUDE "libraries/dosextens.i"
  59.     LIST
  60.  
  61. ANY_CTRL equ SIGBREAKF_CTRL_C+SIGBREAKF_CTRL_D+SIGBREAKF_CTRL_E+SIGBREAKF_CTRL_F
  62.  
  63. jsrlib    MACRO
  64.         xref _LVO\1
  65.         jsr  _LVO\1(a6)
  66.         ENDM
  67.  
  68. jmplib    MACRO
  69.         xref _LVO\1
  70.         jmp  _LVO\1(a6)
  71.         ENDM
  72.  
  73. blink    MACRO
  74.         bchg.b    #1,$bfe001
  75.         ENDM
  76.  
  77. *********************************************
  78.         entry    start            ; comment this out for Metacomco (?)
  79.  
  80. start    move.l    4,a6            ;Get exec library pointer
  81.         suba.l    a1,a1            ;Put zero in A1
  82.         jsrlib    FindTask        ;Find this task
  83.         move.l    d0,a5
  84.         move.l    pr_ReturnAddr(a5),a0
  85.         move.l    (a0),d7             ;---Get stack size--
  86.  
  87.         moveq    #0,d0            ;Set zero for later
  88.         move.l    pr_CLI(a5),d1       ;Check CLI/Workbench flag
  89.         bne.s    fromCLI
  90.  
  91.         lea.l    pr_MsgPort(a5),a0   ;Wait for the message
  92.         jsrlib    WaitPort        ; the Workbench will send
  93.         lea.l    pr_MsgPort(a5),a0
  94.         jsrlib    GetMsg
  95.  
  96. fromCLI     move.l    d0,-(a7)            ;Save the message, or zero
  97. ******************************************  A5-This task D7-Stack size
  98.         bsr.s    main
  99. ******************************************  D7-MUST contain result code
  100. ExitToDOS:    move.l    (a7)+,d2
  101.         beq.s    notWorkbench
  102.  
  103.         jsrlib    Forbid        ;Required so we won't be unloaded by
  104.         move.l    d2,a1        ; the Workbench too soon.
  105.         jsrlib    ReplyMsg    ;Reply to the Workbench message
  106.  
  107. notWorkbench    move.l    d7,d0        ;Return result code:
  108.         rts            ; 0 = ok       10 = error
  109.                     ; 5 = warning  20 = severe failure
  110. *********************************************
  111.  
  112. main    lea        MungeName(pc),a1        ; find MungePort of existing copy
  113.         jsrlib    FindPort
  114.         tst.l    d0
  115.         beq.s    new                        ; is none, so install ourselves
  116.  
  117.         move.l    4,a6
  118.         move.l    d0,a1
  119.         move.l    MP_SIGTASK(a1),a1
  120.         move.l    #SIGBREAKF_CTRL_C,d0    ; signal other copy to terminate
  121.         jsrlib    Signal
  122.  
  123.         moveq    #5,d7                    ; warning, you just turned us off
  124.         rts                                ; and exit outselves
  125.  
  126. new        move.l    4,a6
  127.         move.l    a5,MungePort+MP_SIGTASK ; create a port to declare we're here
  128.         lea        MungePort(pc),a1
  129.         jsrlib    AddPort
  130.  
  131.         move.l    4,a6
  132.  
  133.     ifnd    MUNGELAYER
  134.     ifnd    PATCHED
  135.         lea.l    IntuiName(pc),a1
  136.         jsrlib    OldOpenLibrary    ; OldOpenLibrary(lib) <-> OpenLibrary(lib,0)
  137.         move.l    d0,IntuiBase
  138.     endc
  139.     else
  140.         lea.l    LayersName(pc),a1
  141.         jsrlib    OldOpenLibrary
  142.         move.l    d0,LayersBase
  143.     endc
  144.  
  145.         bsr        dofree                    ; new
  146.  
  147.     ifnd    MUNGELAYER
  148.     ifnd    PATCHED
  149.         move.l    IntuiBase(pc),a1        ;get IntuiBase
  150.         lea.l    MyCloseWindow(pc),a0
  151.         move.l    a0,d0
  152.         move.w    #$ffb8,a0
  153.         jsrlib    SetFunction
  154.         move.l    d0,OldCloseWindow
  155.     endc
  156.     else
  157.         move.l    LayersBase(pc),a1        ;get LayersBase
  158.         lea.l    MyDeleteLayer(pc),a0
  159.         move.l    a0,d0
  160.         move.w    #$ffa6,a0
  161.         jsrlib    SetFunction
  162.         move.l    d0,OldDeleteLayer
  163.     endc
  164.  
  165.         move.l    a6,a1
  166.         lea.l    MyFreeMem(pc),a0        ;patch into FreeMem
  167.         move.l    a0,d0
  168.         move.w    #$ff2e,a0
  169.         jsrlib    SetFunction
  170.         move.l    d0,OldFreeMem
  171.  
  172. ; new mode switching
  173.  
  174. wloop    move.l    #ANY_CTRL,d0
  175.         jsrlib    Wait
  176.         btst.l    #SIGBREAKB_CTRL_C,d0
  177.         bne.s    bye
  178.  
  179.         btst.l    #SIGBREAKB_CTRL_F,d0
  180.         beq.s    ddd
  181.         bchg.b    #0,OnOff                ; toggle on/off state
  182.         bchg.b    #1,MungeState+1
  183.  
  184. ddd        btst.l    #SIGBREAKB_CTRL_D,d0
  185.         beq.s    eee
  186.         tst.l    MungeVal
  187.         beq.s    doff
  188.  
  189.         move.l    #0,MungeVal
  190.         bra.s    don
  191.  
  192. doff    move.l    #$01010101,MungeVal
  193. don        bchg.b    #0,MungeState+1
  194.  
  195.         tst.b    OnOff
  196.         beq.s    eee
  197.  
  198.         FORBID
  199.         move.l    d0,-(sp)
  200.         bsr        infree
  201.         move.l    (sp)+,d0
  202.  
  203. eee        btst.l    #SIGBREAKB_CTRL_E,d0
  204.         beq.s    wloop
  205.         tst.b    OnOff
  206.         beq.s    wloop
  207.  
  208.         FORBID
  209.         bsr        infree
  210.         bra.s    wloop
  211.  
  212. bye        move.l    4,a6
  213.         move.l    OldFreeMem(pc),d0
  214.         move.w    #$ff2e,a0
  215.         move.l    a6,a1
  216.         jsrlib    SetFunction
  217.  
  218.     ifnd    MUNGELAYER
  219.     ifnd    PATCHED
  220.         move.l    OldCloseWindow(pc),d0
  221.         move.w    #$ffb8,a0
  222.         move.l    IntuiBase(pc),a1
  223.         jsrlib    SetFunction
  224.     endc
  225.     else
  226.         move.l    OldDeleteLayer(pc),d0
  227.         move.w    #$ffa6,a0
  228.         move.l    LayersBase(pc),a1
  229.         jsrlib    SetFunction
  230.     endc
  231.  
  232.         lea        MungePort(pc),a1
  233.         jsrlib    RemPort
  234.  
  235.         moveq    #0,d7
  236.         rts
  237.  
  238. dofree
  239.         FORBID
  240. infree
  241.         movem.l    a2/a3,-(sp)
  242.         move.l    MemList+LH_HEAD(a6),a2    ; get First MemHeader address
  243.  
  244. mloop    tst.l    LN_SUCC(a2)                ; are we at list end?
  245.         beq.s    mend                    ; yes, end of mem "clear"
  246.  
  247.         move.l    MH_FIRST(a2),a3            ; First MemChunk
  248. zloop    move.l    MC_BYTES(a3),d0            ; get size
  249.         subq.l    #8,d0                    ; subtract chunk info size
  250.         beq.s    noclear                    ; can't zap any memory in chunk
  251.  
  252.         move.l    a3,a1                    ; calculate first address to clear
  253.         addq.w    #8,a1                    ; (size already in d0)
  254.         bsr        zapmem
  255.  
  256. noclear move.l    MC_NEXT(a3),a3
  257.         move.l    a3,d0
  258.         bne.s    zloop
  259.  
  260.         move.l    LN_SUCC(a2),a2
  261.         move.l    a2,d0
  262.         bra.s    mloop
  263.         
  264. mend    jsrlib    Permit
  265.         movem.l    (sp)+,a2/a3
  266.         rts
  267.  
  268. ; Bryce's
  269.  
  270. ;ff2e  d2 -210 FreeMem(memoryBlock,byteSize)(A1,D0)
  271. ;
  272. MyFreeMem    
  273.         tst.b    OnOff
  274.         beq.s    freeoff
  275.  
  276.         move.l    4,a0
  277.         move.w    #$444,d1    ;Grey
  278.         tst.b    $127(a0)        ;Are we Forbid()en?
  279.         bge.s    oldway
  280.         tst.b    $126(a0)        ;Are we Disabled()ed?
  281.         bge.s    oldway
  282.  
  283.         move.b    $127(a0),$100
  284.         move.w    #$ff0,d1    ;Yellow
  285.         tst.l    CloseLock    ;Called from within CloseWindow?
  286.         beq.s    noprob        ;no...
  287.  
  288. oldway    blink
  289.         move.w    d1,$dff180
  290. freeoff
  291.         move.l    OldFreeMem(pc),-(a7)
  292.         rts
  293.  
  294. ;Zap block before freeing.
  295. ;
  296. noprob    move.w    #$f00,$dff180
  297.         ;blink
  298.         bsr.s    zapmem
  299.         move.w    #$05a,$dff180
  300.         move.l    OldFreeMem(pc),-(a7)
  301.         rts
  302.  
  303. zapmem  ; addr in a1, # bytes to zap in d0, scratch d1
  304.         addq.l    #3,d0            ; calc number of longwords
  305.         lsr.l    #2,d0
  306.         move.l    MungeVal,d1        ; Wipe memory to this value
  307.         movem.l d0/a1,-(a7)
  308.         bra.s    inlp
  309. loop    move.l    d1,(a1)+
  310. inlp    dbra    d0,loop
  311.         sub.l    #$10000,d0
  312.         bpl.s    loop
  313.         movem.l (a7)+,a1/d0
  314.         rts
  315.  
  316.  
  317.     ifnd    MUNGELAYER
  318.     ifnd    PATCHED
  319.  
  320. ;ffb8  48  -72 CloseWindow(Window)(A0)
  321. ;
  322. MyCloseWindow
  323.         addq.l    #1,CloseLock
  324.         move.l    a1,-(a7)
  325.         move.l    OldCloseWindow(pc),a1
  326.         jsr    (a1)
  327.         move.l    (a7)+,a1
  328.         subq.l    #1,CloseLock
  329.         rts
  330.  
  331.     endc
  332.     else
  333.  
  334. ;ffa6  5a  -90 DeleteLayer(LayerInfo,Layer)(A0/A1)
  335. ;
  336. MyDeleteLayer
  337.         addq.l    #1,CloseLock
  338.         move.l    a2,-(a7)
  339.         move.l    OldDeleteLayer(pc),a2
  340.         jsr    (a2)
  341.         move.l    (a7)+,a2
  342.         subq.l    #1,CloseLock
  343.         rts
  344.  
  345.     endc
  346.  
  347. OnOff
  348.         dc.b    1,0
  349. MungeVal
  350.         dc.l    $01010101
  351. OldFreeMem    
  352.         dc.l    0
  353. CloseLock
  354.         dc.l    0
  355.  
  356.     ifnd    MUNGELAYER
  357.     ifnd    PATCHED
  358. OldCloseWindow
  359.         dc.l    0
  360. IntuiBase
  361.         dc.l    0
  362. IntuiName
  363.         dc.b    'intuition.library',0
  364.         ds.w    0
  365.     endc
  366.     else
  367. OldDeleteLayer
  368.         dc.l    0
  369. LayersBase
  370.         dc.l    0
  371. LayersName
  372.         dc.b    'layers.library',0
  373.         ds.w    0
  374.     endc
  375.  
  376. MungeName
  377.         dc.b    'munge.port',0
  378.         ds.w    0
  379. MungePort
  380.         dc.l    0,0            ; Pred,Succ
  381.         dc.b    NT_MSGPORT    ; Type
  382.         dc.b    0            ; Pri
  383.         dc.l    MungeName    ; Name
  384.         dc.b    PA_IGNORE    ; Flags = Ignore
  385.         dc.b    0            ; No Signal #
  386.         dc.l    0            ; Place for task pointer
  387.         ds.b    LH_SIZE        ; List (not initialized)
  388. MungeState
  389.         dc.w    0            ; munge state