Skip to content

memory alignment issue that only occurs with specific buffer sizes (image dimensions) and results in leaks #14459

Open
@CetinSert

Description

@CetinSert

In a project compiled with emscripten (em++) to WASM, calls to a function with some image dimensions keep triggering

  memalign memset free
  memalign memset free
  memalign memset

repeatedly on every function call. When the function eventually returns a memory reference, this works as expected when probed with image-related functions for height, width, etc. BUT it cannot be deallocated and leak completely.

Other image dimensions both work and deallocate just fine; calling the function with such good dimensions do NOT keep triggering the above memalign repeatedly.

The behavior persists across


Please

  • see the attached screenshot first, under the Single-line Reproduction section right below
  • and toggle open the collapsed sections marked with 👉🏻👈🏻 for details.

Investigation Details 🕵🏻‍♂️

paulocoutinhox/pdfium-lib#33 (comment)
paulo-coutinho/pdfium-lib/blob/master/modules/wasm.py (how the project uses emscripten)

Single-line Reproduction 🔬

  1. Go to https://pdfviewer.github.io/
  2. Open developer console F12
  3. Evaluate the following in the console:
_PDFium_Init();
  1. Evaluate the following line several times and watch the memory grow:
var w = 496, h = 496; for (let i = 0; i < 5; i++) _FPDFBitmap_Destroy(FPDF.Bitmap_CreateEx(w, h, 4)); [ wasmMemory, wasmMemory.buffer.byteLength ] // ❌
👉🏻 496 × 496 × 4memalign trigger on each iteration 👈🏻 (CLICK/TAP HERE TO REVIEW EXECUTION)
  CreateEx
    memalign memset free
    memalign memset free
    memalign memset
  Destroy

  CreateEx
    memalign memset free
    memalign memset free
    memalign memset
  Destroy

  CreateEx
    memalign memset free
    memalign memset free
    memalign memset
  Destroy

  CreateEx
    memalign memset free
    memalign memset free
    memalign memset
  Destroy

  CreateEx
    memalign memset free
    memalign memset free
    memalign memset
  Destroy

image

  1. Note that this one is totally fine and does not leak:
var w = 495, h = 495; for (let i = 0; i < 5; i++) _FPDFBitmap_Destroy(FPDF.Bitmap_CreateEx(w, h, 4)); [ wasmMemory, wasmMemory.buffer.byteLength ] // ✔️
👉🏻 496 × 496 × 4 ✔️ memalign trigger only once 👈🏻 (CLICK/TAP HERE TO REVIEW EXECUTION)
  CreateEx
    memalign memset free
    memalign memset free
    memalign memset
  Destroy

  CreateEx
  Destroy

  CreateEx
  Destroy

  CreateEx
  Destroy

  CreateEx
  Destroy

I kindly ask emscripten experts for their help in finding how to compile the said project in a way that resolves this issue.

Investigation Details 🕵🏻‍♂️

paulocoutinhox/pdfium-lib#33 (comment)
paulo-coutinho/pdfium-lib/blob/master/modules/wasm.py (how the project uses emscripten)

FPDFBitmap_CreateEx call tree

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions