Ruby Obfuscator update

Eric Hodel | Tue, 21 Mar 2006 08:06:00 GMT

Ryan writes about our accomplishments in our most recent Ruby obfuscator hacking session.

I must add that making blocks work will continue to be a pain the way we’ve implemented obfuscation. The interpreter gets to cheat because it has the AST lying around. We don’t so we’d either have to rebuild it (too fragile to consider) or build a chunk of AST that calls back to C (still fragile). We went with a simple to implement approach that isn’t as forgiving for users but won’t fail when you switch Ruby versions.

Posted in , , ,  | no comments

String interpolation in ruby2c

Eric Hodel | Sat, 04 Mar 2006 07:27:00 GMT

The toolset zenspider and I built into ParseTree for rewriting Ruby’s AST allows us to make features work that otherwise would be very difficult by rewriting them into easier to implement features.

One feature that makes Ruby so clean is string interpolation but currently ruby2c doesn’t support it. In Ruby there are two types of String nodes, a plain :str node ([:str, "mystring"]) for regular strings and a :dstr node that contains all the parts of the dynamic string which the interpreter concatenates ([:dstr, "val: ", [:vcall, :val]]).

Implementing :dstr in a runtime would be too hard to be worthwhile so I used SexpProcessor to rewrite a :dstr into a series of calls to #<< and #to_s that append to the leading :str node.

Now a dynamic string like:

[:dstr,
 "a",
 [:vcall, :x],
 [:str, "b"],
 [:vcall, :y],
 [:str, "c"]]

Gets transformed into this ugly sexp:

s(:call,
  s(:call,
    s(:call,
      s(:call,
        s(:str, "a"),
          :<<,
          s(:arglist,
            s(:call, s(:call, nil, :x, nil), :to_s, nil))),
        :<<,
        s(:arglist,
          s(:call, s(:str, "b"), :to_s, nil))),
      :<<,
      s(:arglist,
        s(:call, s(:call, nil, :y, nil), :to_s, nil))),
    :<<,
    s(:arglist,
      s(:call, s(:str, "c"), :to_s, nil)))

I don’t know about the inside-outness of it though, I may have to fix that.

Posted in , ,