-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CBL-5661 : Fix a released captured context may be used in observer ca…
…llback * Implemented ContextManager class for retaining and mapping callback context objects with their pointer value. When getting the object back, the retained object will be returned so the object can be safely used in the callback. * Updated ListenerToken<CBLQueryChangeListener> to use ContextManager for it’s C4QueryObserver’s callback lamda so that the callback can validate and safely use its captured context. * Added CBLQuery_SetListenerCallbackDelay() private API for testing the fix. * Updated LiteCore to 3.1.7-1 to get the fix for CBSE-16662.
- Loading branch information
Showing
17 changed files
with
284 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// ContextManager.cc | ||
// | ||
// Copyright © 2024 Couchbase. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
#include "ContextManager.hh" | ||
|
||
using namespace std; | ||
using namespace fleece; | ||
|
||
namespace cbl_internal { | ||
|
||
ContextManager &ContextManager::shared() { | ||
static ContextManager* sContextManager = new ContextManager(); | ||
return *sContextManager; | ||
} | ||
|
||
ContextManager::ContextManager() { } | ||
|
||
void* ContextManager::registerObject(CBLRefCounted* object) { | ||
std::lock_guard<std::mutex> lock(_mutex); | ||
_contexts.insert({(void*)object, retained(object)}); | ||
return object; | ||
} | ||
|
||
void ContextManager::unregisterObject(void* ptr) { | ||
std::lock_guard<std::mutex> lock(_mutex); | ||
if (auto i = _contexts.find(ptr); i != _contexts.end()) { | ||
_contexts.erase(i); | ||
} | ||
} | ||
|
||
Retained<CBLRefCounted> ContextManager::getObject(void* ptr) { | ||
std::lock_guard<std::mutex> lock(_mutex); | ||
if (auto i = _contexts.find(ptr); i != _contexts.end()) { | ||
return i->second; | ||
} | ||
return nullptr; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// ContextManager.hh | ||
// | ||
// Copyright © 2024 Couchbase. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
#pragma once | ||
#include "Internal.hh" | ||
#include <mutex> | ||
#include <unordered_map> | ||
|
||
CBL_ASSUME_NONNULL_BEGIN | ||
|
||
namespace cbl_internal { | ||
|
||
/** | ||
Thread-safe context manager for retaining and mapping the object with its pointer value which can be used | ||
as a (captured) context for LiteCore's callback which could be either in C++ or C. This would allow the | ||
callback to verify that the context pointer value is still valid or not before using it. The implementation simply | ||
stores the object in a map by using its memory address as the key and returns the memory address as | ||
the pointer value. | ||
@note | ||
There is a chance that a new registered objects may have the same memory address as the ones previously | ||
unregistered. This implementation can be improved to reduce a chance of reusing the same memory address | ||
by generating integer keys with reuseable integer number + cycle count as inspired by the implementation of | ||
C# GCHandle. For now, the context object MUST BE validated for its originality before use. */ | ||
class ContextManager { | ||
public: | ||
static ContextManager& shared(); | ||
|
||
void* registerObject(CBLRefCounted* object); | ||
|
||
void unregisterObject(void* ptr); | ||
|
||
fleece::Retained<CBLRefCounted> getObject(void* ptr); | ||
|
||
private: | ||
ContextManager(); | ||
|
||
std::mutex _mutex; | ||
std::unordered_map<void*, fleece::Retained<CBLRefCounted>> _contexts; | ||
}; | ||
|
||
} | ||
|
||
CBL_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.