Skip to content

Commit 66cbebd

Browse files
authored
Use direct references instead of dynamic loading for cgo_free tests (#47624)
### What does this PR do? It changes the instrumentation for `cgo_free` tests in rtloader so that it doesn't rely on dynamic linking to get a reference to the `cgo_free` function, but grabs a reference to it via the dynamically loaded library (via `make3`). ### Motivation As part of migrating rtloader to Bazel ([ABLD-323](https://datadoghq.atlassian.net/browse/ABLD-323)), the existing setup, requiring dynamic linking makes things unnecessarily hard on Windows. The existing setup also doesn't reflect a "realistic" use case, in that linking against and opening the same dynamic library is not how the library would normally be used. The proposed change is more explicit. ### Describe how you validated your changes Passing tests. ### Additional Notes [ABLD-323]: https://datadoghq.atlassian.net/browse/ABLD-323?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ Co-authored-by: alex.lopez <alex.lopez@datadoghq.com>
1 parent 6c30b1c commit 66cbebd

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

rtloader/test/CMakeLists.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@ endif()
2222
set (CGO_CFLAGS \"-I${CMAKE_SOURCE_DIR}/include -I${CMAKE_SOURCE_DIR}/common -Wno-deprecated-declarations\")
2323

2424
if (WIN32)
25-
set (CGO_LDFLAGS -L${PROJECT_BINARY_DIR}/rtloader -ldatadog-agent-rtloader -lstdc++ -static)
25+
set (CGO_LDFLAGS \"-L${PROJECT_BINARY_DIR}/rtloader -ldatadog-agent-rtloader -lstdc++ -static\")
2626
elseif(APPLE)
27-
set (CGO_LDFLAGS -L${PROJECT_BINARY_DIR}/rtloader -ldatadog-agent-rtloader -ldl -rpath ${PROJECT_BINARY_DIR}/../../dev/lib)
27+
set (CGO_LDFLAGS \"-L${PROJECT_BINARY_DIR}/rtloader -ldatadog-agent-rtloader -ldl -rpath ${PROJECT_BINARY_DIR}/../../dev/lib\")
2828
else()
29-
set (CGO_LDFLAGS -L${PROJECT_BINARY_DIR}/rtloader -ldatadog-agent-rtloader -ldl)
29+
set (CGO_LDFLAGS \"-L${PROJECT_BINARY_DIR}/rtloader -ldatadog-agent-rtloader -ldl\")
3030
endif()
3131

32-
set (CGO_LDFLAGS \"-L${PROJECT_BINARY_DIR}/three/ ${CGO_LDFLAGS}\")
3332
add_custom_command(
3433
OUTPUT testPy3
3534
COMMAND ${CMAKE_COMMAND} -E env CGO_CFLAGS=${CGO_CFLAGS} CGO_LDFLAGS=${CGO_LDFLAGS} DYLD_LIBRARY_PATH=${LIBS_PATH} LD_LIBRARY_PATH=${LIBS_PATH} go test -mod=readonly -count=1 -p=1 ${PKGS}

rtloader/test/common/cgo_free_three.go

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,56 @@
66
package testcommon
77

88
/*
9-
#cgo CFLAGS: -I../../common
10-
#cgo !windows LDFLAGS: -L../../three/ -ldatadog-agent-three
11-
#cgo windows LDFLAGS: -L../../three/ -ldatadog-agent-three.dll
12-
#include "cgo_free.h"
9+
#cgo !windows LDFLAGS: -ldl
1310
14-
extern void cgo_free(void *ptr);
11+
// c_callCgoFree calls the cgo_free from the dynamically loaded `three` library.
12+
13+
#ifdef _WIN32
14+
#include <windows.h>
15+
#include <stdio.h>
1516
1617
void c_callCgoFree(void *ptr) {
17-
cgo_free(ptr);
18+
HMODULE handle = GetModuleHandleA("libdatadog-agent-three.dll");
19+
if (handle == NULL) {
20+
fprintf(stderr, "c_callCgoFree: libdatadog-agent-three.dll is not loaded\n");
21+
return;
22+
}
23+
typedef void (*cgo_free_t)(void *);
24+
cgo_free_t fn = (cgo_free_t)GetProcAddress(handle, "cgo_free");
25+
if (fn == NULL) {
26+
fprintf(stderr, "c_callCgoFree: cgo_free symbol not found in libdatadog-agent-three.dll\n");
27+
return;
28+
}
29+
fn(ptr);
1830
}
31+
32+
#else
33+
#include <dlfcn.h>
34+
#include <stdio.h>
35+
36+
#if defined(__linux__) || defined(__FreeBSD__)
37+
# define THREE_LIB "libdatadog-agent-three.so"
38+
#elif defined(__APPLE__)
39+
# define THREE_LIB "libdatadog-agent-three.dylib"
40+
#endif
41+
42+
void c_callCgoFree(void *ptr) {
43+
void *handle = dlopen(THREE_LIB, RTLD_NOLOAD | RTLD_LAZY);
44+
if (handle == NULL) {
45+
fprintf(stderr, "c_callCgoFree: " THREE_LIB " is not loaded\n");
46+
return;
47+
}
48+
typedef void (*cgo_free_t)(void *);
49+
cgo_free_t fn = (cgo_free_t)dlsym(handle, "cgo_free");
50+
if (fn == NULL) {
51+
fprintf(stderr, "c_callCgoFree: cgo_free symbol not found in " THREE_LIB "\n");
52+
dlclose(handle);
53+
return;
54+
}
55+
fn(ptr);
56+
dlclose(handle);
57+
}
58+
59+
#endif
1960
*/
2061
import "C"

0 commit comments

Comments
 (0)