Skip to content

Commit e24e3cd

Browse files
Fix leaking error objects in Execution
1 parent 567d6ff commit e24e3cd

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

Sources/WasmKit/Execution/Execution.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ struct Execution {
1111
private var stackEnd: UnsafeMutablePointer<StackSlot>
1212
/// The error trap thrown during execution.
1313
/// This property must not be assigned to be non-nil more than once.
14+
/// - Note: If the trap is set, it must be released manually.
1415
private var trap: UnsafeRawPointer? = nil
1516

1617
/// Executes the given closure with a new execution state associated with
@@ -25,6 +26,13 @@ struct Execution {
2526
valueStack.deallocate()
2627
}
2728
var context = Execution(runtime: runtime, stackEnd: valueStack.advanced(by: limit))
29+
defer {
30+
if let trap = context.trap {
31+
// Manually release the error object because the trap is caught in C and
32+
// held as a raw pointer.
33+
wasmkit_swift_errorRelease(trap)
34+
}
35+
}
2836
return try body(&context, valueStack)
2937
}
3038

Sources/_CWasmKit/include/_CWasmKit.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,14 @@ static inline void wasmkit_fwrite_stderr(const char *_Nonnull str, size_t len) {
4444
fwrite(str, 1, len, stderr);
4545
}
4646

47+
// MARK: - Swift Runtime Functions
48+
49+
struct SwiftError;
50+
51+
/// Releases the given Swift error object.
52+
static inline void wasmkit_swift_errorRelease(const void *_Nonnull object) {
53+
extern void swift_errorRelease(const struct SwiftError *_Nonnull object);
54+
swift_errorRelease(object);
55+
}
56+
4757
#endif // WASMKIT__CWASMKIT_H

0 commit comments

Comments
 (0)