Commit e542f83f authored by Davide Galassi's avatar Davide Galassi

Merge branch 'malloc_mock' into 'master'

Malloc mock

Closes #26

See merge request !21
parents 7d23544e 18aa5546
Pipeline #59967748 passed with stages
in 2 minutes and 20 seconds
......@@ -24,9 +24,6 @@ int cry_mpi_mul_baseline(cry_mpi *r, const cry_mpi *a, const cry_mpi *b)
pa = a->used;
/* iterate through every digit in the first operand */
for (i = 0; i < pa; i++) {
/* limit ourselves to making digs digits of output */
if (digs < i)
break;
pb = CRY_MIN(b->used, digs - i);
/* copy of the digit to be used within the nested loop */
tmpx = a->data[i];
......
......@@ -4,11 +4,12 @@ CC := gcc
CFLAGS := -O0 -g3 -Wall
CPPFLAGS = -I. -I../include -I../src
LDFLAGS := --coverage
LDFLAGS := --coverage -Wl,--wrap=malloc,--wrap=realloc,--wrap=free
objs := \
test.o \
malloc_mock.o \
version_test.o \
memxor_test.o \
base64_test.o \
......
#include "malloc_mock.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int g_malloc_mock_count;
int g_malloc_mock_state = MALLOC_MOCK_STOPPED;
#define MAGIC 0xABADCAFE
void *__real_malloc(size_t size);
void *__real_realloc(void *ptr, size_t size);
void __real_free(void *ptr);
static int mock_update(void)
{
if (g_malloc_mock_state == MALLOC_MOCK_ACTIVE) {
if (g_malloc_mock_count == 0) {
g_malloc_mock_state = MALLOC_MOCK_FAILED;
}
g_malloc_mock_count--;
}
return g_malloc_mock_state;
}
struct mem_head {
unsigned long magic;
size_t size;
};
#define HEAD_SIZE sizeof(struct mem_head)
void *__wrap_malloc(size_t size)
{
struct mem_head *head;
if (mock_update() == MALLOC_MOCK_FAILED)
return NULL;
head = __real_malloc(HEAD_SIZE + size);
if (head == NULL)
return NULL;
head->magic = MAGIC;
head->size = size;
memset(&head[1], 0xFF, size);
return &head[1];
}
void *__wrap_realloc(void *ptr, size_t size)
{
struct mem_head *head;
if (ptr == NULL)
return __wrap_malloc(size);
if (mock_update() == MALLOC_MOCK_FAILED)
return NULL;
head = ((struct mem_head *)ptr) - 1;
if (head->magic != MAGIC) {
printf(">>> Warning: realloc of a not alloced buffer\n");
return NULL;
}
head = __real_realloc(head, (size != 0) ? size + HEAD_SIZE : 0);
if (size == 0)
return NULL;
if (size > head->size)
memset((unsigned char *)&head[1] + head->size, 0xFF, size - head->size);
else
memset((unsigned char *)&head[1] + size, 0xFF, head->size - size);
head->size = size;
return &head[1];
}
void __wrap_free(void *ptr)
{
struct mem_head *head;
head = ((struct mem_head *)ptr) - 1;
if (head->magic != MAGIC) {
printf(">>> Warning: free of a not alloced buffer\n");
return;
}
memset(head, 0xFF, HEAD_SIZE + head->size);
__real_free(head);
}
#ifndef MALLOC_MOCK_H_
#define MALLOC_MOCK_H_
#define MALLOC_MOCK_STOPPED 0
#define MALLOC_MOCK_READY 1
#define MALLOC_MOCK_ACTIVE 2
#define MALLOC_MOCK_FAILED 3
extern int g_malloc_mock_count;
extern int g_malloc_mock_state;
#endif /* MALLOC_MOCK_H_ */
#include "test.h"
#include "malloc_mock.h"
#include <cry/mpi.h>
#include <stdlib.h>
......@@ -154,6 +155,17 @@ static void mpi_unary_op(int argc, char *argv[], unary_op_f op)
}
#define RUNM(test) do { \
if (g_malloc_mock_state == MALLOC_MOCK_READY) \
g_malloc_mock_state = MALLOC_MOCK_ACTIVE; \
test; \
if (g_malloc_mock_state == MALLOC_MOCK_FAILED) { \
ASSERT(res == -1); \
return; \
} \
} while (0)
typedef int (* binary_op_f)(cry_mpi *r, const cry_mpi *a, const cry_mpi *b);
static void mpi_binary_op(int argc, char *argv[], binary_op_f op)
......@@ -163,7 +175,7 @@ static void mpi_binary_op(int argc, char *argv[], binary_op_f op)
ASSERT_EQ(argc, 3);
ASSERT_OK(mpi_load_args(argc, argv));
res = op(g_mpi_r1, g_mpi0, g_mpi1);
RUNM(res = op(g_mpi_r1, g_mpi0, g_mpi1));
ASSERT_EQ(res, g_exp_res);
if (res == 0)
......@@ -181,7 +193,7 @@ static void mpi_binary_mod_op(int argc, char *argv[], binary_mod_op_f op)
ASSERT_EQ(argc, 4);
ASSERT_OK(mpi_load_args(argc, argv));
res = op(g_mpi_r1, g_mpi0, g_mpi1, g_mpi2);
RUNM(res = op(g_mpi_r1, g_mpi0, g_mpi1, g_mpi2));
ASSERT_EQ(res, g_exp_res);
if (res == 0)
......@@ -196,7 +208,7 @@ static void mpi_div(int argc, char *argv[])
ASSERT_EQ(argc, 4);
ASSERT(mpi_load_args(argc, argv) == 0);
res = cry_mpi_div(g_mpi_r1, g_mpi_r2, g_mpi0, g_mpi1);
RUNM(res = cry_mpi_div(g_mpi_r1, g_mpi_r2, g_mpi0, g_mpi1));
ASSERT(res == g_exp_res);
if (res == 0) {
......@@ -260,9 +272,20 @@ static void mpi_dispatch(int argc, char *argv[])
mpi_teardown();
}
static struct malloc_fail_args g_malloc_fail_tests[] = {
{ 5, { "Multiply Baseline", "mpi_mul_baseline", "12345678", "12345678", "14b66dc1df4d840" }},
{ 5, { "Multiply Comba", "mpi_mul_comba", "12345678", "12345678", "14b66dc1df4d840" }},
{ 5, { "Multiply Karatsuba", "mpi_mul_karatsuba", "FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFF", "fffffffffffffffe0000000000000001" }},
{ 5, { "Multiply Toom-3", "mpi_mul_toom3", "FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFF", "fffffffffffffffe0000000000000001" }},
};
void mpi_test(void)
{
printf("* MPI\n");
func_test("mpi_test.data", mpi_dispatch);
malloc_fail_tests(g_malloc_fail_tests,
ARLEN(g_malloc_fail_tests), mpi_dispatch);
printf("\n");
}
#include "test.h"
#include "malloc_mock.h"
void version_test(void);
void memxor_test(void);
......@@ -151,6 +152,27 @@ void run(const char *name, void (* test)(void),
teardown();
}
void malloc_fail_tests(struct malloc_fail_args *args, size_t num,
dispatch_func_t dispatch)
{
size_t i, fail_after;
if (g_verbose != 0)
printf(" Malloc fails\n");
for (i = 0; i < num; i++) {
if (g_verbose != 0)
printf(" %s \n", args[i].argv[0]);
fail_after = 0;
do {
g_malloc_mock_state = MALLOC_MOCK_READY;
g_malloc_mock_count = fail_after++;
dispatch(args[i].argc - 1, args[i].argv + 1);
} while (g_malloc_mock_state == MALLOC_MOCK_FAILED);
if (g_verbose != 0)
printf(" fail-counter: %u\n", (unsigned)fail_after);
}
g_malloc_mock_state = MALLOC_MOCK_STOPPED;
}
void asc_to_raw(const char *asc, size_t size, unsigned char *raw)
{
......
......@@ -18,11 +18,20 @@ struct test_case {
test_func_t func;
};
typedef void (* dispatch_func_t)(int argc, char *argv[]);
typedef void (* dispatch_func_t)(int argc, char *argv[]);
void func_test(const char *datafile, dispatch_func_t dispatch);
struct malloc_fail_args {
int argc;
char *argv[10];
};
void malloc_fail_tests(struct malloc_fail_args *args, size_t num,
dispatch_func_t dispatch);
void asc_to_raw(const char *asc, size_t size, unsigned char *raw);
int raw_init(unsigned char *raw, size_t rawlen, const char *asc);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment