Skip to content

Commit 2e13188

Browse files
committed
Fix #28008: Handle unmanaged classes in c_addrOf with wide pointers
When c_addrOf is called on an unmanaged class in a wide pointer context (e.g., with CHPL_COMM=gasnet or --no-local), the current implementation incorrectly tries to take the address of the variable itself using c_pointer_return, which treats it as a wide pointer. However, unmanaged class variables are stored as narrow pointers even in wide pointer contexts. This causes: - Segfaults with CHPL_COMM=gasnet - Assertion failures with --no-local and assertions enabled Solution: Add specialized overloads of c_addrOf and c_addrOfConst for unmanaged classes that use c_ptrTo/c_ptrToConst instead of c_pointer_return. This correctly handles the narrow pointer value rather than taking the address of the variable. The fix adds: - c_addrOf overload for isUnmanagedClass(t) returning c_ptr(void) - c_addrOfConst overload for isUnmanagedClass(t) returning c_ptrConst(void)
1 parent 634d394 commit 2e13188

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

modules/standard/CTypes.chpl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,16 @@ module CTypes {
10261026
return c_pointer_return(x);
10271027
}
10281028

1029+
// Special handling for unmanaged classes to work with wide pointers
1030+
// In a wide pointer context, unmanaged class variables are stored as
1031+
// narrow pointers, but c_pointer_return would try to take the address
1032+
// of the variable itself, treating it incorrectly as a wide pointer.
1033+
// Use c_ptrTo instead to get the actual pointer value.
1034+
@chpldoc.nodoc
1035+
inline proc c_addrOf(const x: ?t): c_ptr(void) where isUnmanagedClass(t) {
1036+
return c_ptrTo(x);
1037+
}
1038+
10291039
// Added as unstable for 2.4, can be merged into stable version once we're
10301040
// confident in it.
10311041
@chpldoc.nodoc
@@ -1044,6 +1054,12 @@ module CTypes {
10441054
return c_pointer_return_const(x);
10451055
}
10461056

1057+
// Special handling for unmanaged classes - see corresponding c_addrOf overload
1058+
@chpldoc.nodoc
1059+
inline proc c_addrOfConst(const x: ?t): c_ptrConst(void) where isUnmanagedClass(t) {
1060+
return c_ptrToConst(x);
1061+
}
1062+
10471063
// See note on corresponding c_addrOf overload
10481064
@chpldoc.nodoc
10491065
@unstable("using 'c_addrOfConst' with a domain argument is unstable")

0 commit comments

Comments
 (0)