From d9824dff16a93c416c669299def9f7e34baa5703 Mon Sep 17 00:00:00 2001 From: Wiktor Garbacz Date: Wed, 21 Apr 2021 05:32:48 -0700 Subject: [PATCH] Use absl::Span in BPF disassembler PiperOrigin-RevId: 369636095 Change-Id: I13a8ae08ba354e54c502e0f6cdd35287fdfbb723 --- sandboxed_api/sandbox2/BUILD.bazel | 6 +++++- sandboxed_api/sandbox2/CMakeLists.txt | 1 + sandboxed_api/sandbox2/bpfdisassembler.cc | 4 ++-- sandboxed_api/sandbox2/bpfdisassembler.h | 4 +++- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/sandboxed_api/sandbox2/BUILD.bazel b/sandboxed_api/sandbox2/BUILD.bazel index aa40229..1dbaad8 100644 --- a/sandboxed_api/sandbox2/BUILD.bazel +++ b/sandboxed_api/sandbox2/BUILD.bazel @@ -29,7 +29,11 @@ cc_library( srcs = ["bpfdisassembler.cc"], hdrs = ["bpfdisassembler.h"], 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( diff --git a/sandboxed_api/sandbox2/CMakeLists.txt b/sandboxed_api/sandbox2/CMakeLists.txt index 14c9c80..918eda0 100644 --- a/sandboxed_api/sandbox2/CMakeLists.txt +++ b/sandboxed_api/sandbox2/CMakeLists.txt @@ -24,6 +24,7 @@ add_library(sandbox2_bpfdisassembler ${SAPI_LIB_TYPE} ) add_library(sandbox2::bpfdisassembler ALIAS sandbox2_bpfdisassembler) target_link_libraries(sandbox2_bpfdisassembler PRIVATE + absl::span absl::strings sapi::base ) diff --git a/sandboxed_api/sandbox2/bpfdisassembler.cc b/sandboxed_api/sandbox2/bpfdisassembler.cc index 0b87d1f..f533a3e 100644 --- a/sandboxed_api/sandbox2/bpfdisassembler.cc +++ b/sandboxed_api/sandbox2/bpfdisassembler.cc @@ -234,9 +234,9 @@ std::string DecodeInstruction(const sock_filter& inst, int pc) { } } -std::string Disasm(const std::vector& prog) { +std::string Disasm(absl::Span prog) { 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), ": ", DecodeInstruction(prog[i], i), "\n"); } diff --git a/sandboxed_api/sandbox2/bpfdisassembler.h b/sandboxed_api/sandbox2/bpfdisassembler.h index a7bcad9..0868dd3 100644 --- a/sandboxed_api/sandbox2/bpfdisassembler.h +++ b/sandboxed_api/sandbox2/bpfdisassembler.h @@ -18,6 +18,8 @@ #include #include +#include "absl/types/span.h" + struct sock_filter; namespace sandbox2 { @@ -28,7 +30,7 @@ std::string DecodeInstruction(const sock_filter& inst, int pc); // Disassembles a BPF program. // Returns a human-readable textual represenation. -std::string Disasm(const std::vector& prog); +std::string Disasm(absl::Span prog); } // namespace bpf } // namespace sandbox2