summaryrefslogtreecommitdiff
path: root/dev-util/bpftrace/files/bpftrace-0.16.0-opaquepointer.patch
blob: 656bf0f6a6393c12719cfc5b2bb1b7dbf5d1e4e5 (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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
commit a8665b2ef3199e82fd2ad076c1f07f1af2ec9272
Author: Viktor Malik <viktor.malik@gmail.com>
Date:   Mon Oct 10 14:26:38 2022 +0200

    IR builder: get rid of getPointerElementType calls
    
    Usage of Value::getPointerElementType is deprecated and will be dropped
    in LLVM 16 [1].
    
    There are several places where we use this method:
    - function (value) calls - the called function type is usually
      available, so just pass it to createCall, the only exception is
      CreateProbeReadStr which must have been refactored
    - getting the type of alloca instruction - there is a dedicated
      AllocaInst::getAllocatedType method that can be used instead
    - strncmp - pass sizes of the strings to CreateStrncmp to be able to get
      the correct string type (which is array of uint8)
    
    [1] https://llvm.org/docs/OpaquePointers.html

diff --git a/src/ast/irbuilderbpf.cpp b/src/ast/irbuilderbpf.cpp
index 09ae1c5e..da120ba1 100644
--- a/src/ast/irbuilderbpf.cpp
+++ b/src/ast/irbuilderbpf.cpp
@@ -291,17 +291,16 @@ CallInst *IRBuilderBPF::CreateHelperCall(libbpf::bpf_func_id func_id,
   Constant *helper_func = ConstantExpr::getCast(Instruction::IntToPtr,
                                                 getInt64(func_id),
                                                 helper_ptr_type);
-  return createCall(helper_func, args, Name);
+  return createCall(helper_type, helper_func, args, Name);
 }
 
-CallInst *IRBuilderBPF::createCall(Value *callee,
+CallInst *IRBuilderBPF::createCall(FunctionType *callee_type,
+                                   Value *callee,
                                    ArrayRef<Value *> args,
                                    const Twine &Name)
 {
 #if LLVM_VERSION_MAJOR >= 11
-  auto *calleePtrType = cast<PointerType>(callee->getType());
-  auto *calleeType = cast<FunctionType>(calleePtrType->getPointerElementType());
-  return CreateCall(calleeType, callee, args, Name);
+  return CreateCall(callee_type, callee, args, Name);
 #else
   return CreateCall(callee, args, Name);
 #endif
@@ -310,7 +309,7 @@ CallInst *IRBuilderBPF::createCall(Value *callee,
 CallInst *IRBuilderBPF::CreateBpfPseudoCallId(int mapid)
 {
   Function *pseudo_func = module_.getFunction("llvm.bpf.pseudo");
-  return createCall(pseudo_func,
+  return CreateCall(pseudo_func,
                     { getInt64(BPF_PSEUDO_MAP_FD), getInt64(mapid) },
                     "pseudo");
 }
@@ -349,7 +348,8 @@ CallInst *IRBuilderBPF::createMapLookup(int mapid, Value *key)
       Instruction::IntToPtr,
       getInt64(libbpf::BPF_FUNC_map_lookup_elem),
       lookup_func_ptr_type);
-  return createCall(lookup_func, { map_ptr, key }, "lookup_elem");
+  return createCall(
+      lookup_func_type, lookup_func, { map_ptr, key }, "lookup_elem");
 }
 
 CallInst *IRBuilderBPF::CreateGetJoinMap(Value *ctx, const location &loc)
@@ -400,8 +400,7 @@ Value *IRBuilderBPF::CreateMapLookupElem(Value *ctx,
     CREATE_MEMCPY(value, call, type.GetSize(), 1);
   else
   {
-    assert(value->getType()->isPointerTy() &&
-           (value->getType()->getPointerElementType() == getInt64Ty()));
+    assert(value->getAllocatedType() == getInt64Ty());
     // createMapLookup  returns an u8*
     auto *cast = CreatePointerCast(call, value->getType(), "cast");
     CreateStore(CreateLoad(getInt64Ty(), cast), value);
@@ -451,7 +450,8 @@ void IRBuilderBPF::CreateMapUpdateElem(Value *ctx,
       Instruction::IntToPtr,
       getInt64(libbpf::BPF_FUNC_map_update_elem),
       update_func_ptr_type);
-  CallInst *call = createCall(update_func,
+  CallInst *call = createCall(update_func_type,
+                              update_func,
                               { map_ptr, key, val, flags },
                               "update_elem");
   CreateHelperErrorCond(ctx, call, libbpf::BPF_FUNC_map_update_elem, loc);
@@ -475,7 +475,8 @@ void IRBuilderBPF::CreateMapDeleteElem(Value *ctx,
       Instruction::IntToPtr,
       getInt64(libbpf::BPF_FUNC_map_delete_elem),
       delete_func_ptr_type);
-  CallInst *call = createCall(delete_func, { map_ptr, key }, "delete_elem");
+  CallInst *call = createCall(
+      delete_func_type, delete_func, { map_ptr, key }, "delete_elem");
   CreateHelperErrorCond(ctx, call, libbpf::BPF_FUNC_map_delete_elem, loc);
 }
 
@@ -501,72 +502,53 @@ void IRBuilderBPF::CreateProbeRead(Value *ctx,
   Constant *proberead_func = ConstantExpr::getCast(Instruction::IntToPtr,
                                                    getInt64(read_fn),
                                                    proberead_func_ptr_type);
-  CallInst *call = createCall(proberead_func,
+  CallInst *call = createCall(proberead_func_type,
+                              proberead_func,
                               { dst, size, src },
                               probeReadHelperName(read_fn));
   CreateHelperErrorCond(ctx, call, read_fn, loc);
 }
 
-Constant *IRBuilderBPF::createProbeReadStrFn(llvm::Type *dst,
-                                             llvm::Type *src,
-                                             AddrSpace as)
-{
-  assert(src && (src->isIntegerTy() || src->isPointerTy()));
-  // int bpf_probe_read_str(void *dst, int size, const void *unsafe_ptr)
-  FunctionType *probereadstr_func_type = FunctionType::get(
-      getInt64Ty(), { dst, getInt32Ty(), src }, false);
-  PointerType *probereadstr_func_ptr_type = PointerType::get(
-      probereadstr_func_type, 0);
-  return ConstantExpr::getCast(Instruction::IntToPtr,
-                               getInt64(selectProbeReadHelper(as, true)),
-                               probereadstr_func_ptr_type);
-}
-
 CallInst *IRBuilderBPF::CreateProbeReadStr(Value *ctx,
-                                           AllocaInst *dst,
+                                           Value *dst,
                                            size_t size,
                                            Value *src,
                                            AddrSpace as,
                                            const location &loc)
 {
-  assert(ctx && ctx->getType() == getInt8PtrTy());
   return CreateProbeReadStr(ctx, dst, getInt32(size), src, as, loc);
 }
 
 CallInst *IRBuilderBPF::CreateProbeReadStr(Value *ctx,
                                            Value *dst,
-                                           size_t size,
-                                           Value *src,
-                                           AddrSpace as,
-                                           const location &loc)
-{
-  assert(ctx && ctx->getType() == getInt8PtrTy());
-  Constant *fn = createProbeReadStrFn(dst->getType(), src->getType(), as);
-  auto read_fn = selectProbeReadHelper(as, true);
-  CallInst *call = createCall(fn,
-                              { dst, getInt32(size), src },
-                              probeReadHelperName(read_fn));
-  CreateHelperErrorCond(ctx, call, read_fn, loc);
-  return call;
-}
-
-CallInst *IRBuilderBPF::CreateProbeReadStr(Value *ctx,
-                                           AllocaInst *dst,
                                            llvm::Value *size,
                                            Value *src,
                                            AddrSpace as,
                                            const location &loc)
 {
   assert(ctx && ctx->getType() == getInt8PtrTy());
-  assert(dst && dst->getAllocatedType()->isArrayTy() &&
-         dst->getAllocatedType()->getArrayElementType() == getInt8Ty());
   assert(size && size->getType()->isIntegerTy());
+  if (auto *dst_alloca = dyn_cast<AllocaInst>(dst))
+  {
+    assert(dst_alloca->getAllocatedType()->isArrayTy() &&
+           dst_alloca->getAllocatedType()->getArrayElementType() ==
+               getInt8Ty());
+  }
 
-  auto *size_i32 = CreateIntCast(size, getInt32Ty(), false);
+  auto *size_i32 = size;
+  if (size_i32->getType()->getScalarSizeInBits() != 32)
+    size_i32 = CreateIntCast(size_i32, getInt32Ty(), false);
 
-  Constant *fn = createProbeReadStrFn(dst->getType(), src->getType(), as);
   auto read_fn = selectProbeReadHelper(as, true);
-  CallInst *call = createCall(fn,
+  // int bpf_probe_read_str(void *dst, int size, const void *unsafe_ptr)
+  FunctionType *probereadstr_func_type = FunctionType::get(
+      getInt64Ty(), { dst->getType(), getInt32Ty(), src->getType() }, false);
+  PointerType *probereadstr_func_ptr_type = PointerType::get(
+      probereadstr_func_type, 0);
+  Constant *probereadstr_callee = ConstantExpr::getCast(
+      Instruction::IntToPtr, getInt64(read_fn), probereadstr_func_ptr_type);
+  CallInst *call = createCall(probereadstr_func_type,
+                              probereadstr_callee,
                               { dst, size_i32, src },
                               probeReadHelperName(read_fn));
   CreateHelperErrorCond(ctx, call, read_fn, loc);
@@ -725,8 +707,10 @@ Value *IRBuilderBPF::CreateUSDTReadArgument(Value *ctx,
   return result;
 }
 
-Value *IRBuilderBPF::CreateStrncmp(Value *val1,
-                                   Value *val2,
+Value *IRBuilderBPF::CreateStrncmp(Value *str1,
+                                   uint64_t str1_size,
+                                   Value *str2,
+                                   uint64_t str2_size,
                                    uint64_t n,
                                    bool inverse)
 {
@@ -755,40 +739,21 @@ Value *IRBuilderBPF::CreateStrncmp(Value *val1,
   // Check if the compared strings are literals.
   // If so, we can avoid storing the literal in memory.
   std::optional<std::string> literal1;
-  if (auto constString1 = dyn_cast<ConstantDataArray>(val1))
+  if (auto constString1 = dyn_cast<ConstantDataArray>(str1))
     literal1 = constString1->getAsString();
-  else if (isa<ConstantAggregateZero>(val1))
+  else if (isa<ConstantAggregateZero>(str1))
     literal1 = "";
   else
     literal1 = std::nullopt;
 
   std::optional<std::string> literal2;
-  if (auto constString2 = dyn_cast<ConstantDataArray>(val2))
+  if (auto constString2 = dyn_cast<ConstantDataArray>(str2))
     literal2 = constString2->getAsString();
-  else if (isa<ConstantAggregateZero>(val2))
+  else if (isa<ConstantAggregateZero>(str2))
     literal2 = "";
   else
     literal2 = std::nullopt;
 
-  auto *val1p = dyn_cast<PointerType>(val1->getType());
-  auto *val2p = dyn_cast<PointerType>(val2->getType());
-#ifndef NDEBUG
-  if (!literal1)
-  {
-    assert(val1p);
-    assert(val1p->getPointerElementType()->isArrayTy() &&
-           val1p->getPointerElementType()->getArrayElementType() ==
-               getInt8Ty());
-  }
-  if (!literal2)
-  {
-    assert(val2p);
-    assert(val2p->getPointerElementType()->isArrayTy() &&
-           val2p->getPointerElementType()->getArrayElementType() ==
-               getInt8Ty());
-  }
-#endif
-
   Function *parent = GetInsertBlock()->getParent();
   AllocaInst *store = CreateAllocaBPF(getInt1Ty(), "strcmp.result");
   BasicBlock *str_ne = BasicBlock::Create(module_.getContext(),
@@ -815,8 +780,8 @@ Value *IRBuilderBPF::CreateStrncmp(Value *val1,
       l = getInt8(literal1->c_str()[i]);
     else
     {
-      auto *ptr_l = CreateGEP(val1p->getPointerElementType(),
-                              val1,
+      auto *ptr_l = CreateGEP(ArrayType::get(getInt8Ty(), str1_size),
+                              str1,
                               { getInt32(0), getInt32(i) });
       l = CreateLoad(getInt8Ty(), ptr_l);
     }
@@ -826,8 +791,8 @@ Value *IRBuilderBPF::CreateStrncmp(Value *val1,
       r = getInt8(literal2->c_str()[i]);
     else
     {
-      auto *ptr_r = CreateGEP(val2p->getPointerElementType(),
-                              val2,
+      auto *ptr_r = CreateGEP(ArrayType::get(getInt8Ty(), str2_size),
+                              str2,
                               { getInt32(0), getInt32(i) });
       r = CreateLoad(getInt8Ty(), ptr_r);
     }
@@ -987,11 +952,9 @@ void IRBuilderBPF::CreateGetCurrentComm(Value *ctx,
                                         size_t size,
                                         const location &loc)
 {
-  assert(buf->getType()->getPointerElementType()->isArrayTy() &&
-         buf->getType()->getPointerElementType()->getArrayNumElements() >=
-             size &&
-         buf->getType()->getPointerElementType()->getArrayElementType() ==
-             getInt8Ty());
+  assert(buf->getAllocatedType()->isArrayTy() &&
+         buf->getAllocatedType()->getArrayNumElements() >= size &&
+         buf->getAllocatedType()->getArrayElementType() == getInt8Ty());
 
   // long bpf_get_current_comm(char *buf, int size_of_buf)
   // Return: 0 on success or negative error
@@ -1070,7 +1033,7 @@ void IRBuilderBPF::CreateSignal(Value *ctx, Value *sig, const location &loc)
       Instruction::IntToPtr,
       getInt64(libbpf::BPF_FUNC_send_signal),
       signal_func_ptr_type);
-  CallInst *call = createCall(signal_func, { sig }, "signal");
+  CallInst *call = createCall(signal_func_type, signal_func, { sig }, "signal");
   CreateHelperErrorCond(ctx, call, libbpf::BPF_FUNC_send_signal, loc);
 }
 
@@ -1084,7 +1047,7 @@ void IRBuilderBPF::CreateOverrideReturn(Value *ctx, Value *rc)
   Constant *override_func = ConstantExpr::getCast(Instruction::IntToPtr,
       getInt64(libbpf::BPF_FUNC_override_return),
       override_func_ptr_type);
-  createCall(override_func, { ctx, rc }, "override");
+  createCall(override_func_type, override_func, { ctx, rc }, "override");
 }
 
 CallInst *IRBuilderBPF::CreateSkbOutput(Value *skb,
@@ -1119,7 +1082,8 @@ CallInst *IRBuilderBPF::CreateSkbOutput(Value *skb,
       Instruction::IntToPtr,
       getInt64(libbpf::BPF_FUNC_skb_output),
       skb_output_func_ptr_type);
-  CallInst *call = createCall(skb_output_func,
+  CallInst *call = createCall(skb_output_func_type,
+                              skb_output_func,
                               { skb, map_ptr, flags, data, size_val },
                               "skb_output");
   return call;
@@ -1328,7 +1292,8 @@ void IRBuilderBPF::CreateSeqPrintf(Value *ctx,
                           CreateGEP(getInt64Ty(), meta, getInt64(0)),
                           "seq");
 
-  CallInst *call = createCall(seq_printf_func,
+  CallInst *call = createCall(seq_printf_func_type,
+                              seq_printf_func,
                               { seq, fmt, fmt_size, data, data_len },
                               "seq_printf");
   CreateHelperErrorCond(ctx, call, libbpf::BPF_FUNC_seq_printf, loc);
diff --git a/src/ast/irbuilderbpf.h b/src/ast/irbuilderbpf.h
index b6a06778..19b28180 100644
--- a/src/ast/irbuilderbpf.h
+++ b/src/ast/irbuilderbpf.h
@@ -106,17 +106,11 @@ public:
       bool isVolatile = false,
       std::optional<AddrSpace> addrSpace = std::nullopt);
   CallInst *CreateProbeReadStr(Value *ctx,
-                               AllocaInst *dst,
+                               Value *dst,
                                llvm::Value *size,
                                Value *src,
                                AddrSpace as,
                                const location &loc);
-  CallInst *CreateProbeReadStr(Value *ctx,
-                               AllocaInst *dst,
-                               size_t size,
-                               Value *src,
-                               AddrSpace as,
-                               const location &loc);
   CallInst *CreateProbeReadStr(Value *ctx,
                                Value *dst,
                                size_t size,
@@ -131,7 +125,12 @@ public:
                                 pid_t pid,
                                 AddrSpace as,
                                 const location &loc);
-  Value *CreateStrncmp(Value *val1, Value *val2, uint64_t n, bool inverse);
+  Value *CreateStrncmp(Value *str1,
+                       uint64_t str1_size,
+                       Value *str2,
+                       uint64_t str2_size,
+                       uint64_t n,
+                       bool inverse);
   CallInst *CreateGetNs(bool boot_time, const location &loc);
   CallInst *CreateGetPidTgid(const location &loc);
   CallInst *CreateGetCurrentCgroupId(const location &loc);
@@ -147,7 +146,10 @@ public:
                              ArrayRef<Value *> args,
                              const Twine &Name,
                              const location *loc = nullptr);
-  CallInst   *createCall(Value *callee, ArrayRef<Value *> args, const Twine &Name);
+  CallInst *createCall(FunctionType *callee_type,
+                       Value *callee,
+                       ArrayRef<Value *> args,
+                       const Twine &Name);
   void        CreateGetCurrentComm(Value *ctx, AllocaInst *buf, size_t size, const location& loc);
   void CreatePerfEventOutput(Value *ctx,
                              Value *data,
@@ -205,9 +207,6 @@ private:
                                 AddrSpace as,
                                 const location &loc);
   CallInst *createMapLookup(int mapid, Value *key);
-  Constant *createProbeReadStrFn(llvm::Type *dst,
-                                 llvm::Type *src,
-                                 AddrSpace as);
   libbpf::bpf_func_id selectProbeReadHelper(AddrSpace as, bool str);
 
   llvm::Type *getKernelPointerStorageTy();
diff --git a/src/ast/passes/codegen_llvm.cpp b/src/ast/passes/codegen_llvm.cpp
index d4a5e1c7..0703d196 100644
--- a/src/ast/passes/codegen_llvm.cpp
+++ b/src/ast/passes/codegen_llvm.cpp
@@ -1152,8 +1152,12 @@ void CodegenLLVM::visit(Call &call)
     auto left_string = getString(left_arg);
     auto right_string = getString(right_arg);
 
-    expr_ = b_.CreateStrncmp(
-        left_string.first, right_string.first, size, false);
+    expr_ = b_.CreateStrncmp(left_string.first,
+                             left_string.second,
+                             right_string.first,
+                             right_string.second,
+                             size,
+                             false);
   }
   else if (call.func == "override")
   {
@@ -1284,8 +1288,7 @@ void CodegenLLVM::visit(Variable &var)
   else
   {
     auto *var_alloca = variables_[var.ident];
-    expr_ = b_.CreateLoad(var_alloca->getType()->getPointerElementType(),
-                          var_alloca);
+    expr_ = b_.CreateLoad(var_alloca->getAllocatedType(), var_alloca);
   }
 }
 
@@ -1325,7 +1328,12 @@ void CodegenLLVM::binop_string(Binop &binop)
   auto right_string = getString(binop.right);
 
   size_t len = std::min(left_string.second, right_string.second);
-  expr_ = b_.CreateStrncmp(left_string.first, right_string.first, len, inverse);
+  expr_ = b_.CreateStrncmp(left_string.first,
+                           left_string.second,
+                           right_string.first,
+                           right_string.second,
+                           len,
+                           inverse);
 }
 
 void CodegenLLVM::binop_buf(Binop &binop)
@@ -1349,7 +1357,12 @@ void CodegenLLVM::binop_buf(Binop &binop)
 
   size_t len = std::min(binop.left->type.GetSize(),
                         binop.right->type.GetSize());
-  expr_ = b_.CreateStrncmp(left_string, right_string, len, inverse);
+  expr_ = b_.CreateStrncmp(left_string,
+                           binop.left->type.GetSize(),
+                           right_string,
+                           binop.right->type.GetSize(),
+                           len,
+                           inverse);
 }
 
 void CodegenLLVM::binop_int(Binop &binop)
@@ -3498,9 +3511,8 @@ void CodegenLLVM::createIncDec(Unop &unop)
   else if (unop.expr->is_variable)
   {
     Variable &var = static_cast<Variable &>(*unop.expr);
-    Value *oldval = b_.CreateLoad(
-        variables_[var.ident]->getType()->getPointerElementType(),
-        variables_[var.ident]);
+    Value *oldval = b_.CreateLoad(variables_[var.ident]->getAllocatedType(),
+                                  variables_[var.ident]);
     Value *newval;
     if (is_increment)
       newval = b_.CreateAdd(oldval, b_.GetIntSameSize(step, oldval));