{"id":96,"date":"2018-09-17T22:23:38","date_gmt":"2018-09-17T22:23:38","guid":{"rendered":"http:\/\/www.mbarsinai.com\/blog\/?p=96"},"modified":"2018-09-17T22:23:38","modified_gmt":"2018-09-17T22:23:38","slug":"the-curious-case-of-the-maze-walker-and-the-cows-tail","status":"publish","type":"post","link":"https:\/\/www.mbarsinai.com\/blog\/2018\/09\/17\/the-curious-case-of-the-maze-walker-and-the-cows-tail\/","title":{"rendered":"The Curious Case of the Maze Walker and the Cow&#8217;s Tail"},"content":{"rendered":"<h2 id=\"the-backslash-incident\">The Backslash Incident<\/h2>\n<p>I guess it\u2019s never a good time to find out your code has bugs. But there are exceptionally bad times to do so. A middle of a live demo is one such example. Immediately after said demo, when you\u2019ve invited the attendees to take your code for a spin, is not too good either.<\/p>\n<p>Case in point: I had the honor of presenting <a href=\"https:\/\/github.com\/bThink-BGU\/BPjs\">BPjs<\/a> at a seminar at Harvard School of Engineering and Applied Sciences (SEAS). BPjs is a tool suite for running and verifying programs written in JavaScript, using the <a href=\"https:\/\/b-prog.org\">Behavioral Programming<\/a> programming paradigm. In short (very very very short): It\u2019s a paradigm that focuses on how the system should behave, rather than on how its implemented (e.g.\u00a0objects, functions, or procedures). Because behavioral programs (&#8220;b-programs&#8221; for short) can be verified against a set of formal specification, BPjs allows writing verified systems in JavaScript. Which is, admittedly, weird. But in a good way.<\/p>\n<p>As part of the above mentioned demo, I\u2019ve created <a href=\"https:\/\/github.com\/bThink-BGU\/VisualRunningExamples\">VisualRunningExamples<\/a>, an application that allows playing with a behavioral program for a maze walker. Users can load and modify the walker&#8217;s behavioral model and formal specification, and then run or verify it, using a friendly GUI. It also serves as an example of how to build a program around a BPjs model.<\/p>\n<div id=\"attachment_97\" style=\"width: 257px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/i0.wp.com\/www.mbarsinai.com\/blog\/wp-content\/uploads\/2018\/09\/Screen-Shot-2018-08-15-at-19.28.23.png\"><img data-recalc-dims=\"1\" decoding=\"async\" aria-describedby=\"caption-attachment-97\" loading=\"lazy\" class=\"size-medium wp-image-97\" src=\"https:\/\/i0.wp.com\/www.mbarsinai.com\/blog\/wp-content\/uploads\/2018\/09\/Screen-Shot-2018-08-15-at-19.28.23.png?resize=247%2C300\" alt=\"\" width=\"247\" height=\"300\" srcset=\"https:\/\/i0.wp.com\/www.mbarsinai.com\/blog\/wp-content\/uploads\/2018\/09\/Screen-Shot-2018-08-15-at-19.28.23.png?resize=247%2C300&amp;ssl=1 247w, https:\/\/i0.wp.com\/www.mbarsinai.com\/blog\/wp-content\/uploads\/2018\/09\/Screen-Shot-2018-08-15-at-19.28.23.png?resize=768%2C935&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.mbarsinai.com\/blog\/wp-content\/uploads\/2018\/09\/Screen-Shot-2018-08-15-at-19.28.23.png?resize=842%2C1024&amp;ssl=1 842w, https:\/\/i0.wp.com\/www.mbarsinai.com\/blog\/wp-content\/uploads\/2018\/09\/Screen-Shot-2018-08-15-at-19.28.23.png?w=1282&amp;ssl=1 1282w\" sizes=\"auto, (max-width: 247px) 100vw, 247px\" \/><\/a><p id=\"caption-attachment-97\" class=\"wp-caption-text\">VisualRunningExamples &#8211; Maze solution obtained by formal verification of the maze and walker model.<\/p><\/div>\n<p>One fun part of VisualRunningExample\u2019s UI is the selection of the maze the walker would roam in. My favorite maze has a cow (created using <code>cowsay<\/code>, <a href=\"http:\/\/www.mbarsinai.com\/blog\/2013\/08\/23\/geek-dad-12ffa-cowsay\/\">of course<\/a>). During the presentation I let the walker walk around the cow a bit, and I thought I saw it demonstrating a bad behavior &#8211; it seemed like it was going through a wall. I&#8217;m not an expert in maze walking, but I assume maze walkers should not do that.<\/p>\n<p>Immediately after the presentation, I looked into this in more depth. It seemed like the walker was moving diagonally. Which it should never do, since no part of the program requests such a move. It ostensibly went from point X to point Y:<\/p>\n<pre class=\"ascii\"><code>_______\r\n&lt;BP(moo) &gt;     @@@@@@@@@   t\r\n-------\r\n        \\<strong style=\"color: red;\">Y<\/strong>  ^__^    @@@@@@@@\r\n@ @@@@@ <strong style=\"color: red;\">X<\/strong>\\  (oo)_______\r\ns           (__)~~~~~~~)\\\/\\\r\n@ @@@@@        ||----w~|\r\n               ||     ||<\/code><\/pre>\n<p>Was this really happening? Using <em>VisualRunningExamples<\/em>, I\u2019ve added the following b-threads, and verified the program against it. The idea behind this code is that whenever the maze walker gets into a cell, a set of b-threads that look for diagonal entries is\u00a0spawned. These b-threads blow the program up if they detect a diagonal mode. Pretty neat pattern, I think (reminiscent of LSC\u2019s symbolic charts used to forbid events):<\/p>\n<pre lang=\"javascript\">bp.registerBThread(\"no-diagonal\", function(){\r\n    while (true) {\r\n      var crd=getCoords(bp.sync({waitFor:anyEntrance}).name);\r\n    \tnoEntryNextMove(crd.col-1, crd.row-1);\r\n    \tnoEntryNextMove(crd.col-1, crd.row+1);\r\n    \tnoEntryNextMove(crd.col+1, crd.row-1);\r\n    \tnoEntryNextMove(crd.col+1, crd.row-+1);\r\n    }\r\n});\r\n\r\nfunction noEntryNextMove(col, row){\r\n    bp.registerBThread(\"nenv(\" + col + \",\" + row + \")\", function(){\r\n        var evt = bp.sync({waitFor:anyEntrance});\r\n        var newCoords = getCoords(evt.name);\r\n        bp.ASSERT( newCoords.col !== col || newCoords.row !== row, \"Diagonal Movement detected\");\r\n    });\r\n}\r\n<\/pre>\n<p>Verification did not find anything wrong, yet that parallel movement is happening, live, in front of my eyes. So, verification is broken too. Highly embarrassing, given that I just presented the tools at Harvard SEAS\u2019 SPYS group. Time to do some runtime verification.<\/p>\n<p>Runtime verification really boils down to adding these b-threads to the b-program (\u201cmodel\u201d), and running it. Easily done, and we know the runtime works. So, yay, next time mr. maze walker wants to do a diagonal jump, one of these b-threads will catch it red-handed. With an evil grin, I click the <code>Run<\/code> button.<\/p>\n<p>Nothing. The walker does diagonal jumps like a boss. But only between points <code>X<\/code> and <code>Y<\/code>. Luckily, <em>VisualRunningExamples<\/em>\u2019s UI is fun to play with, so I change the <code>\\<\/code> to <code>*<\/code>.<\/p>\n<p>No more parallel jumps, which never really happened. Good news: BPjs\u2019 verification works. As does its runtime.<\/p>\n<h3 id=\"what-went-wrong\">What Went Wrong<\/h3>\n<p>What really happened was that when creating the source Javascript b-program, the Java layer was copying the maze into an array of strings, verbatim. This meant that <code>\\<\/code> was really an escaped space, which translates to a single space in JavaScript:<\/p>\n<pre lang=\"javascript\">jjs$ var s = \"\\ \"\r\njjs$ \"|\" + s + \"|\"\r\n| |\r\njjs$\r\n<\/pre>\n<p>The Java UI, on the other hand, displayed the backslash in the maze monitor table. In short: the JavaScript b-program saw a space there, whereas Java saw a backslash. The ostensible diagonal jumps were just the maze walker going from <code>X<\/code> to <code>Y<\/code> through an empty cell, that was marked by the Java layer as a\u00a0<code>\\<\/code> cell.<\/p>\n<h3 id=\"lessons-learned\">Lessons Learned<\/h3>\n<ul>\n<li>Need to be careful when constructing code from user input (OK, that\u2019s not a very new lesson, more of a reminder really. But still).<\/li>\n<li>If you have a suspicion something is wrong in a b-program, BPjs makes it easy to verify.<\/li>\n<li>Tooling and UI that make the b-program easy to play with are very important. All in all, I went from having a suspicion to verifying the code in about an hour of casual coding on the couches at an empty student center somewhere in the Harvard campus.<\/li>\n<li>Since BPjs uses the same structures for verification and for modeling\/execution (namely, b-threads written in JavaScript), it is trivial to share code between the b-program and its formal specification. That\u2019s important, e.g., when one wants to extract data from strings.<\/li>\n<\/ul>\n<p>My experience with this whole BPjs as a verification platform, is that it enables \u201ccasual verification\u201d &#8211; it is very easy to create and run verification code, all the tools are there already, and you\u2019re probably in the mindset as well. Compared to re-creating the system by modeling it in, say, PROMELA, it\u2019s much more casual. Not to mention translation errors between model and actual software.<\/p>\n<p>VisualRunningExamples now has a shiny new cow that does not contain any backslashes. I kept the old cow too, but it\u2019s now called \u201ccow (bad)\u201d. Escaping the maze text properly is followed by <a href=\"https:\/\/github.com\/bThink-BGU\/VisualRunningExamples\/issues\/1\">issue #1<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Backslash Incident I guess it\u2019s never a good time to find out your code has bugs. But there are exceptionally bad times to do so. A middle of a live demo is one such example. Immediately after said demo, when you\u2019ve invited the attendees to take your code for a spin, is not too [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[3,4,1],"tags":[15],"class_list":["post-96","post","type-post","status-publish","format-standard","hentry","category-programming","category-talk","category-uncategorized","tag-behavioralprogramming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3NnQg-1y","_links":{"self":[{"href":"https:\/\/www.mbarsinai.com\/blog\/wp-json\/wp\/v2\/posts\/96","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mbarsinai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mbarsinai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mbarsinai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mbarsinai.com\/blog\/wp-json\/wp\/v2\/comments?post=96"}],"version-history":[{"count":2,"href":"https:\/\/www.mbarsinai.com\/blog\/wp-json\/wp\/v2\/posts\/96\/revisions"}],"predecessor-version":[{"id":100,"href":"https:\/\/www.mbarsinai.com\/blog\/wp-json\/wp\/v2\/posts\/96\/revisions\/100"}],"wp:attachment":[{"href":"https:\/\/www.mbarsinai.com\/blog\/wp-json\/wp\/v2\/media?parent=96"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mbarsinai.com\/blog\/wp-json\/wp\/v2\/categories?post=96"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mbarsinai.com\/blog\/wp-json\/wp\/v2\/tags?post=96"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}