go-wca icon indicating copy to clipboard operation
go-wca copied to clipboard

IMMDevice GetId panics due to a memory access violation

Open ThiefMaster opened this issue 4 years ago • 3 comments

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go1.15.7

What operating system and processor architecture are you using (go env)?

Windows 10 (20H2) / amd64

What did you do?

package main

import (
	"fmt"
	"log"

	"github.com/go-ole/go-ole"
	"github.com/moutend/go-wca/pkg/wca"
)

func main() {
	err := ole.CoInitializeEx(0, ole.COINIT_APARTMENTTHREADED)
	if err != nil {
		log.Fatal(err)
	}
	defer ole.CoUninitialize()

	var mmde *wca.IMMDeviceEnumerator
	if err = wca.CoCreateInstance(wca.CLSID_MMDeviceEnumerator, 0, wca.CLSCTX_ALL, wca.IID_IMMDeviceEnumerator, &mmde); err != nil {
		return
	}
	defer mmde.Release()

	var mmd *wca.IMMDevice
	if err = mmde.GetDefaultAudioEndpoint(wca.ERender, wca.EConsole, &mmd); err != nil {
		return
	}
	defer mmd.Release()
	var defaultDevID string
	if err = mmd.GetId(&defaultDevID); err != nil {
		return
	}

	fmt.Println(defaultDevID)
}

What did you expect to see?

A valid device ID

What did you see instead?

unexpected fault address 0x376c6910
fatal error: fault
[signal 0xc0000005 code=0x0 addr=0x376c6910 pc=0x2afe55]

goroutine 1 [running]:
runtime.throw(0x2d9240, 0x5)
	F:/Go/src/runtime/panic.go:1116 +0x79 fp=0xc0000c9df8 sp=0xc0000c9dc8 pc=0x225f19
runtime.sigpanic()
	F:/Go/src/runtime/signal_windows.go:249 +0x24f fp=0xc0000c9e28 sp=0xc0000c9df8 pc=0x2379af
github.com/moutend/go-wca/pkg/wca.mmdGetId(0x25b376af920, 0xc0000c9f20, 0xc0000c9f10, 0x0)
	D:/Dev/go/src/github.com/moutend/go-wca/pkg/wca/IMMDevice_windows.go:60 +0xd5 fp=0xc0000c9ec8 sp=0xc0000c9e28 pc=0x2afe55
github.com/moutend/go-wca/pkg/wca.(*IMMDevice).GetId(...)
	D:/Dev/go/src/github.com/moutend/go-wca/pkg/wca/IMMDevice.go:36
main.main()
	D:/Dev/go/src/github.com/thiefmaster/audiotarget/main.go:30 +0x1c9 fp=0xc0000c9f88 sp=0xc0000c9ec8 pc=0x2b1969
runtime.main()
	F:/Go/src/runtime/proc.go:204 +0x209 fp=0xc0000c9fe0 sp=0xc0000c9f88 pc=0x2286c9
runtime.goexit()
	F:/Go/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc0000c9fe8 sp=0xc0000c9fe0 pc=0x254c81

The panic happens in this line: u := *(*uint16)(unsafe.Pointer(uintptr(start) + 2*uintptr(i))) of mmdGetId

ThiefMaster avatar Jan 31 '21 14:01 ThiefMaster

Replacing var strIDPtr uint32 with var strIDPtr uint64 fixed it, so I sent a PR for this.

ThiefMaster avatar Jan 31 '21 16:01 ThiefMaster

Encountered the same panic. Thanks for posting this.

Replacing var strIDPtr uint32 with var strIDPtr uint64 fixed it, so I sent a PR for this.

Maybe uintptr instead of uint64 should be more appropriate since it holds a pointer, regardless of the platform you are in:

uintptr is an integer type that is large enough to hold the bit pattern of any pointer.

https://golang.org/pkg/builtin/#uintptr

emarj avatar Mar 30 '21 08:03 emarj

I faced the same issue, the suggestions described above fix the fault address for the following functions.

pkg/wca/IAudioSessionControl_windows.go

  • ascGetDisplayName
  • ascGetIconPath

Oppodelldog avatar Nov 20 '21 15:11 Oppodelldog