Skip to content

makefiles: Add -fwrapv to CFLAGS#10748

Merged
fjmolinas merged 1 commit intoRIOT-OS:masterfrom
maribu:sane_signed_interger_overflow
Jan 25, 2021
Merged

makefiles: Add -fwrapv to CFLAGS#10748
fjmolinas merged 1 commit intoRIOT-OS:masterfrom
maribu:sane_signed_interger_overflow

Conversation

@maribu
Copy link
Member

@maribu maribu commented Jan 10, 2019

Contribution description

This commit makes overflow of signed integers to behave as expected by at 90%
of the C developers, even though overflow of signed integers are strictly
undefined behavior.

Note: Please do not add code relying on a specific behavior for the overflow of
signed integers, even though -fwrpav will make that code work. This is
intended to mitigate the risk of bugs in overflow checks being exploited,
not to encourage adding new bugs.

See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=30475 for details and see
http://c-faq.com/misc/intovf.html on how to implement overflow checks properly.

Testing procedure

I guess if Murdock still compiles everything, this should be sufficient

Issues/PRs references

This issue was discussed in #10740, but that PR is not much related otherwise

@maribu maribu added Area: security Area: Security-related libraries and subsystems Process: needs >1 ACK Integration Process: This PR requires more than one ACK labels Jan 10, 2019
@kaspar030
Copy link
Contributor

How about enabling all the warnings there are (-Wstrict-overflow)?

@maribu
Copy link
Member Author

maribu commented Jan 10, 2019

@kaspar030: I agree that addressing the problem (fixing the code that relies on undefined behavior) instead of putting duct tape around the problem (making code relying on undefined behavior work as intended instead of fixing the bug) would be much better. And making the compiler warn about those bugs would be the perfect tool for that.

The problem is when I use this buggy code:

#include <stdio.h>
#include <limits.h>

int main(void)
{
    int a = INT_MAX;
    if (a + 1 < a) { /* <-- Undefined behavior here */
        puts("a + 1 < a!");
    }

    printf("a + 1 = %d, a = %d\n", a + 1, a);
    return 0;
}

The "expected" (expected != correct here) output would be:

a + 1 < a!
a + 1 = -2147483648, a = 2147483647

But when I compile and run I get with GCC 8.2.0:

$ gcc -Wstrict-overflow -Wall -Wextra -std=c99 -pedantic -o test test.c && ./test
a + 1 = -2147483648, a = 2147483647

clang does compile and even produces the expected output in this case, but does not give a warning either. (But beware: clang does also optimize checks out, e.g. the assert in http://ptrace.fefe.de/int.c)

So -Wstrict-overflow does not detect this issue :-(

@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Feb 5, 2020
@fjmolinas
Copy link
Contributor

How about enabling all the warnings there are (-Wstrict-overflow)?

Should we do both then?

@fjmolinas
Copy link
Contributor

@kaspar030 are you fine with the change now?

Copy link
Contributor

@kaspar030 kaspar030 left a comment

Choose a reason for hiding this comment

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

ACK.

@kaspar030
Copy link
Contributor

please squash!

@maribu maribu force-pushed the sane_signed_interger_overflow branch from 2198c78 to e41eb31 Compare June 16, 2020 12:10
@fjmolinas
Copy link
Contributor

All failures seem to be msp430 related, I tested that it works in #12457. I would for now simply add a:

ifneq (,$(filter arch_msp430,$(FEATURES_USED)))
...
endif

Around both CFLAGS, maybe add a comment referencing #12457

@kaspar030
Copy link
Contributor

All failures seem to be msp430 related, I tested that it works in #12457.

I'll rebase and revert that commit in #12457 once this is merged.

@maribu maribu added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Aug 23, 2020
@maribu maribu force-pushed the sane_signed_interger_overflow branch from 83deda5 to c52cb21 Compare August 23, 2020 19:00
@maribu maribu closed this Jan 25, 2021
@maribu maribu deleted the sane_signed_interger_overflow branch January 25, 2021 09:46
@fjmolinas
Copy link
Contributor

@maribu why did you close? If I remember there was a CI issue last time?

@maribu maribu restored the sane_signed_interger_overflow branch January 25, 2021 10:04
@maribu maribu reopened this Jan 25, 2021
@maribu
Copy link
Member Author

maribu commented Jan 25, 2021

Sorry, I thought this was already addressed elsewhere.

Copy link
Contributor

@fjmolinas fjmolinas left a comment

Choose a reason for hiding this comment

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

All green, ACK!

@fjmolinas
Copy link
Contributor

@maribu seems like it needs a rebase for all the checks.

This commit makes overflow of signed integers to behave as expected by at 90%
of the C developers, even though overflow of signed integers are strictly
undefined behavior.

Note: Please do not add code relying on a specific behavior for the overflow of
      signed integers, even though `-fwrpav` will make that code work. This is
      intended to mitigate the risk of bugs in overflow checks being exploited,
      not to encourage adding new bugs.

See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=30475 for details and see
http://c-faq.com/misc/intovf.html on how to implement overflow checks properly.
@maribu maribu force-pushed the sane_signed_interger_overflow branch from c52cb21 to 01382dc Compare January 25, 2021 12:35
@fjmolinas
Copy link
Contributor

There is a test failing, but it's on native and related to timing. I would think the cause is server load https://ci.riot-os.org/RIOT-OS/RIOT/10748/01382dc4ad1d5f7448755a01daf28404dfdb1406/output/compile/tests/posix_semaphore/native:gnu.txt, I can't reproduce locally.

@maribu
Copy link
Member Author

maribu commented Jan 25, 2021

There is a test failing, but it's on native and related to timing. I would think the cause is server load https://ci.riot-os.org/RIOT-OS/RIOT/10748/01382dc4ad1d5f7448755a01daf28404dfdb1406/output/compile/tests/posix_semaphore/native:gnu.txt, I can't reproduce locally.

The same worker also showed another similar issue in another PR: #15760 (comment) - which couldn't be reproduced either by CI or locally.

@maribu maribu added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Jan 25, 2021
@fjmolinas fjmolinas merged commit 25e99d8 into RIOT-OS:master Jan 25, 2021
@maribu
Copy link
Member Author

maribu commented Jan 25, 2021

Thanks. Good to have this in :-)

@maribu maribu deleted the sane_signed_interger_overflow branch January 25, 2021 17:50
@kaspar030 kaspar030 added this to the Release 2021.04 milestone Apr 23, 2021
@kaspar030 kaspar030 added the Release notes: added Set on PRs that have been processed into the release notes for the current release. label Apr 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: security Area: Security-related libraries and subsystems CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Process: needs >1 ACK Integration Process: This PR requires more than one ACK Release notes: added Set on PRs that have been processed into the release notes for the current release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants