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

java: properly implement code_copy_fn #545

Merged
merged 2 commits into from
Oct 14, 2020
Merged

java: properly implement code_copy_fn #545

merged 2 commits into from
Oct 14, 2020

Conversation

axic
Copy link
Member

@axic axic commented Sep 30, 2020

No description provided.

return ByteBuffer.allocateDirect(length).put(code, code_offset, length);
/* Ensure code is returned and both offset/length are positive numbers. */
if (code != null && offset >= 0 && length >= 0 && offset <= code.length) {
int length = Math.min(length, code.length - offset);
Copy link
Member Author

Choose a reason for hiding this comment

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

We can decide whether to do this slicing here or in C.

@axic axic force-pushed the java-codecopy branch 2 times, most recently from 5b74558 to d3d7e59 Compare September 30, 2020 10:38
@axic axic requested review from chfast, atoulme and jrhea and removed request for chfast September 30, 2020 10:38
@axic axic force-pushed the java-codecopy branch 3 times, most recently from 06960d1 to 3ca57ae Compare September 30, 2020 10:42
return ByteBuffer.allocateDirect(length).put(code, code_offset, length);
/* Ensure code is returned and both offset/length are positive numbers. */
if (code != null) {
// TODO: use wrap here, no need to duplicate this given it is copied again.
Copy link
Contributor

Choose a reason for hiding this comment

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

This TODO says we can use ByteBuffer.wrap, am I reading this right? But for C code to interact with ByteBuffer, you need to use a direct memory allocation. I had panics when I tried to use heap-based bytebuffers.

Copy link
Member Author

Choose a reason for hiding this comment

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

I did not see this difference in the JNI docs, only whether it is copied or not. And here we do not need copying given the C counterpart does a copy anyway.

Can you point me to the documentation where this is mentioned?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the code used to get the pointer of the bytebuffer, and that would crash the JVM. So I think you're saying that's all changed now - I'm good with a change there, and I'll make sure to give you a reproduction case if that happens again.

Copy link
Member Author

@axic axic Oct 9, 2020

Choose a reason for hiding this comment

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

I think you are right, documentation does seem to claim that allocateDirect will 100% be laid on a way it can be accessed without copying, while other may require copying. It is not clear however who does that copying, but it seems it is done by the JVM so it would mean a slowdown, but not a crash.

I can remove this comment.

@axic
Copy link
Member Author

axic commented Oct 8, 2020

@chfast which one do you prefer, the first or the second commit?

@axic axic force-pushed the java-codecopy branch 4 times, most recently from 5dc5384 to 85fa5fc Compare October 13, 2020 22:26
buffer_data = (uint8_t*)(*jenv)->GetDirectBufferAddress(jenv, jresult);
(void)buffer_data;
assert(buffer_data != NULL);
uint8_t* code = (uint8_t*)(*jenv)->GetDirectBufferAddress(jenv, jresult);
Copy link
Member Author

Choose a reason for hiding this comment

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

This should use GetDirectBuffer from #552.

Copy link
Member

Choose a reason for hiding this comment

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

Looks unfinished.

Copy link
Member Author

Choose a reason for hiding this comment

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

You can ignore that, still waiting for an answer on this: #545 (comment)

@axic
Copy link
Member Author

axic commented Oct 14, 2020

@chfast still needs your review

@axic
Copy link
Member Author

axic commented Oct 14, 2020

@chfast okay this is my final proposal.

@@ -16,6 +16,13 @@
* <p>The set of all callback functions expected by VM instances.
*/
final class Host {
static private ByteBuffer ensureDirectBuffer(ByteBuffer input) {
// Reallocate if needed.
if (!input.isDirect())
Copy link
Member

Choose a reason for hiding this comment

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

No idea what it does. Is this even tested?

Copy link
Member Author

@axic axic Oct 14, 2020

Choose a reason for hiding this comment

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

Not all of them are, but get/set_storage is. This helper enforces that the ByteBuffer is in a format ("direct buffer") which can be queried from JNI (e.g. the GetDirectBuffer helper in C won't assert). The tests always use "direct buffer" though so we never seen an assert.

@axic axic merged commit 6365871 into master Oct 14, 2020
@axic axic deleted the java-codecopy branch October 14, 2020 11:50
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