Skip to content

Commit 9a99fc2

Browse files
committed
Fix calling function
1 parent 05556fc commit 9a99fc2

3 files changed

Lines changed: 43 additions & 10 deletions

File tree

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module yocto/pkcs11-forkfix
22

33
go 1.20
44

5-
require github.com/jinzhu/copier v0.4.0 // indirect
5+
require (
6+
github.com/ebitengine/purego v0.9.1 // indirect
7+
github.com/jinzhu/copier v0.4.0 // indirect
8+
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
github.com/ebitengine/purego v0.9.1 h1:a/k2f2HQU3Pi399RPW1MOaZyhKJL9w/xFpKAg4q1s0A=
2+
github.com/ebitengine/purego v0.9.1/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
13
github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8=
24
github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=

main.go

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ import "fmt"
77
import "os"
88
import "unsafe"
99

10+
import "github.com/ebitengine/purego"
11+
1012
func main() {}
1113

1214
var libraryHandle unsafe.Pointer = nil
15+
var libraryHandlePure uintptr = nil
1316
var libraryPID int = -1
17+
var libraryPIDPure int = -1
1418

15-
func getDynamicLibrarySymbol(functionName string) any {
19+
func getDynamicLibrary() unsafe.Pointer {
1620
if libraryHandle == nil || libraryPID == -1 || libraryPID != os.Getpid() {
1721
if libraryHandle != nil {
1822
C.dlclose(libraryHandle)
@@ -23,24 +27,48 @@ func getDynamicLibrarySymbol(functionName string) any {
2327
}
2428
libraryPID = os.Getpid()
2529
}
26-
if libraryHandle == nil {
30+
return nil
31+
}
32+
33+
func getDynamicLibraryPure() {
34+
if libraryHandlePure == nil || libraryPIDPure == -1 || libraryPIDPure != os.Getpid() {
35+
if libraryHandlePure != nil {
36+
purego.Dlclose(libraryHandlePure)
37+
}
38+
libraryHandlePure, _ = purego.Dlopen(os.Getenv("PKCS11_SUBMODULE"), purego.RTLD_NOW|purego.RTLD_GLOBAL)
39+
if libraryHandlePure == nil {
40+
return nil
41+
}
42+
libraryPIDPure = os.Getpid()
43+
}
44+
return nil
45+
}
46+
47+
func getDynamicLibrarySymbol(functionName string) any {
48+
lh := getDynamicLibrary()
49+
if lh == nil {
2750
return nil
2851
}
52+
return C.dlsym(lh, C.CString(functionName))
53+
}
2954

30-
return C.dlsym(libraryHandle, C.CString(functionName))
55+
func registerDynamicLibrarySymbolPure(function any, functionName string) any {
56+
lh := getDynamicLibraryPure()
57+
if lh == nil {
58+
return nil
59+
}
60+
purego.RegisterLibFunc(&function, lh, functionName)
61+
return function
3162
}
3263

3364
//export C_CancelFunction
3465
func C_CancelFunction(hSession C.CK_SESSION_HANDLE) C.CK_RV { // Since v1.0
35-
symbol := getDynamicLibrarySymbol("C_CancelFunction")
36-
if symbol == nil {
66+
var function func(C.CK_SESSION_HANDLE) C.CK_RV
67+
function = registerDynamicLibrarySymbolPure("C_CancelFunction")
68+
if function == nil {
3769
fmt.Println("Failed getting symbol for this function.")
3870
return C.CKR_FUNCTION_NOT_SUPPORTED
3971
}
40-
41-
type functionType func(C.CK_SESSION_HANDLE) C.CK_RV
42-
function := *(*functionType)(unsafe.Pointer(&symbol))
43-
4472
return function(hSession)
4573
}
4674

0 commit comments

Comments
 (0)