Skip to content

Commit a0db71f

Browse files
authored
build: add support for Clang-Tidy tool. (proxy-wasm#263)
Signed-off-by: Piotr Sikora <[email protected]>
1 parent 21976e4 commit a0db71f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+552
-405
lines changed

Diff for: .bazelrc

+5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ build:clang-tsan --copt -fsanitize=thread
4040
build:clang-tsan --linkopt -fsanitize=thread
4141
build:clang-tsan --linkopt -fuse-ld=lld
4242

43+
# Use Clang-Tidy tool.
44+
build:clang-tidy --aspects @bazel_clang_tidy//clang_tidy:clang_tidy.bzl%clang_tidy_aspect
45+
build:clang-tidy --@bazel_clang_tidy//:clang_tidy_config=@proxy_wasm_cpp_host//:clang_tidy_config
46+
build:clang-tidy --output_groups=report
47+
4348
# Use GCC compiler.
4449
build:gcc --action_env=BAZEL_COMPILER=gcc
4550
build:gcc --action_env=CC=gcc

Diff for: .clang-tidy

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Checks: clang-*,
2+
-clang-analyzer-optin.portability.UnixAPI,
3+
-clang-analyzer-unix.Malloc,
4+
-clang-diagnostic-pragma-once-outside-header,
5+
cppcoreguidelines-pro-type-member-init,
6+
cppcoreguidelines-pro-type-static-cast-downcast,
7+
misc-*,
8+
-misc-non-private-member-variables-in-classes,
9+
modernize-*,
10+
-modernize-avoid-c-arrays,
11+
-modernize-use-trailing-return-type,
12+
llvm-include-order,
13+
performance-*,
14+
-performance-no-int-to-ptr,
15+
portability-*,
16+
readability-*,
17+
-readability-convert-member-functions-to-static,
18+
-readability-function-cognitive-complexity,
19+
-readability-magic-numbers,
20+
-readability-make-member-function-const,
21+
-readability-simplify-boolean-expr,
22+
23+
WarningsAsErrors: '*'

Diff for: .github/workflows/format.yml

+61
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ on:
2828
- 'envoy-release/**'
2929
- 'istio-release/**'
3030

31+
schedule:
32+
- cron: '0 0 * * *'
33+
3134
jobs:
3235

3336
addlicense:
@@ -105,3 +108,61 @@ jobs:
105108
run: |
106109
find . -name "*.h" -o -name "*.cc" -o -name "*.proto" | grep -v ".pb." | xargs -n1 clang-format-12 -i
107110
git diff --exit-code
111+
112+
clang_tidy:
113+
name: check format with clang-tidy
114+
115+
runs-on: ubuntu-20.04
116+
117+
steps:
118+
- uses: actions/checkout@v2
119+
120+
- name: Install dependencies (Linux)
121+
run: sudo apt-get install -y clang-tidy-12
122+
123+
- name: Bazel cache
124+
uses: PiotrSikora/[email protected]
125+
with:
126+
path: |
127+
~/.cache/bazel
128+
key: clang_tidy-${{ hashFiles('WORKSPACE', '.bazelrc', '.bazelversion', 'bazel/dependencies.bzl', 'bazel/repositories.bzl', 'bazel/cargo/wasmsign/crates.bzl') }}
129+
130+
- name: Bazel build
131+
run: >
132+
bazel build
133+
--config clang-tidy
134+
--define engine=multi
135+
--copt=-DPROXY_WASM_VERIFY_WITH_ED25519_PUBKEY=\"$(xxd -p -c 256 test/test_data/signature_key1.pub | cut -b9-)\"
136+
//...
137+
138+
- name: Skip Bazel cache update
139+
if: ${{ github.ref != 'refs/heads/master' }}
140+
run: echo "CACHE_SKIP_SAVE=true" >> $GITHUB_ENV
141+
142+
- name: Cleanup Bazel cache
143+
if: ${{ github.ref == 'refs/heads/master' }}
144+
run: |
145+
export OUTPUT=$(${{ matrix.run_under }} bazel info output_base)
146+
echo "===== BEFORE ====="
147+
du -s ${OUTPUT}/external/* $(dirname ${OUTPUT})/* | sort -rn | head -20
148+
# BoringSSL's test data (90 MiB).
149+
rm -rf ${OUTPUT}/external/boringssl/crypto_test_data.cc
150+
rm -rf ${OUTPUT}/external/boringssl/src/crypto/*/test/
151+
rm -rf ${OUTPUT}/external/boringssl/src/third_party/wycheproof_testvectors/
152+
# LLVM's tests (500 MiB).
153+
rm -rf ${OUTPUT}/external/llvm*/test/
154+
# V8's tests (100 MiB).
155+
if [ -d "${OUTPUT}/external/v8/test/torque" ]; then
156+
mv ${OUTPUT}/external/v8/test/torque ${OUTPUT}/external/v8/test_torque
157+
rm -rf ${OUTPUT}/external/v8/test/*
158+
mv ${OUTPUT}/external/v8/test_torque ${OUTPUT}/external/v8/test/torque
159+
fi
160+
# Unnecessary CMake tools (65 MiB).
161+
rm -rf ${OUTPUT}/external/cmake-*/bin/{ccmake,cmake-gui,cpack,ctest}
162+
# Distfiles for Rust toolchains (350 MiB).
163+
rm -rf ${OUTPUT}/external/rust_*/*.tar.gz
164+
# Bazel's repository cache (650-800 MiB) and install base (155 MiB).
165+
rm -rf ${OUTPUT}/../cache
166+
rm -rf ${OUTPUT}/../install
167+
echo "===== AFTER ====="
168+
du -s ${OUTPUT}/external/* $(dirname ${OUTPUT})/* | sort -rn | head -20

Diff for: BUILD

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ package(default_visibility = ["//visibility:public"])
1414

1515
exports_files(["LICENSE"])
1616

17+
filegroup(
18+
name = "clang_tidy_config",
19+
data = [".clang-tidy"],
20+
)
21+
1722
cc_library(
1823
name = "wasm_vm_headers",
1924
hdrs = [
@@ -191,7 +196,7 @@ cc_library(
191196
],
192197
hdrs = ["include/proxy-wasm/wavm.h"],
193198
copts = [
194-
'-DWAVM_API=""',
199+
"-DWAVM_API=",
195200
"-Wno-non-virtual-dtor",
196201
"-Wno-old-style-cast",
197202
],

Diff for: bazel/external/bazel_clang_tidy.patch

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# 1. Treat .h files as C++ headers.
2+
# 2. Hardcode clang-tidy-12.
3+
4+
diff --git a/clang_tidy/clang_tidy.bzl b/clang_tidy/clang_tidy.bzl
5+
index 3a5ed07..5db5c6c 100644
6+
--- a/clang_tidy/clang_tidy.bzl
7+
+++ b/clang_tidy/clang_tidy.bzl
8+
@@ -16,6 +16,9 @@ def _run_tidy(ctx, exe, flags, compilation_context, infile, discriminator):
9+
# add source to check
10+
args.add(infile.path)
11+
12+
+ # treat .h files as C++ headers
13+
+ args.add("--extra-arg-before=-xc++")
14+
+
15+
# start args passed to the compiler
16+
args.add("--")
17+
18+
diff --git a/clang_tidy/run_clang_tidy.sh b/clang_tidy/run_clang_tidy.sh
19+
index 582bab1..b9ebb94 100755
20+
--- a/clang_tidy/run_clang_tidy.sh
21+
+++ b/clang_tidy/run_clang_tidy.sh
22+
@@ -11,4 +11,4 @@ shift
23+
touch $OUTPUT
24+
truncate -s 0 $OUTPUT
25+
26+
-clang-tidy "$@"
27+
+clang-tidy-12 "$@"

Diff for: bazel/repositories.bzl

+10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ def proxy_wasm_cpp_host_repositories():
2929
sha256 = "c6966ec828da198c5d9adbaa94c05e3a1c7f21bd012a0b29ba8ddbccb2c93b0d",
3030
)
3131

32+
maybe(
33+
http_archive,
34+
name = "bazel_clang_tidy",
35+
sha256 = "6ed23cbff9423a30ef10becf57210a26d54fe198a211f4037d931c06f843c023",
36+
strip_prefix = "bazel_clang_tidy-c2fe98cfec0430e78bff4169e9ca0a43123e4c99",
37+
url = "https://github.com/erenon/bazel_clang_tidy/archive/c2fe98cfec0430e78bff4169e9ca0a43123e4c99.tar.gz",
38+
patches = ["@proxy_wasm_cpp_host//bazel/external:bazel_clang_tidy.patch"],
39+
patch_args = ["-p1"],
40+
)
41+
3242
maybe(
3343
http_archive,
3444
name = "bazel-zig-cc",

Diff for: include/proxy-wasm/bytecode_util.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class BytecodeUtil {
6969
static bool getStrippedSource(std::string_view bytecode, std::string &ret);
7070

7171
private:
72-
static bool parseVarint(const char *&begin, const char *end, uint32_t &ret);
72+
static bool parseVarint(const char *&pos, const char *end, uint32_t &ret);
7373
};
7474

7575
} // namespace proxy_wasm

Diff for: include/proxy-wasm/context.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ class ContextBase : public RootInterface,
143143
public SharedQueueInterface,
144144
public GeneralInterface {
145145
public:
146-
ContextBase(); // Testing.
147-
ContextBase(WasmBase *wasm); // Vm Context.
148-
ContextBase(WasmBase *wasm, std::shared_ptr<PluginBase> plugin); // Root Context.
146+
ContextBase(); // Testing.
147+
ContextBase(WasmBase *wasm); // Vm Context.
148+
ContextBase(WasmBase *wasm, const std::shared_ptr<PluginBase> &plugin); // Root Context.
149149
ContextBase(WasmBase *wasm, uint32_t parent_context_id,
150-
std::shared_ptr<PluginHandleBase> plugin_handle); // Stream context.
150+
const std::shared_ptr<PluginHandleBase> &plugin_handle); // Stream context.
151151
virtual ~ContextBase();
152152

153153
WasmBase *wasm() const { return wasm_; }
@@ -196,11 +196,11 @@ class ContextBase : public RootInterface,
196196

197197
// HTTP
198198
FilterHeadersStatus onRequestHeaders(uint32_t headers, bool end_of_stream) override;
199-
FilterDataStatus onRequestBody(uint32_t body_buffer_length, bool end_of_stream) override;
199+
FilterDataStatus onRequestBody(uint32_t body_length, bool end_of_stream) override;
200200
FilterTrailersStatus onRequestTrailers(uint32_t trailers) override;
201201
FilterMetadataStatus onRequestMetadata(uint32_t elements) override;
202202
FilterHeadersStatus onResponseHeaders(uint32_t headers, bool end_of_stream) override;
203-
FilterDataStatus onResponseBody(uint32_t body_buffer_length, bool end_of_stream) override;
203+
FilterDataStatus onResponseBody(uint32_t body_length, bool end_of_stream) override;
204204
FilterTrailersStatus onResponseTrailers(uint32_t trailers) override;
205205
FilterMetadataStatus onResponseMetadata(uint32_t elements) override;
206206

@@ -341,7 +341,7 @@ class ContextBase : public RootInterface,
341341
WasmResult registerSharedQueue(std::string_view queue_name,
342342
SharedQueueDequeueToken *token_ptr) override;
343343
WasmResult lookupSharedQueue(std::string_view vm_id, std::string_view queue_name,
344-
SharedQueueEnqueueToken *token) override;
344+
SharedQueueEnqueueToken *token_ptr) override;
345345
WasmResult dequeueSharedQueue(uint32_t token, std::string *data) override;
346346
WasmResult enqueueSharedQueue(uint32_t token, std::string_view value) override;
347347

Diff for: include/proxy-wasm/context_interface.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ struct HttpInterface {
206206
virtual FilterHeadersStatus onRequestHeaders(uint32_t headers, bool end_of_stream) = 0;
207207

208208
// Call on a stream context to indicate that body data has arrived.
209-
virtual FilterDataStatus onRequestBody(uint32_t body_buffer_length, bool end_of_stream) = 0;
209+
virtual FilterDataStatus onRequestBody(uint32_t body_length, bool end_of_stream) = 0;
210210

211211
// Call on a stream context to indicate that the request trailers have arrived.
212212
virtual FilterTrailersStatus onRequestTrailers(uint32_t trailers) = 0;
@@ -218,7 +218,7 @@ struct HttpInterface {
218218
virtual FilterHeadersStatus onResponseHeaders(uint32_t trailers, bool end_of_stream) = 0;
219219

220220
// Call on a stream context to indicate that body data has arrived.
221-
virtual FilterDataStatus onResponseBody(uint32_t body_buffer_length, bool end_of_stream) = 0;
221+
virtual FilterDataStatus onResponseBody(uint32_t body_length, bool end_of_stream) = 0;
222222

223223
// Call on a stream context to indicate that the request trailers have arrived.
224224
virtual FilterTrailersStatus onResponseTrailers(uint32_t trailers) = 0;

Diff for: include/proxy-wasm/exports.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ struct RegisterForeignFunction {
5757
* @param function_name is the key for this foreign function.
5858
* @param f is the function instance.
5959
*/
60-
RegisterForeignFunction(std::string function_name, WasmForeignFunction f);
60+
RegisterForeignFunction(const std::string &function_name, WasmForeignFunction f);
6161
};
6262

6363
namespace exports {
@@ -94,8 +94,8 @@ template <typename Pairs> void marshalPairs(const Pairs &result, char *buffer) {
9494

9595
// ABI functions exported from host to wasm.
9696

97-
Word get_configuration(Word address, Word size);
98-
Word get_status(Word status_code, Word address, Word size);
97+
Word get_configuration(Word value_ptr_ptr, Word value_size_ptr);
98+
Word get_status(Word code_ptr, Word value_ptr_ptr, Word value_size_ptr);
9999
Word log(Word level, Word address, Word size);
100100
Word get_log_level(Word result_level_uint32_ptr);
101101
Word get_property(Word path_ptr, Word path_size, Word value_ptr_ptr, Word value_size_ptr);
@@ -134,7 +134,7 @@ Word get_response_body_buffer_bytes(Word start, Word length, Word ptr_ptr, Word
134134
Word http_call(Word uri_ptr, Word uri_size, Word header_pairs_ptr, Word header_pairs_size,
135135
Word body_ptr, Word body_size, Word trailer_pairs_ptr, Word trailer_pairs_size,
136136
Word timeout_milliseconds, Word token_ptr);
137-
Word define_metric(Word metric_type, Word name_ptr, Word name_size, Word result_ptr);
137+
Word define_metric(Word metric_type, Word name_ptr, Word name_size, Word metric_id_ptr);
138138
Word increment_metric(Word metric_id, int64_t offset);
139139
Word record_metric(Word metric_id, uint64_t value);
140140
Word get_metric(Word metric_id, Word result_uint64_ptr);

Diff for: include/proxy-wasm/null_plugin.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class NullPlugin : public NullVmPlugin {
7171
void onForeignFunction(uint64_t root_context_id, uint64_t foreign_function_id,
7272
uint64_t data_size);
7373

74-
void onCreate(uint64_t context_id, uint64_t root_context_id);
74+
void onCreate(uint64_t context_id, uint64_t parent_context_id);
7575

7676
uint64_t onNewConnection(uint64_t context_id);
7777
uint64_t onDownstreamData(uint64_t context_id, uint64_t data_length, uint64_t end_of_stream);

Diff for: include/proxy-wasm/null_vm.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ struct NullVm : public WasmVm {
3535
std::string_view getEngineName() override { return "null"; }
3636
Cloneable cloneable() override { return Cloneable::InstantiatedModule; };
3737
std::unique_ptr<WasmVm> clone() override;
38-
bool load(std::string_view bytecode, std::string_view precompiled,
39-
std::unordered_map<uint32_t, std::string> function_names) override;
38+
bool load(std::string_view plugin_name, std::string_view precompiled,
39+
const std::unordered_map<uint32_t, std::string> &function_names) override;
4040
bool link(std::string_view debug_name) override;
4141
uint64_t getMemorySize() override;
4242
std::optional<std::string_view> getMemory(uint64_t pointer, uint64_t size) override;

Diff for: include/proxy-wasm/vm_id_handle.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ class VmIdHandle {
3131
};
3232

3333
std::shared_ptr<VmIdHandle> getVmIdHandle(std::string_view vm_id);
34-
void registerVmIdHandleCallback(std::function<void(std::string_view vm_id)> f);
34+
void registerVmIdHandleCallback(const std::function<void(std::string_view vm_id)> &f);
3535

3636
} // namespace proxy_wasm

Diff for: include/proxy-wasm/wasm.h

+12-10
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ class WasmBase : public std::enable_shared_from_this<WasmBase> {
5252
std::string_view vm_configuration, std::string_view vm_key,
5353
std::unordered_map<std::string, std::string> envs,
5454
AllowedCapabilitiesMap allowed_capabilities);
55-
WasmBase(const std::shared_ptr<WasmHandleBase> &other, WasmVmFactory factory);
55+
WasmBase(const std::shared_ptr<WasmHandleBase> &base_wasm_handle, const WasmVmFactory &factory);
5656
virtual ~WasmBase();
5757

5858
bool load(const std::string &code, bool allow_precompiled = false);
5959
bool initialize();
6060
void startVm(ContextBase *root_context);
6161
bool configure(ContextBase *root_context, std::shared_ptr<PluginBase> plugin);
6262
// Returns the root ContextBase or nullptr if onStart returns false.
63-
ContextBase *start(std::shared_ptr<PluginBase> plugin);
63+
ContextBase *start(const std::shared_ptr<PluginBase> &plugin);
6464

6565
std::string_view vm_id() const { return vm_id_; }
6666
std::string_view vm_key() const { return vm_key_; }
@@ -340,11 +340,13 @@ using WasmHandleCloneFactory =
340340
std::function<std::shared_ptr<WasmHandleBase>(std::shared_ptr<WasmHandleBase> wasm)>;
341341

342342
// Returns nullptr on failure (i.e. initialization of the VM fails).
343-
std::shared_ptr<WasmHandleBase>
344-
createWasm(std::string vm_key, std::string code, std::shared_ptr<PluginBase> plugin,
345-
WasmHandleFactory factory, WasmHandleCloneFactory clone_factory, bool allow_precompiled);
346-
// Get an existing ThreadLocal VM matching 'vm_id' or nullptr if there isn't one.
347-
std::shared_ptr<WasmHandleBase> getThreadLocalWasm(std::string_view vm_id);
343+
std::shared_ptr<WasmHandleBase> createWasm(const std::string &vm_key, const std::string &code,
344+
const std::shared_ptr<PluginBase> &plugin,
345+
const WasmHandleFactory &factory,
346+
const WasmHandleCloneFactory &clone_factory,
347+
bool allow_precompiled);
348+
// Get an existing ThreadLocal VM matching 'vm_key' or nullptr if there isn't one.
349+
std::shared_ptr<WasmHandleBase> getThreadLocalWasm(std::string_view vm_key);
348350

349351
class PluginHandleBase : public std::enable_shared_from_this<PluginHandleBase> {
350352
public:
@@ -371,8 +373,8 @@ using PluginHandleFactory = std::function<std::shared_ptr<PluginHandleBase>(
371373
// Get an existing ThreadLocal VM matching 'vm_id' or create one using 'base_wavm' by cloning or by
372374
// using it it as a template.
373375
std::shared_ptr<PluginHandleBase> getOrCreateThreadLocalPlugin(
374-
std::shared_ptr<WasmHandleBase> base_wasm, std::shared_ptr<PluginBase> plugin,
375-
WasmHandleCloneFactory clone_factory, PluginHandleFactory plugin_factory);
376+
const std::shared_ptr<WasmHandleBase> &base_handle, const std::shared_ptr<PluginBase> &plugin,
377+
const WasmHandleCloneFactory &clone_factory, const PluginHandleFactory &plugin_factory);
376378

377379
// Clear Base Wasm cache and the thread-local Wasm sandbox cache for the calling thread.
378380
void clearWasmCachesForTesting();
@@ -403,7 +405,7 @@ inline uint64_t WasmBase::copyString(std::string_view s) {
403405
if (s.empty()) {
404406
return 0; // nullptr
405407
}
406-
uint64_t pointer;
408+
uint64_t pointer = 0;
407409
uint8_t *m = static_cast<uint8_t *>(allocMemory((s.size() + 1), &pointer));
408410
memcpy(m, s.data(), s.size());
409411
m[s.size()] = 0;

Diff for: include/proxy-wasm/wasm_vm.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ class WasmVm {
214214
* @return whether or not the load was successful.
215215
*/
216216
virtual bool load(std::string_view bytecode, std::string_view precompiled,
217-
const std::unordered_map<uint32_t, std::string> function_names) = 0;
217+
const std::unordered_map<uint32_t, std::string> &function_names) = 0;
218218

219219
/**
220220
* Link the WASM code to the host-provided functions, e.g. the ABI. Prior to linking, the module

0 commit comments

Comments
 (0)