mirror of
https://github.com/KolibriOS/kolibrios.git
synced 2024-12-24 23:56:49 +03:00
part of main system dll, not usable yet
git-svn-id: svn://kolibrios.org@5195 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
parent
3054442804
commit
50b2538b02
2
programs/system/os/Tupfile.lua
Normal file
2
programs/system/os/Tupfile.lua
Normal file
@ -0,0 +1,2 @@
|
||||
if tup.getconfig("NO_FASM") ~= "" then return end
|
||||
tup.rule("kolibri.asm", "fasm %f %o " .. tup.getconfig("KPACK_CMD"), "kolibri.dll")
|
66
programs/system/os/export.inc
Normal file
66
programs/system/os/export.inc
Normal file
@ -0,0 +1,66 @@
|
||||
|
||||
; Macroinstruction for making export section
|
||||
|
||||
macro export dllname,[label,string]
|
||||
{ common
|
||||
local module,addresses,names,ordinal,count
|
||||
count = 0
|
||||
forward
|
||||
count = count+1
|
||||
common
|
||||
dd 0,0,0,RVA module,1
|
||||
dd count,count,RVA addresses,RVA names,RVA ordinal
|
||||
addresses:
|
||||
forward
|
||||
dd RVA label
|
||||
common
|
||||
names:
|
||||
forward
|
||||
local name
|
||||
dd RVA name
|
||||
common
|
||||
ordinal: count = 0
|
||||
forward
|
||||
dw count
|
||||
count = count+1
|
||||
common
|
||||
module db dllname,0
|
||||
forward
|
||||
name db string,0
|
||||
common
|
||||
local x,y,z,str1,str2,v1,v2
|
||||
x = count shr 1
|
||||
while x > 0
|
||||
y = x
|
||||
while y < count
|
||||
z = y
|
||||
while z-x >= 0
|
||||
load v1 dword from names+z*4
|
||||
str1=($-RVA $)+v1
|
||||
load v2 dword from names+(z-x)*4
|
||||
str2=($-RVA $)+v2
|
||||
while v1 > 0
|
||||
load v1 from str1+%-1
|
||||
load v2 from str2+%-1
|
||||
if v1 <> v2
|
||||
break
|
||||
end if
|
||||
end while
|
||||
if v1<v2
|
||||
load v1 dword from names+z*4
|
||||
load v2 dword from names+(z-x)*4
|
||||
store dword v1 at names+(z-x)*4
|
||||
store dword v2 at names+z*4
|
||||
load v1 word from ordinal+z*2
|
||||
load v2 word from ordinal+(z-x)*2
|
||||
store word v1 at ordinal+(z-x)*2
|
||||
store word v2 at ordinal+z*2
|
||||
else
|
||||
break
|
||||
end if
|
||||
z = z-x
|
||||
end while
|
||||
y = y+1
|
||||
end while
|
||||
x = x shr 1
|
||||
end while }
|
24
programs/system/os/fpo.inc
Normal file
24
programs/system/os/fpo.inc
Normal file
@ -0,0 +1,24 @@
|
||||
; Magic for proc32.inc: prologue/epilogue for esp-based addressing
|
||||
; fpo stands from Frame Pointer Omission
|
||||
macro fpo_prologue procname,flag,parmbytes,localbytes,reglist
|
||||
{
|
||||
local loc,regcount
|
||||
loc = (localbytes+3) and (not 3)
|
||||
regcount = 0
|
||||
irps reg, reglist
|
||||
\{
|
||||
regcount = regcount+1
|
||||
push reg
|
||||
\}
|
||||
parmbase@proc equ esp+4+regcount*4
|
||||
localbase@proc equ esp-localbytes
|
||||
}
|
||||
macro fpo_epilogue procname,flag,parmbytes,localbytes,reglist
|
||||
{
|
||||
irps reg, reglist \{ reverse pop reg \}
|
||||
if flag and 10000b
|
||||
retn
|
||||
else
|
||||
retn parmbytes
|
||||
end if
|
||||
}
|
144
programs/system/os/kolibri.asm
Normal file
144
programs/system/os/kolibri.asm
Normal file
@ -0,0 +1,144 @@
|
||||
format PE DLL GUI 0.8 at 7FF00000h
|
||||
entry start
|
||||
include '../../struct.inc'
|
||||
include '../../proc32.inc'
|
||||
include 'fpo.inc'
|
||||
include 'export.inc'
|
||||
include 'pe.inc'
|
||||
section '.text' code readable executable
|
||||
|
||||
FS_STACK_MAX equ dword [fs:4]
|
||||
FS_STACK_MIN equ dword [fs:8]
|
||||
FS_SELF_PTR equ dword [fs:0x18]
|
||||
FS_PROCESS_DATA equ dword [fs:0x30]
|
||||
FS_ERRNO equ dword [fs:0x34]
|
||||
FS_SYSCALL_PTR equ dword [fs:0xC0]
|
||||
|
||||
ENOMEM = 12
|
||||
|
||||
DLL_PROCESS_DETACH = 0
|
||||
DLL_PROCESS_ATTACH = 1
|
||||
DLL_THREAD_ATTACH = 2
|
||||
DLL_THREAD_DETACH = 3
|
||||
|
||||
SYSCALL_METHOD_I40 = 1
|
||||
SYSCALL_METHOD_SYSENTER = 2
|
||||
SYSCALL_METHOD_SYSCALL = 3
|
||||
|
||||
; Pointer to this structure is passed as the third argument
|
||||
; to 'start' procedure by the kernel.
|
||||
struct kernel_init_data
|
||||
version dw ?
|
||||
flags dw ?
|
||||
syscall_method dd ?
|
||||
; either one of SYSCALL_METHOD_xxx or pointer to procedure
|
||||
exe_base dd ?
|
||||
stack_base dd ?
|
||||
stack_size dd ?
|
||||
exe_path dd ?
|
||||
command_line dd ?
|
||||
ends
|
||||
|
||||
include 'malloc.inc'
|
||||
|
||||
proc syscall_int40
|
||||
int 0x40
|
||||
ret
|
||||
endp
|
||||
|
||||
proc kercall
|
||||
jmp FS_SYSCALL_PTR
|
||||
endp
|
||||
|
||||
prologue@proc equ fpo_prologue
|
||||
epilogue@proc equ fpo_epilogue
|
||||
|
||||
proc start stdcall, dll_base, reason, reserved
|
||||
; 1. Do nothing unless called by the kernel for DLL_PROCESS_ATTACH.
|
||||
cmp [reason], DLL_PROCESS_ATTACH
|
||||
jnz .nothing
|
||||
; 2. Validate version of the init struct.
|
||||
; If not known, say a debug message and die.
|
||||
mov ebp, [reserved]
|
||||
cmp [ebp+kernel_init_data.version], 1
|
||||
jnz .version_mismatch
|
||||
; 3. Setup common data based on the init struct.
|
||||
mov eax, [ebp+kernel_init_data.stack_base]
|
||||
mov FS_STACK_MIN, eax
|
||||
add eax, [ebp+kernel_init_data.stack_size]
|
||||
mov FS_STACK_MAX, eax
|
||||
mov eax, [ebp+kernel_init_data.syscall_method]
|
||||
cmp eax, 0x10000
|
||||
jae @f
|
||||
mov eax, syscall_int40
|
||||
@@:
|
||||
mov FS_SYSCALL_PTR, eax
|
||||
; 4. Initialize the process heap.
|
||||
mov eax, [ebp+kernel_init_data.exe_base]
|
||||
mov edx, [eax+STRIPPED_PE_HEADER.SizeOfHeapReserve]
|
||||
cmp word [eax], 'MZ'
|
||||
jnz @f
|
||||
add eax, [eax+IMAGE_DOS_HEADER.e_lfanew]
|
||||
mov edx, [eax+IMAGE_NT_HEADERS.OptionalHeader.SizeOfHeapReserve]
|
||||
@@:
|
||||
malloc_init
|
||||
; ...TBD...
|
||||
; Call exe entry point.
|
||||
mov eax, [ebp+kernel_init_data.exe_base]
|
||||
mov edx, [eax+STRIPPED_PE_HEADER.AddressOfEntryPoint]
|
||||
cmp word [eax], 'MZ'
|
||||
jnz @f
|
||||
mov ecx, [eax+IMAGE_DOS_HEADER.e_lfanew]
|
||||
add ecx, eax
|
||||
mov edx, [ecx+IMAGE_NT_HEADERS.OptionalHeader.AddressOfEntryPoint]
|
||||
@@:
|
||||
add edx, eax
|
||||
call edx
|
||||
; If exe entry point has returned control, die.
|
||||
mov eax, -1
|
||||
call FS_SYSCALL_PTR
|
||||
.version_mismatch:
|
||||
mov esi, version_mismatch_msg
|
||||
mov eax, 63
|
||||
mov ebx, 1
|
||||
@@:
|
||||
mov cl, [esi]
|
||||
test cl, cl
|
||||
jz @f
|
||||
int 0x40 ; can't use FS_SYSCALL_PTR here, it has not yet been set
|
||||
inc esi
|
||||
jmp @b
|
||||
@@:
|
||||
mov eax, -1
|
||||
int 0x40
|
||||
.nothing:
|
||||
ret
|
||||
endp
|
||||
|
||||
align 4
|
||||
data export
|
||||
export 'kolibri.dll' \
|
||||
, kercall, 'kercall' \
|
||||
, malloc, 'malloc' \
|
||||
, free, 'free' \
|
||||
, calloc, 'calloc' \
|
||||
, realloc, 'realloc' \
|
||||
, realloc_in_place, 'realloc_in_place' \
|
||||
, memalign, 'memalign' \
|
||||
, create_mspace, 'create_mspace' \
|
||||
, destroy_mspace, 'destroy_mspace' \
|
||||
, mspace_malloc, 'mspace_malloc' \
|
||||
, mspace_free, 'mspace_free' \
|
||||
, mspace_calloc, 'mspace_calloc' \
|
||||
, mspace_realloc, 'mspace_realloc' \
|
||||
, mspace_realloc_in_place, 'mspace_realloc_in_place' \
|
||||
, mspace_memalign, 'mspace_memalign' \
|
||||
|
||||
end data
|
||||
|
||||
version_mismatch_msg db 'Version mismatch between kernel and kolibri.dll',13,10,0
|
||||
|
||||
if FOOTERS
|
||||
section '.data' data readable writable
|
||||
malloc_magic dd ?
|
||||
end if
|
339
programs/system/os/malloc.inc
Normal file
339
programs/system/os/malloc.inc
Normal file
@ -0,0 +1,339 @@
|
||||
; System allocator.
|
||||
; Based on dlmalloc 2.8.6.
|
||||
; dlmalloc is written by Doug Lea and released to the public domain.
|
||||
|
||||
; Algorithms are the same as in dlmalloc, with the following differences:
|
||||
; * segment management uses large segments,
|
||||
; since segments can never be merged;
|
||||
; * top chunk is usually large, so the code tries mmap
|
||||
; for chunks with size >= mmap_threshold before allocating from top;
|
||||
; * there is additional bookkeeping for releasing physical memory
|
||||
; instead of relying on unmapping entire segments:
|
||||
; tree chunks have additional field in the end,
|
||||
; all recently expanded tree chunks are linked in one list for sys_trim;
|
||||
; * there is an additional list of all mmapped chunks,
|
||||
; so that mspace_destroy can free everything, including mmapped chunks;
|
||||
; * realloc and memalign can give back a space before a free chunk
|
||||
; (extending that chunk) even if a space is less than minimal chunk size.
|
||||
|
||||
; Statistics:
|
||||
; Alignment: 8 bytes
|
||||
; Minimum overhead per allocated chunk: 4 or 8 bytes,
|
||||
; depending on whether FOOTERS is defined.
|
||||
; Minimum allocated size: 16 bytes (including overhead)
|
||||
; See details at http://gee.cs.oswego.edu/dl/html/malloc.html.
|
||||
|
||||
; The KolibriOS kernel provides functions similar to mmap/mremap/munmap,
|
||||
; they are used as base for allocations.
|
||||
|
||||
FOOTERS = 0
|
||||
; If true, provide extra checking and dispatching by placing
|
||||
; information in the footers of allocated chunks. This adds
|
||||
; space and time overhead, but can be useful for debugging.
|
||||
|
||||
DEFAULT_MMAP_THRESHOLD = 256*1024
|
||||
; The request size threshold for using MMAP to directly service a
|
||||
; request. Requests of at least this size that cannot be allocated
|
||||
; using already-existing space will be serviced via mmap. (If enough
|
||||
; normal freed space already exists it is used instead.) Using mmap
|
||||
; segregates relatively large chunks of memory so that they can be
|
||||
; individually obtained and released from the host system. A request
|
||||
; serviced through mmap is never reused by any other request (at least
|
||||
; not directly; the system may just so happen to remap successive
|
||||
; requests to the same locations). Segregating space in this way has
|
||||
; the benefits that: Mmapped space can always be individually released
|
||||
; back to the system, which helps keep the system level memory demands
|
||||
; of a long-lived program low. Also, mapped memory doesn't become
|
||||
; `locked' between other chunks, as can happen with normally allocated
|
||||
; chunks, which means that even trimming via malloc_trim would not
|
||||
; release them. However, it has the disadvantage that the space
|
||||
; cannot be reclaimed, consolidated, and then used to service later
|
||||
; requests, as happens with normal chunks. The advantages of mmap
|
||||
; nearly always outweigh disadvantages for "large" chunks, but the
|
||||
; value of "large" may vary across systems. The default is an
|
||||
; empirically derived value that works well in most systems. You can
|
||||
; disable mmap by setting to 0xFFFFFFFF.
|
||||
|
||||
RELEASE_CHECK_RATE = 64
|
||||
; The number of consolidated frees between checks to release
|
||||
; unused segments when freeing. When using non-contiguous segments,
|
||||
; especially with multiple mspaces, checking only for topmost space
|
||||
; doesn't always suffice to trigger trimming. To compensate for this,
|
||||
; free() will, with a period of MAX_RELEASE_CHECK_RATE (or the
|
||||
; current number of segments, if greater) try to release unused
|
||||
; segments to the OS when freeing chunks that result in
|
||||
; consolidation. The best value for this parameter is a compromise
|
||||
; between slowing down frees with relatively costly checks that
|
||||
; rarely trigger versus holding on to unused memory. To effectively
|
||||
; disable, set to MAX_SIZE_T. This may lead to a very slight speed
|
||||
; improvement at the expense of carrying around more memory.
|
||||
|
||||
DEFAULT_MSPACE_SIZE = 1024*1024
|
||||
|
||||
include 'malloc_internal.inc'
|
||||
|
||||
prologue@proc equ fpo_prologue
|
||||
epilogue@proc equ fpo_epilogue
|
||||
|
||||
; void* create_mspace(size_t capacity, int locked)
|
||||
; create_mspace creates and returns a new independent space with the
|
||||
; given initial capacity, or, if 0, the default mspace size. It
|
||||
; returns null if there is no system memory available to create the
|
||||
; space. If argument locked is non-zero, the space uses a separate
|
||||
; lock to control access. The capacity of the space will grow
|
||||
; dynamically as needed to service mspace_malloc requests.
|
||||
proc create_mspace stdcall uses ebx, capacity, locked
|
||||
do_create_mspace
|
||||
endp
|
||||
|
||||
; void destroy_mspace(mspace msp)
|
||||
; destroy_mspace destroys the given space, and attempts to return all
|
||||
; of its memory back to the system, returning the total number of
|
||||
; bytes freed. After destruction, the results of access to all memory
|
||||
; used by the space become undefined.
|
||||
proc destroy_mspace stdcall uses ebx, msp
|
||||
do_destroy_mspace
|
||||
endp
|
||||
|
||||
|
||||
macro set_default_heap
|
||||
{
|
||||
mov ebp, FS_PROCESS_DATA
|
||||
mov ebp, [ebp+0x18]
|
||||
.got_mspace:
|
||||
}
|
||||
|
||||
macro set_explicit_heap
|
||||
{
|
||||
mov ebp, [msp]
|
||||
}
|
||||
|
||||
macro mspace_adapter common_label
|
||||
{
|
||||
mov eax, [esp]
|
||||
mov [esp], ebp
|
||||
mov ebp, [esp+4]
|
||||
mov [esp+4], eax
|
||||
push ebx
|
||||
push esi
|
||||
jmp common_label
|
||||
}
|
||||
|
||||
; void* malloc(size_t bytes)
|
||||
; Returns a pointer to a newly allocated chunk of at least n bytes, or
|
||||
; null if no space is available, in which case errno is set to ENOMEM
|
||||
; on ANSI C systems.
|
||||
;
|
||||
; If n is zero, malloc returns a minimum-sized chunk. (The minimum
|
||||
; size is 16 bytes on most 32bit systems, and 32 bytes on 64bit
|
||||
; systems.) Note that size_t is an unsigned type, so calls with
|
||||
; arguments that would be negative if signed are interpreted as
|
||||
; requests for huge amounts of space, which will often fail. The
|
||||
; maximum supported value of n differs across systems, but is in all
|
||||
; cases less than the maximum representable value of a size_t.
|
||||
align 16
|
||||
proc malloc stdcall uses ebp ebx esi, bytes
|
||||
set_default_heap
|
||||
do_malloc
|
||||
endp
|
||||
|
||||
; void free(void* mem)
|
||||
; Releases the chunk of memory pointed to by mem, that had been previously
|
||||
; allocated using malloc or a related routine such as realloc.
|
||||
; It has no effect if mem is null. If mem was not malloced or already
|
||||
; freed, free(mem) will by default cause the current program to abort.
|
||||
align 16
|
||||
proc free stdcall uses ebp ebx esi, mem
|
||||
set_default_heap
|
||||
do_free
|
||||
endp
|
||||
|
||||
; void* calloc(size_t n_elements, size_t elem_size);
|
||||
; Returns a pointer to n_elements * elem_size bytes, with all locations
|
||||
; set to zero.
|
||||
align 16
|
||||
proc calloc stdcall, n_elements, elem_size
|
||||
do_calloc <stdcall malloc,eax>
|
||||
endp
|
||||
|
||||
; void* realloc(void* oldmem, size_t bytes)
|
||||
; Returns a pointer to a chunk of size bytes that contains the same data
|
||||
; as does chunk oldmem up to the minimum of (bytes, oldmem's size) bytes, or null
|
||||
; if no space is available.
|
||||
;
|
||||
; The returned pointer may or may not be the same as oldmem. The algorithm
|
||||
; prefers extending oldmem in most cases when possible, otherwise it
|
||||
; employs the equivalent of a malloc-copy-free sequence.
|
||||
;
|
||||
; If oldmem is null, realloc is equivalent to malloc.
|
||||
;
|
||||
; If space is not available, realloc returns null, errno is set (if on
|
||||
; ANSI) and oldmem is NOT freed.
|
||||
;
|
||||
; if bytes is for fewer bytes than already held by oldmem, the newly unused
|
||||
; space is lopped off and freed if possible. realloc with a size
|
||||
; argument of zero (re)allocates a minimum-sized chunk.
|
||||
;
|
||||
; The old unix realloc convention of allowing the last-free'd chunk
|
||||
; to be used as an argument to realloc is not supported.
|
||||
align 16
|
||||
proc realloc stdcall uses ebp ebx esi, oldmem, bytes
|
||||
set_default_heap
|
||||
if used mspace_realloc
|
||||
do_realloc <stdcall mspace_malloc,ebp,>, <stdcall mspace_free,ebp,>
|
||||
else
|
||||
do_realloc <stdcall malloc,>, <stdcall free,>
|
||||
end if
|
||||
endp
|
||||
|
||||
; void* realloc_in_place(void* oldmem, size_t bytes)
|
||||
; Resizes the space allocated for oldmem to size bytes, only if this can be
|
||||
; done without moving oldmem (i.e., only if there is adjacent space
|
||||
; available if bytes is greater than oldmem's current allocated size, or bytes is
|
||||
; less than or equal to oldmem's size). This may be used instead of plain
|
||||
; realloc if an alternative allocation strategy is needed upon failure
|
||||
; to expand space; for example, reallocation of a buffer that must be
|
||||
; memory-aligned or cleared. You can use realloc_in_place to trigger
|
||||
; these alternatives only when needed.
|
||||
;
|
||||
; Returns oldmem if successful; otherwise null.
|
||||
align 16
|
||||
proc realloc_in_place stdcall uses ebp ebx esi, oldmem, bytes
|
||||
set_default_heap
|
||||
do_realloc_in_place
|
||||
endp
|
||||
|
||||
; void* memalign(size_t alignment, size_t bytes);
|
||||
; Returns a pointer to a newly allocated chunk of bytes argument, aligned
|
||||
; in accord with the alignment argument.
|
||||
;
|
||||
; The alignment argument should be a power of two. If the argument is
|
||||
; not a power of two, the nearest greater power is used.
|
||||
; 8-byte alignment is guaranteed by normal malloc calls, so don't
|
||||
; bother calling memalign with an argument of 8 or less.
|
||||
;
|
||||
; Overreliance on memalign is a sure way to fragment space.
|
||||
align 16
|
||||
proc memalign stdcall uses ebp ebx esi, alignment, bytes
|
||||
set_default_heap
|
||||
if used mspace_memalign
|
||||
do_memalign <stdcall mspace_malloc,ebp,>
|
||||
else
|
||||
do_memalign <stdcall malloc,>
|
||||
end if
|
||||
endp
|
||||
|
||||
; void* mspace_malloc(mspace msp, size_t bytes)
|
||||
; mspace_malloc behaves as malloc, but operates within
|
||||
; the given space.
|
||||
align 16
|
||||
proc mspace_malloc ;stdcall uses ebp ebx esi, msp, bytes
|
||||
; set_explicit_heap
|
||||
; do_malloc
|
||||
mspace_adapter malloc.got_mspace
|
||||
endp
|
||||
|
||||
; void mspace_free(mspace msp, void* mem)
|
||||
; mspace_free behaves as free, but operates within
|
||||
; the given space.
|
||||
align 16
|
||||
proc mspace_free ;stdcall uses ebp ebx esi, msp, mem
|
||||
; set_explicit_heap
|
||||
; do_free
|
||||
mspace_adapter free.got_mspace
|
||||
endp
|
||||
|
||||
; void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size)
|
||||
; mspace_calloc behaves as calloc, but operates within
|
||||
; the given space.
|
||||
align 16
|
||||
proc mspace_calloc stdcall, msp, n_elements, elem_size
|
||||
do_calloc <stdcall mspace_malloc,[msp+4],eax>
|
||||
endp
|
||||
|
||||
; void* mspace_realloc(mspace msp, void* oldmem, size_t bytes)
|
||||
; mspace_realloc behaves as realloc, but operates within
|
||||
; the given space.
|
||||
align 16
|
||||
proc mspace_realloc ;stdcall uses ebp ebx esi, msp, oldmem, bytes
|
||||
; set_explicit_heap
|
||||
; do_realloc <stdcall mspace_malloc,ebp,>, <stdcall mspace_free,ebp,>
|
||||
mspace_adapter realloc.got_mspace
|
||||
endp
|
||||
|
||||
; void* mspace_realloc_in_place(mspace msp, void* oldmem, size_t bytes)
|
||||
align 16
|
||||
proc mspace_realloc_in_place ;stdcall uses ebp ebx esi, msp, oldmem, bytes
|
||||
; set_explicit_heap
|
||||
; do_realloc_in_place
|
||||
mspace_adapter realloc_in_place.got_mspace
|
||||
endp
|
||||
|
||||
; void* mspace_memalign(mspace msp, size_t alignment, size_t bytes)
|
||||
; mspace_memalign behaves as memalign, but operates within
|
||||
; the given space.
|
||||
align 16
|
||||
proc mspace_memalign ;stdcall uses ebp ebx esi, msp, alignment, bytes
|
||||
; set_explicit_heap
|
||||
; do_memalign <stdcall mspace_malloc,ebp,>
|
||||
mspace_adapter memalign.got_mspace
|
||||
endp
|
||||
|
||||
assert MALLOC_ALIGNMENT >= 8
|
||||
assert MALLOC_ALIGNMENT and (MALLOC_ALIGNMENT - 1) = 0
|
||||
assert MCHUNK_SIZE and (MCHUNK_SIZE - 1) = 0
|
||||
; in: edx = initial size of the process heap
|
||||
macro malloc_init
|
||||
{
|
||||
if FOOTERS
|
||||
mov eax, 26
|
||||
mov ebx, 9
|
||||
call FS_SYSCALL_PTR
|
||||
xor eax, 0x55555555
|
||||
or eax, 8
|
||||
and eax, not 7
|
||||
mov [malloc_magic], eax
|
||||
end if
|
||||
stdcall create_mspace, edx, 1
|
||||
mov ecx, FS_PROCESS_DATA
|
||||
mov [ecx+0x18], eax
|
||||
}
|
||||
|
||||
proc heap_corrupted
|
||||
sub esp, 400h
|
||||
mov eax, 9
|
||||
mov ebx, esp
|
||||
or ecx, -1
|
||||
call FS_SYSCALL_PTR
|
||||
lea esi, [ebx+10]
|
||||
lea edx, [ebx+10+11]
|
||||
mov eax, 63
|
||||
mov ebx, 1
|
||||
mov cl, '['
|
||||
call FS_SYSCALL_PTR
|
||||
@@:
|
||||
mov cl, [esi]
|
||||
test cl, cl
|
||||
jz @f
|
||||
call FS_SYSCALL_PTR
|
||||
inc esi
|
||||
cmp esi, ebx
|
||||
jb @b
|
||||
@@:
|
||||
mov esi, heap_corrupted_msg
|
||||
@@:
|
||||
mov cl, [esi]
|
||||
inc esi
|
||||
test cl, cl
|
||||
jz @f
|
||||
mov eax, 63
|
||||
mov ebx, 1
|
||||
call FS_SYSCALL_PTR
|
||||
jmp @b
|
||||
@@:
|
||||
or eax, -1
|
||||
or ebx, -1
|
||||
call FS_SYSCALL_PTR
|
||||
endp
|
||||
|
||||
heap_corrupted_msg db '] Heap corrupted, aborting',13,10,0
|
2556
programs/system/os/malloc_internal.inc
Normal file
2556
programs/system/os/malloc_internal.inc
Normal file
File diff suppressed because it is too large
Load Diff
448
programs/system/os/malloc_test.asm
Normal file
448
programs/system/os/malloc_test.asm
Normal file
@ -0,0 +1,448 @@
|
||||
format PE console 4.0
|
||||
entry start
|
||||
|
||||
include 'win32a.inc'
|
||||
include '../../struct.inc'
|
||||
include '../../proc32.inc'
|
||||
include 'fpo.inc'
|
||||
|
||||
FS_ERRNO equ dword [errno]
|
||||
ENOMEM = 12
|
||||
include 'malloc.inc'
|
||||
|
||||
start:
|
||||
cinvoke fopen, logfile_name, logfile_mode
|
||||
mov [logfile], eax
|
||||
mov edx, 1 ;shl 25
|
||||
malloc_init
|
||||
call run_test
|
||||
set_default_heap
|
||||
stdcall destroy_mspace, ebp
|
||||
cinvoke fclose, [logfile]
|
||||
ret
|
||||
|
||||
FS_SYSCALL_PTR:
|
||||
cmp eax, 68
|
||||
jnz unknown_syscall
|
||||
cmp ebx, 12
|
||||
jz syscall_malloc
|
||||
cmp ebx, 13
|
||||
jz syscall_free
|
||||
cmp ebx, 20
|
||||
jz syscall_realloc
|
||||
cmp ebx, 26
|
||||
jz syscall_trim
|
||||
|
||||
unknown_syscall:
|
||||
int3
|
||||
jmp $
|
||||
|
||||
syscall_malloc:
|
||||
push ecx edx
|
||||
invoke VirtualAlloc, 0, ecx, MEM_COMMIT, PAGE_READWRITE
|
||||
pop edx ecx
|
||||
ret
|
||||
syscall_free:
|
||||
push ecx edx
|
||||
invoke VirtualFree, ecx, 0, MEM_RELEASE
|
||||
test eax, eax
|
||||
jz @f
|
||||
pop edx ecx
|
||||
ret
|
||||
@@:
|
||||
int3
|
||||
jmp $
|
||||
syscall_realloc:
|
||||
push esi edi
|
||||
push ecx edx
|
||||
mov esi, edx
|
||||
call syscall_malloc
|
||||
mov edi, eax
|
||||
sub esp, 1Ch
|
||||
mov edx, esp
|
||||
invoke VirtualQuery, esi, edx, 1Ch
|
||||
mov ecx, [esp+0Ch]
|
||||
add esp, 1Ch
|
||||
cmp ecx, [esp+4]
|
||||
jb @f
|
||||
mov ecx, [esp+4]
|
||||
@@:
|
||||
shr ecx, 2
|
||||
push esi edi
|
||||
rep movsd
|
||||
pop edi ecx
|
||||
call syscall_free
|
||||
mov eax, edi
|
||||
pop edx ecx
|
||||
pop edi esi
|
||||
ret
|
||||
syscall_trim:
|
||||
push eax ecx edi
|
||||
lea edi, [ecx+edx]
|
||||
mov ecx, esi
|
||||
shr ecx, 2
|
||||
xor eax, eax
|
||||
rep stosd
|
||||
pop edi ecx eax
|
||||
ret
|
||||
|
||||
macro next_random
|
||||
{
|
||||
imul edi, 1103515245
|
||||
add edi, 12345
|
||||
}
|
||||
|
||||
macro call_and_check_regs what
|
||||
{
|
||||
push ebx edi
|
||||
what
|
||||
cmp edi, [esp]
|
||||
jnz edi_destroyed
|
||||
cmp ebx, [esp+4]
|
||||
jnz ebx_destroyed
|
||||
add esp, 8
|
||||
}
|
||||
|
||||
get_malloc_size:
|
||||
and eax, 1023
|
||||
jnz @f
|
||||
next_random
|
||||
mov eax, edi
|
||||
shr eax, 16
|
||||
shl eax, 8
|
||||
@@:
|
||||
ret
|
||||
|
||||
get_and_validate_memory:
|
||||
xor edx, edx
|
||||
div esi
|
||||
mov eax, [esp+edx*8+4]
|
||||
mov ecx, [esp+edx*8+8]
|
||||
push edi eax
|
||||
mov edi, eax
|
||||
mov al, [edi]
|
||||
repz scasb
|
||||
jnz memory_destroyed
|
||||
pop ecx edi
|
||||
ret
|
||||
|
||||
run_test:
|
||||
; 65536 times run random operation.
|
||||
; Randomly select malloc(random size from 1 to 1023 or from 256 to 16M),
|
||||
; free(random of previously allocated areas),
|
||||
; realloc(random of previously allocated areas, random size from 1 to 1023 or from 256 to 16M),
|
||||
; realloc_in_place(<same as realloc>),
|
||||
; memalign(random size from 1 to 1023 or from 256 to 16M, random power of 2 from 8 to 1024)
|
||||
mov edi, 0x12345678
|
||||
xor esi, esi ; 0 areas allocated
|
||||
mov ebx, 65536
|
||||
.loop:
|
||||
; call validate_release_chain
|
||||
next_random
|
||||
mov eax, edi
|
||||
shr eax, 16
|
||||
mov ecx, eax
|
||||
shr eax, 3
|
||||
and ecx, 7
|
||||
jz .memalign
|
||||
dec ecx
|
||||
jz .realloc_in_place
|
||||
dec ecx
|
||||
jz .realloc
|
||||
test ebx, 64
|
||||
jz .prefer_free
|
||||
.prefer_malloc:
|
||||
dec ecx
|
||||
jz .free
|
||||
jmp .malloc
|
||||
.prefer_free:
|
||||
dec ecx
|
||||
jnz .free
|
||||
.malloc:
|
||||
call get_malloc_size
|
||||
jz .loop
|
||||
push eax
|
||||
call_and_check_regs <stdcall malloc,eax>
|
||||
pop ecx
|
||||
pushad
|
||||
cinvoke fprintf, [logfile], malloc_str, ecx, eax
|
||||
popad
|
||||
test eax, eax
|
||||
jz generic_malloc_failure
|
||||
inc esi
|
||||
push ecx eax
|
||||
push edi
|
||||
mov edi, eax
|
||||
mov eax, esi
|
||||
rep stosb
|
||||
pop edi
|
||||
jmp .common
|
||||
.free:
|
||||
test esi, esi
|
||||
jz .loop
|
||||
call get_and_validate_memory
|
||||
push edx
|
||||
pushad
|
||||
cinvoke fprintf, [logfile], free_str, ecx
|
||||
popad
|
||||
call_and_check_regs <stdcall free,ecx>
|
||||
; call validate_release_chain
|
||||
pop edx
|
||||
dec esi
|
||||
pop eax ecx
|
||||
push edi
|
||||
lea edi, [esp+4]
|
||||
@@:
|
||||
dec edx
|
||||
js @f
|
||||
xchg eax, [edi]
|
||||
xchg ecx, [edi+4]
|
||||
add edi, 8
|
||||
jmp @b
|
||||
@@:
|
||||
pop edi
|
||||
jmp .common
|
||||
.realloc:
|
||||
test esi, esi
|
||||
jz .loop
|
||||
call get_and_validate_memory
|
||||
push eax
|
||||
next_random
|
||||
mov eax, edi
|
||||
shr eax, 16
|
||||
call get_malloc_size
|
||||
jnz @f
|
||||
pop eax
|
||||
jmp .loop
|
||||
@@:
|
||||
push eax edx
|
||||
pushad
|
||||
cinvoke fprintf, [logfile], realloc_str1, ecx, eax
|
||||
popad
|
||||
call_and_check_regs <stdcall realloc,ecx,eax>
|
||||
pop edx ecx
|
||||
pushad
|
||||
cinvoke fprintf, [logfile], realloc_str2, eax
|
||||
popad
|
||||
test eax, eax
|
||||
jz generic_malloc_failure
|
||||
push ebx edi ecx
|
||||
mov ebx, [esp+edx*8+20]
|
||||
mov [esp+edx*8+16], eax
|
||||
mov [esp+edx*8+20], ecx
|
||||
cmp ebx, ecx
|
||||
jae @f
|
||||
mov ecx, ebx
|
||||
@@:
|
||||
mov edi, eax
|
||||
mov eax, [esp+12]
|
||||
repz scasb
|
||||
jnz memory_destroyed
|
||||
pop ecx
|
||||
sub ecx, ebx
|
||||
jbe @f
|
||||
rep stosb
|
||||
@@:
|
||||
pop edi ebx eax
|
||||
jmp .common
|
||||
.realloc_in_place:
|
||||
test esi, esi
|
||||
jz .loop
|
||||
call get_and_validate_memory
|
||||
push eax
|
||||
next_random
|
||||
mov eax, edi
|
||||
shr eax, 16
|
||||
call get_malloc_size
|
||||
jnz @f
|
||||
pop eax
|
||||
jmp .loop
|
||||
@@:
|
||||
push eax edx
|
||||
pushad
|
||||
cinvoke fprintf, [logfile], realloc_in_place_str1, ecx, eax
|
||||
popad
|
||||
call_and_check_regs <stdcall realloc_in_place,ecx,eax>
|
||||
pushad
|
||||
cinvoke fprintf, [logfile], realloc_in_place_str2, eax
|
||||
popad
|
||||
pop edx ecx
|
||||
test eax, eax
|
||||
jnz @f
|
||||
pop eax
|
||||
jmp .common
|
||||
@@:
|
||||
cmp [esp+edx*8+4], eax
|
||||
jnz generic_malloc_failure
|
||||
push ebx edi ecx
|
||||
mov ebx, [esp+edx*8+20]
|
||||
mov [esp+edx*8+20], ecx
|
||||
cmp ebx, ecx
|
||||
jae @f
|
||||
mov ecx, ebx
|
||||
@@:
|
||||
mov edi, eax
|
||||
mov eax, [esp+12]
|
||||
repz scasb
|
||||
jnz memory_destroyed
|
||||
pop ecx
|
||||
sub ecx, ebx
|
||||
jbe @f
|
||||
rep stosb
|
||||
@@:
|
||||
pop edi ebx eax
|
||||
jmp .common
|
||||
.memalign:
|
||||
call get_malloc_size
|
||||
jz .loop
|
||||
next_random
|
||||
mov ecx, edi
|
||||
shr ecx, 29
|
||||
mov edx, 8
|
||||
shl edx, cl
|
||||
push eax edx
|
||||
pushad
|
||||
cinvoke fprintf, [logfile], memalign_str1, edx, eax
|
||||
popad
|
||||
call_and_check_regs <stdcall memalign, edx, eax>
|
||||
pushad
|
||||
cinvoke fprintf, [logfile], memalign_str2, eax
|
||||
popad
|
||||
dec dword [esp]
|
||||
test eax, [esp]
|
||||
jnz memalign_invalid
|
||||
add esp, 4
|
||||
pop ecx
|
||||
test eax, eax
|
||||
jz generic_malloc_failure
|
||||
inc esi
|
||||
push ecx eax
|
||||
push edi
|
||||
mov edi, eax
|
||||
mov eax, esi
|
||||
rep stosb
|
||||
pop edi
|
||||
.common:
|
||||
cinvoke fflush, [logfile]
|
||||
dec ebx
|
||||
jnz .loop
|
||||
@@:
|
||||
dec esi
|
||||
js @f
|
||||
pop eax ecx
|
||||
stdcall free, eax
|
||||
jmp @b
|
||||
@@:
|
||||
ret
|
||||
|
||||
generic_malloc_failure:
|
||||
mov eax, 1
|
||||
int3
|
||||
jmp $
|
||||
|
||||
memory_destroyed:
|
||||
mov eax, 2
|
||||
int3
|
||||
jmp $
|
||||
|
||||
edi_destroyed:
|
||||
mov eax, 3
|
||||
int3
|
||||
jmp $
|
||||
|
||||
ebx_destroyed:
|
||||
mov eax, 4
|
||||
int3
|
||||
jmp $
|
||||
|
||||
memalign_invalid:
|
||||
mov eax, 5
|
||||
int3
|
||||
jmp $
|
||||
|
||||
validate_release_chain:
|
||||
push ebx ebp
|
||||
set_default_heap
|
||||
lea ecx, [ebp+malloc_state.release_list-tchunk_release_fd]
|
||||
mov eax, ecx
|
||||
mov edx, [ecx+tchunk_release_fd]
|
||||
@@:
|
||||
cmp [edx+tchunk_release_bk], eax
|
||||
jnz .fail
|
||||
cmp edx, ecx
|
||||
jz @f
|
||||
mov eax, edx
|
||||
mov edx, [edx+tchunk_release_fd]
|
||||
jmp @b
|
||||
@@:
|
||||
lea eax, [ebp-3]
|
||||
add eax, [ebp-4]
|
||||
cmp eax, [ebp+malloc_state.top]
|
||||
jz .ok
|
||||
.chunk_loop:
|
||||
mov ecx, [eax-4]
|
||||
test ecx, CINUSE_BIT
|
||||
jnz .next_chunk
|
||||
cmp ecx, 0x100
|
||||
jb .next_chunk
|
||||
mov edx, ecx
|
||||
and edx, not FLAG_BITS
|
||||
lea edx, [eax+edx]
|
||||
cmp [edx+tchunk_release_fd], edx
|
||||
jnz @f
|
||||
cmp [edx+tchunk_release_bk], edx
|
||||
jnz .fail
|
||||
jmp .next_chunk
|
||||
@@:
|
||||
mov ebx, [ebp+malloc_state.release_list]
|
||||
@@:
|
||||
cmp edx, ebx
|
||||
jz .next_chunk
|
||||
mov ebx, [ebx+tchunk_release_fd]
|
||||
cmp ebx, [ebp+malloc_state.release_list]
|
||||
jnz @b
|
||||
jmp .fail
|
||||
.next_chunk:
|
||||
and ecx, not FLAG_BITS
|
||||
add eax, ecx
|
||||
cmp eax, [ebp+malloc_state.top]
|
||||
jb .chunk_loop
|
||||
ja .fail
|
||||
.ok:
|
||||
pop ebp ebx
|
||||
ret
|
||||
.fail:
|
||||
int3
|
||||
jmp $
|
||||
|
||||
align 4
|
||||
data import
|
||||
library kernel32,'kernel32.dll',msvcrt,'msvcrt.dll'
|
||||
import kernel32,\
|
||||
VirtualAlloc, 'VirtualAlloc', \
|
||||
VirtualFree, 'VirtualFree', \
|
||||
VirtualQuery, 'VirtualQuery'
|
||||
import msvcrt,\
|
||||
fopen,'fopen',\
|
||||
fclose,'fclose',\
|
||||
fprintf,'fprintf',\
|
||||
fflush,'fflush'
|
||||
end data
|
||||
|
||||
malloc_str db 'malloc(0x%X) = 0x%X',10,0
|
||||
free_str db 'free(0x%X)',10,0
|
||||
realloc_str1 db 'realloc(0x%X,0x%X)',0
|
||||
realloc_str2 db ' = 0x%X',10,0
|
||||
realloc_in_place_str1 db 'realloc_in_place(0x%X,0x%X)',0
|
||||
realloc_in_place_str2 db ' = 0x%X',10,0
|
||||
memalign_str1 db 'memalign(0x%X,0x%X)',0
|
||||
memalign_str2 db ' = 0x%X',10,0
|
||||
|
||||
logfile_name db 'test.log',0
|
||||
logfile_mode db 'w',0
|
||||
|
||||
align 4
|
||||
logfile dd ?
|
||||
errno dd ?
|
||||
FS_PROCESS_DATA = process_data
|
||||
process_data rd 1024
|
134
programs/system/os/pe.inc
Normal file
134
programs/system/os/pe.inc
Normal file
@ -0,0 +1,134 @@
|
||||
struct STRIPPED_PE_HEADER
|
||||
Signature dw ?
|
||||
Characteristics dw ?
|
||||
AddressOfEntryPoint dd ?
|
||||
ImageBase dd ?
|
||||
SectionAlignmentLog db ?
|
||||
FileAlignmentLog db ?
|
||||
MajorOSVersion db ?
|
||||
MinorOSVersion db ?
|
||||
SizeOfImage dd ?
|
||||
SizeOfStackReserve dd ?
|
||||
SizeOfHeapReserve dd ?
|
||||
SizeOfHeaders dd ?
|
||||
Subsystem db ?
|
||||
NumberOfRvaAndSizes db ?
|
||||
NumberOfSections dw ?
|
||||
ends
|
||||
STRIPPED_PE_SIGNATURE = 0x4503 ; 'PE' xor 'S'
|
||||
SPE_DIRECTORY_IMPORT = 0
|
||||
SPE_DIRECTORY_EXPORT = 1
|
||||
SPE_DIRECTORY_BASERELOC = 2
|
||||
|
||||
struct IMAGE_DATA_DIRECTORY
|
||||
VirtualAddress dd ?
|
||||
isize dd ?
|
||||
ends
|
||||
|
||||
struct IMAGE_OPTIONAL_HEADER32
|
||||
Magic dw ?
|
||||
MajorLinkerVersion db ?
|
||||
MinorLinkerVersion db ?
|
||||
SizeOfCode dd ?
|
||||
SizeOfInitializedData dd ?
|
||||
SizeOfUninitializedData dd ?
|
||||
AddressOfEntryPoint dd ?
|
||||
BaseOfCode dd ?
|
||||
BaseOfData dd ?
|
||||
ImageBase dd ?
|
||||
SectionAlignment dd ?
|
||||
FileAlignment dd ?
|
||||
MajorOperatingSystemVersion dw ?
|
||||
MinorOperatingSystemVersion dw ?
|
||||
MajorImageVersion dw ?
|
||||
MinorImageVersion dw ?
|
||||
MajorSubsystemVersion dw ?
|
||||
MinorSubsystemVersion dw ?
|
||||
Win32VersionValue dd ?
|
||||
SizeOfImage dd ?
|
||||
SizeOfHeaders dd ?
|
||||
CheckSum dd ?
|
||||
Subsystem dw ?
|
||||
DllCharacteristics dw ?
|
||||
SizeOfStackReserve dd ?
|
||||
SizeOfStackCommit dd ?
|
||||
SizeOfHeapReserve dd ?
|
||||
SizeOfHeapCommit dd ?
|
||||
LoaderFlags dd ?
|
||||
NumberOfDirectories dd ?
|
||||
DataDirectory IMAGE_DATA_DIRECTORY ?
|
||||
Directories rb sizeof.IMAGE_DATA_DIRECTORY*15
|
||||
ends
|
||||
|
||||
struct IMAGE_FILE_HEADER
|
||||
Machine dw ?
|
||||
NumberOfSections dw ?
|
||||
TimeDateStamp dd ?
|
||||
PointerToSymbolTable dd ?
|
||||
NumberOfSymbols dd ?
|
||||
SizeOfOptionalHeader dw ?
|
||||
Characteristics dw ?
|
||||
ends
|
||||
|
||||
struct IMAGE_NT_HEADERS
|
||||
Signature dd ?
|
||||
FileHeader IMAGE_FILE_HEADER
|
||||
OptionalHeader IMAGE_OPTIONAL_HEADER32
|
||||
ends
|
||||
|
||||
struct IMAGE_EXPORT_DIRECTORY
|
||||
Characteristics dd ?
|
||||
TimeDateStamp dd ?
|
||||
MajorVersion dw ?
|
||||
MinorVersion dw ?
|
||||
Name dd ?
|
||||
Base dd ?
|
||||
NumberOfFunctions dd ?
|
||||
NumberOfNames dd ?
|
||||
AddressOfFunctions dd ?
|
||||
AddressOfNames dd ?
|
||||
AddressOfNameOrdinals dd ?
|
||||
ends
|
||||
|
||||
struct IMAGE_IMPORT_DIRECTORY
|
||||
OriginalFirstThunk dd ?
|
||||
TimeDateStamp dd ?
|
||||
ForwarderChain dd ?
|
||||
Name dd ?
|
||||
FirstThunk dd ?
|
||||
ends
|
||||
|
||||
struct IMAGE_DOS_HEADER
|
||||
e_magic dw ?
|
||||
e_cblp dw ?
|
||||
e_cp dw ?
|
||||
e_crlc dw ?
|
||||
e_cparhdr dw ?
|
||||
e_minalloc dw ?
|
||||
e_maxalloc dw ?
|
||||
e_ss dw ?
|
||||
e_sp dw ?
|
||||
e_csum dw ?
|
||||
e_ip dw ?
|
||||
e_cs dw ?
|
||||
e_lfarlc dw ?
|
||||
e_ovno dw ?
|
||||
e_res rw 4
|
||||
e_oemid dw ?
|
||||
e_oeminfo dw ?
|
||||
e_res2 rw 10
|
||||
e_lfanew dd ?
|
||||
ends
|
||||
|
||||
struct IMAGE_SECTION_HEADER
|
||||
Name rb 8
|
||||
VirtualSize dd ?
|
||||
VirtualAddress dd ?
|
||||
SizeOfRawData dd ?
|
||||
OffsetToRawData dd ?
|
||||
OffsetToRelocations dd ?
|
||||
OffsetToLinenumbers dd ?
|
||||
NumberOfRelocations dw ?
|
||||
NumberOfLinenumbers dw ?
|
||||
Characteristics dd ?
|
||||
ends
|
Loading…
Reference in New Issue
Block a user