[pkg] Make package:mmap's manually created finalizer compatible with UBSAN.
Bug: https://github.com/dart-lang/sdk/issues/54243
Change-Id: Iced248a3807bb0ac69afe228bea883bcfea07680
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/340023
Reviewed-by: Daco Harkes <dacoharkes@google.com>
Commit-Queue: Ryan Macnak <rmacnak@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
diff --git a/pkg/mmap/lib/src/mmap_impl.dart b/pkg/mmap/lib/src/mmap_impl.dart
index 51563e4..4081b42 100644
--- a/pkg/mmap/lib/src/mmap_impl.dart
+++ b/pkg/mmap/lib/src/mmap_impl.dart
@@ -125,17 +125,22 @@
// We need to attach the finalizer which calls close() and munmap().
final finalizerAddress = () {
+ // UBSAN will dereference callback-8 to get typeinfo to check for matching
+ // types at the call site for the finalizer callback. Make that slot
+ // addressable and leave it initialized to NULL.
+ final offset = 8;
+
final Pointer<Uint8> finalizerStub = mmap(nullptr, kPageSize,
kProtRead | kProtWrite, kMapPrivate | kMapAnon, -1, 0);
finalizerStub
.cast<Uint8>()
.asTypedList(kPageSize)
- .setAll(0, finalizerCode[Abi.current()]!);
+ .setAll(offset, finalizerCode[Abi.current()]!);
if (mprotect(finalizerStub, kPageSize, kProtRead | kProtExec) != 0) {
throw 'Failed to write executable code to the memory.';
}
- return finalizerStub.cast<Void>();
+ return finalizerStub.elementAt(offset).cast<Void>();
}();
base class PeerData extends Struct {