Skip to content

Add getaddrinfo and freeaddrinfo shims#4984

Merged
RalfJung merged 1 commit intorust-lang:masterfrom
WhySoBad:network-socket-dns
May 6, 2026
Merged

Add getaddrinfo and freeaddrinfo shims#4984
RalfJung merged 1 commit intorust-lang:masterfrom
WhySoBad:network-socket-dns

Conversation

@WhySoBad
Copy link
Copy Markdown
Contributor

Hi,

This pull request adds shims for the getaddrinfo and freeaddrinfo syscalls. They are used by the ToSocketAddrs trait in the standard library to resolve hostnames to socket addresses.
Because the abstraction of the standard library is very tight, we're pretty much only able to shim the exact use case of the standard library.

Also, we cannot shim gai_strerror because the standard library doesn't have ErrorKinds for the DNS errors. This is a bit unfortunate because Miri will throw an unsupported error when there is a resolution error (e.g. hostname doesn't exist).

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 28, 2026

Thank you for contributing to Miri! A reviewer will take a look at your PR, typically within a week or two.
Please remember to not force-push to the PR branch except when you need to rebase due to a conflict or when the reviewer asks you for it.

@rustbot rustbot added the S-waiting-on-review Status: Waiting for a review to complete label Apr 28, 2026
@RalfJung
Copy link
Copy Markdown
Member

Also, we cannot shim gai_strerror because the standard library doesn't have ErrorKinds for the DNS errors. This is a bit unfortunate because Miri will throw an unsupported error when there is a resolution error (e.g. hostname doesn't exist).

Can't we at least return something generic, like what our strerror_r shim does when encountering an unknown error kind?

@WhySoBad
Copy link
Copy Markdown
Contributor Author

Yeah we can do that.

@WhySoBad
Copy link
Copy Markdown
Contributor Author

WhySoBad commented Apr 28, 2026

Unlike strerror_r, gai_strerror doesn't take a buffer argument so we need to do static allocations for the error messages. Since we have the AddressInfoStore anyways, I was thinking about adding the error allocations there.

And we also have the problem that io_error_to_errnum throws an unsupported error here:

miri/src/shims/io_error.rs

Lines 249 to 260 in 3f8007e

/// This function tries to produce the most similar OS error from the `std::io::ErrorKind`
/// as a platform-specific errnum.
fn io_error_to_errnum(&self, err: std::io::Error) -> InterpResult<'tcx, Scalar> {
let this = self.eval_context_ref();
let target = &this.tcx.sess.target;
if target.families.iter().any(|f| f == "unix") {
for &(name, kind) in UNIX_IO_ERROR_TABLE {
if err.kind() == kind {
return interp_ok(this.eval_libc(name));
}
}
throw_unsup_format!("unsupported io error: {err}")

Are there any other shims which needed to circumvent this method?

@RalfJung
Copy link
Copy Markdown
Member

And we also have the problem that io_error_to_errnum throws an unsupported error here:

What are the errnums you are getting?

@WhySoBad
Copy link
Copy Markdown
Contributor Author

Well, we only get a an Uncategorized io::ErrorKind because the io::Error is constructed manually here: https://github.com/rust-lang/rust/blob/HEAD/library/std/src/sys/net/connection/socket/unix.rs#L53

The problem is that the UNIX_IO_ERROR_TABLE doesn't contain an entry for Uncategorized (which it isn't exposed to because we shouldn't match against that).

@RalfJung
Copy link
Copy Markdown
Member

RalfJung commented Apr 29, 2026

Ah so the actual problem is that

  • getaddrinfo returns an error code (but not a standard errnum code, these are specific DNS error codes)
  • std translates that into a string
  • we now have to somehow get a code back out of that again

There's no standard Unix errnum involved here ever, std just shoves this into an io::Error because it has to.

Comment thread src/shims/unix/socket.rs Outdated
@WhySoBad
Copy link
Copy Markdown
Contributor Author

No, we get standard UNIX error codes back -- they are just DNS specific:

RETURN VALUE
     getaddrinfo() returns 0 if it succeeds, or one of the following nonzero error codes:

     EAI_ADDRFAMILY
            The specified network host does not have any network addresses in the requested address family.

     EAI_AGAIN
            The name server returned a temporary failure indication.  Try again later.

     EAI_BADFLAGS
            hints.ai_flags contains invalid flags; or, hints.ai_flags included AI_CANONNAME and node was NULL.

     EAI_FAIL
            The name server returned a permanent failure indication.

     EAI_FAMILY
            The requested address family is not supported.

     EAI_MEMORY
            Out of memory.

     EAI_NODATA
            The specified network host exists, but does not have any network addresses defined.

     EAI_NONAME
            The node or service is not known; or both node and service are NULL; or AI_NUMERICSERV was specified in hints.ai_flags and
            service was not a numeric port-number string.

     EAI_SERVICE
            The requested service is not available for the requested socket type.  It may be available through  another  socket  type.
            For  example,  this  error  could  occur  if  service was "shell" (a service available only on stream sockets), and either
            hints.ai_protocol was IPPROTO_UDP, or hints.ai_socktype was SOCK_DGRAM; or the error could occur if service was not  NULL,
            and hints.ai_socktype was SOCK_RAW (a socket type that does not support the concept of services).

     EAI_SOCKTYPE
            The requested socket type is not supported.  This could occur, for example, if hints.ai_socktype and hints.ai_protocol are
            inconsistent (e.g., SOCK_DGRAM and IPPROTO_TCP, respectively).

     EAI_SYSTEM
            Other system error; errno is set to indicate the error.

     The gai_strerror() function translates these error codes to a human readable string, suitable for error reporting.

And the standard library has by far not all UNIX error codes mapped to io::ErrorKinds (there are some experimental features to add more variants, but even with those features the DNS errorcodes aren't covered).
For unmapped io::ErrorKinds there is just the io::ErrorKind::Uncategorized which shouldn't be matched against because error kinds which map to might receive their own io::ErrorKind variant in the future.

@RalfJung
Copy link
Copy Markdown
Member

RalfJung commented Apr 29, 2026

No, we get standard UNIX error codes back

These are not standard unix error codes. EAI_* are negative numbers directly returned from that function. Normal unix error codes are positive and they are stored in errnum and the function returns -1.

This is basically a separate error enum specifically for getaddrinfo.

@RalfJung
Copy link
Copy Markdown
Member

For unmapped io::ErrorKinds there is just the io::ErrorKind::Uncategorized which shouldn't be matched against because error kinds which map to might receive their own io::ErrorKind variant in the future.

No, io::Error can represent all host errors, even "unmapped" ones. That's what Error::from_raw_os_error is for.

But that's only for "normal" host error codes, not for this other kind of error code that getaddrinfo returns.

@rustbot

This comment has been minimized.

@WhySoBad WhySoBad force-pushed the network-socket-dns branch from ad84f51 to 378d89c Compare May 1, 2026 18:52
@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@WhySoBad WhySoBad force-pushed the network-socket-dns branch from 378d89c to d811aaf Compare May 4, 2026 12:30
@rustbot

This comment has been minimized.

Copy link
Copy Markdown
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the first proper round of comments. :)
@rustbot author

View changes since this review

Comment thread src/shims/unix/socket.rs Outdated
Comment thread src/shims/unix/socket.rs Outdated
Comment thread src/shims/unix/socket.rs Outdated
Comment thread src/shims/unix/socket.rs Outdated
Comment thread src/shims/unix/socket.rs Outdated
Comment thread src/shims/unix/socket.rs Outdated
Comment thread src/diagnostics.rs Outdated
Comment thread src/diagnostics.rs Outdated
Comment thread tests/pass-dep/libc/libc-socket-invalid-addr.rs Outdated
Comment thread tests/utils/libc.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: Waiting for the PR author to address review comments and removed S-waiting-on-review Status: Waiting for a review to complete labels May 5, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 5, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@WhySoBad
Copy link
Copy Markdown
Contributor Author

WhySoBad commented May 5, 2026

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Waiting for a review to complete and removed S-waiting-on-author Status: Waiting for the PR author to address review comments labels May 5, 2026
Copy link
Copy Markdown
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few minor things (and that discussion about the type for the sockname* helpers)

@rustbot author

View changes since this review

Comment thread tests/pass-dep/libc/libc-socket-invalid-addr.rs Outdated
Comment thread src/diagnostics.rs Outdated
Comment thread src/diagnostics.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: Waiting for the PR author to address review comments and removed S-waiting-on-review Status: Waiting for a review to complete labels May 5, 2026
@WhySoBad
Copy link
Copy Markdown
Contributor Author

WhySoBad commented May 5, 2026

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Waiting for a review to complete and removed S-waiting-on-author Status: Waiting for the PR author to address review comments labels May 5, 2026
Copy link
Copy Markdown
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks great! Two last minor comments, and then please squash the commits.
@rustbot author

View changes since this review

Comment thread src/machine.rs Outdated
Comment thread tests/pass-dep/libc/libc-socket.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: Waiting for the PR author to address review comments and removed S-waiting-on-review Status: Waiting for a review to complete labels May 6, 2026
@WhySoBad WhySoBad force-pushed the network-socket-dns branch from 18efa43 to 72b7fe7 Compare May 6, 2026 06:56
@rustbot

This comment has been minimized.

@WhySoBad WhySoBad force-pushed the network-socket-dns branch from 72b7fe7 to a5e33c0 Compare May 6, 2026 07:06
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 6, 2026

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@WhySoBad
Copy link
Copy Markdown
Contributor Author

WhySoBad commented May 6, 2026

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Waiting for a review to complete and removed S-waiting-on-author Status: Waiting for the PR author to address review comments labels May 6, 2026
Comment thread tests/pass/shims/socket-address.rs Outdated
Using those shims socket addresses can be created directly from
hostnames (e.g. using `ToSocketAddrs` from the standard library)
@WhySoBad WhySoBad force-pushed the network-socket-dns branch from a5e33c0 to 5948639 Compare May 6, 2026 11:31
@RalfJung RalfJung enabled auto-merge May 6, 2026 11:36
@RalfJung RalfJung added this pull request to the merge queue May 6, 2026
Merged via the queue into rust-lang:master with commit 0f465c2 May 6, 2026
13 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Waiting for a review to complete label May 6, 2026
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

Successfully merging this pull request may close these issues.

3 participants