Skip to content

Fix possible use-after-free calls in C#/Java bindings #405

Description

@jadamcrain

FFI wrappers (Java/C#) can cause use-after-free crashes when methods are called after shutdown(). Current design frees the wrapper memory in shutdown() instead of the finalizer.

Proposed Solution (2.0)

Add explicit shutdown message, separate from finalization

  1. Add a Shutdown variant to the MasterMsg enum
  2. Add a shutdown() method to dnp3::master::MasterChannel that sends this message
  3. Create separate FFI functions:
    • master_channel_shutdown(): sends shutdown message, doesn't free wrapper
    • master_channel_destroy(): only frees wrapper, only called by finalizer
  4. Update Java/C# shutdown() to call the new shutdown function instead of destroy

Why This Works

No leaks:

Finalizer always runs eventually and frees the wrapper. If shutdown is never called, dropping the Sender when finalizer runs will still terminate the task.

No extra synchronization:

Uses existing channel semantics. After shutdown message is sent, the task terminates and drops the receiver. Future operations fail cleanlywhen send detects the closed channel.

No use-after-free: Wrapper stays valid until finalizer runs. Methods can be called but fail with clear Shutdown errors.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions