How to debug the Java compiler source code (Javac) itself? [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to know the detail compilation process from the Java code to the bytecode, such as how a line Java code translate to the bytecode,so I want to set the breakpoint in the Java compiler source code, however I don't know how to do that , is there any reference or steps for that ?
BTW,
I don't ask how to use the Java debuger(obviously I know how to use it), I mean how to debug the Java compiler source code instead of the Java source code.

Your question about how the compiler works is too broad to answer here. Go read a standard compiler text book.
Regarding debugging a compiler:
I tend to heavily use assertions in the compiler (self-diagnosing) and large suites of small target language test programs focused on various language features that self-verify their correct functioning. (My last compiler has around a thousand of these test programs built over a period of 10 years, and a script to compile and run them all). Often a failure of an assertion or a test hints to me where the compiler is wrong and mere inspection identifies the bug.
I often add special code to dump out key information about the state of the compiler and traces of activities that I can enable with special undocumented command line switches. These special dumps indicate where the compiler goes astray, and again often enable code inspection to identify the location of the bug.
Failing that, I use a debugger for the language in which I have written the compiler.
I don't consider these techniques as particularly special. They are useful when trying to work with any large, complex application.

Related

How to inject and run Scala code in a running JVM [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Is it possible to compile & run scala code dynamically within Java code.
It is possible to achieve a similar result with JS using mozilla rhino. But, I wonder if it is possible with scala?
Theoretically, yes. But you will need to do a lot of things:
ensure the user entered valid scala code
transfer that source code to the server
compile the scala code
run it from within your server (catch errors, deal with resource leaks, ...)
So, possible: yes. Reasonable: not so much.
Obviously: a lot of work
getting to a decent user experience: even more work (like: telling the user exactly where in his source code input your compilation step found a bug ... hard)
And of course: opens your system for a ton of attack vectors.
If you want your users to be able to run code on the backend server, one wonders: why don't they have admin access to that machine already, and are able to deploy there code right there on the server themselves?!
Sure, Scala has its REPL, and as that one comment pointing to an existing answer implies: it is definitely possible to do that.
But as said: we don't do things because we can, but because it makes sense doing it!
twitter util-eval library seems provide what I need, but it is discontinued.
Here is an old fork:
https://github.com/m3dev/twitter-util-eval

How are commands and their functions defined in Java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Does Java use libraries to determine which commands perform specific actions?
After learning Java for a few weeks now I don't really understand how Java recognises which commands perform specific functions in code.
For example, how does Java know what to do when you use a "do while" loop? And how does Java remember so many different commands, is there some sort of master list or a combination of libraries that document recognised Java commands and their functions?
This may seem like a real noob question, but it's been bugging me for a while and is getting in the way of my understanding of how code, and specifically Java works. Thank you all in advance.
Edit: Just to make this more specific, I was confused because I didn't know how new functionality is added. So, as I understand, new commands and functionality is added to Java using additional packages alongside the JDK, which contains a list of the most fundamental Java commands?
The commands and the full syntax of the language is defined in the Java Language Specification. The Java compiler generates low level instructions (i.e. bytecode) according to that syntax for language constructs like the do-while loop. The JVM is then responsible for executing those bytecodes.
I understood your questions in two ways:
1 - Where does java hold its lots of functions, classes, etc.
2 - How does java work (how does an if/else work) under the code's skin.
First, there is no noob questions, we are all here to learn :)
Second, about the questions:
1 - Java holds its classes in the Java Development Kit (JDK). Because of this, when you create a Java application, you need it installed in your machine, so Java will find a lot of its classes in the installed JDK.
You can expand the amount of classes by adding new packages (jars) to your project. Then, Java will see all the JDK classes and your added jars.
2 - If you mean to "how does java work in a do-while loop or something related, you want to learn about javac, java's compiler.
Javac will get your code and transform into an jar file. The Java Virtual Machine interprets the compiled code to do memory operations.
For exanple, when you assign a variable to java, internally you create a space in memory with the datatype's size.
When you do a while loop, the JVM will use other VM functions to do the job.
That's actually a quite advanced question. I think you can find more about what you want here.
Hope it helps! :)

lazy and efficient approach to checked exceptions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm writing small java networking programs for school; obviously RTFM is de rigueur, but in developing skeleton code is it efficient (it's obviously lazy) to just run it by the compiler to see if I've forgotten some checked exception (rather than: RTM-> manually check ...)?
One "Hack" I have done is make a script to periodically compile my c++ source file everytime I save it. Then script autoclears the terminal window and the top errors are visible. The window is also set as "always ontop". This catches semicolons and other trivial errors/typos very quickly and I can fix immediately.
I don't see why not do something similar in Java or any other language. I don't see this as lazy, it takes some effort to do, and it seams like a good tool for more than finding out if you are missing checked exceptions. You can even write a parser to be analyse the errors for you and give you copy & paste code to potentially fix some of the errors, like the checked exceptions.
For Java, eclipse does a really good job on the checked exception side, it even pre-inserts the exceptions for you when you press CTRL+1. When using eclipse I haven't had the need to do something similar as I did in c++ because eclipse does a good job parsing Java sources and putting a marker on errors such as typos and other things even before you save the file.
Yes - the compiler is an essential tool in developing anything that is more than 5 lines long. An IDE such as IntelliJ will compile real time as you type immediately highlighting errors.

How to inject bytecode to a compiled java program without using tools? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to learn how I can create an injectable piece of java bytecode, and inject it into an already compiled java program so it will run when the said program is executed.
It doesn't have to be dynamic and in runtime, just given a compiled java program inject additional code into it.
Now, I know there are many existing tools for this, like Javassist and ASM. But the act itself isn't my goal, I want to learn how its done, so I want to learn how to do this without these tools.
For example: How to strip excess code from the source bytecode, where to inject it into the target code, etc.
The best answer would be one or more simple pieces of source or pseudo-code.
After learning and successfully doing this I'm going to start searching info on how to do this to Linux executable binaries, so adding in more information on that way would also be very helpful and appreciated.
First off, Java classfiles are essentially immutable once loaded, so what you're really asking is how to create and modify classfiles by hand.
The answer is to read the JVM specification. That's how I got started with bytecode. After reading the specs, I wrote a couple simple classfiles by hand in a hex editor and played around with it to see how things worked. Of course, that's not practical for normal usage, so I later wrote an assembler. It's not that hard.
Incidentally, the source code for my assembler is only around 1k lines of code, so it's a lot less to sort through than Javaassist.
Javassist is essentially decompiling and compiling the code, that is why there is a lot of code there.
And you won't find the type code injection you are looking for in javassist. So "Go read javassist" is a rather stupid suggestion.
If you want to put your code into an specific place(for instance in the start or constructor)
you can see how to find the spot by reading JVM docs.
However as Antimony mentioned, you are looking for bytecode knowledge, so here it is:
http://arhipov.blogspot.com.au/2011/01/java-bytecode-fundamentals.html
If you want to inject a piece of bytecode,you can just find the start of your main() and paste the code there. It will be 200-300 LOC MAX.
With linux binaries it is much easier, read this:
http://www.skyfree.org/linux/references/ELF_Format.pdf
Typically, this is done by reading the bytecode (or Linux executable), transforming it into some form of Intermediate Representation (IR), perform additional transformation on the IR, and convert it back into the original format.
If you transform the IR back into Java source code, you will get a decompiler for Java. If you perform analysis on the IR, manipulate it (addition, removal, rewriting, etc.) and convert the transformed IR back to the bytecode format, you will get what you described.
For detailed algorithms on how to convert bytecode to an IR, you can refer to Section 3.3 of http://suif.stanford.edu/~jwhaley/papers/mastersthesis.pdf and Section 3 of https://courses.cs.washington.edu/courses/cse501/01wi/project/sable-thesis.pdf

How to protect a java application from reverse-engineering or code stealing? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 20 days ago.
Improve this question
I want to reveal my desktop java application to public, but I'm not sure how to protect it from reverse-engineering or source code stealing?
Is it possible to convert the application to an exe file? and if it is, wouldn't that be enough to protect it?
If it's really worth someone's time to reverse engineer your source from the binary, they will. You might be able to make it slightly harder, but never impossible.
You can use a Java Obfuscator such as ProGuard.
ProGuard is a free Java class file shrinker, optimizer, obfuscator, and preverifier. It detects and removes unused classes, fields, methods, and attributes. It optimizes bytecode and removes unused instructions. It renames the remaining classes, fields, and methods using short meaningless names. Finally, it preverifies the processed code for Java 6 or for Java Micro Edition.
A Java executable and a native executable are both just a bunch of machine code (with optional annotations and stuff). So there's essentially no difference in terms of how easy it is to reverse-engineer.
If you are compiling with javac, you can use the -g:none flag to eliminate all debug info from being compiled into the executable.

Categories

Resources