I'm having some problems with apparently getting wrong results from pthread_getspecific in a library that's designed to link into various iOS apps.
I see that Apple writes:
Cocoa and POSIX store the thread dictionary in different ways, so you cannot mix and match calls to the two technologies. As long as you stick with one technology inside your thread code, however, the end results should be similar. In Cocoa, you use the threadDictionary method of an NSThread object to retrieve an NSMutableDictionary object, to which you can add any keys required by your thread. In POSIX, you use the pthread_setspecific and pthread_getspecific functions to set and get the keys and values of your thread.
Does that mean that neither the Cocoa nor POSIX TLS functions can be expected to work in library code when we don't know whether the code that calls us is already using one or the other?
How does one get to store and retrieve a thread-local pointer robustly in these circumstances?
Is there a native Darwin TLS support API we should be using instead of either Cocoa or POSIX?
I believe the point that Apple's docs are making is that you cannot use pthread_setspecific to set a value and then expect it to be available in threadDictionary. I wouldn't expect them to directly interfere with each other; they're just separate.
That said, if this is iOS-specific code, then the strongly preferred way to manage this is with GCD rather than POSIX threads. GCD offers the equivalent of TLS in the form of dispatch_get_specific, dispatch_queue_get_specific and dispatch_queue_set_specific. But it also provides much better thread management than POSIX threads.
If you don't mind using C++, boost has thread_specific_ptr. It supports iOS. If you don't want to use C++, the implementation probably offers some hints on how to make it work without a lot of external dependencies.
Related
I’m curious what DispatchQueue really is under the hood, I tried to google this information but all the documentation is rather abstract and doesn’t provide any real information about the implementation. In my understanding DispatchQueue is some kind of an entity that exists somewhere and is able to store blocks of code and is controlled directly by the kernel(by GCD which is baked into the kernel) which is able to inject those blocks in chosen(by GCD/Kernel) thread. This this the correct vision of DispatchQueue, or I misunderstood something?
You've misunderstood, at least in some parts. GCD is not "baked into the kernel", it's a library that runs on top of POSIX threads, which are OS-level primitives with kernel support. GCD is simply a set of APIs that make it easier for developers to do work on multiple threads without having to manage the threads themselves.
For what it's worth, you can see the source code for GCD. It's here: https://opensource.apple.com/tarballs/libdispatch/ That said, it's full of micro-optimizations that take advantage of obscure compiler features (branch prediction directives and things like that) and it can often be hard to read and understand, even for experienced systems programmers.
A full-detail explanation of GCD's inner workings is beyond the scope of a StackOverflow answer, but I'll try to cook up a one or two paragraph explanation.
GCD manages some number of POSIX threads behind the scenes that it will use to execute work in the desired way. It also maintains a number of data structures to organize that work, like "queues" which can be thought of as "lists of blocks of work to be done." There are also groups, which allow you to be notified when a list of work items is completed. There are also various IO mechanisms to allow asynchronous IO to be serviced with these work items. It may (or may not) use various kernel services (like threads, kqueues, etc) to manage parts of its workload, but those aren't specific to GCD.
At the end of the day though, there's little or nothing "special" or "blessed" about GCD. In fact, there are multiple ports of GCD to various other operating systems out there, like this one for Linux: http://nickhutchinson.github.io/libdispatch/ which should drive home the point that it's not something specific to the Darwin kernel. Put differently, you could write your own version of GCD from scratch without needing to recompile the kernel.
I am trying to search to prevent the Method Swizzle in my current library but more or less every documentation or blog post that i found are about how to implement Swizzling.
There are couple of question that i have regarding Method swizzling that i could not find all across.
How to detect the Method Swizzling at runtime?
In case there is a Method Swizzle how to prevent it ?
I know the dangers of method swizzling and have already gone through the related posts here but could not find the related information to prevent it.
If there is any documentation or blog post that available on the above topics, I would really appreciate the assistance.
Avoiding method swizzling is simple; use functions rather than methods (i.e. do things the old-fashioned way without objects, in C++, or at least in Core Foundation). But if you're using the ObjC runtime, you can be swizzled.
You can in principle detect swizzling by caching all your IMP pointers, for example using class_getMethodImplementation in something like +load or maybe a C++ constructor on a global variable (which get run before main()), and then re-checking all your IMP pointers at various times to make sure they haven't changed.
That probably wouldn't be too hard, but it's difficult to imagine what all of this would achieve. If someone has your framework binary, it wouldn't be a major effort to to patch it to remove your check. Somewhere in the source code, there's got to be a if (swizzled) { ... }, and that's going to translate into a branch-if conditional instruction in the assembly. You stick a debugger on the system, wait for the branch to "ah! we're swizzled" to occur, note the point where it happens, and patch that byte to be "branch-if-not" or just add an unconditional jump.
Slowing that attack down, even a little, requires substantial obfuscation. There are techniques (that mostly don't work that well), but they only work by being kept secret. That's the difference between obfuscation and security.
Short answer is you really can't achieve this in any stable way. That means you either need a team devoted to constantly coming up with new, more advanced obfuscations and updating them regularly as new attacks emerge (i.e. how a company like Blizzard or Apple prevent hacking), or you'll need to find a way not to need this.
Simplest answer? Work mostly in C++ and use ObjC classes as little as possible (which will prevent swizzling, but not reverse engineering or patching). Or accept that swizzling is not avoidable.
(BTW, if there were even a "I'm willing to do whatever it takes" answer to this question, then Apple would just use that technique to make jailbreaking impossible. The fact that iPhones get jailbroken regularly suggests the difficulty of the problem.)
With swift, isn't it possible to dump functions and variables into a file instead of implementing a singleton? Is there a good reason (like memory management or something) that I am missing, other than style concerns?
It is very much possible to do so. Module level private variables could be used as private variables for singleton class.
For me it is rather a choice dictated by what makes more sense as a solution to the problem. The language has given us tools each with its particular usage, so use them the way they are meant to be used.
If your solution requires an object with data, state and related functionality which could have only one instance then it is better to use a singleton. This will make more sense when you or someone else will try to understand code at later time.
If your solution is just bunch of loosely related functions without data or state then it is better to use functions in a file.
With swift, isn't it possible to dump functions and variables into a file instead of implementing a singleton?
Yes, it is definitely possible. However, the same is possible in Objective-C, with the same pluses and minuses.
Is there a good reason (like memory management or something) that I am missing, other than style concerns?
Two considerations are relevant here:
You can make your singleton polymorphic, expose an interface, define several implementations, and pick the specific one at runtime
You can "reset" your singleton by discarding the instance and creating a new one.
If you need concurrency, having a singleton makes it a little easier to implement.
I've defined some message types that I'd like to use in my protorpc service on app engine, but the service in throwing an error because it is finding members that are methods, instead of messages.Field instances. Is there a way to get around this? Because defining methods on messages is really useful. In my example, I have a credentials message, and I've defined a method to check if the credentials are correct, on the message. I know I can just define the methods as functions, but it just seams less logical.
One of the goals of ProtoRPC is to remain as faithful to the implementation of Protocol buffers as possible. Writing a protocol buffer using .proto notation does not allow for method definitions. So if one could define methods you would end up with classes that could not correctly be represented as a .proto file. As an aside, the hope is that it makes the developer enforce a clear distinction between their message classes and client/server functionality.
We also went through a lot of effort to make sure that if we needed to make future changes (such as adding support for the protocol buffer mechanism) it easier to do so without breaking compatibility with older versions. Because it was intended for being built in to the App Engine runtime the library is updated automatically when a new version of App Engine is released, not when the developer is ready to upgrade. Restricting what can be added to messages now allows greater flexibility for the library to grow in the future (in truth, at the expense of the user of the library).
In what circumstances should I be using this feature?
How exactly mature is it?
What are the pros and cons?
What problem does it solve?
Is it specific to MonoTouch, Mono GC, or reference counting in ObjC?
Here are some quick, high-level (and out or order) answers to your questions...
Is it specific to MonoTouch, Mono GC, or reference counting in ObjC?
It allows sgen, the mono garbage collector, to work more closely with Objective-C reference counting. That awareness is not needed for Mono (or Mono for Android) so it's specific to MonoTouch.
How exactly mature is it?
As the UI says: Experimental preview. So while we know it works it has not yet seen a wide usage inside applications (compared to the default GC). It's more a direction than a destination (i.e. it will likely evolve).
You're more than welcome to test it, use it (if it proves useful in your situation) and report your findings/experiences with it. However it's not fully supported, e.g. you might hit a bug that we can't immediately fix or workaround (beside asking you to go back to the default settings).
What problem does it solve?
The coexistence of a garbage collector and the reference counting of Objective-C is very complex subject. MonoTouch tries it best to hide (most of) complexity of this to the developers.
Most of this is done inside the runtime (e.g. by using the backing fields). This extension to sgen is meant to have the GC itself (not only the runtime) aware of the needs for reference counting.
What are the pros and cons?
PRO: It saves memory as the linker can remove many of the backing fields that would be otherwise required to ensure we keep a reference to the managed objects. Without those (references to) backing fields the GC would normally collect the instances (while they are still needed by unmanaged code).
CON: We need more feedback, more comparison data (e.g. performance).