Skip to content

UBSan CI abort in iree_vm_buffer_deinitialize #24331

Closed

Description

@bjacob

CI failure: https://github.com/iree-org/iree/actions/runs/25186189597/job/73844554381?pr=24329

Config: UBSan.
Test:

iree-run-mlir --Xcompiler,iree-input-type=stablehlo --Xcompiler,iree-hal-target-device=local --Xcompiler,iree-hal-local-target-device-backends=vmvx /__w/iree/iree/tests/e2e/stablehlo_models/fragment_000.mlir 

Stack:

   #7 0x00005648f745b649 iree_abort /__w/iree/iree/runtime/src/iree/base/assert.h:26:63
   #8 0x00005648f745b5c0 iree_vm_buffer_deinitialize /__w/iree/iree/runtime/src/iree/vm/buffer.c:79:3
   #9 0x00005648f73810b3 iree_vm_bytecode_module_destroy /__w/iree/iree/runtime/src/iree/vm/bytecode/module.c:171:49
  #10 0x00005648f74617b2 iree_vm_context_release_modules /__w/iree/iree/runtime/src/iree/vm/context.c:0:7
  #11 0x00005648f745dfc7 iree_vm_context_destroy /__w/iree/iree/runtime/src/iree/vm/context.c:476:16
  #12 0x00005648f709325d iree_tooling_run_module_with_data /__w/iree/iree/runtime/src/iree/tooling/run_module.c:476:7

The code at iree/runtime/src/iree/vm/buffer.c:79 is:

iree_atomic_ref_count_abort_if_uses(&buffer->ref_object.counter);

So apparently we take the abort branch here:

// Aborts the program if the given reference count value is not 1.
// This should be avoided in all situations but those where continuing execution
// would be invalid. If a reference object is allocated on the stack and the
// parent function is about to return it *must* have a ref count of 1: anything
// else that may be retaining the object will hold a pointer to (effectively)
// uninitialized stack memory.
#define iree_atomic_ref_count_abort_if_uses(count_ptr) \
if (IREE_UNLIKELY(iree_atomic_ref_count_load(count_ptr) != 1)) { \
iree_abort(); \
}

Activity

  1. added
    bug 🐞
    runtime
    on Apr 30, 2026
  2. bjacob commented on Apr 30, 2026

    @bjacob
    CollaboratorAuthor
  3. bjacob commented on Apr 30, 2026

    @bjacob
    CollaboratorAuthor

    Anyone debugging this: try reproducing this locally by running many times the test command in the issue description. Try to reproduce this with IREE_ENABLE_TSAN=ON, build type RelWithDebInfo.

  4. bjacob commented on May 1, 2026

    @bjacob
    CollaboratorAuthor

    Claude-generated diagnostic

    Symptom

    Running

    tools/iree-run-mlir \
      --Xcompiler,iree-input-type=stablehlo \
      --Xcompiler,iree-hal-target-device=local \
      --Xcompiler,iree-hal-local-target-device-backends=vmvx \
      tests/e2e/stablehlo_models/fragment_000.mlir
    

    intermittently aborts with iree_abort from iree_atomic_ref_count_abort_if_uses in iree_vm_buffer_deinitialize at module-destroy time. Stack:

    iree_abort                          runtime/src/iree/base/assert.h:26
    iree_vm_buffer_deinitialize         runtime/src/iree/vm/buffer.c:79
    iree_vm_bytecode_module_destroy     runtime/src/iree/vm/bytecode/module.c:171
    iree_vm_module_release              runtime/src/iree/vm/module.c:238
    iree_vm_context_release_modules     runtime/src/iree/vm/context.c:337
    iree_vm_context_destroy             runtime/src/iree/vm/context.c:482
    iree_vm_context_release             runtime/src/iree/vm/context.c:507
    iree_tooling_run_module_with_data   runtime/src/iree/tooling/run_module.c:476
    CompileAndRunFile                   tools/iree-run-mlir-main.cc:356
    main                                tools/iree-run-mlir-main.cc:538
    

    Output already shows the correct numerical result before aborting — the abort is during teardown only.

    Identifying the leaked buffer

    Patched iree_vm_buffer_deinitialize to print buffer details before aborting. Captured under chaos:

    [CHAOS-DEBUG] iree_vm_buffer_deinitialize:
      pid=2098697  buffer=0x72540001a2e0  data=0x7fffe1447cc0
      size=192  access=0x2  counter=2  (expected 1)
      head=[65 c2 5d 40 bf 9a f4 c0 a3 1e b7 c0 80 60 bf 40 ...]
    
    • size=192 is the _const composite rodata (5×5×f32 = 100 bytes + 5×1×f32 = 20 bytes + padding = 192).
    • head[0..3] = 0x405dc265 = 3.464990 matches the first element of the 5×5 matrix constant.
    • counter=2: under-release. Someone still holds a ref at module-destroy time.

    Root cause

    Use-after-signal in the local-task queue's operation-completion path, not a refcount race.

    iree_hal_task_queue_op_complete at runtime/src/iree/hal/drivers/local_task/task_queue.c:714-733:

    static void iree_hal_task_queue_op_complete(iree_hal_task_queue_op_t* operation) {
      iree_status_t status =
          iree_hal_task_queue_op_unmap_binding_mappings(operation, iree_ok_status());
      if (iree_status_is_ok(status)) {
        // (2) Signal: waiters wake up here.
        status = iree_hal_semaphore_list_signal(operation->signal_semaphores,
                                                /*frontier=*/NULL);
      }
      if (iree_status_is_ok(status)) {
        uint64_t epoch = iree_hal_task_queue_op_reserve_completion_epoch(operation);
        iree_hal_task_queue_op_advance_frontier(operation, epoch);
        iree_hal_task_queue_debug_record_complete(operation->queue, operation);
        // (4) Frees resource_set, which releases the retained command buffer
        //     and binding-table buffers.
        iree_hal_task_queue_op_destroy(operation, status);
      } else {
        iree_hal_task_queue_op_fail(operation, status);
      }
    }

    The resource_set retains the command buffer and the binding-table buffers (here: @__constant_tensor_5x5xf32, the hal.buffer wrapping the 192-byte rodata) for the duration of the dispatch. The signal at step (2) wakes the main thread's hal.fence.await. The main thread then races through:

    hal.fence.await returns
      → vm continues, returns
      → iree_vm_invoke returns
      → iree_vm_context_release → iree_vm_context_destroy
      → HAL module free_state           // releases @__constant_tensor_5x5xf32
                                        //          and @__entry_memoize_result_0_device_0
      → iree_vm_module_release on bytecode module
      → iree_vm_bytecode_module_destroy // iterates rodata_ref_table[]
      → iree_vm_buffer_deinitialize on the 192-byte _const buffer
      → iree_atomic_ref_count_abort_if_uses sees counter==2 → abort
    

    Meanwhile the worker thread is still between step (2) and step (4):

    • resource_set is still alive
    • The command buffer is still retained by it
    • The bindings still hold @__constant_tensor_5x5xf32
    • That hal.buffer still holds the import-callback ref to the 192-byte vm.buffer

    So the vm.buffer's counter is still 2 when the main thread looks.

    If the worker's step (4) wins the race, no abort. If the main thread's teardown wins, abort with counter=2.

    iree_hal_task_queue_op_complete_with_epoch at line 706 has the same pattern.

    Why TSan never reported a "data race"

    Every access on the shared refcount goes through iree_atomic_* ops, so it's properly synchronized at the memory-model level. The bug is a missing happens-before edge between "owning thread releases its retained resources" and "consumer thread observes completion" — the signal is published before the resources are released. TSan only reports unordered accesses to the same memory; here the ordering bug is at a higher abstraction level (resource ownership vs. semaphore signal), which TSan cannot detect.

    Suggested fix

    Free resource_set (and any other retained resource references) before signaling. Apply the same reorder to iree_hal_task_queue_op_complete_with_epoch.

     static void iree_hal_task_queue_op_complete(iree_hal_task_queue_op_t* operation) {
       iree_status_t status =
           iree_hal_task_queue_op_unmap_binding_mappings(operation, iree_ok_status());
    +  // Release retained resources before signaling so a thread waiting on the
    +  // signal cannot tear down dependent objects (modules, rodata, etc.) while
    +  // we still hold command-buffer / binding-table refs.
    +  if (operation->resource_set) {
    +    iree_hal_resource_set_free(operation->resource_set);
    +    operation->resource_set = NULL;
    +  }
       if (iree_status_is_ok(status)) {
         // Signal all semaphores to their new values.
         status = iree_hal_semaphore_list_signal(operation->signal_semaphores,
                                                 /*frontier=*/NULL);
       }
       if (iree_status_is_ok(status)) {
         uint64_t epoch = iree_hal_task_queue_op_reserve_completion_epoch(operation);
         iree_hal_task_queue_op_advance_frontier(operation, epoch);
         iree_hal_task_queue_debug_record_complete(operation->queue, operation);
         iree_hal_task_queue_op_destroy(operation, status);
       } else {
         iree_hal_task_queue_op_fail(operation, status);
       }
     }

    Reproduction statistics

    Setup Runs Aborts Rate
    32 parallel, baseline build 224 1 ~1/200
    64 parallel, baseline build ~700–1000 1 ~1/1000
    64 parallel, fprintf-instrumented 2240+ 0 masked
    128 parallel, instrumented 1920+ 0 masked
    256 parallel, instrumented 320 1 ~1/300
    256 parallel, instrumented 1280 1 ~1/1280

    Higher parallelism (256 = 8× oversubscription on a 32-core machine) reliably reproduces under instrumentation. The high CPU contention varies thread schedules enough to expose the race window between the worker's signal_semaphores and op_destroy.

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

Metadata

Metadata

Assignees

Labels

bug 🐞runtime

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions