From b8d33732b5248bdedaf7d37e5e7e75a0e48f6bf0 Mon Sep 17 00:00:00 2001 From: Matthew Barr Date: Wed, 27 Jul 2016 16:55:28 +1000 Subject: [PATCH] Check for misaligned memory in compile error code 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 | 19 ++++++++++++-- src/hs_compile.h | 8 +++++- unit/hyperscan/allocators.cpp | 48 ++++++++++++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 4 deletions(-) diff --git a/src/compiler/error.cpp b/src/compiler/error.cpp index e806b7a0..07db9819 100644 --- a/src/compiler/error.cpp +++ b/src/compiler/error.cpp @@ -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(failureNoMemory), 0 @@ -49,6 +50,9 @@ extern const hs_compile_error_t hs_enomem = { extern const hs_compile_error_t hs_einternal = { const_cast(failureInternal), 0 }; +extern const hs_compile_error_t hs_badalloc = { + const_cast(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_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_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; } diff --git a/src/hs_compile.h b/src/hs_compile.h index 48168cc2..c5212cbe 100644 --- a/src/hs_compile.h +++ b/src/hs_compile.h @@ -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 diff --git a/unit/hyperscan/allocators.cpp b/unit/hyperscan/allocators.cpp index 66c456ee..40c45072 100644 --- a/unit/hyperscan/allocators.cpp +++ b/unit/hyperscan/allocators.cpp @@ -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 +#include + +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); +}