https://github.com/akkartik/mu/blob/master/subx/069allocate.subx
  1 # Helper to dynamically allocate memory on the heap.
  2 #
  3 # We'd like to be able to write tests for functions that allocate memory,
  4 # making assertions on the precise addresses used. To achieve this we'll pass
  5 # in an *allocation descriptor* to allocate from.
  6 #
  7 # Allocation descriptors are also useful outside of tests. Assembly and machine
  8 # code are of necessity unsafe languages, and one of the most insidious kinds
  9 # of bugs unsafe languages expose us to are dangling pointers to memory that
 10 # has been freed and potentially even reused for something totally different.
 11 # To reduce the odds of such "use after free" errors, SubX programs tend to not
 12 # reclaim and reuse dynamically allocated memory. (Running out of memory is far
 13 # easier to debug.) Long-running programs that want to reuse memory are mostly
 14 # on their own to be careful. However, they do get one bit of help: they can
 15 # carve out chunks of memory and then allocate from them manually using this
 16 # very same 'allocate' helper. They just need a new allocation descriptor for
 17 # their book-keeping.
 18 
 19 == data
 20 
 21 # The 'global' allocation descriptor. Pass this into 'allocate' to claim a
 22 # hitherto unused bit of memory.
 23 Heap:
 24     Start-of-heap/imm32  # curr
 25     00 00 00 0b  # limit = 0x0b000000; keep sync'd with DATA_SEGMENT + SEGMENT_ALIGNMENT
 26 
 27 == code
 28 #   instruction                     effective address                                                   register    displacement    immediate
 29 # . op          subop               mod             rm32          base        index         scale       r32
 30 # . 1-3 bytes   3 bits              2 bits          3 bits        3 bits      3 bits        2 bits      2 bits      0/1/2/4 bytes   0/1/2/4 bytes
 31 
 32 # main:
 33     e8/call  run-tests/disp32  # 'run-tests' is a function created automatically by SubX. It calls all functions that start with 'test-'.
 34     # syscall(exit, Num-test-failures)
 35     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/EBX   Num-test-failures/disp32          # copy *Num-test-failures to EBX
 36     b8/copy-to-EAX  1/imm32/exit
 37     cd/syscall  0x80/imm8
 38 
 39 # Claim the next 'n' bytes of memory starting at ad->curr and update ad->curr.
 40 # If there isn't enough memory before ad->limit, return 0 and leave 'ad' unmodified.
 41 allocate:  # ad : (address allocation-descriptor), n : int -> address-or-null/EAX
 42     # . prolog
 43     55/push-EBP
 44     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 45     # . save registers
 46     51/push-ECX
 47     52/push-EDX
 48     # ECX = ad
 49     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           1/r32/ECX   8/disp8         .                 # copy *(EBP+8) to ECX
 50     # save ad->curr
 51     8b/copy                         0/mod/indirect  1/rm32/ECX    .           .             .           0/r32/EAX   .               .                 # copy *ECX to EAX
 52     # check if there's enough space
 53     # . EDX = ad->curr + n
 54     89/copy                         3/mod/direct    2/rm32/EDX    .           .             .           0/r32/EAX   .               .                 # copy EAX to EDX
 55     03/add                          1/mod/*+disp8   5/rm32/EBP    .           .             .           2/r32/EDX   0xc/disp8       .                 # add *(EBP+12) to EDX
 56     3b/compare                      1/mod/*+disp8   1/rm32/ECX    .           .             .           2/r32/EDX   4/disp8         .                 # compare EDX with *(ECX+4)
 57     7c/jump-if-lesser  $allocate:commit/disp8
 58     # return null if not
 59     b8/copy-to-EAX  0/imm32
 60     eb/jump  $allocate:end/disp8
 61 $allocate:commit:
 62     # update ad->curr
 63     89/copy                         0/mod/indirect  1/rm32/ECX    .           .             .           2/r32/EDX   .               .                 # copy EDX to *ECX
 64 $allocate:end:
 65     # . restore registers
 66     5a/pop-to-EDX
 67     59/pop-to-ECX
 68     # . epilog
 69     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
 70     5d/pop-to-EBP
 71     c3/return
 72 
 73 test-allocate-success:
 74     # . prolog
 75     55/push-EBP
 76     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 77     # var ad/ECX : (address allocation-descriptor) = {11, 15}
 78     68/push  0xf/imm32/limit
 79     68/push  0xb/imm32/curr
 80     89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           4/r32/ESP   .               .                 # copy ESP to ECX
 81     # EAX = allocate(ad, 3)
 82     # . . push args
 83     68/push  3/imm32
 84     51/push-ECX
 85     # . . call
 86     e8/call  allocate/disp32
 87     # . . discard args
 88     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
 89     # check-ints-equal(EAX, 11, msg)
 90     # . . push args
 91     68/push  "F - test-allocate-success: returns current pointer of allocation descriptor"/imm32
 92     68/push  0xb/imm32
 93     50/push-EAX
 94     # . . call
 95     e8/call  check-ints-equal/disp32
 96     # . . discard args
 97     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
 98     # check-ints-equal(ad->curr, 14, msg)
 99     # . . push args
100     68/push  "F - test-allocate-success: updates allocation descriptor"/imm32
101     68/push  0xe/imm32
102     ff          6/subop/push        0/mod/indirect  1/rm32/ECX    .           .             .           .           .               .                 # push *ECX
103     # . . call
104     e8/call  check-ints-equal/disp32
105     # . . discard args
106     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
107     # . epilog
108     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
109     5d/pop-to-EBP
110     c3/return
111 
112 test-allocate-failure:
113     # . prolog
114     55/push-EBP
115     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
116     # var ad/ECX : (address allocation-descriptor) = {11, 15}
117     68/push  0xf/imm32/limit
118     68/push  0xb/imm32/curr
119     89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           4/r32/ESP   .               .                 # copy ESP to ECX
120     # EAX = allocate(ad, 6)
121     # . . push args
122     68/push  6/imm32
123     51/push-ECX
124     # . . call
125     e8/call  allocate/disp32
126     # . . discard args
127     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
128     # check-ints-equal(EAX, 0, msg)
129     # . . push args
130     68/push  "F - test-allocate-failure: returns null"/imm32
131     68/push  0/imm32
132     50/push-EAX
133     # . . call
134     e8/call  check-ints-equal/disp32
135     # . . discard args
136     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
137     # no change to ad->curr
138     # . check-ints-equal(ad->curr, 11)
139     # . . push args
140     68/push  "F - test-allocate-failure: updates allocation descriptor"/imm32
141     68/push  0xb/imm32
142     ff          6/subop/push        0/mod/indirect  1/rm32/ECX    .           .             .           .           .               .                 # push *ECX
143     # . . call
144     e8/call  check-ints-equal/disp32
145     # . . discard args
146     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
147     # . epilog
148     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
149     5d/pop-to-EBP
150     c3/return
151 
152 # helper: create a nested allocation descriptor (useful for tests)
153 allocate-region:  # ad : (address allocation-descriptor), n : int -> new-ad : (address allocation-descriptor)
154     # . prolog
155     55/push-EBP
156     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
157     # . save registers
158     51/push-ECX
159     # EAX = allocate(ad, n)
160     # . . push args
161     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0xc/disp8       .                 # push *(EBP+12)
162     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         .                 # push *(EBP+8)
163     # . . call
164     e8/call  allocate/disp32
165     # . . discard args
166     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
167     # if (EAX == 0) abort
168     81          7/subop/compare     3/mod/direct    0/rm32/EAX    .           .             .           .           .               0/imm32           # compare EAX
169     74/jump-if-equal  $allocate-region:abort/disp8
170     # earmark 8 bytes at the start for a new allocation descriptor
171     # . *EAX = EAX + 8
172     89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           0/r32/EAX   .               .                 # copy EAX to ECX
173     81          0/subop/add         3/mod/direct    1/rm32/ECX    .           .             .           .           .               8/imm32           # add to ECX
174     89/copy                         0/mod/indirect  0/rm32/EAX    .           .             .           1/r32/ECX   .               .                 # copy ECX to *EAX
175     # . *(EAX+4) = EAX + n
176     89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           0/r32/EAX   .               .                 # copy EAX to ECX
177     03/add                          1/mod/*+disp8   5/rm32/EBP    .           .             .           1/r32/ECX   0xc/disp8       .                 # add *(EBP+12) to ECX
178     89/copy                         1/mod/*+disp8   0/rm32/EAX    .           .             .           1/r32/ECX   4/disp8         .                 # copy ECX to *(EAX+4)
179     # . restore registers
180     59/pop-to-ECX
181     # . epilog
182     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
183     5d/pop-to-EBP
184     c3/return
185 
186 # We could create a more general '$abort' jump target, but then we'd need to do
187 # a conditional jump followed by loading the error message and an unconditional
188 # jump. Or we'd need to unconditionally load the error message before a
189 # conditional jump, even if it's unused the vast majority of the time. This way
190 # we bloat a potentially cold segment in RAM so we can abort with a single
191 # instruction.
192 $allocate-region:abort:
193     # . _write(2/stderr, error)
194     # . . push args
195     68/push  "allocate-region: failed to allocate"/imm32
196     68/push  2/imm32/stderr
197     # . . call
198     e8/call  _write/disp32
199     # . . discard args
200     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
201     # . syscall(exit, 1)
202     bb/copy-to-EBX  1/imm32
203     b8/copy-to-EAX  1/imm32/exit
204     cd/syscall  0x80/imm8
205     # never gets here
206 
207 # . . vim:nowrap:textwidth=0