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