Skip to content

fix(runtime): Crashes after destroying a worker runtime #1213

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 5 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion build/scripts/build-step-libffi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ function build {
exit 1
fi

if [ ! -e ./configure ]; then
if [ ! -e ./configure ]; then
autoreconf -i
else
echo "info: ./configure exists. Skipping autoreconf."
fi

mkdir -p "$BINARY_DIR" && pushd "$_"
Expand All @@ -41,6 +43,8 @@ function build {

if [ ! -e Makefile ]; then
./../configure --disable-shared --host="$TRIPLE"
else
echo "info: Makefile exists. Skipping configure."
fi

make
Expand Down
4 changes: 2 additions & 2 deletions src/NativeScript/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ set(SOURCE_FILES
Calling/FFICall.cpp
Calling/FFICallPrototype.cpp
Calling/CFunctionWrapper.mm
Calling/FFIFunctionCallback.cpp
Calling/FFIFunctionCallback.mm
Calling/FunctionWrapper.mm
ConstructorKey.cpp
GlobalObject.mm
Expand Down Expand Up @@ -172,7 +172,7 @@ set(SOURCE_FILES
Metadata/KnownUnknownClassPair.cpp
ObjC/AllocatedPlaceholder.mm
ObjC/Block/ObjCBlockCall.mm
ObjC/Block/ObjCBlockCallback.cpp
ObjC/Block/ObjCBlockCallback.mm
ObjC/Block/ObjCBlockType.mm
ObjC/Block/ObjCBlockTypeConstructor.cpp
ObjC/Constructor/ObjCConstructorBase.mm
Expand Down
4 changes: 3 additions & 1 deletion src/NativeScript/ObjC/Block/ObjCBlockType.mm
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
int32_t reserved;
const void* invoke;
JSBlockDescriptor* descriptor;
Ref<VM> vm;

Strong<ObjCBlockCallback> callback;

Expand All @@ -55,6 +56,7 @@ static CFTypeRef createBlock(ExecState* execState, JSCell* function, ObjCBlockTy
.reserved = 0,
.invoke = blockCallback->functionPointer(),
.descriptor = &kJSBlockDescriptor,
.vm = execState->vm(),
};

blockPointer->callback.set(execState->vm(), blockCallback.get());
Expand All @@ -70,7 +72,7 @@ static void copyBlock(JSBlock* dst, const JSBlock* src) {

static void disposeBlock(JSBlock* block) {
JSLockHolder locker(block->callback->vm());
block->callback.clear();
block->~JSBlock();
}

static JSC::JSCell* getJSFunction(id block) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ NSUInteger TNSFastEnumerationAdapter(id self, NSFastEnumerationState* state, id

if (state->state == State::Uninitialized) {
ExecState* execState = globalObject->globalExec();
JSObject* wrapper = [TNSRuntime runtimeForVM:&globalObject->vm()] -> _objectMap.get()->get(self);
auto runtime = [TNSRuntime runtimeForVM:&globalObject->vm()];
RELEASE_ASSERT_WITH_MESSAGE(runtime, "The runtime is deallocated.");
JSObject* wrapper = runtime->_objectMap.get()->get(self);
RELEASE_ASSERT(wrapper);

JSC::VM& vm = execState->vm();
Expand Down
18 changes: 11 additions & 7 deletions src/NativeScript/ObjC/Inheritance/ObjCClassBuilder.mm
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ static void attachDerivedMachinery(GlobalObject* globalObject, Class newKlass, J
IMP retain = findNotOverridenMethod(newKlass, @selector(retain));
IMP newRetain = imp_implementationWithBlock(^(id self) {
if ([self retainCount] == 1) {
if (JSObject* object = [TNSRuntime runtimeForVM:&globalObject->vm()] -> _objectMap.get()->get(self)) {
JSLockHolder lockHolder(globalObject->vm());
/// TODO: This gcProtect() call might render the same call in the allocWithZone override unnecessary. Check if this is true.
gcProtect(object);
if (auto runtime = [TNSRuntime runtimeForVM:&globalObject->vm()]) {
if (JSObject* object = runtime->_objectMap.get()->get(self)) {
JSLockHolder lockHolder(globalObject->vm());
/// TODO: This gcProtect() call might render the same call in the allocWithZone override unnecessary. Check if this is true.
gcProtect(object);
}
}
}

Expand All @@ -97,9 +99,11 @@ static void attachDerivedMachinery(GlobalObject* globalObject, Class newKlass, J
void (*release)(id, SEL) = (void (*)(id, SEL))findNotOverridenMethod(newKlass, @selector(release));
IMP newRelease = imp_implementationWithBlock(^(id self) {
if ([self retainCount] == 2) {
if (JSObject* object = [TNSRuntime runtimeForVM:&globalObject->vm()] -> _objectMap.get()->get(self)) {
JSLockHolder lockHolder(globalObject->vm());
gcUnprotect(object);
if (auto runtime = [TNSRuntime runtimeForVM:&globalObject->vm()]) {
if (JSObject* object = runtime->_objectMap.get()->get(self)) {
JSLockHolder lockHolder(globalObject->vm());
gcUnprotect(object);
}
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/NativeScript/ObjC/ObjCTypes.mm
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,11 @@ JSValue toValue(ExecState* execState, id object, Structure* (^structureResolver)
UNUSED_PARAM(vm);
#endif

if (JSObject* wrapper = [TNSRuntime runtimeForVM:&globalObject->vm()] -> _objectMap.get()->get(object)) {
ASSERT(wrapper->classInfo(vm) != ObjCWrapperObject::info() || jsCast<ObjCWrapperObject*>(wrapper)->wrappedObject() == object);
return wrapper;
if (auto runtime = [TNSRuntime runtimeForVM:&globalObject->vm()]) {
if (JSObject* wrapper = runtime->_objectMap.get()->get(object)) {
ASSERT(wrapper->classInfo(vm) != ObjCWrapperObject::info() || jsCast<ObjCWrapperObject*>(wrapper)->wrappedObject() == object);
return wrapper;
}
}

return ObjCWrapperObject::create(execState->vm(), structureResolver(), object, globalObject).get();
Expand Down
15 changes: 11 additions & 4 deletions src/NativeScript/ObjC/ObjCWrapperObject.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

void ObjCWrapperObject::finishCreation(VM& vm, id wrappedObject, GlobalObject* globalObject) {
Base::finishCreation(vm);
this->_objectMap = [TNSRuntime runtimeForVM:&globalObject->vm()] -> _objectMap.get();
auto runtime = [TNSRuntime runtimeForVM:&globalObject->vm()];
this->_objectMap = runtime != nullptr ? runtime->_objectMap.get() : nullptr;
this->setWrappedObject(wrappedObject);
this->_canSetObjectAtIndexedSubscript = [wrappedObject respondsToSelector:@selector(setObject:
atIndexedSubscript:)];
Expand Down Expand Up @@ -49,12 +50,16 @@
}

void ObjCWrapperObject::removeFromCache() {
this->_objectMap->remove(this->_wrappedObject.get());
if (this->_objectMap) {
this->_objectMap->remove(this->_wrappedObject.get());
}
}

void ObjCWrapperObject::setWrappedObject(id wrappedObject) {
if (this->_wrappedObject) {
this->_objectMap->remove(this->_wrappedObject.get());
if (this->_objectMap) {
this->_objectMap->remove(this->_wrappedObject.get());
}

if ([this->_wrappedObject.get() conformsToProtocol:@protocol(TNSDerivedClass)] && [this->_wrappedObject retainCount] > 1) {
// Derived classes have additional logic for protecting JS counterparts in their retain/release methods when the retention
Expand All @@ -71,7 +76,9 @@
this->_wrappedObject = wrappedObject;

if (wrappedObject) {
this->_objectMap->set(wrappedObject, this);
if (this->_objectMap) {
this->_objectMap->set(wrappedObject, this);
}

if ([wrappedObject conformsToProtocol:@protocol(TNSDerivedClass)] && [wrappedObject retainCount] > 1) {
// Derived classes have additional logic for protecting JS counterparts in their retain/release methods when the retention
Expand Down
15 changes: 9 additions & 6 deletions src/NativeScript/ObjC/TNSArrayAdapter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,24 @@
@implementation TNSArrayAdapter {
Strong<JSObject> _object;
JSGlobalObject* _globalObject;
RefPtr<VM> _vm;
}

- (instancetype)initWithJSObject:(JSObject*)jsObject execState:(ExecState*)execState {
if (self) {
self->_object = Strong<JSObject>(execState->vm(), jsObject);
self->_globalObject = execState->lexicalGlobalObject();
VM& vm = execState->vm();
[TNSRuntime runtimeForVM:&vm] -> _objectMap.get()->set(self, jsObject);
self->_vm = &execState->vm();
auto runtime = [TNSRuntime runtimeForVM:self->_vm.get()];
RELEASE_ASSERT_WITH_MESSAGE(runtime, "The runtime is deallocated.");
runtime->_objectMap.get()->set(self, jsObject);
}

return self;
}

- (NSUInteger)count {
VM& vm = self->_globalObject->vm();
VM& vm = *self->_vm.get();
RELEASE_ASSERT_WITH_MESSAGE([TNSRuntime runtimeForVM:&vm], "The runtime is deallocated.");
JSLockHolder lock(vm);

Expand All @@ -44,7 +47,7 @@ - (NSUInteger)count {
}

- (id)objectAtIndex:(NSUInteger)index {
VM& vm = self->_globalObject->vm();
VM& vm = *self->_vm.get();
RELEASE_ASSERT_WITH_MESSAGE([TNSRuntime runtimeForVM:&vm], "The runtime is deallocated.");
if (!(index < [self count])) {
@throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"Index (%tu) out of bounds", index] userInfo:nil];
Expand All @@ -56,7 +59,7 @@ - (id)objectAtIndex:(NSUInteger)index {
}

- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState*)state objects:(id[])buffer count:(NSUInteger)len {
VM& vm = self->_globalObject->vm();
VM& vm = *self->_vm.get();
RELEASE_ASSERT_WITH_MESSAGE([TNSRuntime runtimeForVM:&vm], "The runtime is deallocated.");

JSLockHolder lock(vm);
Expand Down Expand Up @@ -87,7 +90,7 @@ - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState*)state objects

- (void)dealloc {
{
VM& vm = self->_globalObject->vm();
VM& vm = *self->_vm.get();
JSLockHolder lock(vm);
if (TNSRuntime* runtime = [TNSRuntime runtimeForVM:&vm]) {
runtime->_objectMap.get()->remove(self);
Expand Down
13 changes: 8 additions & 5 deletions src/NativeScript/ObjC/TNSDataAdapter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@
@implementation TNSDataAdapter {
Strong<JSObject> _object;
JSGlobalObject* _globalObject;
RefPtr<VM> _vm;
}

- (instancetype)initWithJSObject:(JSObject*)jsObject execState:(ExecState*)execState {
if (self) {
VM& vm = execState->vm();
self->_object.set(vm, jsObject);
self->_vm = &execState->vm();
self->_object.set(*self->_vm.get(), jsObject);
self->_globalObject = execState->lexicalGlobalObject();
[TNSRuntime runtimeForVM:&vm] -> _objectMap.get()->set(self, jsObject);
auto runtime = [TNSRuntime runtimeForVM:self->_vm.get()];
RELEASE_ASSERT_WITH_MESSAGE(runtime, "The runtime is deallocated.");
runtime->_objectMap.get()->set(self, jsObject);
}

return self;
Expand All @@ -36,7 +39,7 @@ - (const void*)bytes {
}

- (void*)mutableBytes {
VM& vm = self->_globalObject->vm();
VM& vm = *self->_vm.get();
RELEASE_ASSERT_WITH_MESSAGE([TNSRuntime runtimeForVM:&vm], "The runtime is deallocated.");
JSLockHolder lock(vm);

Expand All @@ -53,7 +56,7 @@ - (void*)mutableBytes {
}

- (NSUInteger)length {
VM& vm = self->_globalObject->vm();
VM& vm = *self->_vm.get();
ExecState* execState = self->_globalObject->globalExec();
RELEASE_ASSERT_WITH_MESSAGE([TNSRuntime runtimeForVM:&vm], "The runtime is deallocated.");
JSLockHolder lock(vm);
Expand Down
28 changes: 15 additions & 13 deletions src/NativeScript/ObjC/TNSDictionaryAdapter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -106,39 +106,41 @@ - (NSArray*)allObjects {
@implementation TNSDictionaryAdapter {
Strong<JSObject> _object;
JSGlobalObject* _globalObject;
VM* _vm;
RefPtr<VM> _vm;
}

- (instancetype)initWithJSObject:(JSObject*)jsObject execState:(ExecState*)execState {
if (self) {
self->_object = Strong<JSObject>(execState->vm(), jsObject);
self->_globalObject = execState->lexicalGlobalObject();
self->_vm = &execState->vm();
[TNSRuntime runtimeForVM:self->_vm] -> _objectMap.get()->set(self, jsObject);
auto runtime = [TNSRuntime runtimeForVM:self->_vm.get()];
RELEASE_ASSERT_WITH_MESSAGE(runtime, "The runtime is deallocated.");
runtime->_objectMap.get()->set(self, jsObject);
}

return self;
}

- (NSUInteger)count {
JSLockHolder lock(self->_vm);
JSLockHolder lock(self->_vm.get());

JSObject* object = self->_object.get();
if (JSMap* map = jsDynamicCast<JSMap*>(*self->_vm, object)) {
return map->size();
}

PropertyNameArray properties(self->_vm, PropertyNameMode::Strings, PrivateSymbolMode::Include);
PropertyNameArray properties(self->_vm.get(), PropertyNameMode::Strings, PrivateSymbolMode::Include);
object->methodTable(*self->_vm)->getOwnPropertyNames(object, self->_globalObject->globalExec(), properties, EnumerationMode());
return properties.size();
}

- (id)objectForKey:(id)aKey {
RELEASE_ASSERT_WITH_MESSAGE([TNSRuntime runtimeForVM:self->_vm], "The runtime is deallocated.");
JSLockHolder lock(self->_vm);
RELEASE_ASSERT_WITH_MESSAGE([TNSRuntime runtimeForVM:self->_vm.get()], "The runtime is deallocated.");
JSLockHolder lock(self->_vm.get());

JSObject* object = self->_object.get();
if (JSMap* map = jsDynamicCast<JSMap*>(*self->_vm, object)) {
if (JSMap* map = jsDynamicCast<JSMap*>(*self->_vm.get(), object)) {
JSValue key = toValue(self->_globalObject->globalExec(), aKey);
return toObject(self->_globalObject->globalExec(), map->get(self->_globalObject->globalExec(), key));
} else if ([aKey isKindOfClass:[NSString class]]) {
Expand All @@ -153,23 +155,23 @@ - (id)objectForKey:(id)aKey {
}

- (NSEnumerator*)keyEnumerator {
RELEASE_ASSERT_WITH_MESSAGE([TNSRuntime runtimeForVM:self->_vm], "The runtime is deallocated.");
RELEASE_ASSERT_WITH_MESSAGE([TNSRuntime runtimeForVM:self->_vm.get()], "The runtime is deallocated.");
JSLockHolder lock(self->_globalObject->globalExec());

JSObject* object = self->_object.get();
if (JSMap* map = jsDynamicCast<JSMap*>(*self->_vm, object)) {
if (JSMap* map = jsDynamicCast<JSMap*>(*self->_vm.get(), object)) {
return [[[TNSDictionaryAdapterMapKeysEnumerator alloc] initWithMap:map execState:self->_globalObject->globalExec()] autorelease];
}

PropertyNameArray properties(self->_vm, PropertyNameMode::Strings, PrivateSymbolMode::Include);
object->methodTable(*self->_vm)->getOwnPropertyNames(object, self->_globalObject->globalExec(), properties, EnumerationMode());
PropertyNameArray properties(self->_vm.get(), PropertyNameMode::Strings, PrivateSymbolMode::Include);
object->methodTable(*self->_vm.get())->getOwnPropertyNames(object, self->_globalObject->globalExec(), properties, EnumerationMode());
return [[[TNSDictionaryAdapterObjectKeysEnumerator alloc] initWithProperties:properties.releaseData()] autorelease];
}

- (void)dealloc {
{
JSLockHolder lock(self->_vm);
if (TNSRuntime* runtime = [TNSRuntime runtimeForVM:self->_vm]) {
JSLockHolder lock(self->_vm.get());
if (TNSRuntime* runtime = [TNSRuntime runtimeForVM:self->_vm.get()]) {
runtime->_objectMap.get()->remove(self);
}
// Clear Strong reference inside the locked section. Otherwise it would be done when the
Expand Down
6 changes: 6 additions & 0 deletions src/NativeScript/TNSRuntime+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@

@interface TNSRuntime () {
@package
#ifdef __cplusplus
WTF::RefPtr<JSC::VM> _vm;
JSC::Strong<NativeScript::GlobalObject> _globalObject;
std::unique_ptr<JSC::WeakGCMap<id, JSC::JSObject>> _objectMap;
#endif //__cplusplus

NSString* _applicationPath;
}

#ifdef __cplusplus
+ (TNSRuntime*)runtimeForVM:(JSC::VM*)vm;
#endif //__cplusplus
+ (NSPointerArray*)runtimes;

@end
4 changes: 4 additions & 0 deletions src/NativeScript/TNSRuntime.mm
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ @implementation TNSRuntime
static WTF::Lock _runtimesLock;
static NSPointerArray* _runtimes;

+ (NSPointerArray*)runtimes {
return _runtimes;
}

+ (TNSRuntime*)current {
WTF::LockHolder lock(_runtimesLock);
Thread* currentThread = &WTF::Thread::current();
Expand Down
Loading