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

cmd/cgo: Go DLL crashes on Windows 10 #58009

Closed
diogosimao opened this issue Jan 25, 2023 · 6 comments
Closed

cmd/cgo: Go DLL crashes on Windows 10 #58009

diogosimao opened this issue Jan 25, 2023 · 6 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge

Comments

@diogosimao
Copy link

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

$ go version
go version go1.19.4 linux/amd64

and

$ go version
go version go1.19.5 windows/amd64

Does this issue reproduce with the latest release?

Yes

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

Two operating systems and same architecture.

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/diogo/.cache/go-build"
GOENV="/home/diogo/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/diogo/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/diogo/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go-1.19"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.19/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19.4"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/diogo/test/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2806850208=/tmp/go-build -gno-record-gcc-switches"

and

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\diogo\AppData\Local\go-build
set GOENV=C:\Users\diogo\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\diogo\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\diogo\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.19.5
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\diogo\Documents\go.mod
set GOWORK=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=C:\Users\diogo\AppData\Local\Temp\go-build3585693648=/tmp/go-build -gno-record-gcc-switches

What did you do?

Basically, a DLL is built with Go, which is filling a output buffer. DLL is loaded by C++ code. On Windows 10 it crashes and on Ubuntu 20.04 it follows the course. The Go portion of code also verifies if a file extracted by the C++ portion exists or not.

test.go

package main

//#include <stdlib.h>
//#include <string.h>
import "C"

import (
	"os"
	"unsafe"
)

func main() {
}

//export Test
func Test(FilePath *C.char, OutBuffer *C.char, BufferLen C.size_t) C.int {
	SrtFilePath := C.GoString(FilePath)

	if _, err := os.Stat(SrtFilePath); err != nil {
		return (C.int)(0)
	}

	Result := "output"
	ResultLen := len(Result)
	CResult := C.CString(Result)

	OutSize := func(a int, b int) int {
		if a < b {
			return a
		}
		return b
	}

	C.memset(unsafe.Pointer(OutBuffer), 0, BufferLen)
	C.memcpy(unsafe.Pointer(OutBuffer), unsafe.Pointer(CResult), C.size_t(OutSize(int(BufferLen), ResultLen)))
	return (C.int)(1)
}

The executable (the runner) test built with C++:

test.cc
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#if defined(_WIN32)
# include <windows.h>
#elif defined(__linux__)
# include <dlfcn.h>
# include <cstring>
#endif // defined(_WIN32)

#if defined(_WIN32)
constexpr char libname[] = "test.dll";
#elif defined(__linux__)
constexpr char libname[] = "test.so";
#endif // defined(_WIN32)

namespace {
    bool get_data_from_go(std::string& outbuf, const std::filesystem::path& document_path, const std::filesystem::path& lib_path);
    bool extract_test_artifact(const std::filesystem::path& filepath);
} // anonymous namespace

int main(void) {
    std::filesystem::path lib_path = std::filesystem::current_path() / std::filesystem::path(libname);
    std::string buf;
    std::cout << "Buckle up..." << std::endl;
    if (get_data_from_go(buf, "404.doc", lib_path) == false) {
        std::cout << "[1] OK... buf = `" << buf << "`" << std::endl;
    }
    if (extract_test_artifact(std::filesystem::current_path() / "test.artifact") == true) {
        std::cout << "[2] OK..." << std::endl;
    }
    if (get_data_from_go(buf, std::filesystem::current_path() / "test.artifact", lib_path) == true) {
        std::cout << "[3] OK... buf = `" << buf << "`" << std::endl;
    }
    std::error_code ec;
    if (std::filesystem::remove(std::filesystem::current_path() / "test.artifact", ec) == true) {
        std::cout << "[4] OK..." << std::endl;
    }
    std::cout << "Congrats! We are still alive!! Issue solved! :)" << std::endl;
    return EXIT_SUCCESS;
}

namespace {

bool get_data_from_go(std::string& outbuf, const std::filesystem::path& document_path, const std::filesystem::path& lib_path) {
    if (document_path.empty()) {
        return false;
    }
#if defined(_WIN32)
    HMODULE lib_handle = LoadLibraryEx(lib_path.u8string().c_str(), NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR);
#elif defined(__linux__)
    void *lib_handle = dlopen(lib_path.u8string().c_str(), RTLD_LAZY);
#endif // defined(_WIN32)
    if (lib_handle == nullptr) {
        return false;
    }
    typedef int(*TestFunc)(char *, char *, size_t);
    TestFunc Test = nullptr;
#if defined(_WIN32)
    Test = (TestFunc)GetProcAddress(lib_handle, "Test");
#elif defined(__linux__)
    Test = (TestFunc)dlsym(lib_handle, "Test");
#endif // defined(_WIN32)
    unsigned int done = 0;
    if (Test != nullptr) {
        constexpr int kBufSize = 1<<20;
        char *buf = new char[kBufSize]{0};
        done = Test((char *)document_path.string().c_str(), buf, kBufSize - 1);
        if (done) {
            outbuf = std::string(buf, strnlen(buf, kBufSize));
        }
        delete []buf;
    }
#if defined(_WIN32)
    FreeLibrary(lib_handle);
#elif defined(__linux__)
    dlclose(lib_handle);
#endif // defined(_WIN32)
    return done;
}

bool extract_test_artifact(const std::filesystem::path& filepath) {
    static unsigned char DataPayload[] = {
      0x46, 0x00, 0x69, 0x00, 0x6c, 0x00, 0x65, 0x00, 0x2f, 0x00, 0x44, 0x00,
      0x6f, 0x00, 0x63, 0x00, 0x50, 0x00, 0x72, 0x00, 0x6f, 0x00, 0x70, 0x00,
      0x65, 0x00, 0x72, 0x00, 0x74, 0x00, 0x69, 0x00, 0x65, 0x00, 0x73, 0x00,
      0x1a, 0x00, 0x4e, 0x00, 0x69, 0x00, 0x63, 0x00, 0x68, 0x00, 0x6f, 0x00,
      0x6c, 0x00, 0x61, 0x00, 0x73, 0x00, 0x20, 0x00, 0x4c, 0x00, 0x2e, 0x00,
      0x20, 0x00, 0x47, 0x00, 0x65, 0x00, 0x6f, 0x00, 0x72, 0x00, 0x67, 0x00,
      0x61, 0x00, 0x6b, 0x00, 0x6f, 0x00, 0x70, 0x00, 0x6f, 0x00, 0x75, 0x00,
      0x6c, 0x00, 0x6f, 0x00, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0xFF, 0xFF, 0xFF, 0xFF
    };
    static size_t DataPayload_len = sizeof(DataPayload) / sizeof(DataPayload[0]);
    std::ofstream ofs(filepath, std::ios_base::binary);
    if (!ofs.is_open()) {
        return false;
    }
    ofs.write(reinterpret_cast<char *>(DataPayload), DataPayload_len);
    ofs.close();
    return true;
}

} // anonymous namespace

On Ubuntu 20.04 buiit it with:

$ gcc --version
gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
make.sh
go build -o test.so -buildmode=c-shared test.go
g++ test.cc -otest -std=c++17 -ldl

And on Windows 10 built it with:

$ gcc --version
gcc (tdm64-1) 10.3.0
make.sh
@go build -o test.dll -buildmode=c-shared test.go
@g++ test.cc -otest -std=c++17

What did you expect to see?

I expected to see an empty output based on a int return of DLL exported function and a output string also filled by it.

test ./test    
Buckle up...
[1] OK... buf = ``
[2] OK...
[3] OK... buf = `output`
[4] OK...
Congrats! We are still alive!! Issue solved! :)

What did you see instead?

Instead, I see a broken sequence os steps.

C:\Users\diogo\Documents\test>test.exe
Buckle up...
[1] OK... buf = ``
[2] OK...
@diogosimao diogosimao changed the title affected/package: runtime/cgo: Go DLL crashes on Windows 10 Jan 25, 2023
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jan 25, 2023
@diogosimao diogosimao changed the title runtime/cgo: Go DLL crashes on Windows 10 cmd/cgo: Go DLL crashes on Windows 10 Jan 25, 2023
@diogosimao
Copy link
Author

It is worth mentioning that it crashes on the second time the DLL is loaded.

@cherrymui
Copy link
Member

It is worth mentioning that it crashes on the second time the DLL is loaded.

Are you loading the same DLL twice in the same process? Currently, the c-shared buildmode assumes the library it produces is the only instance of the Go runtime in the process. It usually doesn't work well if there is another instance of Go runtime in the same process's address space (and this is not supported.). For the same library, it is possible that the Linux dynamic loader deduplicates all the symbols, so it ends up essentially like just a single instance. On Windows, the dynamic loader may not work this way.

Could you explain why you want to load the DLL multiple times? Would it be possible not to do that? Thanks.

@diogosimao
Copy link
Author

Yes, it's been loaded twice but it's also been freed before loaded again. I wanted to load multiples times in order to unload inside a destructor method. I will look into the possibility of doing that.

@cherrymui
Copy link
Member

I'm also not sure if unloading/freeing works with Go c-shared libraries. From Go runtime's perspective we don't know if there is any dangling reference. Also, not sure unloading really gives a clean state. At least, the Go library doesn't try to clean up anything when it is unloaded. I'd suggest not unloading. Thanks.

@qmuntal
Copy link
Contributor

qmuntal commented Jan 26, 2023

Freeing a Go DLL does not generally work, can confirm that.

@seankhliao
Copy link
Member

Duplicate of #11100

@seankhliao seankhliao marked this as a duplicate of #11100 Jan 28, 2023
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jan 28, 2023
@golang golang locked and limited conversation to collaborators Jan 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge
Projects
None yet
Development

No branches or pull requests

5 participants