Skip to content

Fix some UBSAN false positives #6115

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

Merged
merged 2 commits into from
Jul 12, 2022

Conversation

kevinbackhouse
Copy link
Contributor

I tried building ruby with UBSAN and saw some false-positive results for "unsigned integer overflow". These changes fix the false-positives, without changing the behavior of the code.

Copy link
Member

@jhawthorn jhawthorn left a comment

Choose a reason for hiding this comment

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

Looks good to me! Just needs dependencies fixed

@@ -37,6 +37,7 @@

#include "regparse.h"
#include <stdarg.h>
#include "internal/sanitizers.h"
Copy link
Member

Choose a reason for hiding this comment

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

Because of this addition we need to run ruby tool/update-deps --fix (this is the cause of CI failing). It's a little finicky, ping me if you'd prefer I run it to fix the deps 😅.

@@ -115,7 +115,7 @@ RB_INT2FIX(long i)
/* :NOTE: VALUE can be wider than long. As j being unsigned, 2j+1 is fully
* defined. Also it can be compiled into a single LEA instruction. */
const unsigned long j = i;
const unsigned long k = 2 * j + RUBY_FIXNUM_FLAG;
const unsigned long k = (j << 1) + RUBY_FIXNUM_FLAG;
Copy link
Member

@jhawthorn jhawthorn Jul 12, 2022

Choose a reason for hiding this comment

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

(We discussed this a little at work)

It seems like, even though C considers unsigned overflow defined for both << and *, UBSAN checks for overflow on *, but not <<. We figured that this UBSAN behaviour is probably safe to rely on, and doing this avoids having to NO_SANITIZE in this code that is exported in the C extension API.

(The output of gcc and clang w/o UBSAN is identical)

👍

@jhawthorn jhawthorn merged commit 8c18081 into ruby:master Jul 12, 2022
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