|
|
@@ -14,6 +14,86 @@
|
|
14
|
14
|
|
|
15
|
15
|
/* . */
|
|
16
|
16
|
|
|
|
17
|
+static int global_dot_ir_pre(const struct func *func, struct node *n,
|
|
|
18
|
+ struct prog *prog)
|
|
|
19
|
+{
|
|
|
20
|
+ struct node *sou = n->expr.args;
|
|
|
21
|
+
|
|
|
22
|
+ if (node_is(sou, ":deref")) {
|
|
|
23
|
+ /* (*ptr).member, if *ptr is not already loaded let it
|
|
|
24
|
+ * know that we're only interested in one member */
|
|
|
25
|
+ sou->sym->irs.hint.dot = 1;
|
|
|
26
|
+
|
|
|
27
|
+ /* this also means we need to put ourselves on the
|
|
|
28
|
+ * stack since data will be loaded via probe_read */
|
|
|
29
|
+ n->sym->irs.hint.stack = 1;
|
|
|
30
|
+ }
|
|
|
31
|
+ return 0;
|
|
|
32
|
+}
|
|
|
33
|
+
|
|
|
34
|
+static int global_dot_ir_post(const struct func *func, struct node *n,
|
|
|
35
|
+ struct prog *prog)
|
|
|
36
|
+{
|
|
|
37
|
+ struct node *sou, *member;
|
|
|
38
|
+ struct irstate *dst;
|
|
|
39
|
+ ssize_t offset;
|
|
|
40
|
+
|
|
|
41
|
+ sou = n->expr.args;
|
|
|
42
|
+ member = sou->next;
|
|
|
43
|
+ dst = &n->sym->irs;
|
|
|
44
|
+
|
|
|
45
|
+ ir_init_sym(prog->ir, n->sym);
|
|
|
46
|
+
|
|
|
47
|
+ offset = type_offsetof(type_base(sou->sym->type), member->string.data);
|
|
|
48
|
+ assert(offset >= 0);
|
|
|
49
|
+
|
|
|
50
|
+ if (!sou->sym->irs.loc) {
|
|
|
51
|
+ /* sou is a :deref which wasn't loaded by child, just
|
|
|
52
|
+ * read the member we're interested in. */
|
|
|
53
|
+ struct node *ptr = sou->expr.args;
|
|
|
54
|
+
|
|
|
55
|
+ ir_emit_sym_to_reg(prog->ir, BPF_REG_3, ptr->sym);
|
|
|
56
|
+ ir_emit_insn(prog->ir, ALU_IMM(BPF_ADD, 0, offset), BPF_REG_3, 0);
|
|
|
57
|
+ goto probe_read;
|
|
|
58
|
+ }
|
|
|
59
|
+
|
|
|
60
|
+ offset += sou->sym->irs.stack;
|
|
|
61
|
+
|
|
|
62
|
+ if (dst->loc == LOC_REG) {
|
|
|
63
|
+ switch (dst->size) {
|
|
|
64
|
+ case 1:
|
|
|
65
|
+ ir_emit_insn(prog->ir, LDXB(0, offset, 0),
|
|
|
66
|
+ dst->reg, BPF_REG_BP);
|
|
|
67
|
+ break;
|
|
|
68
|
+ case 2:
|
|
|
69
|
+ ir_emit_insn(prog->ir, LDXH(0, offset, 0),
|
|
|
70
|
+ dst->reg, BPF_REG_BP);
|
|
|
71
|
+ break;
|
|
|
72
|
+ case 4:
|
|
|
73
|
+ ir_emit_insn(prog->ir, LDXW(0, offset, 0),
|
|
|
74
|
+ dst->reg, BPF_REG_BP);
|
|
|
75
|
+ break;
|
|
|
76
|
+ case 8:
|
|
|
77
|
+ ir_emit_insn(prog->ir, LDXDW(0, offset, 0),
|
|
|
78
|
+ dst->reg, BPF_REG_BP);
|
|
|
79
|
+ break;
|
|
|
80
|
+ default:
|
|
|
81
|
+ assert(0);
|
|
|
82
|
+ }
|
|
|
83
|
+
|
|
|
84
|
+ return 0;
|
|
|
85
|
+ }
|
|
|
86
|
+
|
|
|
87
|
+ ir_emit_insn(prog->ir, ALU_IMM(BPF_ADD, 0, offset), BPF_REG_3, 0);
|
|
|
88
|
+probe_read:
|
|
|
89
|
+ ir_emit_insn(prog->ir, MOV_IMM(0, (int32_t)dst->size), BPF_REG_2, 0);
|
|
|
90
|
+ ir_emit_insn(prog->ir, MOV(0, 0), BPF_REG_1, BPF_REG_BP);
|
|
|
91
|
+ ir_emit_insn(prog->ir, ALU_IMM(BPF_ADD, 0, dst->stack), BPF_REG_1, 0);
|
|
|
92
|
+ ir_emit_insn(prog->ir, CALL(BPF_FUNC_probe_read), 0, 0);
|
|
|
93
|
+ /* TODO if (r0) exit(r0); */
|
|
|
94
|
+ return 0;
|
|
|
95
|
+}
|
|
|
96
|
+
|
|
17
|
97
|
static int global_dot_type_infer(const struct func *func, struct node *n)
|
|
18
|
98
|
{
|
|
19
|
99
|
struct node *sou, *member;
|
|
|
@@ -37,18 +117,16 @@ static int global_dot_type_infer(const struct func *func, struct node *n)
|
|
37
|
117
|
return -EINVAL;
|
|
38
|
118
|
}
|
|
39
|
119
|
|
|
40
|
|
- tfields_foreach(f, t->sou.fields) {
|
|
41
|
|
- if (!strcmp(f->name, member->string.data)) {
|
|
42
|
|
- /* given `sou.member` where sou is a
|
|
43
|
|
- * struct/union, infer that the expression's
|
|
44
|
|
- * type is equal to member's type. */
|
|
45
|
|
- n->sym->type = f->type;
|
|
46
|
|
- return 0;
|
|
47
|
|
- }
|
|
|
120
|
+ f = tfields_get(t->sou.fields, member->string.data);
|
|
|
121
|
+ if (!f) {
|
|
|
122
|
+ _e("%#N: type '%T' has no member named %N.\n", n, t, member);
|
|
|
123
|
+ return -EINVAL;
|
|
48
|
124
|
}
|
|
49
|
125
|
|
|
50
|
|
- _e("%#N: type '%T' has no member named %N.\n", n, t, member);
|
|
51
|
|
- return -EINVAL;
|
|
|
126
|
+ /* given `sou.member` where sou is a struct/union, infer that
|
|
|
127
|
+ * the expression's type is equal to member's type. */
|
|
|
128
|
+ n->sym->type = f->type;
|
|
|
129
|
+ return 0;
|
|
52
|
130
|
}
|
|
53
|
131
|
|
|
54
|
132
|
|
|
|
@@ -69,20 +147,19 @@ static int global_deref_ir_post(const struct func *func, struct node *n,
|
|
69
|
147
|
* from our argument */
|
|
70
|
148
|
return 0;
|
|
71
|
149
|
|
|
72
|
|
- if (dst->loc)
|
|
73
|
|
- return 0;
|
|
|
150
|
+ ir_init_sym(prog->ir, n->sym);
|
|
74
|
151
|
|
|
75
|
|
- size = type_sizeof(type_base(ptr->sym->type)->ptr.type);
|
|
76
|
|
- /* parent might have already allocated stack for us. typically
|
|
77
|
|
- * when the value is one part of a map key or value. */
|
|
78
|
|
- if (!dst->hint.stack)
|
|
79
|
|
- irs_alloc_stack(dst, prog->ir, size);
|
|
|
152
|
+ if (dst->hint.lval)
|
|
|
153
|
+ /* *ptr = val, whatever is in our storage now it will
|
|
|
154
|
+ be overwritten, so skip the load. */
|
|
|
155
|
+ return 0;
|
|
80
|
156
|
|
|
81
|
157
|
ir_emit_insn(prog->ir, MOV(0, 0), BPF_REG_1, BPF_REG_BP);
|
|
82
|
158
|
ir_emit_insn(prog->ir, ALU_IMM(BPF_ADD, 0, dst->stack), BPF_REG_1, 0);
|
|
83
|
|
- ir_emit_insn(prog->ir, MOV_IMM(0, (int32_t)size), BPF_REG_2, 0);
|
|
84
|
|
- ir_emit_mov_irs(prog->ir, BPF_REG_3, &ptr->sym->irs);
|
|
|
159
|
+ ir_emit_insn(prog->ir, MOV_IMM(0, (int32_t)dst->size), BPF_REG_2, 0);
|
|
|
160
|
+ ir_emit_sym_to_reg(prog->ir, BPF_REG_3, ptr->sym);
|
|
85
|
161
|
ir_emit_insn(prog->ir, CALL(BPF_FUNC_probe_read), 0, 0);
|
|
|
162
|
+ /* TODO if (r0) exit(r0); */
|
|
86
|
163
|
return 0;
|
|
87
|
164
|
}
|
|
88
|
165
|
|
|
|
@@ -208,7 +285,7 @@ static int global_assign_type_infer(const struct func *func, struct node *n)
|
|
208
|
285
|
/* TODO do we need assignment expressions? */
|
|
209
|
286
|
n->sym->type = &t_void;
|
|
210
|
287
|
|
|
211
|
|
- if (node_is_map(lval))
|
|
|
288
|
+ if (node_is(lval, "{}"))
|
|
212
|
289
|
return global_assign_type_infer_map(lval);
|
|
213
|
290
|
|
|
214
|
291
|
return 0;
|
|
|
@@ -229,7 +306,7 @@ static int global_assign_static_validate(const struct func *func, struct node *n
|
|
229
|
306
|
|
|
230
|
307
|
lval = n->expr.args;
|
|
231
|
308
|
|
|
232
|
|
- if (node_is_map(lval) || (lval->ntype == N_IDENT))
|
|
|
309
|
+ if (node_is(lval, "{}") || (lval->ntype == N_IDENT))
|
|
233
|
310
|
return 0;
|
|
234
|
311
|
|
|
235
|
312
|
_e("%#N: can't assign a value to %N.\n", n, lval);
|
|
|
@@ -285,6 +362,19 @@ static int global_quantize_type_infer(const struct func *func, struct node *n)
|
|
285
|
362
|
|
|
286
|
363
|
/* pid */
|
|
287
|
364
|
|
|
|
365
|
+static int global_pid_ir_post(const struct func *func, struct node *n,
|
|
|
366
|
+ struct prog *prog)
|
|
|
367
|
+{
|
|
|
368
|
+ struct node *ptr = n->expr.args;
|
|
|
369
|
+
|
|
|
370
|
+ ir_init_sym(prog->ir, n->sym);
|
|
|
371
|
+
|
|
|
372
|
+ ir_emit_insn(prog->ir, CALL(BPF_FUNC_get_current_pid_tgid), 0, 0);
|
|
|
373
|
+ ir_emit_insn(prog->ir, ALU64_IMM(BPF_RSH, 0, 32), BPF_REG_0, 0);
|
|
|
374
|
+ ir_emit_reg_to_sym(prog->ir, n->sym, BPF_REG_0);
|
|
|
375
|
+ return 0;
|
|
|
376
|
+}
|
|
|
377
|
+
|
|
288
|
378
|
struct type t_pid = {
|
|
289
|
379
|
.ttype = T_TYPEDEF,
|
|
290
|
380
|
|
|
|
@@ -299,6 +389,18 @@ struct type t_pid_func = {
|
|
299
|
389
|
|
|
300
|
390
|
/* time */
|
|
301
|
391
|
|
|
|
392
|
+static int global_time_ir_post(const struct func *func, struct node *n,
|
|
|
393
|
+ struct prog *prog)
|
|
|
394
|
+{
|
|
|
395
|
+ struct node *ptr = n->expr.args;
|
|
|
396
|
+
|
|
|
397
|
+ ir_init_sym(prog->ir, n->sym);
|
|
|
398
|
+
|
|
|
399
|
+ ir_emit_insn(prog->ir, CALL(BPF_FUNC_ktime_get_ns), 0, 0);
|
|
|
400
|
+ ir_emit_reg_to_sym(prog->ir, n->sym, BPF_REG_0);
|
|
|
401
|
+ return 0;
|
|
|
402
|
+}
|
|
|
403
|
+
|
|
302
|
404
|
struct type t_time = {
|
|
303
|
405
|
.ttype = T_TYPEDEF, /* TODO: should be a T_FUNC with a static
|
|
304
|
406
|
* signature */
|
|
|
@@ -379,6 +481,9 @@ static const struct func global_funcs[] = {
|
|
379
|
481
|
.name = ".",
|
|
380
|
482
|
.type = &t_dot_func,
|
|
381
|
483
|
.type_infer = global_dot_type_infer,
|
|
|
484
|
+
|
|
|
485
|
+ .ir_pre = global_dot_ir_pre,
|
|
|
486
|
+ .ir_post = global_dot_ir_post,
|
|
382
|
487
|
},
|
|
383
|
488
|
{
|
|
384
|
489
|
.name = ":deref",
|
|
|
@@ -416,11 +521,15 @@ static const struct func global_funcs[] = {
|
|
416
|
521
|
.name = "pid",
|
|
417
|
522
|
.type = &t_pid_func,
|
|
418
|
523
|
.static_ret = 1,
|
|
|
524
|
+
|
|
|
525
|
+ .ir_post = global_pid_ir_post,
|
|
419
|
526
|
},
|
|
420
|
527
|
{
|
|
421
|
528
|
.name = "time",
|
|
422
|
529
|
.type = &t_time_func,
|
|
423
|
530
|
.static_ret = 1,
|
|
|
531
|
+
|
|
|
532
|
+ .ir_post = global_time_ir_post,
|
|
424
|
533
|
},
|
|
425
|
534
|
|
|
426
|
535
|
{
|