-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CustomOp Lite example leads to use-after-free #17547
Comments
@RandySheriff - can you take a look? |
If understand it correctly - the management of the lifetime of custom ops could be in a way that is appropriate to custom application. |
I think the |
The docs do say this. C API:
C++ API:
Lite Custom ops API (PR recently merged): |
If you're considering this documented, then I strongly urge you to update the example. I followed the linked example and encountered this. (But if it's reasonable for you to fix the unsafety, even better imo, as the resulting crash is nondeterministic, deep in onnxruntime, and not easily debuggable even with symbols)
|
Describe the issue
Starting from a9df3ae
Using the
OrtLiteCustomOp
example from https://onnxruntime.ai/docs/reference/operators/add-custom-op.html leads to a use-after-free if your session lasts longer than the scope used to set it up.v1_domain.Add()
takes an unsafe interior pointer from thestd::unique_ptr
usingcustom_op_one.get()
. If the unique_ptr falls out of scope and is deallocated while the ort session is still alive, calling the op will result in a use-after-free and crash onnxruntime.It's trivial to encounter this if you construct an ort session in a wrapper class and reuse it repeatedly by calling a method on the class - the op will fall out of scope immediately and crash. IMO the domain should take ownership or a copy of the custom op if possible, the caller shouldn't be trusted with an implicit contract to keep the op around for longer than the session.
The tests don't encounter this because they use the ort session in the same scope as the unique_ptr.
I wasted a couple days debugging this.
To reproduce
Follow the custom op lite example, but put it in a scope block so the unique_ptr gets dropped immediately:
Urgency
Ideally this is evaluated before 1.16.0 is actually released with custom ops, in case a breaking api change is needed.
Platform
Mac
OS Version
13.5
ONNX Runtime Installation
Built from Source
ONNX Runtime Version or Commit ID
a9df3ae
ONNX Runtime API
C++
Architecture
ARM64
Execution Provider
Default CPU
Execution Provider Library Version
No response
The text was updated successfully, but these errors were encountered: