Subversion Repositories Kolibri OS

Rev

Rev 1635 | Rev 3232 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2009-2011. All rights reserved. ;;
  4. ;; Distributed under terms of the GNU General Public License    ;;
  5. ;;                                                              ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7.  
  8. ; Tests of malloc()/free() from the kernel heap.
  9. ; This file is not included in the kernel, it is just test application.
  10. use32
  11.         db      'MENUET01'
  12.         dd      1, start, i_end, mem, mem, 0, 0
  13.  
  14. start:
  15. ; Zero-initialize uglobals (as in kernel at boot)
  16.         mov     ecx, (zeroend - zerostart + 3) / 4
  17.         xor     eax, eax
  18.         mov     edi, zerostart
  19.         rep     stosd
  20. ; Initialize small heap (as in kernel at boot)
  21.         call    init_malloc
  22. ; Run tests
  23.         call    run_test1
  24.         call    run_test2
  25.         call    run_test3
  26. ; All is OK, return
  27.         or      eax, -1
  28.         int     0x40
  29.  
  30. run_test1:
  31. ; basic test
  32.         mov     eax, 1
  33.         call    malloc_with_test
  34.         mov     byte [eax], 0xDD
  35.         mov     esi, eax
  36.         mov     eax, 1
  37.         call    malloc_with_test
  38.         cmp     byte [esi], 0xDD
  39.         jnz     memory_destroyed
  40.         mov     byte [eax], 0xEE
  41.         xchg    eax, esi
  42.         call    free
  43.         cmp     byte [esi], 0xEE
  44.         jnz     memory_destroyed
  45.         xchg    eax, esi
  46.         call    free
  47.         ret
  48.  
  49. run_test2:
  50.         ret
  51.  
  52. run_test3:
  53. ; 1024000 times run random operation.
  54. ; Randomly select malloc(random size from 1 to 1023)
  55. ; or free(random of previously allocated areas)
  56.         mov     edi, 0x12345678
  57.         xor     esi, esi        ; 0 areas allocated
  58.         mov     ebx, 1024000
  59. .loop:
  60.         imul    edi, 1103515245
  61.         add     edi, 12345
  62.         mov     eax, edi
  63.         shr     eax, 16
  64.         test    ebx, 64
  65.         jz      .prefer_free
  66. .prefer_malloc:
  67.         test    eax, 3
  68.         jz      .free
  69.         jmp     @f
  70. .prefer_free:
  71.         test    eax, 3
  72.         jnz     .free
  73. @@:
  74.         shr     eax, 2
  75.         and     eax, 1023
  76.         jz      .loop
  77.         push    ebx
  78.         push    eax
  79. ;       mov     ecx, [saved_state_num]
  80. ;       mov     [saved_state+ecx*8], eax
  81.         call    malloc_with_test
  82. ;       mov     ecx, [saved_state_num]
  83. ;       mov     [saved_state+ecx*8+4], eax
  84. ;       inc     [saved_state_num]
  85.         pop     ecx
  86.         pop     ebx
  87.         inc     esi
  88.         push    ecx eax
  89.         push    edi
  90.         mov     edi, eax
  91.         mov     eax, esi
  92.         rep     stosb
  93.         pop     edi
  94.         jmp     .common
  95. .free:
  96.         test    esi, esi
  97.         jz      .loop
  98.         xor     edx, edx
  99.         div     esi
  100.         sub     edx, esi
  101.         neg     edx
  102.         dec     edx
  103.         mov     eax, [esp+edx*8]
  104. ;       mov     ecx, [saved_state_num]
  105. ;       mov     [saved_state+ecx*8], -1
  106. ;       mov     [saved_state+ecx*8+4], eax
  107. ;       inc     [saved_state_num]
  108.         mov     ecx, [esp+edx*8+4]
  109.         push    edi eax
  110.         mov     edi, eax
  111.         mov     al, [edi]
  112.         repz    scasb
  113.         jnz     memory_destroyed
  114.         pop     eax edi
  115.         push    ebx edx
  116.         call    free
  117.         pop     edx ebx
  118.         dec     esi
  119.         pop     eax ecx
  120.         push    edi
  121.         lea     edi, [esp+4]
  122. @@:
  123.         dec     edx
  124.         js      @f
  125.         xchg    eax, [edi]
  126.         xchg    ecx, [edi+4]
  127.         add     edi, 8
  128.         jmp     @b
  129. @@:
  130.         pop     edi
  131. .common:
  132.         dec     ebx
  133.         jnz     .loop
  134. @@:
  135.         dec     esi
  136.         js      @f
  137.         pop     eax ecx
  138.         call    free
  139.         jmp     @b
  140. @@:
  141.         ret
  142.  
  143. malloc_with_test:
  144. ; calls malloc() and checks returned value
  145.         call    malloc
  146.         test    eax, eax
  147.         jz      generic_malloc_fail
  148.         call    check_mutex
  149.         call    check_range
  150.         ret
  151.  
  152. ; Stubs for kernel procedures used by heap code
  153. wait_mutex:
  154.         inc     dword [ebx]
  155.         ret
  156.  
  157. kernel_alloc:
  158.         cmp     dword [esp+4], bufsize
  159.         jnz     error1
  160.         mov     eax, buffer
  161.         ret     4
  162.  
  163. macro $Revision [args]
  164. {
  165. }
  166.  
  167. ; Error handlers
  168. error1:
  169.         mov     eax, 1
  170.         jmp     error_with_code
  171.  
  172. generic_malloc_fail:
  173.         mov     eax, 2
  174.         jmp     error_with_code
  175.  
  176. check_mutex:
  177.         cmp     [mst.mutex], 0
  178.         jnz     @f
  179.         ret
  180. @@:
  181.         mov     eax, 3
  182.         jmp     error_with_code
  183.  
  184. check_range:
  185.         cmp     eax, buffer
  186.         jb      @f
  187.         cmp     eax, buffer+bufsize
  188.         jae     @f
  189.         ret
  190. @@:
  191.         mov     eax, 4
  192.         jmp     error_with_code
  193.  
  194. memory_destroyed:
  195.         mov     eax, 5
  196.         jmp     error_with_code
  197.  
  198. error_with_code:
  199.         mov     edx, saved_state_num
  200. ; eax = error code
  201. ; 1 signals error in testing code (wrong bufsize)
  202. ; 2 = malloc() returned NULL
  203. ; 3 = mutex not released
  204. ; 4 = weird returned value from malloc()
  205. ; 5 = memory destroyed by malloc() or free()
  206.         int3    ; simplest way to report error
  207.         jmp     $-1     ; just in case
  208.  
  209. ; Include main heap code
  210. include '../proc32.inc'
  211. include '../const.inc'
  212. include 'malloc.inc'
  213.  
  214. i_end:
  215.  
  216. align 4
  217. zerostart:
  218. mst     MEM_STATE
  219.  
  220. align 16
  221. bufsize = 0x40000       ; change if malloc.inc changes
  222. buffer  rb      bufsize
  223. zeroend:
  224.  
  225. saved_state_num dd      ?
  226. saved_state     rd      0x10000
  227.  
  228. align 4
  229.         rb      0x10000 ; for stack
  230. mem:
  231.