Rev 2382 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2540 | hidnplayr | 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 |
||
2382 | hidnplayr | 11 | db 'MENUET01' |
12 | dd 1, start, i_end, mem, mem, 0, 0 |
||
1333 | diamond | 13 | |
14 | start: |
||
15 | ; Zero-initialize uglobals (as in kernel at boot) |
||
2382 | hidnplayr | 16 | mov ecx, (zeroend - zerostart + 3) / 4 |
17 | xor eax, eax |
||
18 | mov edi, zerostart |
||
19 | rep stosd |
||
1333 | diamond | 20 | ; Initialize small heap (as in kernel at boot) |
2382 | hidnplayr | 21 | call init_malloc |
1333 | diamond | 22 | ; Run tests |
2382 | hidnplayr | 23 | call run_test1 |
24 | call run_test2 |
||
25 | call run_test3 |
||
1333 | diamond | 26 | ; All is OK, return |
2382 | hidnplayr | 27 | or eax, -1 |
28 | int 0x40 |
||
1333 | diamond | 29 | |
30 | run_test1: |
||
31 | ; basic test |
||
2382 | hidnplayr | 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 |
||
1333 | diamond | 48 | |
49 | run_test2: |
||
2382 | hidnplayr | 50 | ret |
1333 | diamond | 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) |
||
2382 | hidnplayr | 56 | mov edi, 0x12345678 |
57 | xor esi, esi ; 0 areas allocated |
||
58 | mov ebx, 1024000 |
||
1333 | diamond | 59 | .loop: |
2382 | hidnplayr | 60 | imul edi, 1103515245 |
61 | add edi, 12345 |
||
62 | mov eax, edi |
||
63 | shr eax, 16 |
||
64 | test ebx, 64 |
||
65 | jz .prefer_free |
||
1333 | diamond | 66 | .prefer_malloc: |
2382 | hidnplayr | 67 | test eax, 3 |
68 | jz .free |
||
69 | jmp @f |
||
1333 | diamond | 70 | .prefer_free: |
2382 | hidnplayr | 71 | test eax, 3 |
72 | jnz .free |
||
1333 | diamond | 73 | @@: |
2382 | hidnplayr | 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 |
||
1333 | diamond | 95 | .free: |
2382 | hidnplayr | 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] |
||
1333 | diamond | 122 | @@: |
2382 | hidnplayr | 123 | dec edx |
124 | js @f |
||
125 | xchg eax, [edi] |
||
126 | xchg ecx, [edi+4] |
||
127 | add edi, 8 |
||
128 | jmp @b |
||
1333 | diamond | 129 | @@: |
2382 | hidnplayr | 130 | pop edi |
1333 | diamond | 131 | .common: |
2382 | hidnplayr | 132 | dec ebx |
133 | jnz .loop |
||
1333 | diamond | 134 | @@: |
2382 | hidnplayr | 135 | dec esi |
136 | js @f |
||
137 | pop eax ecx |
||
138 | call free |
||
139 | jmp @b |
||
1333 | diamond | 140 | @@: |
2382 | hidnplayr | 141 | ret |
1333 | diamond | 142 | |
143 | malloc_with_test: |
||
144 | ; calls malloc() and checks returned value |
||
2382 | hidnplayr | 145 | call malloc |
146 | test eax, eax |
||
147 | jz generic_malloc_fail |
||
148 | call check_mutex |
||
149 | call check_range |
||
150 | ret |
||
1333 | diamond | 151 | |
152 | ; Stubs for kernel procedures used by heap code |
||
153 | wait_mutex: |
||
2382 | hidnplayr | 154 | inc dword [ebx] |
155 | ret |
||
1333 | diamond | 156 | |
157 | kernel_alloc: |
||
2382 | hidnplayr | 158 | cmp dword [esp+4], bufsize |
159 | jnz error1 |
||
160 | mov eax, buffer |
||
161 | ret 4 |
||
1333 | diamond | 162 | |
163 | macro $Revision [args] |
||
164 | { |
||
165 | } |
||
166 | |||
167 | ; Error handlers |
||
168 | error1: |
||
2382 | hidnplayr | 169 | mov eax, 1 |
170 | jmp error_with_code |
||
1333 | diamond | 171 | |
172 | generic_malloc_fail: |
||
2382 | hidnplayr | 173 | mov eax, 2 |
174 | jmp error_with_code |
||
1333 | diamond | 175 | |
176 | check_mutex: |
||
2382 | hidnplayr | 177 | cmp [mst.mutex], 0 |
178 | jnz @f |
||
179 | ret |
||
1333 | diamond | 180 | @@: |
2382 | hidnplayr | 181 | mov eax, 3 |
182 | jmp error_with_code |
||
1333 | diamond | 183 | |
184 | check_range: |
||
2382 | hidnplayr | 185 | cmp eax, buffer |
186 | jb @f |
||
187 | cmp eax, buffer+bufsize |
||
188 | jae @f |
||
189 | ret |
||
1333 | diamond | 190 | @@: |
2382 | hidnplayr | 191 | mov eax, 4 |
192 | jmp error_with_code |
||
1333 | diamond | 193 | |
194 | memory_destroyed: |
||
2382 | hidnplayr | 195 | mov eax, 5 |
196 | jmp error_with_code |
||
1333 | diamond | 197 | |
198 | error_with_code: |
||
2382 | hidnplayr | 199 | mov edx, saved_state_num |
1333 | diamond | 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() |
||
2382 | hidnplayr | 206 | int3 ; simplest way to report error |
207 | jmp $-1 ; just in case |
||
1333 | diamond | 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: |
||
2382 | hidnplayr | 218 | mst MEM_STATE |
1333 | diamond | 219 | |
220 | align 16 |
||
2382 | hidnplayr | 221 | bufsize = 0x40000 ; change if malloc.inc changes |
222 | buffer rb bufsize |
||
1333 | diamond | 223 | zeroend: |
224 | |||
2382 | hidnplayr | 225 | saved_state_num dd ? |
226 | saved_state rd 0x10000 |
||
1333 | diamond | 227 | |
228 | align 4 |
||
2382 | hidnplayr | 229 | rb 0x10000 ; for stack |
1333 | diamond | 230 | mem: |