Skip to content

Do not use GetPrimitiveArrayCritical(...) due multiple not-fixed bugs…#8921

Merged
normanmaurer merged 2 commits into4.1from
gc_locker
Mar 7, 2019
Merged

Do not use GetPrimitiveArrayCritical(...) due multiple not-fixed bugs…#8921
normanmaurer merged 2 commits into4.1from
gc_locker

Conversation

@normanmaurer
Copy link
Copy Markdown
Member

… related to GCLocker

Motivation:

GetPrimitiveArrayCritical(...) may cause multiple not-fixed bugs related to the GCLocker while there is little gain for our use-case. We should just use GetByteArrayRegion(...) and copy into a small on-stack buffer.

See also:

Special thanks to @jayv @shipilev @apangin for the pointers.

Modifications:

Replace GetPrimitiveArrayCritical(...) with GetByteArrayRegion(...)

Copy link
Copy Markdown

@jayv jayv left a comment

Choose a reason for hiding this comment

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

Makes sense

int netty_unix_socket_initSockaddr(JNIEnv* env, jbyteArray address, jint scopeId, jint jport,
const struct sockaddr_storage* addr, socklen_t* addrSize) {
uint16_t port = htons((uint16_t) jport);
// 24 is the maximum possible
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

128 bit (16 byte) for IPv6 address plus long port 64 bit (8 byte)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

actually we only need 16 here, so I changed it and added a comment. Good catch!

… related to GCLocker

Motivation:

GetPrimitiveArrayCritical(...) may cause multiple not-fixed bugs related to the GCLocker while there is little gain for our use-case. We should just use GetByteArrayRegion(...) and copy into a small on-stack buffer.

See also:

- https://shipilev.net/jvm/anatomy-quarks/9-jni-critical-gclocker/#_g1
- https://bugs.openjdk.java.net/browse/JDK-8048556
- https://bugs.openjdk.java.net/browse/JDK-8057573
- https://bugs.openjdk.java.net/browse/JDK-8057586

Special thanks to @jayv @shipilev @apangin for the pointers.

Modifications:

Replace GetPrimitiveArrayCritical(...) with GetByteArrayRegion(...)

Result:

Less risks hitting GCLocker related bugs.
@normanmaurer
Copy link
Copy Markdown
Member Author

@netty-bot test this please

// https://bugs.openjdk.java.net/browse/JDK-8048556
// https://bugs.openjdk.java.net/browse/JDK-8057573
// https://bugs.openjdk.java.net/browse/JDK-8057586
(*env)->GetByteArrayRegion(env, address, 0, len, addressBytes);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seems we should do min(len, 16) or have an assert. Yes, we know it should be 16 or less, but I don't like playing with fire.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Or just 16? The code which reads the array below assumes that's always the case.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

let me just add a guard for now... can't hurt.

netty_unix_errors_throwOutOfMemoryError(env);
int len = (*env)->GetArrayLength(env, address);

if (len > 16) {
Copy link
Copy Markdown
Contributor

@johnou johnou Mar 7, 2019

Choose a reason for hiding this comment

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

I think it should be != 16 or it's potentially going to segfault..

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ah not if it's AF_INET4.

@normanmaurer normanmaurer merged commit 1725504 into 4.1 Mar 7, 2019
@normanmaurer normanmaurer deleted the gc_locker branch March 7, 2019 09:31
normanmaurer added a commit that referenced this pull request Mar 7, 2019
#8921)

* Do not use GetPrimitiveArrayCritical(...) due multiple not-fixed bugs related to GCLocker

Motivation:

GetPrimitiveArrayCritical(...) may cause multiple not-fixed bugs related to the GCLocker while there is little gain for our use-case. We should just use GetByteArrayRegion(...) and copy into a small on-stack buffer.

See also:

- https://shipilev.net/jvm/anatomy-quarks/9-jni-critical-gclocker/#_g1
- https://bugs.openjdk.java.net/browse/JDK-8048556
- https://bugs.openjdk.java.net/browse/JDK-8057573
- https://bugs.openjdk.java.net/browse/JDK-8057586

Special thanks to @jayv @shipilev @apangin for the pointers.

Modifications:

Replace GetPrimitiveArrayCritical(...) with GetByteArrayRegion(...)

Result:

Less risks hitting GCLocker related bugs.
@Hamlin-Li
Copy link
Copy Markdown

Hamlin-Li commented Nov 30, 2021

I hope G1 Region Pinning in JDK may help eliminate or alleviate this problem in the future.

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.

5 participants