Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5.6k
[wasm] more cases when looking up unmanaged delegates#107113
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
def8ff6
Handle exceptional cases when looking up unmanaged delegates
lewing 7a123d7
Add a couple more changes
lewing 7e1edca
Add namespace to the mix
lewing 750a984
In both places
lewing a3f82a7
Rework tests
lewing 7abee5d
Fix test
lewing 9285256
refactor the entry generator
lewing 8c995ae
More cleanup
lewing f9b6e4e
Rework the key handling
lewing 62f480b
Rework the parameter building
lewing 6629e86
Use the token in the callnback key
lewing d870553
Use string interopolation to reduce allocations
lewing ac71581
Separate the token from the key but look for the token first
lewing f496323
Fix funcs
lewing b0dd210
Fix formatting
lewing ffae952
Do a binary search for the token and key
lewing 848f8be
Clean up import lookup
lewing 752fbf4
Add null terminators back to imports
lewing 2ec97d0
Add more diagnotics
lewing 8124b87
Remove test test case
lewing 3b09edd
Misc cleanup
lewing ea613b4
huh
lewing f1b535d
try again
lewing d4a78f6
fix unbalanced gc unsafe area
lewing 9992ff1
Add assertions
lewing a179289
Fix the comparison
lewing 868fdb3
bsearch for tables too
lewing 96a6abe
Small clean-ups
lewing d4b8bab
try a different compile time newline
lewing 77cefac
Escape the other string literals in the generated code
lewing 0700827
Add tests with surrogate pairs
lewing aee105d
Don't encode null strings
lewing 03e48b1
Update PInvokeTableGeneratorTests.cs
lewing e24272e
Fix typo
lewing f75d28b
Merge branch 'main' into all-missing-ns
lewing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -23,39 +23,71 @@ mono_wasm_pinvoke_vararg_stub (void) | ||
| /* This is just a stub used to mark vararg pinvokes */ | ||
| } | ||
| int | ||
| table_compare_name (const void *t1, const void *t2) | ||
| { | ||
| return strcmp (((PinvokeTable*)t1)->name, ((PinvokeTable*)t2)->name); | ||
| } | ||
| void* | ||
| wasm_dl_lookup_pinvoke_table (const char *name) | ||
| { | ||
| for (int i = 0; i < sizeof (pinvoke_tables) / sizeof (void*); ++i) { | ||
| if (!strcmp (name, pinvoke_names [i])) | ||
| return pinvoke_tables [i]; | ||
| } | ||
| return NULL; | ||
| PinvokeImport needle = { name, NULL }; | ||
| return bsearch (&needle, pinvoke_tables, (sizeof (pinvoke_tables) / sizeof (PinvokeTable)), sizeof (PinvokeTable), table_compare_name); | ||
| } | ||
| int | ||
| wasm_dl_is_pinvoke_table (void *handle) | ||
| { | ||
| for (int i = 0; i < sizeof (pinvoke_tables) / sizeof (void*); ++i) { | ||
| if (pinvoke_tables[i] == handle) { | ||
| for (int i = 0; i < sizeof (pinvoke_tables) / sizeof (PinvokeTable); ++i) { | ||
| if (&pinvoke_tables[i] == handle) { | ||
| return 1; | ||
| } | ||
| } | ||
| return 0; | ||
| } | ||
| static int | ||
| export_compare_key (const void *k1, const void *k2) | ||
| { | ||
| return strcmp (((UnmanagedExport*)k1)->key, ((UnmanagedExport*)k2)->key); | ||
| } | ||
| static int | ||
| export_compare_key_and_token (const void *k1, const void *k2) | ||
| { | ||
| UnmanagedExport *e1 = (UnmanagedExport*)k1; | ||
| UnmanagedExport *e2 = (UnmanagedExport*)k2; | ||
| // first compare by key | ||
| int compare = strcmp (e1->key, e2->key); | ||
| if (compare) | ||
| return compare; | ||
| // then by token | ||
| return (int)(e1->token - e2->token); | ||
lewing marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| void* | ||
| wasm_dl_get_native_to_interp (const char *key, void *extra_arg) | ||
| wasm_dl_get_native_to_interp (uint32_t token, const char *key, void *extra_arg) | ||
| { | ||
| #ifdef GEN_PINVOKE | ||
| for (int i = 0; i < sizeof (wasm_native_to_interp_map) / sizeof (void*); ++i) { | ||
| if (!strcmp (wasm_native_to_interp_map [i], key)) { | ||
| void *addr = wasm_native_to_interp_funcs [i]; | ||
| wasm_native_to_interp_ftndescs [i] = *(InterpFtnDesc*)extra_arg; | ||
| return addr; | ||
| } | ||
| UnmanagedExport needle = { key, token, NULL }; | ||
| int count = (sizeof (wasm_native_to_interp_table) / sizeof (UnmanagedExport)); | ||
| // comparison must match the one used in the PInvokeTableGenerator to ensure the same order | ||
| UnmanagedExport *result = bsearch (&needle, wasm_native_to_interp_table, count, sizeof (UnmanagedExport), export_compare_key_and_token); | ||
| if (!result) { | ||
| // assembly may have been trimmed / modified, try to find by key only | ||
| result = bsearch (&needle, wasm_native_to_interp_table, count, sizeof (UnmanagedExport), export_compare_key); | ||
| } | ||
| return NULL; | ||
| if (!result) | ||
| return NULL; | ||
| void *addr = result->func; | ||
| wasm_native_to_interp_ftndescs [result - wasm_native_to_interp_table] = *(InterpFtnDesc*)extra_arg; | ||
| return addr; | ||
| #else | ||
| return NULL; | ||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66 src/mono/wasm/Wasm.Build.Tests/PInvokeTableGeneratorTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 12 additions & 1 deletion
13 src/mono/wasm/testassets/Wasm.Buid.Tests.Programs/UnmanagedCallback.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -6,13 +6,24 @@ public unsafe partial class Test | ||
| public unsafe static int Main(string[] args) | ||
| { | ||
| ((IntPtr)(delegate* unmanaged<int,int>)&Interop.Managed8\u4F60Func).ToString(); | ||
| Console.WriteLine($"main: {args.Length}"); | ||
| Interop.UnmanagedFunc(); | ||
| return 42; | ||
| } | ||
| } | ||
| namespace Conflict.A { | ||
| file class Interop { | ||
| [UnmanagedCallersOnly(EntryPoint = "ConflictManagedFunc")] | ||
| public static int Managed8\u4F60Func(int number) | ||
pavelsavara marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| { | ||
| Console.WriteLine($"Conflict.A.Managed8\u4F60Func({number}) -> {number}"); | ||
| return number; | ||
| } | ||
| } | ||
| } | ||
| file partial class Interop | ||
| { | ||
| [UnmanagedCallersOnly(EntryPoint = "ManagedFunc")] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.