Skip to content

Comments

Fix lock-free queue bugs, add GitHub Actions CI, and add ABA stress test#10

Draft
Copilot wants to merge 6 commits intoHPfrom
copilot/fix-bugs-and-review-code
Draft

Fix lock-free queue bugs, add GitHub Actions CI, and add ABA stress test#10
Copilot wants to merge 6 commits intoHPfrom
copilot/fix-bugs-and-review-code

Conversation

Copy link

Copilot AI commented Feb 21, 2026

Several correctness bugs in the hazard pointer implementation and test harness, plus missing CI since Travis CI is no longer free for OSS.

Bug Fixes

  • Wrong calloc element sizes in lfq_init: HP (array of pointers) and tid_map (array of int) were both allocated with sizeof(struct lfq_node) — 3× overallocation that masked out-of-bounds writes:

    // Before (wrong)
    ctx->HP      = calloc(max_consume_thread, sizeof(struct lfq_node));
    ctx->tid_map = calloc(max_consume_thread, sizeof(struct lfq_node));
    
    // After (correct)
    ctx->HP      = calloc(max_consume_thread, sizeof(struct lfq_node *));
    ctx->tid_map = calloc(max_consume_thread, sizeof(int));
  • Thread ID off-by-one in test consumer: ATOMIC_ADD(&cn_t, 1) returns 1-based values (1…MAX_CONSUMER), causing the last consumer to write HP[MAX_CONSUMER] — one past the end. Fixed: ATOMIC_ADD(&cn_t, 1) - 1.

  • smb()/lmb() undefined on non-x86 platforms: Added #else fallback to mb() so the code compiles on ARM/RISC-V.

  • Memory leak in lfq_init failure path: If the second calloc failed, tmpnode was leaked. Added free(tmpnode) before the early return.

CI

  • Replaced .travis.yml with a GitHub Actions workflow (.github/workflows/ci.yml) using the official C/C++ CI starter template, running make and make test on ubuntu-latest with contents: read permissions.

ABA Stress Test

Added test_aba.c: a dedicated test designed to maximise the conditions under which the ABA problem would manifest in a CAS-based lock-free queue.

The ABA problem: a CAS on a pointer succeeds spuriously when a node is dequeued, freed, and a new node is allocated at the same address before a stale CAS completes — causing the queue to operate on a logically different node without detecting the change.

How this test maximises ABA probability compared to the existing tests:

Property Existing test ABA stress test
Thread roles Separate producers + consumers Every thread is both producer AND consumer
Queue depth Grows large (500k items per producer) Near-zero (1 item per thread in flight)
Yielding THREAD_YIELD() when queue empty No yielding — tight spin after enqueue
Memory pattern Many different addresses Rapid alloc/free → allocator reuses same addresses
Corruption detection Count mismatch only LIVE_MAGIC/DEAD_MAGIC sentinel per node

Each node carries a LIVE_MAGIC value on enqueue, poisoned to DEAD_MAGIC before free(). Any ABA-induced use-after-free or stale-pointer read surfaces immediately as a magic mismatch. The queue passes the test (8,000,000 ops across 16 threads, zero corruption), confirming the hazard pointer scheme correctly prevents ABA.

The Makefile is updated to build bin/test_aba and include it in make test.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@darkautism
Copy link
Owner

CI改成最新的github提供的workflow @copilot

Copilot AI and others added 3 commits February 21, 2026 11:10
Co-authored-by: darkautism <3898040+darkautism@users.noreply.github.com>
Co-authored-by: darkautism <3898040+darkautism@users.noreply.github.com>
Co-authored-by: darkautism <3898040+darkautism@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix bugs and ensure all CI processes are working Fix lock-free queue bugs and add GitHub Actions CI Feb 21, 2026
Copilot AI requested a review from darkautism February 21, 2026 11:14
Co-authored-by: darkautism <3898040+darkautism@users.noreply.github.com>
Copy link
Author

Copilot AI commented Feb 21, 2026

CI改成最新的github提供的workflow @copilot

Updated in ad326ff — the workflow now follows GitHub's official C/C++ CI starter template (name: C/C++ CI, branch filters on HP, job named build, steps using make / make test).

@darkautism
Copy link
Owner

設計一款更在multithread更容易發生ABBA的測試方法,來測試該LFQUEUE有無此弱點 @copilot

Co-authored-by: darkautism <3898040+darkautism@users.noreply.github.com>
Copy link
Author

Copilot AI commented Feb 21, 2026

設計一款更在multithread更容易發生ABBA的測試方法,來測試該LFQUEUE有無此弱點 @copilot

Added test_aba.c in 32a815f. It's wired into make test via the Makefile.

The test maximises ABA exposure compared to the existing tests by:

  • All threads simultaneously produce AND consume (no separation) — tight enqueue → spin-dequeue loops with no yield, keeping the queue depth near 0–1 items
  • Rapid malloc/free of uniform small objects — causes the allocator to hand back the same addresses frequently, creating the classic A→B→A pointer value cycle on head
  • 16 competing threads (configurable via ABA_THREADS) all racing on the head CAS

Each node carries a LIVE_MAGIC value on enqueue, which is poisoned to DEAD_MAGIC before free(). Any ABA-induced use-after-free or stale-pointer read surfaces immediately as a magic mismatch and is reported as a failure.

The queue passes the test (8 000 000 ops, no corruption detected), confirming the hazard pointer scheme correctly prevents ABA.

Copilot AI changed the title Fix lock-free queue bugs and add GitHub Actions CI Fix lock-free queue bugs, add GitHub Actions CI, and add ABA stress test Feb 21, 2026
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