wingolog

abusing the c compiler

7 September 2010 8:43 PM (larceny | scheme | ffi)

code reading

Today I found something really neat in Larceny's foreign function interface. The deal is that often times you need to parse a C structure or a preprocessor definition, and man, parsing C makes a body feel lazy. What's a hacker to do?

Larceny has an amusing take on this problem. The code looks straightforward enough:

;; parse out ent->d_name as a string
(define (dirent->name ent)
  (define-c-info (include<> "dirent.h")
    (struct "dirent" (name-offs "d_name")))
  (%peek-string (+ ent name-offs)))

The define-c-info block calculates name-offs, which is the offset of d_name in the dirent structure. %peek-string is something internal to Larceny that takes a memory address of a NUL-terminated C string and returns a Scheme string.

I had imagined, looking at this, that they had some kind of database of the headers and such, and in a sense they do -- in the form of the C compiler. define-c-info is a macro that runs the C compiler at macro expansion time, compiling and running a generated C program that spits out the relevant information as an s-expression on its stdout.


some people like diagrams

So in this case, if the d_name field starts 11 bytes into the structure, the generated C program will print out (11) on its stdout, and that number gets read in and inserted into the program. In that way dirent->name expands to something like:

(define (dirent->name ent)
  (define name-offs 11)
  (%peek-string (+ ent name-offs)))

Cool, no? The C compiler is only needed at compile-time, not at run-time.

Further details can be seen at Felix Klock's 2008 paper on Larceny's FFI.

22 responses

  1. jpc says:

    The first time I have seen the idea to use the C compiler like that was in Scheme->C [1] compiler (cdecl/sizeof.c). But to be precise it did it only once and afterwards used the collected sizeof+padding info.

    There is also a very neat idea like this in the Go implementation. [2] They parse the debugging information from the assembly output of the host compiler.

    [1]: https://alioth.debian.org/plugins/scmgit/cgi-bin/gitweb.cgi?p=scheme2c/scheme2c.git;a=blob;f=cdecl/sizeof.c
    [2]: http://golang.org/cmd/godefs/

  2. andrei says:

    Although in Scheme->C that's a bit pointless and one day when I have time it will get simplified. Since we can inline C code we can just use the offsetof macro.

    http://0xab.com/code/matlab-scheme.git/blob/HEAD:/c-macros.sch#l87

  3. robux generator says:

    The designer of the hack tool must be able to guarantee you that the web servers used have a long-term link with Roblox games. It is rather straightforward making use of the free roblox robux generator.

  4. opera browser says:

    If you are getting any errors to Download Opera Mini for PC or Laptop using bluestacks app gamer after that you can follow this below motioned method to complete this pending procedure.

  5. Hearts play online says:

    Hearts solitaire play online is best single player cards games, this is the best game you can play online at home, at your office in your free time

  6. xblivecodes.net says:

    Play styles and preferences across xbox one and MIcrosoft pc games. at the heart of direct x 12 is than ever before maximizing use of existing graphics hardware.

  7. playbox hd apk says:

    nice article, if you want to get free playbox hd apk, click on the link provided.

  8. windows 10 support says:

    this is the first time that I have seen the code like this .this is really a good information about the compiler.

  9. best-essay-writing-service.reviews/7dollaressay-com-review/ says:

    The function will tell the pre-processor usage and expects an argument in the middle of the function. C is a high level language and it is used by all the major services to develop software and operating systems. Pre processor usually runs the program in the kernel space and it only accepts binary values.

  10. how to get free robux says:

    hey guys if you are a gaer and want some interesting game then try roblox

  11. robux generator says:

    Hey guys I like to play online games like free robux game on internet free of cost with full fun and enjoy if you are interested to play so click on our page and play online this game.

  12. Survey Remover Tool says:

    really nice tool here to remove or bypass surveys online.

  13. list.ly says:

    thanks for the awesome article.

  14. No Guarantor Loans says:

    Very nice Article! Thanks for sharing.

  15. Nick Patel says:

    in between the online compilers ms outlook support is one of the useful compilers, and it is compiling with the single line, so the bug detection can be very easy with this type of compiler.

  16. HP Support says:

    This article is good. I have also an HP printer and I am facing issues can anyone tell me how can clear up HP LJ4100 error from Hp printer. And also printer going for walks too slow give some higher answer!!!

  17. Printer Offline Windows 10 says:

    These are not recommended in DTG printing because the white underbase will be showing through those semi-transparencies, creating a very speckled look, which can often look poor quality.

  18. 0xc0000225 says:

    Hey guys I like to play online games like free robux game on internet free of cost with full fun and enjoy if you are interested to play so click on our page and play online this game.

  19. uk assignment says:

    I had inconveniences when I utilized nexon launcher since it introduced maple on programfiles. I would experience the ill effects of stammering in RnJ and when murdering swarms. I uninstalled and downloaded maple straightforwardly from the site and introduced it in a registry. Runs better now and I don't slack like I used to. The slack from opening the world guide or missions has dependably been there for me. Simply persist it

  20. roblox hack says:

    Hey really such a amazing post dear. And guys if you want to hack roblox then must try my link.

  21. Saim Thomus says:

    One critical culmination is that, since Turing fulfillment permits the meaning of non-ending calculations, it is conceivable to express "C++ programs" whose accumulation is non-ending, which additionally infers that the compiler produces neither mistake messages nor question code. (By and by, compilers have a limit on format instantiation profundity to maintain a strategic distance from non-end.) Telstra Helpline
    In the accompanying I am will show several code bits containing non-ending format calculations, and how GCC (g++ 4.8.2) and Clang (clang++ 3.3) handle them.

  22. dialprintersupport says:

    HP Printer Technical Support team is a group of experts which deal with technical issues in your printer. In case you need any technical support contact us by dialing our toll-free number +1-888-902-8333 or visiting our website

Leave a Reply