Skip to content

fix(net): avoid a misaligned reference to a packed epoll_event member - #8215

Merged
PastaPastaPasta merged 1 commit into
dashpay:developfrom
PastaPastaPasta:fix/epoll-packed-member-ubsan
Nov 20, 2026
Merged

PastaPastaPasta merged 1 commit into
dashpay:developfrom
PastaPastaPasta:fix/epoll-packed-member-ubsan

Conversation

@PastaPastaPasta

@PastaPastaPasta PastaPastaPasta commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Issue being fixed or feature implemented

Since #8193 merged, the linux64_asan-test job (ASan + UBSan, halt_on_error=1) fails on develop and on every PR based on it. dashd aborts on its first epoll event, so most functional tests die within a second or two:

src/util/edge.cpp:143:46: runtime error: reference binding to misaligned address 0x75d8e1d184a4 for type 'void *const', which requires 8 byte alignment

glibc declares struct epoll_event with __EPOLL_PACKED on x86_64, so its data member sits at offset 4. #8193 changed EdgeTriggeredEvents::Wait() to build the result pair with occurred_per_tag[idx] = {ev.data.ptr, occurred};. That passes ev.data.ptr to std::pair's constructor by const reference, which binds a reference to a misaligned void*. That is undefined behaviour, and UBSan's alignment check aborts on it. The code before #8193 read static_cast<SOCKET>(ev.data.fd), a prvalue, so it never bound a reference.

What was done?

The pointer is copied into a local (void* const tag{ev.data.ptr};) before the pair is built. This is the only place that binds a reference to a packed epoll_event member; Sock::RegisterEvents() only stores to event.data.ptr. The kqueue branch gets the same copy of ev.udata: the macOS SDK declares struct kevent under #pragma pack(4), so its 8-byte udata pointer isn't guaranteed alignment either (that path isn't built with UBSan in CI).

How Has This Been Tested?

  • With a clang-19 build using CI's -DSANITIZERS=address,float-divide-by-zero,integer,undefined and CI's ASAN_OPTIONS/UBSAN_OPTIONS, develop reproduces the exact error above in p2p_ping.py and mining_getblocktemplate_longpoll_mempool.py. With this fix, feature_block.py, mining_getblocktemplate_longpoll_mempool.py, p2p_addr_relay.py, p2p_disconnect_ban.py, p2p_ping.py, rpc_net.py and rpc_uptime.py all pass under the same build.
  • A standalone file with the same pattern (packed epoll_event, a brace-initialised std::pair<const void*, unsigned> from ev.data.ptr), built with clang++ -fsanitize=alignment, reports the same runtime error. Copying the member into a local first makes it run clean.

Breaking Changes

None.

Checklist:

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have assigned this pull request to a milestone (if you have permission to set one)

🤖 Generated with Claude Code

@PastaPastaPasta PastaPastaPasta added this to the 24.1 milestone Nov 20, 2026
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The epoll and kqueue paths in EdgeTriggeredEvents::Wait now copy each packed event tag into a local void* variable before storing it with the occurred-event flags.

Estimated code review effort: 1 (Trivial) | ~2 minutes

Possibly related PRs

Merge Risk: Minimal · up to 6d1c1

This is a small fix that avoids a sanitizer alignment abort in the event-wait path without changing behavior. No merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: avoiding a misaligned reference to a packed epoll_event member in the net code. It matches the raw summary, which describes copying the packed event tag into a local b…
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.
Description check Passed The description explains the change in detail: a misaligned reference to the packed epoll_event data.ptr member in EdgeTriggeredEvents::Wait() triggers a UBSan alignment abort, and the fix copies the …
Full details: Title check

Explanation

The title names the main change: avoiding a misaligned reference to a packed epoll_event member in the net code. It matches the raw summary, which describes copying the packed event tag into a local before building the pair. The title is specific and concise.

Full details: Description check

Explanation

The description explains the change in detail: a misaligned reference to the packed epoll_event data.ptr member in EdgeTriggeredEvents::Wait() triggers a UBSan alignment abort, and the fix copies the pointer into a local before building the pair. It also covers the kqueue branch, testing, and the affected CI job. It matches the raw summary's changes in src/util/edge.cpp.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b83ce920-f93b-4f17-b238-b6fcdc90db0c
Commits

Reviewing files that changed from the base of the PR and between a0aa2c9 and 6d1c11e.

Files selected for processing: 1

  • src/util/edge.cpp

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@thepastaclaw

thepastaclaw commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

✅ Final review complete — no blockers (commit 6d1c11e) · triage: low

@thepastaclaw thepastaclaw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Final validation — Phase 1 + Phase 2

The local copy fixes the epoll alignment bug without changing the event tag or dispatch. The same reference-binding pattern remains in the kqueue branch, where the macOS SDK also packs the event struct. No CI checks have failed on this head; the ASan/UBSan, TSan, and no-wallet tests and multiprocess lint are still pending.

🟡 1 suggestion(s) | 💬 1 nitpick(s)

Approved: nothing here blocks the merge. The non-blocking findings are optional: fix them forward, in this PR or a follow-up, without waiting for another review round.

1 finding(s) not shown inline (the lines are not part of this PR's diff)

🟡 Suggestion: Copy the kqueue tag before constructing the pair
src/util/edge.cpp:168

The macOS SDK declares struct kevent under #pragma pack(4), so the event array's type guarantees only 4-byte alignment, while a reference to its udata pointer requires 8-byte alignment on 64-bit macOS. Passing ev.udata directly to the pair constructor leaves the same reference-binding hazard that this PR removes from epoll. Copy the tag by value here too, so correctness does not depend on the compiler giving the array more alignment than its type requires.

            void* const tag{ev.udata};
            occurred_per_tag[idx] = {tag, occurred};

source: glm-5.3-flash (phase1-reviewer: general); gpt-6.2-sol (phase2-reviewer: general, dash-core-commit-history)

Review provenance

Source: reviewer 1: glm-5.3-flash (agent: phase1-reviewer, role: general); reviewer 2: gpt-6.2-sol (agent: phase2-reviewer, role: general); reviewer 3: gpt-6.2-sol (agent: phase2-reviewer, role: dash-core-commit-history); final verifier: gpt-6.2-sol (agent: sol-verifier, role: final-verifier)

  • Triage: low by gpt-6.2-sol (effort low) — The change is confined to the epoll branch of EdgeTriggeredEvents::Wait(), where copying the packed pointer into an aligned local avoids a misaligned reference without changing the returned value.
  • Phase 1 reviewers: glm-5.3-flash — general (completed, effort high); agent phase1-reviewer
  • Phase 1 model: glm-5.3-flash — zai quota: 5h 99% left, weekly 48% left; passed over gemini-3.8-flash-high (antigravity quota lookup failed)
  • Fresh verifier: gpt-6.2-sol — final-verifier; agent sol-verifier
  • Phase 2 reviewers: gpt-6.2-sol — general (completed, effort medium); agent phase2-reviewer, gpt-6.2-sol — dash-core-commit-history (completed, effort medium); agent phase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review (PastaClaw). How to work through them:
- Verify each finding against the code; fix only what is real, as narrowly as the finding.
- If a finding is wrong, decline it on its thread in one or two sentences; the bot answers once and stops.
- To defer one, link a tracking issue on its thread (or reply asking the bot to file one).
- Suggestions don't block the merge, but most are worth fixing in this PR; judge each on its merits.
- Keep the PR's scope; don't grow it to satisfy hardening suggestions.
- Carried items were raised in an earlier round: read the thread first, and don't re-fix what you already fixed, declined or deferred there. Disputed and deferred items are already handled and are not listed.

In `src/util/edge.cpp`:
- [SUGGESTION] src/util/edge.cpp:168: Copy the kqueue tag before constructing the pair
  The macOS SDK declares `struct kevent` under `#pragma pack(4)`, so the event array's type guarantees only 4-byte alignment, while a reference to its `udata` pointer requires 8-byte alignment on 64-bit macOS. Passing `ev.udata` directly to the pair constructor leaves the same reference-binding hazard that this PR removes from epoll. Copy the tag by value here too, so correctness does not depend on the compiler giving the array more alignment than its type requires.
- [NITPICK] src/util/edge.cpp:143-144: Document why the epoll tag must be copied
  The local looks like a temporary that can be inlined. A short comment preserves the reason at the point of use: passing `ev.data.ptr` directly would let the pair constructor bind a reference to a potentially misaligned packed member.

Comment thread src/util/edge.cpp
Comment on lines +143 to +144
void* const tag{ev.data.ptr};
occurred_per_tag[idx] = {tag, occurred};

@thepastaclaw thepastaclaw Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

✅ Resolved at 6d1c11ed; see the replies below.

💬 Nitpick: Document why the epoll tag must be copied

The local looks like a temporary that can be inlined. A short comment preserves the reason at the point of use: passing ev.data.ptr directly would let the pair constructor bind a reference to a potentially misaligned packed member.

Suggested change
void* const tag{ev.data.ptr};
occurred_per_tag[idx] = {tag, occurred};
// Copy the tag to avoid binding a reference to a packed member.
void* const tag{ev.data.ptr};
occurred_per_tag[idx] = {tag, occurred};

source: glm-5.3-flash (phase1-reviewer: general)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Resolved (re-reviewed at 6d1c11ed): Your comment at src/util/edge.cpp:143 explains why the tag is copied: binding a reference directly to the packed member could bind to a misaligned address.

@thepastaclaw thepastaclaw added the pastaclaw:approved thepastaclaw's latest review approved this PR label Nov 20, 2026
knst
knst previously approved these changes Nov 20, 2026

@knst knst left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

utACK a0aa2c9

glibc declares struct epoll_event with __EPOLL_PACKED on x86_64, so its
data member sits at offset 4. EdgeTriggeredEvents::Wait() built the
(tag, events) pair from ev.data.ptr directly, which binds a const
reference to that misaligned void* inside std::pair's constructor. That
is undefined behaviour, and UBSan's alignment check aborts dashd on the
first epoll event, failing most functional tests in the sanitizer CI
job since dashpay#8193.

Copy the pointer into a local before building the pair. Do the same for
the kqueue tag: the macOS SDK declares struct kevent under
#pragma pack(4), so its udata pointer is not guaranteed 8-byte
alignment either.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@PastaPastaPasta
PastaPastaPasta force-pushed the fix/epoll-packed-member-ubsan branch from a0aa2c9 to 6d1c11e Compare November 20, 2026 20:06
@thepastaclaw thepastaclaw removed the pastaclaw:approved thepastaclaw's latest review approved this PR label Nov 20, 2026
@PastaPastaPasta

Copy link
Copy Markdown
Member Author

6d1c11e went up three minutes after this ACK and dismissed it. The only change since a0aa2c9 is in src/util/edge.cpp: the kqueue branch now copies ev.udata into a local the same way, because the macOS SDK declares struct kevent under #pragma pack(4), so its udata pointer isn't guaranteed 8-byte alignment either. Each copy also gets a one-line comment. git diff a0aa2c9 6d1c11e is 4 lines added, 1 removed.


🤖 Posted autonomously by Claude (claude-opus-5-5) on behalf of pasta.

@thepastaclaw thepastaclaw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Re-review — Final validation — Phase 1 + Phase 2

Static review of 6d1c11e found no issues: both event backends read packed tags by value before constructing the result pair, leaving socket lookup and dispatch unchanged. No code checks have failed; the macOS and ASan builds passed, while the ASan/UBSan, TSan, and multiprocess tests, multiprocess lint, and standard Linux build are still pending.

🔴 0 blocking | 🟡 0 suggestion(s) | 💬 0 nitpick(s)

Review provenance

Source: reviewer 1: glm-5.3-flash (agent: phase1-reviewer, role: general); reviewer 2: gpt-6.2-sol (agent: phase2-reviewer, role: general); reviewer 3: gpt-6.2-sol (agent: phase2-reviewer, role: dash-core-commit-history); final verifier: gpt-6.2-sol (agent: sol-verifier, role: final-verifier)

  • Triage: low by gpt-6.2-sol (effort low) — This is a small, self-contained undefined-behavior fix in src/util/edge.cpp that copies packed event members before constructing result pairs, with no consensus, funds, cryptographic, or storage impact.
  • Phase 1 reviewers: glm-5.3-flash — general (completed, effort high); agent phase1-reviewer
  • Phase 1 model: glm-5.3-flash — zai quota: 5h 99% left, weekly 48% left; passed over gemini-3.8-flash-high (antigravity quota lookup failed)
  • Fresh verifier: gpt-6.2-sol — final-verifier; agent sol-verifier
  • Phase 2 reviewers: gpt-6.2-sol — general (completed, effort medium); agent phase2-reviewer, gpt-6.2-sol — dash-core-commit-history (completed, effort medium); agent phase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review. Verify the current code and confirm that no unresolved issues remain.

No unresolved findings remain from the prior review on this head.

Comment thread src/util/edge.cpp
Comment on lines +143 to +144
void* const tag{ev.data.ptr};
occurred_per_tag[idx] = {tag, occurred};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Resolved (re-reviewed at 6d1c11ed): Your comment at src/util/edge.cpp:143 explains why the tag is copied: binding a reference directly to the packed member could bind to a misaligned address.

@thepastaclaw thepastaclaw added the pastaclaw:approved thepastaclaw's latest review approved this PR label Nov 20, 2026

@PastaPastaPasta PastaPastaPasta left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Approved

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