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

NFC: Refactor clang extra args addition #1444

Merged

Conversation

DeeSee
Copy link

@DeeSee DeeSee commented Jul 13, 2020

  • Removing unused AddClangArgumentPair method
  • Replacing linear quadratic complexity for flags uniquing with linear
  • Inlining AddClangArgument method (not used anywhere outside SwiftASTContext.cpp)
  • Generalized flags uniquing mechanism for easier expanding unique flags list

@DeeSee
Copy link
Author

DeeSee commented Jul 13, 2020

@adrian-prantl please review

bool AddClangArgument(std::string arg, bool unique = true);

bool AddClangArgumentPair(llvm::StringRef arg1, llvm::StringRef arg2);

Choose a reason for hiding this comment

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

This looks good!

lldb/source/Symbol/SwiftASTContext.cpp Outdated Show resolved Hide resolved
};

inline bool IsMultiArgClangFlag(const StringRef& arg) {
return arg == "-D" || arg == "-U" || arg == "-working-directory";

Choose a reason for hiding this comment

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

Previously we only had to check for multi-arg flags in the 3 cases we actually were doing something special to it. But with uniquing, do we need to check for more cases?
I guess we are still only uniquing the arguments which we recognize in GetClangArgCategory, right?

Copy link
Author

@DeeSee DeeSee Jul 13, 2020

Choose a reason for hiding this comment

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

For now that's true (except that it would be (GetClangArgCategory(arg) & ClangArgCategoryUnique) || arg == "-working-directory" as we use -working-directory only to apply to paths to make them absolute). In future there'll be at least -fmodule-map-file= which is single-arg by design, although it wouldn't break this method logic anyway. I could change this code to use output of GetClangArgCategory if you're okay with single-arg case.

Copy link
Author

Choose a reason for hiding this comment

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

Actually, I've forgot to mention that there're different conditions: == vs startswith. Would you advise to separate some list of constants to unify lists of args to join/unique? I think of something like this:

const StringRef kMacroFlags[] = { "-D", "-U" };
const StringRef kUniqueMultiArgFlags[] = { "-I", "-F" }; // this would appear in the next PR, need an elegant way to append kMacroFlags here (using macro? :) )

And then just use these arrays to check for == or startswith where needed

Copy link
Author

Choose a reason for hiding this comment

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

@adrian-prantl what do you think about this?

Choose a reason for hiding this comment

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

Sure, that seems reasonable. The naming should follow the LLDB style though:
std::array<StringRef> macro_flags = {...};
std::array<StringRef> contractable_multiword_flags = {..};

Copy link
Author

Choose a reason for hiding this comment

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

Ok, will add macro_flags then

lldb/source/Symbol/SwiftASTContext.cpp Outdated Show resolved Hide resolved
lldb/source/Symbol/SwiftASTContext.cpp Outdated Show resolved Hide resolved
} // namespace

void SwiftASTContext::AddExtraClangArgs(std::vector<std::string> ExtraArgs) {
swift::ClangImporterOptions &importer_options = GetClangImporterOptions();

Choose a reason for hiding this comment

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

This function has very few dependencies. I previously recommended writing an end-to-end test for the uniquing, but I wonder if we could write a unit test for it. That seems like a better fit.

Copy link
Author

Choose a reason for hiding this comment

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

There's a test for -Werror skipping in this method, so I was hoping that it's possible to write more tests for logic here :)

Choose a reason for hiding this comment

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

That would certainly work (we'd need to rename the test to be more generic), I just thought that this is such a nice and small piece of functionality that a unit test is a better fit. In the end, both variants are reasonable.

lldb/source/Symbol/SwiftASTContext.cpp Outdated Show resolved Hide resolved
@DeeSee DeeSee force-pushed the refactor_clang_extra_flags branch 3 times, most recently from ec0ec92 to 13caaf9 Compare July 13, 2020 22:08
Copy link

@adrian-prantl adrian-prantl left a comment

Choose a reason for hiding this comment

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

LGTM with inline comments addressed. Please do not forget to cherry-pick this to master-next!

lldb/source/Symbol/SwiftASTContext.cpp Outdated Show resolved Hide resolved
lldb/source/Symbol/SwiftASTContext.cpp Outdated Show resolved Hide resolved
lldb/source/Symbol/SwiftASTContext.cpp Outdated Show resolved Hide resolved
lldb/source/Symbol/SwiftASTContext.cpp Outdated Show resolved Hide resolved
lldb/source/Symbol/SwiftASTContext.cpp Outdated Show resolved Hide resolved
lldb/source/Symbol/SwiftASTContext.cpp Outdated Show resolved Hide resolved
@DeeSee DeeSee force-pushed the refactor_clang_extra_flags branch from 13caaf9 to 3ed20f1 Compare July 14, 2020 07:51
@adrian-prantl
Copy link

@swift-ci test

@DeeSee DeeSee force-pushed the refactor_clang_extra_flags branch 2 times, most recently from 60fc04a to 02cb77e Compare July 14, 2020 21:59
@DeeSee
Copy link
Author

DeeSee commented Jul 14, 2020

@swift-ci test


std::array<StringRef> macro_flags = { "-D", "-U" };

bool IsMultiArgClangFlag(const StringRef& arg) {

Choose a reason for hiding this comment

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

Please use StringRef instead. StringRef only has a size of two pointers; it's designed to be passed by value.


bool IsMacroDefinition(const StringRef& arg) {
for (auto &flag : macro_flags)
if (args.starswith(flag))

Choose a reason for hiding this comment

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

startswith

@DeeSee DeeSee force-pushed the refactor_clang_extra_flags branch from 02cb77e to 1fed12a Compare July 15, 2020 07:26
@DeeSee
Copy link
Author

DeeSee commented Jul 15, 2020

@swift-ci test

1 similar comment
@adrian-prantl
Copy link

@swift-ci test

@DeeSee DeeSee force-pushed the refactor_clang_extra_flags branch 2 times, most recently from ecde483 to fbbd0ef Compare July 15, 2020 17:05
@adrian-prantl
Copy link

@swift-ci test

@adrian-prantl
Copy link

/Users/buildnode/jenkins/workspace/apple-llvm-project-pr-macos/llvm-project/lldb/source/Symbol/SwiftASTContext.cpp:1452:6: error: too few template arguments for class template 'array'
12:28:15 std::array<StringRef> macro_flags = { "-D", "-U" };
12:28:15      ^

Did you build this prior to uploading?

@DeeSee
Copy link
Author

DeeSee commented Jul 15, 2020

Oh, sorry for that, I was too impatient for the last pushes, and incremental build doesn't work on my local checkout, so I have to wait about an hour to check compilation :( I'll make final push afther checking

@DeeSee DeeSee force-pushed the refactor_clang_extra_flags branch from fbbd0ef to 155380d Compare July 16, 2020 00:12
@adrian-prantl
Copy link

@swift-ci test

@adrian-prantl
Copy link

@DeeSee Would you mind doing a git pull --rebase to incorporate #1468 ?

@DeeSee
Copy link
Author

DeeSee commented Jul 16, 2020

Sure!

@DeeSee DeeSee force-pushed the refactor_clang_extra_flags branch from 155380d to 5484364 Compare July 16, 2020 19:49
@adrian-prantl
Copy link

@swift-ci test

@adrian-prantl
Copy link

Thanks!

@adrian-prantl adrian-prantl merged commit 6df337c into swiftlang:swift/master Jul 17, 2020
@DeeSee
Copy link
Author

DeeSee commented Jul 17, 2020

LGTM with inline comments addressed. Please do not forget to cherry-pick this to master-next!

Seems like you've made cherry-pick yourself, thanks!

@adrian-prantl
Copy link

@DeeSee It looks like this patch introduced a use-after-free. Would you mind taking a look? https://ci.swift.org/view/LLDB/job/oss-lldb-asan-osx/6947/consoleText

@adrian-prantl
Copy link

==92179==ERROR: AddressSanitizer: heap-use-after-free on address 0x6040004bee11 at pc 0x000104e6d2bc bp 0x7ffeeadb64d0 sp 0x7ffeeadb5c78
READ of size 12 at 0x6040004bee11 thread T0
    #0 0x104e6d2bb in MemcmpInterceptorCommon(void*, int (*)(void const*, void const*, unsigned long), void const*, void const*, unsigned long)+0x29b (libclang_rt.asan_osx_dynamic.dylib:x86_64+0x192bb)
    #1 0x104e6d67a in wrap_memcmp+0x6a (libclang_rt.asan_osx_dynamic.dylib:x86_64+0x1967a)
    #2 0x113907fab in bool llvm::DenseMapBase<llvm::DenseMap<llvm::StringRef, llvm::detail::DenseSetEmpty, llvm::DenseMapInfo<llvm::StringRef>, llvm::detail::DenseSetPair<llvm::StringRef> >, llvm::StringRef, llvm::detail::DenseSetEmpty, llvm::DenseMapInfo<llvm::StringRef>, llvm::detail::DenseSetPair<llvm::StringRef> >::LookupBucketFor<llvm::StringRef>(llvm::StringRef const&, llvm::detail::DenseSetPair<llvm::StringRef> const*&) const DenseMap.h:601
    #3 0x113908d11 in std::__1::pair<llvm::DenseMapIterator<llvm::StringRef, llvm::detail::DenseSetEmpty, llvm::DenseMapInfo<llvm::StringRef>, llvm::detail::DenseSetPair<llvm::StringRef>, false>, bool> llvm::DenseMapBase<llvm::DenseMap<llvm::StringRef, llvm::detail::DenseSetEmpty, llvm::DenseMapInfo<llvm::StringRef>, llvm::detail::DenseSetPair<llvm::StringRef> >, llvm::StringRef, llvm::detail::DenseSetEmpty, llvm::DenseMapInfo<llvm::StringRef>, llvm::detail::DenseSetPair<llvm::StringRef> >::try_emplace<llvm::detail::DenseSetEmpty&>(llvm::StringRef const&, llvm::detail::DenseSetEmpty&) DenseMap.h:231
    #4 0x11387db74 in llvm::detail::DenseSetImpl<llvm::StringRef, llvm::DenseMap<llvm::StringRef, llvm::detail::DenseSetEmpty, llvm::DenseMapInfo<llvm::StringRef>, llvm::detail::DenseSetPair<llvm::StringRef> >, llvm::DenseMapInfo<llvm::StringRef> >::insert(llvm::StringRef const&) DenseSet.h:189
    #5 0x11387c49d in lldb_private::SwiftASTContext::AddExtraClangArgs(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >) SwiftASTContext.cpp:1509
    #6 0x11387e17c in lldb_private::SwiftASTContext::AddUserClangArgs(lldb_private::TargetProperties&) SwiftASTContext.cpp:1519
    #7 0x113890766 in lldb_private::SwiftASTContext::CreateInstance(lldb::LanguageType, lldb_private::Target&, char const*) SwiftASTContext.cpp:2221
    #8 0x113893479 in CreateTypeSystemInstance(lldb::LanguageType, lldb_private::Module*, lldb_private::Target*, char const*) SwiftASTContext.cpp:2299
    #9 0x1127b74e3 in CreateInstanceHelper(lldb::LanguageType, lldb_private::Module*, lldb_private::Target*, char const*) TypeSystem.cpp:57
    #10 0x1127bbeba in lldb_private::TypeSystemMap::GetTypeSystemForLanguage(lldb::LanguageType, lldb_private::Target*, bool, char const*) TypeSystem.cpp:376
    #11 0x112a0b81e in lldb_private::Target::GetScratchTypeSystemForLanguage(lldb::LanguageType, bool, char const*) Target.cpp:2201
    #12 0x112a108f8 in lldb_private::Target::GetScratchSwiftASTContext(lldb_private::Status&, lldb_private::ExecutionContextScope&, bool) Target.cpp:2489
    #13 0x11243fafc in lldb_private::ValueObject::GetScratchSwiftASTContext() ValueObject.cpp:1707
    #14 0x1129a677d in lldb_private::SwiftLanguageRuntimeImpl::GetMemberVariableOffset(lldb_private::CompilerType, lldb_private::ValueObject*, lldb_private::ConstString, lldb_private::Status*) SwiftLanguageRuntimeDynamicTypeResolution.cpp:642
    #15 0x1129221c4 in lldb_private::SwiftLanguageRuntime::GetMemberVariableOffset(lldb_private::CompilerType, lldb_private::ValueObject*, lldb_private::ConstString, lldb_private::Status*) SwiftLanguageRuntime.cpp:2050
    #16 0x1138bdb86 in GetInstanceVariableOffset(lldb_private::ValueObject*, lldb_private::ExecutionContext*, lldb_private::CompilerType const&, llvm::StringRef, lldb_private::CompilerType const&) SwiftASTContext.cpp:6866
    #17 0x1138bbc31 in lldb_private::SwiftASTContext::GetChildCompilerTypeAtIndex(void*, lldb_private::ExecutionContext*, unsigned long, bool, bool, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, unsigned int&, int&, unsigned int&, unsigned int&, bool&, bool&, lldb_private::ValueObject*, unsigned long long&) SwiftASTContext.cpp:7093
    #18 0x113823566 in lldb_private::TypeSystemSwiftTypeRef::GetChildCompilerTypeAtIndex(void*, lldb_private::ExecutionContext*, unsigned long, bool, bool, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, unsigned int&, int&, unsigned int&, unsigned int&, bool&, bool&, lldb_private::ValueObject*, unsigned long long&) TypeSystemSwiftTypeRef.cpp:1594
    #19 0x11270f488 in lldb_private::CompilerType::GetChildCompilerTypeAtIndex(lldb_private::ExecutionContext*, unsigned long, bool, bool, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, unsigned int&, int&, unsigned int&, unsigned int&, bool&, bool&, lldb_private::ValueObject*, unsigned long long&) const CompilerType.cpp:626
    #20 0x1124354c9 in lldb_private::ValueObject::CreateChildAtIndex(unsigned long, bool, int) ValueObject.cpp:698
    #21 0x112432cb3 in lldb_private::ValueObject::GetChildAtIndex(unsigned long, bool) ValueObject.cpp:520
    #22 0x1124eaf3d in lldb_private::ValueObjectPrinter::GenerateChild(lldb_private::ValueObject*, unsigned long) ValueObjectPrinter.cpp:676
    #23 0x1124eb1c7 in lldb_private::ValueObjectPrinter::PrintChildren(bool, bool, lldb_private::DumpValueObjectOptions::PointerDepth const&) ValueObjectPrinter.cpp:691
    #24 0x1124e6f70 in lldb_private::ValueObjectPrinter::PrintChildrenIfNeeded(bool, bool) ValueObjectPrinter.cpp:804
    #25 0x1124e4112 in lldb_private::ValueObjectPrinter::PrintValueObject() ValueObjectPrinter.cpp:91
    #26 0x11244cdf7 in lldb_private::ValueObject::Dump(lldb_private::Stream&, lldb_private::DumpValueObjectOptions const&) ValueObject.cpp:2794
    #27 0x113c6d2d2 in CommandObjectFrameVariable::DoExecute(lldb_private::Args&, lldb_private::CommandReturnObject&) CommandObjectFrame.cpp:605
    #28 0x112650d69 in lldb_private::CommandObjectParsed::Execute(char const*, lldb_private::CommandReturnObject&) CommandObject.cpp:981
    #29 0x11262e0d9 in lldb_private::CommandInterpreter::HandleCommand(char const*, lldb_private::LazyBool, lldb_private::CommandReturnObject&, lldb_private::ExecutionContext*, bool, bool) CommandInterpreter.cpp:1782
    #30 0x11183d3d9 in lldb::SBCommandInterpreter::HandleCommand(char const*, lldb::SBExecutionContext&, lldb::SBCommandReturnObject&, bool) SBCommandInterpreter.cpp:281
    #31 0x11183c6dc in lldb::SBCommandInterpreter::HandleCommand(char const*, lldb::SBCommandReturnObject&, bool) SBCommandInterpreter.cpp:259
    #32 0x111f7e3ac in _wrap_SBCommandInterpreter_HandleCommand(_object*, _object*) LLDBWrapPython.cpp:14780
    #33 0x7fff43ddc321 in PyEval_EvalFrameEx+0x512a (Python:x86_64+0x86321)
    #34 0x7fff43dd6beb in PyEval_EvalCodeEx+0x212 (Python:x86_64+0x80beb)
    #35 0x7fff43de06e9  (Python:x86_64+0x8a6e9)
    #36 0x7fff43ddbaff in PyEval_EvalFrameEx+0x4908 (Python:x86_64+0x85aff)
    #37 0x7fff43dd6beb in PyEval_EvalCodeEx+0x212 (Python:x86_64+0x80beb)
    #38 0x7fff43de06e9  (Python:x86_64+0x8a6e9)
    #39 0x7fff43ddbaff in PyEval_EvalFrameEx+0x4908 (Python:x86_64+0x85aff)
    #40 0x7fff43dd6beb in PyEval_EvalCodeEx+0x212 (Python:x86_64+0x80beb)
    #41 0x7fff43de06e9  (Python:x86_64+0x8a6e9)
    #42 0x7fff43ddbaff in PyEval_EvalFrameEx+0x4908 (Python:x86_64+0x85aff)
    #43 0x7fff43dd6beb in PyEval_EvalCodeEx+0x212 (Python:x86_64+0x80beb)
    #44 0x7fff43d7daa7  (Python:x86_64+0x27aa7)
    #45 0x7fff43d60142 in PyObject_Call+0x60 (Python:x86_64+0xa142)
    #46 0x7fff43ddc275 in PyEval_EvalFrameEx+0x507e (Python:x86_64+0x86275)
    #47 0x7fff43dd6beb in PyEval_EvalCodeEx+0x212 (Python:x86_64+0x80beb)
    #48 0x7fff43d7daa7  (Python:x86_64+0x27aa7)
    #49 0x7fff43d60142 in PyObject_Call+0x60 (Python:x86_64+0xa142)
    #50 0x7fff43ddc275 in PyEval_EvalFrameEx+0x507e (Python:x86_64+0x86275)
    #51 0x7fff43dd6beb in PyEval_EvalCodeEx+0x212 (Python:x86_64+0x80beb)
    #52 0x7fff43de06e9  (Python:x86_64+0x8a6e9)
    #53 0x7fff43ddbaff in PyEval_EvalFrameEx+0x4908 (Python:x86_64+0x85aff)
    #54 0x7fff43dd6beb in PyEval_EvalCodeEx+0x212 (Python:x86_64+0x80beb)
    #55 0x7fff43de06e9  (Python:x86_64+0x8a6e9)
    #56 0x7fff43ddbaff in PyEval_EvalFrameEx+0x4908 (Python:x86_64+0x85aff)
    #57 0x7fff43de068e  (Python:x86_64+0x8a68e)
    #58 0x7fff43ddbaff in PyEval_EvalFrameEx+0x4908 (Python:x86_64+0x85aff)
    #59 0x7fff43dd6beb in PyEval_EvalCodeEx+0x212 (Python:x86_64+0x80beb)
    #60 0x7fff43d7daa7  (Python:x86_64+0x27aa7)
    #61 0x7fff43d60142 in PyObject_Call+0x60 (Python:x86_64+0xa142)
    #62 0x7fff43ddc275 in PyEval_EvalFrameEx+0x507e (Python:x86_64+0x86275)
    #63 0x7fff43dd6beb in PyEval_EvalCodeEx+0x212 (Python:x86_64+0x80beb)
    #64 0x7fff43d7daa7  (Python:x86_64+0x27aa7)
    #65 0x7fff43d60142 in PyObject_Call+0x60 (Python:x86_64+0xa142)
    #66 0x7fff43d6a8f3  (Python:x86_64+0x148f3)
    #67 0x7fff43d60142 in PyObject_Call+0x60 (Python:x86_64+0xa142)
    #68 0x7fff43da5a1b  (Python:x86_64+0x4fa1b)
    #69 0x7fff43d60142 in PyObject_Call+0x60 (Python:x86_64+0xa142)
    #70 0x7fff43ddbbac in PyEval_EvalFrameEx+0x49b5 (Python:x86_64+0x85bac)
    #71 0x7fff43dd6beb in PyEval_EvalCodeEx+0x212 (Python:x86_64+0x80beb)
    #72 0x7fff43de06e9  (Python:x86_64+0x8a6e9)
    #73 0x7fff43ddbaff in PyEval_EvalFrameEx+0x4908 (Python:x86_64+0x85aff)
    #74 0x7fff43dd6beb in PyEval_EvalCodeEx+0x212 (Python:x86_64+0x80beb)
    #75 0x7fff43de06e9  (Python:x86_64+0x8a6e9)
    #76 0x7fff43ddbaff in PyEval_EvalFrameEx+0x4908 (Python:x86_64+0x85aff)
    #77 0x7fff43dd6beb in PyEval_EvalCodeEx+0x212 (Python:x86_64+0x80beb)
    #78 0x7fff43d7daa7  (Python:x86_64+0x27aa7)
    #79 0x7fff43d60142 in PyObject_Call+0x60 (Python:x86_64+0xa142)
    #80 0x7fff43ddc275 in PyEval_EvalFrameEx+0x507e (Python:x86_64+0x86275)
    #81 0x7fff43dd6beb in PyEval_EvalCodeEx+0x212 (Python:x86_64+0x80beb)
    #82 0x7fff43d7daa7  (Python:x86_64+0x27aa7)
    #83 0x7fff43d60142 in PyObject_Call+0x60 (Python:x86_64+0xa142)
    #84 0x7fff43d6a8f3  (Python:x86_64+0x148f3)
    #85 0x7fff43d60142 in PyObject_Call+0x60 (Python:x86_64+0xa142)
    #86 0x7fff43da5a1b  (Python:x86_64+0x4fa1b)
    #87 0x7fff43d60142 in PyObject_Call+0x60 (Python:x86_64+0xa142)
    #88 0x7fff43ddbbac in PyEval_EvalFrameEx+0x49b5 (Python:x86_64+0x85bac)
    #89 0x7fff43de068e  (Python:x86_64+0x8a68e)
    #90 0x7fff43ddbaff in PyEval_EvalFrameEx+0x4908 (Python:x86_64+0x85aff)
    #91 0x7fff43dd6beb in PyEval_EvalCodeEx+0x212 (Python:x86_64+0x80beb)
    #92 0x7fff43de06e9  (Python:x86_64+0x8a6e9)
    #93 0x7fff43ddbaff in PyEval_EvalFrameEx+0x4908 (Python:x86_64+0x85aff)
    #94 0x7fff43dd6beb in PyEval_EvalCodeEx+0x212 (Python:x86_64+0x80beb)
    #95 0x7fff43dd69d2 in PyEval_EvalCode+0x1f (Python:x86_64+0x809d2)
    #96 0x7fff43df89b5  (Python:x86_64+0xa29b5)
    #97 0x7fff43df8a5c in PyRun_FileExFlags+0x81 (Python:x86_64+0xa2a5c)
    #98 0x7fff43df85e3 in PyRun_SimpleFileExFlags+0x2cd (Python:x86_64+0xa25e3)
    #99 0x7fff43e0a31a in Py_Main+0xc92 (Python:x86_64+0xb431a)
    #100 0x7fff72c02cc8 in start+0x0 (libdyld.dylib:x86_64+0x1acc8)

0x6040004bee11 is located 1 bytes inside of 48-byte region [0x6040004bee10,0x6040004bee40)
freed by thread T0 here:
    #0 0x104ea4fdd in wrap__ZdlPv+0x7d (libclang_rt.asan_osx_dynamic.dylib:x86_64+0x50fdd)
    #1 0x11387d975 in std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >::push_back(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&&) vector:1655
    #2 0x11387c424 in lldb_private::SwiftASTContext::AddExtraClangArgs(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >) SwiftASTContext.cpp:1508
    #3 0x11387e17c in lldb_private::SwiftASTContext::AddUserClangArgs(lldb_private::TargetProperties&) SwiftASTContext.cpp:1519
    #4 0x113890766 in lldb_private::SwiftASTContext::CreateInstance(lldb::LanguageType, lldb_private::Target&, char const*) SwiftASTContext.cpp:2221
    #5 0x113893479 in CreateTypeSystemInstance(lldb::LanguageType, lldb_private::Module*, lldb_private::Target*, char const*) SwiftASTContext.cpp:2299
    #6 0x1127b74e3 in CreateInstanceHelper(lldb::LanguageType, lldb_private::Module*, lldb_private::Target*, char const*) TypeSystem.cpp:57
    #7 0x1127bbeba in lldb_private::TypeSystemMap::GetTypeSystemForLanguage(lldb::LanguageType, lldb_private::Target*, bool, char const*) TypeSystem.cpp:376
    #8 0x112a0b81e in lldb_private::Target::GetScratchTypeSystemForLanguage(lldb::LanguageType, bool, char const*) Target.cpp:2201
    #9 0x112a108f8 in lldb_private::Target::GetScratchSwiftASTContext(lldb_private::Status&, lldb_private::ExecutionContextScope&, bool) Target.cpp:2489
    #10 0x11243fafc in lldb_private::ValueObject::GetScratchSwiftASTContext() ValueObject.cpp:1707
    #11 0x1129a677d in lldb_private::SwiftLanguageRuntimeImpl::GetMemberVariableOffset(lldb_private::CompilerType, lldb_private::ValueObject*, lldb_private::ConstString, lldb_private::Status*) SwiftLanguageRuntimeDynamicTypeResolution.cpp:642
    #12 0x1129221c4 in lldb_private::SwiftLanguageRuntime::GetMemberVariableOffset(lldb_private::CompilerType, lldb_private::ValueObject*, lldb_private::ConstString, lldb_private::Status*) SwiftLanguageRuntime.cpp:2050
    #13 0x1138bdb86 in GetInstanceVariableOffset(lldb_private::ValueObject*, lldb_private::ExecutionContext*, lldb_private::CompilerType const&, llvm::StringRef, lldb_private::CompilerType const&) SwiftASTContext.cpp:6866
    #14 0x1138bbc31 in lldb_private::SwiftASTContext::GetChildCompilerTypeAtIndex(void*, lldb_private::ExecutionContext*, unsigned long, bool, bool, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, unsigned int&, int&, unsigned int&, unsigned int&, bool&, bool&, lldb_private::ValueObject*, unsigned long long&) SwiftASTContext.cpp:7093
    #15 0x113823566 in lldb_private::TypeSystemSwiftTypeRef::GetChildCompilerTypeAtIndex(void*, lldb_private::ExecutionContext*, unsigned long, bool, bool, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, unsigned int&, int&, unsigned int&, unsigned int&, bool&, bool&, lldb_private::ValueObject*, unsigned long long&) TypeSystemSwiftTypeRef.cpp:1594
    #16 0x11270f488 in lldb_private::CompilerType::GetChildCompilerTypeAtIndex(lldb_private::ExecutionContext*, unsigned long, bool, bool, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, unsigned int&, int&, unsigned int&, unsigned int&, bool&, bool&, lldb_private::ValueObject*, unsigned long long&) const CompilerType.cpp:626
    #17 0x1124354c9 in lldb_private::ValueObject::CreateChildAtIndex(unsigned long, bool, int) ValueObject.cpp:698
    #18 0x112432cb3 in lldb_private::ValueObject::GetChildAtIndex(unsigned long, bool) ValueObject.cpp:520
    #19 0x1124eaf3d in lldb_private::ValueObjectPrinter::GenerateChild(lldb_private::ValueObject*, unsigned long) ValueObjectPrinter.cpp:676
    #20 0x1124eb1c7 in lldb_private::ValueObjectPrinter::PrintChildren(bool, bool, lldb_private::DumpValueObjectOptions::PointerDepth const&) ValueObjectPrinter.cpp:691
    #21 0x1124e6f70 in lldb_private::ValueObjectPrinter::PrintChildrenIfNeeded(bool, bool) ValueObjectPrinter.cpp:804
    #22 0x1124e4112 in lldb_private::ValueObjectPrinter::PrintValueObject() ValueObjectPrinter.cpp:91
    #23 0x11244cdf7 in lldb_private::ValueObject::Dump(lldb_private::Stream&, lldb_private::DumpValueObjectOptions const&) ValueObject.cpp:2794
    #24 0x113c6d2d2 in CommandObjectFrameVariable::DoExecute(lldb_private::Args&, lldb_private::CommandReturnObject&) CommandObjectFrame.cpp:605
    #25 0x112650d69 in lldb_private::CommandObjectParsed::Execute(char const*, lldb_private::CommandReturnObject&) CommandObject.cpp:981
    #26 0x11262e0d9 in lldb_private::CommandInterpreter::HandleCommand(char const*, lldb_private::LazyBool, lldb_private::CommandReturnObject&, lldb_private::ExecutionContext*, bool, bool) CommandInterpreter.cpp:1782
    #27 0x11183d3d9 in lldb::SBCommandInterpreter::HandleCommand(char const*, lldb::SBExecutionContext&, lldb::SBCommandReturnObject&, bool) SBCommandInterpreter.cpp:281
    #28 0x11183c6dc in lldb::SBCommandInterpreter::HandleCommand(char const*, lldb::SBCommandReturnObject&, bool) SBCommandInterpreter.cpp:259
    #29 0x111f7e3ac in _wrap_SBCommandInterpreter_HandleCommand(_object*, _object*) LLDBWrapPython.cpp:14780

previously allocated by thread T0 here:
    #0 0x104ea4bbd in wrap__Znwm+0x7d (libclang_rt.asan_osx_dynamic.dylib:x86_64+0x50bbd)
    #1 0x1117d8ac7 in std::__1::__split_buffer<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >&) __split_buffer:318
    #2 0x11387d8cd in std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >::push_back(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&&) vector:1655
    #3 0x11387c424 in lldb_private::SwiftASTContext::AddExtraClangArgs(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >) SwiftASTContext.cpp:1508
    #4 0x11390c844 in std::__1::__function::__func<lldb_private::SwiftASTContext::CreateInstance(lldb::LanguageType, lldb_private::Target&, char const*)::$_4, std::__1::allocator<lldb_private::SwiftASTContext::CreateInstance(lldb::LanguageType, lldb_private::Target&, char const*)::$_4>, void (std::__1::shared_ptr<lldb_private::Module>&&)>::operator()(std::__1::shared_ptr<lldb_private::Module>&&) functional:1724
    #5 0x1138903c4 in lldb_private::SwiftASTContext::CreateInstance(lldb::LanguageType, lldb_private::Target&, char const*) SwiftASTContext.cpp:2203
    #6 0x113893479 in CreateTypeSystemInstance(lldb::LanguageType, lldb_private::Module*, lldb_private::Target*, char const*) SwiftASTContext.cpp:2299
    #7 0x1127b74e3 in CreateInstanceHelper(lldb::LanguageType, lldb_private::Module*, lldb_private::Target*, char const*) TypeSystem.cpp:57
    #8 0x1127bbeba in lldb_private::TypeSystemMap::GetTypeSystemForLanguage(lldb::LanguageType, lldb_private::Target*, bool, char const*) TypeSystem.cpp:376
    #9 0x112a0b81e in lldb_private::Target::GetScratchTypeSystemForLanguage(lldb::LanguageType, bool, char const*) Target.cpp:2201
    #10 0x112a108f8 in lldb_private::Target::GetScratchSwiftASTContext(lldb_private::Status&, lldb_private::ExecutionContextScope&, bool) Target.cpp:2489
    #11 0x11243fafc in lldb_private::ValueObject::GetScratchSwiftASTContext() ValueObject.cpp:1707
    #12 0x1129a677d in lldb_private::SwiftLanguageRuntimeImpl::GetMemberVariableOffset(lldb_private::CompilerType, lldb_private::ValueObject*, lldb_private::ConstString, lldb_private::Status*) SwiftLanguageRuntimeDynamicTypeResolution.cpp:642
    #13 0x1129221c4 in lldb_private::SwiftLanguageRuntime::GetMemberVariableOffset(lldb_private::CompilerType, lldb_private::ValueObject*, lldb_private::ConstString, lldb_private::Status*) SwiftLanguageRuntime.cpp:2050
    #14 0x1138bdb86 in GetInstanceVariableOffset(lldb_private::ValueObject*, lldb_private::ExecutionContext*, lldb_private::CompilerType const&, llvm::StringRef, lldb_private::CompilerType const&) SwiftASTContext.cpp:6866
    #15 0x1138bbc31 in lldb_private::SwiftASTContext::GetChildCompilerTypeAtIndex(void*, lldb_private::ExecutionContext*, unsigned long, bool, bool, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, unsigned int&, int&, unsigned int&, unsigned int&, bool&, bool&, lldb_private::ValueObject*, unsigned long long&) SwiftASTContext.cpp:7093
    #16 0x113823566 in lldb_private::TypeSystemSwiftTypeRef::GetChildCompilerTypeAtIndex(void*, lldb_private::ExecutionContext*, unsigned long, bool, bool, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, unsigned int&, int&, unsigned int&, unsigned int&, bool&, bool&, lldb_private::ValueObject*, unsigned long long&) TypeSystemSwiftTypeRef.cpp:1594
    #17 0x11270f488 in lldb_private::CompilerType::GetChildCompilerTypeAtIndex(lldb_private::ExecutionContext*, unsigned long, bool, bool, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, unsigned int&, int&, unsigned int&, unsigned int&, bool&, bool&, lldb_private::ValueObject*, unsigned long long&) const CompilerType.cpp:626
    #18 0x1124354c9 in lldb_private::ValueObject::CreateChildAtIndex(unsigned long, bool, int) ValueObject.cpp:698
    #19 0x112432cb3 in lldb_private::ValueObject::GetChildAtIndex(unsigned long, bool) ValueObject.cpp:520
    #20 0x1124eaf3d in lldb_private::ValueObjectPrinter::GenerateChild(lldb_private::ValueObject*, unsigned long) ValueObjectPrinter.cpp:676
    #21 0x1124eb1c7 in lldb_private::ValueObjectPrinter::PrintChildren(bool, bool, lldb_private::DumpValueObjectOptions::PointerDepth const&) ValueObjectPrinter.cpp:691
    #22 0x1124e6f70 in lldb_private::ValueObjectPrinter::PrintChildrenIfNeeded(bool, bool) ValueObjectPrinter.cpp:804
    #23 0x1124e4112 in lldb_private::ValueObjectPrinter::PrintValueObject() ValueObjectPrinter.cpp:91
    #24 0x11244cdf7 in lldb_private::ValueObject::Dump(lldb_private::Stream&, lldb_private::DumpValueObjectOptions const&) ValueObject.cpp:2794
    #25 0x113c6d2d2 in CommandObjectFrameVariable::DoExecute(lldb_private::Args&, lldb_private::CommandReturnObject&) CommandObjectFrame.cpp:605
    #26 0x112650d69 in lldb_private::CommandObjectParsed::Execute(char const*, lldb_private::CommandReturnObject&) CommandObject.cpp:981
    #27 0x11262e0d9 in lldb_private::CommandInterpreter::HandleCommand(char const*, lldb_private::LazyBool, lldb_private::CommandReturnObject&, lldb_private::ExecutionContext*, bool, bool) CommandInterpreter.cpp:1782
    #28 0x11183d3d9 in lldb::SBCommandInterpreter::HandleCommand(char const*, lldb::SBExecutionContext&, lldb::SBCommandReturnObject&, bool) SBCommandInterpreter.cpp:281
    #29 0x11183c6dc in lldb::SBCommandInterpreter::HandleCommand(char const*, lldb::SBCommandReturnObject&, bool) SBCommandInterpreter.cpp:259

SUMMARY: AddressSanitizer: heap-use-after-free (libclang_rt.asan_osx_dynamic.dylib:x86_64+0x192bb) in MemcmpInterceptorCommon(void*, int (*)(void const*, void const*, unsigned long), void const*, void const*, unsigned long)+0x29b
Shadow bytes around the buggy address:
  0x1c0800097d70: fa fa fd fd fd fd fd fd fa fa fd fd fd fd fd fd
  0x1c0800097d80: fa fa fa fa fa fa fa fa fa fa fd fd fd fd fd fd
  0x1c0800097d90: fa fa 00 00 00 00 00 00 fa fa 00 00 00 00 00 00
  0x1c0800097da0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x1c0800097db0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
=>0x1c0800097dc0: fa fa[fd]fd fd fd fd fd fa fa fa fa fa fa fa fa
  0x1c0800097dd0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x1c0800097de0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x1c0800097df0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x1c0800097e00: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x1c0800097e10: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
  Shadow gap:              cc
==92179==ABORTING


auto clang_arg_str = clang_argument.str();
if (!ShouldUnique(clang_argument) || !unique_flags.count(clang_arg_str)) {
importer_options.ExtraArgs.push_back(clang_arg_str);
Copy link

Choose a reason for hiding this comment

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

@DeeSee doing importer_options.ExtraArgs.push_back invalidates all the references inside of llvm::DenseSet<StringRef> unique_flags. You might consider using llvm::StringSet.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks! I'll make a fix right away.

Just asking — why not std::unordered_set<std::string>? (I'm not very familiar with historical causes of why there are custom types for hashmap and hashset in llvm, and using types from std looks less error-prone to me)

Choose a reason for hiding this comment

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

One (historical) reason is that LLVM predates std::unordered_set. However, in this concrete case llvm::StringSet will be more efficient than std::unordered_set<std::string> because it allocates the strings in the same memory as the set, so you don't have to pay extra memory management overhead for allocating the contents of the individual std::strings.

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.

4 participants