Skip to content

Commit 202e32a

Browse files
committed
[vm] Native API: Introduce Dart_FinalizableHandle
Introduces Dart_NewFinalizableHandle which does auto delete itself, but does not allow accessing the weak referenced object. Issue: #42312 Change-Id: I24ea732925122c453213c4fa3f629761c352f838 Cq-Include-Trybots:dart/try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-msan-linux-release-x64-try,vm-kernel-precomp-msan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try,vm-kernel-precomp-win-release-x64-try,vm-kernel-mac-debug-x64-try,vm-precomp-ffi-qemu-linux-release-arm-try,vm-kernel-nnbd-linux-debug-x64-try,analyzer-nnbd-linux-release-try,front-end-nnbd-linux-release-x64-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/154695 Reviewed-by: Ryan Macnak <[email protected]> Reviewed-by: Martin Kustermann <[email protected]>
1 parent debb4dd commit 202e32a

33 files changed

+883
-152
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 2.10.0
2+
3+
### Dart VM
4+
5+
* Introduces `Dart_FinalizableHandle`s. They do auto-delete, and the weakly
6+
referred object cannot be accessed through them.
7+
18
## 2.9.0
29

310
### Language
@@ -134,7 +141,7 @@ Updated the Linter to `0.1.117`, which includes:
134141
* Preserve Windows line endings in `pubspec.lock` if they are already there
135142
([#2489](https://github.com/dart-lang/pub/pull/2489)).
136143
* Better terminal color-detection. Use colors in terminals on Windows.
137-
* Fix git folder names in cache, allowing for ssh-style git
144+
* Fix git folder names in cache, allowing for ssh-style git
138145
dependencies.
139146
* Fix: Avoid precompilation of dependencies of global packages.
140147

pkg/vm_service/lib/src/vm_service.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5430,10 +5430,11 @@ class MemoryUsage extends Response {
54305430

54315431
/// The amount of non-Dart memory that is retained by Dart objects. For
54325432
/// example, memory associated with Dart objects through APIs such as
5433-
/// Dart_NewWeakPersistentHandle and Dart_NewExternalTypedData. This usage is
5434-
/// only as accurate as the values supplied to these APIs from the VM embedder
5435-
/// or native extensions. This external memory applies GC pressure, but is
5436-
/// separate from heapUsage and heapCapacity.
5433+
/// Dart_NewFinalizableHandle, Dart_NewWeakPersistentHandle and
5434+
/// Dart_NewExternalTypedData. This usage is only as accurate as the values
5435+
/// supplied to these APIs from the VM embedder or native extensions. This
5436+
/// external memory applies GC pressure, but is separate from heapUsage and
5437+
/// heapCapacity.
54375438
int externalUsage;
54385439

54395440
/// The total capacity of the heap in bytes. This is the amount of memory used

runtime/bin/directory.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,7 @@ void FUNCTION_NAME(Directory_GetAsyncDirectoryListerPointer)(
232232
}
233233
}
234234

235-
static void ReleaseListing(void* isolate_callback_data,
236-
Dart_WeakPersistentHandle handle,
237-
void* peer) {
235+
static void ReleaseListing(void* isolate_callback_data, void* peer) {
238236
AsyncDirectoryListing* listing =
239237
reinterpret_cast<AsyncDirectoryListing*>(peer);
240238
listing->Release();
@@ -247,8 +245,8 @@ void FUNCTION_NAME(Directory_SetAsyncDirectoryListerPointer)(
247245
DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1));
248246
AsyncDirectoryListing* listing =
249247
reinterpret_cast<AsyncDirectoryListing*>(listing_pointer);
250-
Dart_NewWeakPersistentHandle(dart_this, reinterpret_cast<void*>(listing),
251-
sizeof(*listing), ReleaseListing);
248+
Dart_NewFinalizableHandle(dart_this, reinterpret_cast<void*>(listing),
249+
sizeof(*listing), ReleaseListing);
252250
Dart_Handle result = Dart_SetNativeInstanceField(
253251
dart_this, kAsyncDirectoryListerFieldIndex, listing_pointer);
254252
ThrowIfError(result);

runtime/bin/file.cc

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ void FUNCTION_NAME(File_GetPointer)(Dart_NativeArguments args) {
6868
Dart_SetIntegerReturnValue(args, file_pointer);
6969
}
7070

71-
static void ReleaseFile(void* isolate_callback_data,
72-
Dart_WeakPersistentHandle handle,
73-
void* peer) {
71+
static void ReleaseFile(void* isolate_callback_data, void* peer) {
7472
File* file = reinterpret_cast<File*>(peer);
7573
file->Release();
7674
}
@@ -79,9 +77,9 @@ void FUNCTION_NAME(File_SetPointer)(Dart_NativeArguments args) {
7977
Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0));
8078
intptr_t file_pointer = DartUtils::GetNativeIntptrArgument(args, 1);
8179
File* file = reinterpret_cast<File*>(file_pointer);
82-
Dart_WeakPersistentHandle handle = Dart_NewWeakPersistentHandle(
80+
Dart_FinalizableHandle handle = Dart_NewFinalizableHandle(
8381
dart_this, reinterpret_cast<void*>(file), sizeof(*file), ReleaseFile);
84-
file->SetWeakHandle(handle);
82+
file->SetFinalizableHandle(handle);
8583
SetFile(dart_this, file_pointer);
8684
}
8785

@@ -148,7 +146,7 @@ void FUNCTION_NAME(File_Close)(Dart_NativeArguments args) {
148146
return;
149147
}
150148
file->Close();
151-
file->DeleteWeakHandle(Dart_CurrentIsolate());
149+
file->DeleteFinalizableHandle(Dart_CurrentIsolate(), dart_this);
152150
file->Release();
153151

154152
ThrowIfError(

runtime/bin/file.h

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -184,21 +184,23 @@ class File : public ReferenceCounted<File> {
184184
// Calls the platform-specific functions to close the file.
185185
void Close();
186186

187-
// Returns the weak persistent handle for the File's Dart wrapper.
188-
Dart_WeakPersistentHandle WeakHandle() const { return weak_handle_; }
187+
// Returns the finalizable handle for the File's Dart wrapper.
188+
Dart_FinalizableHandle FinalizableHandle() const {
189+
return finalizable_handle_;
190+
}
189191

190-
// Set the weak persistent handle for the File's Dart wrapper.
191-
void SetWeakHandle(Dart_WeakPersistentHandle handle) {
192-
ASSERT(weak_handle_ == NULL);
193-
weak_handle_ = handle;
192+
// Set the finalizable handle for the File's Dart wrapper.
193+
void SetFinalizableHandle(Dart_FinalizableHandle handle) {
194+
ASSERT(finalizable_handle_ == NULL);
195+
finalizable_handle_ = handle;
194196
}
195197

196-
// Deletes the weak persistent handle for the File's Dart wrapper. Call
198+
// Deletes the finalizable handle for the File's Dart wrapper. Call
197199
// when the file is explicitly closed and the finalizer is no longer
198200
// needed.
199-
void DeleteWeakHandle(Dart_Isolate isolate) {
200-
Dart_DeleteWeakPersistentHandle(weak_handle_);
201-
weak_handle_ = NULL;
201+
void DeleteFinalizableHandle(Dart_Isolate isolate, Dart_Handle strong_ref) {
202+
Dart_DeleteFinalizableHandle(finalizable_handle_, strong_ref);
203+
finalizable_handle_ = NULL;
202204
}
203205

204206
// Open the file with the given path. The file is always opened for
@@ -319,7 +321,7 @@ class File : public ReferenceCounted<File> {
319321

320322
private:
321323
explicit File(FileHandle* handle)
322-
: ReferenceCounted(), handle_(handle), weak_handle_(NULL) {}
324+
: ReferenceCounted(), handle_(handle), finalizable_handle_(NULL) {}
323325

324326
~File();
325327

@@ -330,10 +332,10 @@ class File : public ReferenceCounted<File> {
330332
// FileHandle is an OS specific class which stores data about the file.
331333
FileHandle* handle_; // OS specific handle for the file.
332334

333-
// We retain the weak handle because we can do cleanup eagerly when Dart code
334-
// calls closeSync(). In that case, we delete the weak handle so that the
335-
// finalizer doesn't run.
336-
Dart_WeakPersistentHandle weak_handle_;
335+
// We retain the finalizable handle because we can do cleanup eagerly when
336+
// Dart code calls closeSync(). In that case, we delete the finalizable
337+
// handle so that the finalizer doesn't run.
338+
Dart_FinalizableHandle finalizable_handle_;
337339

338340
friend class ReferenceCounted<File>;
339341
DISALLOW_COPY_AND_ASSIGN(File);

runtime/bin/filter.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,7 @@ void FUNCTION_NAME(Filter_Processed)(Dart_NativeArguments args) {
256256
}
257257
}
258258

259-
static void DeleteFilter(void* isolate_data,
260-
Dart_WeakPersistentHandle handle,
261-
void* filter_pointer) {
259+
static void DeleteFilter(void* isolate_data, void* filter_pointer) {
262260
Filter* filter = reinterpret_cast<Filter*>(filter_pointer);
263261
delete filter;
264262
}
@@ -272,8 +270,8 @@ Dart_Handle Filter::SetFilterAndCreateFinalizer(Dart_Handle filter,
272270
if (Dart_IsError(err)) {
273271
return err;
274272
}
275-
Dart_NewWeakPersistentHandle(filter, reinterpret_cast<void*>(filter_pointer),
276-
size, DeleteFilter);
273+
Dart_NewFinalizableHandle(filter, reinterpret_cast<void*>(filter_pointer),
274+
size, DeleteFilter);
277275
return err;
278276
}
279277

runtime/bin/loader.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,7 @@ Dart_Handle Loader::LibraryTagHandler(Dart_LibraryTag tag,
145145
return Dart_Null();
146146
}
147147
#else
148-
static void MallocFinalizer(void* isolate_callback_data,
149-
Dart_WeakPersistentHandle handle,
150-
void* peer) {
148+
static void MallocFinalizer(void* isolate_callback_data, void* peer) {
151149
free(peer);
152150
}
153151

@@ -185,8 +183,8 @@ Dart_Handle Loader::LibraryTagHandler(Dart_LibraryTag tag,
185183
}
186184
result = Dart_NewExternalTypedData(Dart_TypedData_kUint8, kernel_buffer,
187185
kernel_buffer_size);
188-
Dart_NewWeakPersistentHandle(result, kernel_buffer, kernel_buffer_size,
189-
MallocFinalizer);
186+
Dart_NewFinalizableHandle(result, kernel_buffer, kernel_buffer_size,
187+
MallocFinalizer);
190188
return result;
191189
}
192190
if (tag == Dart_kImportExtensionTag) {

runtime/bin/namespace.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ namespace bin {
1616

1717
static const int kNamespaceNativeFieldIndex = 0;
1818

19-
static void ReleaseNamespace(void* isolate_callback_data,
20-
Dart_WeakPersistentHandle handle,
21-
void* peer) {
19+
static void ReleaseNamespace(void* isolate_callback_data, void* peer) {
2220
Namespace* namespc = reinterpret_cast<Namespace*>(peer);
2321
ASSERT(namespc != NULL);
2422
namespc->Release();
@@ -84,8 +82,8 @@ void FUNCTION_NAME(Namespace_Create)(Dart_NativeArguments args) {
8482

8583
// Set up a finalizer for the Dart object so that we can do any necessary
8684
// platform-specific cleanup for the namespc.
87-
Dart_NewWeakPersistentHandle(namespc_obj, reinterpret_cast<void*>(namespc),
88-
sizeof(*namespc), ReleaseNamespace);
85+
Dart_NewFinalizableHandle(namespc_obj, reinterpret_cast<void*>(namespc),
86+
sizeof(*namespc), ReleaseNamespace);
8987
Dart_SetReturnValue(args, namespc_obj);
9088
}
9189

runtime/bin/secure_socket_filter.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ static SSLFilter* GetFilter(Dart_NativeArguments args) {
6262
return filter;
6363
}
6464

65-
static void DeleteFilter(void* isolate_data,
66-
Dart_WeakPersistentHandle handle,
67-
void* context_pointer) {
65+
static void DeleteFilter(void* isolate_data, void* context_pointer) {
6866
SSLFilter* filter = reinterpret_cast<SSLFilter*>(context_pointer);
6967
filter->Release();
7068
}
@@ -78,8 +76,8 @@ static Dart_Handle SetFilter(Dart_NativeArguments args, SSLFilter* filter) {
7876
dart_this, SSLFilter::kSSLFilterNativeFieldIndex,
7977
reinterpret_cast<intptr_t>(filter));
8078
RETURN_IF_ERROR(err);
81-
Dart_NewWeakPersistentHandle(dart_this, reinterpret_cast<void*>(filter),
82-
SSLFilter::kApproximateSize, DeleteFilter);
79+
Dart_NewFinalizableHandle(dart_this, reinterpret_cast<void*>(filter),
80+
SSLFilter::kApproximateSize, DeleteFilter);
8381
return Dart_Null();
8482
}
8583

runtime/bin/security_context.cc

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ SSLCertContext* SSLCertContext::GetSecurityContext(Dart_NativeArguments args) {
9393
return context;
9494
}
9595

96-
static void DeleteSecurityContext(void* isolate_data,
97-
Dart_WeakPersistentHandle handle,
98-
void* context_pointer) {
96+
static void DeleteSecurityContext(void* isolate_data, void* context_pointer) {
9997
SSLCertContext* context = static_cast<SSLCertContext*>(context_pointer);
10098
context->Release();
10199
}
@@ -109,15 +107,13 @@ static Dart_Handle SetSecurityContext(Dart_NativeArguments args,
109107
dart_this, SSLCertContext::kSecurityContextNativeFieldIndex,
110108
reinterpret_cast<intptr_t>(context));
111109
RETURN_IF_ERROR(err);
112-
Dart_NewWeakPersistentHandle(dart_this, context,
113-
SSLCertContext::kApproximateSize,
114-
DeleteSecurityContext);
110+
Dart_NewFinalizableHandle(dart_this, context,
111+
SSLCertContext::kApproximateSize,
112+
DeleteSecurityContext);
115113
return Dart_Null();
116114
}
117115

118-
static void ReleaseCertificate(void* isolate_data,
119-
Dart_WeakPersistentHandle handle,
120-
void* context_pointer) {
116+
static void ReleaseCertificate(void* isolate_data, void* context_pointer) {
121117
X509* cert = reinterpret_cast<X509*>(context_pointer);
122118
X509_free(cert);
123119
}
@@ -158,9 +154,9 @@ Dart_Handle X509Helper::WrappedX509Certificate(X509* certificate) {
158154
const intptr_t approximate_size_of_certificate =
159155
sizeof(*certificate) + EstimateX509Size(certificate);
160156
ASSERT(approximate_size_of_certificate > 0);
161-
Dart_NewWeakPersistentHandle(result, reinterpret_cast<void*>(certificate),
162-
approximate_size_of_certificate,
163-
ReleaseCertificate);
157+
Dart_NewFinalizableHandle(result, reinterpret_cast<void*>(certificate),
158+
approximate_size_of_certificate,
159+
ReleaseCertificate);
164160
return result;
165161
}
166162

0 commit comments

Comments
 (0)