go-astiav: Unexpected segfault error using astiav.FrameData .ToImage()

Hello, I’m encountering an issue with the lib, but I’m still unsure yet if it’s because I’m missing a check on my side or if there is an issue with the lib.

My usecase is to retrieve frames from an H264 socket in order for me to retrieve a JPEG image at the end. (see this discussion we had 2 months ago : https://github.com/asticode/go-astiav/discussions/62 )

It has been working pretty well, but in some case I encounter a segfault. What I can say about my usecase if it helps is that it’s retrieving frames from a phone, meaning that the image can rotate over time. I’m having a feeling it could be linked, yet I’m not completely sure. ( Width x height -> height x width for the image size)

Here is a piece of the code, that we have been discussing before :


        var decCodec *astiav.Codec
	var decCodecContext *astiav.CodecContext

	if decCodec = astiav.FindDecoder(astiav.CodecIDH264); decCodec == nil {
                 //print error
		return
	}

	if decCodecContext = astiav.AllocCodecContext(decCodec); decCodecContext == nil {
		//print error
		return
	}
	defer decCodecContext.Free()

	if err := decCodecContext.Open(decCodec, nil); err != nil {
		//print error
		return
	}

	pkt := astiav.AllocPacket()
	defer pkt.Free()

	f := astiav.AllocFrame()
	defer f.Free()

	inputFormatContext := astiav.AllocFormatContext()
	defer inputFormatContext.Free()

	ioContext, err := astiav.AllocIOContext(0x10000, false, c.Conn.Read, nil, nil)
	if err != nil {
		//print error
		return
	}
	defer ioContext.Free()

	inputFormatContext.SetPb(ioContext)

	inputFormat := astiav.FindInputFormat("h264")
	if inputFormat == nil {
		//print error
		return
	}

	if err := inputFormatContext.OpenInput("", inputFormat, nil); err != nil {
		//print error
		return
	}
	defer inputFormatContext.CloseInput()

	//Read frames

	for {
		select {
		case <-Stop:
			//stopping h264 processing
			return
		default:
			f.Unref()
			pkt.Unref()
			if err := inputFormatContext.ReadFrame(pkt); err != nil {
				//print error
		                 return
			}

			// Send packet
			if err := decCodecContext.SendPacket(pkt); err != nil {
				//print error
		                 return
			}
			// Receive frame
			if err := decCodecContext.ReceiveFrame(f); err != nil {
				//print error
		                 return
			}

			image, err := f.Data().GuessImageFormat()
			if err != nil {
				//print error
		                 return
			}

			err = f.Data().ToImage(image)
			if err != nil {
				//print error
		                 return
			}

			var buf bytes.Buffer
			err = jpeg.Encode(&buf, image, nil)
			if err != nil {
				//print error
		                 return
			}

...

Sometimes, I’m having a segfault at this point in the code :

err = f.Data().ToImage(image)
if err != nil {
    //print error
    return
}

Here is the stacktrace :

unexpected fault address 0x7f7bf6fd2000
fatal error: fault
[signal SIGSEGV: segmentation violation code=0x1 addr=0x7f7bf6fd2000 pc=0x108b20a39]

goroutine 127 gp=0xc0005036c0 m=5 mp=0xc000100008 [running]:
runtime.throw({0x10938f193?, 0x101011e88b0?})
	/usr/local/Cellar/go/1.22.4/libexec/src/runtime/panic.go:1023 +0x5c fp=0xc0011e8860 sp=0xc0011e8830 pc=0x108ae973c
runtime.sigpanic()
	/usr/local/Cellar/go/1.22.4/libexec/src/runtime/signal_unix.go:895 +0x285 fp=0xc0011e88c0 sp=0xc0011e8860 pc=0x108b01525
runtime.memmove()
	/usr/local/Cellar/go/1.22.4/libexec/src/runtime/memmove_amd64.s:427 +0x4f9 fp=0xc0011e88c8 sp=0xc0011e88c0 pc=0x108b20a39
runtime.gobytes(0x7f7bf6f29000, 0x151800)
	/usr/local/Cellar/go/1.22.4/libexec/src/runtime/string.go:309 +0x65 fp=0xc0011e8910 sp=0xc0011e88c8 pc=0x108b07105
github.com/asticode/go-astiav._Cfunc_GoBytes(...)
	_cgo_gotypes.go:2197
github.com/asticode/go-astiav.bytesFromC.func1(0x7f7bf6f29000, 0x108ab682b?)
	/Users/valkhes/go/pkg/mod/github.com/asticode/go-astiav@v0.15.0/bytes.go:30 +0x3e fp=0xc0011e8948 sp=0xc0011e8910 pc=0x109293cde
github.com/asticode/go-astiav.bytesFromC(0xc0011e8978)
	/Users/valkhes/go/pkg/mod/github.com/asticode/go-astiav@v0.15.0/bytes.go:30 +0x38 fp=0xc0011e8970 sp=0xc0011e8948 pc=0x109293c78
github.com/asticode/go-astiav.(*frameDataFrame).planeBytes(0xc000614028?, 0x2?)
	/Users/valkhes/go/pkg/mod/github.com/asticode/go-astiav@v0.15.0/frame_data.go:199 +0x3d fp=0xc0011e89a0 sp=0xc0011e8970 pc=0x10929c8bd
github.com/asticode/go-astiav.(*FrameData).toImageYCbCr(0xc0011e8e80, 0xc00022a380, 0xc00022a398, 0xc00022a3b0, 0xc00022a3c8, 0xc00022a3d0, 0xc00022a3d8, 0xc00022a3e0)
	/Users/valkhes/go/pkg/mod/github.com/asticode/go-astiav@v0.15.0/frame_data.go:116 +0xcb fp=0xc0011e89c8 sp=0xc0011e89a0 pc=0x10929c12b
github.com/asticode/go-astiav.(*FrameData).ToImage(0xc0011e8e90?, {0x109753130?, 0xc00022a380?})
	/Users/valkhes/go/pkg/mod/github.com/asticode/go-astiav@v0.15.0/frame_data.go:161 +0x18a fp=0xc0011e8a28 sp=0xc0011e89c8 pc=0x10929c54a

By any chance, do you have an idea about what can be happening here ? Any way to have more information about the issue, maybe a way to check if my incoming frame is corrupted, or a check I have been missing ?

Thanks a lot !

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 22 (9 by maintainers)

Most upvoted comments

Hello @asticode !

I’ve been running the files I sent you before with my code, it stopped crashing. I’ve then launched the “real” program I have been using with the phone and made multiple rotation, nothing breaks anymore !

It seems your fix has been working succesfully.

Thanks a lot again for all the help and the quick answers !

I’ve pushed a fix on the fix-segfault branch (with it I’m not experiencing any segfaults locally anymore), would you mind testing your code with it and let me know whether you still experience segfaults?

Hello !

Sorry for the delay, I never set SetLogLevel() and SetLogCallback(), I realized I never used it, which may be a reason I missed some errors.

I’ll try to add this tomorrow to the code, maybe I’ll catch new errors. For the moment I was just printing returning errors from all the methods, but I suppose the log callback would help to retrieve ffmpeg internal errors too.

It is nice if you managed to reproduce too.

Note that I have a temporary solution to replace go astiav for the moment, so its not a critical issue for me if it takes some time to find the problem, but at least if I can help to improve go-astiav, I’ll do what I can.

Hi, thanks again for your quick answers.

I have been running some tests this morning to confirm that the issue was only on my Intel mac and not on my ARM mac, and it was the case. As I don’t think the issue was in your code at this point (that would be strange), I realized that it could be because of ffmpeg@5 or some dependencies of ffmpeg. I have been updating everything on my mac with brew and for the moment it seems it is working again. I’ll come back after a day of testing to validate if everything is now fixed, but it may not be on your side.