Use absl::Span in BPF disassembler

PiperOrigin-RevId: 369636095
Change-Id: I13a8ae08ba354e54c502e0f6cdd35287fdfbb723
This commit is contained in:
Wiktor Garbacz 2021-04-21 05:32:48 -07:00 committed by Copybara-Service
parent 4344bbceba
commit d9824dff16
4 changed files with 11 additions and 4 deletions

View File

@ -29,7 +29,11 @@ cc_library(
srcs = ["bpfdisassembler.cc"], srcs = ["bpfdisassembler.cc"],
hdrs = ["bpfdisassembler.h"], hdrs = ["bpfdisassembler.h"],
copts = sapi_platform_copts(), copts = sapi_platform_copts(),
deps = ["@com_google_absl//absl/strings"], visibility = ["//visibility:public"],
deps = [
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:span",
],
) )
cc_library( cc_library(

View File

@ -24,6 +24,7 @@ add_library(sandbox2_bpfdisassembler ${SAPI_LIB_TYPE}
) )
add_library(sandbox2::bpfdisassembler ALIAS sandbox2_bpfdisassembler) add_library(sandbox2::bpfdisassembler ALIAS sandbox2_bpfdisassembler)
target_link_libraries(sandbox2_bpfdisassembler PRIVATE target_link_libraries(sandbox2_bpfdisassembler PRIVATE
absl::span
absl::strings absl::strings
sapi::base sapi::base
) )

View File

@ -234,9 +234,9 @@ std::string DecodeInstruction(const sock_filter& inst, int pc) {
} }
} }
std::string Disasm(const std::vector<sock_filter>& prog) { std::string Disasm(absl::Span<const sock_filter> prog) {
std::string rv; std::string rv;
for (size_t i = 0, len = prog.size(); i < len; ++i) { for (size_t i = 0; i < prog.size(); ++i) {
absl::StrAppend(&rv, absl::Dec(i, absl::kZeroPad3), ": ", absl::StrAppend(&rv, absl::Dec(i, absl::kZeroPad3), ": ",
DecodeInstruction(prog[i], i), "\n"); DecodeInstruction(prog[i], i), "\n");
} }

View File

@ -18,6 +18,8 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "absl/types/span.h"
struct sock_filter; struct sock_filter;
namespace sandbox2 { namespace sandbox2 {
@ -28,7 +30,7 @@ std::string DecodeInstruction(const sock_filter& inst, int pc);
// Disassembles a BPF program. // Disassembles a BPF program.
// Returns a human-readable textual represenation. // Returns a human-readable textual represenation.
std::string Disasm(const std::vector<sock_filter>& prog); std::string Disasm(absl::Span<const sock_filter> prog);
} // namespace bpf } // namespace bpf
} // namespace sandbox2 } // namespace sandbox2