[runtime][codegen] Added lifetime constraint check
Current memory model forbids a heap object to reference a stack object, so check that explicitly on each field store for now
This commit is contained in:
+1
@@ -519,6 +519,7 @@ internal class Llvm(val context: Context, val llvmModule: LLVMModuleRef) {
|
|||||||
val lookupTLS = importRtFunction("LookupTLS")
|
val lookupTLS = importRtFunction("LookupTLS")
|
||||||
val initRuntimeIfNeeded = importRtFunction("Kotlin_initRuntimeIfNeeded")
|
val initRuntimeIfNeeded = importRtFunction("Kotlin_initRuntimeIfNeeded")
|
||||||
val mutationCheck = importRtFunction("MutationCheck")
|
val mutationCheck = importRtFunction("MutationCheck")
|
||||||
|
val checkLifetimesConstraint = importRtFunction("CheckLifetimesConstraint")
|
||||||
val freezeSubgraph = importRtFunction("FreezeSubgraph")
|
val freezeSubgraph = importRtFunction("FreezeSubgraph")
|
||||||
val checkMainThread = importRtFunction("CheckIsMainThread")
|
val checkMainThread = importRtFunction("CheckIsMainThread")
|
||||||
|
|
||||||
|
|||||||
+3
@@ -1633,6 +1633,9 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map<IrE
|
|||||||
functionGenerationContext.call(context.llvm.mutationCheck,
|
functionGenerationContext.call(context.llvm.mutationCheck,
|
||||||
listOf(functionGenerationContext.bitcast(codegen.kObjHeaderPtr, thisPtr)),
|
listOf(functionGenerationContext.bitcast(codegen.kObjHeaderPtr, thisPtr)),
|
||||||
Lifetime.IRRELEVANT, ExceptionHandler.Caller)
|
Lifetime.IRRELEVANT, ExceptionHandler.Caller)
|
||||||
|
|
||||||
|
if (functionGenerationContext.isObjectType(valueToAssign.type))
|
||||||
|
functionGenerationContext.call(context.llvm.checkLifetimesConstraint, listOf(thisPtr, valueToAssign))
|
||||||
}
|
}
|
||||||
functionGenerationContext.storeAny(valueToAssign, fieldPtrOfClass(thisPtr, value.symbol.owner), false)
|
functionGenerationContext.storeAny(valueToAssign, fieldPtrOfClass(thisPtr, value.symbol.owner), false)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -3439,6 +3439,14 @@ void MutationCheck(ObjHeader* obj) {
|
|||||||
ThrowInvalidMutabilityException(obj);
|
ThrowInvalidMutabilityException(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CheckLifetimesConstraint(ObjHeader* obj, ObjHeader* pointee) {
|
||||||
|
if (!obj->local() && pointee != nullptr && pointee->local()) {
|
||||||
|
konan::consolePrintf("Attempt to store a stack object %p into a heap object %p\n", pointee, obj);
|
||||||
|
konan::consolePrintf("This is a compiler bug, please report it to https://kotl.in/issue\n");
|
||||||
|
konan::abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void EnsureNeverFrozen(ObjHeader* object) {
|
void EnsureNeverFrozen(ObjHeader* object) {
|
||||||
ensureNeverFrozen(object);
|
ensureNeverFrozen(object);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -541,6 +541,7 @@ OBJ_GETTER(DerefStablePointer, void*) RUNTIME_NOTHROW;
|
|||||||
OBJ_GETTER(AdoptStablePointer, void*) RUNTIME_NOTHROW;
|
OBJ_GETTER(AdoptStablePointer, void*) RUNTIME_NOTHROW;
|
||||||
// Check mutability state.
|
// Check mutability state.
|
||||||
void MutationCheck(ObjHeader* obj);
|
void MutationCheck(ObjHeader* obj);
|
||||||
|
void CheckLifetimesConstraint(ObjHeader* obj, ObjHeader* pointee) RUNTIME_NOTHROW;
|
||||||
// Freeze object subgraph.
|
// Freeze object subgraph.
|
||||||
void FreezeSubgraph(ObjHeader* obj);
|
void FreezeSubgraph(ObjHeader* obj);
|
||||||
// Ensure this object shall block freezing.
|
// Ensure this object shall block freezing.
|
||||||
|
|||||||
Reference in New Issue
Block a user