SoW-2020-0002: Trace Hit Counters: trigger error reporting integration
[lttng-tools.git] / src / common / bytecode / bytecode.c
1 /*
2 * Copyright 2020 EfficiOS, Inc.
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include "bytecode.h"
9
10 #include <errno.h>
11
12 #include "common/align.h"
13
14 #define INIT_ALLOC_SIZE 4
15
16 static inline int get_count_order(unsigned int count)
17 {
18 int order;
19
20 order = lttng_fls(count) - 1;
21 if (count & (count - 1))
22 order++;
23 return order;
24 }
25
26 LTTNG_HIDDEN
27 int bytecode_init(struct lttng_bytecode_alloc **fb)
28 {
29 uint32_t alloc_len;
30
31 alloc_len = sizeof(struct lttng_bytecode_alloc) + INIT_ALLOC_SIZE;
32 *fb = calloc(alloc_len, 1);
33 if (!*fb) {
34 return -ENOMEM;
35 } else {
36 (*fb)->alloc_len = alloc_len;
37 return 0;
38 }
39 }
40
41 LTTNG_HIDDEN
42 int32_t bytecode_reserve(struct lttng_bytecode_alloc **fb, uint32_t align, uint32_t len)
43 {
44 int32_t ret;
45 uint32_t padding = offset_align((*fb)->b.len, align);
46 uint32_t new_len = (*fb)->b.len + padding + len;
47 uint32_t new_alloc_len = sizeof(struct lttng_bytecode_alloc) + new_len;
48 uint32_t old_alloc_len = (*fb)->alloc_len;
49
50 if (new_len > LTTNG_FILTER_MAX_LEN)
51 return -EINVAL;
52
53 if (new_alloc_len > old_alloc_len) {
54 struct lttng_bytecode_alloc *newptr;
55
56 new_alloc_len =
57 max_t(uint32_t, 1U << get_count_order(new_alloc_len), old_alloc_len << 1);
58 newptr = realloc(*fb, new_alloc_len);
59 if (!newptr)
60 return -ENOMEM;
61 *fb = newptr;
62 /* We zero directly the memory from start of allocation. */
63 memset(&((char *) *fb)[old_alloc_len], 0, new_alloc_len - old_alloc_len);
64 (*fb)->alloc_len = new_alloc_len;
65 }
66 (*fb)->b.len += padding;
67 ret = (*fb)->b.len;
68 (*fb)->b.len += len;
69 return ret;
70 }
71
72 LTTNG_HIDDEN
73 int bytecode_push(struct lttng_bytecode_alloc **fb, const void *data,
74 uint32_t align, uint32_t len)
75 {
76 int32_t offset;
77
78 offset = bytecode_reserve(fb, align, len);
79 if (offset < 0)
80 return offset;
81 memcpy(&(*fb)->b.data[offset], data, len);
82 return 0;
83 }
84
85 LTTNG_HIDDEN
86 int bytecode_push_logical(struct lttng_bytecode_alloc **fb,
87 struct logical_op *data,
88 uint32_t align, uint32_t len,
89 uint16_t *skip_offset)
90 {
91 int32_t offset;
92
93 offset = bytecode_reserve(fb, align, len);
94 if (offset < 0)
95 return offset;
96 memcpy(&(*fb)->b.data[offset], data, len);
97 *skip_offset =
98 (void *) &((struct logical_op *) &(*fb)->b.data[offset])->skip_offset
99 - (void *) &(*fb)->b.data[0];
100 return 0;
101 }
102
103 LTTNG_HIDDEN
104 int bytecode_push_get_payload_root(struct lttng_bytecode_alloc **bytecode)
105 {
106 struct load_op *insn;
107 uint32_t insn_len = sizeof(struct load_op);
108 int ret;
109
110 insn = calloc(insn_len, 1);
111 if (!insn)
112 return -ENOMEM;
113
114 insn->op = BYTECODE_OP_GET_PAYLOAD_ROOT;
115 ret = bytecode_push(bytecode, insn, 1, insn_len);
116 free(insn);
117
118 return ret;
119 }
120
121 LTTNG_HIDDEN
122 int bytecode_push_get_context_root(struct lttng_bytecode_alloc **bytecode)
123 {
124 struct load_op *insn;
125 uint32_t insn_len = sizeof(struct load_op);
126 int ret;
127
128 insn = calloc(insn_len, 1);
129 if (!insn)
130 return -ENOMEM;
131
132 insn->op = BYTECODE_OP_GET_CONTEXT_ROOT;
133 ret = bytecode_push(bytecode, insn, 1, insn_len);
134 free(insn);
135
136 return ret;
137 }
138
139 LTTNG_HIDDEN
140 int bytecode_push_get_app_context_root(struct lttng_bytecode_alloc **bytecode)
141 {
142 struct load_op *insn;
143 uint32_t insn_len = sizeof(struct load_op);
144 int ret;
145
146 insn = calloc(insn_len, 1);
147 if (!insn)
148 return -ENOMEM;
149
150 insn->op = BYTECODE_OP_GET_APP_CONTEXT_ROOT;
151 ret = bytecode_push(bytecode, insn, 1, insn_len);
152 free(insn);
153
154 return ret;
155 }
156
157 LTTNG_HIDDEN
158 int bytecode_push_get_index_u64(struct lttng_bytecode_alloc **bytecode,
159 uint64_t index)
160 {
161 struct load_op *insn;
162 uint32_t insn_len = sizeof(struct load_op)
163 + sizeof(struct get_index_u64);
164 struct get_index_u64 index_op_data;
165 int ret;
166
167 insn = calloc(insn_len, 1);
168 if (!insn)
169 return -ENOMEM;
170
171 insn->op = BYTECODE_OP_GET_INDEX_U64;
172 index_op_data.index = index;
173 memcpy(insn->data, &index_op_data, sizeof(index));
174 ret = bytecode_push(bytecode, insn, 1, insn_len);
175
176 free(insn);
177
178 return ret;
179 }
180
181 LTTNG_HIDDEN
182 int bytecode_push_get_symbol(struct lttng_bytecode_alloc **bytecode,
183 struct lttng_bytecode_alloc **bytecode_reloc,
184 const char *symbol)
185 {
186 struct load_op *insn;
187 uint32_t insn_len = sizeof(struct load_op)
188 + sizeof(struct get_symbol);
189 struct get_symbol symbol_offset;
190 uint32_t reloc_offset_u32;
191 uint16_t reloc_offset;
192 uint32_t bytecode_reloc_offset_u32;
193 int ret;
194
195 insn = calloc(insn_len, 1);
196 if (!insn) {
197 ret = -ENOMEM;
198 goto end;
199 }
200
201 insn->op = BYTECODE_OP_GET_SYMBOL;
202
203 /*
204 * Get offset in the reloc portion at which the symbol name
205 * will end up at (GET_SYMBOL's operand points there).
206 */
207 bytecode_reloc_offset_u32 =
208 bytecode_get_len(&(*bytecode_reloc)->b)
209 + sizeof(reloc_offset);
210 symbol_offset.offset = (uint16_t) bytecode_reloc_offset_u32;
211 memcpy(insn->data, &symbol_offset, sizeof(symbol_offset));
212
213 /*
214 * Get offset in the bytecode where the opcode will end up at,
215 * the reloc offset points to it.
216 */
217 reloc_offset_u32 = bytecode_get_len(&(*bytecode)->b);
218 if (reloc_offset_u32 > LTTNG_FILTER_MAX_LEN - 1) {
219 ret = -EINVAL;
220 goto end;
221 }
222 reloc_offset = (uint16_t) reloc_offset_u32;
223
224 /* Append op in bytecode. */
225 ret = bytecode_push(bytecode, insn, 1, insn_len);
226 if (ret) {
227 goto end;
228 }
229
230 /* Append reloc offset. */
231 ret = bytecode_push(bytecode_reloc, &reloc_offset,
232 1, sizeof(reloc_offset));
233 if (ret) {
234 goto end;
235 }
236
237 /* Append symbol name. */
238 ret = bytecode_push(bytecode_reloc, symbol, 1, strlen(symbol) + 1);
239
240 end:
241 free(insn);
242 return ret;
243 }
244
245 /*
246 * Allocate an lttng_bytecode object and copy the given original bytecode.
247 *
248 * Return allocated bytecode or NULL on error.
249 */
250 struct lttng_bytecode *bytecode_copy(
251 const struct lttng_bytecode *orig_f)
252 {
253 struct lttng_bytecode *bytecode = NULL;
254
255 bytecode = zmalloc(sizeof(*bytecode) + orig_f->len);
256 if (!bytecode) {
257 goto error;
258 }
259
260 memcpy(bytecode, orig_f, sizeof(*bytecode) + orig_f->len);
261
262 error:
263 return bytecode;
264 }
This page took 0.035776 seconds and 5 git commands to generate.