RemoteAPI floating point BUS ERROR on ARM processors

Report crashes, strange behaviour, or apparent bugs
Post Reply
shissamvrep
Posts: 2
Joined: 27 Apr 2016, 13:42

RemoteAPI floating point BUS ERROR on ARM processors

Post by shissamvrep » 27 Apr 2016, 14:21

Product: V-REP Remote API Client Interface
Version: 3.3.0
Platform(s) affected: ARMv7 Processor rev 5 (v7l) (RaspberryPI model 2), rev 3 (Odroid XU4)
Operating systems: Linux 4.1.13-v7+ (pi), 3.10.82-39 (odroid)
Compiler: gcc version 4.6.3 (pi), gcc version 4.9.2 (odroid)
File: programming/remoteApi/extApi.c
Function/method: simxFloat _readPureDataFloat()
Line(s): 1112
Problem: On the client side, unmarshalling of floating point number from V-REP server received char buffer where an offset from the beginning of that buffer is cast to a simxFloat and then dereferenced can cause a BUS ERROR (crash/core dump) when that memory reference is not aligned properly.
Offered solution: Explicitly copy the necessary bytes from the char buffer into a properly aligned memory location and then continue with the remaining client side conversion.
Offered patch:

Code: Select all

--- ./V-REP_PRO_EDU_V3_3_0_64_Linux/programming/remoteApi/extApi.c      2016-02-19 20:09:13.000000000 +0000
+++ ./vrep/programming/remoteApi/extApi.c       2016-04-27 12:20:58.120018564 +0000
@@ -1109,7 +1109,16 @@
                        stringCnt--;
                }
                additionalOffset+=byteOffset;
+#if 1
+               /*
+                * works on ARM and X86* arch. On ARM, the alternative causes a BUS ERROR
+                * due to alignment error for float.
+                */
+               memcpy (&retVal, commandPointer+SIMX_SUBHEADER_SIZE+additionalOffset, sizeof(simxFloat));
+               retVal=extApi_endianConversionFloat(retVal);
+#else
                retVal=extApi_endianConversionFloat(((simxFloat*)(commandPointer+SIMX_SUBHEADER_SIZE+additionalOffset))[0]);
+#endif
        }
        return(retVal);
 }

coppelia
Site Admin
Posts: 6766
Joined: 14 Dec 2012, 00:25

Re: RemoteAPI floating point BUS ERROR on ARM processors

Post by coppelia » 28 Apr 2016, 15:29

Thanks for the info!

It seems that several other locations in the code also face that problem.

Cheers

shissamvrep
Posts: 2
Joined: 27 Apr 2016, 13:42

Re: RemoteAPI floating point BUS ERROR on ARM processors

Post by shissamvrep » 28 Apr 2016, 15:56

I saw some of those, and I could have been more precise in the post yesterday.

I am not an expert in how gcc decides which asm instructions to emit and when but there alignment seems to only occur when the dereference is done on the stack. here is a cutdown

Code: Select all

$ gcc -o fltest_cutdown fltest_cutdown.c 
$ ./fltest_cutdown 
value 0.000000
Bus error (core dumped)
$ uname -a
Linux node7 3.10.82-39 #1 SMP PREEMPT Fri Jul 10 17:47:37 BRT 2015 armv7l armv7l armv7l GNU/Linux
and the code is

Code: Select all

#include <stdio.h>
#include <string.h>

/*
 * gcc -o fltest_cutdown fltest_cutdown.c
 */
int main(int argc, char**argv)
{
  unsigned char buf[16];
  unsigned int i;
  memset (buf, 1, 16);
  float x;

  for (i = 0; i < 12; ++i)
  {
    x = *((float*)(buf + i)); // no error
    printf ("value %f\n", *((float*)(buf + i))); // bus error
  }
}
since repairing / patch the line at 1112 (in the original post) all of our remote/distributed vrep clients have been working fine.

HTH
scott

Post Reply