Skip to content

Fix OGSubgraph.current crash in CFRelease #32

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 3 commits into from
Feb 24, 2024
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
2 changes: 1 addition & 1 deletion Sources/_OpenGraph/Debug/OGDebugServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "OGDebugServer.h"
#include "og-debug-server.hpp"

#if TARGET_OS_DARWIN
#if OG_TARGET_OS_DARWIN

// MARK: - Exported C functions

Expand Down
2 changes: 1 addition & 1 deletion Sources/_OpenGraph/Debug/OGDebugServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ typedef struct OG_BRIDGED_TYPE(id) OGDebugServerStorage * OGDebugServerRef;

struct OGDebugServerStorage;

#if TARGET_OS_DARWIN
#if OG_TARGET_OS_DARWIN

OG_ASSUME_NONNULL_BEGIN

Expand Down
4 changes: 2 additions & 2 deletions Sources/_OpenGraph/Debug/og-debug-server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#define og_debug_server_hpp

#include "OGBase.h"
#if TARGET_OS_DARWIN
#if OG_TARGET_OS_DARWIN
#include "OGDebugServer.h"
#include "../Util/vector.hpp"
#include <dispatch/dispatch.h>
Expand Down Expand Up @@ -65,5 +65,5 @@ struct OGDebugServerStorage {

OG_ASSUME_NONNULL_END

#endif /* TARGET_OS_DARWIN */
#endif /* OG_TARGET_OS_DARWIN */
#endif /* og_debug_server_ hpp */
4 changes: 2 additions & 2 deletions Sources/_OpenGraph/Debug/og-debug-server.mm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Audited for 2021 Release

#include "og-debug-server.hpp"
#if TARGET_OS_DARWIN
#if OG_TARGET_OS_DARWIN

#include "../Util/log.hpp"
#include "../Util/assert.hpp"
Expand Down Expand Up @@ -332,4 +332,4 @@ bool blocking_write(int descriptor, void *buf, unsigned long count) {
return;
}

#endif /* TARGET_OS_DARWIN */
#endif /* OG_TARGET_OS_DARWIN */
22 changes: 22 additions & 0 deletions Sources/_OpenGraph/Graph/Graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,27 @@
//

#include "Graph.hpp"
#include "Subgraph.hpp"

#if !OG_TARGET_OS_WASI
#include <dispatch/dispatch.h>
#endif
#include <pthread.h>

pthread_key_t OG::Graph::_current_update_key;

OG::Graph::Graph() OG_NOEXCEPT {
// TODO

// libdispatch is not supported on WASI
// Tracked via https://github.com/swiftwasm/swift/issues/5565
#if !OG_TARGET_OS_WASI
static dispatch_once_t make_keys;
dispatch_once_f(&make_keys, nullptr, [](void *context){
pthread_key_create(&_current_update_key, nullptr);
OG::Subgraph::make_current_subgraph_key();
});
#endif

// TODO
}
2 changes: 2 additions & 0 deletions Sources/_OpenGraph/Graph/Graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class Graph final {
return _current_update_key;
}

Graph() OG_NOEXCEPT;

class Context final {
private:
Graph *_graph;
Expand Down
2 changes: 1 addition & 1 deletion Sources/_OpenGraph/Graph/OGGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ OGGraphRef OGGraphCreate() {

OGGraphRef OGGraphCreateShared(OGGraphRef storage) {
const CFIndex extraSize = sizeof(OGGraphStorage)-sizeof(CFRuntimeBase);
#if TARGET_CPU_WASM32
#if OG_TARGET_CPU_WASM32
// FIXME: extraSize will be 8 on WASM. Investate later.
static_assert(extraSize == 0x8);
#else
Expand Down
11 changes: 7 additions & 4 deletions Sources/_OpenGraph/Graph/OGSubgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ OGSubgraphRef OGSubgraphCreate(OGGraphRef cf_graph) {
OGSubgraphRef OGSubgraphCreate2(OGGraphRef cf_graph, OGAttribute attribute) {
OG::Graph::Context &context = OG::Graph::Context::from_cf(cf_graph);
const CFIndex extraSize = sizeof(OGSubgraphStorage)-sizeof(CFRuntimeBase);
#if TARGET_CPU_WASM32
#if OG_TARGET_CPU_WASM32
// FIXME: extraSize will always be 8 thus it will fail on WASM. Investate later.
static_assert(extraSize == 8);
#else
Expand All @@ -67,15 +67,15 @@ OGSubgraphRef OGSubgraphCreate2(OGGraphRef cf_graph, OGAttribute attribute) {
return cf_subgrah;
}

OGSubgraphRef OGSubgraphGetCurrent() {
_Nullable OGSubgraphRef OGSubgraphGetCurrent() {
OG::Subgraph* subgraph = (OG::Subgraph*)pthread_getspecific(OG::Subgraph::current_key());
if (subgraph == nullptr) {
return nullptr;
}
return subgraph->to_cf();
}

void OGSubgraphSetCurrent(OGSubgraphRef cf_subgraph) {
void OGSubgraphSetCurrent(_Nullable OGSubgraphRef cf_subgraph) {
OG::Subgraph* oldSubgraph = (OG::Subgraph*)pthread_getspecific(OG::Subgraph::current_key());
if (cf_subgraph == nullptr) {
pthread_setspecific(OG::Subgraph::current_key(), nullptr);
Expand All @@ -86,7 +86,10 @@ void OGSubgraphSetCurrent(OGSubgraphRef cf_subgraph) {
}
}
if (oldSubgraph != nullptr) {
CFRelease(oldSubgraph->to_cf());
OGSubgraphRef cf_oldSubgraph = oldSubgraph->to_cf();
if (cf_oldSubgraph) {
CFRelease(cf_oldSubgraph);
}
}
}

Expand Down
16 changes: 3 additions & 13 deletions Sources/_OpenGraph/OGBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,7 @@
#include <CoreFoundation/CoreFoundation.h>
#include <stdint.h>
#include <stdbool.h>
#ifdef __APPLE__
#include <TargetConditionals.h>
#ifndef TARGET_OS_DARWIN
#define TARGET_OS_DARWIN TARGET_OS_MAC
#endif
#ifndef TARGET_CPU_WASM32
#define TARGET_CPU_WASM32 0
#endif
#else
#include <CoreFoundation/TargetConditionals.h>
#endif
#include "OGTargetConditionals.h"

#define OG_OPTIONS CF_OPTIONS
#define OG_EXTERN_C_BEGIN CF_EXTERN_C_BEGIN
Expand All @@ -66,10 +56,10 @@
#define OG_SWIFT_NAME CF_SWIFT_NAME
#define OG_BRIDGED_TYPE CF_BRIDGED_TYPE

#if TARGET_OS_DARWIN && __OBJC__
#if OG_TARGET_OS_DARWIN && __OBJC__
#define OG_OBJC_FOUNDATION 1
#else
#define OG_OBJC_FOUNDATION 0
#endif /* TARGET_OS_DARWIN && __OBJC__ */
#endif /* OG_TARGET_OS_DARWIN && __OBJC__ */

#endif /* OGBase_h */
Loading