From 3b4fdaf548621cd905e419fa0b5d6b169cf230db Mon Sep 17 00:00:00 2001 From: Bob Singor Date: Thu, 19 Nov 2026 23:29:58 +0200 Subject: [PATCH] Keep a thread's PDFium up until its last runtime shuts down Two runtimes on one thread share that thread's PDFium on native: each is a new module object over the same library. Each calls EPDF_InitThread and EPDF_ShutdownThread once, and the first shutdown ran FPDF_DestroyLibrary for both. The runtime still open then read content with the thread's globals gone: EPDFAnnot_GetBlendMode crashed in CPDF_StreamContentParser::OnOperator on a null operator table (a read of address 0x8). Two local engines in one Node thread, one destroyed before the other, are enough. WASM is not affected: each runtime there has its own memory. EPDF_InitThread and EPDF_ShutdownThread are now counted per thread, and only the last EPDF_ShutdownThread tears PDFium down. A shutdown with no init before it still tears down, as FPDF_DestroyLibrary does. Test: EPDFViewEmbedderTest.ThreadLifecycleIsCounted, on a thread of its own. After two inits and one shutdown it reads an annotation's blend mode, which parses the appearance; after the last shutdown a new init starts PDFium again. Without the change it crashes at the line the engine crashed at. --- docs/embedpdf/fork-ledger.md | 8 ++-- fpdfsdk/epdf_threading.cpp | 32 +++++++++++++--- fpdfsdk/epdf_view_embeddertest.cpp | 59 ++++++++++++++++++++++++++++++ public/epdf_view.h | 15 +++++--- 4 files changed, 101 insertions(+), 13 deletions(-) diff --git a/docs/embedpdf/fork-ledger.md b/docs/embedpdf/fork-ledger.md index fd88d852ae..a04c153b09 100644 --- a/docs/embedpdf/fork-ledger.md +++ b/docs/embedpdf/fork-ledger.md @@ -486,10 +486,12 @@ taking upstream's side without putting `EPDF_TLS` back, on the variable renamed ### per-thread-init **Rule.** Library start-up runs once per thread. A process-wide value it writes must be -thread-local, or identical on every thread and on the allowlist. -**Why.** `EPDF_InitThread` calls `FPDF_InitLibrary` on every worker thread. +thread-local, or identical on every thread and on the allowlist. `EPDF_InitThread` and +`EPDF_ShutdownThread` are counted per thread: the last `EPDF_ShutdownThread` tears PDFium down. +**Why.** `EPDF_InitThread` calls `FPDF_InitLibrary` on every worker thread. Two runtimes on one +thread (two engines in a Node thread) share its PDFium, and each calls the pair. **Where.** `fpdf_view.cpp` (`FPDF_InitLibrary`), `epdf_threading.cpp`. -**Guarded by.** The thread soak. +**Guarded by.** The thread soak; `EPDFViewEmbedderTest.ThreadLifecycleIsCounted`. **When upstream touches this.** Anything new in `FPDF_InitLibraryWithConfig` or `FPDF_DestroyLibrary`. diff --git a/fpdfsdk/epdf_threading.cpp b/fpdfsdk/epdf_threading.cpp index 7b66801e47..b62b56717a 100644 --- a/fpdfsdk/epdf_threading.cpp +++ b/fpdfsdk/epdf_threading.cpp @@ -7,8 +7,12 @@ // explicit for callers that run PDFium on worker threads. With // embedpdf_thread_local_globals enabled (see core/fxcrt/epdf_tls.h) each worker // thread owns its own PDFium globals, so every such thread must initialize and -// tear down PDFium itself. With the flag disabled these are exact aliases of -// FPDF_InitLibrary / FPDF_DestroyLibrary. +// tear down PDFium itself. With the flag disabled they start and tear down the +// process's one PDFium, as FPDF_InitLibrary / FPDF_DestroyLibrary do. +// +// The calls are counted per thread. Callers that share a thread share its +// PDFium (two engines in one Node thread, each over the same native library), +// so a caller that leaves must not tear it down under the ones still there. // // These are intentionally exported for ALL targets (including wasm, where they // simply wrap the normal init/destroy) so shared runtime code can call a single @@ -16,16 +20,34 @@ #include "public/epdf_view.h" +#include "core/fxcrt/epdf_tls.h" #include "public/fpdfview.h" +namespace { + +// How many EPDF_InitThread() calls on this thread still wait for their +// EPDF_ShutdownThread(). +EPDF_TLS int g_thread_users = 0; + +} // namespace + FPDF_EXPORT void FPDF_CALLCONV EPDF_InitThread() { // Routes through FPDF_InitLibrary so the standard config/init path runs for - // the calling thread. + // the calling thread. It does nothing on a thread that is already started. FPDF_InitLibrary(); + ++g_thread_users; } FPDF_EXPORT void FPDF_CALLCONV EPDF_ShutdownThread() { - // Lifecycle-strict: callers must have already closed every PDFium handle - // created on this thread before invoking this. + // Lifecycle-strict: a caller must have already closed every PDFium handle + // it created on this thread before invoking this. + if (g_thread_users > 1) { + // Others still use this thread's PDFium: the last one out tears it down. + --g_thread_users; + return; + } + // The last one out, or a call with no EPDF_InitThread() before it, which + // tears down as FPDF_DestroyLibrary() does. + g_thread_users = 0; FPDF_DestroyLibrary(); } diff --git a/fpdfsdk/epdf_view_embeddertest.cpp b/fpdfsdk/epdf_view_embeddertest.cpp index e9e69e13ce..48bf0fdd85 100644 --- a/fpdfsdk/epdf_view_embeddertest.cpp +++ b/fpdfsdk/epdf_view_embeddertest.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include "constants/page_object.h" @@ -28,7 +29,9 @@ #include "fpdfsdk/epdf_c_api_test.h" #include "fpdfsdk/epdf_edit_test_util.h" #include "public/cpp/fpdf_scopers.h" +#include "public/epdf_annot.h" #include "public/epdf_progressive.h" +#include "public/fpdf_annot.h" #include "public/fpdf_edit.h" #include "public/fpdf_progressive.h" #include "public/fpdf_save.h" @@ -987,3 +990,59 @@ TEST_F(EPDFViewEmbedderTest, ImageOfAnotherDocumentDraws) { EXPECT_EQ(render(document.get(), document.get()), render(document.get(), other.get())); } + +#if defined(EPDF_THREAD_LOCAL_GLOBALS) +// EPDF_InitThread() and EPDF_ShutdownThread() are counted (rule +// per-thread-init): callers that share a thread share its PDFium, and one +// that leaves does not take it from the ones still there. A second engine in +// a Node thread did: once it was destroyed, the first read content with the +// thread's operator table gone. Runs on a thread of its own, as a worker +// does: the harness started the main thread's PDFium, and the last +// EPDF_ShutdownThread() here would tear that down. +TEST_F(EPDFViewEmbedderTest, ThreadLifecycleIsCounted) { + // A page with a square whose appearance multiplies. + const std::string pdf = MakePdf({ + "<< /Type /Catalog /Pages 2 0 R >>", + "<< /Type /Pages /Kids [3 0 R] /Count 1 >>", + "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] " + "/Annots [4 0 R] >>", + "<< /Type /Annot /Subtype /Square /Rect [10 10 60 60] " + "/AP << /N 5 0 R >> >>", + Stream("/Type /XObject /Subtype /Form /BBox [0 0 50 50] " + "/Resources << /ExtGState << /GS << /BM /Multiply >> >> >>", + "/GS gs 1 0 0 rg 0 0 50 50 re f"), + }); + // The square's blend mode, which is read by parsing its appearance. + auto blend_mode = [&pdf]() -> int { + embedpdf_test::ScopedTestDocument doc( + embedpdf_test::LoadMemDocumentForTest(pdf.data(), pdf.size(), nullptr)); + if (!doc) { + return -1; + } + ScopedFPDFPage page(FPDF_LoadPage(doc.get(), 0)); + if (!page) { + return -1; + } + ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page.get(), 0)); + return annot ? EPDFAnnot_GetBlendMode(annot.get()) : -1; + }; + + int with_one_left = -1; + int started_again = -1; + std::thread worker([&] { + EPDF_InitThread(); + EPDF_InitThread(); + EPDF_ShutdownThread(); + with_one_left = blend_mode(); + EPDF_ShutdownThread(); + + // The last one out tore PDFium down; the next one in starts it again. + EPDF_InitThread(); + started_again = blend_mode(); + EPDF_ShutdownThread(); + }); + worker.join(); + EXPECT_EQ(FPDF_BLENDMODE_Multiply, with_one_left); + EXPECT_EQ(FPDF_BLENDMODE_Multiply, started_again); +} +#endif // defined(EPDF_THREAD_LOCAL_GLOBALS) diff --git a/public/epdf_view.h b/public/epdf_view.h index 86fe406c50..e55fa1c3e9 100644 --- a/public/epdf_view.h +++ b/public/epdf_view.h @@ -24,11 +24,16 @@ extern "C" { // PDFium state, so every thread that uses PDFium MUST call // EPDF_InitThread before creating any handle and EPDF_ShutdownThread // after closing every handle it created. Handles must never cross -// threads. When per-thread globals are disabled, these are exact -// aliases of FPDF_InitLibrary / FPDF_DestroyLibrary and are safe to -// call once on a single thread. EPDF_ShutdownThread is lifecycle- -// strict: only legal after all handles on the calling thread are -// closed. +// threads. When per-thread globals are disabled, they start and tear +// down the process's one PDFium, as FPDF_InitLibrary and +// FPDF_DestroyLibrary do. +// The calls are counted per thread. Callers that share a thread +// (two runtimes loaded over the same native library) each call +// EPDF_InitThread once and EPDF_ShutdownThread once, and only the +// last EPDF_ShutdownThread tears PDFium down: a caller that leaves +// does not take PDFium from the ones still using it. +// EPDF_ShutdownThread is lifecycle-strict: a caller makes it only +// after closing every handle it created on the calling thread. FPDF_EXPORT void FPDF_CALLCONV EPDF_InitThread(); FPDF_EXPORT void FPDF_CALLCONV EPDF_ShutdownThread();