Skip to content

Fix dangling pointer problem in _linker.py #516

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

Merged
merged 3 commits into from
Mar 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions cuda_core/cuda/core/experimental/_linker.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,12 @@ class Linker:
"""

class _MembersNeededForFinalize:
__slots__ = ("handle", "use_nvjitlink")
__slots__ = ("handle", "use_nvjitlink", "const_char_keep_alive")

def __init__(self, program_obj, handle, use_nvjitlink):
self.handle = handle
self.use_nvjitlink = use_nvjitlink
self.const_char_keep_alive = []
weakref.finalize(program_obj, self.close)

def close(self):
Expand Down Expand Up @@ -390,27 +391,30 @@ def _add_code_object(self, object_code: ObjectCode):
data = object_code._module
assert_type(data, bytes)
with _exception_manager(self):
name_str = f"{object_code._handle}_{object_code._code_type}"
if _nvjitlink:
_nvjitlink.add_data(
self._mnff.handle,
self._input_type_from_code_type(object_code._code_type),
data,
len(data),
f"{object_code._handle}_{object_code._code_type}",
name_str,
)
else:
name_bytes = name_str.encode()
handle_return(
_driver.cuLinkAddData(
self._mnff.handle,
self._input_type_from_code_type(object_code._code_type),
data,
len(data),
f"{object_code._handle}_{object_code._code_type}".encode(),
name_bytes,
0,
None,
None,
)
)
self._mnff.const_char_keep_alive.append(name_bytes)

def link(self, target_type) -> ObjectCode:
"""
Expand Down
Loading