Run time machine code generation and execution

Some Common Lisp implementations (and I believe other languages’ also, but I don’t know of any) are incremental compilers, which means they are able to read Lisp code during program execution, compile it and make it immediately available for execution, without requiring the program to be restarted.

I find that quite amazing, so I decided to try it out for myself. The following code contains a function that generates an array of opcodes for a function that returns an integer passed as the first function’s argument. After calling the function, it is possible to convert the returned pointer to a function pointer and call it.

#include <stdio.h>
#include <stdlib.h>
 
char* makefunction(int ret)
{
    char* opcodes = malloc(6);
    opcodes[0] = 0xb8;
    *((int*)(opcodes + 1)) = ret;
    opcodes[5] = 0xc3;
    return opcodes;
}
 
int main()
{
    char* code = makefunction(20);
    int (*f)() = (int(*)()) code;
    int a = f();
    printf("%d\n", a);
    return 0;
}

That should print “20″ to the console.

What’s happening here? Here’s a function that returns 2 when called:

int f(void)
{
    return 2;
}

This is the output of objdump -d after compiling it:

f.o:     file format elf32-i386

Disassembly of section .text:

00000000 :
   0:	55                   	push   %ebp
   1:	89 e5                	mov    %esp,%ebp
   3:	b8 02 00 00 00       	mov    $0x2,%eax
   8:	5d                   	pop    %ebp
   9:	c3                   	ret

The first two instructions, as well as the fourth (push, mov and pop), are stack-related code the compiler generates for all functions. So what really accomplishes the task of returning 2 is the following opcode sequence:

b8 02 00 00 00 c3

By checking this reference, I verified that b8 moves a value to the EAX register, which is commonly used to store function return values. The other 4 bytes correspond to the value that should be stored in that register. c3 is the opcode for ret, which returns from a function call (Captain Obvious, 2010).

Now let’s see what makefunction does:

  1. Allocates memory for 6 opcodes:
    char* opcodes = malloc(6);
  2. Writes the opcode for moving a value into the EAX register into the array:
    opcodes[0] = 0xb8;
  3. Writes the actual value that should be moved into EAX
  4. *((int*)(opcodes + 1)) = ret;
  5. Writes the opcode for the ret instruction
    opcodes[5] = 0xc3;
  6. Returns the array of opcodes:
    return opcodes;

Back in main, The pointer to the opcode array is converted to a function pointer of the appropriate type:

int (*f)() = (int(*)()) code;

And then called as any other function:

int a = f();

This generates a call instruction, which causes the current value of the IP register to be saved and the execution to jump to the specified address. In this case, that address corresponds to the opcode array’s address, which contains valid instructions that will be executed by the processor.

The difference here in relation to regular function call code is that the code is being fetched and executed from the data section of the program, instead of the code section. I’m almost sure that will crash on processors with the NX bit enabled, but I haven’t checked it. Anyways, that’s an extremely simple example of how it is possible to generate machine code during run time and execute it right away.

Nerd Girls

Disclaimer: this post contains my personal view on a given matter. Don’t ask me for statistical data that supports what’s written here, because I don’t have it. These are simply my own perceptions and conclusions, which might be completely mistaken.

There’s this movement called Nerd Girls, and reading their website just made me laugh.

So, they want to encourage girls to enter the “science, technology, math and engineering” fields so that they can “change their world”. That’s actually a very nice initiative, and I would be willing to support it.

But there’s a problem.

The science and engineering fields are traditionally dominated by men. I believe that might be alredy intimidating for women. However, I believe the real issue is that many men in these fields act just like the character sitting on the chair in this xkcd comic.

What’s the problem with Nerd Girls? Check out the Profiles section on their website. It does a really lousy job at trying to get women to be respected in science and engineering. The Nerd Girls website doesn’t look like one which is trying to promote the inclusion of women in specific academic/market fields. Pictures like Casey’s bottommost one will only encourage men in science and engineering (I mean, men who have such an attitude) to continue having an idiotic attitude towards women in their fields, thus keeping them away.

So please, Nerd Girls, get serious.

Propositional logic symbols in LaTeX

Found this while doing some backups. Might be useful to someone.

logicsymbols

Solving linker errors OpenThreads/Atomic on OSG projects on Mac OS X 10.6

I’ve just spent two days trying to figure out what was wrong with an OSG application I was not being able to build on Mac OS X 10.6. After importing all necessary OSG frameworks, I kept constantly getting linker errors saying the symbols OpenThreads::Atomic::operator--() and OpenThreads::Atomic::operator--() couldn’t be found.

It turns out it was a binary compatilbility issue. I built the 32-bit Debug Carbon configuration of OSG 2.8.3 from the Xcode project files that come in the source distribution (not the ones generated by CMake). The problem is that the Deployment Target is set to Mac OS X 10.4 in that project:

screen-shot-2010-06-11-at-120254-am

As far as I know, there’s no binary compatibility with libraries targeted at Mac OS X 10.4 or older starting with SDK 10.5. So, I was trying to build an application using the 10.6 SDK, but linking it to libraries built for Mac OS X 10.4.

Therefore, the solution was very simple (took me 2 days to find it, however). I just had to change the Deployment Target in the OSG Xcode project settings to 10.5 and rebuild it:

screen-shot-2010-06-11-at-120320-am

I first changed the Deployment Target to 10.6, but I wasn’t able to build OSG then. So I changed it to 10.5. I didn’t take the time to figure out what was wrong when targeting at 10.6, though.

Building OpenCV 2.0 on Mac OS X 10.6

I first tried it this way:


$ cd OpenCV-2.0.0
$ mkdir build
$ cd build
$ cmake ..
$ make

But it failed. I got lots of errors when trying to link highgui to the Carbon libraries. The problem is that Carbon is 32-bit only, and by default OpenCV was being built for 64-bit.

Here the (very simple) solution to that:


$ CFLAGS="-arch i386" CXXFLAGS="-arch i386" cmake ..
$ make

Worried

I’ve lost about 8 kilograms in the past 5~6 months. While at first that’s a good thing (I was getting a little fat), it makes me worried because I made no effort at all to lose that much weight. However, I feel fine, except for an occasional dizziness that usually strikes me in the morning.

I’ll go to a doctor, but they always tell me I’m “too anxious” and that’s all, no matter what symptoms I have. That’s actually quite good, but it’s also a waste of time and money :(

Why Lua’s 0 (zero) as a true value makes sense

When I started to learn about Lua, I found it a little odd that the numeric 0 (zero) value is treated as a true boolean value by the interpreter, which was different from all other programming languages I had worked with.

However, it turns out that it makes a lot of sense to have 0 as a true value, and that’s when you take short circuit expressions into account.

For those who don’t know, short-circuit expressions are used to conditionally select values without having to code that selection into an if-elseif-else structure. Short-circuit expressions are built using logical (and, or, not) operators. The and operator returns the value of the first false expression, or the last expression, in case all others were true. The or operator returns the value of the first true expression, or the last expression, in case all others were false. Here are a few examples in Lua:

Code:

word = 1 < 2 and "hello" or "world"
print(word)

Output:

hello

Code:

level = 10 > 9 and 2 or 4
print(level)

Output:

2

Now, consider the following Lua short-circuit expression:

bit = math.random() < 0.5 and 0 or 1

Running it many times will psuedo-randomly assign both 0 and 1 values to the bit variable:

Code:

for i=1,8 do
    bit = math.random() < 0.5 and 0 or 1
    print(bit)
end

Output:

0
1
1
1
0
0
1
0

Now, consider what happens when we try to do the same thing in Python 2.6:

Code:

import random
for i in range(8):
    bit = random.random() < 0.5 and 0 or 1
    print(bit)

Output:

1
1
1
1
1
1
1
1

The first operand of the and expression is always false, because 0 is considered to be a false boolean value by the Python interpreter.

Even though one could argue that it’s just a matter of knowing how to rewrite such logical expressions, I believe it’s good not having to worry about undesired behavior caused by 0’s in short-circuit expressions. It allows me to write expressions just as they come to my mind, which makes me more productive than if I had to re-arrange them.

Creationism vs. Evolutionism

As a Christian, I’m just growing sick of that absolutely unfruitful discussion. I can’t stand listening to what theologians have to say, nor can I stand what atheists have to say. It’s all a huge load of garbage.

Here are my takes on the issue:

  1. People should only talk about what really concerns them. For me, a theologian who tries to force his faith into scientific reasoning is just as mistaken as the atheist who attempts to prove his unbelief using materialist arguments.
  2. Following the same line of reasoning from the item above, scientific books aren’t religious texts, nor are religious texts scientific books. As Francis Collins points out in The Language of God, scientific books describe the natural world, while religious books (for me, not bookS, only the Christian Bible) talk about the spiritual world.
  3. The whole thing doesn’t make a difference at all for me. The Book of Genesis presents an entirely spiritual issue (the fall of mankind). Whether things happened literally as described there, or if God used evolution as the mechanism for creation, is something that I don’t care much about. I find both alternatives equally admirable in my Creator.
  4. If people want to talk about science, they should talk about science. And if the want to talk about faith, they should talk about faith. Don’t try to mix them. Personally, I believe there is perfect agreement between them, but that agreement doesn’t seem to have been found yet. And we should probably not try to find it: up until now, it has proven to be quite an unhealthy thing.

Maybe I have some more points in the back of my mind, but I think the three above are the main ones. Those are personal opinions. I am NOT trying to convince anyone here. The only thing I would like to ask people for is:

Please stop talking about what you don’t know (or understand)!

And that goes to people in both sides of the issue.

A word for my fellow brothers in Christ: concentrate on what really matters, which is the Gospel of our Lord Jesus. It’s about love, forgiveness, wisdom, tolerance, respect, dignity, honor, and a bunch of other really good things. If you ever happen to discover if the universe was created in literal 7 days or if God decided to take billions of years to create will very likely not add a single bit to the kind and loving character you were taught to pursue.

Filmes do feriado

Neste feriado, estudei pra caramba, mas também deu pra assistir vários filmes:

Só não gostei do Goal! III. A história não tem nada a ver com o Goal II, que termina com “To be continued…”. Perdeu totalmente o sentido, e foi um filme fraco. Thinner não faz meu estilo, mas é um bom filme, assim como The Machinist, que eu achei muito pra baixo. Os outros dois, achei bem legais.

“Doppler effect” kitchen tiles

The tiles in my kitchen remind me of some Doppler effect illustrations I have seen:

"Doppler Effect" kitchen tiles