google / flatbuffers

FlatBuffers: Memory Efficient Serialization Library

Home Page:https://flatbuffers.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

trying to send flatbuffers from javascript to C++ server , results in assertion failure in verify method.

RavikumarTulugu opened this issue · comments

Hi,
I am trying to send a simple flatbuffer from javascript ( webpage ) to C++ server. I am seeing that always the assertion fails during verify.
I am pasting below the stack trace of the crash. I am seeing that offset computed at the server is way bigger than the size of the buffer itself. I doubt whether my api usage is proper. my flatbuffers version is 1.12. I am using "flatbuffers.js" file from the google chrome git repository.

stacktrace :

#0  __pthread_kill_implementation (no_tid=0, signo=6, threadid=140186095112384) at pthread_kill.c:44
#1  __pthread_kill_internal (signo=6, threadid=140186095112384) at pthread_kill.c:80
#2  __GI___pthread_kill (threadid=140186095112384, signo=signo@entry=6) at pthread_kill.c:91
#3  0x00007f7f9ea00476 in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26
#4  0x00007f7f9e9e67b7 in __GI_abort () at abort.c:79
#5  0x00007f7f9e9e66db in __assert_fail_base (fmt=0x7f7f9eb9a770 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n", assertion=0x5f568a "ok", 
    file=0x5e94f0 "/usr/include/flatbuffers/flatbuffers.h", line=2156, function=<optimized out>) at assert.c:92
#6  0x00007f7f9e9f7e26 in __GI___assert_fail (assertion=0x5f568a "ok", file=0x5e94f0 "/usr/include/flatbuffers/flatbuffers.h", line=2156, 
    function=0x5f568d "bool flatbuffers::Verifier::Check(bool) const") at assert.c:101
#7  0x00000000005264bb in flatbuffers::Verifier::Check (this=0x7fffd6b6c5c0, ok=false) at /usr/include/flatbuffers/flatbuffers.h:2156
#8  0x000000000052652a in flatbuffers::Verifier::Verify (this=0x7fffd6b6c5c0, elem=262153, elem_len=1)
    at /usr/include/flatbuffers/flatbuffers.h:2175
#9  0x00000000005262c4 in flatbuffers::Verifier::VerifyOffset (this=0x7fffd6b6c5c0, start=0) at /usr/include/flatbuffers/flatbuffers.h:2318

in the stack frame # 8 'elem' is way too bigger than the size of the buffer itself.
flatbuffers definition:

//lobby list request
table GetLobbyList {
  gameName : string;
}

// Message switch
union MessageType {
 GetLobbyList, GameInvite, AcceptInvite, DeclineInvite, CancelInvite
}

javascript code:

function getLobbyList ( gamename ) {
  var fbb = new flatbuffers.Builder();
  var req = createGetLobbyList( fbb, fbb.createString(gamename));  <----- generated by flatbuf compiler.
  var msg = createMessage( fbb, MessageType.GetLobbyList, req ); <------- generated by flatbuf compiler. 
  fbb.finish( msg );
  ws.send( fbb.asUint8Array() );
  return;
}

bytebuffer hex dump on client:
c00080a090408000c00002060804060004000e000666f6f62696c6c617264706c757300

Please understand i cannot paste some confidential parts of the code.

can some one pls answer this, this is very critical to our progress.

(Note: we prefer if you keep you questions to one site, see same question on stackoverflow).

Have you checked that the bytes you think you are sending are the same ones you are receiving?

i know it is a weekend , i live on other side. i grew a bit desperate and posted the same on stackoverflow as well. I am closing it here and will continue the discussion over there.

I am reopening the bug as i didnt get anywhere since morning, i also deleted the stackoverflow post to continue discussing here. I verified the hexdump on the client and server byte by byte and i dont see any difference. The hexdump is same both on client and server. I suspect my javascript code as there is a certain order for the apis to be called. I ommitted some code for brevity reasons.
I replaced the javascript code from below

function getLobbyList ( gamename ) {
  var fbb = new flatbuffers.Builder();
  var req = createGetLobbyList( fbb, fbb.createString(gamename));  <----- generated by flatbuf compiler.
  var msg = createMessage( fbb, MessageType.GetLobbyList, req ); <------- generated by flatbuf compiler. 
  fbb.finish( msg );
  ws.send( fbb.asUint8Array() );
  return;
}

to

function getLobbyList ( gamename ) { 
  var fbb = new flatbuffers.Builder();
  var gn = fbb.createString ( gamename);
  GetLobbyList.startGetLobbyList(fbb);
  GetLobbyList.addGameName(fbb, gn );
  var req = GetLobbyList.endGetLobbyList(fbb);
  Message.startMessage(fbb);
 Message.addMessage(fbb, req);
  Message.addMessageType(fbb, MessageType.GetLobbyList);
  var msg = Message.endMessage(fbb);
  fbb.finish( msg );
  Module.__ws.send( fbb.asUint8Array() );
  var bytes =  fbb.asUint8Array()
  var hexString = ""; 
  for(var i = 0; i < bytes.length; i++) hexString += bytes[i].toString(16).padStart(2,'0');
  console.log(bytes.length);
  console.log(hexString);
  return;
}

The hexdump on client and server side for reference.
0c00000008000e000700080008000000000000200c000000000006000800040006000000040000000e000000666f6f62696c6c617264706c75730000

Please advise , i have done maximum from my side and provided all the information needed. The tutorials available on the flatbuffers javascript are very rare and are using old api.

I am using "flatbuffers.js" file from the google chrome git repository.

We don't really support using this file from another repo. I can only look into things with the current typescript implementation at head: https://github.com/google/flatbuffers/blob/master/ts/flatbuffers.ts

Even if there was a bug in the file you are using, we cannot patch it, as we only work at head. So if you could reproduce the issue with the latest code, using typescript transpiled to javascript, we could spend more time looking into it.