Wednesday, January 25, 2012

Proactive Legislation

The recent SOPA/PIPA debacle has me marvelling, not at the hamfisted nature of the MPAA, but at the vast, disparate collection of internet companies, startups, luminaries in the fields of CS, and privacy, and forward-thinking individuals and the effort that was brought to bear on our various legislators. For the most part, it was a loose organization of people and enterprises acting on their own, and the concerted nature of the blackout seemed to be a avalanche where small important actors triggered the event rather than ran it. And the amount of effort and energy that went into it was pretty staggering.

Just think if that amount of energy was put into writing legislation rather than merely blocking legislation?

Providing our congress-critters alternative legislation, completely drafted, seems like it could be far better than MPAA-, RIAA-, Big Oil, Big Whatever providing a significant amount of the guidance as to what the laws should be enacted. We should have our own groups put together ideas, crafting and analysing and reshaping laws to be ultimately given to legislators to introduce in their fora, and then be there to guide them as they try to reshape them to discuss our choices (especially when we chose otherwise).

The problem is that Big Whatever works. Fragmented groups of self-interested individuals and corporations never seem to form a cohesive movement, and only act in concert when threatened and then only from each of their own fears. There is no such thing as Big People or Big Citizenry. If there were, we'd ship the vast majority of Congress home and replace it with people willing to do our bidding, and change the laws to better ensure that they did so. As it stands, Big Citizenry politics is a study in waveform superposition -- it's only when there's enough people who happen to be aligned, when there's a "rogue wave" of citizen support for change, that we stand any chance of making progress. And Congressional rules increasingly impede progress, so that any such wave is likely to dissipate before any action is completed.1 So we have to be strategic when such a wave occurs, and dismantle the structure Big Everyone-Else has created.

Projects like RootStrikers are on the right track, but the larger "we" need a game plan. First, we have to strike at the things that impede political change:
  1. Cut the flow of money from lobbyists to congress-critters in all arenas: gifts, election donations, PACs, superPACs.
  2. Replace or remove old and anti-democratic Congressional rules: (secret) holds, anything that means we have to have a supermajority to pass anything, anything that requires that every piece of legislation take some undue amount of time and prevent discussion/handling of alternative legislation.2
  3. Improve the ability to hold congress-critters accountable to their campaign statements and their vows in office. This requires a bit more noodling on my part (and I'll take suggestions), but I'm thinking having some kind of contract-like device that allows something short of impeachment happen when they don't follow up at all or, worse, go directly against what they claimed they would do in office. (Can you say, "Transparency?" I knew you could.)
Second, we need to strike at the things that greatly facilitate Big Brother3:
  1. Strengthen privacy transparency requirements. Companies need to be extremely transparent about what of your data is consumed (even if not the special sauce of how).
  2. Strengthen minimum privacy requirements. Companies need to limit what they record about people that is ephemeral to their actual service, and not share it with other companies without explicit approval (that isn't buried in some arcane Terms of Service).
  3. Decentralize. While this isn't a government thing entirely, nor should it be mandated,  having a best-practices document and easy how-tos to ensure you don't have all of your online presence stuck in a company's monolithic service umbrella, and that some things are explicitly separated so as to ensure that collusion of several companies and the government (I'm looking at you, AT&T) are required to perform clandestine domestic spying.
Third, we need to strike at the things that inhibit technical progress:
  1. Overhaul the Copyright office. Restore expiration terms to their original values, making considerations for increased lifespans of the creators. Set up iron-clad fair use examples and put the onus of responsibility on the creator to show at least some infringement before issuing take-downs. Make bad take-downs cost you (e.g., a Copyright version of SLAPP).
  2. Overhaul the Patent offices. Require Software Patent Bonds, monies put in by big corporations when applying for patents, that get paid out to finders when said approved patents are later found to have prior art–don't make the system perversely set up to make it so that creators want to not look at the set of other patents to avoid the triple penalty for possible infringement (if someone finds out). Clean the house of bad patents and figure out how better to interoperate with international patents.
  3. Fix the state of anti-logic/anti-science education. It's impossible to get things done when you're not operating with people even remotely attempting to be rational actors.
Then there's a swath of other things I would get at (healthcare, taxation), but I'll save that for another day.

The remaining question is who or what group(s) is/are best set up to provide a cohesive set of legislation to enact these things, and full logistics support to shepherd them through a narcissistic, divisive Congress? And how do we rally the Big Citizenry to support them? How do we start this avalanche?
--
1To some extent this is a good thing, so that rational thought and consideration drive change rather than instinct and reaction, to avoid the perverse and destructive tendencies of the latter two (e.g., post-911 security theater), not to mention the cost of vacillation when the needle swings back the other way. I just think the knob has been adjusted too far.
2Even the operating system vendors have finally gotten on the bandwagon of not running blocking operations on the main/UI thread. Legislation should have deadlines for congresspeople to consume, understand, and hear back from their constituents, but it shouldn't require spinwaiting; the filibuster should not stall other legislation.
3Government or even corporate strongarming via data collection, aggregation and mining will always be possible. Making every internet service required to have some backdoor putatively for "organized crime" or more recently "the terrorists" just allows entities like the FBI/NSA/CIA/police to selectively target individuals or communities it takes issue with for harassment in the form of selective prosecution.

Wednesday, December 14, 2011

Thursday, March 03, 2011

[Non]recourse loans

NPR’s Planet Money had a podcast describing differences between nonrecourse loans, which Americans traditionally have, and recourse loans, which everyone else traditionally has. The pros for nonrecourse loan systems is that borrowers are more willing to take risks, and thus capitalize on them, increasing the general wealth, at the cost of finding it slightly harder to find banks willing to make loans and general extra volatility of the system with regards to speculative prices. The pros for recourse loan systems is that banks are more willing to make loans, so finding them might be easier, and the consequences of a default give incentive to the potential borrowers to be more conservative, reducing volatility.

What is left out of the discussion is that these systems are not in isolation. The fact that the vastly increased volatility of the nonrecourse mortgage market in the United States has been the single largest factor in the most recent financial crisis weighs in on the pros and cons, since the negative effects of that crisis has sent ripple effects across the Atlantic.

The problem, as I see it, is that we have an iterated prisoners’ dilemma: Whereas the recourse-loan-based societies might enjoy relative stability of their markets if they were in isolation, the fact that we’re all in the same boat means that if other societies “defect” to become nonrecourse-loan-based, then the volatility spills over, the value the recourse-loan-based societies are expecting from stability is decreased. In effect, it means that there’s a new pressure to cause further “defections”—the reward for increased capital risk now costs less, since they’re already incurring some volatility.

Thursday, August 19, 2010

Unexpected Mach port refcounts

I recently found two cases where it was not obvious to me that I would have had ownership of a reference of a Mach port, and resulted in a Mach port “leak”:
  1. mach_thread_self() implies adding a MACH_PORT_RIGHT_SEND on the thread port. It must be followed with a matching mach_port_deallocate call when the reference is no longer needed.
  2. Mach messages received from the kernel after registering with thread_swap_exception_ports may contain Mach ports, depending on the flavor of message. Any such ports must be deallocated by the receiver. If you pass the message along, then you can get away with just transferring ownership along as well, presuming that the next mach port in the chain wants a sufficiently similar flavor of exception message as you do.
In our case, the extra un-deallocated references to the thread port would eventually turn into dead names after the thread died, but would never go away. As a result, if you went through enough threads over enough time, you’d starve your application for Mach ports and it would go unresponsive, crash, or otherwise terminate. In addition, we would incur (via the exception messages) ever increasing refcount on the task port, with somewhat unclear implications.1

Although I was able to use the dtrace example earlier to do some early diagnostics, since mach_port_allocate was not the only generator of Mach ports in the process (e.g., the kernel when sending us exception messages), it was not sufficient. Instead, I ended up using Apple’s own sample, MachPortDump, in combination with some strategically placed breakpoints in gdb to try and narrow down when the ports were coming into existence, and what the ports actually were.2

It really is too bad that Apple deems these APIs to be unworthy of documenting. Admittedly, only a few developers really need to get down into this nitty-gritty, but if the only choices are reading the 17-year-old Programming Under Mach whose API references are out-of-date and whose code samples don’t deallocate these ports either, reading the darwin source (not an option for me), pestering ADC, or stumbling around in the dark, I can wager what will happen, and what the quality of the resulting products will be on the OS. Perhaps the consumers will lay the blame at the application developer’s feet, or at Apple’s, or both. In any case, it makes it hard to do things the right way, which I’m pretty sure we all want. (Or at least want slightly more than doing it the wrong way — not doing it at all isn’t an option.)
----
1What does happen when you wrap the refcount number on a Mach port? Hm…
2MachPortDump just lists what ports are in the process and what kind/rights it has, not what the port is related to. You have to be a little creative to figure out what the port being incremented belongs to, e.g., (mach_port_t)pthread_mach_thread_np((pthread_t)pthread_self()) to figure out what the current thread’s port is. AFAICT, there’s no Mac OS X analog to netmsgserver to annotate ports.

Wednesday, August 11, 2010

Recording the thread ID in Mac OS X Instruments

Hidden in the depths of the man page for dtrace(1), there is a reference to a new built-in variable, tid:
tid A uint64_t thread ID of the currently executing thread…
What this means is that it’s trivial to log the thread ID associated with a call, which is incredibly useful for multi-threaded applications. (Can you say, “GCD?” I knew you could.) In a custom probe, you can record it as follows:

Monday, August 09, 2010

Thread-based thread-level Mach exception handler and debugging

Apple implemented an awesome feature in their version of gdb in Xcode 3.2 (SnowLeopard is minimum OS requirement) for those (I hope few) of us1 who (1) have implemented a thread-based Mach exception handler, (2) actually use it with a mask for EXC_BREAKPOINT to conditionally self-handle breakpoints, and (3) still want to be able to debug the result: thread dont-suspend-while-stepping. The problem that occurs is that when gdb is stepping (either by user specified command or in an attempt to get to a safe-to-read-objc state), it freezes all the other threads in your task. Normally, this is exactly what you want—isolation from the other aspects of your program while you inspect and complete something under the watchful eye of the debugger.2 However, if one of those threads is the thread that is set up to handle a thread-level exception by looping receiving messages off of a Mach port, its Mach port gets sent the message (before gdb’s, which is a task-level exception handler, which gets second shot at such exceptions), and the thread never is woken up to handle it, so gdb deadlocks. Enter thread dont-suspend-while-stepping. You tell it to not bother suspending your exception thread, and now it is awake to handle the exception message, deal with it, and respond back to the kernel. Then, unless it intercepts a temporary breakpoint or a single-step exception that wasn’t set by it, the Mach exception message will then be sent to gdb’s handler, and then gdb will resume control nicely. If you want to do this automagically, you can set yourself a breakpoint on thread entry function for your exception-handling thread, have it prevent its own suspension, and then continue on. The general code to run would be:
thread dont-suspend-while-stepping on -port ((mach_port_t)pthread_mach_thread_np((pthread_t)pthread_self()))
pthread_self() will return the current pthread, and then pthread_mach_thread_np will return the Mach port given a pthread.
----
1 At the moment, I suspect that the people who are actually doing this are
  • us, i.e., Mac CoreCLR
  • Java
  • Flash
It’d be interesting to know if there were yet others, especially if there’s any likelihood you’ll find yourself in a browser process-space. There are some unfortunate interactions that occur when each of these apps either stomp on each others’ exception handling registration, or try to forward messages along the “chain” of handlers. To wit, if you’re calling thread_set_exception_ports, you’re definitely doing it wrong, and if you’re calling thread_swap_exception_ports, it’s only that it’s very likely that you’re doing it wrong. Less so if you’re doing it on entry/exit of your special code. Much more so otherwise.
2 It’s not a panacea, in that if you have complicated timing issues between two threads, you’ll need to be a little more inventive.

Thursday, August 05, 2010

Dipping my toes into Cocoa again

Last time I did any significant Cocoa programming was prototyping a Installer-plug-in-style modular pane system for implementing one’s own Setup Assistant, in our case, a Mac Office Setup Assistant. I think it was a toss-up between the Hypercard-like architecture of the system or the product ID field editor which I liked more. But I left the team to head to the CLR before it was complete, and it’s unclear how much of that work was used, cherry-picked, or thrown out for Mac Office 2008.

Since then, the clients of my code have almost all been C++, and my memories of .nibs and actions and outlets have faded. Last week, though, one of the partners using Cocoa reported an issue. I sat down to try and write a simple Cocoa version of our C++ client (with some actual UI) and knew enough to know I didn’t remember enough to sit down and do it. Perhaps if I had spent more than a few months developing Cocoa UI a few years back, it would have stuck better. I popped open my Cocoa® Programming for Mac® OS X (2nd ed.), and bogged down nigh immediately: quite a bit had transpired in the Interface Builder world since 2004. I don’t remember any @prototype or @synthesize. So, I shelved the project, ordered the 3rd edition (published in 2008), and worked on other projects.

Now that it’s arrived, I’m looking forward to sitting down with it, Xcode, and Interface Builder and making a simple CoreCLR loading application. It will have use beyond this simple reproduction case, I wager.