From 78715b14c51c710d88860ac3899f0230fefb626a Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Tue, 30 Apr 2019 22:30:23 +0300 Subject: [PATCH] Fix incorrect data race handling in ObjHeader::meta_object See #2931 --- runtime/src/main/cpp/Memory.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/runtime/src/main/cpp/Memory.cpp b/runtime/src/main/cpp/Memory.cpp index a8d1a66b987..57ccfb674b9 100644 --- a/runtime/src/main/cpp/Memory.cpp +++ b/runtime/src/main/cpp/Memory.cpp @@ -1125,15 +1125,23 @@ inline size_t containerSize(const ContainerHeader* container) { } // namespace MetaObjHeader* ObjHeader::createMetaObject(TypeInfo** location) { - MetaObjHeader* meta = konanConstructInstance(); TypeInfo* typeInfo = *location; RuntimeCheck(!hasPointerBits(typeInfo, OBJECT_TAG_MASK), "Object must not be tagged"); + +#if !KONAN_NO_THREADS + if (typeInfo->typeInfo_ != typeInfo) { + // Someone installed a new meta-object since the check. + return reinterpret_cast(typeInfo); + } +#endif + + MetaObjHeader* meta = konanConstructInstance(); meta->typeInfo_ = typeInfo; #if KONAN_NO_THREADS *location = reinterpret_cast(meta); #else TypeInfo* old = __sync_val_compare_and_swap(location, typeInfo, reinterpret_cast(meta)); - if (old->typeInfo_ != old) { + if (old != typeInfo) { // Someone installed a new meta-object since the check. konanFreeMemory(meta); meta = reinterpret_cast(old);