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