summaryrefslogtreecommitdiffstats
path: root/src/marshal.c
blob: c6a25f7b4cdce8b4b7f8ff83a68d420996ed348d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/* -*- mode: c; c-file-style: "openbsd" -*- */
/*
 * Copyright (c) 2012 Vincent Bernat <bernat@luffy.cx>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#define MARSHAL_EXPORT
#include "marshal.h"

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/queue.h>
#include <string.h>

#include "compat/compat.h"
#include "log.h"

#include "lldpd-structs.h"

/* Stolen from CCAN */
#if HAVE_ALIGNOF
#  define ALIGNOF(t) (__alignof__(t))
#else
#  define ALIGNOF(t)               \
    ((sizeof(t) > 1) ?             \
	    ((char *)(&((struct {  \
  char c;                          \
  t _h;                            \
	    } *)0)                 \
			   ->_h) - \
		(char *)0) :       \
	    1)
#endif

/* A serialized object */
struct marshal_serialized {
	void *orig; /* Original reference. Also enforce alignment. */
	size_t size;
	unsigned char object[0];
};

struct marshal_info marshal_info_string = {
	.name = "null string",
	.size = 0,
	.pointers = { MARSHAL_SUBINFO_NULL },
};
struct marshal_info marshal_info_fstring = {
	.name = "fixed string",
	.size = 0,
	.pointers = { MARSHAL_SUBINFO_NULL },
};
struct marshal_info marshal_info_ignore = {
	.name = "ignored",
	.size = 0,
	.pointers = { MARSHAL_SUBINFO_NULL },
};

/* List of already seen pointers */
struct ref {
	TAILQ_ENTRY(ref) next;
	void *pointer;
	uintptr_t dummy; /* To renumerate pointers */
};
TAILQ_HEAD(ref_l, ref);

/* Serialize the given object. */
ssize_t
marshal_serialize_(struct marshal_info *mi, void *unserialized, void **input, int skip,
    void *_refs, int osize)
{
	struct ref_l *refs = _refs;
	struct ref *cref;
	int size;
	size_t len;
	struct marshal_subinfo *current;
	struct marshal_serialized *new = NULL, *serialized = NULL;
	uintptr_t dummy = 1;

	log_debug("marshal", "start serialization of %s", mi->name);

	/* Check if we have already serialized this one. */
	if (!refs) {
		refs = calloc(1, sizeof(struct ref_l));
		if (!refs) {
			log_warnx("marshal",
			    "unable to allocate memory for list of references");
			return -1;
		}
		TAILQ_INIT(refs);
	}
	TAILQ_FOREACH (cref, refs, next) {
		if (unserialized == cref->pointer) return 0;
		/* dummy should be higher than any existing dummy */
		if (cref->dummy >= dummy) dummy = cref->dummy + 1;
	}

	/* Handle special cases. */
	size = mi->size;
	if (!strcmp(mi->name, "null string")) /* We know we can't be called with NULL */
		size = strlen((char *)unserialized) + 1;
	else if (!strcmp(mi->name, "fixed string"))
		size = osize;

	/* Allocate serialized structure */
	len = sizeof(struct marshal_serialized) + (skip ? 0 : size);
	serialized = calloc(1, len);
	if (!serialized) {
		log_warnx("marshal",
		    "unable to allocate memory to serialize structure %s", mi->name);
		len = -1;
		goto marshal_error;
	}
	/* We don't use the original pointer but a dummy one. */
	serialized->orig = (unsigned char *)dummy;

	/* Append the new reference */
	if (!(cref = calloc(1, sizeof(struct ref)))) {
		log_warnx("marshal",
		    "unable to allocate memory for list of references");
		free(serialized);
		len = -1;
		goto marshal_error;
	}
	cref->pointer = unserialized;
	cref->dummy = dummy;
	TAILQ_INSERT_TAIL(refs, cref, next);

	/* First, serialize the main structure */
	if (!skip) memcpy(serialized->object, unserialized, size);

	/* Then, serialize inner structures */
	for (current = mi->pointers; current->mi; current++) {
		size_t sublen;
		size_t padlen;
		void *source;
		void *target = NULL;
		if (current->kind == ignore) continue;
		if (current->kind == pointer) {
			memcpy(&source, (unsigned char *)unserialized + current->offset,
			    sizeof(void *));
			if (source == NULL) continue;
		} else
			source =
			    (void *)((unsigned char *)unserialized + current->offset);
		if (current->offset2)
			memcpy(&osize, (unsigned char *)unserialized + current->offset2,
			    sizeof(int));
		target = NULL;
		sublen = marshal_serialize_(current->mi, source, &target,
		    current->kind == substruct, refs, osize);
		if (sublen == -1) {
			log_warnx("marshal",
			    "unable to serialize substructure %s for %s",
			    current->mi->name, mi->name);
			free(serialized);
			return -1;
		}
		/* We want to put the renumerated pointer instead of the real one. */
		if (current->kind == pointer && !skip) {
			TAILQ_FOREACH (cref, refs, next) {
				if (source == cref->pointer) {
					void *fakepointer =
					    (unsigned char *)cref->dummy;
					memcpy((unsigned char *)serialized->object +
						current->offset,
					    &fakepointer, sizeof(void *));
					break;
				}
			}
		}
		if (sublen == 0) continue; /* This was already serialized */
		/* Append the result, force alignment to be able to unserialize it */
		padlen = ALIGNOF(struct marshal_serialized);
		padlen = (padlen - (len % padlen)) % padlen;
		new = realloc(serialized, len + padlen + sublen);
		if (!new) {
			log_warnx("marshal",
			    "unable to allocate more memory to serialize structure %s",
			    mi->name);
			free(serialized);
			free(target);
			len = -1;
			goto marshal_error;
		}
		memset((unsigned char *)new + len, 0, padlen);
		memcpy((unsigned char *)new + len + padlen, target, sublen);
		free(target);
		len += sublen + padlen;
		serialized = (struct marshal_serialized *)new;
	}

	serialized->size = len;
	*input = serialized;
marshal_error:
	if (refs && !_refs) {
		struct ref *cref, *cref_next;
		for (cref = TAILQ_FIRST(refs); cref != NULL; cref = cref_next) {
			cref_next = TAILQ_NEXT(cref, next);
			TAILQ_REMOVE(refs, cref, next);
			free(cref);
		}
		free(refs);
	}
	return len;
}

/* This structure is used to track memory allocation when serializing */
struct gc {
	TAILQ_ENTRY(gc) next;
	void *pointer;
	void *orig; /* Original reference (not valid anymore !) */
};
TAILQ_HEAD(gc_l, gc);

static void *
marshal_alloc(struct gc_l *pointers, size_t len, void *orig)
{
	struct gc *gpointer = NULL;

	void *result = calloc(1, len);
	if (!result) return NULL;
	if ((gpointer = (struct gc *)calloc(1, sizeof(struct gc))) == NULL) {
		free(result);
		return NULL;
	}
	gpointer->pointer = result;
	gpointer->orig = orig;
	TAILQ_INSERT_TAIL(pointers, gpointer, next);
	return result;
}
static void
marshal_free(struct gc_l *pointers, int gconly)
{
	struct gc *pointer, *pointer_next;
	for (pointer = TAILQ_FIRST(pointers); pointer != NULL; pointer = pointer_next) {
		pointer_next = TAILQ_NEXT(pointer, next);
		TAILQ_REMOVE(pointers, pointer, next);
		if (!gconly) free(pointer->pointer);
		free(pointer);
	}
}

/* Unserialize the given object. */
size_t
marshal_unserialize_(struct marshal_info *mi, void *buffer, size_t len, void **output,
    void *_pointers, int skip, int osize)
{
	int total_len = sizeof(struct marshal_serialized) + (skip ? 0 : mi->size);
	struct marshal_serialized *serialized = buffer;
	struct gc_l *pointers = _pointers;
	int size, already, extra = 0;
	void *new;
	struct marshal_subinfo *current;
	struct gc *apointer;

	log_debug("marshal", "start unserialization of %s", mi->name);

	if (len < sizeof(struct marshal_serialized) || len < total_len) {
		log_warnx("marshal",
		    "data to deserialize is too small (%zu) for structure %s", len,
		    mi->name);
		return 0;
	}

	/* Initialize garbage collection */
	if (!pointers) {
		pointers = calloc(1, sizeof(struct gc_l));
		if (!pointers) {
			log_warnx("marshal",
			    "unable to allocate memory for garbage collection");
			return 0;
		}
		TAILQ_INIT(pointers);
	}

	/* Special cases */
	size = mi->size;
	if (!strcmp(mi->name, "null string") || !strcmp(mi->name, "fixed string")) {
		switch (mi->name[0]) {
		case 'n':
			size = strnlen((char *)serialized->object,
				   len - sizeof(struct marshal_serialized)) +
			    1;
			break;
		case 'f':
			size = osize;
			extra = 1;
			break; /* The extra byte is to ensure that
				  the string is null terminated. */
		}
		if (size > len - sizeof(struct marshal_serialized)) {
			log_warnx("marshal",
			    "data to deserialize contains a string too long");
			total_len = 0;
			goto unmarshal_error;
		}
		total_len += size;
	}

	/* First, the main structure */
	if (!skip) {
		if ((*output = marshal_alloc(pointers, size + extra,
			 serialized->orig)) == NULL) {
			log_warnx("marshal",
			    "unable to allocate memory to unserialize structure %s",
			    mi->name);
			total_len = 0;
			goto unmarshal_error;
		}
		memcpy(*output, serialized->object, size);
	}

	/* Then, each substructure */
	for (current = mi->pointers; current->mi; current++) {
		size_t sublen;
		size_t padlen;
		new = (unsigned char *)*output + current->offset;
		if (current->kind == ignore) {
			memset((unsigned char *)*output + current->offset, 0,
			    sizeof(void *));
			continue;
		}
		if (current->kind == pointer) {
			if (*(void **)new == NULL) continue;

			/* Did we already see this reference? */
			already = 0;
			TAILQ_FOREACH (apointer, pointers, next)
				if (apointer->orig == *(void **)new) {
					memcpy((unsigned char *)*output +
						current->offset,
					    &apointer->pointer, sizeof(void *));
					already = 1;
					break;
				}
			if (already) continue;
		}
		/* Deserialize */
		if (current->offset2)
			memcpy(&osize, (unsigned char *)*output + current->offset2,
			    sizeof(int));
		padlen = ALIGNOF(struct marshal_serialized);
		padlen = (padlen - (total_len % padlen)) % padlen;
		if (len < total_len + padlen ||
		    ((sublen = marshal_unserialize_(current->mi,
			  (unsigned char *)buffer + total_len + padlen,
			  len - total_len - padlen, &new, pointers,
			  current->kind == substruct, osize)) == 0)) {
			log_warnx("marshal",
			    "unable to serialize substructure %s for %s",
			    current->mi->name, mi->name);
			total_len = 0;
			goto unmarshal_error;
		}
		/* Link the result */
		if (current->kind == pointer)
			memcpy((unsigned char *)*output + current->offset, &new,
			    sizeof(void *));
		total_len += sublen + padlen;
	}

unmarshal_error:
	if (pointers && !_pointers) {
		marshal_free(pointers, (total_len > 0));
		free(pointers);
	}
	return total_len;
}