Rev 7133 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2455 | mario79 | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
9715 | Doczom | 3 | ;; Copyright (C) KolibriOS team 2009-2022. All rights reserved. ;; |
2455 | mario79 | 4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
||
6 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
7 | |||
2288 | clevermous | 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: |
||
3126 | clevermous | 53 | ; 1024 times run random operation. |
2288 | clevermous | 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 |
||
3126 | clevermous | 58 | mov ebx, 1024 |
2288 | clevermous | 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 |
||
3126 | clevermous | 81 | push edi |
2288 | clevermous | 82 | call malloc_with_test |
3126 | clevermous | 83 | pop ecx |
84 | cmp ecx, edi |
||
85 | jnz edi_destroyed |
||
2288 | clevermous | 86 | ; mov ecx, [saved_state_num] |
87 | ; mov [saved_state+ecx*8+4], eax |
||
88 | ; inc [saved_state_num] |
||
89 | pop ecx |
||
90 | pop ebx |
||
91 | inc esi |
||
92 | push ecx eax |
||
93 | push edi |
||
94 | mov edi, eax |
||
95 | mov eax, esi |
||
96 | rep stosb |
||
97 | pop edi |
||
98 | jmp .common |
||
99 | .free: |
||
100 | test esi, esi |
||
101 | jz .loop |
||
102 | xor edx, edx |
||
103 | div esi |
||
104 | sub edx, esi |
||
105 | neg edx |
||
106 | dec edx |
||
107 | mov eax, [esp+edx*8] |
||
108 | ; mov ecx, [saved_state_num] |
||
109 | ; mov [saved_state+ecx*8], -1 |
||
110 | ; mov [saved_state+ecx*8+4], eax |
||
111 | ; inc [saved_state_num] |
||
112 | mov ecx, [esp+edx*8+4] |
||
113 | push edi eax |
||
114 | mov edi, eax |
||
115 | mov al, [edi] |
||
116 | repz scasb |
||
117 | jnz memory_destroyed |
||
118 | pop eax edi |
||
119 | push ebx edx |
||
3126 | clevermous | 120 | push edi |
2288 | clevermous | 121 | call free |
3126 | clevermous | 122 | pop ecx |
123 | cmp ecx, edi |
||
124 | jnz edi_destroyed |
||
2288 | clevermous | 125 | pop edx ebx |
126 | dec esi |
||
127 | pop eax ecx |
||
128 | push edi |
||
129 | lea edi, [esp+4] |
||
130 | @@: |
||
131 | dec edx |
||
132 | js @f |
||
133 | xchg eax, [edi] |
||
134 | xchg ecx, [edi+4] |
||
135 | add edi, 8 |
||
136 | jmp @b |
||
137 | @@: |
||
138 | pop edi |
||
139 | .common: |
||
140 | dec ebx |
||
141 | jnz .loop |
||
142 | @@: |
||
143 | dec esi |
||
144 | js @f |
||
145 | pop eax ecx |
||
146 | call free |
||
147 | jmp @b |
||
148 | @@: |
||
149 | ret |
||
150 | |||
151 | malloc_with_test: |
||
152 | ; calls malloc() and checks returned value |
||
153 | call malloc |
||
154 | test eax, eax |
||
155 | jz generic_malloc_fail |
||
156 | call check_mutex |
||
157 | call check_range |
||
158 | ret |
||
159 | |||
160 | ; Stubs for kernel procedures used by heap code |
||
3126 | clevermous | 161 | mutex_init: |
162 | and dword [ecx], 0 |
||
2288 | clevermous | 163 | ret |
3126 | clevermous | 164 | mutex_lock: |
165 | inc dword [ecx] |
||
166 | ret |
||
167 | mutex_unlock: |
||
168 | dec dword [ecx] |
||
169 | ret |
||
2288 | clevermous | 170 | |
171 | kernel_alloc: |
||
172 | cmp dword [esp+4], bufsize |
||
173 | jnz error1 |
||
174 | mov eax, buffer |
||
175 | ret 4 |
||
176 | |||
177 | macro $Revision [args] |
||
178 | { |
||
179 | } |
||
180 | |||
181 | ; Error handlers |
||
182 | error1: |
||
183 | mov eax, 1 |
||
184 | jmp error_with_code |
||
185 | |||
186 | generic_malloc_fail: |
||
187 | mov eax, 2 |
||
188 | jmp error_with_code |
||
189 | |||
190 | check_mutex: |
||
3126 | clevermous | 191 | cmp dword [mst.mutex], 0 |
2288 | clevermous | 192 | jnz @f |
193 | ret |
||
194 | @@: |
||
195 | mov eax, 3 |
||
196 | jmp error_with_code |
||
197 | |||
198 | check_range: |
||
199 | cmp eax, buffer |
||
200 | jb @f |
||
201 | cmp eax, buffer+bufsize |
||
202 | jae @f |
||
203 | ret |
||
204 | @@: |
||
205 | mov eax, 4 |
||
206 | jmp error_with_code |
||
207 | |||
208 | memory_destroyed: |
||
209 | mov eax, 5 |
||
210 | jmp error_with_code |
||
211 | |||
3126 | clevermous | 212 | edi_destroyed: |
213 | mov eax, 6 |
||
214 | jmp error_with_code |
||
215 | |||
2288 | clevermous | 216 | error_with_code: |
217 | mov edx, saved_state_num |
||
218 | ; eax = error code |
||
219 | ; 1 signals error in testing code (wrong bufsize) |
||
220 | ; 2 = malloc() returned NULL |
||
221 | ; 3 = mutex not released |
||
222 | ; 4 = weird returned value from malloc() |
||
223 | ; 5 = memory destroyed by malloc() or free() |
||
224 | int3 ; simplest way to report error |
||
225 | jmp $-1 ; just in case |
||
226 | |||
227 | ; Include main heap code |
||
7133 | dunkaist | 228 | include '../macros.inc' |
2288 | clevermous | 229 | include '../proc32.inc' |
3126 | clevermous | 230 | include '../struct.inc' |
2288 | clevermous | 231 | include '../const.inc' |
232 | include 'malloc.inc' |
||
233 | |||
234 | i_end: |
||
235 | |||
236 | align 4 |
||
237 | zerostart: |
||
238 | mst MEM_STATE |
||
239 | |||
240 | align 16 |
||
241 | bufsize = 0x40000 ; change if malloc.inc changes |
||
242 | buffer rb bufsize |
||
243 | zeroend: |
||
244 | |||
245 | saved_state_num dd ? |
||
246 | saved_state rd 0x10000 |
||
247 | |||
248 | align 4 |
||
249 | rb 0x10000 ; for stack |
||
250 | mem: |