diff options
author | Ilya Biryukov <ibiryukov@google.com> | 2023-10-23 14:29:00 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-23 14:29:00 +0200 |
commit | 324d1bb35aefec737ef381d98211f7771c4b0ab5 (patch) | |
tree | b59152850721faadf1a87a39694be05641a052a4 /clang/lib/Basic/SourceManager.cpp | |
parent | 89eeb4f03d86f1411c21c18744d8d80effdcaef8 (diff) | |
download | llvm-324d1bb35aefec737ef381d98211f7771c4b0ab5.zip llvm-324d1bb35aefec737ef381d98211f7771c4b0ab5.tar.gz llvm-324d1bb35aefec737ef381d98211f7771c4b0ab5.tar.bz2 |
[Clang] Report an error and crash on source location exhaustion in macros (#69908)
`createExpansionLocImpl` has an assert that checks if we ran out of
source locations. We have observed this happening on real code and in
release builds the assertion does not fire and the compiler just keeps
running indefinitely without giving any indication that something went
wrong.
Diagnose this problem and reliably crash to make sure the problem is
easy to detect.
I have also tried:
- returning invalid source locations,
- reporting sloc address space usage on error.
Both caused the compiler to run indefinitely. It would be nice to dig
further why that happens, but until then crashing seems like a better
alternative.
Diffstat (limited to 'clang/lib/Basic/SourceManager.cpp')
-rw-r--r-- | clang/lib/Basic/SourceManager.cpp | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp index c44ecac..d627c23 100644 --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -605,7 +605,7 @@ FileID SourceManager::createFileIDImpl(ContentCache &File, StringRef Filename, unsigned FileSize = File.getSize(); if (!(NextLocalOffset + FileSize + 1 > NextLocalOffset && NextLocalOffset + FileSize + 1 <= CurrentLoadedOffset)) { - Diag.Report(IncludePos, diag::err_include_too_large); + Diag.Report(IncludePos, diag::err_sloc_space_too_large); noteSLocAddressSpaceUsage(Diag); return FileID(); } @@ -663,10 +663,16 @@ SourceManager::createExpansionLocImpl(const ExpansionInfo &Info, return SourceLocation::getMacroLoc(LoadedOffset); } LocalSLocEntryTable.push_back(SLocEntry::get(NextLocalOffset, Info)); - // FIXME: Produce a proper diagnostic for this case. - assert(NextLocalOffset + Length + 1 > NextLocalOffset && - NextLocalOffset + Length + 1 <= CurrentLoadedOffset && - "Ran out of source locations!"); + if (NextLocalOffset + Length + 1 <= NextLocalOffset || + NextLocalOffset + Length + 1 > CurrentLoadedOffset) { + Diag.Report(SourceLocation(), diag::err_sloc_space_too_large); + // FIXME: call `noteSLocAddressSpaceUsage` to report details to users and + // use a source location from `Info` to point at an error. + // Currently, both cause Clang to run indefinitely, this needs to be fixed. + // FIXME: return an error instead of crashing. Returning invalid source + // locations causes compiler to run indefinitely. + llvm::report_fatal_error("ran out of source locations"); + } // See createFileID for that +1. NextLocalOffset += Length + 1; return SourceLocation::getMacroLoc(NextLocalOffset - (Length + 1)); |