<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>Bluish Coder: waspvm</title>
 <link href="http://bluishcoder.co.nz/tag/waspvm/atom.xml" rel="self"/>
 <link href="http://bluishcoder.co.nz/"/>
 <updated>2020-07-10T16:25:05+12:00</updated>
 <id>http://bluishcoder.co.nz/</id>
 <author>
   <name>Bluishcoder</name>
   <email>admin@bluishcoder.co.nz</email>
 </author>

 
 <entry>
   <title>Shen Language Port for Wasp Lisp</title>
   <link href="http://bluishcoder.co.nz/2017/04/24/shen-language-port-for-wasp-lisp.html"/>
   <updated>2017-04-24T23:00:00+12:00</updated>
   <id>http://bluishcoder.co.nz/2017/04/24/shen-language-port-for-wasp-lisp</id>
   <content type="html">&lt;p&gt;This post intersects two of my favourite lispy languages. &lt;a href=&quot;http://shenlanguage.org/&quot;&gt;Shen&lt;/a&gt; is a functional programming language with a number of interesting features. These include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Optional static type checking&lt;/li&gt;
&lt;li&gt;Pattern matching&lt;/li&gt;
&lt;li&gt;Integrated Prolog system&lt;/li&gt;
&lt;li&gt;Parsing libraries&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;I&#39;ve written about &lt;a href=&quot;http://bluishcoder.co.nz/2016/08/30/kicking-the-tires-of-shen-prolog.html&quot;&gt;Shen Prolog&lt;/a&gt; before which gives a bit of a feel for the language.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://waspvm.blogspot.co.nz/&quot;&gt;Wasp Lisp&lt;/a&gt; is a small Scheme-like lisp with lightweight concurrency and the ability to send bytecode across the network. It&#39;s used in the &lt;a href=&quot;http://bluishcoder.co.nz/2009/11/28/using-wasp-lisp-secure-remote-injection.html&quot;&gt;MOSREF&lt;/a&gt; secure remote injection framework. I&#39;ve &lt;a href=&quot;http://bluishcoder.co.nz/tags/waspvm/index.html&quot;&gt;written a number of posts&lt;/a&gt; about it.&lt;/p&gt;

&lt;p&gt;A feature of Shen is that it is designed to run on top of a lighter weight lisp called &lt;a href=&quot;http://www.shenlanguage.org/learn-shen/shendoc.htm#Kl&quot;&gt;KLambda&lt;/a&gt;. KLambda has only about 46 primitives, many of which already exist in lisp systems, making it possible to write compilers to other languages without too much work. There exist a few &lt;a href=&quot;http://shenlanguage.org/download_form.html&quot;&gt;Shen ports&lt;/a&gt; already. I wanted to port Shen to Wasp Lisp so I can experiment with using the pattern matching, Prolog and types in some of the distributed Wasp code I use.&lt;/p&gt;

&lt;p&gt;Wasp Lisp is not actively developed but the author &lt;a href=&quot;https://waspvm.blogspot.com/&quot;&gt;Scott Dunlop&lt;/a&gt; monitors the &lt;a href=&quot;https://github.com/swdunlop/WaspVM/&quot;&gt;github repository&lt;/a&gt; and processes pull requests. Shen requires features that Wasp Lisp doesn&#39;t currently support, like real numbers. I maintain a &lt;a href=&quot;https://github.com/doublec/WaspVM/tree/shen&quot;&gt;fork on github&lt;/a&gt; that implements the features that Shen needs and any features that apply back to core Wasp Lisp I&#39;ll upstream.&lt;/p&gt;

&lt;p&gt;This port is heavily based on the &lt;a href=&quot;https://github.com/tizoc/shen-scheme&quot;&gt;Shen Scheme&lt;/a&gt; implementation. Much of the code is ported from Scheme to Wasp Lisp and the structure is kept the same. The license for code I wrote is the same as the Shen Scheme License, BSD3-Clause.&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;https://github.com/Shen-Language/shen-sources&quot;&gt;Shen Source&lt;/a&gt; is written in the Shen language. Using an existing Shen implementation this source is compiled to Klambda:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ shen-chibi
(0-) (load &quot;make.shen&quot;)
(1-) (make)
compiling ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To port to another language then becomes writing a KLambda interpreter or compiler. In this case it&#39;s a compiler from KLambda to Wasp Lisp. Implementing the primitives is also required but there aren&#39;t many of them. Some of the characters that KLambda uses in symbols aren&#39;t compatible with the Wasp reader so I used an S-expression parser to read the KLambda code and then walked the tree converting expressions as it went. This is written in Wasp code, converted from the original Scheme. In hindsight it probably would have been easier to write this part in Shen and bootstrap it in another Shen instance to make use of Shen&#39;s parsing and pattern matching libraries.&lt;/p&gt;

&lt;p&gt;Shen makes heavy use of tail calls in code meaning some form of tail call optimisation is needed to be efficient. In &lt;a href=&quot;http://bluishcoder.co.nz/2016/07/14/concurrency-in-wasp-lisp.html&quot;&gt;a previous post&lt;/a&gt; I mentioned some places where Wasp doesn&#39;t identify tail calls. These are cases Shen hit a lot, causing performance issues. I made some &lt;a href=&quot;https://github.com/doublec/WaspVM/blob/shen/mod/lib/optimize.ms&quot;&gt;changes to the optimizer&lt;/a&gt; to identify these cases and it improved the Shen on Wasp runtime performance quite a bit.&lt;/p&gt;

&lt;h2&gt;Current Port State&lt;/h2&gt;

&lt;p&gt;This is a very early version. I&#39;ve only just got it working. The &lt;a href=&quot;https://github.com/Shen-Language/shen-sources/tree/master/tests&quot;&gt;Shen tests&lt;/a&gt; pass with the exception of the &lt;a href=&quot;https://github.com/Shen-Language/shen-sources/blob/master/tests/proof%20assistant.shen&quot;&gt;Proof Assistant test&lt;/a&gt; which hangs when loading.&lt;/p&gt;

&lt;p&gt;Note 2017-04-26: The bug with the proof assistant test not passing is now fixed. It was caused by an integer overflow when computing complexities within the Shen prolog code. Wasp integers are smaller than other Shen implementations which is why none of them hit the issue. The binaries have been updated with this fix.&lt;/p&gt;

&lt;p&gt;The port is slower than I&#39;d like - about half the speed of the Shen C interpreter and significantly slower than Shen Scheme and Shen on SBCL. I&#39;ve done some work on optimizing tail calls in the fork of the Wasp VM for Shen but there&#39;s much more work on the entire port that could improve things.&lt;/p&gt;

&lt;h2&gt;Binaries&lt;/h2&gt;

&lt;p&gt;The following compiled binaries are available:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://bluishcoder.co.nz/shen/shen_static.bz2&quot;&gt;shen_static.bz2&lt;/a&gt;. This is a static 64-bit linux binary with no dependancies. It should run on any 64-bit Linux system.  Decompress with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ bunzip2 shen_static.bz2
$ chmod +x shen_static
$ ./shen_static
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href=&quot;https://bluishcoder.co.nz/shen/shen_macos.bz2&quot;&gt;shen_macos.bz2&lt;/a&gt;. 64-bit binary for Mac OS. Decompress with &lt;code&gt;bunzip2&lt;/code&gt; as above.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://bluishcoder.co.nz/shen/shen.zip&quot;&gt;shen.zip&lt;/a&gt;. The zip file contains a Windows 64-bit binary, &lt;code&gt;shen.exe&lt;/code&gt;. It should run on any modern 64-bit Windows system.&lt;/p&gt;

&lt;h2&gt;Building&lt;/h2&gt;

&lt;p&gt;First step, build the fork of Wasp Lisp needed to run:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ git clone --branch shen https://github.com/doublec/WaspVM wasp-shen
$ cd wasp-shen
$ make install
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Follow the prompts for the location to install the wasp lisp binaries and add that &lt;code&gt;bin&lt;/code&gt; directory of that location to your path:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ export PATH=$PATH:/path/to/install/bin
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Shen is provided in source code format from the &lt;a href=&quot;https://github.com/Shen-Language/shen-sources&quot;&gt;Shen Sources&lt;/a&gt; github repository. The code is written in Shen. It needs a working Shen system to compile that code to &lt;a href=&quot;http://www.shenlanguage.org/learn-shen/shendoc.htm#The%20Primitive%20Functions%20of%20K%20Lambda&quot;&gt;KLambda&lt;/a&gt;, a small Lisp subset that Shen uses as a virtual machine.&lt;/p&gt;

&lt;p&gt;This KLamda code can be found in the &lt;code&gt;kl&lt;/code&gt; directory in the &lt;a href=&quot;https://github.com/doublec/shen-wasp&quot;&gt;shen-wasp&lt;/a&gt; repository. These KLambda files are compiled to Wasp Lisp and stored as compiled code in the &lt;code&gt;compiled&lt;/code&gt; directory. The shen wasp repository includes a recent version of these files. To generate, or re-generate, run the following commands:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ git clone https://github.com/doublec/shen-wasp
$ cd shen-wasp
$ rlwrap wasp
&amp;gt;&amp;gt; (import &quot;driver&quot;)
&amp;gt;&amp;gt; (compile-all)
Compiling toplevel.kl
Compiling core.kl
Compiling sys.kl
Compiling sequent.kl
Compiling yacc.kl
Compiling reader.kl
Compiling prolog.kl
Compiling track.kl
Compiling load.kl
Compiling writer.kl
Compiling macros.kl
Compiling declarations.kl
Compiling types.kl
Compiling t-star.kl
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will create files with the Wasp Lisp code in the &lt;code&gt;compiled/*.ms&lt;/code&gt; files, and the compiled bytecode in &lt;code&gt;compiled/*.mo&lt;/code&gt; files.&lt;/p&gt;

&lt;p&gt;Creating a Shen executable can be done with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ waspc -exe shen shen.ms
$ chmod +x shen
$ rlwrap ./shen
Shen, copyright (C) 2010-2015 Mark Tarver
www.shenlanguage.org, Shen 20.0
running under Wasp Lisp, implementation: WaspVM
port 0.3 ported by Chris Double


(0-) 
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that it takes a while to startup as it runs through the Shen and KLambda initialization.&lt;/p&gt;

&lt;h2&gt;Running from the Wasp REPL&lt;/h2&gt;

&lt;p&gt;Shen can be run and debugged from the Wasp REPL. To load the compiled code and run Shen:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ rlwrap wasp
&amp;gt;&amp;gt; (import &quot;driver&quot;)
&amp;gt;&amp;gt; (load-all)
&amp;gt;&amp;gt; (kl:shen.shen)
Shen, copyright (C) 2010-2015 Mark Tarver
www.shenlanguage.org, Shen 20.0
running under Wasp Lisp, implementation: WaspVM
port 0.3 ported by Chris Double


(0-)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When developing on the compiler it&#39;s useful to use &lt;code&gt;eval-all&lt;/code&gt; instead of &lt;code&gt;load-all&lt;/code&gt;. This will load the KLambda files, compile them to Scheme and &lt;code&gt;eval&lt;/code&gt; them:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; (eval-all)
&amp;gt;&amp;gt; (kl:shen.shen)
...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A single input line of Shen can be entered and run, returning to the Wasp REPL with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; (kl:shen.read-evaluate-print) 
(+ 1 2)
3:: 3
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;KLambda functions can be called from Wasp by prefixing them with &lt;code&gt;kl:&lt;/code&gt;. For example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; (kl:shen.read-evaluate-print)
(define factorial
  1 -&amp;gt; 1
  X -&amp;gt; (* X (factorial (- X 1))))
factorial:: factorial
&amp;gt;&amp;gt; (kl:factorial 10)
:: 3628800
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Shen allows introspecting compiled Shen functions and examining the KLambda code. From the Wasp REPL this is useful for viewing the KLambda and comparing with the generated Wasp Lisp:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; (kl:ps &#39;factorial)
:: (defun factorial (V1172) (cond (...) (...)))
&amp;gt;&amp;gt; (pretty (kl:ps &#39;factorial))
(defun factorial (V1172 ) (cond ((= 1 V1172 ) 1 ) (#t (* V1172 (factorial (- V1172 1 ) ) ) ) ) ) :: null
&amp;gt;&amp;gt; (pretty (kl-&amp;gt;wasp (kl:ps &#39;factorial)))
(begin (register-function-arity (quote factorial ) 1 )
       (define (kl:factorial V1172)
         (cond
           ((kl:= 1 V1172) 1)
           (#t (* V1172 (kl:factorial (- V1172 1))))))
       (quote factorial ) ) :: null
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Cross Compilation&lt;/h2&gt;

&lt;p&gt;Wasp binaries are a small Wasp VM stub plus the compiled Lisp code appended to it. This makes building for other platforms easy as long as you have the stub for that platform. Wasp can be built for &lt;a href=&quot;http://bluishcoder.co.nz/2013/05/09/building-wasp-lisp-and-mosref-for-android.html&quot;&gt;Android&lt;/a&gt; and &lt;a href=&quot;http://bluishcoder.co.nz/2016/06/05/building-static-wasp-lisp-binaries.html&quot;&gt;static binaries via musl&lt;/a&gt; are possible.&lt;/p&gt;

&lt;p&gt;I&#39;ve made the following stubs available for building binaries for other systems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://bluishcoder.co.nz/shen/waspvm-static-linux-x86_64.bz2&quot;&gt;Musl 64-bit Linux static stub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://bluishcoder.co.nz/shen/waspvm-linux-x86_64.bz2&quot;&gt;64-bit Linux stub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://bluishcoder.co.nz/shen/waspvm-win-x86_64.exe.bz2&quot;&gt;64-bit Windows stub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://bluishcoder.co.nz/shen/waspvm-Darwin-x86_64.bz2&quot;&gt;64-bit Mac OS stub&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Decompress them and copy into the &lt;code&gt;lib/waspvm-stubs&lt;/code&gt; directory where Wasp Lisp was installed. Shen can then be built on any host platform for 64 bit linux, 64 bit Linux static binaries, 64 bit Windows or 64 bit Mac OS with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ waspc -exe shen -platform linux-x86_64 shen.ms
$ waspc -exe shen_static -platform static-linux-x86_64 shen.ms
$ waspc -exe shen.exe -platform win-x86_64 shen.ms
$ waspc -exe shen_macos -platform Darwin-x86_64 shen.ms
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Learning Shen&lt;/h2&gt;

&lt;p&gt;Some places to go to learn Shen:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;a href=&quot;http://shenlanguage.org/learn-shen/index.html&quot;&gt;Shen OS Kernel Manual&lt;/a&gt; has a good overview of what the open source version of Shen can do.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/Shen-Language/shen-sources/blob/master/doc/system-functions.md&quot;&gt;Shen System Functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://bluishcoder.co.nz/2016/08/30/kicking-the-tires-of-shen-prolog.html&quot;&gt;Kicking the tires of Shen Prolog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=lMcRBdSdO_U&quot;&gt;Shen, A Sufficiently Advanced Lisp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=BUJNyHAeAc8&quot;&gt;Shen Trick Shots&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.amazon.co.uk/Book-Shen-Third-Mark-Tarver/dp/1784562130&quot;&gt;The Book of Shen&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;Other Ports&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/tizoc/shen-scheme&quot;&gt;Shen Scheme&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://github.com/deech/shen-elisp&quot;&gt;Shen Elisp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/gregspurrier/shen-ruby&quot;&gt;Shen Ruby&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/mthom/shentong&quot;&gt;Shen Haskell&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/otabat/shen-c/&quot;&gt;Shen C&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</content>
 </entry>
 
 <entry>
   <title>Concurrency in Wasp Lisp</title>
   <link href="http://bluishcoder.co.nz/2016/07/14/concurrency-in-wasp-lisp.html"/>
   <updated>2016-07-14T18:00:00+12:00</updated>
   <id>http://bluishcoder.co.nz/2016/07/14/concurrency-in-wasp-lisp</id>
   <content type="html">&lt;p&gt;&lt;a href=&quot;http://bluishcoder.co.nz/2009/11/27/wasp-lisp-small-scheme-like-lisp.html&quot;&gt;Wasp Lisp&lt;/a&gt; has a light weight co-operative threading model that&#39;s allows programming in an &lt;a href=&quot;https://en.wikipedia.org/wiki/Actor_model&quot;&gt;Actor&lt;/a&gt; style. It&#39;s possible to serialize Wasp values and send them to other processes and machines to be deserialized and run. &lt;a href=&quot;https://bluishcoder.co.nz/2009/11/28/using-wasp-lisp-secure-remote-injection.html&quot;&gt;MOSREF&lt;/a&gt; uses this to compile Lisp code on the console process and send the bytecode to drone processes to execute. This allows drones to operate without the Lisp compiler present.&lt;/p&gt;

&lt;h2&gt;Spawning threads&lt;/h2&gt;

&lt;p&gt;Threads are created using the &lt;code&gt;spawn&lt;/code&gt; function. It takes the function to run as a thread as an argument:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(spawn (lambda () (print &quot;Hello World\n&quot;)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Communication between threads is done using queues. A queue is an unbounded channel that can have many senders but only one receiver. The function &lt;code&gt;send&lt;/code&gt; adds data to the queue and &lt;code&gt;wait&lt;/code&gt; receives data. If there is no data in the queue then &lt;code&gt;wait&lt;/code&gt; blocks. Input/Output in Wasp Lisp is done using the same wait/send mechanism making it easy to pipeline data from console and file output to sockets.&lt;/p&gt;

&lt;h2&gt;Implementing Actors&lt;/h2&gt;

&lt;p&gt;A basic Actor can be implemented like the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (actor1)
  (define counter 0)
  (define chan (make-queue))

  (define (loop)
    (define msg (wait chan))
    (cond
      ((eq? msg &#39;inc)
        (set! counter (+ 1 counter)))
      ((eq? msg &#39;dec)
        (set! counter (- 1 counter)))
      ((and (list? msg) (eq? (car msg) &#39;get))
       (send counter (cadr msg))))
    (loop))

  (spawn loop)
  chan)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;actor1&lt;/code&gt; is a function that contains a &lt;code&gt;counter&lt;/code&gt; holding an numeric value. It creates &lt;code&gt;chan&lt;/code&gt;, a queue for holding messages, spawns a thread to run &lt;code&gt;loop&lt;/code&gt; and returns the &lt;code&gt;chan&lt;/code&gt; so messages can be queued for &lt;code&gt;loop&lt;/code&gt; to process.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;loop&lt;/code&gt; waits for a message on &lt;code&gt;chan&lt;/code&gt;. This is a blocking call and the thread will go idle until a message is queued. It processes the message, incrementing or decrementing the counter as requested. An additional message, &lt;code&gt;get&lt;/code&gt;, can be used to get the value of the counter. That message also includes a channel object to place the result in. &lt;code&gt;loop&lt;/code&gt; recursively calls itself to continue.&lt;/p&gt;

&lt;p&gt;A sample interaction is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; (define a1 (actor1))
&amp;gt;&amp;gt; (define result (make-queue))
&amp;gt;&amp;gt; (send (list &#39;get result) a1)
&amp;gt;&amp;gt; (wait result)
:: 0
&amp;gt;&amp;gt; (send &#39;inc a1)
&amp;gt;&amp;gt; (send (list &#39;get result) a1)
&amp;gt;&amp;gt; (wait result)
:: 1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This creates an actor and a queue to receive results. It asks for the current value of the actor, increments it, then asks again.&lt;/p&gt;

&lt;h2&gt;Updating an Actor&lt;/h2&gt;

&lt;p&gt;It&#39;s possible to update the code for an Actor without stopping the application. Running in a &lt;a href=&quot;https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop&quot;&gt;Lisp REPL&lt;/a&gt; means you can change functions on the fly but you can&#39;t change the internal implementation of a running loop from the REPL if that loop is internal to a function. A way around this is to provide the Actor with the means to receive a function as a message that performs the update. Here is an example of an updatable actor:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (actor3)
  (define counter 0)
  (define chan (make-queue))

  (define (loop chan)
    (define msg (wait chan))
    (cond
      ((eq? msg &#39;inc)
        (set! counter (+ 1 counter)))
      ((eq? msg &#39;dec)
        (set! counter (- 1 counter)))
      ((and (list? msg) (eq? (car msg) &#39;get))
       (send counter (cadr msg)))
      ((function? msg)
       (return ((msg counter) chan))))
    (loop chan))

  (spawn loop chan)
  chan)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This code contains an additional branch in the &lt;code&gt;cond&lt;/code&gt; to check if the message is a function. If it is then that function is called passing the current value of the counter. It is expected to return a function which will be the new &lt;code&gt;loop&lt;/code&gt; to call. This can contain any code and effectively updates the entire actor with new functionality. An example update function to change the messages to increment/decrement by two is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (update oldstate)
  (define counter (* oldstate 2))
  (define (loop chan)
    (define msg (wait chan))
    (cond
      ((eq? msg &#39;inc)
        (set! counter (+ 2 counter)))
      ((eq? msg &#39;dec)
        (set! counter (- 2 counter)))
      ((and (list? msg) (eq? (car msg) &#39;get))
       (send counter (cadr msg)))
      ((function? msg)
       (return ((msg counter) chan))))
   (loop chan))
  loop)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;An example interaction of the actor and upgrading it is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; (define a3 (actor3))
&amp;gt;&amp;gt; (define result (make-queue))
&amp;gt;&amp;gt; (send &#39;inc a3)
&amp;gt;&amp;gt; (send (list &#39;get result) a3)
&amp;gt;&amp;gt; (wait result)
:: 1
&amp;gt;&amp;gt; (send update a3)   ;; Updating the actor here
&amp;gt;&amp;gt; (send (list &#39;get result) a3)
&amp;gt;&amp;gt; (wait result)
:: 2                  ;; This shows the new counter value that &#39;update&#39; changed
&amp;gt;&amp;gt; (send &#39;inc a3)
&amp;gt;&amp;gt; (send (list &#39;get result) a3)
&amp;gt;&amp;gt; (wait result)
:: 4                  ;; Amount is now incrementing by two
&amp;gt;&amp;gt; (send &#39;inc a3)
&amp;gt;&amp;gt; (send (list &#39;get result) a3)
&amp;gt;&amp;gt; (wait result)
:: 6
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is a variant of &lt;a href=&quot;https://joearms.github.io/2013/11/21/My-favorite-erlang-program.html&quot;&gt;Joe Armstrong&#39;s Erlang Universal Server&lt;/a&gt; allowing a server to be updated to do anything.&lt;/p&gt;

&lt;h2&gt;Filters&lt;/h2&gt;

&lt;p&gt;An idiom when programming in an Actor or coroutine style is to write small processes that take an input, modify it in some way, and send it to another process to do something else. A program becomes a chain or pipeline of these individual processes. Wasp Lisp calls these small units of functionality filters. They are described in &lt;a href=&quot;https://github.com/swdunlop/WaspVM/blob/master/mod/lib/filter.ms&quot;&gt;filter.ms&lt;/a&gt; as:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;A process that waits for data from an input channel, and sends data to an output channel.  Filters are constructed using a constructor function, then wired together using either the input-chain or output-chain functions.&quot;&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;This is an example of a line filter from the Wasp source code;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define-filter (line-filter)
  (define buf (make-string 80)) 

  (define (parse)
    (forever
      (define next (string-read-line! buf))
      (if next (send next out)
               (return))))

  (define (line-loop)
    (forever
      (define next (wait-input in))
      (cond 
        ((string? next)
         (string-append! buf next)
         (parse))
        ((eq? next &#39;close)
         (return))
        (else
          (send-output next out)))))

  (line-loop)

  (send-output buf out)
  (send-output &#39;close out))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A &lt;code&gt;line-filter&lt;/code&gt; receives strings of bytes on the input channel and outputs a complete line on the output channel when it has one. It does this by appending received bytes onto a string buffer and checking if that buffer contains a line. If it does it removes the line data from the buffer and sends it to the output channel. It then continues to wait for data on the input channel. An example of usage:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; (import &quot;lib/filter&quot;)
&amp;gt;&amp;gt; (import &quot;lib/line-filter&quot;)
&amp;gt;&amp;gt; (define q (make-queue))
&amp;gt;&amp;gt; (define lines (input-chain q (line-filter)))
&amp;gt;&amp;gt; (spawn (lambda () (forever (print (wait lines)))))

&amp;gt;&amp;gt; (send &quot;hello&quot; q)
&amp;gt;&amp;gt; (send &quot;world\n&quot; q)
helloworld
&amp;gt;&amp;gt; (send &quot;foo\nbar&quot; q)
foo
&amp;gt;&amp;gt; (send &quot;baz\n&quot; q)
barbaz
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This creates a queue, &lt;code&gt;q&lt;/code&gt; for input data. It creates a chain containing only one filter, the &lt;code&gt;line-filter&lt;/code&gt;. It returns the output channel which contains the filtered data. Data placed in &lt;code&gt;q&lt;/code&gt; is retrieved by the line filter and when a line is received it is sent to the output channel. A thread is spawned to loop forever printing any lines from the output channel.  Notice in the manual sending of data to the channel &lt;code&gt;q&lt;/code&gt; that output is only printed by the spawned thread when a line is completed.&lt;/p&gt;

&lt;p&gt;Wasp Lisp comes with some default filters for parsing s-expressions, encrypting and decrypting data and fuzzing data amongst other things. Scott Dunlop &lt;a href=&quot;https://waspvm.blogspot.co.nz/2008/08/best-state-machine-is-coroutine.html&quot;&gt;wrote about coroutines and filters&lt;/a&gt; on the Wasp blog.&lt;/p&gt;

&lt;h2&gt;Sending data to other OS processes&lt;/h2&gt;

&lt;p&gt;Some Wasp values can be serialized and deserialized. This provides a way to send values to other wasp instances running in different OS processes or machines. Lisp objects are serialized using &lt;code&gt;freeze&lt;/code&gt; and unserialized using &lt;code&gt;thaw&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The following &lt;code&gt;server&lt;/code&gt; function starts a TCP server on port 10000. Clients connnected to it send Lisp objects to it and it prints it to the standard output on the server process.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(import &quot;lib/tcp-server&quot;)

(define (server)
  (define server-output (current-output))

  (define (acceptor)
    (forever
      (define data (wait))
      (with-output server-output
        (print (format (thaw data)))
        (print &quot;\n&quot;))))

  (spawn-tcp-server 10000 acceptor))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;acceptor&lt;/code&gt; function is called with its current input and output bound to the TCP stream. For this reason we capture the value of &lt;code&gt;current-output&lt;/code&gt; before it is bound so we can output to the server console rather than to the TCP stream. A sample test:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;;; On server 
&amp;gt;&amp;gt; (server)

;; On client
&amp;gt;&amp;gt; (define s (tcp-connect &quot;127.0.0.1&quot; 10000))
&amp;gt;&amp;gt; (send (freeze &quot;foo&quot;) s)

;; On Server
&quot;foo&quot;

;; On Client
&amp;gt;&amp;gt; (send (freeze 66) s)

;; On Server
66

;; On Client
&amp;gt;&amp;gt; (send (freeze &#39;(one (two three))) s)

;; On Server
(one (two three))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Notice that all i/o is done using the &#39;send&#39; and &#39;wait&#39; channel operators. This means we can use a filter to do the freezing/thawing automatically and Wasp has a &lt;code&gt;freeze-filter&lt;/code&gt; and &lt;code&gt;thaw-filter&lt;/code&gt; that does this. The server becomes:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(import &quot;lib/tcp-server&quot;)
(import &quot;lib/package-filter&quot;)
(import &quot;lib/filter&quot;)
(import &quot;lib/format-filter&quot;)

(define (server2)             
  (define server-output (current-output))

  (define (acceptor)
    (define chan (input-chain (current-input)
                              (thaw-filter)
                              (format-filter)))
    (forever
      (define data (wait chan))
      (print* data &quot;\n&quot;)))

  (spawn-tcp-server 10000 acceptor))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Usage from a client is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; (import &quot;lib/filter&quot;)
&amp;gt;&amp;gt; (import &quot;lib/package-filter&quot;)

&amp;gt;&amp;gt; (define s (tcp-connect &quot;127.0.0.1&quot; 10000))
&amp;gt;&amp;gt; (define chan (output-chain s (freeze-filter)))
&amp;gt;&amp;gt; (send &quot;hello&quot; chan)
&amp;gt;&amp;gt; (send &#39;(one (two three)) chan)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Through the use of the thaw/freeze filter there is no need to manually call &lt;code&gt;freeze&lt;/code&gt; and &lt;code&gt;thaw&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;Sending bytecode to other processes&lt;/h2&gt;

&lt;p&gt;Unfortunately it&#39;s not possible to freeze or thaw closures or functions. It is possible however to assemble Lisp to bytecode and send that. This enables sending new functions across OS processes and is how MOSREF is able to compile Lisp on the console and send it to the drone. This example will compile a function from source to bytecode and run it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; (define code &#39;((print &quot;Hello World\n&quot;)))
&amp;gt;&amp;gt; (define proc (assemble (optimize (compile code))))
&amp;gt;&amp;gt; (proc)
Hello World
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The result of &lt;code&gt;assemble&lt;/code&gt; can be frozen, sent somewhere and thawed:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; (define x (freeze (assemble (optimize (compile &#39;((print &quot;Hello World\n&quot;)))))))
&amp;gt;&amp;gt; (define y (thaw x))
&amp;gt;&amp;gt; (y)
Hello World
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Using this we can have an upgradable server process:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (server3)
  (define server-output (current-output))

  (define (acceptor)
    (define chan (input-chain (current-input)
                              (thaw-filter)))

    (define (loop chan)
      (define data (wait chan))
      (cond
        ((function? data)
          (return ((data) chan)))
        (else
          (print* &quot;OLD: &quot; (format data) &quot;\n&quot;)
          (return (loop chan)))))
     (loop chan))

  (spawn-tcp-server 10000 acceptor))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will display the data sent to the server prefixed by &quot;OLD:&quot; unless it is sent a function. In which case it calls that function as the new server loop. An upgraded server loop to prefix with &quot;NEW: &quot; is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (new-server3)
  (assemble
    (optimize
      (compile
        &#39;((define (loop chan)
            (define data (wait chan))
            (cond
              ((function? data)
                (return ((data) chan)))
              (else
                (print* &quot;NEW: &quot; (format data) &quot;\n&quot;)
                (return (loop chan))))))))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We can&#39;t send a function directly so this compiles the new loop from source and returns the compiled procedure. This can be frozen, sent to the server and it will execute it as the new loop. An example interaction:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;;; On Server
&amp;gt;&amp;gt; (server3)

;; On Client
&amp;gt;&amp;gt; (define s (tcp-connect &quot;127.0.0.1&quot; 10000))
&amp;gt;&amp;gt; (define chan (output-chain s (freeze-filter)))
&amp;gt;&amp;gt; (send &#39;(one (two three)) chan)

;; On Server
OLD: (one (two three))

;; On Client
&amp;gt;&amp;gt; (send (new-server3) chan)
&amp;gt;&amp;gt; (send &#39;(one (two three)) chan)

;; On Server
&amp;gt;&amp;gt; NEW: (one (two three))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Why not send the source to the server process and have it &lt;code&gt;eval&lt;/code&gt; it? The approach of sending the bytecode allows the server process to skip including the Lisp compiler. The Wasp VM includes an interpreter and deserializer - the compiler and other libraries are all in Lisp. A Wasp executable consists of the VM stub with bytecode appended to the end of it. On execution it looks for the bytecode, deserializes it and runs it. This provides a minimal program that can have functionality added by sending it bytecode as needed.&lt;/p&gt;

&lt;h2&gt;An aside on tail call optimization&lt;/h2&gt;

&lt;p&gt;It&#39;s important that a process loop is tail recursive otherwise each call through the loop will increase stack size and eventually exhaust memory. The following is not tail recursive in Wasp Lisp, even though it looks like it should be:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (test1 chan)
  (define msg (wait chan))
  (cond
    ((eq msg &#39;foo)
      (test1 chan))
    ((eq msg &#39;bar)
      (test1 chan))
    (else
      (test1 chan))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is because the recursive call to &#39;test1&#39; compiles down to bytecode that looks like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(newf)
(ldg eq)
(arg)
(ldg msg)
(arg)
(ldc bar)
(arg)
(call)
(jf false-47) ;; If the msg is not &#39;bar then jump to false-47
...
false-47
(newf)
(ldg test1)
(arg)
(ldg chan)
(arg)
(call)        ;; recursively call &#39;test1&#39;
done-46
done-44
(retn)        ;; return from function &#39;test1&#39;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The stack frame for &lt;code&gt;test1&lt;/code&gt; is not exited (the &lt;code&gt;retn&lt;/code&gt; instruction) until after the recursive call is done. Compare this to the obvious tail recursive case:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(newf)
(ldg wait)
(arg)
(ldg chan)
(arg)
(call)
(stg msg)
(newf)
(ldg test2)
(arg)
(ldg chan)
(arg)
(tail)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that &lt;code&gt;tail&lt;/code&gt; instruction. This does an immediate jump rather than a &lt;code&gt;call&lt;/code&gt; so a &lt;code&gt;retn&lt;/code&gt; is not necessary. The call stack does not grow. The difference between the two cases is due to the way the Wasp Lisp compiler generates the instructions and optimizes looking for tail calls. The instructions generated can be viewed using:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define x &#39;(define (test2 chan)
             (define msg (wait chan))
             (test2 chan)))
(define code (compile x))
(for-each (lambda (x) (print* (format x) &quot;\n&quot;)) code)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Using &lt;code&gt;compile&lt;/code&gt; shows the first pass which does not look for tail calls:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(newf)
(ldg test2)
(arg)
(ldg chan)
(arg)
(call)
(retn)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Notice the &lt;code&gt;call&lt;/code&gt; followed by &lt;code&gt;retn&lt;/code&gt;. This is the sequence that &lt;code&gt;optimize&lt;/code&gt; looks for to generate the &lt;code&gt;tail&lt;/code&gt; instruction:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define x &#39;(define (test2 chan)
             (define msg (wait chan))
             (test2 chan)))
(define code (optimize (compile x)))
(for-each (lambda (x) (print* (format x) &quot;\n&quot;)) code)
...
(newf)
(ldg test2)
(arg)
(ldg chan)
(arg)
(tail)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Looking back at the instructions for &lt;code&gt;test1&lt;/code&gt; the &lt;code&gt;call&lt;/code&gt; is followed by a jump or a label before &lt;code&gt;retn&lt;/code&gt; so the optimizer misses it. This can be worked around by doing an explicit &lt;code&gt;return&lt;/code&gt; statement:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (test3 chan)
  (define msg (wait chan))
  (cond
    ((eq msg &#39;foo)
      (return (test1 chan)))
    ((eq msg &#39;bar)
      (return (test1 chan)))
    (else
      (return (test1 chan)))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The code in the &lt;code&gt;cond&lt;/code&gt; branches generates to the following which is now a tail call:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(jf false-93)
(newf)
(ldg test1)
(arg)
(ldg chan)
(arg)
(tail)
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Some things to note&lt;/h2&gt;

&lt;p&gt;The Wasp VM is single threaded and non-preemptive. Threads yield to the scheduler explicitly using &lt;code&gt;yield&lt;/code&gt; or implicitly when doing i/o or waiting on a queue. The bytecode is cross platform. Serialized objects on one architecture can be deserialized on another. The Wasp VM history comes from Mosquito Lisp and MOSREF - a penetration testing platform. It&#39;s written in C with some GNU extensions (nested functions are used in the VM).&lt;/p&gt;

&lt;p&gt;This post came about from exploring the difference in Actor programming in the &lt;a href=&quot;http://www.ponylang.org/&quot;&gt;Pony programming language&lt;/a&gt; and a dynamic language where the Actor model isn&#39;t explicit. The programming style is similar in that pipelines of calls to actors to transform data is a common idiom.&lt;/p&gt;

&lt;p&gt;Wasp Lisp isn&#39;t actively developed anymore but the author, Scott Dunlop, still processes pull requests and monitors it. I like to use it for projects and tinker with it as it&#39;s an interesting little cross platform lisp. MOSREF is useful as a way to access and maintain servers of different architectures, aside from its use as a penetration testing tool.&lt;/p&gt;

&lt;p&gt;Some other Wasp resources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://sites.google.com/site/waspvm/&quot;&gt;Wasp Lisp main site&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://waspvm.blogspot.com&quot;&gt;Wasp Developments Blog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/swdunlop/WaspVM/&quot;&gt;Source on Github&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://web.archive.org/web/20070208062534/http://www.ephemeralsecurity.com/lisp&quot;&gt;Ephemeral Security Mosquito Lisp&lt;/a&gt; on the Internet Archive Wayback Machine.&lt;/li&gt;
&lt;/ul&gt;

</content>
 </entry>
 
 <entry>
   <title>Building Static Wasp Lisp Binaries on Linux</title>
   <link href="http://bluishcoder.co.nz/2016/06/05/building-static-wasp-lisp-binaries.html"/>
   <updated>2016-06-05T23:00:00+12:00</updated>
   <id>http://bluishcoder.co.nz/2016/06/05/building-static-wasp-lisp-binaries</id>
   <content type="html">&lt;p&gt;&lt;a href=&quot;http://bluishcoder.co.nz/2009/11/27/wasp-lisp-small-scheme-like-lisp.html&quot;&gt;Wasp Lisp&lt;/a&gt; builds binaries that are linked dynamically to &lt;a href=&quot;https://www.gnu.org/software/libc/&quot;&gt;glibc&lt;/a&gt;. This ties the binary to specific versions of Linux. It&#39;s usually not possible to run on an OS with older glibc versions than what it was compiled against. I wanted to be able to run a single binary of Wasp Lisp and &lt;a href=&quot;http://bluishcoder.co.nz/2009/11/28/using-wasp-lisp-secure-remote-injection.html&quot;&gt;MOSREF&lt;/a&gt; drones on new Ubuntu versions and some machines with an older version of Ubuntu. To do this I needed to have the libc linked statically.&lt;/p&gt;

&lt;p&gt;Changing Wasp Lisp to statically link glibc doesn&#39;t work though. Some networking routines in glibc &lt;a href=&quot;https://sourceware.org/glibc/wiki/FAQ#Even_statically_linked_programs_need_some_shared_libraries_which_is_not_acceptable_for_me.__What_can_I_do.3F&quot;&gt;require dynamic linking&lt;/a&gt;. If glibc is statically linked then networking doesn&#39;t work.&lt;/p&gt;

&lt;p&gt;The solution I opted for is to use &lt;a href=&quot;https://www.musl-libc.org/&quot;&gt;musl libc&lt;/a&gt; instead of glibc. This is a libc that was designed to be statically linked. To buid Wasp Lisp binaries with musl it required:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Building musl libc&lt;/li&gt;
&lt;li&gt;Building libevent using musl libc headers&lt;/li&gt;
&lt;li&gt;Building Wasp Lisp against musl and libevent&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;Building musl libc&lt;/h2&gt;

&lt;p&gt;Building musl libc requires using &lt;code&gt;git&lt;/code&gt; to clone the repository and following the standard &lt;code&gt;configure&lt;/code&gt;, &lt;code&gt;make&lt;/code&gt;, &lt;code&gt;make install&lt;/code&gt; invocations. The &lt;code&gt;bin&lt;/code&gt; directory for the musl tools is added to the &lt;code&gt;PATH&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ git clone git://git.musl-libc.org/musl
$ cd musl
$ ./configure
$ make
$ sudo make install
$ export PATH=$PATH:/usr/local/musl/bin/
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Building libevent&lt;/h2&gt;

&lt;p&gt;Building libevent with musl requires using the &lt;code&gt;musl-gcc&lt;/code&gt; command which was installed by the previous step. This invokes GCC with the required options to use musl. The following steps performs the build:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ wget https://github.com/libevent/libevent/releases/download/release-2.0.22-stable/libevent-2.0.22-stable.tar.gz
$ tar xvf libevent-2.0.22-stable.tar.gz
$ cd libevent-2.0.22-stable/
$ ./configure --prefix=/tmp/musl/usr CC=musl-gcc --enable-static --disable-shared
$ make
$ make install
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Building Wasp Lisp&lt;/h2&gt;

&lt;p&gt;The Wasp VM source requires a change to the &lt;code&gt;Makefile.cf&lt;/code&gt; to use static linking for all libraries. This changes:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;EXEFLAGS += -Wl,-Bstatic $(STATICLIBS) -Wl,-Bdynamic $(DYNAMICLIBS)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;to:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;EXEFLAGS += -static $(STATICLIBS) $(DYNAMICLIBS)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I&#39;ve made this change in the &lt;code&gt;static&lt;/code&gt; branch of my &lt;a href=&quot;https://github.com/doublec/waspvm&quot;&gt;github fork &lt;/a&gt;. This branch also includes some other changes from the &lt;a href=&quot;https://github.com/swdunlop/WaspVM&quot;&gt;official repository&lt;/a&gt; for real number support. Building with musl and libevent is done with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ git clone https://github.com/doublec/WaspVM --branch static
$ cd WaspVM
$ CC=musl-gcc CFLAGS=&quot;-I /tmp/musl/usr/include -L /tmp/musl/usr/lib&quot; make repl
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This runs directly into the Lisp REPL. The following confirms a static binary:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ldd wasp
not a dynamic executable
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Building MOSREF&lt;/h2&gt;

&lt;p&gt;The stub generated is also static and can be used to build static drones:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ cd mod
$ ../waspc -exe ../mosref bin/mosref
$ chmod +x ../mosref
$ ../mosref
console&amp;gt; set addr=xx.xx.xx.xx
console&amp;gt; set port=8000
console&amp;gt; drone mydrone foo linux-x86_64
Drone executable created.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The generated drone should run on a wider range of Linux versions than the non-static build at the cost of a larger size. I rename the &lt;code&gt;waspvm-linux-x86_64&lt;/code&gt; stub to be &lt;code&gt;waspvm-musl-x86-64&lt;/code&gt; so I can generate static drones or dynamic linked drones as needed from the MOSREF console by using &lt;code&gt;linux-x86_64&lt;/code&gt; or &lt;code&gt;musl-x86_64&lt;/code&gt; respectively.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Spawning Windows Commands in Wasp Lisp and MOSREF</title>
   <link href="http://bluishcoder.co.nz/2015/02/19/spawning-windows-commands-in-wasp-lisp-and-mosref.html"/>
   <updated>2015-02-19T23:00:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2015/02/19/spawning-windows-commands-in-wasp-lisp-and-mosref</id>
   <content type="html">&lt;p&gt;It&#39;s been a while since I last wrote about &lt;a href=&quot;http://bluishcoder.co.nz/2009/11/28/using-wasp-lisp-secure-remote-injection.html&quot;&gt;MOSREF&lt;/a&gt; and &lt;a href=&quot;http://bluishcoder.co.nz/2009/11/27/wasp-lisp-small-scheme-like-lisp.html&quot;&gt;Wasp Lisp&lt;/a&gt;. MOSREF is the secure remote injection framework written in Wasp Lisp. It facilitates penetration testing by enabling a console node to spawn drone nodes on different machines. The console handles communication between nodes and can run lisp programs on any node.&lt;/p&gt;

&lt;p&gt;The console can execute programs on other nodes with the input and output redirected to the console. One use for this is to create remote shells. MOSREF uses the Wasp Lisp function &lt;code&gt;spawn-command&lt;/code&gt;. The implementation for this in Linux is fairly small and simple. On Windows drones it&#39;s somewhat more difficult. It&#39;s not implemented in current Wasp Lisp and attempting to use the &lt;code&gt;sh&lt;/code&gt; command in MOSREF or the &lt;code&gt;spawn-command&lt;/code&gt; function in Lisp fails with an error.&lt;/p&gt;

&lt;p&gt;I&#39;ve been meaning to try implementing this for quite a while and finally got around to it recently. I&#39;m doing the work in the &lt;a href=&quot;https://github.com/doublec/WaspVM/tree/win_spawn&quot;&gt;win_spawn branch&lt;/a&gt; of my github fork of WaspVM. With that version of Wasp Lisp and MOSREF built with Windows and Linux stubs available you can spawn Windows commands and capture the output:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; (define a (spawn-command &quot;cmd.exe /c echo hi&quot;))
:: [win32_pipe_connection 5179A0]
&amp;gt;&amp;gt; (wait a)
:: &quot;hi\r\n&quot;
&amp;gt;&amp;gt; (wait a)
:: close
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Bidirectional communication works too:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; (define a (spawn-command &quot;cmd.exe&quot;))
:: [win32_pipe_connection 517770]
&amp;gt;&amp;gt; (wait a)
:: &quot;Microsoft Windows ...&quot;
&amp;gt;&amp;gt; (send &quot;echo hi\n&quot; a)
:: [win32-pipe-output 517748]
&amp;gt;&amp;gt; (wait a)
:: &quot;echo hi\nhi\r\n\r\nE:\l&amp;gt;&quot;
&amp;gt;&amp;gt; (send &quot;exit\n&quot; a)
:: [win32-pipe-output 517748]
&amp;gt;&amp;gt; (wait a)
:: &quot;exit\n&quot;
&amp;gt;&amp;gt; (wait a)
:: close
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With that implemented and some minor changes to MOSREF to remove the check for Windows you can interact with remote Windows nodes. I made a quick video demonstrating this. There is no sound but it shows a linux console on the left and a windows shell on the right running in a VM.&lt;/p&gt;

&lt;p&gt;I create a Windows drone and copy it to a location the Windows VM can access using the MOSREF &lt;code&gt;cp&lt;/code&gt; command. This actually copies from the console where the drone was create to another Linux drone called &lt;code&gt;tpyo&lt;/code&gt;. The Windows VM is running on the machine running &lt;code&gt;tpyo&lt;/code&gt; and access the drone executable. This is run in the VM to connect to the console.&lt;/p&gt;

&lt;p&gt;Once connected I run a few Lisp commands on the Windows node. The lisp is compiled to bytecode on the console, and the bytecode is shipped to the drone where it executes. The result then goes back to the console. This is all normal MOSREF operation and works already, I just do it to ensure things are working correctly.&lt;/p&gt;

&lt;p&gt;Next I run a &lt;code&gt;sh&lt;/code&gt; command which executes the command in the windows VM with the result sent back to view on the console. Then I do a typo which breaks the connection because of a bug in my code, oops. I &lt;code&gt;recover&lt;/code&gt; the drone, reconnect, and run a remote shell like I originally intended. This spawning of commands on Windows is the new code I have implemented.&lt;/p&gt;

&lt;p&gt;&lt;video width=&quot;640&quot; height=&quot;360&quot; controls=&quot;controls&quot;&gt;
  &lt;source src=&#39;http://bluishcoder.co.nz/waspvm/mosref.webm&#39; type=&#39;video/webm&#39; /&gt;
  &lt;source src=&#39;http://bluishcoder.co.nz/waspvm/mosref.mp4&#39; type=&#39;video/mp4&#39; /&gt;
&lt;/video&gt;&lt;/p&gt;

&lt;p&gt;The video is available as &lt;a href=&quot;http://bluishcoder.co.nz/waspvm/mosref.webm&quot;&gt;mosref.webm&lt;/a&gt;, &lt;a href=&quot;http://bluishcoder.co.nz/waspvm/mosref.mp4&quot;&gt;mosref.mp4&lt;/a&gt; or on &lt;a href=&quot;https://www.youtube.com/watch?v=ewmarUDA4vE&quot;&gt;YouTube&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The implementation on Windows required a bunch of Win32 specific code. I followed an MSDN article on &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/windows/desktop/ms682499%28v=vs.85%29.aspx&quot;&gt;redirecting child process output&lt;/a&gt; and another on &lt;a href=&quot;http://support.microsoft.com/kb/190351&quot;&gt;spawning console processes&lt;/a&gt;. This got the basic functionality working pretty quickly but hooking it into the Wasp Lisp event and stream systems took a bit longer.&lt;/p&gt;

&lt;p&gt;Wasp uses libevent for asynchronous network and timer functionality. I couldn&#39;t find a way for this to be compatible with the Win32 HANDLE&#39;s that result from the console spawning code. I ended up writing derived &lt;code&gt;connection&lt;/code&gt;, &lt;code&gt;input&lt;/code&gt; and &lt;code&gt;output&lt;/code&gt; Wasp VM classes for Win32 pipes that used Win32 Asynchronous RPC callbacks to avoid blocking reads. My inspiration for this was the &lt;a href=&quot;https://github.com/doublec/WaspVM/blob/master/vm/os.c#L507&quot;&gt;existing Wasp routines&lt;/a&gt; to interact with the Win32 console used by the REPL.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;connection&lt;/code&gt; is basically a bidirectional stream where you can obtain an &lt;code&gt;input&lt;/code&gt; and an &lt;code&gt;output&lt;/code&gt; channel. A &lt;code&gt;wait&lt;/code&gt; on an &lt;code&gt;input&lt;/code&gt; channel receives data and a &lt;code&gt;send&lt;/code&gt; on the &lt;code&gt;output&lt;/code&gt; channel transmits data. When &lt;code&gt;wait&lt;/code&gt; is called on the input channel a callback is invoked which should do the read. This can&#39;t block otherwise all Wasp VM coroutines will stop running. The callback instead sets a Win32 event which notifies a thread to read data and post the result back to the main thread via an &lt;a href=&quot;https://msdn.microsoft.com/en-nz/library/windows/desktop/ms681951%28v=vs.85%29.aspx&quot;&gt;asynchronous procedure call&lt;/a&gt;. A &lt;code&gt;send&lt;/code&gt; on the output channel invokes another callback which does the write. Although this can technically block if the pipe buffers are full I currently call &lt;code&gt;Write&lt;/code&gt; directly.&lt;/p&gt;

&lt;p&gt;The Wasp VM scheduler has code that checks if there are any active processes running and can do a blocking wait on libevent for notification to prevent spinning a polling loop. This had the side effect of preventing the asynchronous procedure call from running as Windows only executes it at certain control points. I had to insert a check that although our reading process was de-scheduled waiting for the APC, it was in fact still around and needed the event loop to spin so a call to &lt;code&gt;SleepEx&lt;/code&gt; occurs for the APC to run.&lt;/p&gt;

&lt;p&gt;I&#39;m still working on testing and debugging the implementation but it works pretty well as is. Before I submit a pull request I want to clean up the code a bit and maybe combine some of the duplicate functionality from the console handling code and the pipe code. I also need to check that I&#39;m cleaning up resources correctly, especially the spawned reading/APC handling threads.&lt;/p&gt;

&lt;p&gt;Some minor changes were needed to other parts of Wasp Lisp and those commits are in the github repository. They involve environment variable handling on Windows. First I had to enable the support for it, and then change it so on Windows the environment names were all uppercase. This avoided issues with Wasp looking for commands in &lt;code&gt;PATH&lt;/code&gt; vs the &lt;code&gt;Path&lt;/code&gt; that was set on my machine. On Windows these are case insensitive.&lt;/p&gt;

&lt;p&gt;For building a Windows compatible Wasp VM stub and REPL I used a cross compiler on Linux. I used the &lt;code&gt;gcc-mingw-w64&lt;/code&gt; package in Ubuntu for this. Build wasp VM with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ./configure --host=i686-w64-mingw32
$ OS=MINGW32 CC=i686-w64-mingw32-gcc make
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This puts the Windows stub in the &lt;code&gt;stubs&lt;/code&gt; directory. I copied this to the &lt;code&gt;stubs&lt;/code&gt; directory of the node running the console so it could generate Windows drones. I had to build &lt;a href=&quot;http://libevent.org/&quot;&gt;libevent&lt;/a&gt; for Windows using the same cross compiler and tweak the Wasp &lt;code&gt;Makefile.cf&lt;/code&gt; to find it. Removing the &lt;code&gt;-mno-cygwin&lt;/code&gt; flag was needed as well. I&#39;ll do patches to have the makefile work for cross compilation without changes if no one else gets to it.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Building Wasp Lisp and MOSREF for Android</title>
   <link href="http://bluishcoder.co.nz/2013/05/09/building-wasp-lisp-and-mosref-for-android.html"/>
   <updated>2013-05-09T19:00:00+12:00</updated>
   <id>http://bluishcoder.co.nz/2013/05/09/building-wasp-lisp-and-mosref-for-android</id>
   <content type="html">&lt;p&gt;Wasp Lisp is a small cross platform lisp by Scott Dunlop &lt;a href=&quot;http://bluishcoder.co.nz/2009/11/27/wasp-lisp-small-scheme-like-lisp.html&quot;&gt;that I&#39;ve written about before&lt;/a&gt; and MOSREF is the &lt;a href=&quot;http://bluishcoder.co.nz/2009/11/28/using-wasp-lisp-secure-remote-injection.html&quot;&gt;secure remote injection framework&lt;/a&gt; that is one of the applications written with it.&lt;/p&gt;

&lt;p&gt;I&#39;ve been wanting to get Wasp running on Android and Gonk (the low level Android layer of Firefox OS) for debugging and small applications. One of the nice features of Wasp is being able to have a minimal interpreter running on a platform and send byte code from another system to that interpreter which is loaded and run. You can even send the bytecode compiler itself to be loaded and run in the interpreter to get a full system on the target. This is the basis of how MOSREF works where the drone gets sent code to run from the console system.&lt;/p&gt;

&lt;h2&gt;Standalone Toolkit&lt;/h2&gt;

&lt;p&gt;Building Wasp for Android requires generating a &lt;a href=&quot;http://www.kandroid.org/ndk/docs/STANDALONE-TOOLCHAIN.html&quot;&gt;standalone toolchain&lt;/a&gt; from the Android NDK. This results in a vesion of &lt;code&gt;gcc&lt;/code&gt; and libraries that can be run from conventional makefiles and configure scripts.&lt;/p&gt;

&lt;p&gt;To generate a standalone toolchain to build Wasp I used:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$NDK_ROOT/build/tools/make-standalone-toolchain.sh \
      --platform=android-8 \
      --install-dir=$STK_ROOT
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Replace &lt;code&gt;NDK_ROOT&lt;/code&gt; with the path to the Android SDK and &lt;code&gt;STK_ROOT&lt;/code&gt; with the destination where you want the standalone toolkit installed. The &lt;code&gt;android-8&lt;/code&gt; in the platform makes the standalone toolkit generate applications for FroYo and above (See &lt;a href=&quot;http://www.kandroid.org/ndk/docs/STABLE-APIS.html&quot;&gt;STABLE-APIS.html&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Adding &lt;code&gt;$STK_ROOT/bin&lt;/code&gt; to the &lt;code&gt;PATH&lt;/code&gt; will make the compiler available:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;export PATH=$PATH:$STK_ROOT/bin
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Building libevent for Android&lt;/h2&gt;

&lt;p&gt;The Wasp virtual machine uses &lt;a href=&quot;http://libevent.org/&quot;&gt;libevent&lt;/a&gt; to enable asynchronous operations. Building this for Android using the standalone toolkit is straightforward:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;wget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
tar xvf libevent-2.0.21-stable.tar.gz
cd libevent-2.0.21-stable
./configure --host=arm-linux-androideabi --prefix=$STK_ROOT
make
make install
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This configures &lt;code&gt;libevent&lt;/code&gt; to be built using the ARM compilers in the standalone toolkit and to install the resulting libraries in the same install location as the toolkit. This makes it easy for other applications to find and link with the library.&lt;/p&gt;

&lt;h2&gt;Building the Wasp stub for Android&lt;/h2&gt;

&lt;p&gt;A Wasp VM stub file is used for generating Wasp applications. The bytecode for an application is appended to the stub file and the result made to be executable. By having stubs for various operating systems and architectures you can create executables specific to them easily. To build the Android stub:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git clone git://github.com/swdunlop/WaspVM.git
cd WaspVM
STRIP=arm-linux-androideabi-strip \
  CC=arm-linux-androideabi-gcc \
  CFLAGS=&quot;-I $STK_ROOT/include -L $STK_ROOT/lib&quot; \
  ARCH=arm-linux \
  OS=android \
  make
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To build we define the correct version of the build programs to compile for ARM and set the architecture and OS to stop the build system from picking up the host system version of these. The result is a &lt;code&gt;waspvm-android-arm-linux&lt;/code&gt; stub in the &lt;code&gt;stubs&lt;/code&gt; directory.&lt;/p&gt;

&lt;h2&gt;Creating the host Wasp programs&lt;/h2&gt;

&lt;p&gt;To easily create Wasp programs for different platforms we&#39;ll need versions of them for the host we are running on. By doing a &#39;clean&#39; followed by a &#39;make&#39; on the host we&#39;ll clean out the Android build files and rebuild what&#39;s needed for the host. The &#39;clean&#39; process doesn&#39;t remove the stub file we created in the previous step which still allows us to do android Wasp programs.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;make clean
make
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This produces a &lt;code&gt;waspvm-linux-x86_64&lt;/code&gt; stub in my &lt;code&gt;stubs&lt;/code&gt; directory. To generate the Wasp interpreter and other programs:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;make repl
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Exit out of the repl with &lt;code&gt;(exit)&lt;/code&gt; and there will be a &lt;code&gt;wasp&lt;/code&gt;, &lt;code&gt;waspc&lt;/code&gt; and &lt;code&gt;waspld&lt;/code&gt; program for the host system. Now we can make a version of &lt;code&gt;wasp&lt;/code&gt; for Android:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cd mod    
../waspc -exe ../awasp -platform android-arm-linux bin/wasp.ms
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;waspc&lt;/code&gt; command takes a stub file (defined by the &lt;code&gt;platform&lt;/code&gt; or &lt;code&gt;stub&lt;/code&gt; argument) and appends the bytecode resulting from the compilation of the Wasp lisp code given as an argument. &lt;code&gt;wasp.ms&lt;/code&gt; is the source for the intepreter. The output is set as &lt;code&gt;awasp&lt;/code&gt; which is our executable for running on Android. We can use a similar command for producing Android versions of the other Wasp programs. MOSREF for example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;../waspc -exe ../amosref -platform android-arm-linux bin/mosref.ms
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Running on Android&lt;/h2&gt;

&lt;p&gt;Running these executables on Android involves pushing them to somewhere writeable on the phone. If you have a rooted Android device (or a Firefox OS device) you can push them pretty much anywhere. On my non-rooted Jellybean Galaxy Note 2 I&#39;m limited to just &lt;code&gt;/data/local/tmp&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;adb push awasp /data/local/tmp
adb shell
cd /data/local/tmp
chmod 0755 awasp
./awasp
&amp;gt;&amp;gt; (+ 1 2 3)
:: 6
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This gives you a running Wasp interpreter. To develop and do interesting things on the phone you really need the Wasp &lt;code&gt;mod&lt;/code&gt; files and the stub files copied over:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;adb push mod /data/local/tmp
adb push stubs /data/local/tmp
adb shell
cd /data/local/tmp
./awasp
&amp;gt;&amp;gt; (import &quot;lib/http-client&quot;)
:: #t
&amp;gt;&amp;gt; (http-response-body (http-get &quot;http://icanhazip.com/&quot;))
:: &quot;xxx.xxx.xxx.xxx\n&quot;
&amp;gt;&amp;gt; (exit)
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;MOSREF Android drone&lt;/h2&gt;

&lt;p&gt;It&#39;s simple now to create a &lt;a href=&quot;http://bluishcoder.co.nz/2009/11/28/using-wasp-lisp-secure-remote-injection.html&quot;&gt;MOSREF drone&lt;/a&gt;  that runs on Android. Make sure the MOSREF console has a &lt;code&gt;stubs&lt;/code&gt; directory containing the Android stub then create the drone as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;console&amp;gt; drone drone1 myphone android-arm-linux
Drone executable created.
Listening for drone on 10000...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Copy the created &lt;code&gt;drone1&lt;/code&gt; onto the phone and run it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;adb push mod/drone1 /data/local/tmp
adb shell
cd /data/local/tmp
chmod 0755 drone1
./drone1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now on the console you can see the drone on the phone:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;console&amp;gt; nodes
NODES: console online address: 192.168.1.101 port: 10000
       myphone online
console&amp;gt; on myphone sh ls
...directory listing...
console on myphone do (print &quot;hello\n&quot;)
...prints hello on the phone &#39;adb shell&#39; session...
console&amp;gt; on myphone load lib/http-file-server.ms
:: spawn-http-file-server
console&amp;gt; on myphone do (offer-http-file 8080 &quot;/test&quot; &quot;text/plain&quot; &quot;Hello world!&quot;)
:: [queue 1F5DD00]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;load&lt;/code&gt; command shown above loads and compiles the Wasp lisp file on the host and sends the compiled bytecode to the drone on the phone. We then run the lisp code on the phone to start an HTTP server to serve data.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Wasp Lisp Respository Moves to Github</title>
   <link href="http://bluishcoder.co.nz/2011/01/14/wasp-lisp-repository-moves-to-github.html"/>
   <updated>2011-01-14T22:30:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2011/01/14/wasp-lisp-repository-moves-to-github</id>
   <content type="html">&lt;p&gt;Scott Dunlop, The author of &lt;a href=&quot;http://sites.google.com/site/waspvm/&quot;&gt;Wasp Lisp&lt;/a&gt;, has moved development from its bzr based &lt;a href=&quot;https://launchpad.net/waspvm&quot;&gt;launchpad location&lt;/a&gt; to &lt;a href=&quot;https://github.com/swdunlop/WaspVM&quot;&gt;WaspVM on github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Hopefully this will encourage more interest in the implementation with people forking and working on it. For an overview of Wasp Lisp you can read a couple of my previous posts on it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://bluishcoder.co.nz/2009/11/27/wasp-lisp-small-scheme-like-lisp.html&quot;&gt;Wasp Lisp - A Small Scheme-like Lisp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://bluishcoder.co.nz/2009/11/28/using-wasp-lisp-secure-remote-injection.html&quot;&gt;Using the Wasp Lisp Secure Remote Injection Framework (MOSREF)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;I used to have a private git repository containing an import of Wasp Lisp and various branches of things I was working on. With the move to github I&#39;ve rebased my changes on top of Scott&#39;s github repository and put them in the following branches in my &lt;a href=&quot;https://github.com/doublec/WaspVM&quot;&gt;github fork&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/doublec/WaspVM/tree/real_numbers&quot;&gt;real_numbers&lt;/a&gt;: This adds support for floating point numbers to Wasp Lisp. Some of the numerical operators are overloaded to accept floats and support for floats added to the serialisation code so they can be sent to other nodes.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/doublec/WaspVM/tree/opengl&quot;&gt;opengl&lt;/a&gt;: This was a start at adding support for OpenGL graphics. I never finished it but it&#39;s there in case I (or someone else) decides to hack on it again.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/doublec/WaspVM/tree/node_retry&quot;&gt;node_retry&lt;/a&gt;: This changes MOSREF drones so that they try to connect again if the connection to the console node breaks, or it can&#39;t connect in the first place.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;They&#39;ve been a couple of other thing I had tinkered with and these have been upstreamed already. The branches above are things that are either not ready for upstreaming or not suitable for other reasons.&lt;/p&gt;

&lt;p&gt;Another Wasp project I did a while back was create a skeleton reference documentation for the Wasp Lisp functions and primitives, with some basic build instructions. I put this in a &lt;a href=&quot;https://github.com/doublec/WaspVM-Articles&quot;&gt;WaspVM-Articles&lt;/a&gt; github repository. It requires &lt;a href=&quot;http://www.methods.co.nz/asciidoc/&quot;&gt;ASCIIDoc&lt;/a&gt; to build. A pre-generated version is available in &lt;a href=&quot;https://github.com/doublec/WaspVM-Articles/raw/8ba255d6b47a3d3669ffcc709aed2e8454486b9a/waspvm-articles.pdf&quot;&gt;waspvm-articles.pdf&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The documentation was created by using the &#39;waspdoc&#39; tool to get the import and exports of the Wasp Lisp modules. I added the primitives by grepping/searching the VM source code by hand. Hopefully this can form the base of some up to date reference material.&lt;/p&gt;

&lt;p&gt;You can see MOSREF in action from the older Mosquito Lisp system in these videos:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://media.tinyvid.tv/1c44dker58yqw.ogg&quot;&gt;HITBSecConf2006 Malaysia: Wes Brown - MOSREF&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://media.tinyvid.tv/1k3kych0da6wm.ogg&quot;&gt;Exploit Writing Using Injectable Virtual Machines&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;MOSREF was ported to Wasp Lisp by Scott Dunlop and is included in the source repository.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Using the Wasp Lisp Secure Remote Injection Framework (MOSREF)</title>
   <link href="http://bluishcoder.co.nz/2009/11/28/using-wasp-lisp-secure-remote-injection.html"/>
   <updated>2009-11-28T11:24:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2009/11/28/using-wasp-lisp-secure-remote-injection</id>
   <content type="html">&lt;p&gt;MOSREF is a secure remote injection framework written in &lt;a href=&quot;http://waspvm.blogspot.com/&quot;&gt;Wasp Lisp&lt;/a&gt; (which I previously wrote about &lt;a href=&quot;http://bluishcoder.co.nz/2009/11/27/wasp-lisp-small-scheme-like-lisp.html&quot;&gt;here&lt;/a&gt;). With MOSREF you have a &#39;console&#39; program running which can create dones applications that are run on a target system.&lt;/p&gt;

&lt;p&gt;The console and drones can communicate with each other, executing shell commands or Wasp Lisp code. Drones can create other drones to &#39;bridge&#39; bridge communications so the console can send commands to drones it is not directly connected too.&lt;/p&gt;

&lt;p&gt;Lisp code can be compiled and sent to drones to execute. One of the &#39;built in&#39; capabilities is a Socks proxy server. This can be run on a drone and data is tunneled to the console which the controller can use as a socks proxy.&lt;/p&gt;

&lt;p&gt;MOSREF isn&#39;t built by default with Wasp. To build it you need to run &#39;waspc&#39; to compile and create an executable (example assumes you are in the root of the Wasp source directory):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ cd mod
$ waspc -exe ../mosref bin/mosref
BUILD: bin/mosref
BUILD: core/macro
BUILD: core/config
BUILD: site/config
...
BUILD: mosref/cmd/with
BUILD: mosref/cmds
BUILD: bin/mosref
$ chmod +x ../mosref
$ rlwrap ../moseref
console&amp;gt; help
Commands: clear &amp;lt;key&amp;gt; ...
          cp &amp;lt;src-file&amp;gt; &amp;lt;dst-file&amp;gt;
          do &amp;lt;lisp-expr&amp;gt;
          drone &amp;lt;file&amp;gt; &amp;lt;id&amp;gt; &amp;lt;platform&amp;gt;
          exit
          help [i&amp;lt;command&amp;gt;]
          load &amp;lt;path&amp;gt;
          nodes
          on &amp;lt;node-id&amp;gt; [&amp;lt;command&amp;gt;]
          proxy [&amp;lt;portno&amp;gt; [&amp;lt;secret&amp;gt;]]
          recover &amp;lt;id&amp;gt;
          set [&amp;lt;key&amp;gt;[=&amp;lt;value&amp;gt;] [&amp;lt;command&amp;gt;]]
          sh &amp;lt;cmd&amp;gt;
          with &amp;lt;key&amp;gt;[=&amp;lt;value&amp;gt;] [&amp;lt;command&amp;gt;]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The first thing you need to do on the running console is set the IP address and the port it will listen on (Replacing xx.xx.xx.xx with the IP address):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;console&amp;gt; set addr=xx.xx.xx.xx
Set.
console&amp;gt; set port=10000
Set.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now you need to create a drone executable to be run on the target system:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;console&amp;gt; drone drone1 foo linux-x86
Drone executable created.
Listening for drone on 10000...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This creates an executable for Linux systems called &#39;drone1&#39;. It is given the name node name &#39;foo&#39;. It will show in the list of nodes available from the console:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;console&amp;gt; nodes
NODES: console online address: xx.xx.xx.xx port: 10000
       foo offline
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When the &#39;drone1&#39; executable is run on a target system it will connect to the console using an encrypted connection. You will need to find a way to copy and run the executable onto the target. The following shows it being run on the target:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;target$ ./drone1
  DRONE: Preparing keys...
  DRONE: Sending Drone Public Key...
  DRONE: Sending Drone IV...
  DRONE: Reading Console IV...
  DRONE: CONSOLE IV CT is ...
  DRONE: CONSOLE IV PT is ...
  DRONE: Confirming Console IV...
  DRONE: Waiting for Console to confirm Drone IV...
  DRONE: Affiliation complete....
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The connection will show on the console:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;console&amp;gt; nodes
NODES: console online address: xx.xx.xx.xx port: 10000
       foo online
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Notice it shows &#39;foo&#39; is now online. It&#39;s now possible to execute commands on the target machine by sending them to the drone. Using &#39;sh&#39; you can execute shell commands. From the console:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;console&amp;gt; on foo sh ifconfig
eth0      Link encap:Ethernet  HWaddr ...
          inet addr:yy.yy.yy.yy
...
          RX bytes:2458924520 (2.2 GB)  TX bytes:2458924520 (2.2 GB)
console&amp;gt; on foo set addr=yy.yy.yy.yy
Set.
console&amp;gt; on foo set port=10000
Set.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here we execute &#39;ifconfig&#39; on the target so we can find out the IP address. The output of &#39;ifconfig&#39; is sent back to the console. Using this information the addr and port is set on the drone. This can be used to have the target create new drones on internal machines that only it can access. It will then act as a bridge between the console and the internal machine.&lt;/p&gt;

&lt;p&gt;As well as running shell commands you can run Wasp Lisp code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;console&amp;gt; on foo do (+ 1 2)
:: 3
console&amp;gt; on foo load lib/http-file-server.ms
:: spawn-http-file-server
console&amp;gt; on foo do (offer-http-file 2080 &quot;/test&quot; &quot;text/plain&quot; &quot;Hello world!&quot;)
:: [queue 824A098]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Using &#39;on foo do&#39; we compile a Lisp expression and send it to &#39;foo&#39; to be run. In this case a simple addition. &#39;on foo load&#39; will load a Lisp file located on the console, compile it, and send the byte code to be run on &#39;foo&#39;. Then we &#39;on foo do&#39; to run a function contained in that file on the &#39;foo&#39; machine. In this case, it runs a simple webserver. Any Lisp code can be sent and run.&lt;/p&gt;

&lt;p&gt;It&#39;s possible to copy files between nodes too. In this next example we create another drone that will be run on a machine internal to the network that the target is on. Note that the build is performed on the console so we need to copy the executable from there to the target.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;console&amp;gt; on foo drone drone2 bar linux-x86
Drone executable created.
Listening for drone on 10000...
console&amp;gt; cp console:/home/console/waspvm/mod/drone2 foo:/tmp/drone2
Copy from console:/home/console/waspvm/mod/drone2 to foo:/tmp/drone2
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Using some magic means (an exploit, copying file using shell commands on the target drone, etc) we get &#39;drone2&#39; running on another machine available internally on the target network:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;target2$ ./drone2
  DRONE: Preparing keys...
  DRONE: Sending Drone Public Key...
  DRONE: Sending Drone IV...
  DRONE: Reading Console IV...
  DRONE: CONSOLE IV CT is ...
  DRONE: CONSOLE IV PT is ...
  DRONE: Confirming Console IV...
  DRONE: Waiting for Console to confirm Drone IV...
  DRONE: Affiliation complete....
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;From the console we now see the &#39;bar&#39; node:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;console&amp;gt; nodes
NODES: bar online
       console online address: xx.xx.xx.xx port: 10000
foo online address: yy.yy.yy.yy port: 10000
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;MOSREF has a &#39;proxy&#39; command that lets you set up a Socks 4 proxy on the console that tunnels traffic to and from a target node. Here we set up a proxy to the &#39;bar&#39; node:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;console&amp;gt; on bar proxy 5000
SOCKS Proxy created, listening on port 5000.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will result in the &#39;console&#39; machine having a Socks 4 proxy running on port 5000. If you configure Firefox to use this proxy you can access local webservers available from the internal network that &#39;bar&#39; can see. Note that &#39;console&#39; can&#39;t see &#39;bar&#39; directly. It is tunnelling traffic to &#39;foo&#39;, from there to &#39;bar&#39;, then back from &#39;bar&#39; to &#39;foo&#39; and to &#39;console.&lt;/p&gt;

&lt;p&gt;The source for MOSREF is in the Wasp Lisp distribution on &lt;a href=&quot;https://github.com/swdunlop/WaspVM&quot;&gt;github&lt;/a&gt; and makes for a good body of Wasp Lisp code for learning.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Wasp Lisp - a Small Scheme-like Lisp</title>
   <link href="http://bluishcoder.co.nz/2009/11/27/wasp-lisp-small-scheme-like-lisp.html"/>
   <updated>2009-11-27T21:35:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2009/11/27/wasp-lisp-small-scheme-like-lisp</id>
   <content type="html">&lt;p&gt;&lt;a href=&quot;http://waspvm.blogspot.com/&quot;&gt;Wasp Lisp&lt;/a&gt; is a small Scheme-like Lisp implementation developed by Scott Dunlop. It features a lightweight concurrency model (with similarities to &lt;a href=&quot;http://www.erlang.org&quot;&gt;Erlang&lt;/a&gt; and &lt;a href=&quot;http://code.google.com/p/termite/&quot;&gt;Termite&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Wasp Lisp was originally derived from &lt;a href=&quot;http://bc.tech.coop/blog/061119.html&quot;&gt;MOSREF&lt;/a&gt; - the Mosquito Secure Remote Execution Framework. It includes an implementation of MOSREF so can do similar things that the original was built for.&lt;/p&gt;

&lt;p&gt;Wasp feels a lot like Scheme. It has a REPL which you can use to try Lisp interactively. You can spawn lightweight threads and use channels to communicate between threads. Here&#39;s a simple example with a thread that loops forever, waits for data to be sent to a channel and then prints that out. From the REPL strings are interactively sent to the channel:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ rlwrap ../wasp
&amp;gt;&amp;gt; (define channel (make-queue))
:: [queue 98154C0]
&amp;gt;&amp;gt; (define (looper channel)
..   (forever
..     (define data (wait channel))
..     (print data)))
:: looper
&amp;gt;&amp;gt; (spawn looper channel)
:: [process looper]
&amp;gt;&amp;gt; (send &quot;hello\n&quot; channel)
hello
:: [queue-output 9815500]
&amp;gt;&amp;gt; (send &quot;world\n&quot; channel)
world
:: [queue-output 9815500]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Threads are co-operative in Wasp. You need to manually yield to switch from a thread. The following example doesn&#39;t manually yield and will constantly print &#39;a&#39; to the terminal:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(begin 
  (spawn 
    (lambda () 
      (forever (print &quot;a\n&quot;))))
  (spawn
    (lambda () 
      (forever (print &quot;b\n&quot;)))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To yield you use the &#39;pause&#39; function. Adding this to the &#39;forever&#39; loop in the example above will switch between the two threads:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(begin 
  (spawn
    (lambda () 
      (forever 
        (pause)
        (print &quot;a\n&quot;)))) 
  (spawn 
    (lambda () 
      (forever 
        (pause)
        (print &quot;b\n&quot;)))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Wasp Lisp code can get compiled to a compact bytecode format using the &#39;waspc&#39; command. This can produce a binary executable for the platform:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ cat &amp;gt;test.ms
(define (main)
  (print &quot;hello world!\n&quot;))
$ waspc -exe hello test.ms
BUILD: test
BUILD: core/macro
BUILD: core/config
BUILD: site/config
BUILD: core/file
BUILD: core/module
BUILD: core/io
BUILD: core/macro
BUILD: core/config
BUILD: site/config
BUILD: core/file
BUILD: core/module
BUILD: core/io
BUILD: test
$ chmod +x hello
$ ./hello
hello world!
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There is quite a bit of example code, including a simple HTTP server:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ rlwrap ../wasp
&amp;gt;&amp;gt; (import &quot;lib/http-file-server&quot;)
:: #t
&amp;gt;&amp;gt; (offer-http-file 2080 &quot;/test&quot; &quot;text/plain&quot; &quot;Hello world!&quot;)
:: [queue 985A1F0]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This serves the text &#39;Hello world!&#39; when http://localhost:2080/test is requested. The Wasp VM site has an informal &lt;a href=&quot;http://waspvm.googlepages.com/dragracingagainstthttpdusingsiege&quot;&gt;speed test of serving data&lt;/a&gt; comparing against THTTPD.&lt;/p&gt;

&lt;p&gt;Wasp builds and runs on Linux, Mac OS X and Windows. Instructions for building and links to other information are &lt;a href=&quot;http://waspvm.googlepages.com/&quot;&gt;here&lt;/a&gt;. The source is available on &lt;a href=&quot;https://github.com/swdunlop/WaspVM&quot;&gt;github&lt;/a&gt;. &lt;a href=&quot;http://www.monkey.org/~provos/libevent/&quot;&gt;libevent&lt;/a&gt; is needed to build.&lt;/p&gt;
</content>
 </entry>
 
 
</feed>
