Skip to content

Improve binary processing. Improve native CRC performance. Add benchmarks. - #3719

Open
ryanmelt wants to merge 4 commits into
mainfrom
binary_improvements
Open

Improve binary processing. Improve native CRC performance. Add benchmarks.#3719
ryanmelt wants to merge 4 commits into
mainfrom
binary_improvements

Conversation

@ryanmelt

Copy link
Copy Markdown
Member

No description provided.

@ryanmelt
ryanmelt requested a review from jmthomas August 16, 2026 04:25
@codecov

codecov Bot commented Aug 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.82353% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 79.32%. Comparing base (d43e1eb) to head (a09b776).

Files with missing lines Patch % Lines
openc3/lib/openc3/accessors/binary_accessor.rb 97.05% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3719      +/-   ##
==========================================
+ Coverage   79.30%   79.32%   +0.01%     
==========================================
  Files         885      885              
  Lines       65365    65447      +82     
  Branches     2543     2591      +48     
==========================================
+ Hits        51838    51914      +76     
- Misses      12859    12860       +1     
- Partials      668      673       +5     
Flag Coverage Δ
frontend 63.57% <ø> (-0.06%) ⬇️
python 81.56% <ø> (+0.03%) ⬆️
ruby-api 82.13% <ø> (-0.05%) ⬇️
ruby-backend 84.11% <98.82%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Reads of items outside an undersized buffer return nil again instead of
rejecting the packet, and Packet skips conversions, states and format
strings for those nil values. Variable bit sizes larger than the buffer
are only an error when the packet does not allow short buffers.

Harden the remaining integer overflow and unbounded allocation paths:
little endian bitfields must span bytes inside the buffer, negative bit
size and negative bit offset recalculations are done in 64 bits, and
writes beyond the native int range raise a buffer error rather than
RangeError. Mirror all of it in Python and add ALLOW_SHORT test
coverage for both languages.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Comment thread openc3/python/openc3/accessors/binary_accessor.py Fixed
jmthomas
jmthomas previously approved these changes Aug 17, 2026

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

ALLOW_SHORT was broken. I fixed and then ran a fuzz test on the existing logic and found another 2 other issues:

  1. LE bitfield exception allowed unbounded bit_size and CPU/memory DoS
  2. Signed overflow in the negative-bit_size recalculation (C only).

Comment thread openc3/lib/openc3/packets/structure.rb

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR strengthens OpenC3’s binary/packet processing correctness (especially around short/undersized buffers and integer-overflow edge cases), improves CRC performance in the pure-Ruby and Python implementations, and adds repeatable benchmarks to compare native vs pure implementations and Ruby vs Python.

Changes:

  • Add benchmark harnesses for C-extension vs pure Ruby throughput, plus a Ruby (YJIT on/off) vs Python comparison with correctness signatures.
  • Optimize CRC table generation and reflected CRC calculation paths (Ruby + Python), with expanded CRC correctness tests.
  • Harden packet/structure/binary accessor handling for short buffers and extreme offsets/sizes; add broad regression tests in both Ruby and Python.

Reviewed changes

Copilot reviewed 22 out of 22 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
openc3/test/benchmarks/ruby_python_benchmark.py New Ruby (YJIT on/off) vs Python benchmark runner with per-case correctness signatures.
openc3/test/benchmarks/README.md Documents running benchmarks and supported environment variables/filters.
openc3/test/benchmarks/c_extensions_benchmark.rb New benchmark runner comparing C extensions vs pure Ruby with correctness verification and summary stats.
openc3/spec/utilities/crc_spec.rb Adds Ruby spec coverage for Crc32#calc using default seed when seed is nil.
openc3/spec/packets/structure_spec.rb Adds Ruby specs covering short-buffer and variable-size edge cases (including overflow-adjacent offsets).
openc3/spec/packets/packet_spec.rb Adds Ruby specs ensuring conversions/formatting/states don’t apply to nil values for short buffers.
openc3/spec/accessors/binary_accessor_spec.rb Adds Ruby specs for little-endian bitfield bounds and overflow scenarios.
openc3/python/test/utilities/test_crc.py Expands Python CRC tests and adds an “old algorithm” oracle for table/implementation equivalence.
openc3/python/test/packets/test_structure.py Adds Python tests mirroring Ruby short-buffer and variable-size behaviors.
openc3/python/test/packets/test_packet.py Adds Python tests for short-buffer behavior in Packet.read/limits/identify.
openc3/python/test/accessors/test_binary_accessor_write.py Adds Python tests for little-endian bitfield write bounds.
openc3/python/test/accessors/test_binary_accessor_read.py Adds Python tests for little-endian bitfield read bounds and negative-size overflow edge cases.
openc3/python/openc3/utilities/crc.py CRC init/calc optimizations (precomputed masks/shifts, reflected tables, faster reflected loop).
openc3/python/openc3/packets/structure.py Validates variable bit sizes against available buffer bits; aligns error behavior with Ruby.
openc3/python/openc3/packets/packet.py Avoids applying conversions/states/formatting when reading None from a short buffer.
openc3/python/openc3/accessors/binary_accessor.py Validates variable bit sizes; prevents out-of-range/negative derived sizing from reaching low-level access; tightens bounds checks.
openc3/lib/openc3/utilities/crc.rb Pure-Ruby CRC performance improvements via cached params and reflected-table fast path.
openc3/lib/openc3/packets/structure.rb Adds buffer-aware validation for variable bit sizes (mirrors Python behavior).
openc3/lib/openc3/packets/packet.rb Avoids applying conversions/states/formatting when reading nil from a short buffer.
openc3/lib/openc3/accessors/binary_accessor.rb Adds variable-size validation, native-int range checks, and improved bounds checks for short buffers.
openc3/ext/openc3/ext/structure/structure.c Hardens bounds/size calculations against integer overflow; improves safety checks before allocations.
openc3/ext/openc3/ext/platform/platform.c Replaces function-scope const int with macro for fixed-size filename buffer.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread openc3/lib/openc3/accessors/binary_accessor.rb
Comment thread openc3/python/openc3/accessors/binary_accessor.py
Comment thread openc3/ext/openc3/ext/structure/structure.c
Assert the last accepted and first rejected little endian bitfield spans
with their values so an off-by-one in the pre-check fails loudly instead
of silently reading nil.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@sonarqubecloud

Copy link
Copy Markdown

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