insou22 / mipsy

Education-focused MIPS Emulator written in Rust.
86 stars 13 forks source link

Mark sbrk region as uninitialized #300

Open ramidzkh opened 10 months ago

ramidzkh commented 10 months ago

Making the region revealed by sbrk filled with uninitialized by default is possibly sufficient.

A heap allocator using sbrk could allocate extra memory to ensure subsequent sbrk addresses remain well-aligned. Rather than filling the gap between the requested memory and the end of the allocation with nulls, or clear marker bytes, it would be useful for debugging purposes to mark this gap as uninitialized. In fact, the entire region should probably be uninitialized and set by the code.

main:
        addiu   $sp, $sp, -4
        sw $ra, 0($sp)

        addiu   $a0, $zero, 1           # 1 byte
        jal     malloc                  # sbrk's 16 bytes header + 1 byte + 7 byte padding for 8 byte alignment
        lb      $t0, 0($v0)             # Access first byte (uninitialized, should die)
        lb      $t1, 1($v0)             # Access padding (uninitialized and padding, should die)
        lb      $t1, 8($v0)             # Access 8th byte (out of bounds, dies here)

        move    $a0, $v0
        jal     free

        addiu   $v0, $zero, 0           # Return 0

        lw      $ra, 0($sp)
        addiu   $sp, $sp, 4
        jr      $ra
[mipsy] run
error: segmentation fault

this happened because you tried to read from
the address `0x10040018`, which is not a valid address to read from

the instruction that failed was:
0x00400290 208 [0x80490008]    lb     $t1, 8($v0)       #  lb      $t1, 8($v0)             # Access 8th byte (out of bounds, dies here)

tip: the address `0x10040018` is part of the DATA segment

[mipsy] x 0x10040000
0x10040000: 0000 0000 0000 0000 1800 0000 01__ ____  .............___
0x10040010: ____ ____ ____ ____ ____ ____ ____ ____  ________________
0x10040020: ____ ____ ____ ____ ____ ____ ____ ____  ________________
0x10040030: ____ ____ ____ ____ ____ ____ ____ ____  ________________
0x10040040: ____ ____ ____ ____ ____ ____ ____ ____  ________________
0x10040050: ____ ____ ____ ____ ____ ____ ____ ____  ________________
0x10040060: ____ ____ ____ ____ ____ ____ ____ ____  ________________
0x10040070: ____ ____ ____ ____ ____ ____ ____ ____  ________________