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

Fix strict aliasing violations in database_utils.hpp #131

Merged
merged 1 commit into from
May 9, 2022

Conversation

spoonincode
Copy link
Member

This ports over EOSIO/eos#9761

There are a number of uses of reinterpret_cast in database_utils.hpp. They violate the strict aliasing rule.
The fix is using memcpy to assign values between non-compatible types.

std::string s = "0x";
s.append( to_hex( reinterpret_cast<const char*>(&as_bytes), sizeof(as_bytes) ) );
Copy link
Contributor

Choose a reason for hiding this comment

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

Cast to const char* is legal and doesn't require memcpy. This could just drop as_bytes and cast &f.

Copy link
Member Author

Choose a reason for hiding this comment

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

Are you suggesting to change it? I'm mostly indifferent, as it was already approved previously like this.

Copy link
Contributor

Choose a reason for hiding this comment

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

Up to you. It's not a big deal, and the code works either way.

@@ -106,35 +106,61 @@ namespace fc {
from_variant(v, oid._id);
}

inline
void float64_to_double (const float64_t& f, double& d) {
Copy link
Contributor

Choose a reason for hiding this comment

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

softfloat.hpp has has double from_softfloat64(float64_t) and float64_t to_softfloat64(double). I would use those and define the float128 versions similarly. (Note that C++20 has std::bit_cast)

Copy link
Member Author

Choose a reason for hiding this comment

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

At first I wasn't sure if eos-vm considered these softfloat functions part of its public interface. But it seems they're present even without ENABLE_SOFTFLOAT so maybe it does. The code reuse would probably be a good thing, I suppose, even in this trivial case.

Copy link
Contributor

Choose a reason for hiding this comment

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

They're not from eos-vm. They're from berkeley softfloat.

Copy link
Member Author

Choose a reason for hiding this comment

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

Well that's the thing, we bring in softfloat two places: eos-vm and as a library in eosio.

eos-vm exposes the softfloat headers in a way that suggests they're public interface. (it's included in a cmake PUBLIC interface -- which they probably need to be due to being a header only library -- but they're not in an /impl/ or /detail/ directory and/or namespace either)

The library that's pulled for the rest of the eosio build is primarily used for softfloat injection in EOS VM OC. But mid/long term I expect that injection to go away. There are a few very very minor usages of softfloat outside of EOS VM OC injection -- funny enough already in database_utils.hpp does it include softfloat.hpp. Kinda lousy to double down depending on this library for just a few lines of code here and there; even more lousy if we need to go add float128 versions to said library 😕

Copy link
Contributor

Choose a reason for hiding this comment

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

We already have a direct dependency here in the types float32_t and float64_t

Copy link
Contributor

Choose a reason for hiding this comment

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

If we do eliminate those types, then the dependency on softfloat sill naturally go away. Anyway, I'm approving this PR as-is. Feel free to take or ignore my suggestions at your own discretion.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll go ahead and take as-is since 1) it was approved in the past and 2) there doesn't seem to be anything straight out incorrect with the changes. You can submit a PR with your improvements if you have a chance.

@spoonincode spoonincode merged commit fcbefbe into main May 9, 2022
@spoonincode spoonincode deleted the fix_strict_alias branch May 9, 2022 15:41
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.

3 participants