Skip to content

port/mmap: fix Clang -Wnontrivial-memcall error on macOS#429

Merged
ti-chi-bot[bot] merged 1 commit into
tikv:8.10.tikvfrom
liyishuai:fix/macos-clang-nontrivial-memcall
Apr 28, 2026
Merged

port/mmap: fix Clang -Wnontrivial-memcall error on macOS#429
ti-chi-bot[bot] merged 1 commit into
tikv:8.10.tikvfrom
liyishuai:fix/macos-clang-nontrivial-memcall

Conversation

@liyishuai

@liyishuai liyishuai commented Apr 27, 2026

Copy link
Copy Markdown

Problem

Clang 16+ enables -Wnontrivial-memcall, which rejects memcpy on pointers to non-trivially copyable types. MemMapping has a user-provided destructor, making it non-trivially copyable. The move assignment operator used memcpy(this, &other, sizeof(*this)), which triggers -Werror,-Wnontrivial-memcall on macOS and causes a build failure:

port/mmap.cc:46:15: error: first argument in call to 'memcpy' is a pointer to
non-trivially copyable type 'rocksdb::MemMapping' [-Werror,-Wnontrivial-memcall]
  std::memcpy(this, &other, sizeof(*this));

Fix

Replace the memcpy + placement-new pattern with explicit std::exchange member assignments — the correct C++ idiom for move semantics on a type with managed resources. Also removes the now-unused <cstring> and <new> includes.

Test

Build passes on macOS with Apple Clang (tested via tikv/titan).

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Refactor
    • Improved internal memory mapping implementation for better efficiency and safety.

Copilot AI review requested due to automatic review settings April 27, 2026 09:04
@ti-chi-bot ti-chi-bot Bot added contribution This PR is from a community contributor. needs-ok-to-test Indicates a PR created by contributors and need ORG member send '/ok-to-test' to start testing. dco-signoff: no Indicates the PR's author has not signed dco. labels Apr 27, 2026
@ti-chi-bot

ti-chi-bot Bot commented Apr 27, 2026

Copy link
Copy Markdown

Hi @liyishuai. Thanks for your PR.

I'm waiting for a tikv member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@ti-chi-bot ti-chi-bot Bot added the first-time-contributor Indicates that the PR was contributed by an external member and is a first-time contributor. label Apr 27, 2026
@ti-chi-bot

ti-chi-bot Bot commented Apr 27, 2026

Copy link
Copy Markdown

Welcome @liyishuai!

It looks like this is your first PR to tikv/rocksdb 🎉.

I'm the bot to help you request reviewers, add labels and more, See available commands.

We want to make sure your contribution gets all the attention it needs!



Thank you, and welcome to tikv/rocksdb. 😃

@ti-chi-bot ti-chi-bot Bot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Apr 27, 2026
@coderabbitai

coderabbitai Bot commented Apr 27, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@liyishuai has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 48 minutes and 39 seconds before requesting another review.

To keep reviews running without waiting, you can enable usage-based add-on for your organization. This allows additional reviews beyond the hourly cap. Account admins can enable it under billing.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c723fdc7-4e95-4b24-ac30-e23b88db5ba5

📥 Commits

Reviewing files that changed from the base of the PR and between 63d62a4 and f67c665.

📒 Files selected for processing (1)
  • port/mmap.cc
📝 Walkthrough

Walkthrough

The move assignment operator for MemMapping is refactored from a memcpy-based state transfer with placement-new rebuilding to a safer member-wise move using std::exchange, properly leaving the source object in a null state. Unused headers <cstring> and <new> are removed.

Changes

Cohort / File(s) Summary
Move Assignment Operator Refactoring
port/mmap.cc
Replaced unsafe memcpy-based move assignment with member-wise std::exchange operations for safer ownership transfer; removed unused <cstring> and <new> headers.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A hop, a skip, a safer way,
Move semantics dance and play,
No memcpy tricks to hide,
Just exchange with graceful stride,
The source left null and light! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title directly describes the main fix: replacing unsafe memcpy usage with proper move semantics to resolve a Clang compilation error on macOS.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@liyishuai liyishuai force-pushed the fix/macos-clang-nontrivial-memcall branch from 63d62a4 to cd2a4b8 Compare April 27, 2026 09:05
@ti-chi-bot ti-chi-bot Bot added dco-signoff: yes Indicates the PR's author has signed the dco. and removed dco-signoff: no Indicates the PR's author has not signed dco. labels Apr 27, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Fixes macOS (Clang 16+) build failures caused by -Werror,-Wnontrivial-memcall by removing the memcpy-based move assignment implementation for MemMapping and replacing it with member-wise moves.

Changes:

  • Remove memcpy + placement-new move-assignment pattern from MemMapping.
  • Implement move assignment via std::exchange on the mapping’s owned members.
  • Drop now-unused headers (<cstring>, <new>).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread port/mmap.cc Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
port/mmap.cc (1)

43-48: Member access after explicit destructor call is technically UB; consider placement-new or inline cleanup.

After this->~MemMapping() at Line 43, the lifetime of *this has ended. Per [basic.life], using this to access non-static data members (Lines 44–48) is undefined behavior, even though it works in practice here because the members are trivial scalar types. The original memcpy-based code avoided this by re-starting object lifetime via placement-new.

Two clean options:

♻️ Option A — restart lifetime via placement-new (most idiomatic, re-adds ``)
 MemMapping& MemMapping::operator=(MemMapping&& other) noexcept {
   if (&other == this) {
     return *this;
   }
   this->~MemMapping();
-  addr_ = std::exchange(other.addr_, nullptr);
-  length_ = std::exchange(other.length_, 0);
-#ifdef OS_WIN
-  page_file_handle_ = std::exchange(other.page_file_handle_, NULL);
-#endif
+  ::new (this) MemMapping(std::move(other));
   return *this;
 }
♻️ Option B — release resources without calling the destructor, then move members (keeps `` removed)
 MemMapping& MemMapping::operator=(MemMapping&& other) noexcept {
   if (&other == this) {
     return *this;
   }
-  this->~MemMapping();
+  // Release currently owned resources without ending *this's lifetime.
+#ifdef OS_WIN
+  if (addr_ != nullptr) {
+    (void)::UnmapViewOfFile(addr_);
+  }
+  if (page_file_handle_ != NULL) {
+    (void)::CloseHandle(page_file_handle_);
+  }
+#else
+  if (addr_ != nullptr) {
+    auto status = munmap(addr_, length_);
+    assert(status == 0);
+    (void)status;
+  }
+#endif
   addr_ = std::exchange(other.addr_, nullptr);
   length_ = std::exchange(other.length_, 0);
 `#ifdef` OS_WIN
   page_file_handle_ = std::exchange(other.page_file_handle_, NULL);
 `#endif`
   return *this;
 }

Either option avoids the post-destructor member access. Option A is the smaller diff and most closely matches the original intent.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@port/mmap.cc` around lines 43 - 48, Calling this->~MemMapping() ends the
object's lifetime and then accessing members (addr_, length_, page_file_handle_)
is UB; restore lifetime before touching members by either (A) performing a
placement-new of MemMapping at this to reinitialize the object and then assign
std::exchange(other.addr_, nullptr)/std::exchange(other.length_,
0)/std::exchange(other.page_file_handle_, NULL), or (B) avoid calling the
destructor entirely and instead call the destructor logic that releases
resources (e.g., a private cleanup method) without ending the object's lifetime,
then move the members from other into addr_, length_, and page_file_handle_;
update MemMapping's move-or-swap implementation accordingly so no member is
accessed after lifetime ends.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@port/mmap.cc`:
- Around line 43-48: Calling this->~MemMapping() ends the object's lifetime and
then accessing members (addr_, length_, page_file_handle_) is UB; restore
lifetime before touching members by either (A) performing a placement-new of
MemMapping at this to reinitialize the object and then assign
std::exchange(other.addr_, nullptr)/std::exchange(other.length_,
0)/std::exchange(other.page_file_handle_, NULL), or (B) avoid calling the
destructor entirely and instead call the destructor logic that releases
resources (e.g., a private cleanup method) without ending the object's lifetime,
then move the members from other into addr_, length_, and page_file_handle_;
update MemMapping's move-or-swap implementation accordingly so no member is
accessed after lifetime ends.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 74b9cef1-c973-46e9-b01a-45cc57b2815e

📥 Commits

Reviewing files that changed from the base of the PR and between d0602d2 and 63d62a4.

📒 Files selected for processing (1)
  • port/mmap.cc

Summary:
fix the following error showing up in continuous tests:
```
Makefile:186: Warning: Compiling in debug mode. Don't use the resulting binary in production
port/mmap.cc:46:15: error: first argument in call to 'memcpy' is a pointer to non-trivially copyable type 'rocksdb::MemMapping' [-Werror,-Wnontrivial-memcall]
   46 |   std::memcpy(this, &other, sizeof(*this));
      |               ^
port/mmap.cc:46:15: note: explicitly cast the pointer to silence this warning
   46 |   std::memcpy(this, &other, sizeof(*this));
      |               ^
      |               (void*)
1 error generated.
make: *** [Makefile:2580: port/mmap.o] Error 1
make: *** Waiting for unfinished jobs....
```

Pull Request resolved: facebook#13864

Test Plan: `make USE_CLANG=1 j=150 check` with https://github.com/facebook/rocksdb/blob/13f054febb26100184eeefaac11877d735d45ac2/build_tools/build_detect_platform#L61-L70 commented out.

Reviewed By: mszeszko-meta

Differential Revision: D80033441

Pulled By: cbi42

fbshipit-source-id: b2330eea71fe28243236b75128ec6f3f1e971873
Signed-off-by: Yishuai Li <yishuai.li@pingcap.com>
@liyishuai liyishuai force-pushed the fix/macos-clang-nontrivial-memcall branch from cd2a4b8 to f67c665 Compare April 27, 2026 09:15

@Connor1996 Connor1996 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@ti-chi-bot ti-chi-bot Bot added needs-1-more-lgtm Indicates a PR needs 1 more LGTM. approved labels Apr 27, 2026
@ti-chi-bot

ti-chi-bot Bot commented Apr 27, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Connor1996, v01dstar

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:
  • OWNERS [Connor1996,v01dstar]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot Bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Apr 27, 2026
@ti-chi-bot

ti-chi-bot Bot commented Apr 27, 2026

Copy link
Copy Markdown

[LGTM Timeline notifier]

Timeline:

  • 2026-04-27 20:59:49.15112444 +0000 UTC m=+2631594.356484497: ☑️ agreed by Connor1996.
  • 2026-04-27 21:01:21.585200147 +0000 UTC m=+2631686.790560204: ☑️ agreed by v01dstar.

@liyishuai

Copy link
Copy Markdown
Author

/retest

@ti-chi-bot

ti-chi-bot Bot commented Apr 28, 2026

Copy link
Copy Markdown

@liyishuai: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

Details

In response to this:

/retest

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@lhy1024

lhy1024 commented Apr 28, 2026

Copy link
Copy Markdown
Member

/ok-to-test

@ti-chi-bot ti-chi-bot Bot added ok-to-test Indicates a PR is ready to be tested. and removed needs-ok-to-test Indicates a PR created by contributors and need ORG member send '/ok-to-test' to start testing. labels Apr 28, 2026
@ti-chi-bot ti-chi-bot Bot merged commit 088f014 into tikv:8.10.tikv Apr 28, 2026
11 checks passed
@liyishuai liyishuai deleted the fix/macos-clang-nontrivial-memcall branch April 28, 2026 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved contribution This PR is from a community contributor. dco-signoff: yes Indicates the PR's author has signed the dco. first-time-contributor Indicates that the PR was contributed by an external member and is a first-time contributor. lgtm ok-to-test Indicates a PR is ready to be tested. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants