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

Support for use in WASM environments (#192) #1697

Merged
merged 1 commit into from Jul 3, 2020

Conversation

kleisauke
Copy link
Member

This PR enables the use of libvips in WebAssembly environments (see #192). It consist of fixing function pointer cast issues,
which can only be detected at runtime (as far as I know). This PR has been tested on a WASM-ported version of the libvips test suite that includes 192 different tests.

It is possible that not all function pointer cast issues are fixed by this PR, as the WASM build does not contain all dependencies supported by libvips. I've managed to support JPEG, PNG, TIFF and WebP images by using libjpeg-turbo, libpng, libtiff and libwebp. EXIF metadata in JPEG files and ICC profile support is also available by using libexif and lcms2. For more details, see #192 (comment).

Here is a brief overview of the related changes within this PR:

  • Add vips_area_free_cb (that matches the int (*VipsCallbackFn) (void *a, void *b) signature), in favour of the vips_free callback.
  • Deprecate vips_free, just use g_free for non-callback functions and vips_area_free_cb for callback functions (e.g. vips_blob_new or vips_image_set_blob).
  • vips_crop needs an adapter function to work properly. This is basically the same as this patch for glib. I've #ifdef'd this change with __EMSCRIPTEN__ since this is a quite a hackish workaround.
  • All GThreadFunc callbacks now have the correct signature (i.e. void *(*GThreadFunc) (void *data)).
  • Sometimes the last void * parameter was missing from the VipsSListMap2Fn and VipsRegionFillFn callbacks.
  • Add a void *user_data parameter to the GCompareFunc and GFunc callback functions. Needed by g_slist_sort and g_slist_foreach. This SO answer might be relevant here.
  • Signals using the vips_INT__VOID, g_cclosure_marshal_VOID__POINTER and g_cclosure_marshal_VOID__VOID closures are called with an additional void *data argument.

abi-compliance-checker result:
https://kleisauke.nl/compat_reports/vips/master_to_wasm-function-pointers/compat_report.html

It is undefined behavior in C and C++ to cast a function pointer
to another type and call it that way. This does work in most native
platforms, however, despite it being UB, but in WASM it can fail.

See:
https://emscripten.org/docs/porting/guidelines/function_pointer_issues.html
@jcupitt jcupitt merged commit e728e56 into libvips:master Jul 3, 2020
@jcupitt
Copy link
Member

jcupitt commented Jul 3, 2020

This patch is much smaller than I was fearing, phew! It seems straightforward.

I was wondering about the best way to prevent this kind of issue creeping back into libvips in the future. Do you have any thoughts on this? I suppose your set of WASM tests would be a good start.

@kleisauke
Copy link
Member Author

I was counting the amount of lines of the .patch file, and not the amount of changed lines, so the PR arrived much smaller. 😄

Indeed, the WASM tests should be a good start. I just found out that GCC supports a compiler flag called -Wcast-function-type that warns when a function pointer is cast to an incompatible function, which might help to prevent these issues as well. I had an attempt to add this to Travis, see:
master...kleisauke:cast-function-type
https://travis-ci.org/github/kleisauke/libvips/jobs/707609405

It reports some false-postives with the GCompareFunc callback as that is called with an extra user_data argument. But it also found 2 function pointer cast issues that the WASM test suite didn't catch, see:

libvips/tools/vips.c

Lines 111 to 112 in 84d0d5a

static void *
list_class( GType type )

static void *
list_class( GType type, void *data )

static void *
vips_gsf_tree_close( VipsGsfDirectory *tree )

static void *
vips_gsf_tree_close( VipsGsfDirectory *tree, void *a, void *b )

@kleisauke kleisauke deleted the wasm-function-pointers branch July 13, 2020 14:44
@jcupitt
Copy link
Member

jcupitt commented Jul 14, 2020

I think I fixed those function casts, thanks!

Do you plan a PR with those travis improvements?

jcupitt added a commit that referenced this pull request Jul 14, 2020
kleisauke added a commit to kleisauke/libvips that referenced this pull request Jul 14, 2020
kleisauke added a commit to kleisauke/libvips that referenced this pull request Jul 14, 2020
@kleisauke kleisauke mentioned this pull request Jul 14, 2020
@kleisauke
Copy link
Member Author

Great! PR #1716 improves the CI by enabling the -Wcast-function-type warning and updating Ubuntu/GCC to the latest version.

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

Successfully merging this pull request may close these issues.

None yet

2 participants