Executing generated assembler inline

2019-07-08 23:46发布

问题:

I was reading the following presentation: http://wingolog.org/pub/qc-2012-js-slides.pdf which talks about (4,10,19) inline ASM generation as a technique used in Javascript optimisation.

In the following paper: https://sites.google.com/site/juliangamble/Home/Compilers%20Tutorial%202006-09-16.pdf?attredirects=0&d=1 at page 30 and 31 they talk about using scheme to generate ASM that is subsequently linked and executed in a subsequent OS process.

What about the scenario where you want to generate the ASM and execute it inside your existing process? (ie no subsequent link and execute in separate steps).

I assume this comes down to (in C for example) generating the ASM, writing the bytes to an area of memory (code as data) and adding a function header and return (compatible with a C caller (similar to what we see on page 3 of the Goloum paper above). Then we take that data pointer and convert it to a function pointer and call it (code as code - from code as data).

My questions are:

(a) What is the terminology for this inline code generation and execution?

(b) Is there a simple 'hello world' C example of this available?

回答1:

There is quite a good example here. Again they use the terminology of self-modifying code.

Another one here.

Here they give it four categories (use cases):

  • Metamorphism
  • Trampolining
  • JIT compilation
  • Security implications (insecure coding/malware)

There is a discussion here about whether LISP is truly self-modifying - and they end up concluding that it is not.