Skip to content

Commit 2a8229d

Browse files
misc/cgo/test: get uintptr, not pointer, from dlopen
The dlopen function returns an opaque handle, and it is possible for it to look like a Go pointer, causing garbage collector and cgo confusion. Fixes #23663 Change-Id: Id080e2bbcee8cfa7ac4a457a927f96949eb913f8 Reviewed-on: https://go-review.googlesource.com/91596 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Russ Cox <[email protected]>
1 parent 851e98f commit 2a8229d

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

misc/cgo/test/issue4029.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@
44

55
// +build !windows
66

7+
#include <stdint.h>
8+
#include <dlfcn.h>
9+
10+
// Write our own versions of dlopen/dlsym/dlclose so that we represent
11+
// the opaque handle as a Go uintptr rather than a Go pointer to avoid
12+
// garbage collector confusion. See issue 23663.
13+
14+
uintptr_t dlopen4029(char* name, int flags) {
15+
return (uintptr_t)(dlopen(name, flags));
16+
}
17+
18+
uintptr_t dlsym4029(uintptr_t handle, char* name) {
19+
return (uintptr_t)(dlsym((void*)(handle), name));
20+
}
21+
22+
int dlclose4029(uintptr_t handle) {
23+
return dlclose((void*)(handle));
24+
}
25+
726
void call4029(void *arg) {
827
void (*fn)(void) = arg;
928
fn();

misc/cgo/test/issue4029.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
package cgotest
88

99
/*
10+
#include <stdint.h>
1011
#include <dlfcn.h>
1112
#cgo linux LDFLAGS: -ldl
1213
13-
extern void call4029(void *arg);
14+
extern uintptr_t dlopen4029(char*, int);
15+
extern uintptr_t dlsym4029(uintptr_t, char*);
16+
extern int dlclose4029(uintptr_t);
17+
18+
extern void call4029(uintptr_t arg);
1419
*/
1520
import "C"
1621

@@ -51,15 +56,15 @@ func test4029(t *testing.T) {
5156
}
5257

5358
func loadThySelf(t *testing.T, symbol string) {
54-
this_process := C.dlopen(nil, C.RTLD_NOW)
55-
if this_process == nil {
59+
this_process := C.dlopen4029(nil, C.RTLD_NOW)
60+
if this_process == 0 {
5661
t.Error("dlopen:", C.GoString(C.dlerror()))
5762
return
5863
}
59-
defer C.dlclose(this_process)
64+
defer C.dlclose4029(this_process)
6065

61-
symbol_address := C.dlsym(this_process, C.CString(symbol))
62-
if symbol_address == nil {
66+
symbol_address := C.dlsym4029(this_process, C.CString(symbol))
67+
if symbol_address == 0 {
6368
t.Error("dlsym:", C.GoString(C.dlerror()))
6469
return
6570
}

0 commit comments

Comments
 (0)