Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Fix undefined behaviour in C11 atomics #11528

Merged
merged 1 commit into from May 15, 2019

Conversation

maribu
Copy link
Member

@maribu maribu commented May 15, 2019

Contribution description

Casting pointers to volatile memory to pointers to regular memory is permitted, but using those pointers to access the memory results in undefined behavior. This PR changes the casts to no longer drop the volatile qualifier.

References: https://en.cppreference.com/w/c/language/volatile

Testing procedure

Compile some applications before and after this PR. The output should not differ.

Issues/PRs references

#11514

@maribu maribu added Area: core Area: RIOT kernel. Handle PRs marked with this with care! Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) labels May 15, 2019
@kaspar030 kaspar030 added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR CI: run tests If set, CI server will run tests on hardware for the labeled PR labels May 15, 2019
@kaspar030
Copy link
Contributor

IMO this should do it. Did elf_diff show any differences?

@maribu
Copy link
Member Author

maribu commented May 15, 2019

I checked for the Nucleo-F767zi with examples/default: https://mari-bu.de/c11_atomics.html No differences there

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. Thanks!

@kaspar030
Copy link
Contributor

This might need a second ACK, too. @SemjonKerner? ;)

Copy link
Member

@SemjonWilke SemjonWilke left a comment

Choose a reason for hiding this comment

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

Fix as disscussed. You could fix the indentions, but feel free to squash right away.
ACK

core/atomic_c11.c Outdated Show resolved Hide resolved
core/atomic_c11.c Outdated Show resolved Hide resolved
Casting pointers to volatile memory to pointers to regular memory is permitted,
but using those pointers to access the memory results in undefined behavior.
This commit changes the casts to no longer drop the volatile qualifier.

References: https://en.cppreference.com/w/c/language/volatile
unsigned int mask = irq_disable(); \
(void)memmodel; \
I##n tmp = *(volatile I##n *)ptr; \
*(volatile I##n *)ptr = prefixop(tmp op val); \
Copy link
Contributor

Choose a reason for hiding this comment

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

weird, github still shows this line's last backslash one off, but with a direct checkout, it is alright?

Copy link
Member Author

Choose a reason for hiding this comment

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

On my browser (Firefox) they look all aligned.

I also changed the column of the backslashes as requested to be 4 * n, and not 4 * n + 1. So merge?

Copy link
Member

Choose a reason for hiding this comment

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

Looks good in github now. You might F5 on this, Kaspar^^

Copy link
Contributor

Choose a reason for hiding this comment

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

I F5'ed the shit out of this, still shows the missing space. Anyhow, it also doesn't let me upload the screenshot. Anyways, directly checking this out looks fine, so let's go!

@SemjonWilke SemjonWilke merged commit 9ab427a into RIOT-OS:master May 15, 2019
@maribu maribu deleted the atomic_c11 branch November 4, 2019 19:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: core Area: RIOT kernel. Handle PRs marked with this with care! CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR CI: run tests If set, CI server will run tests on hardware for the labeled PR Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants