Skip to content

[java] First part of the JNI error handling rewrite#12013

Merged
justinchuby merged 4 commits intomicrosoft:masterfrom
Craigacp:jni-rewrite-initial
Jul 12, 2022
Merged

[java] First part of the JNI error handling rewrite#12013
justinchuby merged 4 commits intomicrosoft:masterfrom
Craigacp:jni-rewrite-initial

Conversation

@Craigacp
Copy link
Contributor

@Craigacp Craigacp commented Jun 28, 2022

Description: This fixes error handling in the JNI code in OnnxMap, OnnxSequence, OnnxRuntime, RunOptions. SessionOptions and OrtEnvironment are correct as is.

The bulk of the work will be in rewriting OnnxTensor, OnnxSparseTensor (after the merge of #10653) and OrtSession, along with the helper methods in OrtJniUtil. I plan to tackle those in separate PRs to reduce the amount of code to review.

Motivation and Context

  • Why is this change required? What problem does it solve? The current native interop code doesn't return control to Java immediately on throwing an exception from an ORT error code, which can cause incorrect interactions with native ORT, and issues with exception propagation on the Java side.
  • If it fixes an open issue, please link to the issue here. Partial work towards solving JAVA API does not handle exceptions correctly - causing crash or potential memory leak #11451.

@Craigacp
Copy link
Contributor Author

In addition to some whitespace issues which I'll fix, the linter still thinks my C code is C++, and is complaining I'm not using C++ casts.

@fs-eire
Copy link
Contributor

fs-eire commented Jun 28, 2022

In addition to some whitespace issues which I'll fix, the linter still thinks my C code is C++, and is complaining I'm not using C++ casts.

The message from linter sounds reasonable to me. JNI code is C++ and using reinterpret_cast is good suggestion to me.

@Craigacp
Copy link
Contributor Author

JNI can be written in C or C++, but this code is all C.

@fs-eire
Copy link
Contributor

fs-eire commented Jul 5, 2022

JNI can be written in C or C++, but this code is all C.

This sounds reasonable to me. Is there a way to setup the linter to apply C instead of C++ on those files? @justinchuby

fs-eire
fs-eire previously approved these changes Jul 5, 2022
Copy link
Contributor

@fs-eire fs-eire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code change looks good to me.

There are 2 work items left for the error handling:

  • other parts of the error handling update for API calls, including the OrtRun() (which is one of the most complicated case)
  • add tests for those error handling. add some test code to cover the error handling, and expect the correct behavior (exception thrown instead of crashing)

@fs-eire
Copy link
Contributor

fs-eire commented Jul 5, 2022

/azp run Linux CPU CI Pipeline, Linux CPU Minimal Build E2E CI Pipeline, Linux GPU CI Pipeline, Linux GPU TensorRT CI Pipeline, Linux Nuphar CI Pipeline, Linux OpenVINO CI Pipeline, MacOS CI Pipeline, ONNX Runtime Web CI Pipeline, onnxruntime-binary-size-checks-ci-pipeline

@fs-eire
Copy link
Contributor

fs-eire commented Jul 5, 2022

/azp run Windows CPU CI Pipeline, Windows GPU CI Pipeline, Windows GPU TensorRT CI Pipeline, Windows WebAssembly CI Pipeline, orttraining-amd-gpu-ci-pipeline, orttraining-linux-ci-pipeline, orttraining-linux-gpu-ci-pipeline, orttraining-ortmodule-distributed, onnxruntime-python-checks-ci-pipeline

@azure-pipelines
Copy link

Azure Pipelines successfully started running 9 pipeline(s).

@azure-pipelines
Copy link

Azure Pipelines successfully started running 8 pipeline(s).

@Craigacp
Copy link
Contributor Author

Craigacp commented Jul 5, 2022

JNI can be written in C or C++, but this code is all C.

This sounds reasonable to me. Is there a way to setup the linter to apply C instead of C++ on those files? @justinchuby

I looked into it, but Google don't provide a style guide for C, and cpplint is a C++ specific thing without a way to restrict it to C syntax. If you've got a preferred C style guide then I'm happy to follow it and we can try to find something to enforce it in the CI. Java uses spotless as part of the gradle build, we should probably pull that out as a Lint check for the CI rather than a compile/test time check as it currently is, and we can discuss doing that later.

The code change looks good to me.

There are 2 work items left for the error handling:

  • other parts of the error handling update for API calls, including the OrtRun() (which is one of the most complicated case)
  • add tests for those error handling. add some test code to cover the error handling, and expect the correct behavior (exception thrown instead of crashing)

Sure. I think I'll probably split the rest of the error handling into multiple PRs though, rather than a single one for all the remaining classes. Some of them are going to be quite big and reviewing that much code is painful.

For the tests, I'll try to find some way of exercising it in a repeatable fashion as I thought I had tests which check that exceptions are thrown for the wrong shape, so it might be something specific to the android/react native use case that causes issues. The Android runtime might behave differently than the JVM does wrt JNI and exception handling.

@justinchuby
Copy link
Contributor

@fs-eire I can look into the config

@justinchuby
Copy link
Contributor

justinchuby commented Jul 5, 2022

Proposed #12094. Needs verification

@justinchuby
Copy link
Contributor

Actually, I think cpplint still provides useful lints on c files. Since the lints are non-blocking. We can safely ignore lints that do not make sense with c. In the long run we may be better off adopting clang-tidy in CI.

@Craigacp
Copy link
Contributor Author

Craigacp commented Jul 6, 2022

I've added a cast from size_t to jsize to make MSVC happy, please could someone kick off the pipelines again?

@justinchuby
Copy link
Contributor

/azp run Linux CPU CI Pipeline, Linux CPU Minimal Build E2E CI Pipeline, Linux GPU CI Pipeline, Linux GPU TensorRT CI Pipeline, Linux Nuphar CI Pipeline, Linux OpenVINO CI Pipeline, MacOS CI Pipeline, ONNX Runtime Web CI Pipeline, onnxruntime-binary-size-checks-ci-pipeline

@justinchuby
Copy link
Contributor

/azp run Windows CPU CI Pipeline, Windows GPU CI Pipeline, Windows GPU TensorRT CI Pipeline, Windows WebAssembly CI Pipeline, orttraining-amd-gpu-ci-pipeline, orttraining-linux-ci-pipeline, orttraining-linux-gpu-ci-pipeline, orttraining-ortmodule-distributed, onnxruntime-python-checks-ci-pipeline

@azure-pipelines
Copy link

Azure Pipelines successfully started running 9 pipeline(s).

@azure-pipelines
Copy link

Azure Pipelines successfully started running 8 pipeline(s).

@Craigacp
Copy link
Contributor Author

Looks like all the checks passed bar the linter complaining about my C code not being C++. Can we merge this in?

@justinchuby justinchuby merged commit e0ed9f0 into microsoft:master Jul 12, 2022
@Craigacp Craigacp deleted the jni-rewrite-initial branch July 12, 2022 22:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants