Daneel: Type inference for Dalvik bytecode
In the last blog post about Daneel I mentioned one particular caveat of Dalvik bytecode, namely the existence of untyped instructions, which has a huge impact on how we transform bytecode. I want to take a similar approach as last time and look at one specific example to illustrate those implications. So let us take a look at the following Java method.
public float untyped(float[] array, boolean flag) {
if (flag) {
float delta = 0.5f;
return array[7] + delta;
} else {
return 0.2f;
}
}The above is a straightforward snippet and most of you probably know how the generated Java bytecode will look like. So let’s jump right to the Dalvik bytecode and discuss that in detail.
UntypedSample.untyped:([FZ)F: [regs=5, ins=3, outs=0] 0000: if-eqz v4, 0009 0002: const/high16 v0, #0x3f000000 0004: const/4 v1, #0x7 0005: aget v1, v3, v1 0007: add-float/2addr v0, v1 0008: return v0 0009: const v0, #0x3e4ccccd 000c: goto 0008
Keep in mind that Daneel doesn’t like to remember things, so he wants to look through the code just once from top to bottom and emit Java bytecode while doing so. He gets really puzzled at certain points in the code.
- Label 2: What is the type of register
v0? - Label 4: What is the type of register
v1? - Label 9: Register
v0again? What’s the type at this point?
You, as a reader, do have the answer because you know and understand the semantic of the underlying Java code, but Daneel doesn’t, so he tries to infer the types. Let’s look through the code in the same way Daneel does.
At method entry he knows about the types of method parameters. Dalvik passes parameters in the last registers (in this case in v3 and v4). Also we have a register (in this case v2) holding a this reference. So we start out with the following register types at method entry.
UntypedSample.untyped:([FZ)F: [regs=5, ins=3, outs=0] uninit uninit object [float bool
The array to the right represents the inferred register types at each point in the instruction stream as determined by the abstract interpreter. Note that we also have to keep track of the dimension count and the element type for array references. Now let’s look at the first block of instructions.
0002: const/high16 v0, #0x3f000000 u32 uninit object [float bool 0004: const/4 v1, #0x7 u32 u32 object [float bool 0005: aget v1, v3, v1 u32 float object [float bool 0007: add-float/2addr v0, v1 float float object [float bool
Each line shows the register type after the instruction has been processed. At each line Daneel learns something new about the register types.
- Label 2: I don’t know the type of
v0, only that it holds an untyped 32-bit value. - Label 4: Same applies for
v1here, it’s an untyped 32-bit value as well. - Label 5: Now I know
v1is used as an array index, it must have been an integer value. Also the array reference in registerv3is accessed, so I know the result is a float value. The result is stored inv1, overwriting it’s previous content. - Label 7: Now I know
v0is used in a floating-point addition, it must have been a float value.
Keep in mind that at each line, Daneel emits appropriate Java bytecode. So whenever he learns the concrete type of a register, he might need to retroactively patch previously emitted instructions, because some of his assumptions about the type were broken.
Finally we look at the second block of instructions reached through the conditional branch as part of the if-statement.
0009: const v0, #0x3e4ccccd u32 uninit object [float bool 000c: goto 0008 float uninit object [float bool
When reaching this block we basically have the same information as at method entry. Again Daneel learns in the process.
- Label 9: I don’t know the type of
v0, only that it holds an untyped 32-bit value. - Label 12: Now I know that
v0has to be a float value because the unconditional branch targets the join-point at label 8. And I already looked at that code and know that we expect a float value in that register at that point.
This illustrates why our abstract interpreter also has to remember and merge register type information at each join-point. It’s important to keep in mind that Daneel follows the instruction stream from top to bottom, as opposed to the control-flow of the code.
Now imagine scrambling up the code so that instruction stream and control-flow are vastly different from each other, together with a few exception handlers and an optimal register re-usage as produced by some SSA representation. That’s where Daneel still keeps choking at the moment. But we can handle most of the code produced by the dx tool already and will hunt down all those nasty bugs triggered by obfuscated code as well.
Disclaimer: The abstract interpreter and the method rewriter were mostly written by Rémi Forax, with this post I take no credit for it’s implementation whatsoever, I just want to explain how it works.
So many great perspectives
So many great perspectives here—it’s inspiring.
Thanks for creating such a
Thanks for creating such a supportive space. Thanks for sharing!
On a completely different
On a completely different note, creating a comfortable environment at home is just as important as clean code. With Screen enclosures Nokomis, you can upgrade your outdoor space into a relaxing, bug-free area that’s perfect for unwinding, entertaining, or even getting some fresh air while working on your next project.
Cool explanation of how
Cool explanation of how register types get resolved through execution flow.
Reminds me a bit of asphalt paving — the surface looks simple, but proper structure only shows once layers, load points, and stress distribution are evaluated carefully.
Interesting breakdown of
Interesting breakdown of Dalvik type inference — especially how values only become “typed” based on usage context like float ops or array access.
Kind of like pool screen repair work in real life: you don’t always know the exact issue until you inspect the frame, mesh, and pressure points step by step.
When searching for a fence
When searching for a fence installation contractor in Charleston, SC, you need someone who knows the landscape well - someone who has battled humidity, salt air and the relentless sun to produce fences that stand tall. That's where <a href="https://charlestonfencingpros.com/">Charleston Fencing Prosc</a> comes in; with over 20 years' experience installing posts deep and true using materials designed specifically to withstand South Carolina weather - whether chain link, wood or vinyl we create lasting boundaries as tough as gator skin!
Thank you for explaining it
Thank you for explaining it so clearly.
Type inference for Dalvik
Type inference for Dalvik bytecode plays a key role in understanding and optimizing Android applications, helping ensure code runs efficiently and securely across devices. And just like clean code matters in tech, reliable power matters in real life—trust Electricians Venice to keep everything running safely and smoothly.
Great post! I really enjoyed
Great post! I really enjoyed reading this—it’s full of helpful insights.
And when it comes to
And when it comes to improving comfort at home, Screen enclosures Port Charlotte offer a practical way to enjoy outdoor living—keeping spaces protected, breezy, and free from insects while still feeling open and relaxing.
This work on type inference
This work on type inference for Dalvik bytecode is interesting because it tackles the challenge of recovering higher-level type information from low-level Android bytecode. That can improve static analysis, security auditing, and reverse engineering of mobile applications. It would be useful to understand how the inference handles dynamic loading and reflection, which are common in Dalvik/ART environments.
Daneel’s core idea—type
Daneel’s core idea—type inference over Dalvik—is essentially to treat the bytecode as a constraint system. It propagates type constraints through instructions (moves, method calls, field accesses, array operations) and then solves for a consistent typing of registers and heap objects. In effect, it reconstructs a “best guess” of the original type graph.
That’s an interesting
That’s an interesting perspective. Keep on sharing, please!
Great work!!
Great work!!
https://fencingcraigieburn.co
https://fencingcraigieburn.com.au/
great explanation, would read
great explanation, would read again
https://fencingbunbury.com.au
https://fencingbunbury.com.au/services/timber-fencing/
Thank you for explaining it
Thank you for explaining it so clearly.
It’s good to see such an
It’s good to see such an informative and interesting topic being discussed here.
Thanks for sharing your
Thanks for sharing your insights. This was very helpful.
Thanks for posting—keep them
Thanks for posting—keep them coming!
Thanks for sharing your
Thanks for sharing your knowledge with the community.
This was a wonderfully
This was a wonderfully written article that balanced detail with readability perfectly. I walked away feeling more informed and inspired, which is always a sign of great content.
http://www.windsorfenceinstal
http://www.windsorfenceinstallation.com
I learned something new from
I learned something new from this post—thank you!
I enjoy how open everyone is
I enjoy how open everyone is about sharing ideas.
https://coffsharbourconcreter
https://coffsharbourconcreters.com/
<a
<a href="https://fencing-sutherlandshire.com.au/services/colorbond-fencing/">Colorbond Fencing Sutherland Shire</a>
I like how everyone shares
I like how everyone shares helpful insights here.
I like how everyone shares
I like how everyone shares helpful insights here.
great platform
great platform
Paver Installation Contractor
Paver Installation Contractor in Tucson, AZ
When it comes to updating the exterior of your home, selecting the appropriate team for residential paver installation in Tucson, AZ is an investment worth making. Each stone laid becomes part of a setting where family gatherings, quiet evenings and neighborhood pride can occur. By choosing a contractor with expertise in level surfaces, precise cuts, durable materials that withstand Arizona climate conditions as well as long-lasting function delivering value over time, immediate beauty can become long-term value that adds lasting beauty.
http://www.floresandpavers.com/
I appreciate the effort you
I appreciate the effort you put into writing this.
Awesome blog thanks!
Awesome blog thanks!
If you’re also focusing on
If you’re also focusing on your financial health, exploring credit repair Fort Lauderdale services can help you review your credit report and work toward improving your overall credit standing.
Me too! Let's collaborate!
Me too! Let's collaborate!
If you’re also looking to
If you’re also looking to strengthen your financial standing, exploring credit repair Greeley services can help you review your credit profile and work toward improving your credit health over time.
I appreciate everyone sharing
I appreciate everyone sharing their experiences, it’s so helpful.
Thanks for sharing such
Thanks for sharing such useful insights.
Lots of good takeaways here,
Lots of good takeaways here, appreciate the input.
Glad to check this site. Keep
Glad to check this site. Keep on sharing here.
I will keep you updated!
I will keep you updated!
Thanks to everyone who shared
Thanks to everyone who shared their thoughts. I’m learning a lot.
i appreciate the effort that
i appreciate the effort that has been put into this platform!
Really glad I came across
Really glad I came across this post, the community here always shares valuable insights.
<a
<a href="https://fencingechuca.com.au/services/colorbond-fencing/">Colorbond Fencing Echuca</a>
This is awesome! Glad to
This is awesome! Glad to check this site.
This is a great topic, thanks
This is a great topic, thanks for sharing.
There are a lot of people
There are a lot of people looking for help and getting their issues resolved by the workers who know how to resolve it and get the solutions. When I visited the site here https://sydneyconcretingexperts.com.au/ I saw these are the best professionals who can do it perfectly without any issue.
very informative, great
very informative, great job
https://a1fencingtownsville.com/services/pool-fencing/