https://github.com/akkartik/mu/blob/master/059stop.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 # Configure an exit-descriptor for a call pushing 'nbytes' bytes of args to
 41 # the stack.
 42 # Ugly that we need to know the size of args. Don't allocate variables between
 43 # tailor-exit-descriptor and the call it's for.
 44 tailor-exit-descriptor:  # ed : (address exit-descriptor), nbytes : int -> <void>
 45     # . prolog
 46     55/push-ebp
 47     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
 48     # . save registers
 49     50/push-eax
 50     51/push-ecx
 51     # eax = nbytes
 52     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   0xc/disp8       .                 # copy *(ebp+12) to eax
 53     # Let X be the value of esp in the caller, before the call to tailor-exit-descriptor.
 54     # The return address for a call in the caller's body will be at:
 55     #   X-8 if the caller takes 4 bytes of args for the exit-descriptor (add 4 bytes for the return address)
 56     #   X-12 if the caller takes 8 bytes of args
 57     #   ..and so on
 58     # That's the value we need to return: X-nbytes-4
 59     #
 60     # However, we also need to account for the perturbance to esp caused by the
 61     # call to tailor-exit-descriptor. It pushes 8 bytes of args followed by 4
 62     # bytes for the return address and 4 bytes to push ebp above.
 63     # So ebp at this point is X-16.
 64     #
 65     # So the return address for the next call in the caller is:
 66     #   ebp+8 if the caller takes 4 bytes of args
 67     #   ebp+4 if the caller takes 8 bytes of args
 68     #   ebp if the caller takes 12 bytes of args
 69     #   ebp-4 if the caller takes 16 bytes of args
 70     #   ..and so on
 71     # That's ebp+12-nbytes.
 72     # option 1: 6 + 3 bytes
 73 #?     2d/subtract                     3/mod/direct    0/rm32/eax    .           .             .           .           .               8/imm32           # subtract from eax
 74 #?     8d/copy-address                 0/mod/indirect  4/rm32/sib    5/base/ebp  0/index/eax   .           0/r32/eax   .               .                 # copy ebp+eax to eax
 75     # option 2: 2 + 4 bytes
 76     f7          3/subop/negate      3/mod/direct    0/rm32/eax    .           .             .           .           .               .                 # negate eax
 77     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
 78     # copy eax to ed->target
 79     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           1/r32/ecx   8/disp8         .                 # copy *(ebp+8) to ecx
 80     89/copy                         0/mod/indirect  1/rm32/ecx    .           .             .           0/r32/eax   .               .                 # copy eax to *ecx
 81     # initialize ed->value
 82     c7          0/subop/copy        1/mod/*+disp8   1/rm32/ecx    .           .             .           .           4/disp8         0/imm32           # copy to *(ecx+4)
 83 $tailor-exit-descriptor:end:
 84     # . restore registers
 85     59/pop-to-ecx
 86     58/pop-to-eax
 87     # . epilog
 88     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
 89     5d/pop-to-ebp
 90     c3/return
 91 
 92 stop:  # ed : (address exit-descriptor), value : int
 93     # no prolog; one way or another, we're going to clobber registers
 94     # eax = ed
 95     8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/esp  4/index/none  .           0/r32/eax   4/disp8         .                 # copy *(esp+4) to eax
 96     # if (ed->target == 0) really exit
 97     81          7/subop/compare     0/mod/indirect  0/rm32/eax    .           .             .           .           .               0/imm32           # compare *eax
 98     75/jump-if-not-equal  $stop:fake/disp8
 99     # . syscall(exit, value)
100     8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/esp  4/index/none  .           3/r32/ebx   8/disp8         .                 # copy *(esp+8) to ebx
101     b8/copy-to-eax  1/imm32/exit
102     cd/syscall  0x80/imm8
103 $stop:fake:
104     # otherwise:
105     # ed->value = value+1
106     8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/esp  4/index/none  .           1/r32/ecx   8/disp8         .                 # copy *(esp+8) to ecx
107     41/increment-ecx
108     89/copy                         1/mod/*+disp8   0/rm32/eax    .           .             .           1/r32/ecx   4/disp8         .                 # copy ecx to *(eax+4)
109     # perform a non-local jump to ed->target
110     8b/copy                         0/mod/indirect  0/rm32/eax    .           .             .           4/r32/esp   .               .                 # copy *eax to esp
111 $stop:end:
112     c3/return  # doesn't return to caller
113 
114 test-stop-skips-returns-on-exit:
115     # This looks like the standard prolog, but is here for different reasons.
116     # A function calling 'stop' can't rely on ebp persisting past the call.
117     #
118     # Use ebp here as a stable base to refer to locals and arguments from in the
119     # presence of push/pop/call instructions.
120     # *Don't* use ebp as a way to restore esp.
121     55/push-ebp
122     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
123     # Make room for an exit descriptor on the stack. That's almost always the
124     # right place for it, available only as long as it's legal to use. Once this
125     # containing function returns we'll need a new exit descriptor.
126     # var ed/eax : (address exit-descriptor)
127     68/push  0/imm32
128     68/push  0/imm32
129     89/copy                         3/mod/direct    0/rm32/eax    .           .             .           4/r32/esp   .               .                 # copy esp to eax
130     # Size the exit-descriptor precisely for the next call below, to _test-stop-1.
131     # tailor-exit-descriptor(ed, 4)
132     # . . push args
133     68/push  4/imm32/nbytes-of-args-for-_test-stop-1
134     50/push-eax
135     # . . call
136     e8/call  tailor-exit-descriptor/disp32
137     # . . discard args
138     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
139     # . _test-stop-1(ed)
140     # . . push args
141     50/push-eax
142     # . . call
143     e8/call  _test-stop-1/disp32
144     # registers except esp may be clobbered at this point
145     # restore args
146     58/pop-to-eax
147     # check that _test-stop-1 tried to call exit(1)
148     # . check-ints-equal(ed->value, 2, msg)  # i.e. stop was called with value 1
149     # . . push args
150     68/push  "F - test-stop-skips-returns-on-exit"/imm32
151     68/push  2/imm32
152     # . . push ed->value
153     ff          6/subop/push        1/mod/*+disp8   0/rm32/eax    .           .             .           .           4/disp8         .                 # push *(eax+4)
154     # . . call
155     e8/call  check-ints-equal/disp32
156     # . . discard args
157     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
158     # . epilog
159     # don't restore esp from ebp; manually reclaim locals
160     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
161     5d/pop-to-ebp
162     c3/return
163 
164 _test-stop-1:  # ed : (address exit-descriptor)
165     # . prolog
166     55/push-ebp
167     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
168     # _test-stop-2(ed)
169     # . . push args
170     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
171     # . . call
172     e8/call  _test-stop-2/disp32
173     # should never get past this point
174 $_test-stop-1:dead-end:
175     # . . discard args
176     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
177     # signal test failed: check-ints-equal(1, 0, msg)
178     # . . push args
179     68/push  "F - test-stop-skips-returns-on-exit"/imm32
180     68/push  0/imm32
181     68/push  1/imm32
182     # . . call
183     e8/call  check-ints-equal/disp32
184     # . . discard args
185     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
186     # . epilog
187     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
188     5d/pop-to-ebp
189     c3/return
190 
191 _test-stop-2:  # ed : (address exit-descriptor)
192     # . prolog
193     55/push-ebp
194     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
195     # . stop(ed, 1)
196     # . . push args
197     68/push  1/imm32
198     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
199     # . . call
200     e8/call  stop/disp32
201     # should never get past this point
202 $_test-stop-2:dead-end:
203     # . epilog
204     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
205     5d/pop-to-ebp
206     c3/return
207 
208 # . . vim:nowrap:textwidth=0