The Secret Diary of Han, Aged 0x29

twitter.com/h4n

Life through the backdoor

Ken Thompson once inserted a backdoor in the Unix login program by modifying the C compiler to compile a version of itself that would include the backdoor creating code. He could then delete the backdoor from the C compiler’s source code. Since the creation of the backdoor was now part of the machinery of compilation, the binary executable C compiler, it did not need to be part of the C compiler’s source.

Once upon a time I did a PhD. Tradition in Holland has it that you can add a list of “positions” to your thesis, often unrelated to the PhD work, that you are willing to defend during the Q and A section of the PhD ceremony. I included a position that said something along the lines of “Our DNA does not contain all information necessary for our procreation”. What this alludes to is that there are bound to be “backdoors” in the machinery that creates us from our DNA.

The original source code for Ken Thompson’s C compiler backdoor could be deleted, because it was now part of the machinery. Similarly, it seems likely that the machinery that creates our proteins from our DNA will have evolved in such a way that parts of it no longer need to be coded for by the DNA directly. The information to recreate the machinery is now partly in the machinery itself, and over time the DNA encoding for those parts will have disappeared since the evolutionary pressure to keep that DNA intact is no longer there.

We start out life with DNA from our parents, but we also receive the protein building machinery from our mothers in the fertilized egg. Over time that machinery has evolved to great complexity. Our C compiler has many backdoors that it depends on for its proper functioning. And like Ken Thompson’s backdoor, there is no need to keep its DNA around. Our procreation needs not only our source code, but also that complex backdoor-rigged machinery that turns our source code into us.

Written by Han

April 7, 2009 at 10:15

Posted in Uncategorized

It’s official: The iPhone 3G is toilet resistant

On Friday, on a slight belly flu induced toilet visit, the 2 week old iPhone managed to slip out of my rear pocket and into the bowl. I heard the clunk and knew it wasn’t good. Fortunately I hadn’t dropped anything else.

When I fished it out, the screen had turned on and showed proof of Apple’s amazing capability of anticipating usage scenarios:

Then the screen faded. Water had entered the screen and camera. What to do?

When dropping a mobile phone in water, here is what you should do immediately:

  1. Turn phone off
  2. Remove battery

Unfortunately, for number 1 you need a working touch screen, and for number 2 you need to visit the Apple store. The only option was to let it die a painful death. The screen came on a few times, flashing bright white, as iPhone desperately tried to breathe, but it finally died after about 15 minutes.

Not covered, can’t fix, visit Softbank to buy a replacement. This is what the genius in the Apple store told me an hour later. Instead I decided to go to Tokyu Hands and bought a screw driver and a watch case opener. Back home I disassembled the iPhone, and let it dry overnight.

The next morning I put it back together, which was considerably less straight forward than taking it apart. When I finally clicked the screen back into the case, fastened the two screws at the bottom, and  looked at the screen, there was the Apple logo! Some people get warm fuzzy feelings whenever they see the Apple logo. Not me. But this time must have made up for that. iPhone had resurrected. After installing the SIM card, it booted normally, and everything functions OK.

Well, except for the volume up button.

Written by Han

July 28, 2008 at 13:38

Posted in Uncategorized

Tagged with

Including remote javascript in a bookmarklet

The size of a URL is limited to 488 characters in IE 6 SP2. This limits what can be done in a bookmarklet. Fortunately it’s possible to include external javascript. Unfortunately, IE 6 again makes things more difficult that they should be.

To load a remote a remote javascript file, just construct a script tag and insert it into the document body. To call a function, in order to feed it some data stored in the bookmarklet, set the “onload” attribute to a callback to be invoked when loading is ready:

(function() {
  var d=document;
  var s=d.createElement("script");
  s.onload = function() {enter()}; // enter is a function defined in the loaded script
  s.src="/path/to/java/script.js";
  d.body.appendChild(s);
})();

This works nicely in Firefox, Safari and Opera.

The problem is, loading the script is synchronous in IE (both 6 and 7), and the onload callback does not get called. This in spite of MSDN documentation claiming that onload on script tags is honored. However, since loading is synchronous, the script will have completed loading in IE when the appendChild call returns. A workaround, then, is to call the callback explicitly after the appendChild call, but guard it with a flag so that it is run only once.

(function() {
  var d=document;
  var s=d.createElement("script");
  s.onload = function() {enter()}; // enter is a function defined in the loaded script
  s.src="/path/to/java/script.js";
  d.body.appendChild(s);
  if (window.enter)
    enter();
})();

// "enter" function in included javascript file:
var entered = false;
function enter() {
  if (entered)
    return;

  entered = true;
  //...
}     

Written by Han

February 2, 2008 at 18:51

Posted in Uncategorized

Tagged with

Line No. in Rhino

Rhino is a great tool for developing javascript code, since it comes with a shell that let’s you run scripts from the command line and provides a REPL loop for quick try-outs. The command line is kind of long and unfriendly, but this can be taken care of with an alias or shell script:

java -classpath js.jar
    org.mozilla.javascript.tools.shell.Main  -f test.js

However, if you run a script that bombs out with an exception, you don’t get any line information, just the plain exception message:

js: uncaught JavaScript runtime exception:
    TypeError: Cannot call method "charCodeAt" of undefined

To get line information, you need to ensure that rhino runs in interpretive mode, using the -opt -1 option:

java -classpath js.jar
    org.mozilla.javascript.tools.shell.Main -opt -1 -f test.js

This results in:

js: "test.js", line 577: uncaught JavaScript runtime exception:
    TypeError: Cannot call method "charCodeAt" of undefined

which has the offending line number, and thus is much more helpful. In addition, it it possible to get a stacktrace by invoking your code in a try catch block, and handling the exception as follows:

try {
     // call your code here ...
}
catch (exc) {
    print(exc);
    if (exc.rhinoException)
        exc.rhinoException.printStackTrace();
}

Don’t forget to specify the -opt -1 flag, since otherwise you’ll only see java-level calls. Also, note that the “rhinoException” property is defined on Error objects only, so the object caught should be explicitly tested for it.

Written by Han

January 10, 2008 at 13:50

Posted in Uncategorized

Building readline 5.2 on OS X Leopard

Readline 5.2 does not build properly on OS X Leopard. It fails with a  -compatibility_version only allowed with -dynamiclib error. I ran into this problem when trying to build ruby, using GNU readline instead of the default editline. The problem is easily fixed though. Readline explicitly checks for the darwin version, but does not include 9 (Leopard) in this check. Patch support/shobj-conf using the following:

 --- support/shobj-conf	2007-12-26 18:30:46.000000000 +0900
+++ support/shobj-conf.new	2007-12-26 18:30:39.000000000 +0900
@@ -142,7 +142,7 @@
;;


# Darwin/MacOS X
-darwin8*)
+darwin89*)
SHOBJ_STATUS=supported
SHLIB_STATUS=supported


@@ -171,7 +171,7 @@
SHLIB_LIBSUFF='dylib'


case "${host_os}" in
-	darwin[78]*)	SHOBJ_LDFLAGS=''
+	darwin[789]*)	SHOBJ_LDFLAGS=''
SHLIB_XLDFLAGS='-dynamiclib -arch_only `/usr/bin/arch` -install_name $(libdir)/$@ -current_version $(SHLIB_MAJOR)$(SHLIB_MINOR) -compatibility_version $(SHLIB_MAJOR) -v'
;;
*)		SHOBJ_LDFLAGS='-dynamic'

and rerun configure and make.

Written by Han

December 26, 2007 at 18:29

Posted in Uncategorized

Tagged with

Ruby 1.9.0 released

Ruby 1.9.0 has been released a few minutes ago.

From the Changelog:

Tue Dec 25 23:33:55 2007 Yukihiro Matsumoto

* development version 1.9.0 released.

As promised at Ruby Kaigi last summer, it was released on Christmas day (with almost half an hour to spare).

Get it from subversion at http://svn.ruby-lang.org/repos/ruby/trunk

Congratulations to Matz, Ko1, and all others who worked hard to make this happen!

Written by Han

December 25, 2007 at 23:38

Posted in Uncategorized

Y combinator in Ruby 1.9

Just for fun, Tom Moertel’s Y combinator in Ruby 1.9’s new lambda syntax:

def y
  -> (x) { x.(x) }.(
    -> (f) { -> (*args) { yield (f.(f)).(*args) } }
  )
end

fac = y { |rec| -> (n) { n < 2 ? 1 : n * rec.(n-1) } }

puts fac.(5)   # ==> 120

Written by Han

December 20, 2007 at 20:56

Posted in Uncategorized