Skip to content

Add ION-DTN host integration: memory allocators, BIB source fix, and WriteBTSD realloc#137

Open
iondev33 wants to merge 5 commits intoNASA-AMMOS:mainfrom
iondev33:bsl-ion-integration
Open

Add ION-DTN host integration: memory allocators, BIB source fix, and WriteBTSD realloc#137
iondev33 wants to merge 5 commits intoNASA-AMMOS:mainfrom
iondev33:bsl-ion-integration

Conversation

@iondev33
Copy link

@iondev33 iondev33 commented Mar 6, 2026

This PR enables BSL to be built and linked as part of the ION-DTN bundle protocol stack. It
adds an ION_INTEGRATION build option that redirects BSL's memory allocators
(BSL_MALLOC/BSL_CALLOC/BSL_REALLOC/BSL_FREE) to ION's shared working memory system, and
fixes two bugs discovered during end-to-end multinode testing with ION.

This is based on the work done initially by Scott Burleigh as identified in issue: #135 and expanded through debugging and testing process.

The changes in this PR will be required for ION 4.2.0-a.1 release which is integrated with BSL. Current a BSL fork with this branch is part of ION's submodule. ION automake system is updated to automatically build BSL with ION together. The ION 4.2.0-a.1 release is planned for mid-March release.

Changes

  1. ION memory allocator integration (ionpatch.c, ionpatch.h, CMake changes)

When built with -DION_INTEGRATION=ON, BSL uses ION's allocFromIonMemory() /
releaseToIonMemory() instead of the standard C allocator. This is required because ION
manages its own shared memory partition; mixing malloc/free with ION-allocated memory
causes munmap_chunk() crashes.

Files:

  • src/ION_integration/ionpatch.h — Redefines BSL_MALLOC/BSL_CALLOC/BSL_REALLOC/BSL_FREE
    macros to ION wrappers
  • src/ION_integration/ionpatch.c — Implements ion_malloc, ion_calloc, ion_realloc, ion_free
    using ION's memory API. Note: ion_realloc copies to a new block because ION's memory
    layout does not support in-place expansion
  • src/BSLConfig.h.in — Includes ionpatch.h before default allocator definitions when
    ION_INTEGRATION is defined
  • CMakeLists.txt — Adds ION_INTEGRATION option, finds ION headers/library, builds
    bsl_ionpatch static library, adds platform defines required by ION's platform.h
  • src/CMakeLists.txt — Links all BSL component libraries against bsl_ionpatch and libici
    when ION_INTEGRATION is enabled
  • build-for-ion.sh — Convenience script for building BSL with ION integration
  • src/BPSecLib_Private.h — Adds #ifndef guards around CHKVOID/CHKNULL macros to avoid
    redefinition conflicts with ION's platform.h

With this change, all BSL heap allocations go through ION's memory manager, eliminating the
cross-allocator corruption that occurred when ION passed its own memory to BSL and BSL
later called free() on it.

  1. Fix BSL_ExecBIBSource missing block number assignment (SecurityContext.c)

BSL_ExecBIBSource was not storing the newly created BIB block number in
sec_oper->sec_block_num after calling BSL_BundleCtx_CreateBlock. The security context
execution function needs this value to retrieve the block's metadata (BTSD buffer).
BSL_ExecBCBSource already had this assignment; this fix brings BIB in line.

Without this fix, BIB source operations fail because the security context cannot locate the
block it needs to write the integrity signature into.

  1. Fix BSL_BundleCtx_WriteBTSD to realloc before write (HostInterface.c)

BSL_BundleCtx_WriteBTSD was calling the host's write callback without first ensuring the
BTSD buffer was large enough. When ION creates extension blocks, they start with a 1-byte
placeholder. BSL would then attempt to write the full security result (e.g., 82 bytes for a
BIB HMAC signature) into that 1-byte buffer.

The fix calls block_realloc_btsd_fn before block_write_btsd_fn to expand the buffer to the
needed size. If realloc fails, the function returns NULL with an error log.

  1. Simplify BSL_PrimaryBlock_deinit (PublicInterfaceImpl.c)

Adds a NULL check before calling BSL_FREE(obj->block_numbers). With the ION memory
allocator integration, BSL_FREE routes to the correct allocator regardless of who allocated
the memory, so the previously added ownership-tracking field (block_numbers_owned) was
removed in favor of this simpler approach.

  1. Fix mock BPA test policy (policy_provider_test.json)
  • Change "loc" from "clin" to "appin" for source-role policy rules (source rules apply at
    application ingress, not convergence layer ingress)
  • Add "src": "ipn:2." and "dest": "ipn:3." EID filter patterns (previously missing,
    causing rules to not match any bundles)

Build & Test

Standard BSL build (no ION, unchanged behavior)

./build.sh

BSL build with ION integration

ION_ROOT=/path/to/ion ./build-for-ion.sh

Or manually:

./build.sh prep -DION_INTEGRATION=ON -DION_ROOT=/path/to/ion
./build.sh

Tested with ION's tests/bpsec/bpsec-all-multinode-test.bsl — 3-node topology (nodes 2, 3,
4) over LTP, 6 bundles with BIB+BCB, all tests passing.

Notes for reviewers

  • The 4 commits on this branch reflect the debugging sequence. Commit 1
    (block_numbers_owned) was a temporary fix that commit 3 (ION_INTEGRATION) superseded — the
    net diff to BPSecLib_Public.h and mock_bpa/agent.c is zero. Consider squash-merging if a
    cleaner history is preferred.
  • ion_realloc includes a detailed comment explaining why it copies to a new block rather
    than attempting in-place expansion — ION's memory partition does not support that.
  • The #ifndef CHKVOID/#ifndef CHKNULL guards are needed because ION's platform.h defines
    the same macro names.

Jay L. Gao added 4 commits February 19, 2026 17:28
   This commit fixes a critical memory corruption issue when BSL is integrated
   with ION-DTN. The problem occurred because BSL was calling free() on memory
   that ION allocated using BSL_CALLOC(), causing munmap_chunk() errors.

   Changes:
   - Add bool block_numbers_owned field to BSL_PrimaryBlock_t structure
     This follows the same ownership pattern as BSL_Data_t.owned

   - Modify BSL_PrimaryBlock_deinit() to check ownership before freeing
     Only calls BSL_FREE() on block_numbers if BSL owns the memory

   - Update BSL mock BPA to set ownership flag after allocation
     Ensures BSL-allocated memory is properly tracked

   - Fix policy configuration file (policy_provider_test.json):
     * Change location from "clin" to "appin" for source role policies
     * Fix JSON keys: "src_eid"/"dst_eid" -> "src"/"dest"
     * Add EID patterns: "ipn:2.*" and "ipn:3.*" for test scenarios

   This fix resolves the crash in ION's bpsec-all-multinode-test.bsl test
   where bundles with security blocks would fail with memory corruption errors.

   Related ION changes (in ion-ios-dev repository):
   - bpv7/bsl/bsl.c sets block_numbers_owned = true after BSL_CALLOC calls

   Tested-by: Running bpsec-all-multinode-test.bsl without crashes
  - Add ionpatch.h/c for ION memory allocator wrappers
  - Add ION_INTEGRATION build flag and CMake support
  - Remove block_numbers_owned field (simplified to unconditional free)
  - Add macro guards for CHKVOID/CHKNULL to avoid conflicts with ION
  - Add build-for-ion.sh convenience script
   When BSL creates a new BIB security block at the source node, it must store the created block number in the security operation structure so that the security context execution function can retrieve the block metadata. BSL_ExecBCBSource already had this assignment, but BSL_ExecBIBSource was missing it.
…MOS#2)

   Bug NASA-AMMOS#2: BSL_BundleCtx_WriteBTSD must call realloc callback before write

   Problem:
   - BSL_BundleCtx_WriteBTSD was calling the write callback directly without
     first ensuring the BTSD buffer was large enough
   - When ION creates extension blocks, they start with length=1 (placeholder)
   - BSL then attempted to write 82 bytes into the 1-byte buffer
   - This caused the ION realloc callback to be called during write, but write
     had already started with insufficient buffer space

   Root Cause:
   - BSL_BundleCtx_WriteBTSD (lines 131-137) immediately called:
       return HostDescriptorTable.block_write_btsd_fn(bundle, block_num, btsd_len);
   - No buffer size check or realloc call before writing
   - The write callback would fail when attempting to write beyond allocated space

   Fix:
   - Added realloc call before write in BSL_BundleCtx_WriteBTSD (lines 138-148)
   - Check if realloc callback is registered and btsd_len > 0
   - Call block_realloc_btsd_fn to expand buffer to needed size
   - Return NULL if realloc fails (with error logging)
   - Only proceed to write if realloc succeeds

   Code:
       /* Ensure the BTSD buffer is large enough before writing */
       if (btsd_len > 0 && HostDescriptorTable.block_realloc_btsd_fn)
       {
           int realloc_result = HostDescriptorTable.block_realloc_btsd_fn(bundle,
   block_num, btsd_len);
           if (realloc_result != 0)
           {
               BSL_LOG_ERR("Failed to realloc BTSD buffer: block=%llu size=%zu
   result=%d",
                           (unsigned long long)block_num, btsd_len, realloc_result);
               return NULL;
           }
       }

   Impact:
   - BSL now properly expands BTSD buffers before writing
   - BCB encryption can now write full encrypted payloads
   - BIB can write full HMAC signatures
   - This is part of a series of fixes enabling BSL BCB encryption in ION

   Related ION integration fixes (separate ION commits):
   - Bug NASA-AMMOS#3: Fix SDR violation in ion_bsl_ReallocBTSD
   - Bug NASA-AMMOS#4: Add payload block special handling in ion_bsl_ReallocBTSD

   Test: ION tests/bpsec/bpsec-all-multinode-test.bsl
   Status: BSL operations now succeed with this fix

WIP: bpsec/bpsec-all-multinode-test.bsl not yet passing.
@iondev33 iondev33 requested a review from a team as a code owner March 6, 2026 20:16
@github-project-automation github-project-automation bot moved this to Todo in BSL Mar 6, 2026
  When ION is built with autotools/libtool, the compiled libraries
  are placed in the .libs/ subdirectory of the build tree before
  installation. Add ${ION_ROOT}/.libs to the find_library hints
  so that in-tree builds can locate libici without requiring a
  prior make install.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

1 participant