Skip to content

Switch to getnameinfo/getaddrinfo for address/name resolution - #76

Merged
guillerodriguez merged 3 commits into
masterfrom
feat/cpnet-getnameinfo-getaddrinfo
Oct 19, 2026
Merged

guillerodriguez merged 3 commits into
masterfrom
feat/cpnet-getnameinfo-getaddrinfo

Conversation

@guillerodriguez

Copy link
Copy Markdown
Contributor

No description provided.

cpnet_getHostByAddr() currently uses gethostbyaddr(), which is not
guaranteed to be thread-safe. Concurrent reverse lookups through
InetAddress.getHostName() can overwrite each other's results.

Prefer getnameinfo() when available. This function is thread-safe,
standardized in POSIX.1-2001, and widely available.

Signed-off-by: Guillermo Rodríguez <grodriguez@ingelabs.com>
cpnet_getHostByName() currently uses gethostbyname_r() if available,
and falls back to gethostbyname(), which is not guaranteed to be
thread-safe. On systems without gethostbyname_r(), concurrent lookups
through InetAddress.getByName() can overwrite each other's results.

Also, gethostbyname_r() is not standard, and its signature differs
between systems.

Prefer getaddrinfo() when available. It is thread-safe, standardized
in POSIX.1-2001, and widely available.

Signed-off-by: Guillermo Rodríguez <grodriguez@ingelabs.com>
Require getaddrinfo() and getnameinfo() for native networking, and
remove the gethostbyname_r(), gethostbyname(), and gethostbyaddr()
fallbacks. Also remove the configure macro that detected the
gethostbyname_r() signature.

While at it, make cpnet_getHostByName() return resolver errors as
errno values, like cpnet_getHostByAddr() does, instead of negative
h_errno values. Both now use a shared helper for the translation.
This does not change behavior, since the only caller of
cpnet_getHostByName() just checks for success.

Signed-off-by: Guillermo Rodríguez <grodriguez@ingelabs.com>
@guillerodriguez
guillerodriguez force-pushed the feat/cpnet-getnameinfo-getaddrinfo branch from f57d46c to 8062e73 Compare October 17, 2026 12:28
@duncanmcqueen

Copy link
Copy Markdown
Contributor

Thank you. I tested #76 on Haiku. It works, and it is better than #74.

Setup: Haiku R1/beta6 x86_64 (hrev60228) in QEMU/KVM with 6 CPUs, JamVM 2.1.0 with the Haiku port, and ingelabs master with the Haiku patches. I compiled libjavanet with the cpnet.c from 8062e73. There are no warnings (-W -Wall -pedantic). I did not run the new configure.ac on Haiku: I used my configured build tree. For comparison, "before" is master with #74, which uses gethostbyname() and gethostbyaddr() on Haiku.

One thread: the results before and after are the same:

getAllByName(localhost) = [localhost/127.0.0.1]
getAllByName(www.haiku-os.org) = [www.haiku-os.org/98.84.224.111, www.haiku-os.org/18.208.88.157]
getAllByName(github.com) = [github.com/140.82.113.3]
getAllByName(no-such-host.invalid): java.net.UnknownHostException: no-such-host.invalid
reverse(127.0.0.1) = localhost.localdomain
reverse(8.8.8.8) = dns.google
reverse(192.0.2.1) = 192.0.2.1
reverse(::1) = localhost.localdomain
reverse(2001:4860:4860::8888) = dns.google
connect github.com:80 -> github.com/140.82.113.3

(reverse() is getCanonicalHostName(). 192.0.2.1 has no PTR record.)

Forward lookups, 12 threads: 600 calls to InetAddress.getAllByName() for localhost, dns.google, one.one.one.one and www.gnu.org. The resolver cache is off (networkaddress.cache.ttl=0), thus each call goes to the native code. The test compares each result with the result from one thread.

Run Before (#74) After (#76)
1 Hang (stopped after 120 s) 0 wrong, 0 failed
2 Hang (stopped after 120 s) 0 wrong, 0 failed
3 2 wrong, 237 failed 0 wrong, 0 failed

Before, the wrong results included www.gnu.org = [127.0.0.1], www.gnu.org = [0.0.0.0] and localhost = [108.111.99.97] (the bytes of "loca"). This agrees with the shared static buffer in Haiku's gethostbyname() that you found. OpenJDK 8 on the same VM also gives 0 wrong and 0 failed.

Forward and reverse lookups together, 12 threads (480 calls): before, 74 errors. For example, 127.0.0.1 gave haiku.netlify.app, and dns.google gave [127.0.0.1]. After, 9 errors. All 9 are 127.0.0.1 = localhost instead of localhost.localdomain.

These 9 errors come from Haiku, not from #76. Haiku's gethostbyaddr_r() reads /etc/hosts with fparseln(), which uses fgetln() from libbsd. That fgetln() has a static buffer ("TODO: this function is not thread-safe") and frees the buffer at the end of the file. I can show the problem with a C program that calls getnameinfo() from 12 threads, without Java. For 127.0.0.1 some names are wrong. For an address that is not in /etc/hosts, the program stops in a double free and all threads hang:

Thread 120999 called debugger(): bogus pointer (double free?) 0xd73000
#4  free () from libroot.so
#5  fgetln () from libbsd.so
#6  fparseln () from libnetwork.so
#7  gethostent_r () from libnetwork.so
#8  _hf_gethtbyaddr () from libnetwork.so
#9  nsdispatch () from libnetwork.so
#10 gethostbyaddr_r () from libnetwork.so
#11 getnameinfo_inet () from libnetwork.so

getaddrinfo() does not have this problem, because it reads /etc/hosts with fgets() into a buffer on the stack. Classpath cannot fix this. It is necessary to fix it in Haiku. The code before #76 has the same problem, because gethostbyaddr() also calls gethostbyaddr_r().

If you merge #76, #74 is not necessary.

A small item that is not part of this change: in Java_java_net_VMInetAddress_getHostByName(), the error path after cpnet_getHostByName() throws the exception but does not call ReleaseStringUTFChars() for hostname.

An AI model (Claude Code, by Anthropic) did these tests and wrote this text. I directed the work and checked the results.

@guillerodriguez guillerodriguez changed the title Switch to getnameinfo/getaddrinfo for name/address resolution Switch to getnameinfo/getaddrinfo for address/name resolution Oct 17, 2026
@guillerodriguez

Copy link
Copy Markdown
Contributor Author

Thank you for the thorough testing!

I did not run the new configure.ac on Haiku: I used my configured build tree.

If you get a chance, it would be great if you could give the new configure.ac a go. This PR makes getnameinfo/getaddrinfo a hard requirement and will abort configure if they are missing, so we want to make sure they are detecten in Haiku as well (they should).

These 9 errors come from Haiku, not from #76. [...] The code before #76 has the same problem

I see, so at least #76 is not making things worse.
I'd guess this should be easy to fix on the Haiku side (since you mentioned getaddrinfo() already gets it right). Your C reproducer and backtrace would make a good bug report for them :-)

A small item that is not part of this change: [...]

Yes, this is on my radar. Thank you!

@guillerodriguez
guillerodriguez marked this pull request as ready for review October 17, 2026 17:31
@duncanmcqueen

Copy link
Copy Markdown
Contributor

I tested the new configure.ac on Haiku. It finds the two functions:

checking for library containing inet_pton... -lnetwork
checking for library containing getaddrinfo... none required
checking for library containing getnameinfo... none required

They are in libnetwork, and the inet_pton check before them already adds -lnetwork to LIBS. Thus the result is "none required".

What I did: I checked out 8062e73 without other changes, ran autoreconf -fi on Linux, and copied the tree to Haiku R1/beta6 x86_64 (hrev60228). Then I ran configure --enable-jni --disable-gtk-peer --disable-gconf-peer --disable-plugin --disable-gjdoc --disable-examples, make and make install. All steps completed without errors. There are no warnings in cpnet.c.

With the libjavanet from this build (JamVM 2.1.0):

  • The single-thread lookups give the same results as in my first comment.
  • 12 threads, 600 forward lookups, resolver cache off: 0 wrong and 0 failed, in 3 of 3 runs.
  • 12 threads, forward and reverse lookups together: 6 errors. All 6 are 127.0.0.1 = localhost instead of localhost.localdomain. One more run of this test hung in reverse lookups. Both come from the Haiku fgetln() problem.

Your C reproducer and backtrace would make a good bug report for them :-)

I agree. I prepared a small C reproducer and a report for the Haiku bug tracker.

@guillerodriguez

Copy link
Copy Markdown
Contributor Author

I tested the new configure.ac on Haiku. It finds the two functions:

Thank you!

@guillerodriguez
guillerodriguez merged commit 2ba4c48 into master Oct 19, 2026
3 checks passed
@guillerodriguez
guillerodriguez deleted the feat/cpnet-getnameinfo-getaddrinfo branch October 19, 2026 07:52
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