This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Extending bugprone-signal-handler with POSIX functions.
ClosedPublic

Authored by balazske on Nov 5 2020, 7:13 AM.

Details

Summary

An option is added to the check to select wich set of functions is
defined as asynchronous-safe functions.

Diff Detail

Event Timeline

balazske created this revision.Nov 5 2020, 7:13 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 5 2020, 7:13 AM
balazske requested review of this revision.Nov 5 2020, 7:13 AM
Eugene.Zelenko added inline comments.
clang-tools-extra/docs/clang-tidy/checks/bugprone-signal-handler.rst
38

Please add newline.

Eugene.Zelenko added a project: Restricted Project.
balazske updated this revision to Diff 303356.Nov 6 2020, 12:59 AM

Small fixes.

One thing that's not clear to me in the patch is what the behavior should be when the user specifies that they want the POSIX set but they're compiling for a non-POSIX system like Windows. Do you have thoughts on that situation?

clang-tools-extra/clang-tidy/bugprone/SignalHandlerCheck.cpp
215

You should also mention here that the POSIX list is not a strict superset of the minimal list.

clang-tools-extra/clang-tidy/bugprone/SignalHandlerCheck.h
25

I don't think this needs to be public?

clang-tools-extra/docs/clang-tidy/checks/bugprone-signal-handler.rst
24–25

Huh, that's really strange that POSIX leaves quick_exit off the list. I'm going to reach out to someone from the Austin Group to see if that's intentional or not.

clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/signal.h
1

I think we should strive to replicate the system headers rather than fake up a system header (these headers can be used by more than one check). So I think we want to keep signal.h and stdlib.h, and should include the POSIX-specific functionality with a macro. This also helps us test the behavior on systems like Windows which are not POSIX systems.

clang-tools-extra/test/clang-tidy/checkers/bugprone-signal-handler-posix.c
5

I'd like to make sure we have a test for this that explicitly runs on Windows.

One thing that's not clear to me in the patch is what the behavior should be when the user specifies that they want the POSIX set but they're compiling for a non-POSIX system like Windows. Do you have thoughts on that situation?

I think that really it does not matter on what system the checker runs, at least from the check's point of view. The check itself is not dependent on the compilation target. If the compile target is not POSIX compliant but the flag is still set to POSIX it may indicate a problem but how to detect this? If we could detect this automatically the new option would not be needed, POSIX mode could be enabled automatically.
Because this issue maybe it is safer to have the minimal as the default option?

clang-tools-extra/clang-tidy/bugprone/SignalHandlerCheck.h
25

The getEnumMapping function uses it that is external to the class.

clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/signal.h
1

My concern was to add these many small header files with just some functions in them. For accept more than one header is needed if we want to exactly replicate the system files. And data types like size_t should have a common header too. So I decided to have one header that contains all system functions and data types. This can be used by multiple tests and extended as needed.

aaron.ballman added inline comments.Nov 10 2020, 8:37 AM
clang-tools-extra/clang-tidy/bugprone/SignalHandlerCheck.h
25

Ah, I hadn't caught that, thanks!

clang-tools-extra/docs/clang-tidy/checks/bugprone-signal-handler.rst
24–25

I didn't need to reach out to them because I realized the latest POSIX spec is still based on C99 and they've not released the updated POSIX spec yet.

I think the POSIX functions should be a superset of the C functions.

clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/signal.h
1

I don't think we're too worried about having a bunch of small test headers around -- I think it's more important the headers used to check system header behavior be understandable as to what you're getting from them. For instance, there are clang-tidy checks for llvm-libc that may have very different needs from what you're doing here.

What's more, these changes break existing tests -- I don't see any companion changes to fix those up.

balazske added inline comments.Nov 17 2020, 2:11 AM
clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/signal.h
1

On my system (Ubuntu) I do not see failing tests, and these changes do not touch files that were created before D87449, no new problems should happen. I am not against "mirroring" the POSIX API header structure into the test stub header structure, only do not like the overhead of adding these many files with just 1-2 lines (that is needed for this test) in them. It is not done this way in the clang tests either (there are multiple "system-header-simulator" files that contain every declaration usable for specific purposes at one or more tests). Also I want to have the opinion of another reviewer for this question.

aaron.ballman added inline comments.Nov 17 2020, 5:31 AM
clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/signal.h
1

On my system (Ubuntu) I do not see failing tests, and these changes do not touch files that were created before D87449, no new problems should happen.

You renamed stdlib.h and lllvmlibc-restrict-system-libc-headers.cpp includes stdlib.h, but after closer inspection, I see now that it's including one from a different directory. So this doesn't break the things I thought it was breaking, good!

Also I want to have the opinion of another reviewer for this question.

I'm happy to go with whatever @alexfh thinks.

balazske added inline comments.Feb 3 2021, 12:55 AM
clang-tools-extra/docs/clang-tidy/checks/bugprone-signal-handler.rst
24–25

Do you mean that we should add quick_exit to the POSIX set? And have a requirement that minimal set is subset of the POSIX set (if the lists are modified in the future)?

alexfh added inline comments.Feb 3 2021, 4:25 PM
clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/signal.h
1

Based on the experience with our internal set of checks and tests for them, I tend to think that keeping mock API definitions arranged in a similar way to the corresponding real library is a more sustainable way to manage mock headers. When arranged this way, it's easier to find the origins of, verify, and update definitions, when, for example, a higher fidelity replication of a system/STL/... entity becomes necessary. Dependencies between these mock headers (if necessary) can repeat those in the real library, and #includes of these mock headers can be composed in a similar way. Though the structure of the mock headers can start be approximate and the level of detail can be added when necessary. For example, one can start with a mock header for <string> that would contain initializer_list implementation. But when another test would need <initializer_list>, the corresponding entities can be moved to a separate initializer_list header.

As for different header contents depending on target platform, preprocessor macros seem to be a better way to handle this than using separate mock headers.

The set of mock headers can be shared by all tests that examine the corresponding API. I don't see good reasons to keep different mocks of the same API for different tests.

balazske added inline comments.Feb 5 2021, 8:20 AM
clang-tools-extra/test/clang-tidy/checkers/bugprone-signal-handler-posix.c
5

If an extra test file only for windows, can you tell what additionally to test for in that file? I think the current tests run an all platforms, to add a new test there should be additional cases that are Windows-specific. The header files should come only from the test directory (same for all platforms), not from the real system includes.

balazske updated this revision to Diff 321766.Feb 5 2021, 8:22 AM

Re-organized testing header files.

aaron.ballman added inline comments.Feb 8 2021, 7:45 AM
clang-tools-extra/docs/clang-tidy/checks/bugprone-signal-handler.rst
24–25

Do you mean that we should add quick_exit to the POSIX set? And have a requirement that minimal set is subset of the POSIX set (if the lists are modified in the future)?

Sorry for having missed your question earlier!

I think any function in the minimally safe set should also be safe within the POSIX set unless there's some documentation to suggest that POSIX removed an allowance the C standard provides for a given API (which would be pretty surprising). So yes, I think the minimal set should be a subset of the POSIX set.

balazske updated this revision to Diff 322362.Feb 9 2021, 5:57 AM

Added quick_exit to POSIX-allowed set.

balazske updated this revision to Diff 322364.Feb 9 2021, 5:57 AM

Update relative to previous commit.

aaron.ballman added inline comments.Feb 10 2021, 7:53 AM
clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/string.h
12

I think this is causing some test failures: https://reviews.llvm.org/harbormaster/unit/view/324847/

A better way to do this would be: typedef __typeof__(sizeof(0)) size_t;

balazske updated this revision to Diff 323340.Feb 12 2021, 8:15 AM

Changed definition of size_t in test header.

This revision is now accepted and ready to land.Feb 16 2021, 6:47 AM
balazske updated this revision to Diff 325453.Feb 22 2021, 8:24 AM

Rebase, improved documentation.