[DTrace-devel] [PATCH] Provide set_upper_bound() macro for C-to-BPF source code

Kris Van Hees kris.van.hees at oracle.com
Thu Jul 29 20:39:07 PDT 2021


The BPF verifier obtains (and needs) information about upper and  lower
bounds for values based on conditional clauses.  The BPF GCC cross
compiler generates conditionals in a variety of forms.  The BPF verifier
sypports only a limited set of conditionals, so we need to be able to
enforce a specific form.  More specifically, the BPF verifier requires
the bound (a constant value) to be stored in the src register while the
value to be validated is in the dst register.

Signed-off-by: Kris Van Hees <kris.van.hees at oracle.com>
---
 include/bpf-lib.h | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
 create mode 100644 include/bpf-lib.h

diff --git a/include/bpf-lib.h b/include/bpf-lib.h
new file mode 100644
index 00000000..70783fe0
--- /dev/null
+++ b/include/bpf-lib.h
@@ -0,0 +1,30 @@
+/*
+ * Oracle Linux DTrace.
+ * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
+ * Licensed under the Universal Permissive License v 1.0 as shown at
+ * http://oss.oracle.com/licenses/upl.
+ */
+
+#ifndef BPF_LIB_H
+#define BPF_LIB_H
+
+/*
+ * Explicit inline assembler to implement an upper bound check:
+ *
+ *	if (var > bnd)
+ *		var = bnd;
+ *
+ * The BPF GCC compiler does not guarantee that the bound (expected to be a
+ * variable that holds a constant value) will be encoded in the source register
+ * while the BPF verifier does require that (for now).
+ */
+#define set_upper_bound(var, bnd) \
+	asm ("jle %0, %1, 1f\n\t" \
+	     "mov %0, %1\n\t" \
+	     "1:" \
+		: "+r" (var) \
+		: "r" (bnd) \
+		: /* no clobbers */ \
+	);
+
+#endif /* BPF_LIB_H */
-- 
2.32.0




More information about the DTrace-devel mailing list