Jelajahi Sumber

Merge pull request #1480 from turol/ubsan

Undefined Behavior sanitizer
turol 8 tahun lalu
induk
melakukan
82b269c424
6 mengubah file dengan 24 tambahan dan 4 penghapusan
  1. 4 0
      .travis.sh
  2. 3 0
      .travis.yml
  3. 10 0
      CMakeLists.txt
  4. 4 2
      code/B3DImporter.cpp
  5. 2 1
      code/FBXBinaryTokenizer.cpp
  6. 1 1
      code/IFCBoolean.cpp

+ 4 - 0
.travis.sh

@@ -26,6 +26,10 @@ function generate()
         OPTIONS="$OPTIONS -DASSIMP_ASAN=OFF"
     fi
 
+    if [ "$UBSAN" = "ON" ] ; then
+        OPTIONS="$OPTIONS -DASSIMP_UBSAN=ON"
+    fi
+
     cmake -G "Unix Makefiles" $OPTIONS
 }
 

+ 3 - 0
.travis.yml

@@ -46,6 +46,9 @@ matrix:
     - os: linux
       compiler: clang
       env: ASAN=ON
+    - os: linux
+      compiler: clang
+      env: UBSAN=ON
     - os: linux
       compiler: clang
       env: SHARED_BUILD=ON

+ 10 - 0
CMakeLists.txt

@@ -86,6 +86,10 @@ OPTION ( ASSIMP_ASAN
   "Enable AddressSanitizer."
   OFF
 )
+OPTION ( ASSIMP_UBSAN
+  "Enable Undefined Behavior sanitizer."
+  OFF
+)
 OPTION ( SYSTEM_IRRXML
   "Use system installed Irrlicht/IrrXML library."
   OFF
@@ -234,6 +238,12 @@ if (ASSIMP_ASAN)
     SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
 endif()
 
+if (ASSIMP_UBSAN)
+    MESSAGE(STATUS "Undefined Behavior sanitizer enabled")
+    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fno-sanitize-recover=all")
+    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined -fno-sanitize-recover=all")
+endif()
+
 INCLUDE (FindPkgMacros)
 INCLUDE (PrecompiledHeader)
 

+ 4 - 2
code/B3DImporter.cpp

@@ -171,7 +171,8 @@ int B3DImporter::ReadByte(){
 // ------------------------------------------------------------------------------------------------
 int B3DImporter::ReadInt(){
     if( _pos+4<=_buf.size() ){
-        int n=*(int*)&_buf[_pos];
+        int n;
+        memcpy(&n, &_buf[_pos], 4);
         _pos+=4;
         return n;
     }
@@ -182,7 +183,8 @@ int B3DImporter::ReadInt(){
 // ------------------------------------------------------------------------------------------------
 float B3DImporter::ReadFloat(){
     if( _pos+4<=_buf.size() ){
-        float n=*(float*)&_buf[_pos];
+        float n;
+        memcpy(&n, &_buf[_pos], 4);
         _pos+=4;
         return n;
     }

+ 2 - 1
code/FBXBinaryTokenizer.cpp

@@ -151,7 +151,8 @@ uint32_t ReadWord(const char* input, const char*& cursor, const char* end)
         TokenizeError("cannot ReadWord, out of bounds",input, cursor);
     }
 
-    uint32_t word = *reinterpret_cast<const uint32_t*>(cursor);
+    uint32_t word;
+    memcpy(&word, cursor, 4);
     AI_SWAP4(word);
 
     cursor += k_to_read;

+ 1 - 1
code/IFCBoolean.cpp

@@ -272,7 +272,6 @@ bool IntersectsBoundaryProfile(const IfcVector3& e0, const IfcVector3& e1, const
         const IfcVector3& b0 = boundary[i];
         const IfcVector3& b1 = boundary[(i + 1) % bcount];
         IfcVector3 b = b1 - b0;
-        IfcFloat b_sqlen_inv = 1.0 / b.SquareLength();
 
         // segment-segment intersection
         // solve b0 + b*s = e0 + e*t for (s,t)
@@ -281,6 +280,7 @@ bool IntersectsBoundaryProfile(const IfcVector3& e0, const IfcVector3& e1, const
             // no solutions (parallel lines)
             continue;
         }
+        IfcFloat b_sqlen_inv = 1.0 / b.SquareLength();
 
         const IfcFloat x = b0.x - e0.x;
         const IfcFloat y = b0.y - e0.y;