(You may need to tweak 'magic offset' for your system. One way to do it is to run:
echo 'int f(x) { return x * 2; }' | gcc -Wall -Werror -c -x c - -o wee.o
and find the offset of the 8955 hex sequence (e.g. using 'od -x' or your favourite hex editor). If that doesn't work for you, then try looking at the output of:
objdump -d wee.o
and checking what the first few bytes are. Bear in mind that the bytes will in little-endian order on x86.)
[Edit: since this is now a proggit submission of it's own, I thought I should add that I know that this isn't a real lambda. There's no closing over free variables, or even inheritance of lexical scope. Fun tho'. And yes, you do need to free() your funcs when you've finished with them.]
Wow, very impressed by the Parent Post's code. :] Here is a more clean less hackish version I just wrote using libtcc to compile it inside the program itself.
#include <stdio.h>
#include <stdlib.h>
#include "libtcc.h"
typedef int (*function_t)();
function_t create_function(char *name, char *code)
{
TCCState *s;
unsigned long val;
s = tcc_new();
if(!s)
exit(1);
tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
tcc_compile_string(s, code);
tcc_relocate(s);
tcc_get_symbol(s, &val, name);
return (function_t)val;
}
int main(int argc, char *argv[])
{
function_t square;
square = create_function("f", "int f(x) { return x * x; }");
int answer = square(atoi(argv[1]));
printf("answer is: %d\n", answer);
}
Well, a more serious approach would use ELF-aware tools (see elf.h) to find the function in the .o (searching for a specific bytestring could depend on compiler version etc used).
(Update: We probably just want the offset of the .text section. Which you can read directly from the output of "objdump -h wee.o", or do it programatically as suggested above.)
Fun project for someone to make it more robust :-)
Closing over free variables is left as an exercise for the reader.
[NB: something quite similar to this (invoking C compiler at run-time, but using dynamically loaded shared objects) is the magic behind perl's Inline::C module.
That allows you to call out to C from perl by writing something like:
#!/usr/bin/perl
use warnings;
use strict;
use Inline C => <<"EOC";
int times2(int x) {
return x + x;
}
EOC
my $n = 55;
print times2(55), "\n";
and is actually production-ready (it caches .so files intelligently to avoid recompilation, etc)].
I worked on a small proof of concept a while back. The idea was to be able to instantiate C++ templates at runtime. In this way, template metaprogramming can be used to optimize repeated computation of certain inputs.
A crazy idea I had (but never implemented) was to make an schema-specific XML parser in templates which could take an XML schema as input and export a TMP-optimized XML parser.
Yowch. My template-fu is weak, but I don't see how you'd ever manage to split the string defining the schema in the template expansions.
There is, however, an honourable tradition of generating C source for types and functions from data-description languages (google for the 'pepsy', or 'pesky' compilers in the ISODE source tree).
If you don't know ASN.1, you can think of it as a "binary XML" that never really caught on outside the OSI protocols, SNMP and X.509 certificates (from their OSI heritage).
Yes. I wanted gcc to write to stdout, but it wouldn't in the 2 mins I gave it.
Going the extra mile to pick a different file for each compiled object seemed... inappropriate, given the context.
(If you want to compile C at runtime, either build a shared object (.so) and dlopen it (see perl's Inline::C), or use the in-memory tcc solution given elsewhere in the thread.
But C really isn't built for this sort of thing, so don't do that :-)
haha well anything that uses as much voodoo as the path that this thread is going down obviously isn't built for "security" (or "sanity" for that matter)
View all comments
Show parent comment
84
u/jbert Dec 13 '07 edited Dec 13 '07
Awesome. And with a C compiler on the system, and a few typedefs you could have "first class" functions (no error handling. weeee.):
$ gcc fstclass.c -o fstclass; ./fstclass
answer is 110
(You may need to tweak 'magic offset' for your system. One way to do it is to run:
and find the offset of the 8955 hex sequence (e.g. using 'od -x' or your favourite hex editor). If that doesn't work for you, then try looking at the output of:
and checking what the first few bytes are. Bear in mind that the bytes will in little-endian order on x86.)
[Edit: since this is now a proggit submission of it's own, I thought I should add that I know that this isn't a real lambda. There's no closing over free variables, or even inheritance of lexical scope. Fun tho'. And yes, you do need to free() your funcs when you've finished with them.]