Skip to content
Zwetan Kjukov edited this page Feb 26, 2016 · 2 revisions

binfmt_misc

Binfmt_misc is a "Kernel Support for miscellaneous (your favourite) Binary Formats".

This Kernel feature allows you to invoke almost (for restrictions see below)
every program by simply typing its name in the shell.
This includes for example compiled Java(TM), Python or Emacs programs.

Which is great for us when we compile ActionScript 3.0 to ABC or SWF binary format, because this mean we could directly execute the file as $ ./hello.abc instead of using a command-line utility as $ redshell hello.abc.

Since Ubuntu 14.04 binfmt_misc is mounted by default,
but if it s not you can mount it using the following
$ sudo nano /etc/fstab
and add this line
none /proc/sys/fs/binfmt_misc binfmt_misc defaults 0 0
then mount it with $ sudo mount -a.

To verify that binfmt_misc is available do
cat /proc/sys/fs/binfmt_misc/status
it should return enabled.

Now to make thing easier we will install a small utility
$ sudo apt-get install binfmt-support
see the man page for binfmt-support.

To know the formats enabled do
$ update-binfmts --display

If you had installed Java
(you could as we need java to compile with asc.jar)
$ sudo apt-get install default-jdk

you would see in the different entries

jar (enabled):
     package = openjdk-7
        type = magic
      offset = 0
       magic = PK\x03\x04
        mask = 
 interpreter = /usr/bin/jexec
    detector = 

Our goal is to do the same but with 3 formats

  • .as3 to execute uncompiled AS3 files
  • .abc to execute ABC files
  • .swf to execute SWF files

From the extras, download

  • install-as3run, select-as3run
  • install-abcrun, select-abcrun
  • install-swfrun, select-swfrun

Or if you need to directly copy them to your server via SSH
$ cat ~/tmp/install-as3run | ssh root@server "cat > ~/install-as3run"
$ cat ~/tmp/select-as3run | ssh root@server "cat > ~/select-as3run"

Or you can simply download them too
$ wget https://raw.githubusercontent.com/corsaair/redtamarin/master/extras/linux/install-as3run
$ wget https://raw.githubusercontent.com/corsaair/redtamarin/master/extras/linux/select-as3run

You will need to make them executable
$ chmod +x install-as3run
$ chmod +x select-as3run

Now install the different formats, for example
$ sudo ./install-as3run

the install script basically generate a file into /usr/bin

for example /usr/bin/as3run

#!/bin/sh

AS3NAME=$1
shift
/usr/lib/redtamarin/bin/redshell "${AS3NAME}" -- $@

and then register the .as3 extension
$ update-binfmts --package as3run --install as3run /usr/bin/as3run --extension as3

so now if you do a $ update-binfmts --display
you should see

as3run (enabled):
     package = as3run
        type = extension
      offset = 0
       magic = as3
        mask = 
 interpreter = /usr/bin/as3run
    detector = 

To test it, create a file with the .as3 extension
$ touch hello.as3

edit it and add a simple line, for example
$ nano hello.as3

trace( "hello world" );

now make the file executable
$ chmod +x hello.as3

and run it
$ ./hello.as3

It's magic right :)

Note:
this is different of what does as3shebang
which reuse the shell and the shebang line
to find which program to execute a script with.

With as3shebang, you don't care about the extension
but it will work only with plain text files that
can include the famous shebang line
eg. #!/usr/bin/as3shebang

In short, once you have an extension registered with binfmt_misc
when you run it, the system automatically recognize it
and run it with the what have been registered
here for .as3 it will basically do
$ /usr/bin/as3run hello.as3

And because our little script as3run
it very simple and straightforward, it call
$ /usr/lib/redtamarin/bin/redshell -- hello.as3

And this not only for the executable that you (as a user) run on the command-line, but for the whole system.

That means it will work in cron jobs, in Apache CGI scripts, etc.

ActionScript 3.0 everywhere :)

The other script select-as3run allow to chose which redshell runtime is used.

By default, we associate the extension .as3 with /usr/lib/redtamarin/bin/redshell
but maybe you would prefer to test with the debug or debugger
eg. /usr/lib/redtamarin/bin/redshell_d, /usr/lib/redtamarin/bin/redshell_dd.

To do that, simply run the select script
$ sudo ./select-as3run

You will see a menu of choices

1) release
2) debug
3) debugger
4) status
5) quit
Please enter your choice: 

Just type the number you want

  • 1 will select the release redshell
    ex: "Set as3run to Release"
  • 2 will select the debug redshell
    ex: "Set as3run to Debug"
  • 3 will select the debugger redshell
    ex: "Set as3run to Debugger"
  • 4 will inform you which redshell
    is currently associated with as3run ex: "as3run is Release"
  • 5 will just quit and do nothing

To test it, edit your file hello.as3
$ nano hello.as3

trace( "hello world" );

trace( UNKOWN );

Run it
$ ./hello.as3

you should get

hello world
ReferenceError: Error #1065

Now, select the redshell debugger for as3run
$ ./select-as3run (chose 3)

Run the script again
$ ./hello.as3

now you should get

hello world
ReferenceError: Error #1065: Variable UNKNOWN is not defined.
	at global$init()[:3]

Once you are done with the scripts
you can move them to the redtamarin directory for example
$ sudo mv install-as3run /usr/lib/redtamarin/run/
$ sudo mv select-as3run /usr/lib/redtamarin/run/
(or any other path)

Note:
later we will try to release
a redtamarin-misc.deb package

Clone this wiki locally