Skip to content

Fix ASAN thread race with TLS dynamic initialization of variable_registry#1107

Merged
elazarg merged 1 commit intovbpf:mainfrom
saxena-anurag:user/anusa/asan_fix
May 3, 2026
Merged

Fix ASAN thread race with TLS dynamic initialization of variable_registry#1107
elazarg merged 1 commit intovbpf:mainfrom
saxena-anurag:user/anusa/asan_fix

Conversation

@saxena-anurag
Copy link
Copy Markdown
Contributor

@saxena-anurag saxena-anurag commented Apr 30, 2026

Problem

When running under AddressSanitizer (ASan), extra threads are spawned before the CRT debug locks are fully initialized. The thread_local VariableRegistry variable_registry global variable triggered TLS dynamic-init callbacks that could race with CRT startup on these early threads, leading to heap-buffer-overflow or other undefined behavior.

Solution

Replace the file-scope thread_local variable with a function-local thread_local inside a getter function (get_variable_registry()). This defers construction until the first actual use rather than running during TLS dynamic initialization, avoiding the race condition with early-spawned threads.

A backward-compatible #define variable_registry macro alias is provided so all existing call sites continue to work without modification.

Changes

  • src/crab/var_registry.hpp: Replace extern thread_local VariableRegistry variable_registry with VariableRegistry& get_variable_registry() declaration and a backward-compatible macro alias.
  • src/crab/var_registry.cpp: Move the thread_local instance inside a function-local scope in get_variable_registry().

Testing

All 1533 tests pass (1295 passed, 1 skipped, 237 failed as expected — no change from baseline).

Signed-off-by: Anurag Saxena <anusa@microsoft.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 30, 2026

📝 Walkthrough

Walkthrough

Refactors the global thread-local VariableRegistry from a translation-unit scoped variable to a function-local accessor pattern. Introduces get_variable_registry() function for deferred initialization, with a macro alias for backward compatibility.

Changes

Cohort / File(s) Summary
Variable Registry Thread-Local Refactoring
src/crab/var_registry.hpp, src/crab/var_registry.cpp
Replaces extern thread_local VariableRegistry variable_registry with get_variable_registry() function returning deferred-initialized thread-local instance. Adds macro alias variable_registry mapping to get_variable_registry() for backward compatibility. Updates operator<< to use the new accessor.
🚥 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 The description clearly explains the problem, solution, code changes, and test results, all directly related to the changeset.
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.
Title check ✅ Passed The title accurately summarizes the main change: replacing a file-scoped thread_local variable with a function-local getter to resolve an ASAN thread race condition during TLS dynamic initialization.

✏️ 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.

@saxena-anurag saxena-anurag changed the title [DRAFT: DO NOT REVIEW] ASAN Fix [DRAFT: DO NOT REVIEW] Fix ASan thread race with TLS dynamic initialization of variable_registry Apr 30, 2026
@saxena-anurag
Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 30, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@saxena-anurag saxena-anurag changed the title [DRAFT: DO NOT REVIEW] Fix ASan thread race with TLS dynamic initialization of variable_registry Fix ASan thread race with TLS dynamic initialization of variable_registry May 1, 2026
@saxena-anurag saxena-anurag marked this pull request as ready for review May 1, 2026 01:32
@saxena-anurag saxena-anurag changed the title Fix ASan thread race with TLS dynamic initialization of variable_registry Fix ASAN thread race with TLS dynamic initialization of variable_registry May 1, 2026
@elazarg elazarg merged commit f39732c into vbpf:main May 3, 2026
16 checks passed
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