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

dlopen(NULL) returns a handle for libjulia-internal, not the process #120

Open
maleadt opened this issue May 3, 2023 · 2 comments
Open

Comments

@maleadt
Copy link

maleadt commented May 3, 2023

The CBinding.jl code contains the following:

CBinding.jl/src/context.jl

Lines 242 to 244 in 38e76e5

# WARNING: risky hack to trick Julia dlopen into ccalling the C dlopen with NULL (meaning to dlopen current process rather than a library)
struct _NullCString <: AbstractString end
Base.cconvert(::Type{Cstring}, ::_NullCString) = Cstring(C_NULL)

CBinding.jl/src/context.jl

Lines 293 to 294 in 38e76e5

lib = dlopen(_NullCString())
lib == C_NULL && error("Failed to dlopen Julia process")

This broke on 1.10, but PkgEval spotted it and it will soon be fixed, see JuliaLang/julia#49010 (comment) and JuliaLang/julia#49611

However, it should be noted that this operation does not actually return a handle to the process, but to libjulia-internal (JuliaLang/julia#49010 (comment)). Opening an issue to see if this is intended, and if so, maybe the comment should be updated.

@krrutkow
Copy link
Member

krrutkow commented May 3, 2023

Good catch! The purpose of the dlopen(NULL) is to have a handle for calling dlsym to find symbols in the global/process symbol table (as described by the POSIX dlopen documentation) that are not found in an explicitly chosen library opened with dlopen("..."). It appears that the only current reliance on dlopen(NULL) in CBinding is to enable meaningful warning messages.

However, this leads to the question of how to correctly determine if a symbol is capable of being referenced with ccall or cglobal. The case of determining at load-time that an emitted ccall((:function, "library"), ...) will fail using dlopen+dlsym is clear, but how should a ccall(:function, ...) be checked? Again, this is not a critical feature as it is just to provide load-time warnings of the potential for errors at run-time, but it is an important diagnostic aid.

@maleadt
Copy link
Author

maleadt commented May 4, 2023

this leads to the question of how to correctly determine if a symbol is capable of being referenced with ccall or cglobal. The case of determining at load-time that an emitted ccall((:function, "library"), ...) will fail using dlopen+dlsym is clear, but how should a ccall(:function, ...) be checked?

Maybe @topolarity has some thoughts, as he recently worked on that code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants