Skip to content

Refactoring sockaddr Handling #3

@Yaxuan-w

Description

@Yaxuan-w

Description:

This issue tracks the refactoring of sockaddr handling and addresses the problems encountered with accept_syscall.

Background:

Currently, sockaddr is initialized using the default Unix GenSockaddr borrowed from RustPOSIX, which has the largest memory space. This may cause issues with getpeername, as IPv4/IPv6 shouldn't have a path field.

It's been observed that byte-level operations should be preferred over struct-type checks.

In Linux, there are various sockaddr structures, such as sockaddr_in for IPv4 and sockaddr_in6 for IPv6. However, tracing through the PostgreSQL source code shows that PostgreSQL uses sockaddr_storage, which is cast to sockaddr when necessary. The key difference between sockaddr and sockaddr_storage is size, with sockaddr_storage being large enough to accommodate all sockaddr types.

PostgreSQL's accept syscall passes a sockaddr->family=0 with sock_len=128, making it impossible to determine the family based on size alone.

Problem:

When using accept, recvfrom, getsockname, or getpeername, issues arise because the wrong sockaddr family is being inferred. The system receives a NULL sockaddr, and the copy_out function from RustPOSIX doesn't perform any operations.

Current Patch

I made a temporary modification to accept_syscall to make RawPOSIX work for now:

  • Initialize a default GenSockaddr struct based on the sockaddr family received at the dispatcher stage.
  • Handle the UNIX path conversion within the syscall itself.
  • Additionally, in the copy_out function, the number of bytes to copy will be determined by comparing initaddrlen with the actual length of the structure (taking the minimum value), ensuring that no more than the reserved space is copied.

Proposed Further Solution:

To resolve this, I suggest:

  • Refining the GenSockAddr data structure. Directly allocate a buffer of size 128 bytes for syscalls like accept, recvfrom, getsockname, and getpeername.
  • Implement a new function: Pass pointers to avoid NULL values being sent to syscalls.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions