Fix race condition and use less memory in mono_lookup_icall_symbol. (mono/mono#14532)
authorJay Krell <jaykrell@microsoft.com>
Wed, 31 Jul 2019 12:13:21 +0000 (05:13 -0700)
committerAlexander Köplinger <alex.koeplinger@outlook.com>
Wed, 31 Jul 2019 12:13:21 +0000 (14:13 +0200)
commit938d4cf75f9c8191761be1c4c0606b059c4cda3f
tree9edb1bbc08f1bab6b6657f22c5f75bc1c204885a
parent5c392a6d6eaefe6f4e1d127dffa288c8797bfdf4
Fix race condition and use less memory in mono_lookup_icall_symbol. (mono/mono#14532)

It is racy because it is doing on-demand initialization, which is often racy.

The rewrite changes the result to be one pointer, atomically swapped, which implies a full barrier and data dependency, so no race.

It could also be fixed by copying and sorting an array of pairs, and one pointer to that.

However in order to save memory I instead use indirect data that is an array of uint16 pointing into the original data. That is a slight memory vs. time tradeoff.

Yes there is debugging code left, under #if, that I prefer to leave, both as evidence that I tested it, and to make it somewhat but ideally testable in future.

The code also was using an unnecessary somewhat risky but probably ok here way to compare pointers, that I fixed.

As well the result is marginally smaller and faster because it was inlining something like bubblesort, now reuses qsort.

Commit migrated from https://github.com/mono/mono/commit/c37fc12ca5edd9cbf169ac7e4ab5b2e6ef202f79
src/mono/mono/metadata/icall-table.c
src/mono/mono/mini/main.c