WebNN: Use raw_ref for WebNNContextImpl in WebNNTensorImpl [chromium/src : main]

0 views
Skip to first unread message

Bernhart, Bryan (Gerrit)

unread,
Feb 27, 2026, 12:43:15 PM (13 hours ago) Feb 27
to Reilly Grant, Chromium LUCI CQ, chromium...@chromium.org, Hu, Ningxin, Jiewei Qian, mac-r...@chromium.org
Attention needed from Reilly Grant

Bernhart, Bryan added 1 comment

Patchset-level comments
File-level comment, Patchset 12 (Latest):
Bernhart, Bryan . resolved

PTAL.

Open in Gerrit

Related details

Attention is currently required from:
  • Reilly Grant
Submit Requirements:
  • requirement satisfiedCode-Coverage
  • requirement is not satisfiedCode-Owners
  • requirement is not satisfiedCode-Review
  • requirement is not satisfiedReview-Enforcement
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: chromium/src
Gerrit-Branch: main
Gerrit-Change-Id: I4eee83b2be73bd37bc18908d86ab8fa13b07a950
Gerrit-Change-Number: 7609411
Gerrit-PatchSet: 12
Gerrit-Owner: Bernhart, Bryan <bryan.b...@intel.com>
Gerrit-Reviewer: Bernhart, Bryan <bryan.b...@intel.com>
Gerrit-Reviewer: Reilly Grant <rei...@chromium.org>
Gerrit-CC: Hu, Ningxin <ningx...@intel.com>
Gerrit-CC: Jiewei Qian <q...@chromium.org>
Gerrit-Attention: Reilly Grant <rei...@chromium.org>
Gerrit-Comment-Date: Fri, 27 Feb 2026 17:43:09 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
satisfied_requirement
unsatisfied_requirement
open
diffy

Reilly Grant (Gerrit)

unread,
Feb 27, 2026, 2:39:46 PM (11 hours ago) Feb 27
to Bernhart, Bryan, Reilly Grant, Chromium LUCI CQ, chromium...@chromium.org, Hu, Ningxin, Jiewei Qian, mac-r...@chromium.org
Attention needed from Bernhart, Bryan

Reilly Grant added 1 comment

File services/webnn/webnn_tensor_impl.h
Line 28, Patchset 12 (Latest): : public WebNNObjectImpl<mojom::WebNNTensor,
Reilly Grant . unresolved

Can this be changed to `WebNNObjectBase` so that this class no longer uses reference counting?

Otherwise this patch is depending too much on the promises made by comments and the MiraclePtr checks for correctness.

Open in Gerrit

Related details

Attention is currently required from:
  • Bernhart, Bryan
Submit Requirements:
    • requirement satisfiedCode-Coverage
    • requirement is not satisfiedCode-Owners
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: chromium/src
    Gerrit-Branch: main
    Gerrit-Change-Id: I4eee83b2be73bd37bc18908d86ab8fa13b07a950
    Gerrit-Change-Number: 7609411
    Gerrit-PatchSet: 12
    Gerrit-Owner: Bernhart, Bryan <bryan.b...@intel.com>
    Gerrit-Reviewer: Bernhart, Bryan <bryan.b...@intel.com>
    Gerrit-Reviewer: Reilly Grant <rei...@chromium.org>
    Gerrit-CC: Hu, Ningxin <ningx...@intel.com>
    Gerrit-CC: Jiewei Qian <q...@chromium.org>
    Gerrit-Attention: Bernhart, Bryan <bryan.b...@intel.com>
    Gerrit-Comment-Date: Fri, 27 Feb 2026 19:39:40 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    satisfied_requirement
    unsatisfied_requirement
    open
    diffy

    Bernhart, Bryan (Gerrit)

    unread,
    Feb 27, 2026, 5:38:04 PM (8 hours ago) Feb 27
    to Reilly Grant, Chromium LUCI CQ, chromium...@chromium.org, Hu, Ningxin, Jiewei Qian, mac-r...@chromium.org
    Attention needed from Reilly Grant

    Bernhart, Bryan added 1 comment

    File services/webnn/webnn_tensor_impl.h
    Line 28, Patchset 12 (Latest): : public WebNNObjectImpl<mojom::WebNNTensor,
    Reilly Grant . unresolved

    Can this be changed to `WebNNObjectBase` so that this class no longer uses reference counting?

    Otherwise this patch is depending too much on the promises made by comments and the MiraclePtr checks for correctness.

    Bernhart, Bryan

    `WebNNTensorImpl` must remain ref-counted because it is passed into `gpu::Scheduler` tasks. These tasks need to extend the lifetime of the tensor until execution completes on the GPU sequence.

    Moving `WebNNObjectBase` to unique ownership would force the scheduler to use `base::Unretained` or raw pointers, which is significantly more dangerous than `scoped_refptr`.

    Switching to `raw_ref` moves us away from "promises made by comments" because it makes the relationship a compiler-enforced invariant. Currently, using `WeakPtr` allows for potential undefined behavior if a null-check is missed, whereas `raw_ref` would always `CHECK`.

    We can continue to use `AsWeakPtr()` for asynchronous tasks (TFLite & CoreML backends) while using the `raw_ref` for synchronous logic where the context is guaranteed to be alive (DML & ORT backends). WDYT?

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Reilly Grant
    Submit Requirements:
    • requirement satisfiedCode-Coverage
    • requirement is not satisfiedCode-Owners
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: chromium/src
    Gerrit-Branch: main
    Gerrit-Change-Id: I4eee83b2be73bd37bc18908d86ab8fa13b07a950
    Gerrit-Change-Number: 7609411
    Gerrit-PatchSet: 12
    Gerrit-Owner: Bernhart, Bryan <bryan.b...@intel.com>
    Gerrit-Reviewer: Bernhart, Bryan <bryan.b...@intel.com>
    Gerrit-Reviewer: Reilly Grant <rei...@chromium.org>
    Gerrit-CC: Hu, Ningxin <ningx...@intel.com>
    Gerrit-CC: Jiewei Qian <q...@chromium.org>
    Gerrit-Attention: Reilly Grant <rei...@chromium.org>
    Gerrit-Comment-Date: Fri, 27 Feb 2026 22:37:59 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Comment-In-Reply-To: Reilly Grant <rei...@chromium.org>
    satisfied_requirement
    unsatisfied_requirement
    open
    diffy

    Reilly Grant (Gerrit)

    unread,
    Feb 27, 2026, 7:04:25 PM (7 hours ago) Feb 27
    to Bernhart, Bryan, Reilly Grant, Chromium LUCI CQ, chromium...@chromium.org, Hu, Ningxin, Jiewei Qian, mac-r...@chromium.org
    Attention needed from Bernhart, Bryan

    Reilly Grant added 2 comments

    File services/webnn/webnn_tensor_impl.h
    Line 28, Patchset 12 (Latest): : public WebNNObjectImpl<mojom::WebNNTensor,
    Reilly Grant . resolved

    Can this be changed to `WebNNObjectBase` so that this class no longer uses reference counting?

    Otherwise this patch is depending too much on the promises made by comments and the MiraclePtr checks for correctness.

    Bernhart, Bryan

    `WebNNTensorImpl` must remain ref-counted because it is passed into `gpu::Scheduler` tasks. These tasks need to extend the lifetime of the tensor until execution completes on the GPU sequence.

    Moving `WebNNObjectBase` to unique ownership would force the scheduler to use `base::Unretained` or raw pointers, which is significantly more dangerous than `scoped_refptr`.

    Switching to `raw_ref` moves us away from "promises made by comments" because it makes the relationship a compiler-enforced invariant. Currently, using `WeakPtr` allows for potential undefined behavior if a null-check is missed, whereas `raw_ref` would always `CHECK`.

    We can continue to use `AsWeakPtr()` for asynchronous tasks (TFLite & CoreML backends) while using the `raw_ref` for synchronous logic where the context is guaranteed to be alive (DML & ORT backends). WDYT?

    Reilly Grant

    Ack. Let me just suggest some comment updates to better explain what's happening here. Note that this is not compiler-enforced. As mentioned in the suggested comment above this adds a runtime check to debug builds and if the invariant is broken in a production build MiraclePtr only protects against the resulting use-after-free being exploitable.

    Line 24, Patchset 12 (Latest):// GPU process implementation of the MLTensor interface. A WebNNTensorImpl is
    // always associated with a WebNNContextImpl and is guaranteed to be outlived by
    // that context.
    Reilly Grant . unresolved

    ```suggestion
    // GPU process implementation of the `MLTensor` interface. While this class is
    // reference-counted a `WebNNTensorImpl` is guaranteed not to outlive the
    // `WebNNContextImpl` that created it because references are only held by tasks
    // posted to the `gpu::SchedulerTaskRunner` that is shut down when the context is
    // destroyed.
    //
    // This invariant is checked by the `raw_ref<WebNNContextImpl>` member, which will
    // trigger dangling pointer warnings in debug builds and safe crashes in release
    // builds.
    ```

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Bernhart, Bryan
    Submit Requirements:
    • requirement satisfiedCode-Coverage
    • requirement is not satisfiedCode-Owners
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: chromium/src
    Gerrit-Branch: main
    Gerrit-Change-Id: I4eee83b2be73bd37bc18908d86ab8fa13b07a950
    Gerrit-Change-Number: 7609411
    Gerrit-PatchSet: 12
    Gerrit-Owner: Bernhart, Bryan <bryan.b...@intel.com>
    Gerrit-Reviewer: Bernhart, Bryan <bryan.b...@intel.com>
    Gerrit-Reviewer: Reilly Grant <rei...@chromium.org>
    Gerrit-CC: Hu, Ningxin <ningx...@intel.com>
    Gerrit-CC: Jiewei Qian <q...@chromium.org>
    Gerrit-Attention: Bernhart, Bryan <bryan.b...@intel.com>
    Gerrit-Comment-Date: Sat, 28 Feb 2026 00:04:19 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Comment-In-Reply-To: Bernhart, Bryan <bryan.b...@intel.com>
    Comment-In-Reply-To: Reilly Grant <rei...@chromium.org>
    satisfied_requirement
    unsatisfied_requirement
    open
    diffy

    Reilly Grant (Gerrit)

    unread,
    Feb 27, 2026, 7:10:40 PM (7 hours ago) Feb 27
    to Bernhart, Bryan, Alex Gough, Reilly Grant, Chromium LUCI CQ, chromium...@chromium.org, Hu, Ningxin, Jiewei Qian, mac-r...@chromium.org
    Attention needed from Bernhart, Bryan

    Reilly Grant added 1 comment

    File services/webnn/webnn_tensor_impl.h
    Line 28, Patchset 12 (Latest): : public WebNNObjectImpl<mojom::WebNNTensor,
    Reilly Grant . resolved

    Can this be changed to `WebNNObjectBase` so that this class no longer uses reference counting?

    Otherwise this patch is depending too much on the promises made by comments and the MiraclePtr checks for correctness.

    Bernhart, Bryan

    `WebNNTensorImpl` must remain ref-counted because it is passed into `gpu::Scheduler` tasks. These tasks need to extend the lifetime of the tensor until execution completes on the GPU sequence.

    Moving `WebNNObjectBase` to unique ownership would force the scheduler to use `base::Unretained` or raw pointers, which is significantly more dangerous than `scoped_refptr`.

    Switching to `raw_ref` moves us away from "promises made by comments" because it makes the relationship a compiler-enforced invariant. Currently, using `WeakPtr` allows for potential undefined behavior if a null-check is missed, whereas `raw_ref` would always `CHECK`.

    We can continue to use `AsWeakPtr()` for asynchronous tasks (TFLite & CoreML backends) while using the `raw_ref` for synchronous logic where the context is guaranteed to be alive (DML & ORT backends). WDYT?

    Reilly Grant

    Ack. Let me just suggest some comment updates to better explain what's happening here. Note that this is not compiler-enforced. As mentioned in the suggested comment above this adds a runtime check to debug builds and if the invariant is broken in a production build MiraclePtr only protects against the resulting use-after-free being exploitable.

    Reilly Grant

    CC @aj...@chromium.org to confirm my understanding and sign off on this from a Security perspective.

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Bernhart, Bryan
    Submit Requirements:
    • requirement satisfiedCode-Coverage
    • requirement is not satisfiedCode-Owners
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: chromium/src
    Gerrit-Branch: main
    Gerrit-Change-Id: I4eee83b2be73bd37bc18908d86ab8fa13b07a950
    Gerrit-Change-Number: 7609411
    Gerrit-PatchSet: 12
    Gerrit-Owner: Bernhart, Bryan <bryan.b...@intel.com>
    Gerrit-Reviewer: Bernhart, Bryan <bryan.b...@intel.com>
    Gerrit-Reviewer: Reilly Grant <rei...@chromium.org>
    Gerrit-CC: Alex Gough <aj...@chromium.org>
    Gerrit-CC: Hu, Ningxin <ningx...@intel.com>
    Gerrit-CC: Jiewei Qian <q...@chromium.org>
    Gerrit-Attention: Bernhart, Bryan <bryan.b...@intel.com>
    Gerrit-Comment-Date: Sat, 28 Feb 2026 00:10:35 +0000
    satisfied_requirement
    unsatisfied_requirement
    open
    diffy

    Hu, Ningxin (Gerrit)

    unread,
    Feb 27, 2026, 9:24:43 PM (5 hours ago) Feb 27
    to Bernhart, Bryan, Alex Gough, Reilly Grant, Chromium LUCI CQ, chromium...@chromium.org, Jiewei Qian, mac-r...@chromium.org
    Attention needed from Bernhart, Bryan

    Hu, Ningxin added 1 comment

    File services/webnn/webnn_tensor_impl.h
    Line 24, Patchset 12 (Latest):// GPU process implementation of the MLTensor interface. A WebNNTensorImpl is
    // always associated with a WebNNContextImpl and is guaranteed to be outlived by
    // that context.
    Reilly Grant . unresolved

    ```suggestion
    // GPU process implementation of the `MLTensor` interface. While this class is
    // reference-counted a `WebNNTensorImpl` is guaranteed not to outlive the
    // `WebNNContextImpl` that created it because references are only held by tasks
    // posted to the `gpu::SchedulerTaskRunner` that is shut down when the context is
    // destroyed.
    //
    // This invariant is checked by the `raw_ref<WebNNContextImpl>` member, which will
    // trigger dangling pointer warnings in debug builds and safe crashes in release
    // builds.
    ```

    Hu, Ningxin

    // a `WebNNTensorImpl` is guaranteed not to outlive the


    // `WebNNContextImpl` that created it because references are only held by tasks
    // posted to the `gpu::SchedulerTaskRunner` that is shut down when the context is
    // destroyed.

    Is it guaranteed for backends which still use `ResourceTask`? including CoreML and TFlite backends.

    Gerrit-Comment-Date: Sat, 28 Feb 2026 02:24:36 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Comment-In-Reply-To: Reilly Grant <rei...@chromium.org>
    satisfied_requirement
    unsatisfied_requirement
    open
    diffy

    Reilly Grant (Gerrit)

    unread,
    1:07 AM (1 hour ago) 1:07 AM
    to Bernhart, Bryan, Alex Gough, Reilly Grant, Chromium LUCI CQ, chromium...@chromium.org, Hu, Ningxin, Jiewei Qian, mac-r...@chromium.org
    Attention needed from Bernhart, Bryan

    Reilly Grant added 1 comment

    File services/webnn/webnn_tensor_impl.h
    Line 24, Patchset 12 (Latest):// GPU process implementation of the MLTensor interface. A WebNNTensorImpl is
    // always associated with a WebNNContextImpl and is guaranteed to be outlived by
    // that context.
    Reilly Grant . unresolved

    ```suggestion
    // GPU process implementation of the `MLTensor` interface. While this class is
    // reference-counted a `WebNNTensorImpl` is guaranteed not to outlive the
    // `WebNNContextImpl` that created it because references are only held by tasks
    // posted to the `gpu::SchedulerTaskRunner` that is shut down when the context is
    // destroyed.
    //
    // This invariant is checked by the `raw_ref<WebNNContextImpl>` member, which will
    // trigger dangling pointer warnings in debug builds and safe crashes in release
    // builds.
    ```

    Hu, Ningxin

    // a `WebNNTensorImpl` is guaranteed not to outlive the
    // `WebNNContextImpl` that created it because references are only held by tasks
    // posted to the `gpu::SchedulerTaskRunner` that is shut down when the context is
    // destroyed.

    Is it guaranteed for backends which still use `ResourceTask`? including CoreML and TFlite backends.

    Reilly Grant

    Yes, the `ResourceTask` bit has its own referencing counting that doesn't depend on the lifetime of the `WebNNTensorImpl`.

    Gerrit-Comment-Date: Sat, 28 Feb 2026 06:07:26 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Comment-In-Reply-To: Hu, Ningxin <ningx...@intel.com>
    Comment-In-Reply-To: Reilly Grant <rei...@chromium.org>
    satisfied_requirement
    unsatisfied_requirement
    open
    diffy
    Reply all
    Reply to author
    Forward
    0 new messages