This is an archive of the discontinued LLVM Phabricator instance.

[clang][MinGW] Add asan DLL lib before other libs and objects
ClosedPublic

Authored by alvinhochun on Mar 26 2023, 5:07 AM.

Details

Summary

As stated in https://github.com/llvm/llvm-project/issues/61685, by
passing LLD the import lib of the asan DLL first, the asan DLL will be
listed as the first entry in the Import Directory Table, making it be
loaded first before other user DLLs. This allows asan to be initialized
as early as possible to increase its instrumentation coverage to include
other DLLs not built with asan.

This also avoids some false asan reports on realloc for memory
allocated during initialization of user DLLs being loaded earlier than
asan, because after this change they will be loaded later than asan.

Diff Detail

Event Timeline

alvinhochun created this revision.Mar 26 2023, 5:07 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 26 2023, 5:07 AM
alvinhochun requested review of this revision.Mar 26 2023, 5:07 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 26 2023, 5:07 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

Trying to fix the test...

I tested this, and this does fix the repro from the linked issue.

I wonder if we do want to move all of these before the app itself, or if it's enough to just move the reference to asan_dynamic-<arch>.dll.a earlier, and keep the rest as it was? Especially the whole-archive bit ends up linking a bunch of things statically, ordered way before the app itself. On one hand, I wonder if it would have subtle effects that we don't notice until way later, and we should only change as little as we need (moving the import library reference earlier), or if there are other potential benefits to moving the wholearchive-linked bits earlier too? (I'm wondering about things like constructor execution orders and such.)

clang/test/Driver/mingw-sanitizers.c
1–15

Hm, what does this do - some sort of more portable version of just touching a file to create it?

I tested this, and this does fix the repro from the linked issue.

Thanks for testing!

I wonder if we do want to move all of these before the app itself, or if it's enough to just move the reference to asan_dynamic-<arch>.dll.a earlier, and keep the rest as it was? Especially the whole-archive bit ends up linking a bunch of things statically, ordered way before the app itself. On one hand, I wonder if it would have subtle effects that we don't notice until way later, and we should only change as little as we need (moving the import library reference earlier), or if there are other potential benefits to moving the wholearchive-linked bits earlier too? (I'm wondering about things like constructor execution orders and such.)

That is a valid concern, especially if we want to backport this change to 16.x. I didn't think about this clearly before. With this in mind, I would change the patch to only add a duplicate of asan_dynamic-<arch>.dll.a early and keep the existing flags at their old position.

I can do some experiments with the new asan test setup I have and see if there are other benefits of moving the other flags. That will be for separate patches.

clang/test/Driver/mingw-sanitizers.c
1–15

Yeah that's just creating the file (though it would also overwrite the existing content). I believe lit has echo built-in but touch may not be available on Windows so this seems like a good idea to me.

mstorsjo added inline comments.Mar 28 2023, 4:26 AM
clang/test/Driver/mingw-sanitizers.c
1–15

Ah, I see. FWIW, the lit tests in general do assume a handful of unix utilities - we seem to have unconditional uses of touch in various tests. The lit tests check if these tools are available in path, and if they aren't, it tries to locate an installation of Git for Windows (which contains what's needed) and add that to the path internally: https://github.com/llvm/llvm-project/blob/llvmorg-17-init/llvm/utils/lit/lit/llvm/config.py#L29-L35

alvinhochun added inline comments.Mar 28 2023, 4:31 AM
clang/test/Driver/mingw-sanitizers.c
1–15

I suppose that works. Would you prefer to use touch here too?

mstorsjo added inline comments.Mar 28 2023, 4:33 AM
clang/test/Driver/mingw-sanitizers.c
1–15

I don't have a strong opinion, but with touch I know what is intended, while this stopped my reading, wondering what the flag to echo does and if this is meant to have any specific function other than creating the file.

alvinhochun retitled this revision from [clang][MinGW] Add asan link flags before other libs and objects to [clang][MinGW] Add asan DLL lib before other libs and objects.

Updated per comments.

mstorsjo accepted this revision.Mar 28 2023, 12:24 PM

LGTM overall.

I wondered if the modified test runs correctly on Windows (any test that touches paths is prone to break) but I see that it seems to have run successfully on both Windows and Linux in the premerge CI, so that's at least good. One minor question for the test.

clang/test/Driver/mingw-sanitizers.c
2

You're using %/t.a here while the file you touch is %t.a - I don't think I have seen %/t anywhere here before. I do see that lit seems to replace it though... Is this an intentional thing (what's the difference to %t though?) or is it a consistently copied typo?

This revision is now accepted and ready to land.Mar 28 2023, 12:24 PM
alvinhochun added inline comments.Mar 28 2023, 1:19 PM
clang/test/Driver/mingw-sanitizers.c
2

Ah yes, that is intentional. It's documented on https://llvm.org/docs/CommandGuide/lit.html#substitutions to be "%t but \ is replaced by /". The reason for this is that, the previous Windows pre-merge check failed because when clang -### prints the command line it quotes and backslash-escape the arguments., FileCheck cannot match the INPUT path with double backslashes. Using forward slashes avoids this issue.

mstorsjo added inline comments.Mar 28 2023, 1:31 PM
clang/test/Driver/mingw-sanitizers.c
2

Oh, I see, thanks - TIL! That makes sense. I wonder why a grep for that didn't get almost any hits in the clang/test/Driver directory though - but maybe it's used more elsewhere?