Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working v8 sample to load file in win-x64 or win-x86 #105

Closed
wickyhu opened this issue Mar 28, 2023 · 38 comments
Closed

Working v8 sample to load file in win-x64 or win-x86 #105

wickyhu opened this issue Mar 28, 2023 · 38 comments
Labels

Comments

@wickyhu
Copy link

wickyhu commented Mar 28, 2023

Hi,

I'm looking for a working sample with v8 win-x64 or win-x86 binaries.
I was successful with common binaries to load and render pdf, but always get memory access violation when swiched to v8 binaries.
I searched and found FPDF_InitEmbeddedLibraries in comments, but seems it is not found in v8 dll.
Could you advice me correct initial steps for v8?

Thanks!
Wicky Hu

@bblanchon
Copy link
Owner

Hi @wickyhu,

With the recent builds, you no longer need to call FPDF_InitEmbeddedLibraries().
Instead, I patch FPDF_InitLibraryWithConfig() so that it initializes V8 for you.

I last tested the V8 build some time ago; maybe an upstream change is causing a bug now.
I'll look into this and keep you updated.

Best regards,
Benoit

@bblanchon bblanchon added the bug label Mar 28, 2023
@wickyhu
Copy link
Author

wickyhu commented Mar 28, 2023

Hi Benoit,

Thank you for the quick reply.
I will wait for your good news.

Thanks~
Wicky Hu

@bblanchon
Copy link
Owner

I just ran a test program: it doesn't crash, but some elements are missing, so there is definitely something wrong.
@wickyhu, how did you make it crash?

@wickyhu
Copy link
Author

wickyhu commented Mar 28, 2023

Hi, bblanchon,

I have a fork of PdfiumViewer, which I combined some enhancements from others.
It was working properly with NuGet package bblanchon.PDFium.Win32.

I changed the package to bblanchon.PDFium.V8.Win32 just now.
So you can re-produce the issue by below steps:

  1. clone the project: https://github.com/wickyhu/PdfiumViewer.git
  2. compile and start Demo project
  3. open a pdf file.
  4. It would crash at call to FPDFDOC_InitFormFillEnvironment.

I guessed I need to do some initilization for v8 to make it working.
Please have a try.

Thanks~
Wicky Hu

@mara004
Copy link
Contributor

mara004 commented Mar 28, 2023

I've been facing similar problems with a crash at FPDFDOC_InitFormFillEnvironment() when using the V8 enabled binaries with pypdfium2 (see pypdfium2-team/pypdfium2#182).
Last time, I assumed that was because we don't implement all mandatory fields of FPDF_FORMFILLINFO, but perhaps this hypothesis was wrong, seeing as the same code can render forms correctly with non-V8 builds.

@mara004
Copy link
Contributor

mara004 commented Mar 28, 2023

Instead, I patch FPDF_InitLibraryWithConfig() so that it initializes V8 for you.

I've been using FPDF_InitLibrary(), does the patch cover that as well? Anyway, we still get the same crash even if changing the code to FPDF_InitLibraryWithConfig( FPDF_LIBRARY_CONFIG(2, None, None, 0) ).

@bblanchon
Copy link
Owner

@mara004, my test program calls FPDFDOC_InitFormFillEnvironment() but doesn't crash.
FPDF_InitLibrary() calls FPDF_InitLibraryWithConfig(), so it should work too.

@mara004
Copy link
Contributor

mara004 commented Mar 28, 2023

my test program calls FPDFDOC_InitFormFillEnvironment() but doesn't crash.

With pypdfium2, the crash is constantly reproducible:

@bblanchon
Copy link
Owner

bblanchon commented Mar 28, 2023

I updated the example to call FPDFDOC_InitFormFillEnvironment() and triggered a full build:
https://github.com/bblanchon/pdfium-binaries/actions/runs/4543264945
So far, no crash.

@wickyhu
Copy link
Author

wickyhu commented Mar 29, 2023

Hi, bblanchon,

I just tried the example.c in my environment:
win11, vs2022 community

I created an empty C++ console project named test, and build example.c with latest downloaded pdfium-v8-win-x64.tgz.

Below are my steps and findings:

Case 1: form_callbacks.version = 1

  1. run test.exe test.pdf out.pdf
  2. the exe completed without error, however, the out.pdf is corrupted, it can not be opened in acrobat reader or chrome
  3. when I traced into source, I found the returned form is zero for FPDFDOC_InitFormFillEnvironment call
    FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);

Case 2: form_callbacks.version = 2

  1. changed version from 1 to 2 , because of comments from https://pdfium.googlesource.com/pdfium/+/refs/heads/main/public/fpdf_formfill.h line 377
    form_callbacks.version = 2;
  2. build and run test.exe test.pdf out.pdf
  3. got exception on call FPDFDOC_InitFormFillEnvironment
    Exception thrown at 0x00007FF91A2120AB (pdfium.dll) in Test.exe: 0xC0000005: Access violation reading location 0x0000000000000018.

Best Regards
Wicky Hu

@wickyhu
Copy link
Author

wickyhu commented Mar 29, 2023

Hi, bblanchon,

One more information for Case 2:

  1. when I run Case 2 exe in command promt, I don't get error message, just no out.pdf generated
  2. when I run Case 2 in vs2022 debug mode, I got exception catched by vs2022

Best Regards
Wicky Hu

@wickyhu
Copy link
Author

wickyhu commented Mar 29, 2023

Hi, bblanchon,

I further tried example.c with pdfium-win-x64.tgz, which is non-v8 version.

Case 1: version=1, no error, returned form of FPDFDOC_InitFormFillEnvironment is not zero, but out.pdf is corrupted
Case 2: version=2, no exception, returned form of FPDFDOC_InitFormFillEnvironment is not zero, but out.pdf is corrupted

Best Regards
Wicky Hu

@bblanchon
Copy link
Owner

Hi Wicky,

Thank you very much for this information; there may be a problem with the initialization of XFA. I'll look into that.

The output from example.c is not a PDF but a PPM, a simple image format.
You can read this file format with GIMP, for example.

Best regards,
Benoit

@wickyhu
Copy link
Author

wickyhu commented Mar 29, 2023

Hi bblanchon,

Got it, I can open out.ppm now.

Best Regards
Wicky

@bblanchon
Copy link
Owner

I added a call to cppgc::InitializeProcess(); it seems to have fixed the issue.

@wickyhu
Copy link
Author

wickyhu commented Mar 29, 2023

Hi Benoit,

I just re-downloaded the v8 x64 binary from home page, it seems same as previous version.
I guess new build is not yet ready for downloading.
I will test again after new build released.

Thank you very much for the so quick reponse!

Best Regards
Wicky

@bblanchon
Copy link
Owner

You can download the build artifacts here:
https://github.com/bblanchon/pdfium-binaries/actions/runs/4552831557#artifacts

@mara004
Copy link
Contributor

mara004 commented Mar 29, 2023

Sounds good, thanks!

mara004 added a commit to pypdfium2-team/pypdfium2 that referenced this issue Mar 29, 2023
@mara004
Copy link
Contributor

mara004 commented Mar 29, 2023

Also thanks a lot to @wickyhu for reporting this!
(When I initially noticed the problem, I should probably have asked here in the first place rather than assuming it was an issue with my config.)

@bblanchon
Copy link
Owner

I’m closing this; don’t hesitate to open a new issue.
Thanks a lot guys.

@wickyhu
Copy link
Author

wickyhu commented Mar 30, 2023

Hi Benoit,

I just tested example.c with latest binaries.
There seems still an issue, not sure whether it's designed or not.

  1. pdfium-v8-win-x64
    case1: version=1, form=0, no error, out.ppm is good <---Note: here form=0, not same as other cases
    case2: version=2, form>0, no error, out.ppm is good

  2. pdfium-win-x64
    case1: version=1, form>0, no error, out.ppm is good
    case2: version=2, form>0, no error, out.ppm is good

The new build works in my project now.
Thanks again!

Best Regards
Wicky

@bblanchon
Copy link
Owner

bblanchon commented Mar 30, 2023

The comment you referred to previously gives an explanation:

When PDFium is built without the XFA module, version can be 1 or 2.
[...]
When PDFium is built with XFA module then the version must be 2.

To me, this explains why FPDFDOC_InitFormFillEnvironment() returns 0 with the V8 build, since XFA is enabled.

@wickyhu
Copy link
Author

wickyhu commented Mar 30, 2023

Yes, you are correct, it's reasonable. Thanks~~

@mara004
Copy link
Contributor

mara004 commented Apr 4, 2023

Fix confirmed with yesterday's build. pypdfium2's test suite now runs as expected with the V8 binaries.

@mara004
Copy link
Contributor

mara004 commented May 27, 2023

Hi @bblanchon,

A pypdfium2 user is asking for XFA support (pypdfium2-team/pypdfium2#221), and we both figured out that FPDF_LoadXFA() always seems to return error despite the V8/XFA enabled binaries.

Is it possible there is still something wrong with XFA init?

Thanks!

@bblanchon
Copy link
Owner

@mara004, I saw your message, and I plan on testing this function soon, but I'm overwhelmed at the moment.
I'll get back to you as soon I can put my hands on it.

@mara004
Copy link
Contributor

mara004 commented Jun 5, 2023

Thanks for the response. Take your time. :) I already guessed you might be away or else busy.

@mara004
Copy link
Contributor

mara004 commented Jun 20, 2023

FYI, there's been a report on pdfium's mailing list concerning an out-of-memory error on library init with iOS.
From the stack trace, it seems like the V8 enabled binaries. cppgc::InitializeProcess() seems to be the problematic call: https://groups.google.com/g/pdfium/c/XG7hE1K3Ug8

Is it possible this is caused by the pdfium-binaries V8 patch?
Just mentioning for the record as I saw it and thought of this thread. No need to hurry.

@bblanchon
Copy link
Owner

The last message on the thread says:

Problem solved. I was able to run it on my iPad.
I built using "build.sh" from pdfium-binaries. When doing the build, I added the "build for musl" build option.

I'll assume that this was the issue.

@mara004
Copy link
Contributor

mara004 commented Jun 21, 2023

Yeah, sounds like it had nothing to do with the V8 patch after all. Sorry for the disruption.

@mara004
Copy link
Contributor

mara004 commented Aug 11, 2023

Hi @bblanchon,
I don't want to disturb you - just a small notification this XFA quest still stands, as it was some time ago now.
Thanks!

@bblanchon
Copy link
Owner

Thank you for reaching out. You're right; this issue got out of my attention.
I'll try to work on this during the weekend. Hopefully, today 🤞

@mara004

This comment was marked as outdated.

@mara004
Copy link
Contributor

mara004 commented Aug 12, 2023

Nevermind, when setting chrome://flags/#pdf-xfa-forms to Enabled, Chromium can display our test file correctly, but with our code FPDF_LoadXFA() fails. So we would still be glad if you could take a look.

@bblanchon
Copy link
Owner

I made some discoveries:

  1. FPDF_LoadXFA() must be called after FPDFDOC_InitFormFillEnvironment()
  2. The m_pJsPlatform of the FPDF_FORMFILLINFO passed to FPDFDOC_InitFormFillEnvironment() must point to an IPDF_JSPLATFORM
  3. The version of the IPDF_JSPLATFORM must be set to 3
  4. The callback pointers in IPDF_JSPLATFORM can be null

Something along those lines:

IPDF_JSPLATFORM platform_callbacks = {0};
platform_callbacks.version = 3;

FPDF_FORMFILLINFO form_callbacks = {0};
form_callbacks.version = 2;
form_callbacks.m_pJsPlatform = &platform_callbacks;
FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);

FPDF_LoadXFA(doc);

When you do this, FPDF_LoadXFA() succeeds 🎉

However, when we call FPDF_DestroyLibrary() after a successful FPDF_LoadXFA(), the following assertion fails:

fxjs/cfxjs_engine.cpp:324: void FXJS_Release(): Assertion `!g_isolate || g_isolate_ref_count == 0' failed.

There is undoubtedly something wrong with my patch to this function, but it's late, so I must stop here for today.

@mara004
Copy link
Contributor

mara004 commented Aug 12, 2023

Gotcha, thanks for the pre-requisites checklist, that was an eye-opener.
We had a typo in the FPDF_FORMFILLINFO construction. It's not jsPlatform, but m_pJsPlatform. (I inherited that typo from the reporter's code snippet and didn't verify.) Unfortunately setting a wrong field silently passes.

My sample now renders correctly. I didn't have trouble with crashes on library destruction yet.
Did you make sure to close all objects correctly?

Anyway, thanks a lot!

@bblanchon
Copy link
Owner

I finally found what was causing the assertion failure: you must call FPDFDOC_ExitFormFillEnvironment() before FPDF_CloseDocument().

@mara004
Copy link
Contributor

mara004 commented Sep 25, 2023

Yeah I think the API contract with pdfium is that objects should be closed in reverse order to loading.
(I doubt if it's in the headers, but I once asked on the mailing list.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants