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

Segfault when attempting to connect using hostname without internet (SRT v1.5.1) #34

Closed
geosir opened this issue Jun 8, 2023 · 2 comments

Comments

@geosir
Copy link

geosir commented Jun 8, 2023

The root cause of this issue actually seems to be with the SRT v1.5.1 library itself, which is the version srtdroid currently uses. The issue appears to be fixed in the upcoming SRT v1.5.2, but I thought I'd flag this and share a workaround in case anyone else is having this issue now.

Summary: When not connected to the internet, calling connect(ip, addr) on a Socket and using a hostname for ip results in a SIGSEGV (SEGV_MAPERR), causing the app to suffer a fatal crash that cannot be caught.

Steps to reproduce:

  1. Build and use the example srtdroid app (or a StreamPack example).
  2. Disconnect from the internet (e.g. with airplane mode)
  3. Set the IP to a hostname that needs name resolution (e.g. "stream.example.com")
  4. Attempt to start streaming as a client.

Expected Result: A catchable exception indicating a problem with resolving the hostname and connecting.

Actual Result: A segfault that causes the app to crash.

Crash Log:

13:51:47.158  A  Cmdline: io.github.thibaultbee.srtdroid.examples
13:51:47.158  A  pid: 8576, tid: 9021, name: pool-2-thread-1  >>> io.github.thibaultbee.srtdroid.examples <<<
13:51:47.158  A        #00 pc 00000000000d4ef0  /data/app/~~h1598JzzdNyrg5M-YH0GfQ==/io.github.thibaultbee.srtdroid.examples-AlyMPdm6iVgGO74-GGbuFg==/lib/arm64/libsrt.so (srt::sockaddr_any::set(sockaddr const*)+28) (BuildId: fdc6a370d36f35a297172064143a3ffda8af6552)
13:51:47.158  A        #01 pc 00000000000c12d8  /data/app/~~h1598JzzdNyrg5M-YH0GfQ==/io.github.thibaultbee.srtdroid.examples-AlyMPdm6iVgGO74-GGbuFg==/lib/arm64/libsrt.so (srt::sockaddr_any::sockaddr_any(sockaddr const*, int)+48) (BuildId: fdc6a370d36f35a297172064143a3ffda8af6552)
13:51:47.158  A        #02 pc 00000000000c1714  /data/app/~~h1598JzzdNyrg5M-YH0GfQ==/io.github.thibaultbee.srtdroid.examples-AlyMPdm6iVgGO74-GGbuFg==/lib/arm64/libsrt.so (srt::CUDTUnited::connect(int, sockaddr const*, int, int)+64) (BuildId: fdc6a370d36f35a297172064143a3ffda8af6552)
13:51:47.158  A        #03 pc 00000000000c9e2c  /data/app/~~h1598JzzdNyrg5M-YH0GfQ==/io.github.thibaultbee.srtdroid.examples-AlyMPdm6iVgGO74-GGbuFg==/lib/arm64/libsrt.so (srt::CUDT::connect(int, sockaddr const*, int, int)+76) (BuildId: fdc6a370d36f35a297172064143a3ffda8af6552)
13:51:47.158  A        #04 pc 00000000001b764c  /data/app/~~h1598JzzdNyrg5M-YH0GfQ==/io.github.thibaultbee.srtdroid.examples-AlyMPdm6iVgGO74-GGbuFg==/lib/arm64/libsrt.so (srt_connect+40) (BuildId: fdc6a370d36f35a297172064143a3ffda8af6552)
13:51:47.159  A        #05 pc 0000000000048cbc  /data/app/~~h1598JzzdNyrg5M-YH0GfQ==/io.github.thibaultbee.srtdroid.examples-AlyMPdm6iVgGO74-GGbuFg==/lib/arm64/libsrtdroid.so (nativeConnect(_JNIEnv*, _jobject*, _jobject*)+268) (BuildId: 93b3944d1b7a9566336affa7f58ce4cf754645ed)
13:51:47.159  A        #08 pc 00000000000042d6  [anon:dalvik-classes3.dex extracted in memory from /data/app/~~h1598JzzdNyrg5M-YH0GfQ==/io.github.thibaultbee.srtdroid.examples-AlyMPdm6iVgGO74-GGbuFg==/base.apk!classes3.dex] (io.github.thibaultbee.srtdroid.models.Socket.connect+10)
13:51:47.159  A        #10 pc 0000000000004288  [anon:dalvik-classes3.dex extracted in memory from /data/app/~~h1598JzzdNyrg5M-YH0GfQ==/io.github.thibaultbee.srtdroid.examples-AlyMPdm6iVgGO74-GGbuFg==/base.apk!classes3.dex] (io.github.thibaultbee.srtdroid.models.Socket.connect+20)
13:51:47.159  A        #12 pc 000000000000131e  [anon:dalvik-classes8.dex extracted in memory from /data/app/~~h1598JzzdNyrg5M-YH0GfQ==/io.github.thibaultbee.srtdroid.examples-AlyMPdm6iVgGO74-GGbuFg==/base.apk!classes8.dex] (io.github.thibaultbee.srtdroid.examples.tests.TestClient.launchImpl+70)
13:51:47.159  A        #14 pc 00000000000016e6  [anon:dalvik-classes8.dex extracted in memory from /data/app/~~h1598JzzdNyrg5M-YH0GfQ==/io.github.thibaultbee.srtdroid.examples-AlyMPdm6iVgGO74-GGbuFg==/base.apk!classes8.dex] (io.github.thibaultbee.srtdroid.examples.tests.Test.launch$lambda-0+46)
13:51:47.159  A        #16 pc 0000000000001630  [anon:dalvik-classes8.dex extracted in memory from /data/app/~~h1598JzzdNyrg5M-YH0GfQ==/io.github.thibaultbee.srtdroid.examples-AlyMPdm6iVgGO74-GGbuFg==/base.apk!classes8.dex] (io.github.thibaultbee.srtdroid.examples.tests.Test.$r8$lambda$jc03Nn7oJUZ8jpk4I2t67O96Xqs+0)
13:51:47.159  A        #18 pc 00000000000011e4  [anon:dalvik-classes8.dex extracted in memory from /data/app/~~h1598JzzdNyrg5M-YH0GfQ==/io.github.thibaultbee.srtdroid.examples-AlyMPdm6iVgGO74-GGbuFg==/base.apk!classes8.dex] (io.github.thibaultbee.srtdroid.examples.tests.Test$$ExternalSyntheticLambda0.run+12)

Analysis:

Socket.connect(ip, addr) is an alias for Socket.connect(InetSocketAddress(ip, addr)). InetSocketAddress provides name resolution if ip is a hostname and not an IP address. However, if name resolution fails, its getAddress() method returns null. Since InetSocketAddress::getNative(...) returns nullptr when getAddress() is null (InetSocketAddress.h:55), the sockaddr pointer passed into srt_connect(...) at glue.cpp:300 is a null pointer.

In the SRT library, srt_connect wraps CUDT::connect which calls CUDTUnited::connect. In v1.5.1, there is no null check for the sockaddr*, and it gets dereferenced when constructing sockaddr_any in the method at netinet_any.h:148, resulting in the segfault. In v1.5.2, they've added a null check at api.cpp:1244 which appears to fix the issue.

Workarounds:

A. Compile srtdroid by changing the SRT version to 1.5.2-rc2 or similar in CMakeLists.txt. This seems to fix the crash in the srtdroid example app (showing a connection error popup instead). I haven't tested if any features are affected by the new version though.

B. Do val foo = InetSocketAddress(ip, port) yourself, check if foo.address is null, and call Socket.connect(foo) directly with this safe InetSocketAddress so no null pointer is passed into the underlying library. This is the solution I've used in the extension to StreamPack I'm working on so I didn't have to recompile srtdroid.

@geosir geosir changed the title Segfault when attempting to connect using hostname without internet Segfault when attempting to connect using hostname without internet (SRT v1.5.1) Jun 8, 2023
@ThibaultBee
Copy link
Owner

Hi,

Thank for you clear report.

I already noticed this behavior, that's why I made this fix: Haivision/srt@e082f30

As 1.5.2-rc2 is not a stable version, so I can't use it in srtdroid.

My original plan was to wait for the release of 1.5.2. Do you have a reason why we should not wait for 1.5.2?

@geosir
Copy link
Author

geosir commented Jun 9, 2023

Nice, I didn't realize that was your change :)

I agree that it makes sense to wait for the stable 1.5.2 release. I just figured it might be useful to submit a report in case anyone was searching for the same issue.

Cheers,
George

@geosir geosir closed this as completed Jun 9, 2023
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