Skip to content

Commit d35513b

Browse files
authored
Fix OGSubgraph.current crash in CFRelease (#32)
* Fix OGSubgraph.current crash in CFRelease * Add OGTargetConditionals * Disable dispatch in WASI support
1 parent caae2e2 commit d35513b

18 files changed

+335
-40
lines changed

Sources/_OpenGraph/Debug/OGDebugServer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "OGDebugServer.h"
99
#include "og-debug-server.hpp"
1010

11-
#if TARGET_OS_DARWIN
11+
#if OG_TARGET_OS_DARWIN
1212

1313
// MARK: - Exported C functions
1414

Sources/_OpenGraph/Debug/OGDebugServer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ typedef struct OG_BRIDGED_TYPE(id) OGDebugServerStorage * OGDebugServerRef;
1414

1515
struct OGDebugServerStorage;
1616

17-
#if TARGET_OS_DARWIN
17+
#if OG_TARGET_OS_DARWIN
1818

1919
OG_ASSUME_NONNULL_BEGIN
2020

Sources/_OpenGraph/Debug/og-debug-server.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#define og_debug_server_hpp
1010

1111
#include "OGBase.h"
12-
#if TARGET_OS_DARWIN
12+
#if OG_TARGET_OS_DARWIN
1313
#include "OGDebugServer.h"
1414
#include "../Util/vector.hpp"
1515
#include <dispatch/dispatch.h>
@@ -65,5 +65,5 @@ struct OGDebugServerStorage {
6565

6666
OG_ASSUME_NONNULL_END
6767

68-
#endif /* TARGET_OS_DARWIN */
68+
#endif /* OG_TARGET_OS_DARWIN */
6969
#endif /* og_debug_server_ hpp */

Sources/_OpenGraph/Debug/og-debug-server.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// Audited for 2021 Release
77

88
#include "og-debug-server.hpp"
9-
#if TARGET_OS_DARWIN
9+
#if OG_TARGET_OS_DARWIN
1010

1111
#include "../Util/log.hpp"
1212
#include "../Util/assert.hpp"
@@ -332,4 +332,4 @@ bool blocking_write(int descriptor, void *buf, unsigned long count) {
332332
return;
333333
}
334334

335-
#endif /* TARGET_OS_DARWIN */
335+
#endif /* OG_TARGET_OS_DARWIN */

Sources/_OpenGraph/Graph/Graph.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,27 @@
66
//
77

88
#include "Graph.hpp"
9+
#include "Subgraph.hpp"
10+
11+
#if !OG_TARGET_OS_WASI
12+
#include <dispatch/dispatch.h>
13+
#endif
14+
#include <pthread.h>
915

1016
pthread_key_t OG::Graph::_current_update_key;
17+
18+
OG::Graph::Graph() OG_NOEXCEPT {
19+
// TODO
20+
21+
// libdispatch is not supported on WASI
22+
// Tracked via https://github.com/swiftwasm/swift/issues/5565
23+
#if !OG_TARGET_OS_WASI
24+
static dispatch_once_t make_keys;
25+
dispatch_once_f(&make_keys, nullptr, [](void *context){
26+
pthread_key_create(&_current_update_key, nullptr);
27+
OG::Subgraph::make_current_subgraph_key();
28+
});
29+
#endif
30+
31+
// TODO
32+
}

Sources/_OpenGraph/Graph/Graph.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class Graph final {
3333
return _current_update_key;
3434
}
3535

36+
Graph() OG_NOEXCEPT;
37+
3638
class Context final {
3739
private:
3840
Graph *_graph;

Sources/_OpenGraph/Graph/OGGraph.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ OGGraphRef OGGraphCreate() {
1515

1616
OGGraphRef OGGraphCreateShared(OGGraphRef storage) {
1717
const CFIndex extraSize = sizeof(OGGraphStorage)-sizeof(CFRuntimeBase);
18-
#if TARGET_CPU_WASM32
18+
#if OG_TARGET_CPU_WASM32
1919
// FIXME: extraSize will be 8 on WASM. Investate later.
2020
static_assert(extraSize == 0x8);
2121
#else

Sources/_OpenGraph/Graph/OGSubgraph.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ OGSubgraphRef OGSubgraphCreate(OGGraphRef cf_graph) {
5353
OGSubgraphRef OGSubgraphCreate2(OGGraphRef cf_graph, OGAttribute attribute) {
5454
OG::Graph::Context &context = OG::Graph::Context::from_cf(cf_graph);
5555
const CFIndex extraSize = sizeof(OGSubgraphStorage)-sizeof(CFRuntimeBase);
56-
#if TARGET_CPU_WASM32
56+
#if OG_TARGET_CPU_WASM32
5757
// FIXME: extraSize will always be 8 thus it will fail on WASM. Investate later.
5858
static_assert(extraSize == 8);
5959
#else
@@ -67,15 +67,15 @@ OGSubgraphRef OGSubgraphCreate2(OGGraphRef cf_graph, OGAttribute attribute) {
6767
return cf_subgrah;
6868
}
6969

70-
OGSubgraphRef OGSubgraphGetCurrent() {
70+
_Nullable OGSubgraphRef OGSubgraphGetCurrent() {
7171
OG::Subgraph* subgraph = (OG::Subgraph*)pthread_getspecific(OG::Subgraph::current_key());
7272
if (subgraph == nullptr) {
7373
return nullptr;
7474
}
7575
return subgraph->to_cf();
7676
}
7777

78-
void OGSubgraphSetCurrent(OGSubgraphRef cf_subgraph) {
78+
void OGSubgraphSetCurrent(_Nullable OGSubgraphRef cf_subgraph) {
7979
OG::Subgraph* oldSubgraph = (OG::Subgraph*)pthread_getspecific(OG::Subgraph::current_key());
8080
if (cf_subgraph == nullptr) {
8181
pthread_setspecific(OG::Subgraph::current_key(), nullptr);
@@ -86,7 +86,10 @@ void OGSubgraphSetCurrent(OGSubgraphRef cf_subgraph) {
8686
}
8787
}
8888
if (oldSubgraph != nullptr) {
89-
CFRelease(oldSubgraph->to_cf());
89+
OGSubgraphRef cf_oldSubgraph = oldSubgraph->to_cf();
90+
if (cf_oldSubgraph) {
91+
CFRelease(cf_oldSubgraph);
92+
}
9093
}
9194
}
9295

Sources/_OpenGraph/OGBase.h

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,7 @@
4444
#include <CoreFoundation/CoreFoundation.h>
4545
#include <stdint.h>
4646
#include <stdbool.h>
47-
#ifdef __APPLE__
48-
#include <TargetConditionals.h>
49-
#ifndef TARGET_OS_DARWIN
50-
#define TARGET_OS_DARWIN TARGET_OS_MAC
51-
#endif
52-
#ifndef TARGET_CPU_WASM32
53-
#define TARGET_CPU_WASM32 0
54-
#endif
55-
#else
56-
#include <CoreFoundation/TargetConditionals.h>
57-
#endif
47+
#include "OGTargetConditionals.h"
5848

5949
#define OG_OPTIONS CF_OPTIONS
6050
#define OG_EXTERN_C_BEGIN CF_EXTERN_C_BEGIN
@@ -66,10 +56,10 @@
6656
#define OG_SWIFT_NAME CF_SWIFT_NAME
6757
#define OG_BRIDGED_TYPE CF_BRIDGED_TYPE
6858

69-
#if TARGET_OS_DARWIN && __OBJC__
59+
#if OG_TARGET_OS_DARWIN && __OBJC__
7060
#define OG_OBJC_FOUNDATION 1
7161
#else
7262
#define OG_OBJC_FOUNDATION 0
73-
#endif /* TARGET_OS_DARWIN && __OBJC__ */
63+
#endif /* OG_TARGET_OS_DARWIN && __OBJC__ */
7464

7565
#endif /* OGBase_h */

0 commit comments

Comments
 (0)