Changed encoder to use longjmp() for error recovery.

pull/13171/head
Joshua Haberman 5 years ago
parent 6e140c267c
commit ada28896b9
  1. 175
      upb/encode.c

@ -2,6 +2,7 @@
#include "upb/encode.h"
#include <setjmp.h>
#include <string.h>
#include "upb/msg.h"
@ -10,7 +11,6 @@
#include "upb/port_def.inc"
#define UPB_PB_VARINT_MAX_LEN 10
#define CHK(x) do { if (!(x)) { return false; } } while(0)
static size_t upb_encode_varint(uint64_t val, char *buf) {
size_t i;
@ -29,6 +29,7 @@ static uint32_t upb_zzencode_32(int32_t n) { return ((uint32_t)n << 1) ^ (n >> 3
static uint64_t upb_zzencode_64(int64_t n) { return ((uint64_t)n << 1) ^ (n >> 63); }
typedef struct {
jmp_buf err;
upb_alloc *alloc;
char *buf, *ptr, *limit;
} upb_encstate;
@ -41,11 +42,14 @@ static size_t upb_roundup_pow2(size_t bytes) {
return ret;
}
static bool upb_encode_growbuffer(upb_encstate *e, size_t bytes) {
UPB_NORETURN static void encode_err(upb_encstate *e) { longjmp(e->err, 1); }
static void upb_encode_growbuffer(upb_encstate *e, size_t bytes) {
size_t old_size = e->limit - e->buf;
size_t new_size = upb_roundup_pow2(bytes + (e->limit - e->ptr));
char *new_buf = upb_realloc(e->alloc, e->buf, old_size, new_size);
CHK(new_buf);
if (!new_buf) encode_err(e);
/* We want previous data at the end, realloc() put it at the beginning. */
if (old_size > 0) {
@ -55,87 +59,85 @@ static bool upb_encode_growbuffer(upb_encstate *e, size_t bytes) {
e->ptr = new_buf + new_size - (e->limit - e->ptr);
e->limit = new_buf + new_size;
e->buf = new_buf;
return true;
}
/* Call to ensure that at least "bytes" bytes are available for writing at
* e->ptr. Returns false if the bytes could not be allocated. */
static bool upb_encode_reserve(upb_encstate *e, size_t bytes) {
CHK(UPB_LIKELY((size_t)(e->ptr - e->buf) >= bytes) ||
upb_encode_growbuffer(e, bytes));
static void upb_encode_reserve(upb_encstate *e, size_t bytes) {
if ((size_t)(e->ptr - e->buf) < bytes) {
upb_encode_growbuffer(e, bytes);
}
e->ptr -= bytes;
return true;
}
/* Writes the given bytes to the buffer, handling reserve/advance. */
static bool upb_put_bytes(upb_encstate *e, const void *data, size_t len) {
if (len == 0) return true;
CHK(upb_encode_reserve(e, len));
static void upb_put_bytes(upb_encstate *e, const void *data, size_t len) {
if (len == 0) return; /* memcpy() with zero size is UB */
upb_encode_reserve(e, len);
memcpy(e->ptr, data, len);
return true;
}
static bool upb_put_fixed64(upb_encstate *e, uint64_t val) {
static void upb_put_fixed64(upb_encstate *e, uint64_t val) {
val = _upb_be_swap64(val);
return upb_put_bytes(e, &val, sizeof(uint64_t));
upb_put_bytes(e, &val, sizeof(uint64_t));
}
static bool upb_put_fixed32(upb_encstate *e, uint32_t val) {
static void upb_put_fixed32(upb_encstate *e, uint32_t val) {
val = _upb_be_swap32(val);
return upb_put_bytes(e, &val, sizeof(uint32_t));
upb_put_bytes(e, &val, sizeof(uint32_t));
}
static bool upb_put_varint(upb_encstate *e, uint64_t val) {
static void upb_put_varint(upb_encstate *e, uint64_t val) {
size_t len;
char *start;
CHK(upb_encode_reserve(e, UPB_PB_VARINT_MAX_LEN));
upb_encode_reserve(e, UPB_PB_VARINT_MAX_LEN);
len = upb_encode_varint(val, e->ptr);
start = e->ptr + UPB_PB_VARINT_MAX_LEN - len;
memmove(start, e->ptr, len);
e->ptr = start;
return true;
}
static bool upb_put_double(upb_encstate *e, double d) {
static void upb_put_double(upb_encstate *e, double d) {
uint64_t u64;
UPB_ASSERT(sizeof(double) == sizeof(uint64_t));
memcpy(&u64, &d, sizeof(uint64_t));
return upb_put_fixed64(e, u64);
upb_put_fixed64(e, u64);
}
static bool upb_put_float(upb_encstate *e, float d) {
static void upb_put_float(upb_encstate *e, float d) {
uint32_t u32;
UPB_ASSERT(sizeof(float) == sizeof(uint32_t));
memcpy(&u32, &d, sizeof(uint32_t));
return upb_put_fixed32(e, u32);
upb_put_fixed32(e, u32);
}
static bool upb_put_tag(upb_encstate *e, int field_number, int wire_type) {
return upb_put_varint(e, (field_number << 3) | wire_type);
static void upb_put_tag(upb_encstate *e, int field_number, int wire_type) {
upb_put_varint(e, (field_number << 3) | wire_type);
}
static bool upb_put_fixedarray(upb_encstate *e, const upb_array *arr,
static void upb_put_fixedarray(upb_encstate *e, const upb_array *arr,
size_t elem_size, uint32_t tag) {
size_t bytes = arr->len * elem_size;
const char* data = _upb_array_constptr(arr);
const char* ptr = data + bytes - elem_size;
if (tag) {
while (true) {
CHK(upb_put_bytes(e, ptr, elem_size) && upb_put_varint(e, tag));
upb_put_bytes(e, ptr, elem_size);
upb_put_varint(e, tag);
if (ptr == data) break;
ptr -= elem_size;
}
return true;
} else {
return upb_put_bytes(e, data, bytes) && upb_put_varint(e, bytes);
upb_put_bytes(e, data, bytes);
upb_put_varint(e, bytes);
}
}
bool upb_encode_message(upb_encstate *e, const char *msg,
const upb_msglayout *m, size_t *size);
static void upb_encode_message(upb_encstate *e, const char *msg,
const upb_msglayout *m, size_t *size);
static bool upb_encode_scalarfield(upb_encstate *e, const void *_field_mem,
static void upb_encode_scalarfield(upb_encstate *e, const void *_field_mem,
const upb_msglayout *m,
const upb_msglayout_field *f,
bool skip_zero_value) {
@ -143,10 +145,11 @@ static bool upb_encode_scalarfield(upb_encstate *e, const void *_field_mem,
#define CASE(ctype, type, wire_type, encodeval) do { \
ctype val = *(ctype*)field_mem; \
if (skip_zero_value && val == 0) { \
return true; \
return; \
} \
return upb_put_ ## type(e, encodeval) && \
upb_put_tag(e, f->number, wire_type); \
upb_put_ ## type(e, encodeval); \
upb_put_tag(e, f->number, wire_type); \
return; \
} while(0)
switch (f->descriptortype) {
@ -178,47 +181,50 @@ static bool upb_encode_scalarfield(upb_encstate *e, const void *_field_mem,
case UPB_DESCRIPTOR_TYPE_BYTES: {
upb_strview view = *(upb_strview*)field_mem;
if (skip_zero_value && view.size == 0) {
return true;
return;
}
return upb_put_bytes(e, view.data, view.size) &&
upb_put_varint(e, view.size) &&
upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
upb_put_bytes(e, view.data, view.size);
upb_put_varint(e, view.size);
upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
return;
}
case UPB_DESCRIPTOR_TYPE_GROUP: {
size_t size;
void *submsg = *(void **)field_mem;
const upb_msglayout *subm = m->submsgs[f->submsg_index];
if (submsg == NULL) {
return true;
return;
}
return upb_put_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP) &&
upb_encode_message(e, submsg, subm, &size) &&
upb_put_tag(e, f->number, UPB_WIRE_TYPE_START_GROUP);
upb_put_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP);
upb_encode_message(e, submsg, subm, &size);
upb_put_tag(e, f->number, UPB_WIRE_TYPE_START_GROUP);
return;
}
case UPB_DESCRIPTOR_TYPE_MESSAGE: {
size_t size;
void *submsg = *(void **)field_mem;
const upb_msglayout *subm = m->submsgs[f->submsg_index];
if (submsg == NULL) {
return true;
return;
}
return upb_encode_message(e, submsg, subm, &size) &&
upb_put_varint(e, size) &&
upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
upb_encode_message(e, submsg, subm, &size);
upb_put_varint(e, size);
upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
return;
}
}
#undef CASE
UPB_UNREACHABLE();
}
static bool upb_encode_array(upb_encstate *e, const char *field_mem,
static void upb_encode_array(upb_encstate *e, const char *field_mem,
const upb_msglayout *m,
const upb_msglayout_field *f) {
const upb_array *arr = *(const upb_array**)field_mem;
bool packed = f->label == _UPB_LABEL_PACKED;
if (arr == NULL || arr->len == 0) {
return true;
return;
}
#define VARINT_CASE(ctype, encode) \
@ -229,10 +235,10 @@ static bool upb_encode_array(upb_encstate *e, const char *field_mem,
uint32_t tag = packed ? 0 : (f->number << 3) | UPB_WIRE_TYPE_VARINT; \
do { \
ptr--; \
CHK(upb_put_varint(e, encode)); \
if (tag) CHK(upb_put_varint(e, tag)); \
upb_put_varint(e, encode); \
if (tag) upb_put_varint(e, tag); \
} while (ptr != start); \
if (!tag) CHK(upb_put_varint(e, e->limit - e->ptr - pre_len)); \
if (!tag) upb_put_varint(e, e->limit - e->ptr - pre_len); \
} \
break; \
do { \
@ -243,18 +249,18 @@ static bool upb_encode_array(upb_encstate *e, const char *field_mem,
switch (f->descriptortype) {
case UPB_DESCRIPTOR_TYPE_DOUBLE:
CHK(upb_put_fixedarray(e, arr, sizeof(double), TAG(UPB_WIRE_TYPE_64BIT)));
upb_put_fixedarray(e, arr, sizeof(double), TAG(UPB_WIRE_TYPE_64BIT));
break;
case UPB_DESCRIPTOR_TYPE_FLOAT:
CHK(upb_put_fixedarray(e, arr, sizeof(float), TAG(UPB_WIRE_TYPE_32BIT)));
upb_put_fixedarray(e, arr, sizeof(float), TAG(UPB_WIRE_TYPE_32BIT));
break;
case UPB_DESCRIPTOR_TYPE_SFIXED64:
case UPB_DESCRIPTOR_TYPE_FIXED64:
CHK(upb_put_fixedarray(e, arr, sizeof(uint64_t), TAG(UPB_WIRE_TYPE_64BIT)));
upb_put_fixedarray(e, arr, sizeof(uint64_t), TAG(UPB_WIRE_TYPE_64BIT));
break;
case UPB_DESCRIPTOR_TYPE_FIXED32:
case UPB_DESCRIPTOR_TYPE_SFIXED32:
CHK(upb_put_fixedarray(e, arr, sizeof(uint32_t), TAG(UPB_WIRE_TYPE_32BIT)));
upb_put_fixedarray(e, arr, sizeof(uint32_t), TAG(UPB_WIRE_TYPE_32BIT));
break;
case UPB_DESCRIPTOR_TYPE_INT64:
case UPB_DESCRIPTOR_TYPE_UINT64:
@ -276,11 +282,11 @@ static bool upb_encode_array(upb_encstate *e, const char *field_mem,
const upb_strview *ptr = start + arr->len;
do {
ptr--;
CHK(upb_put_bytes(e, ptr->data, ptr->size) &&
upb_put_varint(e, ptr->size) &&
upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
upb_put_bytes(e, ptr->data, ptr->size);
upb_put_varint(e, ptr->size);
upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
} while (ptr != start);
return true;
return;
}
case UPB_DESCRIPTOR_TYPE_GROUP: {
const void *const*start = _upb_array_constptr(arr);
@ -289,11 +295,11 @@ static bool upb_encode_array(upb_encstate *e, const char *field_mem,
do {
size_t size;
ptr--;
CHK(upb_put_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP) &&
upb_encode_message(e, *ptr, subm, &size) &&
upb_put_tag(e, f->number, UPB_WIRE_TYPE_START_GROUP));
upb_put_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP);
upb_encode_message(e, *ptr, subm, &size);
upb_put_tag(e, f->number, UPB_WIRE_TYPE_START_GROUP);
} while (ptr != start);
return true;
return;
}
case UPB_DESCRIPTOR_TYPE_MESSAGE: {
const void *const*start = _upb_array_constptr(arr);
@ -302,22 +308,21 @@ static bool upb_encode_array(upb_encstate *e, const char *field_mem,
do {
size_t size;
ptr--;
CHK(upb_encode_message(e, *ptr, subm, &size) &&
upb_put_varint(e, size) &&
upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
upb_encode_message(e, *ptr, subm, &size);
upb_put_varint(e, size);
upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
} while (ptr != start);
return true;
return;
}
}
#undef VARINT_CASE
if (packed) {
CHK(upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
}
return true;
}
static bool upb_encode_map(upb_encstate *e, const char *field_mem,
static void upb_encode_map(upb_encstate *e, const char *field_mem,
const upb_msglayout *m,
const upb_msglayout_field *f) {
const upb_map *map = *(const upb_map**)field_mem;
@ -326,7 +331,7 @@ static bool upb_encode_map(upb_encstate *e, const char *field_mem,
const upb_msglayout_field *val_field = &entry->fields[1];
upb_strtable_iter i;
if (map == NULL) {
return true;
return;
}
upb_strtable_begin(&i, &map->table);
@ -338,18 +343,15 @@ static bool upb_encode_map(upb_encstate *e, const char *field_mem,
upb_map_entry ent;
_upb_map_fromkey(key, &ent.k, map->key_size);
_upb_map_fromvalue(val, &ent.v, map->val_size);
CHK(upb_encode_scalarfield(e, &ent.v, entry, val_field, false));
CHK(upb_encode_scalarfield(e, &ent.k, entry, key_field, false));
upb_encode_scalarfield(e, &ent.v, entry, val_field, false);
upb_encode_scalarfield(e, &ent.k, entry, key_field, false);
size = (e->limit - e->ptr) - pre_len;
CHK(upb_put_varint(e, size));
CHK(upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
upb_put_varint(e, size);
upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
}
return true;
}
bool upb_encode_message(upb_encstate *e, const char *msg,
static void upb_encode_message(upb_encstate *e, const char *msg,
const upb_msglayout *m, size_t *size) {
int i;
size_t pre_len = e->limit - e->ptr;
@ -366,9 +368,9 @@ bool upb_encode_message(upb_encstate *e, const char *msg,
const upb_msglayout_field *f = &m->fields[i];
if (_upb_isrepeated(f)) {
CHK(upb_encode_array(e, msg + f->offset, m, f));
upb_encode_array(e, msg + f->offset, m, f);
} else if (f->label == _UPB_LABEL_MAP) {
CHK(upb_encode_map(e, msg + f->offset, m, f));
upb_encode_map(e, msg + f->offset, m, f);
} else {
bool skip_empty = false;
if (f->presence == 0) {
@ -385,12 +387,11 @@ bool upb_encode_message(upb_encstate *e, const char *msg,
continue;
}
}
CHK(upb_encode_scalarfield(e, msg + f->offset, m, f, skip_empty));
upb_encode_scalarfield(e, msg + f->offset, m, f, skip_empty);
}
}
*size = (e->limit - e->ptr) - pre_len;
return true;
}
char *upb_encode(const void *msg, const upb_msglayout *m, upb_arena *arena,
@ -401,11 +402,13 @@ char *upb_encode(const void *msg, const upb_msglayout *m, upb_arena *arena,
e.limit = NULL;
e.ptr = NULL;
if (!upb_encode_message(&e, msg, m, size)) {
if (setjmp(e.err)) {
*size = 0;
return NULL;
}
upb_encode_message(&e, msg, m, size);
*size = e.limit - e.ptr;
if (*size == 0) {
@ -416,5 +419,3 @@ char *upb_encode(const void *msg, const upb_msglayout *m, upb_arena *arena,
return e.ptr;
}
}
#undef CHK

Loading…
Cancel
Save