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.
This commit is contained in:
Matthew Barr 2016-07-27 16:55:28 +10:00
parent cded5552c2
commit b8d33732b5
3 changed files with 71 additions and 4 deletions

View 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;
}

View 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

View 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);
}