Skip to content

The threadsafety.py addon now flags MT-Unsafe symbols and functions.#5086

Merged
danmar merged 10 commits into
cppcheck-opensource:mainfrom
andrew-aitchison:MT_unsafe
Jun 8, 2023
Merged

The threadsafety.py addon now flags MT-Unsafe symbols and functions.#5086
danmar merged 10 commits into
cppcheck-opensource:mainfrom
andrew-aitchison:MT_unsafe

Conversation

@andrew-aitchison
Copy link
Copy Markdown
Contributor

At least on linux, many man pages have an "Attributes" section which lists MT-Unsafe (multi-thread unsafe) symbols
in a standard format.

This pull request extends the existing addon threadsafety.py to warn when these symbols are used.
It includes the tool tools/MT-Unsafe.py to re-generate the list of symbols by reading the (troff) man pages,
and a cpp test file which uses some symbols which these man pages list as safe or unsafe.

Copy link
Copy Markdown
Collaborator

@danmar danmar left a comment

Choose a reason for hiding this comment

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

how is the test file supposed to be used and what is the expected output.

I suggest that you add a test execution in the .github/workflows/CI-unixish.yml file.

We have this testing in that file:

...
      # TODO: move to scriptcheck.yml so these are tested with all Python versions?
      - name: Test addons
        run: |
          ./cppcheck --addon=threadsafety addons/test/threadsafety
          ./cppcheck --addon=threadsafety --std=c++03 addons/test/threadsafety
...

I don't know it does not seem to enforce that the expected warnings are printed. Imho we should add --error-exitlevel=1 --inline-suppr to the commands and then in the test files such comments should be added where warnings are expected:

--cppcheck-suppress threadsafety-the-warning-id

Comment thread addons/threadsafety.py Outdated
Comment thread addons/threadsafety.py
@andrew-aitchison
Copy link
Copy Markdown
Contributor Author

how is the test file supposed to be used and what is the expected output.

I suggest that you add a test execution in the .github/workflows/CI-unixish.yml file.

We have this testing in that file:

...
      # TODO: move to scriptcheck.yml so these are tested with all Python versions?
      - name: Test addons
        run: |
          ./cppcheck --addon=threadsafety addons/test/threadsafety
          ./cppcheck --addon=threadsafety --std=c++03 addons/test/threadsafety
...

Since addons/test/threadsafety is a directory, the existing lines in CI-unixish.yml do exercise my new testfile.

I don't know it does not seem to enforce that the expected warnings are printed. Imho we should add --error-exitlevel=1 --inline-suppr to the commands and then in the test files such comments should be added where warnings are expected:

With --error-exitcode=1 that looks right - I haven't tried it in the CI yet.
Should I do the same for the other test files in addons/test/threadsafety ?

@danmar
Copy link
Copy Markdown
Collaborator

danmar commented May 25, 2023

With --error-exitcode=1 that looks right - I haven't tried it in the CI yet.
Should I do the same for the other test files in addons/test/threadsafety ?

yes please add --error-exitcode=1 for all the testfiles and add proper cppcheck-suppress comments in the files.

@andrew-aitchison
Copy link
Copy Markdown
Contributor Author

I have added --error-exitcode=1 --inline-suppr
and the inline comments to enable the suppressions in MT-Unsafe.cpp and local_static.cpp
but local_static_const.cpp behaves differently with and without --std=c++03 and did so without my changes,
I have no idea what changed in C++ before or after c++03 so would rather not touch it.
I forgot to squash the merge, so there are three commits, sorry.

I will look at your other suggestions and see whether I can implement them.

@danmar
Copy link
Copy Markdown
Collaborator

danmar commented May 25, 2023

I have no idea what changed in C++ before or after c++03 so would rather not touch it.

In C++11 and later, initialization of static objects is thread safe.

hmm so somehow we would like to test that there is no warning in local_static.cpp normally. Only if --std=c++03 we want to have warnings.

Comment thread addons/threadsafety.py Outdated
Comment thread addons/threadsafety.py Outdated
@danmar
Copy link
Copy Markdown
Collaborator

danmar commented May 27, 2023

Not sure what I did to close this PR. I definitely did not mean to do so.

good it was closed by mistake not by intention :-)

tools/MT-Unsafe.py generates a list of these MT-Unsafe symbols for this addon,
by reading the "Attributes" sections of man pages,

and addons/test/threadsafety/MT-Unsafe.cpp exercises the threadsafety.py addon.
…he number of arguments. Fix it.

Originally changed to appease quake8 line length warning.
   --error-exitcode=1 --inline-suppr
so that they are quiet if the result is as expected,
but the return code indicates if something unexpected happens.
with comments that cppcheck understands, so that it can be silent about them.
…at cppcheck understands, so that it can be silent about them.
…onst.cpp

    correctly test the threadsafety addon.
- they were copied over from the y2038 addon.
Move argument parsing after function definitions, and
describe each function. Appeases flake8.
Comment thread addons/threadsafety.py
@andrew-aitchison
Copy link
Copy Markdown
Contributor Author

andrew-aitchison commented May 30, 2023 via email

@danmar
Copy link
Copy Markdown
Collaborator

danmar commented May 31, 2023

Hmm. The code is the same as addons/y2038.py

 cppcheck --suppress=missingInclude:: --template=cppcheck1
--cppcheck-build-dir=.cppcheck
     --addon=threadsafety.py
     --addon=y2038.py
    src/my-src.c

The intention of the code is that srcfile will be src/my-src.c

But when the dump file is created in .cppcheck, the srcfile will more likely be something like .cppcheck/my-src.c

You only use it to print some "Checking.. " message as far as I see but that message will be wrong..

@andrew-aitchison
Copy link
Copy Markdown
Contributor Author

andrew-aitchison commented May 31, 2023 via email

@danmar
Copy link
Copy Markdown
Collaborator

danmar commented Jun 1, 2023

cppcheck --cppcheck-build-dir=qct/.cppcheck --addon=y2038.py qct/qctdataset.cpp
Checking qct/qctdataset.cpp ...
qct/qctdataset.cpp:3074:31: warning: ctime_r is Y2038-unsafe [y2038-unsafe-call]

The "Checking qct/qctdataset.cpp is written by Cppcheck so it does not come from the addon at all.

The "qct/qctdataset.cpp:3074:31: warning: ctime_r is Y2038-unsafe [y2038-unsafe-call]" comes from the addon but I believe it takes the token filename not srcfile

@andrew-aitchison
Copy link
Copy Markdown
Contributor Author

cppcheck --cppcheck-build-dir=qct/.cppcheck --addon=y2038.py qct/qctdataset.cpp
Checking qct/qctdataset.cpp ...
qct/qctdataset.cpp:3074:31: warning: ctime_r is Y2038-unsafe [y2038-unsafe-call]

The "Checking qct/qctdataset.cpp is written by Cppcheck so it does not come from the addon at all.

The "qct/qctdataset.cpp:3074:31: warning: ctime_r is Y2038-unsafe [y2038-unsafe-call]" comes from the addon but I believe it takes the token filename not srcfile

There is something wrong earlier:
cppcheck --force --addon=y2038 /usr/include/c++/12/stdatomic.h
Checking /usr/include/c++/12/stdatomic.h ...
Checking /usr/include/c++/12/stdatomic.h: _GLIBCXX_USE_C99_STDINT_TR1...
Checking /usr/include/c++/12/stdatomic.h: _GLIBCXX_USE_CHAR8_T...
Checking /usr/include/c++/12/stdatomic.h: clang...
Bailing out from checking /usr/include/c++/12/stdatomic.h since there was an internal error: Failed to execute 'python3 /usr/local/cppcheck/cppcheck-2.10/build/bin/addons/runaddon.py /usr/local/cppcheck/cppcheck-2.10/build/bin/addons/y2038.py --cli /usr/include/c++/12/stdatomic.h.dump'. Checking /usr/include/c++/12/stdatomic.h.dump...
Traceback (most recent call last):
File "/usr/local/cppcheck/cppcheck-2.10/build/bin/addons/runaddon.py", line 8, in
runpy.run_path(addon, run_name='main')
File "", line 291, in run_path
File "", line 98, in _run_module_code
File "", line 88, in _run_code
File "/usr/local/cppcheck/cppcheck-2.10/build/bin/addons/y2038.py", line 250, in
check_y2038_safe(dumpfile, quiet)
File "/usr/local/cppcheck/cppcheck-2.10/build/bin/addons/y2038.py", line 157, in check_y2038_safe
data = cppcheckdata.CppcheckData(dumpfile)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/cppcheck/cppcheck-2.10/build/bin/addons/cppcheckdata.py", line 1068, in init
for event, node in ElementTree.iterparse(self.filename, events=('start', 'end')):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/xml/etree/ElementTree.py", line 1268, in iterparse
next(it)
File "/usr/lib/python3.11/xml/etree/ElementTree.py", line 1245, in iterator
source = open(source, "rb")
^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: '/usr/include/c++/12/stdatomic.h.dump'


I copied the code from y2038.py on the assumption that it was correct, without understanding it;
my extended threadsaftey.py works as well as y2038.py
I am not fluent in python; I am not sure there is more I can do.

@danmar
Copy link
Copy Markdown
Collaborator

danmar commented Jun 8, 2023

well I merge this and then try to clean it up..

@danmar danmar merged commit 0727528 into cppcheck-opensource:main Jun 8, 2023
@danmar
Copy link
Copy Markdown
Collaborator

danmar commented Jun 8, 2023

FileNotFoundError: [Errno 2] No such file or directory: '/usr/include/c++/12/stdatomic.h.dump'

ok well the problem with that is a permission problem.

Cppcheck tries to create the file /usr/include/c++/12/stdatomic.h.dump but likely you don't have proper permissions for that (good!).

Cppcheck should have written a better error message when there was permission error.

Also I have the feeling it's a bad idea to create the dump files in the source folder I am thinking about creating temporary files instead.. I wrote a point about that in https://trac.cppcheck.net/ticket/11750

@danmar
Copy link
Copy Markdown
Collaborator

danmar commented Jun 8, 2023

For information: #5132

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.

2 participants