Description
First and foremost, apologies if this is the incorrect repository to submit this bug report.
I am in the middle of creating a Ghostscript Dart binding using dart:ffi
, package:ffi/ffi.dart
, and ffigen
. I am basing my logic on a PdfScribe which contains a near identical binding to the Ghostscript C-API. The only difference is that the latest commit on the agpl
branch has the instance pointer managed as a singleton in the C code. However, I am coming across intermittent crashes that appear unrelated to my code.
Sometimes when running the example Flutter app in Windows, calling the binding function ("Start" button) would work as intended (see expected print outputs).
flutter: The Dart VM service is listening on http://127.0.0.1:64764/s59weqmIGwk=/
flutter: [gs, -dBATCH, -dNOPAUSE, -dSAFER, -dAutoRotatePages=/None, -sDEVICE=pdfwrite, -sOutputFile=out.pdf, C:\src\ghostscript_dart\in.tmp, -c, [/Creator(flutter (PSCRIPT5)) /DOCINFO pdfmark, -f]
flutter: argv = 21cd7c7ef00
flutter: argv[0] @ 21cd0894420 = gs
flutter: argv[1] @ 21cd0894160 = -dBATCH
flutter: argv[2] @ 21cd0824e60 = -dNOPAUSE
flutter: argv[3] @ 21cd0894190 = -dSAFER
flutter: argv[4] @ 21cd08253a0 = -dAutoRotatePages=/None
flutter: argv[5] @ 21cd0824e80 = -sDEVICE=pdfwrite
flutter: argv[6] @ 21cd0824ee0 = -sOutputFile=out.pdf
flutter: argv[7] @ 21cd83046c0 = C:\src\ghostscript_dart\in.tmp
flutter: argv[8] @ 21cd08941a0 = -c
flutter: argv[9] @ 21cd82681c0 = [/Creator(flutter (PSCRIPT5)) /DOCINFO pdfmark
flutter: argv[10] @ 21cd08941f0 = -f
flutter: argv[11] @ 0 = nullptr (believe me)
flutter: -----------------------------------------------
argc=11
String 0 @ 0000021CD7C7EF00: gs
String 1 @ 0000021CD7C7EF08: -dBATCH
String 2 @ 0000021CD7C7EF10: -dNOPAUSE
String 3 @ 0000021CD7C7EF18: -dSAFER
String 4 @ 0000021CD7C7EF20: -dAutoRotatePages=/None
String 5 @ 0000021CD7C7EF28: -sDEVICE=pdfwrite
String 6 @ 0000021CD7C7EF30: -sOutputFile=out.pdf
String 7 @ 0000021CD7C7EF38: C:\src\ghostscript_dart\in.tmp
String 8 @ 0000021CD7C7EF40: -c
String 9 @ 0000021CD7C7EF48: [/Creator(flutter (PSCRIPT5)) /DOCINFO pdfmark
String 10 @ 0000021CD7C7EF50: -f
GPL Ghostscript GIT PRERELEASE 10.04.0 (2024-02-20)
Copyright (C) 2024 Artifex Software, Inc. All rights reserved.
This software is supplied under the GNU AGPLv3 and comes with NO WARRANTY:
see the file COPYING for details.
Loading NimbusMonoPS-Regular font from %rom%Resource/Font/NimbusMonoPS-Regular... 4015840 2684625 1807376 524005 1 done.
%%[ ProductName: GPL Ghostscript GIT PRERELEASE ]%%
%%[Page: 1]%%
%%[LastPage]%%
flutter: initWithArgs: 0
flutter:
-------------------
flutter: _onPressed done!
Unhandled exception: A heap has been corrupted
Other times (for reasons unknown), calling the same binding function would crash with the message "Lost connection to device.". When running the same Windows app with the Visual Studio debugger (open ghostscript_dart\example\build\windows\x64\ghostscript_dart_example.sln
in Visual Studio and press "Local Windows Debugger"), I get the following error:
A breakpoint instruction (__debugbreak() statement or a similar call) was executed in ghostscript_dart_example.exe.
After clicking on "Continue" on the debugger, I get the following error dialog:
Unhandled exception at 0x00007FF9C037C7A9 (ntdll.dll) in ghostscript_dart_example.exe: 0xC0000374: A heap has been corrupted (parameters: 0x00007FF9C03F38A0).
After pressing "Continue" inside of Visual Studio, the app continues to run, however with corrupted arguments.
flutter: [gs, -dBATCH, -dNOPAUSE, -dSAFER, -dAutoRotatePages=/None, -sDEVICE=pdfwrite, -sOutputFile=out.pdf, C:\src\ghostscript_dart\in.tmp, -c, [/Creator(flutter (PSCRIPT5)) /DOCINFO pdfmark, -f]
flutter: argv = 21cd7c7dbc0
flutter: argv[0] @ 21cd7fe1610 = gs
flutter: argv[1] @ 21cd7fe1390 = -dBATCH
flutter: argv[2] @ 21cd7c7dbe0 = -dNOPAUSE
flutter: argv[3] @ 21cd7fe15d0 = -dSAFER
flutter: argv[4] @ 21cd7c7df80 = -dAutoRotatePages=/None
flutter: argv[5] @ 21cd7c7dc00 = -sDEVICE=pdfwrite
flutter: argv[6] @ 21cd7c7dc20 = -sOutputFile=out.pdf
flutter: argv[7] @ 21cd082dcf0 = C:\src\ghostscript_dart\in.tmp
flutter: argv[8] @ 21cd7fe1580 = -c
flutter: argv[9] @ 21cd08b4290 = [/Creator(flutter (PSCRIPT5)) /DOCINFO pdfmark
flutter: argv[10] @ 21cd7fe1640 = -f
flutter: argv[11] @ 0 = nullptr (believe me)
flutter: -----------------------------------------------
argc=11
String 0 @ 0000021CD7C7DBC0: gs
String 1 @ 0000021CD7C7DBC8: -dBATCH
String 2 @ 0000021CD7C7DBD0: Ç▀╟╫∟☻
String 3 @ 0000021CD7C7DBD8: -dSAFER
String 4 @ 0000021CD7C7DBE0: -dAutoRotatePages=/None
String 5 @ 0000021CD7C7DBE8: ǧ■╫∟☻
String 6 @ 0000021CD7C7DBF0: -sOutputFile=out.pdf
String 7 @ 0000021CD7C7DBF8: C:\src\ghostscript_dart\in.tmp
String 8 @ 0000021CD7C7DC00: -c
String 9 @ 0000021CD7C7DC08: [/Creator(flutter (PSCRIPT5)) /DOCINFO pdfmark
String 10 @ 0000021CD7C7DC10: -f
GPL Ghostscript GIT PRERELEASE 10.04.0 (2024-02-20)
Copyright (C) 2024 Artifex Software, Inc. All rights reserved.
This software is supplied under the GNU AGPLv3 and comes with NO WARRANTY:
see the file COPYING for details.
Error: /undefinedfilename in (\034\002)
Operand stack:
Execution stack: %interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push
Dictionary stack: --dict:744/1123(ro)(G)-- --dict:0/20(G)-- --dict:85/200(L)--
Current allocation mode is local
Last OS error: Invalid argumentGPL Ghostscript GIT PRERELEASE 10.04.0: Unrecoverable error, exit code 1
flutter: initWithArgs: -100
flutter:
-------------------
This memory corruption has lead to either:
- string parsing errors,
- pointer access violation
0x900001a751cabb20 -- access violation
0x000001a751cabb20 -- correct address
I have noticed a couple things I believe are worth noting and related questions:
- The crashes (access violations exceptions) are occurring intermittently. When running the example project in my repository, select any PostScript file (you can get one from Ghostscript website), press "Start" consecutively with the debugger on, and observe the exceptions thrown at seemingly random amounts of pressing the "Start" button (usually within 10 presses). Can someone confirm whether the exceptions thrown are due to bugs in my implementation of the SDK or the SDK itself?
Pointer addresses of pointers created byThis concerned has been resolved,malloc
/calloc
in Dart are not equivalent pointer addresses as can be seen in the printed logs (Please refer to the linked codebase to see the exact print statements used). I have long understood that pointers shall not be passed between isolates. However, this is occurring regardless of whether the pointer has been allocated in a Dart isolate, or whether the pointer has been allocated synchronously with the UI-thread. I see that Dart usesCoTaskMemAlloc
andCoTaskMemFree
on Windows for memory allocation. However, why does it appear the pointer addresses differ in Dart and in C?- Can I see the function responsible for passing pointers between Dart and C?
Access violation in flutter_windows.dll
Sometimes, I would get this error instead:
Unhandled exception at 0x00007FF93FAEC1A6 (flutter_windows.dll) in ghostscript_dart_example.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
Behavior: crashes
Call stack:
flutter_windows.dll!flutter::Engine::DispatchPointerDataPacket(std::unique_ptr<flutter::PointerDataPacket,std::default_delete<flutter::PointerDataPacket>> packet, unsigned __int64 trace_flow_id) Line 436
[Inline Frame] flutter_windows.dll!std::_Func_class<void>::operator()() Line 874
flutter_windows.dll!fml::MessageLoopWin::Run() Line 39
flutter_windows.dll!fml::MessageLoopImpl::DoRun() Line 94
[External Code]
flutter_windows.dll!thread_start<unsigned int (__cdecl*)(void *),1>(void * const parameter) Line 97
Illegal instruction in flutter_windows.dll
Unhandled exception at 0x00007FF92CA95D89 (flutter_windows.dll) in ghostscript_dart_example.exe: 0xC000001D: Illegal Instruction.
Environment information
Windows 11 Home 23H2 build 22631.3155 running in Parallels on an Intel-based MacBook Pro from 2019.
Output of flutter doctor -v
:
PS C:\src\ghostscript_dart> flutter doctor -v
[√] Flutter (Channel stable, 3.19.2, on Microsoft Windows [Version 10.0.22631.3155], locale en-NL)
• Flutter version 3.19.2 on channel stable at C:\src\flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 7482962148 (10 days ago), 2024-02-27 16:51:22 -0500
• Engine revision 04817c99c9
• Dart version 3.3.0
• DevTools version 2.31.1
[√] Windows Version (Installed version of Windows is version 10 or higher)
(or visit https://flutter.dev/docs/get-started/install/windows#android-setup for detailed instructions).
If the Android SDK has been installed to a custom location, please use
`flutter config --android-sdk` to update to that location.
[X] Chrome - develop for the web (Cannot find Chrome executable at .\Google\Chrome\Application\chrome.exe)
! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.
[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.8.6)
• Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
• Visual Studio Community 2022 version 17.8.34525.116
• Windows 10 SDK version 10.0.22621.0
[!] Android Studio (not installed)
• Android Studio not found; download from https://developer.android.com/studio/index.html
(or visit https://flutter.dev/docs/get-started/install/windows#android-setup for detailed instructions).
[√] VS Code (version 1.87.0)
• VS Code at C:\Users\dsrahmatullah\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.84.0
[√] Connected device (2 available)
• Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.22631.3155]
• Edge (web) • edge • web-javascript • Microsoft Edge 122.0.2365.66
[√] Network resources
• All expected network resources are available.
! Doctor found issues in 3 categories.