Skip to content

Fix some UBSAN false positives#6115

Merged
jhawthorn merged 2 commits intoruby:masterfrom
kevinbackhouse:ubsan-false-positive
Jul 12, 2022
Merged

Fix some UBSAN false positives#6115
jhawthorn merged 2 commits intoruby:masterfrom
kevinbackhouse:ubsan-false-positive

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


#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 😅.

* 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