From 1eb9b60c13cb45ca87af224b52c8b07d002e971f Mon Sep 17 00:00:00 2001 From: Julian P Samaroo Date: Thu, 17 Dec 2020 15:20:16 -0600 Subject: [PATCH] Add ubpf support via UBPF_jll --- Manifest.toml | 261 ----------------------------------------------- Project.toml | 2 + src/BPFnative.jl | 6 ++ src/ubpf.jl | 75 ++++++++++++++ test/runtests.jl | 50 ++++++++- 5 files changed, 132 insertions(+), 262 deletions(-) delete mode 100644 Manifest.toml create mode 100644 src/ubpf.jl diff --git a/Manifest.toml b/Manifest.toml deleted file mode 100644 index 58f163f..0000000 --- a/Manifest.toml +++ /dev/null @@ -1,261 +0,0 @@ -# This file is machine-generated - editing it directly is not advised - -[[ArgTools]] -uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f" - -[[Artifacts]] -uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33" - -[[Base64]] -uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" - -[[Bzip2_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] -git-tree-sha1 = "c3598e525718abcc440f69cc6d5f60dda0a1b61e" -uuid = "6e34b625-4abd-537c-b88f-471c36dfa7a0" -version = "1.0.6+5" - -[[CBinding]] -deps = ["Clang_jll", "Libdl", "Markdown", "Scratch"] -git-tree-sha1 = "78ef620748d8206e53e776a3f7eea0485896d178" -uuid = "d43a6710-96b8-4a2d-833c-c424785e5374" -version = "1.0.2" - -[[CEnum]] -git-tree-sha1 = "215a9aa4a1f23fbd05b92769fdd62559488d70e9" -uuid = "fa961155-64e5-5f13-b03f-caf6b980ea82" -version = "0.4.1" - -[[Clang_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "libLLVM_jll"] -git-tree-sha1 = "a5923c06de3178dd755f4b9411ea8922a7ae6fb8" -uuid = "0ee61d77-7f21-5576-8119-9fcc46b10100" -version = "11.0.1+3" - -[[Compat]] -deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"] -git-tree-sha1 = "ac4132ad78082518ec2037ae5770b6e796f7f956" -uuid = "34da2185-b29b-5c13-b0c7-acf172513d20" -version = "3.27.0" - -[[DataStructures]] -deps = ["Compat", "InteractiveUtils", "OrderedCollections"] -git-tree-sha1 = "4437b64df1e0adccc3e5d1adbc3ac741095e4677" -uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" -version = "0.18.9" - -[[Dates]] -deps = ["Printf"] -uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" - -[[DelimitedFiles]] -deps = ["Mmap"] -uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab" - -[[Distributed]] -deps = ["Random", "Serialization", "Sockets"] -uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b" - -[[Downloads]] -deps = ["ArgTools", "LibCURL", "NetworkOptions"] -uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6" - -[[Elfutils_jll]] -deps = ["Artifacts", "Bzip2_jll", "JLLWrappers", "Libdl", "Pkg", "XZ_jll", "Zlib_jll", "argp_standalone_jll", "fts_jll", "obstack_jll"] -git-tree-sha1 = "76cbf1134983cfb371ad77117bb2659600ed64d6" -uuid = "ab5a07f8-06af-567f-a878-e8bb879eba5a" -version = "0.179.0+0" - -[[ExprTools]] -git-tree-sha1 = "10407a39b87f29d47ebaca8edbc75d7c302ff93e" -uuid = "e2ba6199-217a-4e67-a87a-7c52f15ade04" -version = "0.1.3" - -[[GPUCompiler]] -deps = ["DataStructures", "ExprTools", "InteractiveUtils", "LLVM", "Libdl", "Logging", "Scratch", "Serialization", "TimerOutputs", "UUIDs"] -git-tree-sha1 = "7f13030524614d90c876332c980fd1edef2331e8" -uuid = "61eb1bfa-7361-4325-ad38-22787b887f55" -version = "0.11.3" - -[[InteractiveUtils]] -deps = ["Markdown"] -uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" - -[[JLLWrappers]] -git-tree-sha1 = "a431f5f2ca3f4feef3bd7a5e94b8b8d4f2f647a0" -uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210" -version = "1.2.0" - -[[LLVM]] -deps = ["CEnum", "Libdl", "Printf", "Unicode"] -git-tree-sha1 = "b616937c31337576360cb9fb872ec7633af7b194" -uuid = "929cbde3-209d-540e-8aea-75f648917ca0" -version = "3.6.0" - -[[LibCURL]] -deps = ["LibCURL_jll", "MozillaCACerts_jll"] -uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21" - -[[LibCURL_jll]] -deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"] -uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0" - -[[LibGit2]] -deps = ["Base64", "NetworkOptions", "Printf", "SHA"] -uuid = "76f85450-5226-5b5a-8eaa-529ad045b433" - -[[LibSSH2_jll]] -deps = ["Artifacts", "Libdl", "MbedTLS_jll"] -uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8" - -[[Libbpf_jll]] -deps = ["Artifacts", "Elfutils_jll", "JLLWrappers", "Libdl", "Pkg"] -git-tree-sha1 = "00264b33b422d0a85d20210dfc00e2011a31a1ef" -uuid = "02f9a84d-9555-5f1a-8c3c-42027521038d" -version = "0.3.0+0" - -[[Libdl]] -uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" - -[[LinearAlgebra]] -deps = ["Libdl"] -uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" - -[[Logging]] -uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" - -[[Markdown]] -deps = ["Base64"] -uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" - -[[MbedTLS_jll]] -deps = ["Artifacts", "Libdl"] -uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1" - -[[Mmap]] -uuid = "a63ad114-7e13-5084-954f-fe012c677804" - -[[MozillaCACerts_jll]] -uuid = "14a3606d-f60d-562e-9121-12d972cd8159" - -[[NetworkOptions]] -uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908" - -[[OrderedCollections]] -git-tree-sha1 = "4fa2ba51070ec13fcc7517db714445b4ab986bdf" -uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" -version = "1.4.0" - -[[Pkg]] -deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"] -uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" - -[[Preferences]] -deps = ["TOML"] -git-tree-sha1 = "ea79e4c9077208cd3bc5d29631a26bc0cff78902" -uuid = "21216c6a-2e73-6563-6e65-726566657250" -version = "1.2.1" - -[[Printf]] -deps = ["Unicode"] -uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" - -[[REPL]] -deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"] -uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" - -[[Random]] -deps = ["Serialization"] -uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" - -[[SHA]] -uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" - -[[Scratch]] -deps = ["Dates"] -git-tree-sha1 = "ad4b278adb62d185bbcb6864dc24959ab0627bf6" -uuid = "6c6a2e73-6563-6170-7368-637461726353" -version = "1.0.3" - -[[Serialization]] -uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" - -[[SharedArrays]] -deps = ["Distributed", "Mmap", "Random", "Serialization"] -uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383" - -[[Sockets]] -uuid = "6462fe0b-24de-5631-8697-dd941f90decc" - -[[SparseArrays]] -deps = ["LinearAlgebra", "Random"] -uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" - -[[Statistics]] -deps = ["LinearAlgebra", "SparseArrays"] -uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" - -[[TOML]] -deps = ["Dates"] -uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76" - -[[Tar]] -deps = ["ArgTools", "SHA"] -uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e" - -[[Test]] -deps = ["InteractiveUtils", "Logging", "Random", "Serialization"] -uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" - -[[TimerOutputs]] -deps = ["Printf"] -git-tree-sha1 = "32cdbe6cd2d214c25a0b88f985c9e0092877c236" -uuid = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f" -version = "0.5.8" - -[[UUIDs]] -deps = ["Random", "SHA"] -uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" - -[[Unicode]] -uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" - -[[XZ_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] -git-tree-sha1 = "9f76853ea2ba894054e24640abfb73d73e5a4cb5" -uuid = "ffd25f8a-64ca-5728-b0f7-c24cf3aae800" -version = "5.2.5+0" - -[[Zlib_jll]] -deps = ["Libdl"] -uuid = "83775a58-1f1d-513f-b197-d71354ab007a" - -[[argp_standalone_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] -git-tree-sha1 = "c4fa3457046fc93249b63e8319e743b6c8590609" -uuid = "c53206cc-00f7-50bf-ad1e-3ae1f6e49bc3" -version = "1.3.0+0" - -[[fts_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] -git-tree-sha1 = "fa3d234bbbad588979c5409eee29e7967b6ed8a6" -uuid = "d65627f6-89bd-53e8-8ab5-8b75ff535eee" -version = "1.2.7+0" - -[[libLLVM_jll]] -deps = ["Artifacts", "Libdl"] -uuid = "8f36deef-c2a5-5394-99ed-8e07531fb29a" - -[[nghttp2_jll]] -deps = ["Artifacts", "Libdl"] -uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d" - -[[obstack_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] -git-tree-sha1 = "c417cbbce3efb49a309b2d478fea9df7cfe024c0" -uuid = "c88a4935-d25e-5644-aacc-5db6f1b8ef79" -version = "1.1.0+0" - -[[p7zip_jll]] -deps = ["Artifacts", "Libdl"] -uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0" diff --git a/Project.toml b/Project.toml index 7fc0a55..d93f8bc 100644 --- a/Project.toml +++ b/Project.toml @@ -12,6 +12,7 @@ Libbpf_jll = "02f9a84d-9555-5f1a-8c3c-42027521038d" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" Preferences = "21216c6a-2e73-6563-6e65-726566657250" Sockets = "6462fe0b-24de-5631-8697-dd941f90decc" +UBPF_jll = "502467ad-4a4a-57e4-9860-6b433130b33f" [compat] CBinding = "1" @@ -19,6 +20,7 @@ GPUCompiler = "0.11" LLVM = "3.6" Libbpf_jll = "0.3" Preferences = "1" +UBPF_jll = "0.0.2" julia = "1.6" [extras] diff --git a/src/BPFnative.jl b/src/BPFnative.jl index 966c368..392468a 100644 --- a/src/BPFnative.jl +++ b/src/BPFnative.jl @@ -30,6 +30,12 @@ function enable_vmlinux(ans::Bool=true) end end +# ubpf VM +module UBPF +using UBPF_jll +include("ubpf.jl") +end + # Common API module API if !parse(Bool, get(ENV, "JULIA_BPFNATIVE_DISABLE_ARTIFACTS", "0")) diff --git a/src/ubpf.jl b/src/ubpf.jl new file mode 100644 index 0000000..8f3bb3c --- /dev/null +++ b/src/ubpf.jl @@ -0,0 +1,75 @@ +export call, verify, load_elf + +struct ebpf_inst + opcode::UInt8 + dst_src::UInt8 + offset::Int16 + imm::Int32 +end +const ubpf_jit_fn = Ptr{Cvoid} +const ext_func = Ptr{Cvoid} +Base.@kwdef struct ubpf_vm + insts::Ptr{ebpf_inst} + num_insts::UInt16 + jitted::ubpf_jit_fn = C_NULL + jitted_size::Csize_t = 0 + ext_funcs::Ptr{ext_func} = C_NULL + ext_func_names::Ptr{Cstring} = C_NULL + bounds_check_enabled::Bool = true +end +ubpf_vm(exe::Vector{ebpf_inst}; kwargs...) = + ubpf_vm(pointer(exe), length(exe); kwargs...) +ubpf_vm(exe::Vector{UInt8}; kwargs...) = + ubpf_vm(reinterpret(Ptr{ebpf_inst}, pointer(exe)), + div(length(exe),sizeof(ebpf_inst)); + kwargs...) + +call(exe::Vector{UInt8}, mem::Vector{UInt8}; kwargs...) = + call(collect(reinterpret(ebpf_inst, exe)), mem; kwargs...) +function call(exe::Vector{ebpf_inst}, mem::Vector{UInt8}; kwargs...) + GC.@preserve exe mem begin + vm = ubpf_vm(exe; kwargs...) + if verify(vm) == 0 + unsafe_call(vm, mem) + else + error("BPF verification failed") + end + end +end +function verify(vm::ubpf_vm) + vm_ref = Ref(vm) + GC.@preserve vm_ref begin + ccall((:ubpf_verify, libubpf), Cint, (Ptr{ubpf_vm},), vm_ref) + end +end +function unsafe_call(vm::ubpf_vm, mem::Union{<:Ptr,<:Integer}, mem_len::Integer) + vm_ref = Ref(vm) + mem = sizeof(mem) < sizeof(Ptr{Cvoid}) ? UInt(mem) : mem + GC.@preserve vm_ref mem begin + ccall((:ubpf_exec, libubpf), UInt64, + (Ptr{ubpf_vm}, Ptr{Cvoid}, Csize_t), + vm_ref, reinterpret(Ptr{Cvoid}, mem), mem_len) + end +end +function unsafe_call(vm::ubpf_vm, mem::Vector{UInt8}) + vm_ref = Ref(vm) + GC.@preserve vm_ref mem begin + ccall((:ubpf_exec, libubpf), UInt64, + (Ptr{ubpf_vm}, Ptr{Cvoid}, Csize_t), + vm_ref, mem, length(mem)) + end +end +function load_elf(vm::ubpf_vm, exe::Vector{UInt8}) + vm_ref = Ref(vm) + errmsg = Ref{Cstring}() + ret = GC.@preserve vm_ref errmsg begin + ccall((:ubpf_load_elf, libubpf), Cint, + (Ptr{ubpf_vm}, Ptr{UInt8}, Csize_t, Ptr{Cstring}), + vm_ref, pointer(exe), length(exe), errmsg) + end + if ret != 0 + error(unsafe_string(errmsg[])) + end + vm_ref[] +end +load_elf(path::String) = load_elf(read(path)) diff --git a/test/runtests.jl b/test/runtests.jl index a516169..364033a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,5 +1,5 @@ using BPFnative -import BPFnative: API, RT, Host +import BPFnative: API, RT, Host, UBPF using Sockets using Test @@ -22,6 +22,33 @@ end using InteractiveUtils +if !isdefined(BPFnative, :libubpf) + @warn "ubpf unavailable; skipping interpreter tests" +end +function ubpftest(fn, Targs, arg) + exe = bpffunction(fn, Targs; btf=false, name="kernel") + vm = UBPF.ubpf_vm(;insts=C_NULL, num_insts=0) + vm = UBPF.load_elf(vm, exe) + if UBPF.verify(vm) != 0 + error("eBPF verifier error during test") + end + if arg === nothing + UBPF.unsafe_call(vm, C_NULL, 0) + else + UBPF.unsafe_call(vm, arg, sizeof(arg)) + end +end +macro ubpftest(call) + if isdefined(BPFnative, :libubpf) + @assert Meta.isexpr(call, :call) "@ubpftest expects a call" + @assert length(call.args) <= 2 "Too many call arguments, 1 expected" + fn = call.args[1] + arg = get(call.args, 2, nothing) + Targ = arg !== nothing ? Base.to_tuple_type(typeof.((arg,))) : Tuple{} + :(ubpftest($(esc(fn)), $Targ, $arg)) + end +end + @testset "codegen" begin map_types = (UInt8, UInt16, UInt32, UInt64, Int8, Int16, Int32, Int64) @@ -33,6 +60,27 @@ using InteractiveUtils @test occursin(".section\tlicense,", asm) @test occursin("_license,@object", asm) @test occursin(".asciz\t\"abc\"", asm) + @test @ubpftest(kernel(0)) == 0 + end + @testset "verifier errors" begin + @testset "loop" begin + function kernel(x) + while true end + 0 + end + @test_throws ErrorException @ubpftest(kernel(0)) + end + end + @info "uBPF error messages during testing are OK" + @testset "runtime errors" begin + @testset "out-of-bounds load" begin + kernel(x) = unsafe_load(Ptr{UInt8}(1)) + @test @ubpftest(kernel(0)) == typemax(UInt) + end + end + @testset "argument usage" begin + kernel(x) = x+1 + @test @ubpftest(kernel(1)) == 2 end @testset "map helpers" begin function kernel(x)