Skip to content

fix(ext/ai): Invalid tensor length causing Segmentation Fault - #483

Merged
nyannyacha merged 4 commits into
supabase:mainfrom
kallebysantos:fix-ort-segmentation-fault
Feb 3, 2025
Merged

fix(ext/ai): Invalid tensor length causing Segmentation Fault#483
nyannyacha merged 4 commits into
supabase:mainfrom
kallebysantos:fix-ort-segmentation-fault

Conversation

@kallebysantos

@kallebysantos kallebysantos commented Feb 2, 2025

Copy link
Copy Markdown
Member

What kind of change does this PR introduce?

Bug fix, CRITICAL

What is the current behavior?

If a Tensor received from JS land haves data.length not compatible with the dims then when feeding the Session a Segmentation Fault: 11 error will be panic!.

How to reproduce it:

The tensors is successfully handled by rust as well JsTensor::extract_ort_input but error will be panic when feed this values into the Session.

const t1 = new Tensor("float32", [], [1] ); // This one always will result in panic!
const t2 = new Tensor("float32", [0.1, 0.2, 0.3], [1, 2] ); // Session may safely throws while validating inputs
// ...
await core.ops.op_sb_ai_ort_run_session("123", { t1, t2 });
./scripts/run.sh: line 13: 12963 Segmentation fault: 11  RUST_BACKTRACE=full ./target/debug/edge-runtime "$@" start --main-service ./examples/main --event-worker ./examples/event-manager

What is the new behavior?

Following the ort api reference: The 'N' of elements 'T' must be equal to the Tensor's 'dims product'.
So we must prevent it by checking if the Tensor length reflects in its dim shape

- ERROR: If a blank data `Tensor` is created and then used to feed
`Session` it will panic! with "Segmentation Fault: 11" error. So we must
prevent it by checking if the `Tensor` size reflects in its `dim shape`
@kallebysantos kallebysantos changed the title fix(sb_ai): Invalid tensor length causing Segmentation Fault fix(ext/sb_ai): Invalid tensor length causing Segmentation Fault Feb 2, 2025
@kallebysantos kallebysantos changed the title fix(ext/sb_ai): Invalid tensor length causing Segmentation Fault fix(ext/ai): Invalid tensor length causing Segmentation Fault Feb 2, 2025
@laktek
laktek requested a review from nyannyacha February 2, 2025 23:36
@laktek

laktek commented Feb 2, 2025

Copy link
Copy Markdown
Contributor

@nyannyacha can you take a look?

Comment thread ext/ai/onnxruntime/tensor.rs Outdated
@nyannyacha
nyannyacha merged commit 6a32308 into supabase:main Feb 3, 2025
@github-actions

github-actions Bot commented Feb 3, 2025

Copy link
Copy Markdown

🎉 This PR is included in version 1.66.7 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

nyannyacha added a commit to nyannyacha/edge-runtime that referenced this pull request Feb 20, 2025
…se#483)

* fix(sb_ai): prevent creation of invalid ort tensors refs

- ERROR: If a blank data `Tensor` is created and then used to feed
`Session` it will panic! with "Segmentation Fault: 11" error. So we must
prevent it by checking if the `Tensor` size reflects in its `dim shape`

* fix(ext/ai): add an utility crate

* chore: update `Cargo.lock`

* stamp: use `v8_do` instead of initialize v8 platform manually

---------

Co-authored-by: Nyannyacha <meow@nnc.gg>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants