question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PooledUnsafeDirectByteBuf instanceof Bytebuf returns false

See original GitHub issue

I’m using bytebuddy to “hook” the following method: https://github.com/netty/netty/blob/d34212439068091bcec29a8fad4df82f0a82c638/transport/src/main/java/io/netty/channel/socket/nio/NioSocketChannel.java#L347

Inside the hook, I get a reference to the method’s parameters as Object. Therefore, I’m trying to cast the first and only parameter to ByteBuf, but arguments[0] instanceof ByteBuf returns false and (ByteBuf)arguments[0] outputs: java.lang.ClassCastException: io.netty.buffer.PooledUnsafeDirectByteBuf cannot be cast to io.netty.buffer.ByteBuf After checking the code a few times, I made sure PooledUnsafeDirectByteBuf extends ByteBuf (after a long inheritance chain). Here is the relevant code:

System.out.println("Parameter: " + arguments[0]);
System.out.println("instance? " + (arguments[0] instanceof io.netty.buffer.ByteBuf));
b = (io.netty.buffer.ByteBuf) arguments[0];

Expected behavior

arguments[0] instanceof ByteBuf should return True since arguments[0] is of type PooledUnsafeDirectByteBuf (or other type that extends from ByteBuf)

Actual behavior

Parameter: PooledUnsafeDirectByteBuf(ridx: 0, widx: 0, cap: 2048)
instance? false
java.lang.ClassCastException: io.netty.buffer.PooledUnsafeDirectByteBuf cannot be cast to io.netty.buffer.ByteBuf

Steps to reproduce

The steps to reproduce are a bit complicated, If it is really necessary I will upload the smallest code I manage to make that includes a bytebuddy builder that hooks the doReadBytes method.

Netty version

I’m using this line in my build.gradle: implementation "io.netty:netty-all:4.1.73.Final"

JVM version (e.g. java -version)


------------------------------------------------------------
Gradle 7.3.2
------------------------------------------------------------

Build time:   2021-12-15 11:22:31 UTC
Revision:     26c186eb63b2b02e68d83b0dbc6ec69ab118653a

Kotlin:       1.5.31
Groovy:       3.0.9
Ant:          Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM:          1.8.0_312 (Red Hat, Inc. 25.312-b07)
OS:           Linux 5.10.75-79.358.amzn2.x86_64 amd64

OS version (e.g. uname -a)

I’m using the new AWS linux image: [ec2-user@ip-******** netty]$ uname -a Linux ip-*********.us-east-2.compute.internal 5.10.75-79.358.amzn2.x86_64 #1 SMP Thu Nov 4 21:08:30 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Thanks for your help!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
sides-flowcommented, Jan 20, 2022

TL;DR: The problem has nothing to do with netty.

I have managed to advance some more. After a few hours of working on it and trying to understand whats going on, I tried switching the JVM to an updated version (correto-11). The newer version printed a better explanation of the problem:

java.lang.ClassCastException: class io.netty.buffer.PooledUnsafeDirectByteBuf cannot be cast to class io.netty.buffer.ByteBuf (io.netty.buffer.PooledUnsafeDirectByteBuf is in unnamed module of loader 'app'; io.netty.buffer.ByteBuf is in unnamed module of loader 'bootstrap')

This proves that you were right @raphw (as always). After some more time trying to work it out, I ended up creating another Advice class inside the AgentClass (where agentmain is located). For some reason that I still don’t fully understand, the cast worked this time. I guess that it has something to do with the class loader that loaded the Advice class, maybe… After printing the data of the argument ByteBuf I also realized that this is not the data that I was looking for 😕

I better close this issue since (now I know that) it has nothing to do with netty, but rather my lack of knowledge of how class loaders work in Java. I guess I will face this problem in the future…

0reactions
sides-flowcommented, Jan 19, 2022

Unfortunately I’m unable to use PooledUnsafeDirectByteBuf since its access modifier is package, and my code is outside the package. However, ByteBuf is public.

I will check what you said about 2 class loaders. I didn’t even think about this possibility of 2 different ByteBuf classes loaded…

If I won’t be able to figure it out myself I will upload a small snippet of the code here to make sure I didn’t do something dumb.

Thank you so much!

Read more comments on GitHub >

github_iconTop Results From Across the Web

io.netty.buffer.PooledUnsafeDirectByteBuf.getBytes java code ...
public int getBytes(int index, FileChannel out, long position, int length) throws IOException { return getBytes(index, out, position, length, false);
Read more >
ByteBuf (Netty API Reference (4.0.56.Final))
Returns the number of bytes (octets) this buffer can contain. ... This method also returns false for null and an object which is...
Read more >
io.netty.buffer.ByteBuf Java Examples - ProgramCreek.com
This page shows Java code examples of io.netty.buffer.ByteBuf. ... DEFAULT, 1, false, null, Unpooled. ... readerIndex(i); return false; }. Example #11 ...
Read more >
netty http/2 APNs Client (header frame, data frame)
@return true if a successful HTTP/2 end of stream message was received. ... "value"); // if you have a data frame you have...
Read more >
SslHandler.java example - Javatips.net
@return encrypted * {@code true} if the {@link ByteBuf} is encrypted, {@code false} otherwise. * @throws IllegalArgumentException * Is thrown if the given ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found