Skip to content
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
61 changes: 61 additions & 0 deletions .github/workflows/windows_ffi_memory.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Windows FFI memory ownership

on:
push:
branches: [windows-worker-thread-lazy-library]
paths:
- 'flutter_local_notifications_windows/src/**'
- 'flutter_local_notifications_windows/test/native/**'
- '.github/workflows/windows_ffi_memory.yml'
pull_request:
paths:
- 'flutter_local_notifications_windows/src/**'
- 'flutter_local_notifications_windows/test/native/**'
- '.github/workflows/windows_ffi_memory.yml'
workflow_dispatch:

permissions:
contents: read

jobs:
portable-ownership:
runs-on: ubuntu-latest
timeout-minutes: 10
defaults:
run:
working-directory: flutter_local_notifications_windows
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- name: Build production deallocators with ASan and UBSan
run: >-
g++ -std=c++17 -O1 -g -Wall -Wextra -Werror
-fsanitize=address,undefined -fno-omit-frame-pointer -fno-pie -no-pie
src/ffi_memory.cpp test/native/ffi_memory_test.cpp -o ffi_memory_test
- name: Exercise the actual C ABI deallocators
env:
ASAN_OPTIONS: alloc_dealloc_mismatch=1:detect_leaks=1
UBSAN_OPTIONS: halt_on_error=1:print_stacktrace=1
run: ./ffi_memory_test

windows-ownership:
runs-on: windows-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- name: Build and run production deallocators with MSVC ASan
shell: pwsh
working-directory: flutter_local_notifications_windows
env:
ASAN_OPTIONS: alloc_dealloc_mismatch=1
run: |
$vswhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
$vs = & $vswhere -latest -products '*' -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath
if (-not $vs) { throw 'Visual C++ tools not found' }
$vcvars = Join-Path $vs 'VC\Auxiliary\Build\vcvars64.bat'
$command = "call `"$vcvars`" && cl /nologo /std:c++17 /EHsc /Zi /W4 /WX /fsanitize=address src\ffi_memory.cpp test\native\ffi_memory_test.cpp /Fe:ffi_memory_test.exe && ffi_memory_test.exe"
& cmd.exe /d /c $command
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
1 change: 1 addition & 0 deletions flutter_local_notifications_windows/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ add_compile_definitions(_SILENCE_EXPERIMENTAL_COROUTINE_DEPRECATION_WARNINGS)

add_library(flutter_local_notifications_windows SHARED
"ffi_api.cpp"
"ffi_memory.cpp"
"plugin.cpp"
"utils.cpp"
"worker.cpp"
Expand Down
12 changes: 0 additions & 12 deletions flutter_local_notifications_windows/src/ffi_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,3 @@ NativeNotificationDetails* getPendingNotifications(NativePlugin* plugin, int* si
*size = count;
return out;
}

void freeDetailsArray(NativeNotificationDetails* ptr) { delete[] ptr; }

void freeLaunchDetails(NativeLaunchDetails details) {
if (details.payload != nullptr) delete[] details.payload;
for (int index = 0; index < details.data.size; index++) {
const auto pair = details.data.entries[index];
delete pair.key;
delete pair.value;
}
if (details.data.entries != nullptr) delete[] details.data.entries;
}
16 changes: 16 additions & 0 deletions flutter_local_notifications_windows/src/ffi_memory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "ffi_api.h"

// Keep deallocation independent of WinRT so its allocator contract can be tested
// without registering an app or contacting the Windows notification service.
void freeDetailsArray(NativeNotificationDetails* ptr) { delete[] ptr; }

void freeLaunchDetails(NativeLaunchDetails details) {
if (details.payload != nullptr) delete[] details.payload;
for (int index = 0; index < details.data.size; index++) {
const auto pair = details.data.entries[index];
// toNativeString allocates both strings with new char[].
delete[] pair.key;
delete[] pair.value;
}
if (details.data.entries != nullptr) delete[] details.data.entries;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "../../src/ffi_api.h"

#include <cstring>
#include <iostream>

// Match the allocation contract of toNativeString; no Dart arena owns these.
static char* copyString(const char* value) {
const auto size = std::strlen(value) + 1;
auto* result = new char[size];
std::memcpy(result, value, size);
return result;
}

int main() {
// Safe empty callback, including the absent optional payload/map.
freeLaunchDetails({false, notification, nullptr, {nullptr, 0}});

// Runtime size also covers the new T[count] path when an API returns zero.
volatile int emptySize = 0;
// An allocated empty array still belongs to the callback consumer.
freeLaunchDetails({true, notification, copyString(""), {new StringMapEntry[emptySize], 0}});

// Payload-only callbacks have no input map.
freeLaunchDetails({true, action, copyString("payload"), {nullptr, 0}});

// Exercise every string and the map array repeatedly. AddressSanitizer must
// check alloc_dealloc_mismatch=1 (Windows disables that check by default).
for (int iteration = 0; iteration < 10000; ++iteration) {
const auto entries = new StringMapEntry[3]{
{copyString("input"), copyString("value")},
{copyString(""), copyString("")},
{copyString("utf8"), copyString("\xD7\x90\xD7\x91")},
};
freeLaunchDetails({true, action, copyString("payload"), {entries, 3}});
}

// The public query-array deallocator retains its existing ABI and behavior.
freeDetailsArray(nullptr);
freeDetailsArray(new NativeNotificationDetails[emptySize]);
freeDetailsArray(new NativeNotificationDetails[2]{{1}, {2}});

std::cout << "ffi memory: empty, allocated-empty, payload-only, 10000 input maps, query arrays passed\n";
}
Loading