https://github.com/akkartik/mu/blob/main/linux/313index-bounds-check.subx

     1 # Helper to check an array's bounds, and to abort if they're violated.
     2 # Really only intended to be called from code generated by mu.subx.
     3
     4 == code
     5
     6 __check-mu-array-bounds:  # index: int, elem-size: int, arr-size: int, function-name: (addr array byte), array-name: (addr array byte)
     7     # . prologue
     8     55/push-ebp
     9     89/<- %ebp 4/r32/esp
    10     # . save registers
    11     50/push-eax
    12     51/push-ecx
    13     52/push-edx
    14     # . not bothering saving ebx; it's only clobbered if we're going to abort
    15     # ecx = arr-size
    16     8b/-> *(ebp+0x10) 1/r32/ecx
    17     # var overflow/edx: int = 0
    18     ba/copy-to-edx 0/imm32
    19     # var offset/eax: int = index * elem-size
    20     8b/-> *(ebp+8) 0/r32/eax
    21     f7 4/subop/multiply-eax-with *(ebp+0xc)
    22     # check for overflow
    23     81 7/subop/compare %edx 0/imm32
    24     0f 85/jump-if-!= __check-mu-array-bounds:overflow/disp32
    25     # check bounds
    26     39/compare %eax 1/r32/ecx
    27     0f 82/jump-if-unsigned< $__check-mu-array-bounds:end/disp32  # negative index should always abort
    28     # abort if necessary
    29     (write-buffered Stderr "fn ")
    30     (write-buffered Stderr *(ebp+0x14))
    31     (write-buffered Stderr ": offset ")
    32     (write-int32-hex-buffered Stderr %eax)
    33     (write-buffered Stderr " is too large for array '")
    34     (write-buffered Stderr *(ebp+0x18))
    35     (write-buffered Stderr "'\n")
    36     (flush Stderr)
    37     # exit(1)
    38     bb/copy-to-ebx 1/imm32
    39     e8/call syscall_exit/disp32
    40     # never gets here
    41 $__check-mu-array-bounds:end:
    42     # . restore registers
    43     5a/pop-to-edx
    44     59/pop-to-ecx
    45     58/pop-to-eax
    46     # . epilogue
    47     89/<- %esp 5/r32/ebp
    48     5d/pop-to-ebp
    49     c3/return
    50
    51 __check-mu-array-bounds:overflow:
    52     # "fn " function-name ": offset to array '" array-name "' overflowed 32 bits\n"
    53     (write-buffered Stderr "fn ")
    54     (write-buffered Stderr *(ebp+0x14))
    55     (write-buffered Stderr ": offset to array '")
    56     (write-buffered Stderr *(ebp+0x18))
    57     (write-buffered Stderr "' overflowed 32 bits\n")
    58     (flush Stderr)
    59     # exit(1)
    60     bb/copy-to-ebx 1/imm32
    61     e8/call syscall_exit/disp32
    62     # never gets here
    63
    64 # potential alternative
    65
    66 #? __bounds-check:  # msg: (addr array byte)
    67 #?   (write-buffered Stderr "abort: array bounds exceeded in fn ")
    68 #?   8b/-> *(esp+4) 0/r32/eax  # we're going to abort, so just clobber away
    69 #?   (write-buffered Stderr %eax)
    70 #?   (write-buffered Stderr Newline)
    71 #?   # exit(1)
    72 #?   bb/copy-to-ebx 1/imm32
    73 #?   e8/call syscall_exit/disp32
    74
    75 # to be called as follows:
    76 #   var/reg <- index arr/rega: (addr array T), idx/regi: int
    77 #     | if size-of(T) is 1, 2, 4 or 8
    78 #         => # temporarily save array size to reg to check bounds
    79 #            "8b/-> *" rega " " reg "/r32"
    80 #            "c1/shift 5/subop/right %" reg " " log2(size-of(T)) "/imm32"
    81 #            "3b/compare " reg "/r32 *" rega
    82 #            "68/push \"" function "\"/imm32"  # pass function name to error message
    83 #            "0f 8d/jump-if->= __bounds_check/disp32"
    84 #            "81 0/subop/add %esp 4/imm32"  # drop function name
    85 #            # actually save the index addr in reg
    86 #            "8d/copy-address *(" rega "+" regi "<<" log2(size-of(T)) "+4) " reg "/r32"