]> git.ipfire.org Git - thirdparty/vectorscan.git/commitdiff
Check for misaligned memory in compile error code
authorMatthew Barr <matthew.barr@intel.com>
Wed, 27 Jul 2016 06:55:28 +0000 (16:55 +1000)
committerMatthew Barr <matthew.barr@intel.com>
Wed, 10 Aug 2016 05:10:09 +0000 (15:10 +1000)
We now check that mem alloc for error message is aligned, and
fail with an appropriate message in the compile error.

src/compiler/error.cpp
src/hs_compile.h
unit/hyperscan/allocators.cpp

index e806b7a0a1ef37076d04eb664f9b9547118a9707..07db98192dc88672fcb43d355beb8e74b9c91a57 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Intel Corporation
+ * Copyright (c) 2015-2016, Intel Corporation
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
@@ -42,6 +42,7 @@ using std::string;
 
 static const char failureNoMemory[] = "Unable to allocate memory.";
 static const char failureInternal[] = "Internal error.";
+static const char failureBadAlloc[] = "Allocator returned misaligned memory.";
 
 extern const hs_compile_error_t hs_enomem = {
     const_cast<char *>(failureNoMemory), 0
@@ -49,6 +50,9 @@ extern const hs_compile_error_t hs_enomem = {
 extern const hs_compile_error_t hs_einternal = {
     const_cast<char *>(failureInternal), 0
 };
+extern const hs_compile_error_t hs_badalloc = {
+    const_cast<char *>(failureBadAlloc), 0
+};
 
 namespace ue2 {
 
@@ -56,8 +60,18 @@ hs_compile_error_t *generateCompileError(const string &err, int expression) {
     hs_compile_error_t *ret =
         (struct hs_compile_error *)hs_misc_alloc(sizeof(hs_compile_error_t));
     if (ret) {
+        hs_error_t e = hs_check_alloc(ret);
+        if (e != HS_SUCCESS) {
+            hs_misc_free(ret);
+            return const_cast<hs_compile_error_t *>(&hs_badalloc);
+        }
         char *msg = (char *)hs_misc_alloc(err.size() + 1);
         if (msg) {
+            e = hs_check_alloc(msg);
+            if (e != HS_SUCCESS) {
+                hs_misc_free(msg);
+                return const_cast<hs_compile_error_t *>(&hs_badalloc);
+            }
             memcpy(msg, err.c_str(), err.size() + 1);
             ret->message = msg;
         } else {
@@ -83,7 +97,8 @@ void freeCompileError(hs_compile_error_t *error) {
     if (!error) {
         return;
     }
-    if (error == &hs_enomem || error == &hs_einternal) {
+    if (error == &hs_enomem || error == &hs_einternal ||
+        error == &hs_badalloc) {
         // These are not allocated.
         return;
     }
index 48168cc2a4097500eaa897d54265d101b701343c..c5212cbe1a28707ec8af063b69b5f9157b58ce45 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Intel Corporation
+ * Copyright (c) 2015-2016, Intel Corporation
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
@@ -98,6 +98,12 @@ extern "C"
  *      The library was unable to allocate temporary storage used during
  *      compilation time.
  *
+ *    - *Allocator returned misaligned memory*
+ *
+ *      The memory allocator (either malloc() or the allocator set with @ref
+ *      hs_set_allocator()) did not correctly return memory suitably aligned
+ *      for the largest representable data type on this platform.
+ *
  *    - *Internal error*
  *
  *      An unexpected error occurred: if this error is reported, please contact
index 66c456eed1032f8523895be46b9a86255a2cae52..40c450720b3a5058eb1a32c7295502c7f29aa13b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Intel Corporation
+ * Copyright (c) 2015-2016, Intel Corporation
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
@@ -33,6 +33,9 @@
 #include "test_util.h"
 
 #include <cstdlib>
+#include <string>
+
+using std::string;
 
 static void *null_malloc(size_t) { return nullptr; }
 
@@ -83,6 +86,22 @@ TEST(CustomAllocator, TwoAlignedCompile) {
     hs_set_database_allocator(nullptr, nullptr);
 }
 
+TEST(CustomAllocator, TwoAlignedCompileError) {
+    hs_set_misc_allocator(two_aligned_malloc, two_aligned_free);
+
+    hs_database_t *db = nullptr;
+    hs_compile_error_t *compile_err = nullptr;
+    const hs_platform_info_t *platform = nullptr;
+    hs_error_t err =
+        hs_compile("\\1", 0, HS_MODE_BLOCK, platform, &db, &compile_err);
+    ASSERT_EQ(HS_COMPILER_ERROR, err);
+    ASSERT_EQ(nullptr, db);
+    ASSERT_NE(nullptr, compile_err);
+    EXPECT_STREQ("Allocator returned misaligned memory.", compile_err->message);
+    hs_free_compile_error(compile_err);
+    hs_set_database_allocator(nullptr, nullptr);
+}
+
 TEST(CustomAllocator, TwoAlignedDatabaseInfo) {
     hs_database_t *db = buildDB("foobar", 0, 0, HS_MODE_BLOCK);
     ASSERT_TRUE(db != nullptr);
@@ -149,3 +168,30 @@ TEST(CustomAllocator, TwoAlignedAllocScratch) {
     hs_set_scratch_allocator(nullptr, nullptr);
     hs_free_database(db);
 }
+
+TEST(CustomAllocator, NullMallocExpressionInfo) {
+    hs_set_allocator(null_malloc, nullptr);
+
+    string pattern = "foobar";
+    hs_expr_info_t *info = nullptr;
+    hs_compile_error_t *c_err = nullptr;
+    hs_error_t err = hs_expression_info(pattern.c_str(), 0, &info, &c_err);
+    ASSERT_EQ(HS_COMPILER_ERROR, err);
+    ASSERT_NE(nullptr, c_err);
+    hs_free_compile_error(c_err);
+    hs_set_allocator(nullptr, nullptr);
+}
+
+TEST(CustomAllocator, TwoAlignedExpressionInfo) {
+    hs_set_misc_allocator(two_aligned_malloc, two_aligned_free);
+
+    string pattern = "\\1";
+    hs_expr_info_t *info = nullptr;
+    hs_compile_error_t *c_err = nullptr;
+    hs_error_t err = hs_expression_info(pattern.c_str(), 0, &info, &c_err);
+    ASSERT_EQ(HS_COMPILER_ERROR, err);
+    ASSERT_NE(nullptr, c_err);
+    EXPECT_STREQ("Allocator returned misaligned memory.", c_err->message);
+    hs_free_compile_error(c_err);
+    hs_set_allocator(nullptr, nullptr);
+}