https://github.com/akkartik/mu/blob/master/subx/057stop.subx
  1 # stop: dependency-injected wrapper around the exit() syscall
  2 #
  3 # We'd like to be able to write tests for functions that call exit(), and to
  4 # make assertions about whether they exit() or not in a given situation. To
  5 # achieve this we'll call exit() via a smarter wrapper called 'stop'.
  6 #
  7 # In the context of a test, calling a function X that calls 'stop' (directly
  8 # or through further intervening calls) will unwind the stack until X returns,
  9 # so that we can say check any further assertions after the execution of X. To
 10 # achieve this end, we'll pass the return address of X as a 'target' argument
 11 # into X, plumbing it through to 'stop'. When 'stop' gets a non-null target it
 12 # unwinds the stack until the target. If it gets a null target it calls
 13 # exit().
 14 #
 15 # We'd also like to get the exit status out of 'stop', so we'll combine the
 16 # input target with an output status parameter into a type called 'exit-descriptor'.
 17 #
 18 # So the exit-descriptor looks like this:
 19 #   target : address  # return address for 'stop' to unwind to
 20 #   value : int  # exit status stop was called with
 21 #
 22 # 'stop' thus takes two parameters: an exit-descriptor and the exit status.
 23 #
 24 # 'stop' won't bother cleaning up any other processor state besides the stack,
 25 # such as registers. Only ESP will have a well-defined value after 'stop'
 26 # returns. (This is a poor man's setjmp/longjmp, if you know what that is.)
 27 #
 28 # Before you can call any function that may call 'stop', you need to pass in an
 29 # exit-descriptor to it. To create an exit-descriptor use 'tailor-exit-descriptor'
 30 # below. It's not the most pleasant abstraction in the world.
 31 #
 32 # An exit-descriptor's target is its input, computed during 'tailor-exit-descriptor'.
 33 # Its value is its output, computed during stop and available to the test.
 34 
 35 == code
 36 #   instruction                     effective address                                                   register    displacement    immediate
 37 # . op          subop               mod             rm32          base        index         scale       r32
 38 # . 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
 39 
 40 # main:
 41     e8/call  run-tests/disp32  # 'run-tests' is a function created automatically by SubX. It calls all functions that start with 'test-'.
 42 #?     e8/call test-stop-skips-returns-on-exit/disp32
 43     # syscall(exit, Num-test-failures)
 44     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/EBX   Num-test-failures/disp32          # copy *Num-test-failures to EBX
 45     b8/copy-to-EAX  1/imm32/exit
 46     cd/syscall  0x80/imm8
 47 
 48 # Configure an exit-descriptor for a call pushing 'nbytes' bytes of args to
 49 # the stack.
 50 # Ugly that we need to know the size of args, but so it goes.
 51 tailor-exit-descriptor:  # ed : (address exit-descriptor), nbytes : int -> <void>
 52     # . prolog
 53     55/push-EBP
 54     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 55     # . save registers
 56     50/push-EAX
 57     51/push-ECX
 58     # EAX = nbytes
 59     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           0/r32/EAX   0xc/disp8       .                 # copy *(EBP+12) to EAX
 60     # Let X be the value of ESP in the caller, before the call to tailor-exit-descriptor.
 61     # The return address for a call in the caller's body will be at:
 62     #   X-8 if the caller takes 4 bytes of args for the exit-descriptor (add 4 bytes for the return address)
 63     #   X-12 if the caller takes 8 bytes of args
 64     #   ..and so on
 65     # That's the value we need to return: X-nbytes-4
 66     #
 67     # However, we also need to account for the perturbance to ESP caused by the
 68     # call to tailor-exit-descriptor. It pushes 8 bytes of args followed by 4
 69     # bytes for the return address and 4 bytes to push EBP above.
 70     # So EBP at this point is X-16.
 71     #
 72     # So the return address for the next call in the caller is:
 73     #   EBP+8 if the caller takes 4 bytes of args
 74     #   EBP+4 if the caller takes 8 bytes of args
 75     #   EBP if the caller takes 12 bytes of args
 76     #   EBP-4 if the caller takes 16 bytes of args
 77     #   ..and so on
 78     # That's EBP+12-nbytes.
 79     # option 1: 6 + 3 bytes
 80 #?     2d/subtract                     3/mod/direct    0/rm32/EAX    .           .             .           .           .               8/imm32           # subtract from EAX
 81 #?     8d/copy-address                 0/mod/indirect  4/rm32/sib    5/base/EBP  0/index/EAX   .           0/r32/EAX   .               .                 # copy EBP+EAX to EAX
 82     # option 2: 2 + 4 bytes
 83     f7          3/subop/negate      3/mod/direct    0/rm32/EAX    .           .             .           .           .               .                 # negate EAX
 84     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    5/base/EBP  0/index/EAX   .           0/r32/EAX   0xc/disp8         .               # copy EBP+EAX+12 to EAX
 85     # copy EAX to ed->target
 86     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           1/r32/ECX   8/disp8         .                 # copy *(EBP+8) to ECX
 87     89/copy                         0/mod/indirect  1/rm32/ECX    .           .             .           0/r32/EAX   .               .                 # copy EAX to *ECX
 88     # initialize ed->value
 89     c7          0/subop/copy        1/mod/*+disp8   1/rm32/ECX    .           .             .           .           4/disp8         0/imm32           # copy to *(ECX+4)
 90 $tailor-exit-descriptor:end:
 91     # . restore registers
 92     59/pop-to-ECX
 93     58/pop-to-EAX
 94     # . epilog
 95     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
 96     5d/pop-to-EBP
 97     c3/return
 98 
 99 stop:  # ed : (address exit-descriptor), value : int
100     # no prolog; one way or another, we're going to clobber registers
101     # EAX = ed
102     8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/ESP  4/index/none  .           0/r32/EAX   4/disp8         .                 # copy *(ESP+4) to EAX
103     # exit(value) if ed->target == 0
104     81          7/subop/compare     0/mod/indirect  0/rm32/EAX    .           .             .           .           .               0/imm32           # compare *EAX
105     75/jump-if-not-equal  $stop:fake/disp8
106     # syscall(exit, value)
107     8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/ESP  4/index/none  .           3/r32/EBX   8/disp8         .                 # copy *(ESP+8) to EBX
108     b8/copy-to-EAX  1/imm32/exit
109     cd/syscall  0x80/imm8
110 $stop:fake:
111     # ed->value = value+1
112     8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/ESP  4/index/none  .           1/r32/ECX   8/disp8         .                 # copy *(ESP+8) to ECX
113     41/inc-ECX
114     89/copy                         1/mod/*+disp8   0/rm32/EAX    .           .             .           1/r32/ECX   4/disp8         .                 # copy ECX to *(EAX+4)
115     # non-local jump to ed->target
116     8b/copy                         0/mod/indirect  0/rm32/EAX    .           .             .           4/r32/ESP   .               .                 # copy *EAX to ESP
117 $stop:end:
118     c3/return  # doesn't return to caller
119 
120 test-stop-skips-returns-on-exit:
121     # This looks like the standard prolog, but is here for different reasons.
122     # A function calling 'stop' can't rely on EBP persisting past the call.
123     #
124     # Use EBP here as a stable base to refer to locals and arguments from in the
125     # presence of push/pop/call instructions.
126     # *Don't* use EBP as a way to restore ESP.
127     55/push-EBP
128     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
129     # Make room for an exit descriptor on the stack. That's almost always the
130     # right place for it, available only as long as it's legal to use. Once this
131     # containing function returns we'll need a new exit descriptor.
132     # var ed/EAX : (address exit-descriptor)
133     81          5/subop/subtract    3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # subtract from ESP
134     89/copy                         3/mod/direct    0/rm32/EAX    .           .             .           4/r32/ESP   .               .                 # copy ESP to EAX
135     # Size the exit-descriptor precisely for the next call below, to _test-stop-1.
136     # tailor-exit-descriptor(ed, 4)
137     # . . push args
138     68/push  4/imm32/nbytes-of-args-for-_test-stop-1
139     50/push-EAX
140     # . . call
141     e8/call  tailor-exit-descriptor/disp32
142     # . . discard args
143     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
144     # . _test-stop-1(ed)
145     # . . push args
146     50/push-EAX
147     # . . call
148     e8/call  _test-stop-1/disp32
149     # registers except ESP may be clobbered at this point
150     # restore args
151     58/pop-to-EAX
152     # check that _test-stop-1 tried to call exit(1)
153     # check-ints-equal(ed->value, 2, msg)  # i.e. stop was called with value 1
154     # . . push args
155     68/push  "F - test-stop-skips-returns-on-exit"/imm32
156     68/push  2/imm32
157     # . . push ed->value
158     ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           4/disp8         .                 # push *(EAX+4)
159     # . . call
160     e8/call  check-ints-equal/disp32
161     # . . discard args
162     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
163     # . epilog
164     # don't restore ESP from EBP; manually reclaim locals
165     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
166     5d/pop-to-EBP
167     c3/return
168 
169 _test-stop-1:  # ed : (address exit-descriptor)
170     # . prolog
171     55/push-EBP
172     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
173     # _test-stop-2(ed)
174     # . . push args
175     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         .                 # push *(EBP+8)
176     # . . call
177     e8/call  _test-stop-2/disp32
178     # should never get past this point
179 $_test-stop-1:dead-end:
180     # . . discard args
181     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
182     # signal test failed: check-ints-equal(1, 0, msg)
183     # . . push args
184     68/push  "F - test-stop-skips-returns-on-exit"/imm32
185     68/push  0/imm32
186     68/push  1/imm32
187     # . . call
188     e8/call  check-ints-equal/disp32
189     # . . discard args
190     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
191     # . epilog
192     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
193     5d/pop-to-EBP
194     c3/return
195 
196 _test-stop-2:  # ed : (address exit-descriptor)
197     # . prolog
198     55/push-EBP
199     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
200     # . stop(ed, 1)
201     # . . push args
202     68/push  1/imm32
203     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         .                 # push *(EBP+8)
204     # . . call
205     e8/call  stop/disp32
206     # should never get past this point
207 $_test-stop-2:dead-end:
208     # . epilog
209     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
210     5d/pop-to-EBP
211     c3/return
212 
213 # . . vim:nowrap:textwidth=0