tessel / t1-runtime

[UNMAINTAINED] Tessel 1 JavaScript runtime.
Other
117 stars 33 forks source link

Writing buffer to `process.stdout` causes segfault #686

Closed natevw closed 9 years ago

natevw commented 9 years ago

With npm install tap:

require('tap').test("Nothing", function (t) {
    t.end();
});

Expected:

TAP version 13

Nothing

1..0

tests 0

ok

Actual:

Segmentation fault: 11

LLDB reveals (full session) that colony_tolutf8 is passing along a NULL pointer from lua_tolstring. Inserting this assert confirms that this is the source:

diff --git a/src/colony/lua_tm.c b/src/colony/lua_tm.c
index 1f3a464..ffe7c36 100644
--- a/src/colony/lua_tm.c
+++ b/src/colony/lua_tm.c
@@ -11,6 +11,7 @@
 #include <lauxlib.h>
 #include <lualib.h>
 #include <math.h>
+#include <assert.h>

 #include "tm.h"
 #include "colony.h"
@@ -62,6 +63,7 @@ const char* colony_tolutf8 (lua_State* L, int index, size_t* res_len)
 {
   size_t str_len;
   const uint8_t* str = (const uint8_t*) lua_tolstring(L, index, &str_len);
+  assert(str);

   const uint8_t* utf8;
   size_t utf8_len = tm_str_to_utf8(str, str_len + 1, &utf8) - 1;    // compensate for NUL byte at end

So basically something is trying to use this on a value that is neither string nor number — is there some way perhaps a buffer could be making it's way through process.stdout or something?

natevw commented 9 years ago

Yup:

process.stdout.write(Buffer(0))

Segmentation fault: 11

natevw commented 9 years ago

Anything written/piped to these streams should be passed along as a string in stream.setEncoding('utf8') sort of fashion, or at least whatever is doing the actual "pipe" from those streams to the tm_log stuff should be making sure the conversion has happened.

natevw commented 9 years ago

https://github.com/tessel/runtime/blob/19dc4b718de24070c9216f9cc1d70d7981dded75/src/colony/lua/preload.lua#L175