shm: Include the returned element length in the range check
[lttng-ust.git] / libringbuffer / shm.h
CommitLineData
a6352fd4
MD
1#ifndef _LIBRINGBUFFER_SHM_H
2#define _LIBRINGBUFFER_SHM_H
3
4/*
5 * libringbuffer/shm.h
6 *
7 * Copyright 2011 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
9 * Dual LGPL v2.1/GPL v2 license.
10 */
11
12#include <stdint.h>
1dbfff0c 13#include <ust/usterr-signal-safe.h>
a6352fd4 14#include "ust/core.h"
1d498196 15#include "shm_types.h"
a6352fd4 16
a6352fd4 17/*
1d498196
MD
18 * Pointer dereferencing. We don't trust the shm_ref, so we validate
19 * both the index and offset with known boundaries.
a6352fd4 20 */
a6352fd4 21static inline
4746ae29 22char *_shmp_offset(struct shm_object_table *table, struct shm_ref *ref,
cba4b7a3 23 size_t idx, size_t elem_size)
a6352fd4 24{
1d498196 25 struct shm_object *obj;
cba4b7a3 26 size_t objindex, ref_offset;
a6352fd4 27
cba4b7a3
MD
28 objindex = (size_t) ref->index;
29 if (unlikely(objindex >= table->allocated_len))
1d498196 30 return NULL;
cba4b7a3 31 obj = &table->objects[objindex];
4746ae29 32 ref_offset = (size_t) ref->offset;
cba4b7a3
MD
33 ref_offset += idx * elem_size;
34 /* Check if part of the element returned would exceed the limits. */
35 if (unlikely(ref_offset + elem_size > obj->memory_map_size))
a6352fd4 36 return NULL;
4746ae29 37 return &obj->memory_map[ref_offset];
a6352fd4
MD
38}
39
cba4b7a3 40#define shmp_index(handle, ref, index) \
1d498196
MD
41 ({ \
42 __typeof__((ref)._type) ____ptr_ret; \
cba4b7a3 43 ____ptr_ret = (__typeof__(____ptr_ret)) _shmp_offset((handle)->table, &(ref)._ref, index, sizeof(*____ptr_ret)); \
1d498196
MD
44 ____ptr_ret; \
45 })
46
4746ae29
MD
47#define shmp(handle, ref) shmp_index(handle, ref, 0)
48
431d5cf0 49static inline
1d498196 50void _set_shmp(struct shm_ref *ref, struct shm_ref src)
431d5cf0 51{
1d498196 52 *ref = src;
431d5cf0
MD
53}
54
1d498196
MD
55#define set_shmp(ref, src) _set_shmp(&(ref)._ref, src)
56
57struct shm_object_table *shm_object_table_create(size_t max_nb_obj);
58void shm_object_table_destroy(struct shm_object_table *table);
59struct shm_object *shm_object_table_append(struct shm_object_table *table,
60 size_t memory_map_size);
61
62/*
63 * zalloc_shm - allocate memory within a shm object.
64 *
65 * Shared memory is already zeroed by shmget.
66 * *NOT* multithread-safe (should be protected by mutex).
67 * Returns a -1, -1 tuple on error.
68 */
69struct shm_ref zalloc_shm(struct shm_object *obj, size_t len);
70void align_shm(struct shm_object *obj, size_t align);
71
5d61a504
MD
72static inline
73int shm_get_wakeup_fd(struct shm_handle *handle, struct shm_ref *ref)
74{
75 struct shm_object_table *table = handle->table;
76 struct shm_object *obj;
77 size_t index;
78
79 index = (size_t) ref->index;
80 if (unlikely(index >= table->allocated_len))
81 return -EPERM;
82 obj = &table->objects[index];
83 return obj->wait_fd[1];
84
85}
86
87static inline
88int shm_get_wait_fd(struct shm_handle *handle, struct shm_ref *ref)
89{
90 struct shm_object_table *table = handle->table;
91 struct shm_object *obj;
92 size_t index;
93
94 index = (size_t) ref->index;
95 if (unlikely(index >= table->allocated_len))
96 return -EPERM;
97 obj = &table->objects[index];
98 return obj->wait_fd[0];
99}
100
a6352fd4 101#endif /* _LIBRINGBUFFER_SHM_H */
This page took 0.026663 seconds and 4 git commands to generate.