Archive

Posts Tagged ‘Java’

What language was an executable originally written in?

April 11th, 2010 Derek-Jones No comments

Apple have recently added an unusual requirement to the iPhone developer agreement “Applications must be originally written in Objective-C, C, C++, or JavaScript …”. As has been pointed out elsewhere the real purpose is stop third party’s from acquiring any control over application development on Apple’s products; the banning of other languages is presumably regarded as acceptable collateral damage.

Is it possible to tell by analyzing an executable what language it was originally written in?

There are two ways in which executables contain source language ’signatures’. Detecting these signatures requires knowledge of specific compiler behavior, i.e., a database of information about the behavior of compilers capable of creating the executables is needed.

Runtime library. Most compilers make use of a language specific runtime library, rather than generating inline code for some kinds of functionality. For instance, setjmp/longjmp in C and vtables in C++.

The presence of a known C runtime library does not guarantee that the application was originally written in C; it could have been written in Java and converted to C source.

The absence of a known C runtime library could mean that the source was compiled by a C compiler using a runtime system unknown to the analyzer.

The presence of a known Java, for instance, runtime library would suggest that the original source contained some Java. This kind of analysis would obviously require that the runtime library database not restrict itself to the ‘C’ languages.

Compiler behavior patterns. There is usually more than one way in which a source language construct can be translated to machine code and a compiler has to pick one of them. The perfect optimizing compiler would always make the optimal choice, but real compilers follow a fixed pattern of code generation for at least some language constructs (e.g., initialization of registers on function entry).

The presence of known code patterns in an executable is evidence that a particular compiler has been used; how much depends on the likelihood it could have been generated by other means and how many other patterns suggest the same compiler. In the case of the GNU Compiler Collection the source language might also be Fortran, Java or Ada; I don’t know enough about the behavior of GCC to provide an informed estimate of whether it is possible to recognize the source language from the translated form of constructs shared by several languages, I suspect not.

The fact that an executable can be decompiled to C is not a guarantee that it was originally written in C.

Some languages support source language constructs whose corresponding machine code is unlikely to ever be generated by source from another language. The Fortran computed goto allows constructs to be written that have no equivalent in the other languages supported by GCC (none of them allow statement labels appearing in a multi-way jump to appear before the jump test):

10    I=I+1
20    J=J+1
       goto (10, 20, 30, 40) J
30    I=I+3
40    I=I*2

The presence of a compiled form of this kind of construct in the executable would be very suggestive of Fortran source.

Apple are famously paranoid and control freakery. It will be very interesting to see what level of compliance checking they decide to perform on executables submitted to the App Store.

On another note: What does “originally written” mean? For instance, many of the mathematical functions (e.g., sine, log, gamma, etc) contained in R were originally written in Fortran and translated to C for use in R; this C source is what is now maintained. Does this historical implementation decision mean that R cannot be legally ported to the iPhone?

Register vs. stack based VMs

September 17th, 2009 Derek-Jones 3 comments

Traditionally the virtual machine architecture of choice has been the stack machine; benefits include simplicity of VM implementation, ease of writing a compiler back-end (most VMs are originally designed to host a single language) and code density (i.e., executables for stack architectures are invariably smaller than executables for register architectures).

For a stack architecture to be an effective solution two conditions need to be met:

  • The generated code has to ensure that the top of stack is kept in sync with where the next instruction expects it to be. For instance, on its return a function cannot leave stuff lying around on the stack like it can leave values in registers (whose contents can simply be overwritten).
  • Instruction execution needs to be generally free of state, so an add-two-integers instruction should not have to consult some state variable to find out the size of integers being added. When the value of such state variables have to be saved and restored around function calls they effectively become VM registers.

Cobol is one language where it makes more sense to use a register based VM. I wrote one and designed two machine code generators for the MicroFocus Cobol VM and always find it difficult to explain to people what a very different kind of beast it is compared to the VMs usually encountered.

Parrot, the VM designed as the target for compiled PERL, is register based. A choice driven, I suspect, by the difficulty of ensuring a consistent top-of-stack and perhaps the dynamic typing of the language.

On register based cpus with 64k of storage the code density benefits of a stack based VM are usually sufficient to cancel out the storage overhead of the VM interpreter and support a more feature rich application (provided speed of execution is not crucial).

If storage capacity is not a significant issue and a VM has to be used, what are the runtime performance differences between a register and stack based VM? Answering this question requires compiling and executing the same set of applications for the two kinds of VM. Something that until 2001 nobody had done, or at least not published the results.

A comparison of the Java (stack based) VM with a register VM (The Case for Virtual Register Machines) found that while the stack based code was more compact, fewer instructions needed to be executed on the register based VM.

Most VM instructions are very simple and take relatively little time to execute. When hosted on a pipelined processor the main execution time overhead of a VM is the instruction dispatch (Optimizing Indirect Branch Prediction Accuracy in Virtual Machine Interpreters) and reducing the number of VM instructions executed, even if they are larger and more complicated, can produce a worthwhile performance improvement.

Google has chosen a register based VM for its Android platform. While licensing issues may have been a consideration there are a number of technical advantages to this decision:

  • A register VM is likely to have an intrinsic performance advantage over a stack VM when hosted on a pipelined processor.
  • Byte code verification is likely to be faster on a register VM (i.e., faster startup times) because stack height integrity checks will be greatly simplified.
  • A register VM will be more forgiving of incorrect code (in the VM, generated by the compiler, code corrupted during program transmission or storage attacked by malware) than a stack VM.

Assuming compilers are clever enough (part 1)

May 12th, 2009 Derek-Jones 1 comment

Developers often assume the compiler they use will do all sorts of fancy stuff for them. Is this because they are lazy and happy to push responsibility for parts of the code they write on to the compiler, or do they actually believe that their compiler does all the clever stuff they assume?

An example of unmet assumptions about compiler performance is the use of const in C/C++, final in Java or readonly in other languages. These are often viewed as a checking mechanism, i.e., the developer wants the compiler to check that no attempt is made to, accidentally, change the value of some variable, perhaps via code added during maintenance.

The surprising thing about variables in source code is that approximately 50% of them don’t change once they have been assigned a value (A Theory of Type Qualifiers for C measurements and Automatic Inference of Stationary Fields for Java).

Developers don’t use const/final qualifiers nearly as often as they could. Most modern compilers can deduce if a locally defined variable is only assigned a value once and make use of this fact during optimization. It takes a lot more resources to deduce this information for non-local variables; developers want their compiler to be fast and so implementors don’t won’t them waiting around while whole program analysis is performed.

Why don’t developers make more use of const/final qualifiers? Is this usage, or lack of, an indicator that developers don’t have an accurate grasp of variable usage, or that they don’t see the benefit of using these qualifiers or perhaps they pass responsibility on to the compiler (program size seems to grow sufficiently fast that whole program optimization often consumes more memory than likely to be available; and when are motherboards going to break out of the 4G limit?)

Using local context to disambiguate source

February 12th, 2009 Derek-Jones No comments

Developers can often do a remarkably good job of figuring out what a snippet of code does without seeing (i.e., knowing anything about) most of the declarations of the identifiers involved. In a previous post I discussed how frequency of occurrence information could be used to help parse C without using a symbol table. Other information that could be used is the context in which particular identifiers occur. For instance, in:

f(x);
y = (f)z;

while the code f(x); is probably a function call, the use of f as the type in a cast means that f(x) is actually a definition an object x having type f.

A project investigating the analysis of partial Java programs uses this context information as its sole means of disambiguating Java source (while they do build a symbol table they do not analyze the source of any packages that might be imported). Compared to C Java parsers have it easy, but Java’s richer type system means that semantic analysis can be much more complicated.

On a set of benchmarks the researchers obtained a very reasonable 91.2% accuracy in deducing the type of identifiers.

There are other kinds of information that developers probably use to disambiguate source: the operation that the code is intended to perform and the identifier names. Figuring out the ‘high level’ operation that code performs is a very difficult problem, but the names of Java identifiers have been used to predict object lifetime and appear to be used to help deduce operator precedence. Parsing source by just looking at the identifiers (i.e., treating all punctuators and operators as whitespace) has been on my list of interesting project to do for some time, but projects that are likely to provide a more immediate interesting result keep getting in the way.

www.wenn.com
FireStats icon Powered by FireStatswww.tinynibbles.com cialis and canada custom

ganeric cialis

buy cheapest propecia

cheap viagra online

levitra low price

order cheapest propecia online

order propecia

buy viagra germany canadian meds

get cialis online

online propecia uk

cialis vs levitra

how strong is 5 mg of cialis

gele viagra

buy cialis without prescription

cost levitra low

cialis on women

online cheap viagra

how much does cialis cost

generic cialis next day shipping

buy levitra overnight

cheapest viagra

lowest price levitra

generic levitra cheap

best price propecia

online levitra

best price cialis

buy generic levitra

canada generic propecia

online viagra gel to buy

canada viagra generic

canadian viagra

levitra viagra online

canada viagra pharmacies scam

buy propecia now

cheapest propecia prescription

cialis overnight delivery

levitra cost

buy cialis once daily

discount cialis india

online generic cialis 100 mg

canada viagra

levitra sales uk

order cheap levitra

buy cheap generic propecia

generic cialis soft tabs

buy cialis usa

cheap levitra without prescription

brand name cialis

cheap propecia online

order generic levitra

buy levitra uk

mail order propecia

5 mg original brand cialis

cheapest propecia sale uk

how to buy cialis in canada

buying levitra online

low cost propecia

generic levitra online

next day viagra

buy cheap levitra

levitra for sale

canada propecia prescription

cialis no prescription

buy prescription propecia without

discount propecia propecia

cialis quick shipment

cheap levitra

levitra cheap fast

generic propecia 5mg

cheap levitra tablets

generic propecia finasteride

discount propecia online

how much to buy viagra in pounds

how much cialis

brand viagra professional

brand viagra over the net

canadian pharmacy

cialis daily

levitra in canada

levitra viagra cialis

generic propecia fda approved

cialis 5 mg buy

levitra next day delivery

cialis uk

cialis buy overnight

buy real viagra online

cialis pharmacy

online propecia prescription

cheap viagra from uk

canadian viagra and healthcare

indian cialis

buy cialis for daily use

cheap levitra prescription

levitra canadian

buy viagra mexico

cheap viagra canada or india

generic propecia online pharmacy

canada online pharmacy propecia

cheap prescription propecia

buy viagra without prescription

generic levitra purchase

cheap canadian viagra

cheap cialis

cialis generic 100 mg

how much is viagra

discount us propecia

best way to use cialis

generic viagra canadian

buy levitra online no prescription

canada online pharmacy levitra

generic levitra canada

cialis professional 20 mg

buying cialis soft tabs 100 mg

cialis en mexico

drug generic propecia

ordering propecia online

cheap cialis soft

cheap levitra uk

generic propecia effective

brand cialis for sale

buy cialis in usa

ordering cialis gel

cialis strenght mg

levitra 10mg

generic propecia sale

cialis price

buying cialis next day delivery

buy real cialis

levitra pill

buy discount viagra

obtain viagra without prescription

best price for propecia

canadian pharmacy viagra

brand name cialis overnight

mexico levitra

cialis purchase

once a day viagra

healthcare canadian pharmacy

cialis 100 mg

buy can from i propecia who

buy generic propecia

generic levitra vardenafil

generic viagra canada

cialis transdermal

buy levitra us

canadian healthcare

cialis professional no prescription

cialis prescription

canadian healthcare viagra

how to get cialis in canada

levitra mg

cialis 5 mg italia

buy propecia in the uk

generic propecia for sale

can i get viagra in mexico

how to get viagra

buying generic cialis mexico rx

buy viagra online

levitra online sales

discount drug propecia

cialis woman

buy propecia online prescription

name brand cialis

cialis next day delivery

buy cialis fedex shipping

generic viagra made in usa

cialis price in canada

levitra in india

canadian pharmacy cialis

info levitra

buy now propecia

order levitra online

buy propecia canada

cialis price 100 mg

cialis by mail

buy online prescription propecia

online pharmacy propecia viagra

cialis 100 mg generic

buy generic cialis

i need to buy propecia

cialis tablets foreign

discount levitra rx

best viagra

indian generic levitra

cheap propecia 5mg

cialis 5 mg

buy propecia generic

generic propecia alternative

cialis to buy

hydrochlorothiazide cialis

canada meds viagra

cialis daily in canada

natural viagra

indian viagra

cialis fast delivery usa

cheap propecia no prescription

daily dosage cialis

bio viagra herbal

cheap cialis from india

buying viagra in canada

canadian drugs propecia

buy propecia on line

levitra discount

cheap propecia uk

buying cialis

buy propecia prescriptions online

online ordering propecia

levitra online us

once daily cialis

best price levitra

buying propecia

buy cheap generic levitra

buying propecia online

overnight delivery cialis

get levitra

cialis fast delivery

female viagra pills

cialis refractory

order viagra or levitra

cheap propecia online prescription

cialis one a day

get propecia online pharmacy

cialis from mexico

cialis and ketoconazole

levitra now online

levitra from canadian pharmacy

buy viagra online cheap us

buy levitra online from canada

canada levitra

cheapest price propecia cheap

cialis alternative

cialis profesional

discount generic propecia

buy propecia online from usa pharmacy

canadian online pharmacy cialis

order prescription propecia

generic cialis from india

levitra prescription

levitra order prescription

levitra online

mexico pharmacy cialis

buy propecia without prescription

low cost canadian viagra

cheap fast levitra

buy propecia where

getting cialis from canada

cialis 50 mg

generic levitra overnight delivery

cialis delivered overnight

online propecia prescriptions

levitra mail order

overnight delivery viagra

mail online order propecia

buy canada levitra

buy propecia online pharmacy

does generic cialis work

buy cheap levitra online

buy propecia online

cheap order prescription propecia

canadian viagra india

cialis cheap

online pharmacy propecia renova

cialis online

buy 5 mg cialis

generic cialis sale

buy cialis cannada

levitra where to buy

buy viagra

discount levitra online

low price levitra

lowest price on non generic levitra

buying online propecia

cialis professional 100 mg

cialis for woman

levitra buy online

best price generic propecia

combine cialis and levitra

next day delivery cialis

lowest propecia prices

buy cialis canada

bestellen levitra online

levitra online no prescription

buy generic viagra india rx

cialis tablets

canadian pharmacies cialis

generic viagra 100 mg

lowest propecia prices in canada

canadian healthcare pharmacy

cialis cheap us pharmacy

cialis headaches

buy dosages levitra

buy propecia cheap

cialis dosage mg

herbal propecia

5 mg daily cialis

cost of viagra

buy levitra online viagra

cheapest propecia uk

cialis discount

levitra online prescription