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  # input return address for 'stop' to unwind to
 20 #   value : int  # output exit status stop was called with
 21 #
 22 # 'stop' thus takes two parameters: an exit-descriptor and the exit status.
 23 #
 24 # We 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 == code
 29 
 30 # instruction                     effective address                                                   operand     displacement    immediate
 31 # op          subop               mod             rm32          base        index         scale       r32
 32 # 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
 33 
 34 # main:  (manual test if this is the last file loaded)
 35 #?   e8/call  test-stop-skips-returns-on-exit/disp32
 36   e8/call  run-tests/disp32  # 'run-tests' is a function created automatically by SubX. It calls all functions that start with 'test-'.
 37   # syscall(exit, Num-test-failures)
 38   8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/EBX   Num-test-failures/disp32          # copy *Num-test-failures to EBX
 39   b8/copy-to-EAX  1/imm32
 40   cd/syscall  0x80/imm8
 41 
 42 # Configure an exit-descriptor for a call pushing 'nbytes' bytes of args to
 43 # the stack.
 44 # Ugly that we need to know the size of args, but so it goes.
 45 tailor-exit-descriptor:  # ed : (address exit-descriptor), nbytes : int -> ()
 46   # prolog
 47   55/push-EBP
 48   89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 49   # save registers
 50   50/push-EAX
 51   51/push-ECX
 52   # EAX = nbytes
 53   8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           0/r32/EAX   0xc/disp8       .                 # copy *(EBP+12) to EAX
 54   # Let X be the value of ESP in the caller, before the call to tailor-exit-descriptor.
 55   # The return address for a call in the caller's body will be at:
 56   #   X-8 if the caller takes 4 bytes of args for the exit-descriptor (add 4 bytes for the return address)
 57   #   X-12 if the caller takes 8 bytes of args
 58   #   ..and so on
 59   # That's the value we need to return: X-nbytes-4
 60   #
 61   # However, we also need to account for the perturbance to ESP caused by the
 62   # call to tailor-exit-descriptor. It pushes 8 bytes of args followed by 4
 63   # bytes for the return address and 4 bytes to push EBP above.
 64   # So EBP at this point is X-16.
 65   #
 66   # So the return address for the next call in the caller is:
 67   #   EBP+8 if the caller takes 4 bytes of args
 68   #   EBP+4 if the caller takes 8 bytes of args
 69   #   EBP if the caller takes 12 bytes of args
 70   #   EBP-4 if the caller takes 16 bytes of args
 71   #   ..and so on
 72   # That's EBP+12-nbytes.
 73     # option 1: 6 + 3 bytes
 74 #?   2d/subtract                     3/mod/direct    0/rm32/EAX    .           .             .           .           .               8/imm32           # subtract from EAX
 75 #?   8d/copy-address                 0/mod/indirect  4/rm32/sib    5/base/EBP  0/index/EAX   .           0/r32/EAX   .               .                 # copy EBP+EAX to EAX
 76     # option 2: 2 + 4 bytes
 77   f7          3/subop/negate      3/mod/direct    0/rm32/EAX    .           .             .           .           .               .                 # negate EAX
 78   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
 79   # copy EAX to ed->target
 80   8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           1/r32/ECX   8/disp8         .                 # copy *(EBP+8) to ECX
 81   89/copy                         0/mod/indirect  1/rm32/ECX    .           .             .           0/r32/EAX   .               .                 # copy EAX to *ECX
 82   # initialize ed->value
 83   c7/copy                         1/mod/*+disp8   1/rm32/ECX    .           .             .           .           4/disp8         0/imm32           # copy to *(ECX+4)
 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   # exit(value) if ed->target == 0
 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
102   cd/syscall  0x80/imm8
103 $stop:fake:
104   # ed->value = value+1
105   8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/ESP  4/index/none  .           1/r32/ECX   8/disp8         .                 # copy *(ESP+8) to ECX
106   41/inc-ECX
107   89/copy                         1/mod/*+disp8   0/rm32/EAX    .           .             .           1/r32/ECX   4/disp8         .                 # copy ECX to *(EAX+4)
108   # non-local jump to ed->target
109   8b/copy                         0/mod/indirect  0/rm32/EAX    .           .             .           4/r32/ESP   .               .                 # copy *EAX to ESP
110   c3/return  # doesn't return to caller
111 
112 test-stop-skips-returns-on-exit:
113   # This looks like the standard prolog, but is here for different reasons.
114   # A function calling 'stop' can't rely on EBP persisting past the call.
115   #
116   # Use EBP here as a stable base to refer to locals and arguments from in the
117   # presence of push/pop/call instructions.
118   # *Don't* use EBP as a way to restore ESP.
119   55/push-EBP
120   89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
121   # Make room for an exit descriptor on the stack. That's almost always the
122   # right place for it, available only as long as it's legal to use. Once this
123   # containing function returns we'll need a new exit descriptor.
124   # var ed/EAX : (address exit-descriptor)
125   81          5/subop/subtract    3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # subtract from ESP
126   8d/copy-address                 0/mod/indirect  4/rm32/sib    4/base/ESP  4/index/none  .           0/r32/EAX   .               .                 # copy ESP to EAX
127   # Size the exit-descriptor precisely for the next call below, to _test-stop-1.
128   # tailor-exit-descriptor(ed, 4)
129     # push args
130   68/push  4/imm32/nbytes-of-args-for-_test-stop-1
131   50/push-EAX
132     # call
133   e8/call  tailor-exit-descriptor/disp32
134     # discard args
135   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
136   # call _test-stop-1(ed)
137     # push args
138   50/push-EAX
139     # call
140   e8/call  _test-stop-1/disp32
141   ## registers except ESP may be clobbered at this point
142     # restore args
143   58/pop-to-EAX
144   # check that _test-stop-1 tried to call exit(1)
145   # check-ints-equal(ed->value, 2, msg)  # i.e. stop was called with value 1
146     # push args
147   68/push  "F - test-stop-skips-returns-on-exit"/imm32
148   68/push  2/imm32
149     # push ed->value
150   ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           4/disp8         .                 # push *(EAX+4)
151     # call
152   e8/call  check-ints-equal/disp32
153     # discard args
154   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
155   # epilog
156   5d/pop-to-EBP
157     # don't restore ESP from EBP; manually reclaim locals
158   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
159   c3/return
160 
161 _test-stop-1:  # ed : (address exit-descriptor)
162   # prolog
163   55/push-EBP
164   89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
165   # _test-stop-2(ed)
166     # push args
167   ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           8/disp8         .                 # push *(EBP+8)
168     # call
169   e8/call  _test-stop-2/disp32
170   ## should never get past this point
171     # discard args
172   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
173   # signal test failed: check-ints-equal(1, 0, msg)
174     # push args
175   68/push  "F - test-stop-skips-returns-on-exit"/imm32
176   68/push  0/imm32
177   68/push  1/imm32
178     # call
179   e8/call  check-ints-equal/disp32
180     # discard args
181   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
182   # epilog
183   89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
184   5d/pop-to-EBP
185   c3/return
186 
187 _test-stop-2:  # ed : (address exit-descriptor)
188   # prolog
189   55/push-EBP
190   89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
191   # call stop(ed, 1)
192     # push args
193   68/push  1/imm32
194   ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           8/disp8         .                 # push *(EBP+8)
195     # call
196   e8/call  stop/disp32
197   ## should never get past this point
198   # epilog
199   89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
200   5d/pop-to-EBP
201   c3/return