mortbopet / Ripes

A graphical processor simulator and assembly editor for the RISC-V ISA
https://ripes.me/
MIT License
2.49k stars 270 forks source link

Open ecall does not support read only mode #316

Closed lukasrad02 closed 8 months ago

lukasrad02 commented 9 months ago

It is not possible to open a file in read only mode by passing 0 as flags parameter. The file will have the state QIODevice::NotOpen instead.

This seems to be related to a bug in the implementation of the ecall. Parsing the flags does not work for a 0 flag, as flags & O_RDONLY always evaluates to false since O_RDONLY equals 0:
https://github.com/mortbopet/Ripes/blob/27fcc58c02b26e84156ef588d42fb36d8ada4543/src/syscall/systemio.h#L149-L154

The only way to open a file for reading is using the read-write mode and then seeking back to position 0 in the file.

Not working code using O_RDONLY

.data

filename: .string "/tmp/test.txt"

.text

la a0 filename # a0: Pointer to filename
li a1 0 # Readonly flag
li a7 1024 # "Open" ecall
ecall # Returns: File descriptor

li a1 0x2000 # a1: buffer
li a2 100 # a2: max. buffer size
li a7 63 # "Read" ecall
ecall

li a0 0x2000
li a7 4 # "Print String" ecall
ecall

Workaround using O_RDWR and seek

.data

filename: .string "/tmp/test.txt"

.text

la a0 filename # a0: Pointer to filename
li a1 2 # Read-Write flag
li a7 1024 # "Open" ecall
ecall # Returns: File descriptor
mv s0 a0 # Save fd, since it will be overwritten by return values

nop # file descriptor stays in a0
li a1 0 # Offset 0
li a2 0 # From the beginning
li a7 62 # "Seek" ecall
ecall

mv a0 s0 # Get back file descriptor
li a1 0x2000 # a1: buffer
li a2 100 # a2: max. buffer size
li a7 63 # "Read" ecall
ecall

li a0 0x2000
li a7 4 # "Print String" ecall
ecall
raccog commented 8 months ago

@lukasrad02 Thank you for reporting this bug!

I submitted a pull request here that fixes it: #325.