Commit efccdd42 authored by Sven Gestegård Robertz's avatar Sven Gestegård Robertz
Browse files

added sanity check for lengths in encoder

parent 89cfad68
...@@ -29,6 +29,8 @@ ...@@ -29,6 +29,8 @@
//define the following to disable encoding of typedefs //define the following to disable encoding of typedefs
#undef LABCOMM_WITHOUT_TYPE_DEFS #undef LABCOMM_WITHOUT_TYPE_DEFS
#undef LABCOMM2014_WITH_SANITY_CHECKS
struct encoder { struct encoder {
struct labcomm2014_encoder encoder; struct labcomm2014_encoder encoder;
LABCOMM_SIGNATURE_ARRAY_DEF(registered, int); LABCOMM_SIGNATURE_ARRAY_DEF(registered, int);
...@@ -36,10 +38,47 @@ struct encoder { ...@@ -36,10 +38,47 @@ struct encoder {
LABCOMM_SIGNATURE_ARRAY_DEF(typedefs, int); LABCOMM_SIGNATURE_ARRAY_DEF(typedefs, int);
}; };
#ifdef LABCOMM2014_WITH_SANITY_CHECKS
static int expectedByteCount;
static void encoder_check_write_start(struct labcomm2014_encoder *e, int numBytes)
{
int previouslyWritten = 0;
int err = labcomm2014_encoder_ioctl(e, LABCOMM_IOCTL_WRITER_GET_BYTES_WRITTEN, &previouslyWritten);
if(err) {
printf("ERROR: get_bytes_written returned %d (%s)\n", err, strerror(err));
}
expectedByteCount = numBytes + previouslyWritten;
#ifdef LABCOMM2014_ENCODER_DEBUG
printf("previously written: %d bytes, length = %d bytes, expected = %d bytes\n",
previouslyWritten, numBytes, expectedByteCount);
#endif
}
static int encoder_check_write_end(struct labcomm2014_encoder *e)
{
int written = 0;
int err = labcomm2014_encoder_ioctl(e, LABCOMM_IOCTL_WRITER_GET_BYTES_WRITTEN, &written);
if(err) {
printf("ERROR: get_bytes_written returned %d (%s)\n", err, strerror(err));
}
int result = 0;
#ifdef LABCOMM2014_ENCODER_DEBUG
printf("DEBUG: encoder_check_write_end: expected: %d, was: %d\n",
expectedByteCount, written);
#endif
if(written != expectedByteCount) {
printf("WARNING! encoder_check_write_end: expected: %d, was: %d\n",
expectedByteCount, written);
result = -EINVAL;
}
return result;
}
#endif
/* XXX: TEMPORARY PLACEHOLDERS FOR INTENTIONS */ /* XXX: TEMPORARY PLACEHOLDERS FOR INTENTIONS */
static int TODO_sizeof_intentions(const struct labcomm2014_signature *signature) { static int TODO_sizeof_intentions(const struct labcomm2014_signature *signature) {
return labcomm2014_size_string(signature->name) + 2; int res = labcomm2014_size_string(signature->name) + 2;
return res;
} }
static int TODO_encode_intentions( static int TODO_encode_intentions(
...@@ -72,7 +111,7 @@ static int do_sample_register( ...@@ -72,7 +111,7 @@ static int do_sample_register(
goto out; } goto out; }
*done = 1; *done = 1;
err = labcomm2014_writer_start(e->writer, e->writer->action_context, err = labcomm2014_writer_start(e->writer, e->writer->action_context,
index, signature, NULL); index, signature, NULL);
if (err == -EALREADY) { result = 0; goto out; } if (err == -EALREADY) { result = 0; goto out; }
if (err != 0) { result = err; goto out; } if (err != 0) { result = err; goto out; }
labcomm2014_write_packed32(e->writer, LABCOMM_SAMPLE_DEF); labcomm2014_write_packed32(e->writer, LABCOMM_SAMPLE_DEF);
...@@ -106,7 +145,7 @@ static int do_encode( ...@@ -106,7 +145,7 @@ static int do_encode(
{ {
int result, index, length; int result, index, length;
struct encoder *ie = e->context; struct encoder *ie = e->context;
index = labcomm2014_get_local_index(signature); index = labcomm2014_get_local_index(signature);
length = (signature->encoded_size(value)); length = (signature->encoded_size(value));
labcomm2014_scheduler_writer_lock(e->scheduler); labcomm2014_scheduler_writer_lock(e->scheduler);
...@@ -152,7 +191,7 @@ static int do_ref_register( ...@@ -152,7 +191,7 @@ static int do_ref_register(
if (*done) { goto out; } if (*done) { goto out; }
*done = 1; *done = 1;
err = labcomm2014_writer_start(e->writer, e->writer->action_context, err = labcomm2014_writer_start(e->writer, e->writer->action_context,
index, signature, NULL); index, signature, NULL);
if (err == -EALREADY) { result = 0; goto out; } if (err == -EALREADY) { result = 0; goto out; }
if (err != 0) { result = err; goto out; } if (err != 0) { result = err; goto out; }
labcomm2014_write_packed32(e->writer, LABCOMM_SAMPLE_REF); labcomm2014_write_packed32(e->writer, LABCOMM_SAMPLE_REF);
...@@ -208,8 +247,8 @@ static int do_ioctl( ...@@ -208,8 +247,8 @@ static int do_ioctl(
index = labcomm2014_get_local_index(signature); index = labcomm2014_get_local_index(signature);
result = labcomm2014_writer_ioctl(encoder->writer, result = labcomm2014_writer_ioctl(encoder->writer,
encoder->writer->action_context, encoder->writer->action_context,
index, signature, action, va); index, signature, action, va);
return result; return result;
} }
...@@ -252,8 +291,8 @@ static void write_sig_tree_byte(char b, const struct labcomm2014_signature *sign ...@@ -252,8 +291,8 @@ static void write_sig_tree_byte(char b, const struct labcomm2014_signature *sign
} }
} }
static void do_write_signature(struct labcomm2014_encoder * e, static void do_write_signature(struct labcomm2014_encoder * e,
const struct labcomm2014_signature *signature, const struct labcomm2014_signature *signature,
unsigned char flatten) unsigned char flatten)
{ {
map_signature(write_sig_tree_byte, e, signature, flatten); map_signature(write_sig_tree_byte, e, signature, flatten);
...@@ -289,7 +328,7 @@ static int internal_reg_type( ...@@ -289,7 +328,7 @@ static int internal_reg_type(
int result = -EINVAL; int result = -EINVAL;
int index, *done, err; int index, *done, err;
struct encoder *ie = e->context; struct encoder *ie = e->context;
index = labcomm2014_get_local_index(signature); index = labcomm2014_get_local_index(signature);
labcomm2014_scheduler_writer_lock(e->scheduler); labcomm2014_scheduler_writer_lock(e->scheduler);
if (index <= 0) { goto out; } if (index <= 0) { goto out; }
...@@ -301,19 +340,30 @@ static int internal_reg_type( ...@@ -301,19 +340,30 @@ static int internal_reg_type(
if (err == -EALREADY) { result = 0; goto out; } if (err == -EALREADY) { result = 0; goto out; }
if (err != 0) { result = err; goto out; } if (err != 0) { result = err; goto out; }
printf("internal_reg_type: %s\n", signature->name);
int sig_size = calc_sig_encoded_size(e, signature); int sig_size = calc_sig_encoded_size(e, signature);
int len = (labcomm2014_size_packed32(index) + int len1= labcomm2014_size_packed32(index);
TODO_sizeof_intentions(signature) + int len2 =TODO_sizeof_intentions(signature);
labcomm2014_size_packed32(signature->size) + int len3 =labcomm2014_size_packed32(signature->size);
sig_size); int len4 =sig_size;
printf("len (index) : %d. (intentions) : %d, (sig_size): %d, (sig): %d)\n",
len1, len2, len3, len4);
int len = len1 + len2 + len3 + len4;
labcomm2014_write_packed32(e->writer, LABCOMM_TYPE_DEF); labcomm2014_write_packed32(e->writer, LABCOMM_TYPE_DEF);
labcomm2014_write_packed32(e->writer, len); labcomm2014_write_packed32(e->writer, len);
#ifdef LABCOMM2014_WITH_SANITY_CHECKS
encoder_check_write_start(e, len);
#endif
labcomm2014_write_packed32(e->writer, index); labcomm2014_write_packed32(e->writer, index);
TODO_encode_intentions(e, signature); TODO_encode_intentions(e, signature);
labcomm2014_write_packed32(e->writer, sig_size); labcomm2014_write_packed32(e->writer, sig_size);
do_write_signature(e, signature, LABCOMM2014_FALSE); do_write_signature(e, signature, LABCOMM2014_FALSE);
#ifdef LABCOMM2014_WITH_SANITY_CHECKS
encoder_check_write_end(e);
#endif
labcomm2014_writer_end(e->writer, e->writer->action_context); labcomm2014_writer_end(e->writer, e->writer->action_context);
result = e->writer->error; result = e->writer->error;
out: out:
...@@ -346,7 +396,7 @@ static int do_type_bind( ...@@ -346,7 +396,7 @@ static int do_type_bind(
labcomm2014_scheduler_writer_lock(e->scheduler); labcomm2014_scheduler_writer_lock(e->scheduler);
if(sindex <= 0 || (has_deps && tindex <= 0)) {goto out;} if(sindex <= 0 || (has_deps && tindex <= 0)) {goto out;}
err = labcomm2014_writer_start(e->writer, e->writer->action_context, err = labcomm2014_writer_start(e->writer, e->writer->action_context,
LABCOMM_TYPE_BINDING, signature, NULL); LABCOMM_TYPE_BINDING, signature, NULL);
if (err == -EALREADY) { result = 0; goto out; } if (err == -EALREADY) { result = 0; goto out; }
if (err != 0) { result = err; goto out; } if (err != 0) { result = err; goto out; }
int length = (labcomm2014_size_packed32(sindex) + int length = (labcomm2014_size_packed32(sindex) +
......
...@@ -26,9 +26,14 @@ ...@@ -26,9 +26,14 @@
#include <stdarg.h> #include <stdarg.h>
#include "labcomm2014_private.h" #include "labcomm2014_private.h"
#include "labcomm2014_fd_writer.h" #include "labcomm2014_fd_writer.h"
#include "labcomm2014_ioctl.h"
#define BUFFER_SIZE 2048 #define BUFFER_SIZE 2048
#ifdef LABCOMM_WITH_SANITY_CHECKS
static int bytecount_carry = 0;
#endif
struct labcomm2014_fd_writer { struct labcomm2014_fd_writer {
struct labcomm2014_writer writer; struct labcomm2014_writer writer;
struct labcomm2014_writer_action_context action_context; struct labcomm2014_writer_action_context action_context;
...@@ -36,10 +41,10 @@ struct labcomm2014_fd_writer { ...@@ -36,10 +41,10 @@ struct labcomm2014_fd_writer {
int close_fd_on_free; int close_fd_on_free;
}; };
static int fd_flush(struct labcomm2014_writer *w, static int fd_flush(struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context); struct labcomm2014_writer_action_context *action_context);
static int fd_alloc(struct labcomm2014_writer *w, static int fd_alloc(struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context) struct labcomm2014_writer_action_context *action_context)
{ {
w->data = labcomm2014_memory_alloc(w->memory, 0, BUFFER_SIZE); w->data = labcomm2014_memory_alloc(w->memory, 0, BUFFER_SIZE);
...@@ -57,7 +62,7 @@ static int fd_alloc(struct labcomm2014_writer *w, ...@@ -57,7 +62,7 @@ static int fd_alloc(struct labcomm2014_writer *w,
return w->error; return w->error;
} }
static int fd_free(struct labcomm2014_writer *w, static int fd_free(struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context) struct labcomm2014_writer_action_context *action_context)
{ {
struct labcomm2014_fd_writer *fd_writer = action_context->context; struct labcomm2014_fd_writer *fd_writer = action_context->context;
...@@ -76,23 +81,26 @@ static int fd_free(struct labcomm2014_writer *w, ...@@ -76,23 +81,26 @@ static int fd_free(struct labcomm2014_writer *w,
return 0; return 0;
} }
static int fd_start(struct labcomm2014_writer *w, static int fd_start(struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context, struct labcomm2014_writer_action_context *action_context,
int index, int index,
const struct labcomm2014_signature *signature, const struct labcomm2014_signature *signature,
void *value) void *value)
{ {
#ifdef LABCOMM_WITH_SANITY_CHECKS
bytecount_carry = w->pos;
#endif
w->pos = 0; w->pos = 0;
return w->error; return w->error;
} }
static int fd_flush(struct labcomm2014_writer *w, static int fd_flush(struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context) struct labcomm2014_writer_action_context *action_context)
{ {
struct labcomm2014_fd_writer *fd_context = action_context->context; struct labcomm2014_fd_writer *fd_context = action_context->context;
int start, err; int start, err;
start = 0; start = 0;
err = 0; err = 0;
while (start < w->pos) { while (start < w->pos) {
...@@ -107,18 +115,45 @@ static int fd_flush(struct labcomm2014_writer *w, ...@@ -107,18 +115,45 @@ static int fd_flush(struct labcomm2014_writer *w,
} else if (err == 0) { } else if (err == 0) {
w->error = -EINVAL; w->error = -EINVAL;
} }
#ifdef LABCOMM_WITH_SANITY_CHECKS
bytecount_carry = w->pos;
#endif
w->pos = 0; w->pos = 0;
return w->error; return w->error;
} }
#ifdef LABCOMM_WITH_SANITY_CHECKS
static int fd_ioctl(struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context,
int signature_index,
const struct labcomm2014_signature *signature,
uint32_t action, va_list arg)
{
int result = -ENOTSUP;
switch (action) {
case LABCOMM_IOCTL_WRITER_GET_BYTES_WRITTEN: {
int *value = va_arg(arg, int*);
*value = w->pos + bytecount_carry;
result = 0;
} break;
}
return result;
}
#endif
static const struct labcomm2014_writer_action action = { static const struct labcomm2014_writer_action action = {
.alloc = fd_alloc, .alloc = fd_alloc,
.free = fd_free, .free = fd_free,
.start = fd_start, .start = fd_start,
.end = fd_flush, .end = fd_flush,
.flush = fd_flush, .flush = fd_flush,
#ifdef LABCOMM_WITH_SANITY_CHECKS
.ioctl = fd_ioctl
#else
.ioctl = NULL .ioctl = NULL
#endif
}; };
struct labcomm2014_writer *labcomm2014_fd_writer_new(struct labcomm2014_memory *memory, struct labcomm2014_writer *labcomm2014_fd_writer_new(struct labcomm2014_memory *memory,
......
Supports Markdown
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