PooledUnsafeDirectByteBuf instanceof Bytebuf returns false
See original GitHub issueI’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:
- Created 2 years ago
- Comments:5 (1 by maintainers)
Top GitHub Comments
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:
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 theAgentClass
(whereagentmain
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 argumentByteBuf
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…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!