News for hackers. Stuff that matters.
Re:Function hooking in Swift by zw on Thursday June 19, 2014 @01:26AM (#7914590)
The "function trampoline" that's described looks more or less like the Block ABI. Some good learning is available in the source for `libclosure`, [Language Specification for Blocks](http://www.opensource.apple.com/source/libclosure/libclosure...) and [Block Implementation Specification](http://www.opensource.apple.com/source/libclosure/libclosure...).

----

Consistently being able to hook functions in Swift is going to be a pain in release mode optimizations, as the Swift compiler does aggressive inlining.

Re:Function hooking in Swift by blarney_stone on Thursday June 19, 2014 @10:24AM (#7916198)
I've seen this concern that there are no function pointers in swift, and it seems to come from a misunderstanding on what it means for functions to be first class.

From https://developer.apple.com/library/prerelease/ios/documenta... :

You use function types just like any other types in Swift. For example, you can define a constant or variable to be of a function type and assign an appropriate function to that variable:

var mathFunction: (Int, Int) -> Int = addTwoInts

mathFunction can now be passed around, fed in as a parameter in another function, etc. mathFunction is for all purposes a function pointer.

Re:Function hooking in Swift by ololoev on Thursday June 19, 2014 @12:00PM (#7916784)
Well, there isn't any C-style function pointer (means you can not get it's raw address in memory to do something with it), but yeah — you can pass functions as arguments.
Re:Function hooking in Swift by thinkpad20 on Wednesday June 18, 2014 @11:29PM (#7914266)
Very interesting write-up, thanks! Makes me want to get back into C programming :) How did you manage to learn all of this stuff, for a new, closed-source and relatively undocumented (e.g. @asmname) language?
Re:Function hooking in Swift by ololoev on Thursday June 19, 2014 @12:34AM (#7914494)
It's all about reverse-engineering stuff; my usual workflow looks like: (1) Complile some Swift program & look at the disasembler listing of it (I use IDA and — for Objective-C — Hopper Disassembler); (2) Do some changes in source code in the program, compile it again, and see diffs in disassembler listing; that way I can see what is changed and how.

About the @asmname thing: you can easily dump every default Swift module using a REPL command:

repl> :print_module Swift

There you can find a lot of interesting language lexems and more, so all you have to do is to try using them in your code until you don't figure it out ;)

Re:Function hooking in Swift by bthdonohue on Thursday June 19, 2014 @09:58AM (#7916068)
const char type:1; // I'm not sure about this and padding, char padding[7]; // maybe it's just a uint64_t too...

This is for struct alignment to address boundaries. It's fairly typical C and results in some performance optimizations.

Re:Function hooking in Swift by milkofthesun on Wednesday June 18, 2014 @11:30PM (#7914276)
Boo. Hoo.
Re:Function hooking in Swift by collyw on Thursday June 19, 2014 @07:54AM (#7915612)
I know its a new language, but does every blog post about Swift now need to show up on HN front page? I thought it was bad for "anything JS related" posts here.