Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POSIX ql_syscall_readv returns incorrect value. #1443

Open
iMoD1998 opened this issue Feb 2, 2024 · 0 comments
Open

POSIX ql_syscall_readv returns incorrect value. #1443

iMoD1998 opened this issue Feb 2, 2024 · 0 comments
Assignees

Comments

@iMoD1998
Copy link

iMoD1998 commented Feb 2, 2024

*Describe the bug
readv() according to the manpages should return the number of bytes read but it seems to return the size of the iovec everytime???

Sample Code

def ql_syscall_readv(ql: Qiling, fd: int, vec: int, vlen: int):
    regreturn = 0
    size_t_len = ql.arch.pointersize
    iov = ql.mem.read(vec, vlen * size_t_len * 2)
    ql.log.debug('readv() CONTENT:')

    for i in range(vlen):
        addr = ql.unpack(iov[i * size_t_len * 2 : i * size_t_len * 2 + size_t_len])
        l = ql.unpack(iov[i * size_t_len * 2 + size_t_len : i * size_t_len * 2 + size_t_len * 2])
        regreturn += l

        if hasattr(ql.os.fd[fd], 'read'):
            data = ql.os.fd[fd].read(l)
            ql.log.debug(f'{data!r}')
            ql.mem.write(addr, data)

    return regreturn

Expected behavior
readv() should return the amount of bytes read from fd.

Proposed Change
My solution that fixes this looks like the following:

def ql_syscall_readv(ql: Qiling, fd: int, vec: int, vlen: int):
    regreturn = 0
    size_t_len = ql.arch.pointersize
    iov = ql.mem.read(vec, vlen * size_t_len * 2)
    ql.log.debug('readv() CONTENT:')

    for i in range(vlen):
        addr = ql.unpack(iov[i * size_t_len * 2 : i * size_t_len * 2 + size_t_len])
        l = ql.unpack(iov[i * size_t_len * 2 + size_t_len : i * size_t_len * 2 + size_t_len * 2])

        if hasattr(ql.os.fd[fd], 'read'):
            data = ql.os.fd[fd].read(l)
            ql.log.debug(f'{data!r}')
            ql.mem.write(addr, data)
            regreturn += len(data)

    return regreturn
@iMoD1998 iMoD1998 changed the title POSIX readv returns incorrect value. POSIX ql_syscall_readv returns incorrect value. Feb 2, 2024
@elicn elicn self-assigned this Feb 4, 2024
@elicn elicn mentioned this issue Feb 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants