Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ALP4IoT 2017

ALP4IoT 2017

Towards Runtime Monitoring of Node.js and Its
Application to the Internet of Things

Luca Franceschini

September 18, 2017
Tweet

More Decks by Luca Franceschini

Other Decks in Research

Transcript

  1. Towards Runtime Monitoring of Node.js and Its Application to the

    Internet of Things Davide Ancona Luca Franceschini Giorgio Delzanno Maurizio Leotta Marina Ribaudo Filippo Ricca 1st workshop on Architectures, Languages and Paradigms for IoT September 18th, 2017, Turin, Italy University of Genoa, Italy
  2. Node.js Node.js has become popular… but why? • JavaScript based

    • High performance • Huge ecosystem 1/16
  3. Node.js Node.js has become popular… but why? • JavaScript based

    • High performance • Huge ecosystem … and now the Internet of Things! 1/16
  4. Node.js & IoT Key features for the Internet of Things

    • Large volume of data generated, a lot of requests → that’s what Node.js is good at! 2/16
  5. Node.js & IoT Key features for the Internet of Things

    • Large volume of data generated, a lot of requests • Devices are now able to run JavaScript and Node.js → Raspberry Pi, Intel Edison, Beaglebone, Arduino… 2/16
  6. Node.js & IoT Key features for the Internet of Things

    • Large volume of data generated, a lot of requests • Devices are now able to run JavaScript and Node.js • Growing number of modules and projects for IoT → Cylon.js, Node-RED, Johnny-Five, Azure IoT… 2/16
  7. Node.js & IoT Key features for the Internet of Things

    • Large volume of data generated, a lot of requests • Devices are now able to run JavaScript and Node.js • Growing number of modules and projects for IoT • No more C/C++ low-level programming → V8 runtime is written in C++ itself 2/16
  8. Node.js hello world const http = require('http'); const server =

    http.createServer( (req, res) => res.end('Hello World!') }); server.listen(8080); 4/16
  9. Node.js and files const fs = require('fs'); const LIMIT =

    2**8; fs.open('tmp.txt', 'w', (err, fd) => { if (!err) for (let i=0; i<LIMIT; i++) fs.write(fd, i+'\n', () => {}); fs.close(fd, console.log('bye')); }); 5/16
  10. Node.js and files const fs = require('fs'); const LIMIT =

    2**24; // a bit more... fs.open('tmp.txt', 'w', (err, fd) => { if (!err) for (let i=0; i<LIMIT; i++) fs.write(fd, i+'\n', () => {}); fs.close(fd, console.log('bye')); }); 6/16
  11. Looking at the documentation “Note that it is unsafe to

    use fs.write multiple times on the same file without waiting for the callback. For this scenario, fs.createWriteStream is strongly recommended.” 8/16
  12. Runtime Verification Different approaches • Static analysis: not so easy

    in such a dynamic language • Testing: bugs like this can be hard to reproduce 9/16
  13. Runtime Verification Different approaches • Static analysis: not so easy

    in such a dynamic language • Testing: bugs like this can be hard to reproduce • Runtime verification! 9/16
  14. Monitoring Node.js code Things we need to monitor: • Function

    calls (like fs.write) • Callback execution (and the operation from which they are triggered) 10/16
  15. Code instrumentation with Jalangi Our approach Code instrumentation The tool

    Jalangi (actually Jalangi2) The features we need Jalangi allows to run arbitrary code before and a ter every function call (and much more) 11/16
  16. Specification We use trace expressions to specify the expected behavior

    File write specification O = ε ∨ (open : W) W = (write : callback : W) ∨ (close : ε) 12/16
  17. Specification We use trace expressions to specify the expected behavior

    File write specification O = ε ∨ (open : W) W = (write : callback : W) ∨ (close : ε) Recursion: trace expressions can be cyclic! 12/16
  18. Specification Trace expressions can also be parametric Multiple files specification

    O = ε ∨ ⟨id; open(id) : (O′ | O)⟩ O′ = ⟨fd; callback(id, fd) : W⟩ W = ⟨id2; write(id2, fd) : callback(id2) : W⟩ ∨ C C = ⟨id3; close(id3, fd) : callback(id3) : ε⟩ 13/16
  19. HTTP monitoring The http module has many unsafe behaviors as

    well: • “Node.js does not check whether Content-Length and the length of the body which has been transmitted are equal or not” • “the 204 and 304 responses must not include a message body” • “The method, response.end(), MUST be called on each response” • “if a 'response' event handler is added, then the data from the response object must be consumed” • and more… 15/16
  20. Conclusion and future work • This work is a first

    step towards runtime verification of Node.js and Node-RED systems using trace expressions 16/16
  21. Conclusion and future work • This work is a first

    step towards runtime verification of Node.js and Node-RED systems using trace expressions • The specification can also be used without any knowledge about trace expressions to verify correct use of libraries 16/16
  22. Conclusion and future work • This work is a first

    step towards runtime verification of Node.js and Node-RED systems using trace expressions • The specification can also be used without any knowledge about trace expressions to verify correct use of libraries • Decentralized: a single monitor server can verify a distributed system 16/16
  23. Conclusion and future work • This work is a first

    step towards runtime verification of Node.js and Node-RED systems using trace expressions • The specification can also be used without any knowledge about trace expressions to verify correct use of libraries • Decentralized: a single monitor server can verify a distributed system • Trace expressions are very expressive 16/16
  24. Conclusion and future work • This work is a first

    step towards runtime verification of Node.js and Node-RED systems using trace expressions • The specification can also be used without any knowledge about trace expressions to verify correct use of libraries • Decentralized: a single monitor server can verify a distributed system • Trace expressions are very expressive • Still a lot to do! Real-world programs to test, more modules and libraries, performances and scalability… 16/16