Skip to content
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

Add more error handling in writeBytes. #152

Conversation

hiddenalpha
Copy link

  • Throw NullPointerException (in place of crashing) if caller passes null buffer.
  • Pass-through OutOfMemoryError if GetByteArrayElements() fails to alloc memory.

- Throw NullPointerException (in place of crashing) if caller passes
  null buffer.
- Pass-through OutOfMemoryError if GetByteArrayElements() fails to alloc
  memory.

Cherry-picked from 5abdd45.
@tresf
Copy link

tresf commented Nov 9, 2023

Pass-through OutOfMemoryError if GetByteArrayElements() fails to alloc memory.

Can you explain where the OutOfMemoryError is thrown?

Throw NullPointerException (in place of crashing) if caller passes null buffer.

Hmm... Should we wrap exceptions which are historically non-checked?

return NULL, without any code that would prepare a java exception. In
JNI doc I read that an exception gets setup if a JNI function fails.
(Read "https://www.developer.com/open-source/exception-handling-in-jni/"
to see what I mean) But for whatever reason this seems not to be the
case for GetByteArrayElements().

- writeBytes() calls GetByteArrayElements().
- GetByteArrayElements() calls NEW_C_HEAP_ARRAY_RETURN_NULL().
- NEW_C_HEAP_ARRAY_RETURN_NULL() calls NEW_C_HEAP_ARRAY3().
- NEW_C_HEAP_ARRAY3() calls AllocateHeap().
- AllocateHeap() uses malloc() to get memory.

Direct Links:
GetByteArrayElements: https://github.com/openjdk/jdk/blob/jdk8-b120/hotspot/src/share/vm/prims/jni.cpp#L3597
call to NEW_C_HEAP_ARRAY_RETURN_NULL: https://github.com/openjdk/jdk/blob/jdk8-b120/hotspot/src/share/vm/prims/jni.cpp#L3611
NEW_C_HEAP_ARRAY_RETURN_NULL: https://github.com/openjdk/jdk/blob/jdk8-b120/hotspot/src/share/vm/memory/allocation.hpp#L680
call to NEW_C_HEAP_ARRAY3: https://github.com/openjdk/jdk/blob/jdk8-b120/hotspot/src/share/vm/memory/allocation.hpp#L681
NEW_C_HEAP_ARRAY3: https://github.com/openjdk/jdk/blob/jdk8-b120/hotspot/src/share/vm/memory/allocation.hpp#L668
call to AllocateHeap: https://github.com/openjdk/jdk/blob/jdk8-b120/hotspot/src/share/vm/memory/allocation.hpp#L669
AllocateHeap: https://github.com/openjdk/jdk/blob/jdk8-b120/hotspot/src/share/vm/memory/allocation.inline.hpp#L52

Picked from 4a805a7.
@hiddenalpha
Copy link
Author

Will have a look as soon I find some time. Thx for feedback so far.

@tresf
Copy link

tresf commented Nov 10, 2023

Hmm... Should we wrap exceptions which are historically non-checked?

Will have a look as soon I find some time. Thx for feedback so far.

Whoops, we don't. We only catch IOException, so we're still going to throw unchecked exceptions, which I think is what we want, per:

} catch(IOException ex) {
throw SerialPortException.wrapNativeException(ex, this, "writeBytes");
}

I think unchecked exceptions are fine for now (they're certainly better than a JVM crash). If users complain about this we, can modify the the catch statement at a future time.

@tresf
Copy link

tresf commented Nov 10, 2023

@hiddenalpha do you believe we should add more unit tests for these? Furthemore, have you had time to evaluate the safety of the windows code too?

@hiddenalpha
Copy link
Author

do you believe we should add more unit tests for these?

I think writing unit tests may be challenging for some of those cases. Especially edge-cases in the native code. I think best is to add a test when we see a good opportunity and if it can be written in a simple (as in understandable/maintainable) way.

have you had time to evaluate the safety of the windows code too?

Did not look at the windows code at all yet. Will keep it in mind when I've a free time slot.

@tresf
Copy link

tresf commented Nov 15, 2023

This can be tested in its current form using mvn -DskipTests install && mvn until rebased against master (specifically #51, #151).

It looks good to me, but I've left a question about the unit test, since matching an exception string can have unobvious test failures in the event this string value ever gets changed.

…ng-in-writeBytes-20231109

Conflicts:
  src/test/java/jssc/SerialNativeInterfaceTest.java
@tresf
Copy link

tresf commented Nov 30, 2023

When you have time, this PR needs to be rebased and there's still one comment remaining. I've love to see it merged. :)

@tresf tresf merged commit b9b6b25 into java-native:master Dec 1, 2023
@pietrygamat pietrygamat added this to the 2.9.6 milestone Dec 1, 2023
hiddenalpha added a commit to hiddenalpha/jssc that referenced this pull request Dec 13, 2023
This adds the risk of false results as the test now will pass
for ANY kind of NPE. But who cares.
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