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

fix data/elem drop #2747

Merged
merged 22 commits into from
Nov 18, 2023
Merged

fix data/elem drop #2747

merged 22 commits into from
Nov 18, 2023

Conversation

yamt
Copy link
Collaborator

@yamt yamt commented Nov 10, 2023

currently, data.drop instruction is implemented by directly modifying the underlying module.
it breaks use cases where you have multiple instances sharing a single loaded module.
elem.drop has the same problem too.
this fixes the issue by mirroring the implementation approarch from toywasm: https://github.com/yamt/toywasm/blob/fca52a0174eb1644fea9caa3fe1faad598864f6a/lib/type.h#L488-L494.
that is, keep track of which data/elem segments have been dropped by using bitmaps for each module instances separately.
an obvious drawback of the approach is to consume extra memory for tracking. however,
given the way the wasm spec defines these operations, i suspect this is the only way
to support shared modules. it isn't allowed to simply ignore data.drop and elem.drop as
we should raise a trap when dropped segments are used.

also, add a sample to demonstrate the issue and make the ci run it.

also, add a missing check of dropped elements to the fast-jit version of table.init.

fixes: #2735
fixes: #2772


todo:

@yamt yamt changed the title add a test code to demonstrate data.drop issues fix data/elem drop Nov 14, 2023
@yamt yamt force-pushed the 2735 branch 3 times, most recently from 89f4c38 to 9e22da7 Compare November 16, 2023 02:46
@yamt yamt marked this pull request as ready for review November 16, 2023 05:31
Comment on lines 227 to 228
bh_bitmap *data_dropped;
bh_bitmap *elem_dropped;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The data drop is introduced in bulk memory and the elem drop is introduced in reference types, could we add macro WASM_ENABLE_BULK_MEMORY and WASM_ENABLE_REF_TYPES to control the code here, new and delete bitmap, so as to decrease footprint when they are not needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines +30 to +31
wasm_module_inst_t module_inst[N];
wasm_exec_env_t exec_env[N];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had better add = { 0 } to initialize module_inst and exec_env, or unexpected behavior may occur when destroying them.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they are initialized below explicitly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks.

Comment on lines 1169 to 1170
common->data_dropped = bh_bitmap_new(0, module->mem_init_data_count);
common->elem_dropped = bh_bitmap_new(0, module->table_init_data_count);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we allocate memory only when mem_init_data_count/mem_init_data_count is larger than 0? Or there may be a warning:
https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/core/iwasm/common/wasm_memory.c#L245

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Contributor

@wenyongh wenyongh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment on lines +30 to +31
wasm_module_inst_t module_inst[N];
wasm_exec_env_t exec_env[N];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks.

Comment on lines 6 to 8
#include <limits.h>
#include <stddef.h>
#include <string.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had better remove these lines, normally they are included in platform_internal.h of each platforms, and we don't include libc header files in core/shared/utils.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. done.

they (or equivalents) are supposed to be provided by bh_platform.h.
@yamt
Copy link
Collaborator Author

yamt commented Nov 17, 2023

the ci failure doesn't seem related to the changes.

  Connection failed [IP: 23.7.100.62 443]

@wenyongh wenyongh merged commit 562a5dd into bytecodealliance:main Nov 18, 2023
698 checks passed
victoryang00 pushed a commit to victoryang00/wamr-aot-gc-checkpoint-restore that referenced this pull request May 27, 2024
Currently, `data.drop` instruction is implemented by directly modifying the
underlying module. It breaks use cases where you have multiple instances
sharing a single loaded module. `elem.drop` has the same problem too.

This PR  fixes the issue by keeping track of which data/elem segments have
been dropped by using bitmaps for each module instances separately, and
add a sample to demonstrate the issue and make the CI run it.

Also add a missing check of dropped elements to the fast-jit `table.init`.

Fixes: bytecodealliance#2735
Fixes: bytecodealliance#2772
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.

fast-jit: missing check of dropped elements data.drop etc should not affect other instances sharing a module
2 participants