Subversion Repositories Kolibri OS

Rev

Rev 9715 | 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
; Error handlers
178
error1:
179
        mov     eax, 1
180
        jmp     error_with_code
181
 
182
generic_malloc_fail:
183
        mov     eax, 2
184
        jmp     error_with_code
185
 
186
check_mutex:
3126 clevermous 187
        cmp     dword [mst.mutex], 0
2288 clevermous 188
        jnz     @f
189
        ret
190
@@:
191
        mov     eax, 3
192
        jmp     error_with_code
193
 
194
check_range:
195
        cmp     eax, buffer
196
        jb      @f
197
        cmp     eax, buffer+bufsize
198
        jae     @f
199
        ret
200
@@:
201
        mov     eax, 4
202
        jmp     error_with_code
203
 
204
memory_destroyed:
205
        mov     eax, 5
206
        jmp     error_with_code
207
 
3126 clevermous 208
edi_destroyed:
209
        mov     eax, 6
210
        jmp     error_with_code
211
 
2288 clevermous 212
error_with_code:
213
        mov     edx, saved_state_num
214
; eax = error code
215
; 1 signals error in testing code (wrong bufsize)
216
; 2 = malloc() returned NULL
217
; 3 = mutex not released
218
; 4 = weird returned value from malloc()
219
; 5 = memory destroyed by malloc() or free()
220
        int3    ; simplest way to report error
221
        jmp     $-1     ; just in case
222
 
223
; Include main heap code
7133 dunkaist 224
include '../macros.inc'
2288 clevermous 225
include '../proc32.inc'
3126 clevermous 226
include '../struct.inc'
2288 clevermous 227
include '../const.inc'
228
include 'malloc.inc'
229
 
230
i_end:
231
 
232
align 4
233
zerostart:
234
mst     MEM_STATE
235
 
236
align 16
237
bufsize = 0x40000       ; change if malloc.inc changes
238
buffer  rb      bufsize
239
zeroend:
240
 
241
saved_state_num dd      ?
242
saved_state     rd      0x10000
243
 
244
align 4
245
        rb      0x10000 ; for stack
246
mem: