Open vasslitvinov opened 7 years ago
I'm hoping @mppf will weigh in on this since it seems similar to other cases he's already had to handle such as supporting functions that return arrays over domains that are about to go out of scope.
IIRC we don't treat runtime types the same as normal records, for the purposes of copy/deinitialize etc. I think we would need to do so in order to correct this kind of error in a principled way.
The problem
Scenario: the Chapel program calls a function returning a type that's a dmapped domain.
In the generated code / at run time, that function returns a chpl___RuntimeTypeInfo struct, which points to the distribution class allocated by the
dmapped
clause.Before returning, however, that function deletes this distribution class.
Accessing the distribution class through the runtime type after the function returns results in a use-after-free / invalid memory access error. This happens, for example, when we create a variable of the type returned by the function. The error is detectable with valgrind.
The function can be an "if" function (see an example below).
Original user code
This issue was encountered by @ben-albrecht on an earlier version of the test
studies/prk/Sparse/sparse.chpl
developed in #6573. The essence of that code is:Here the culprit function is the "if" function that the compiler generates implicitly for the expression
if distributed then ... else ...
. Here it is atype
function.Other versions
We can make an explicit function like
makedomain
here:We can further simplify the "sparse subdomain" thing like so:
This requires wrestling
CSR
into acting as a rectangular (not sparse) domain map. Use this patch.Some details
Consider again this snippet:
The clause
dmapped CSR()
is converted by the compiler into:Here,
temp7
"owns" the newly-allocated CSR class. So, whenmakedomain()
exits,temp7
goes out of scope and deallocates the class.The expression
(some domain type) dmapped (some _distribution)
is converted by the compiler into:temp9
is a runtime-type record. It contains the _distribution in one of its fields. chpl_distributed() is currently defined at ChapelArray.chpl:744.Alas,
temp9
is returned by the compiled version ofmakedomain()
.