Previously, in “Thoughts on Thoughts on Drizzle” I theorized that one of the major reasons why we did not see lots of people jumping at stored procedures in MySQL was that it wasn’t in their native language (for lack of a better term). We’ve seen External Language Stored Procedures for MySQL that let you write stored procedures in some other languages…. but I felt something was missing.
Firstly, I wanted a language I was really familiar with and comfortable writing complex things in.
Secondly, it should be compiled so that it runs as fast as possible.
Thirdly, it shouldn’t just be linking to a pre-compiled library (drizzle function plugins do that already)
So… the obvious choice was C.
I have a really, really, really early prototype:
drizzle> SELECT LIBTCC("int foo(char* s) { s[0]='4'; s[1]='2'; s[2]=0; return 2; }") AS RESULT;
+--------+ | RESULT | +--------+ | 42 | +--------+ 1 row in set (0 sec)
or… a bit more sophisticated:
drizzle> SELECT LIBTCC("#include <string.h>\nint foo(char* s) { strcpy(s,\"Hello World!\");; return strlen(s); }") AS RESULT;
+--------------+ | RESULT | +--------------+ | Hello World! | +--------------+ 1 row in set (0 sec)
I’m using a function as a bit of a cheat… but the string is passed to libtcc (modified so it’s a shared library so I can load it into drizzle), where it is compiled into native object code (in my case x86-64) and then run.
With the right bits of foo… I could allow calling of all sorts of server functions…. such as those to execute SQL inside the current transaction context.
There are a number of reasons why this is Pure Evil(TM):
- It executes inside the address space of your database server
one null pointer dereference and your database server is all gone. - It’s arbitrary code injection by design
Exactly how insane are you? Security–; - While great for me and my C hacking friends, possibly not for web app developers, who likely aren’t writing their apps in C every day.
- See the first reason. Is that not reason enough? Memory protection is a good thing yo.
Anyway, you can see the code up on launchpad in the drizzle-libtcc-function branch. You’ll need to modify your tcc source so that the Makefile snippet for libtcc.o looks like this:
# libtcc generation and test libtcc.o: $(NATIVE_FILES) $(CC) -fPIC -o $@ -c libtcc.c $(NATIVE_TARGET) $(CFLAGS) libtcc.a: libtcc.o $(AR) rcs $@ $^ libtcc.so: libtcc.o $(CC) -shared -Wl,-soname,libtcc.so.1 -o $@ libtcc.o