-
Notifications
You must be signed in to change notification settings - Fork 0
/
malloc.S
76 lines (59 loc) · 2.1 KB
/
malloc.S
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
.intel_syntax noprefix
.section .rodata
error_allocating_memory: .asciz "Error while allocating memory\n"
error_allocating_memory_len: .8byte 30
.section .text
print_memory_error:
mov rax, 1 // write
mov rdi, 1 // Stdout
lea rsi, [rip+error_allocating_memory] // buf
mov rdx, [rip+error_allocating_memory_len] // length
syscall
// Exit with exit code 42
mov rax, 60
mov rdi, 42
syscall
// This is absolutely not malloc, but it is a very minimal memory
// Allocation wrapper. It works by increasing the "program break",
// which is the heap of the program. It returns a pointer to the beginning
// of the section you can write to the number of bytes you requrested in
// rdi. The section returned may be writable even after it, but just don't do that, ok?
// Also, this implementation exists the program on errors
.global some_kind_of_malloc
some_kind_of_malloc:
// rdi (input): number of bytes to allocate
// rax (output): pointer to first byte of allocated memory or 0 on error
push rbx
push r12
// Save the number of bytes to allocate in rbx
mov rbx, rdi
// Call the brk(0) syscall to get current program break (basically end of heap?)
mov rdi, 0
mov rax, 0xc
syscall
// Save previous break
mov r12, rax
// Now do the same thing again, but we call it with the increased size we want
add rax, rbx
// Get the new page break
mov rdi, rax
mov rax, 0xc
syscall
// if we got the same break, allocation failed
cmp r12, rax
JE .Lfail
// Now rax contains the end of our program break
// We can subtract rbx and return that pointer, as now we can write `rbx` many bytes
// One thing to note here is that `brk` can give us much more than we wanted, but
// for ease of use we can also just do nothing and waste a lot of memory
sub rax, rbx
JMP .Lcleanup_malloc
.Lfail:
// Make sure we align our pointer to 16 bytes; calling convention
sub rsp, 8
call print_memory_error
// This part is never reached as print_memory_error exits
.Lcleanup_malloc:
pop r12
pop rbx
ret