From 851b0ce93696aeb44bbc8e4cc4ea4d1c10adfe29 Mon Sep 17 00:00:00 2001 From: shalvamist <94086448+shalvamist@users.noreply.github.com> Date: Wed, 21 Sep 2022 13:59:17 -0700 Subject: [PATCH] [js/web][Fix] - updating the C API to catch non-tensor data (#12811) Added a check for tensor validation on the input - this change fixes the quiet abort WASM takes when processing a non tensor data in "OrtGetTensorData" **Motivation and Context** At the current status when we try to process non-tensor data through OrtGetTensorData and exception is thrown which results in a quiet abort from WASM (assuming WASM was built without exception handling). I added a check in the C API to catch this case and output a meaningful message to the user [example_error_github_12622.zip](https://github.com/microsoft/onnxruntime/files/9464328/example_error_github_12622.zip) --- js/web/lib/wasm/wasm-core-impl.ts | 2 +- onnxruntime/wasm/api.cc | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/js/web/lib/wasm/wasm-core-impl.ts b/js/web/lib/wasm/wasm-core-impl.ts index 3d55584abf..890ef58ada 100644 --- a/js/web/lib/wasm/wasm-core-impl.ts +++ b/js/web/lib/wasm/wasm-core-impl.ts @@ -328,7 +328,7 @@ export const run = errorCode = wasm._OrtGetTensorData( tensor, tensorDataOffset, tensorDataOffset + 4, tensorDataOffset + 8, tensorDataOffset + 12); if (errorCode !== 0) { - throw new Error(`Can't get a tensor data. error code = ${errorCode}`); + throw new Error(`Can't access output tensor data. error code = ${errorCode}`); } let tensorDataIndex = tensorDataOffset / 4; const dataType = wasm.HEAPU32[tensorDataIndex++]; diff --git a/onnxruntime/wasm/api.cc b/onnxruntime/wasm/api.cc index 85c7a6f477..2e52460b34 100644 --- a/onnxruntime/wasm/api.cc +++ b/onnxruntime/wasm/api.cc @@ -113,7 +113,7 @@ OrtSessionOptions* OrtCreateSessionOptions(size_t graph_optimization_level, RETURN_NULLPTR_IF_ERROR(EnableOrtCustomOps, session_options); #endif #if defined(USE_XNNPACK) - + RETURN_NULLPTR_IF_ERROR(SessionOptionsAppendExecutionProvider, session_options, "XNNPACK", nullptr, nullptr, 0); #endif return session_options; @@ -246,6 +246,12 @@ int OrtGetTensorData(OrtValue* tensor, int* data_type, void** data, size_t** dim size_t* p_dims = nullptr; void* p_string_data = nullptr; + ONNXType tensor_type; + RETURN_ERROR_CODE_IF_ERROR(GetValueType, tensor, &tensor_type); + if ( tensor_type != ONNX_TYPE_TENSOR ) { + return ORT_FAIL; + } + RETURN_ERROR_CODE_IF_ERROR(GetTensorTypeAndShape, tensor, &info); size_t dims_len = 0;