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

Use -Wno-invalid-offsetof instead of dangerous offset_of hack #9563

Closed
wants to merge 2 commits into from

Conversation

pdillinger
Copy link
Contributor

@pdillinger pdillinger commented Feb 14, 2022

Summary: After #9515 added a unique_ptr to Status, we see some
warnings-as-error in some internal builds like this:

stderr: rocksdb/src/db/compaction/compaction_job.cc:2839:7: error:
offset of on non-standard-layout type 'struct CompactionServiceResult'
[-Werror,-Winvalid-offsetof]
     {offsetof(struct CompactionServiceResult, status),
      ^                                        ~~~~~~

I see three potential solutions to resolving this:

  • Expand our use of an idiom that works around the warning (see offset_of
    functions removed in this change, inspired by
    https://gist.github.com/graphitemaster/494f21190bb2c63c5516) However,
    this construction is invoking undefined behavior that assumes consistent
    layout with no compiler-introduced indirection. A compiler incompatible
    with our assumptions will likely compile the code and exhibit undefined
    behavior.
  • Migrate to something in place of offset, like a function mapping
    CompactionServiceResult* to Status* (for the status field). This might
    be required in the long term.
  • Selected: Use our new C++17 dependency to use offsetof in a well-defined way
    when the compiler allows it. From a comment on
    https://gist.github.com/graphitemaster/494f21190bb2c63c5516:
A final note: in C++17, offsetof is conditionally supported, which
means that you can use it on any type (not just standard layout
types) and the compiler will error if it can't compile it correctly.
That appears to be the best option if you can live with C++17 and
don't need constexpr support.

The C++17 semantics are confirmed on
https://en.cppreference.com/w/cpp/types/offsetof, so we can suppress the
warning as long as we accept that we might run into a compiler that
rejects the code, and at that point we will find a solution, such as
the more intrusive "migrate" solution above.

Although this is currently only showing in our buck build, it will
surely show up also with make and cmake, so I have updated those
configurations as well.

Also in the buck build, -Wno-expansion-to-defined does not appear to be
needed anymore (both current compiler configurations) so I
removed it.

Test Plan: Tried out buck builds with both current compiler configurations

Summary: After facebook#9515 added a unique_ptr to Status, we see some
warnings-as-error in some internal builds like this:

```
stderr: rocksdb/src/db/compaction/compaction_job.cc:2839:7: error:
offset of on non-standard-layout type 'struct CompactionServiceResult'
[-Werror,-Winvalid-offsetof]
     {offsetof(struct CompactionServiceResult, status),
      ^                                        ~~~~~~
```

I see three potential solutions to resolving this:

* Be more tricky about computing offsets, to bypass the warning, but
this is basically invoking undefined behavior.
* Migrate to something in place of offset, like a function mapping
CompactionServiceResult* to Status* (for the `status` field). This might
be required in the long term.
* Use our new C++17 dependency to use offsetof in a well-defined way
when the compiler allows it. From a comment on
https://gist.github.com/graphitemaster/494f21190bb2c63c5516:

    A final note: in C++17, offsetof is conditionally supported, which
    means that you can use it on any type (not just standard layout
    types) and the compiler will error if it can't compile it correctly.
    That appears to be the best option if you can live with C++17 and
    don't need constexpr support.

The C++17 semantics are confirmed on
https://en.cppreference.com/w/cpp/types/offsetof, so we can suppress the
warning as long as we accept that we might run into a compiler that
rejects the code, and at that point we will find a solution, such as
the more intrusive "migrate" solution above.

Although this is currently only showing in our buck build, it will
surely show up also with make and cmake, so I have updated those
configurations as well.

Test Plan: Tried out buck builds with platform009 and platform010

Reviewers:

Subscribers:

Tasks:

Tags:
@facebook-github-bot
Copy link
Contributor

@pdillinger has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

Copy link
Contributor

@riversand963 riversand963 left a comment

Choose a reason for hiding this comment

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

LGTM.
The explanation of -Wno-expansion-to-defined is somewhat easy to follow. Some more detail would be nice.

Makefile Outdated
@@ -501,6 +501,10 @@ endif
CFLAGS += $(C_WARNING_FLAGS) $(WARNING_FLAGS) -I. -I./include $(PLATFORM_CCFLAGS) $(OPT)
CXXFLAGS += $(WARNING_FLAGS) -I. -I./include $(PLATFORM_CXXFLAGS) $(OPT) -Woverloaded-virtual -Wnon-virtual-dtor -Wno-missing-field-initializers

# For offsetof to work on non-standard layout types, until some compiler
# completely rejects our usage of offsetof
CXXFLAGS += -Wno-expansion-to-defined
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reference on why we need to make this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

copy-paste error, sorry

@pdillinger pdillinger changed the title Fix platform010 build with -Wno-invalid-offsetof Use -Wno-invalid-offsetof instead of dangerous offset_of hack Feb 14, 2022
@facebook-github-bot
Copy link
Contributor

@pdillinger has updated the pull request. You must reimport the pull request before landing.

@pdillinger
Copy link
Contributor Author

Requesting re-review because I expanded the scope

@facebook-github-bot
Copy link
Contributor

@pdillinger has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

Copy link
Contributor

@riversand963 riversand963 left a comment

Choose a reason for hiding this comment

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

LGTM again. Thanks @pdillinger

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants