Skip to content

libsimlin: TOCTOU race in apply_project_patch_internal unit warnings check #296

Closed

Description

@bpowers

Problem

In src/libsimlin/src/patch.rs, the apply_project_patch_internal function has a time-of-check to time-of-use (TOCTOU) race condition between two separate lock acquisitions on project_ref.project:

  1. Lines 415-418: Lock to collect existing unit warnings
  2. Lines 420-423: Lock to clone the datamodel

Between these two acquisitions, another thread could commit a different patch, making the baseline of existing warnings stale. This could cause a patch to be incorrectly accepted when it actually introduces new unit warnings.

Why It Matters

This is a correctness issue in the patch validation logic. A patch validation decision depends on the state at warning-collection time, but the actual datamodel state at patch-application time may differ due to concurrent patches. This could silently allow invalid unit warnings to be committed.

Affected Component

  • src/libsimlin/src/patch.rs - apply_project_patch_internal function (lines ~415-423)

Solution

Combine both lock acquisitions into a single scope:

let (models_with_existing_warnings, mut staged_datamodel) = {
    let project_locked = project_ref.project.lock().unwrap();
    let warnings = collect_models_with_unit_warnings(&project_locked);
    let datamodel = project_locked.datamodel.clone();
    (warnings, datamodel)
};

This ensures the warning baseline and datamodel are captured atomically from the same locked state.

Context

Identified during code review of PR #289 (incremental compilation implementation).

Activity

  1. bpowers commented on Feb 24, 2026

    @bpowers
    OwnerAuthor

    Fixed in PR #289 (incremental compilation). Regression test added in PR #314.

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

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions