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.

41 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

  23. ishagarg says:

    I have recently talked about the latest updated blog, This is one of the fabulous ways of serving the best choices thanks lot..

  24. Bangalore call Girls says:

    Happy to see this amazing article, you are giving us inspiration, inspiration. The extraordinary degree that you are scattered here is amazing. It helped me to refine me a ton. A responsibility of gratitude is for your efforts and support, keep sharing such things for each situation. Your blog is appreciated !!

  25. Hyderabad Escorts says:

    Thank you for posting such a great article! I found your website perfect for my needs. It contains wonderful and helpful posts. Keep up the good work!. Thank you for this wonderful Article!

  26. Brother Printer support number says:

    We are independent third party brother printer tech support provider in the USA. We have a team of certified experts for solving out the printer issues. We provide users the best possible solution in less time we also solve error via remote access. Dial Brother Printer support number +1-800-684-5649 ask your query.

  27. Printer Customer Support says:

    http://pastebin.com/u/goemasmith Experts gives the best and most reliable solutions for your printers. the experts can be reached anytime for help.

  28. HP printer repair near me says:

    Larceny that takes a memory address of a NUL-terminated C string and returns a Scheme string.

  29. chocolartshop says:

    I learn scripting languages step by step since it is so convoluted and the new individual did not learn at one time. I am hoping to gain more from you.

  30. utorrent download says:

    Currently, uTorrent's software is considered safe and free of dangerous malware. The primary risk of using uTorrent comes not from the software itself, but the risk of downloading dangerous or infected torrents from unknown sources.

  31. buy real targeted instagram followers says:

    Buy Instagram and place the list of the most popular satanin. Twitter, youtube, facebook and igtv services are available. Buy quality and real followers. You can place an order with our 24/7 customer service support.

  32. best tent camp in jaisalmer says:

    This program can be designed to do nearly everything. The setup is finished amid aggregate time by composing in C code what one might want the program to perform. An unimportant model is given in the 'how to order' segment, yet the conceivable outcomes are unmistakably restricted just by your creative ability and programming aptitudes

  33. stream complet says:

    Les extraterrestres ont envahi la Terre. Occupée, Chicago se divise entre les collaborateurs qui ont juré allégeance à l’envahisseur et les rebelles qui les combattent dans la clandestinité depuis dix ans.
    regarder ce film en streaming gratuit sur streamcomplet

  34. HP Printer Setup says:

    Once you buy a new HP printer it is very necessary to setup it correctly to get printout easily. If you are not able to configure your HP printer correctly then contact experts to resolve HP Printer Setup related issues. You will save your time by having expert’s guide to setup printer.

  35. Sockshare says:

    Its an amazing website, really enjoy your articles. Helpful and interesting too. Keep doing this in future. I will support you.

  36. GoStream says:

    No one can refuse the value of your article, but should you add more picture for reader can easily understand about what you want to transmit. Anyways, thank for your shared.

  37. XMovies8 says:

    Your opinion are very interesting, attractive article, so i shared it on my facebook. Promise that a lot of people will enjoy it.

  38. Jav Hot says:

    Thanks for sharing this wonderful post, blog comments really help us grow because we are building relationship with others and also it help us build SEO and also comments makes our blog alive and google also index comments. I would appreciate if you include my blog to your list of blog commenting sites.

  39. Canon Printer Offline says:

    Printer offline is one of the most common issues faced by printer users. Some technical peoples resolve this issue by yourself. But sometimes it is hard for the non-technical person to resolve this issue. You can get help from experts to resolve Canon Printer Offline issue.

  40. Jack pontiing says:

    sdgfsd

  41. AOL Gold download says:

    AOL desktop gold is famous software that enhances the security of email application. Installation of this software is not an easy task. To understand how to perform AOL Gold download connect to our experts. A team of experts is always available to handle customers’ queries.

Leave a Reply