Skip to content

Fix SWAR underscore-caret borrow and out-of-bounds pointer arithmetic… - #4445

Merged
kddnewton merged 2 commits into
ruby:mainfrom
stevenjohnstone:sjj/fix-swar-identifier-scan
Nov 24, 2026
Merged

kddnewton merged 2 commits into
ruby:mainfrom
stevenjohnstone:sjj/fix-swar-identifier-scan

Conversation

@stevenjohnstone

Copy link
Copy Markdown
Contributor

… in scan_identifier_ascii

  • Replace (xor_us - ones) & ~xor_us & highs with the borrow-free ~((xor_us + ones * 0x7F) | xor_us) & highs in SWAR scan_identifier_ascii, preventing '_' (0x5F) from borrowing into an immediately following '^' (0x5E)
  • Replace cursor + 16 <= end (NEON/SSE2) and cursor + 8 <= end (SWAR) with end - cursor >= 16 / end - cursor >= 8 to avoid forming out-of-bounds pointers
  • Add PRISM_ACCEL_SWAR override in include/prism/compiler/accel.h so SWAR can be compiled and tested on little-endian NEON/SSE2 hosts
  • Add test/cbmc/scan_identifier_ascii.c and make test-cbmc to verify the SWAR contract and pointer bounds over symbolic 0..16-byte inputs, plus regression cases in test/prism/lex_test.rb

Fixes #4444

stevenjohnstone and others added 2 commits November 19, 2026 12:19
… in scan_identifier_ascii

- Replace (xor_us - ones) & ~xor_us & highs with the borrow-free ~((xor_us + ones * 0x7F) | xor_us) & highs in SWAR scan_identifier_ascii, preventing '_' (0x5F) from borrowing into an immediately following '^' (0x5E)
- Replace cursor + 16 <= end (NEON/SSE2) and cursor + 8 <= end (SWAR) with end - cursor >= 16 / end - cursor >= 8 to avoid forming out-of-bounds pointers
- Add PRISM_ACCEL_SWAR override in include/prism/compiler/accel.h so SWAR can be compiled and tested on little-endian NEON/SSE2 hosts
- Add test/cbmc/scan_identifier_ascii.c and make test-cbmc to verify the SWAR contract and pointer bounds over symbolic 0..16-byte inputs, plus regression cases in test/prism/lex_test.rb

@kddnewton kddnewton 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.

Thanks for the fix! I didn't want to pull in a dep, so I added a test for this particular case that will get picked up by the targets we have in CI that use the SWAR accel anyway.

@kddnewton
kddnewton merged commit 1d8f226 into ruby:main Nov 24, 2026
122 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.

Parsing with SWAR enabled (incorrectly) diverges from NEON

2 participants