This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Finds uses of OSRead* calls on macOS that may mask unexpected behavior due to unaligned reads
Needs ReviewPublic

Authored by dmaclach on Sep 20 2019, 2:40 PM.

Details

Reviewers
gribozavr
Summary

We ran into a problem in protocol buffers recently when compiling with Xcode 11 that caused crashes on 32 bit ARM architectures due to undefined behavior caused by OSRead* calls in OSByteOrder.h when reading unaligned addresses.

These potential errors can easily be fixed by replacing the OSRead* call with a memcpy and then a OSSwap{Big|Little}ToHostInt{16|32|64}.

Diff Detail

Event Timeline

dmaclach created this revision.Sep 20 2019, 2:40 PM
Eugene.Zelenko added inline comments.
clang-tidy/objc/AvoidOSReadCheck.cpp
22

Please add check if language is Objective-C.

docs/ReleaseNotes.rst
103

Please enclose OSRead* in double back-ticks.

docs/clang-tidy/checks/objc-avoid-osread.rst
7

Please synchronize with sentence in Release Notes.

30

Please separate with empty line.

Eugene.Zelenko added inline comments.Sep 20 2019, 5:10 PM
clang-tidy/objc/AvoidOSReadCheck.h
6

License was changed this year. Same in source file.

dmaclach updated this revision to Diff 221420.Sep 23 2019, 3:07 PM

Fixed up review comments.

dmaclach marked 5 inline comments as done.Sep 23 2019, 3:10 PM
dmaclach added inline comments.
clang-tidy/objc/AvoidOSReadCheck.cpp
22

So this isn't specific to Objective C, and applies to all C based languages. This is more Darwin specific. I based this CL on the AvoidSpinlockCheck which is very similar in this respect. If we want to do clean up on this later we should probably do them together.

docs/clang-tidy/checks/objc-avoid-osread.rst
7

Done. I coped this version into the release notes.

Eugene.Zelenko added inline comments.Sep 23 2019, 3:31 PM
docs/clang-tidy/checks/objc-avoid-osread.rst
27

Please highlight OSRead* and libkern/OSByteOrder.h with double back-ticks. Please also make lines no more then 80 characters long.

dmaclach updated this revision to Diff 221433.Sep 23 2019, 4:25 PM

Updated based on review

dmaclach marked an inline comment as done.Sep 23 2019, 4:25 PM

Any other comments on this?

alexfh edited reviewers, added: gribozavr; removed: alexfh.Sep 26 2019, 2:07 AM

What is the expected contract of the functions that this checker flags? Are they supposed to perform unaligned reads correctly, and we have just an implementation bug in these functions, or is it the caller's fault if they pass an unaligned address?

clang-tidy/objc/ObjCTidyModule.cpp
30

Maybe a better place for this checker is the new "darwin" module? It is being added in https://reviews.llvm.org/D67567.

test/clang-tidy/objc-avoid-osread.m
5

Please add declarations for these functions. It is strange that this test even works without them...