Skip to content

Commit da99e5c

Browse files
committed
bazel-registry: Fix proxy-wasm-cpp-host@0.0.0-260115-beb8a4e.envoy (#3674)
Signed-off-by: Ryan Northey <ryan@synca.io>
1 parent f1a4218 commit da99e5c

File tree

5 files changed

+366
-44
lines changed

5 files changed

+366
-44
lines changed

bazel-registry/modules/hyperscan/5.4.2.envoy/overlay/BUILD.bazel

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
load("@rules_cc//cc:defs.bzl", "cc_library")
2+
load(":fat_runtime.bzl", "hs_exec_variant")
23

3-
# Generate config.h
44
genrule(
55
name = "config_h",
66
outs = ["config.h"],
@@ -24,6 +24,9 @@ genrule(
2424
#define HS_PATCH_VERSION 2
2525
#define BUILD_DATE "2023-04-19"
2626
#define RELEASE_BUILD
27+
#define FAT_RUNTIME
28+
#define BUILD_AVX512
29+
#define BUILD_AVX512VBMI
2730
2831
#endif /* CONFIG_H_ */
2932
EOF
@@ -100,54 +103,28 @@ cc_library(
100103
visibility = ["//visibility:private"],
101104
)
102105

103-
# Runtime library (all C runtime sources)
104-
# Note: This is a simplified build for x86_64 with -march=native.
105-
# Hyperscan is a performance-critical library that heavily relies on SIMD
106-
# instructions (SSE, AVX2, AVX512). For production use, consider building
107-
# a fat runtime with multiple architecture targets similar to upstream CMake.
106+
# Export the rename_symbols.sh script
107+
exports_files(["rename_symbols.sh"])
108+
109+
# Build five architecture variants
110+
hs_exec_variant("core2", "core2", "-march=core2")
111+
hs_exec_variant("corei7", "corei7", "-march=corei7")
112+
hs_exec_variant("avx2", "avx2", "-march=core-avx2")
113+
hs_exec_variant("avx512", "avx512", "-march=skylake-avx512")
114+
hs_exec_variant("avx512vbmi", "avx512vbmi", "-march=icelake-server")
115+
116+
# Dispatcher library
108117
cc_library(
109-
name = "hs_runtime",
110-
srcs = glob(
111-
[
112-
"src/crc32.c",
113-
"src/database.c",
114-
"src/runtime.c",
115-
"src/stream_compress.c",
116-
"src/hs_version.c",
117-
"src/hs_valid_platform.c",
118-
"src/fdr/*.c",
119-
"src/hwlm/*.c",
120-
"src/nfa/*.c",
121-
"src/rose/*.c",
122-
"src/som/*.c",
123-
"src/util/masked_move.c",
124-
"src/util/simd_utils.c",
125-
"src/util/state_compress.c",
126-
],
127-
exclude = [
128-
"src/**/*_dump*.c",
129-
"src/**/test_*.c",
130-
"src/hwlm/noodle_engine_avx2.c", # Textual include in noodle_engine.c
131-
"src/hwlm/noodle_engine_avx512.c", # Textual include in noodle_engine.c
132-
"src/hwlm/noodle_engine_sse.c", # Textual include in noodle_engine.c
133-
],
134-
),
135-
hdrs = glob([
136-
"src/**/*.h",
137-
]),
138-
textual_hdrs = [
139-
"src/hwlm/noodle_engine_avx2.c",
140-
"src/hwlm/noodle_engine_avx512.c",
141-
"src/hwlm/noodle_engine_sse.c",
142-
],
118+
name = "hs_dispatcher",
119+
srcs = ["dispatcher.c"],
120+
hdrs = glob(["src/**/*.h"]),
143121
copts = [
144122
"-std=c99",
145123
"-O3",
146124
"-DNDEBUG",
147125
"-fno-strict-aliasing",
148-
"-march=native", # Required for SIMD optimizations
149126
"-Wno-unused-parameter",
150-
"-Wno-sign-compare",
127+
"-Wno-unused-function",
151128
],
152129
includes = [
153130
".",
@@ -161,6 +138,32 @@ cc_library(
161138
deps = [":hs_common"],
162139
)
163140

141+
# Runtime library combining all variants with dispatcher
142+
# Note: This implements fat runtime with multiple architecture targets
143+
# to avoid SIGILL crashes when running on different CPUs.
144+
cc_library(
145+
name = "hs_runtime",
146+
hdrs = glob(["src/**/*.h"]),
147+
includes = [
148+
".",
149+
"src",
150+
],
151+
target_compatible_with = [
152+
"@platforms//cpu:x86_64",
153+
"@platforms//os:linux",
154+
],
155+
visibility = ["//visibility:private"],
156+
deps = [
157+
":hs_common",
158+
":hs_dispatcher",
159+
":hs_exec_core2_renamed_import",
160+
":hs_exec_corei7_renamed_import",
161+
":hs_exec_avx2_renamed_import",
162+
":hs_exec_avx512_renamed_import",
163+
":hs_exec_avx512vbmi_renamed_import",
164+
],
165+
)
166+
164167
# Compiler library (all C++ compile sources)
165168
cc_library(
166169
name = "hs_compile",
@@ -205,10 +208,13 @@ cc_library(
205208
"-O2",
206209
"-DNDEBUG",
207210
"-fno-strict-aliasing",
211+
"-fvisibility=hidden",
208212
"-Wno-unused-parameter",
209213
"-Wno-sign-compare",
210214
"-Wno-unused-variable",
211215
"-Wno-unused-but-set-variable",
216+
"-Wno-unqualified-std-cast-call",
217+
"-Wno-redundant-move",
212218
],
213219
includes = [
214220
".",
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* Copyright (c) 2016-2020, Intel Corporation
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
*
7+
* * Redistributions of source code must retain the above copyright notice,
8+
* this list of conditions and the following disclaimer.
9+
* * Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* * Neither the name of Intel Corporation nor the names of its contributors
13+
* may be used to endorse or promote products derived from this software
14+
* without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
29+
#include "config.h"
30+
#include "hs_common.h"
31+
#include "hs_runtime.h"
32+
#include "ue2common.h"
33+
#include "util/cpuid_inline.h"
34+
#include "util/join.h"
35+
36+
#if defined(DISABLE_AVX512_DISPATCH)
37+
#define avx512_ disabled_
38+
#define check_avx512() (0)
39+
#endif
40+
41+
#if defined(DISABLE_AVX512VBMI_DISPATCH)
42+
#define avx512vbmi_ disabled_
43+
#define check_avx512vbmi() (0)
44+
#endif
45+
46+
#define CREATE_DISPATCH(RTYPE, NAME, ...) \
47+
/* create defns */ \
48+
RTYPE JOIN(avx512vbmi_, NAME)(__VA_ARGS__); \
49+
RTYPE JOIN(avx512_, NAME)(__VA_ARGS__); \
50+
RTYPE JOIN(avx2_, NAME)(__VA_ARGS__); \
51+
RTYPE JOIN(corei7_, NAME)(__VA_ARGS__); \
52+
RTYPE JOIN(core2_, NAME)(__VA_ARGS__); \
53+
\
54+
/* error func */ \
55+
static inline RTYPE JOIN(error_, NAME)(__VA_ARGS__) { \
56+
return (RTYPE)HS_ARCH_ERROR; \
57+
} \
58+
\
59+
/* resolver */ \
60+
static RTYPE (*JOIN(resolve_, NAME)(void))(__VA_ARGS__) { \
61+
if (check_avx512vbmi()) { \
62+
return JOIN(avx512vbmi_, NAME); \
63+
} \
64+
if (check_avx512()) { \
65+
return JOIN(avx512_, NAME); \
66+
} \
67+
if (check_avx2()) { \
68+
return JOIN(avx2_, NAME); \
69+
} \
70+
if (check_sse42() && check_popcnt()) { \
71+
return JOIN(corei7_, NAME); \
72+
} \
73+
if (check_ssse3()) { \
74+
return JOIN(core2_, NAME); \
75+
} \
76+
/* anything else is fail */ \
77+
return JOIN(error_, NAME); \
78+
} \
79+
\
80+
/* function */ \
81+
HS_PUBLIC_API \
82+
RTYPE NAME(__VA_ARGS__) __attribute__((ifunc("resolve_" #NAME)))
83+
84+
CREATE_DISPATCH(hs_error_t, hs_scan, const hs_database_t *db, const char *data,
85+
unsigned length, unsigned flags, hs_scratch_t *scratch,
86+
match_event_handler onEvent, void *userCtx);
87+
88+
CREATE_DISPATCH(hs_error_t, hs_stream_size, const hs_database_t *database,
89+
size_t *stream_size);
90+
91+
CREATE_DISPATCH(hs_error_t, hs_database_size, const hs_database_t *db,
92+
size_t *size);
93+
CREATE_DISPATCH(hs_error_t, dbIsValid, const hs_database_t *db);
94+
CREATE_DISPATCH(hs_error_t, hs_free_database, hs_database_t *db);
95+
96+
CREATE_DISPATCH(hs_error_t, hs_open_stream, const hs_database_t *db,
97+
unsigned int flags, hs_stream_t **stream);
98+
99+
CREATE_DISPATCH(hs_error_t, hs_scan_stream, hs_stream_t *id, const char *data,
100+
unsigned int length, unsigned int flags, hs_scratch_t *scratch,
101+
match_event_handler onEvent, void *ctxt);
102+
103+
CREATE_DISPATCH(hs_error_t, hs_close_stream, hs_stream_t *id,
104+
hs_scratch_t *scratch, match_event_handler onEvent, void *ctxt);
105+
106+
CREATE_DISPATCH(hs_error_t, hs_scan_vector, const hs_database_t *db,
107+
const char *const *data, const unsigned int *length,
108+
unsigned int count, unsigned int flags, hs_scratch_t *scratch,
109+
match_event_handler onevent, void *context);
110+
111+
CREATE_DISPATCH(hs_error_t, hs_database_info, const hs_database_t *db, char **info);
112+
113+
CREATE_DISPATCH(hs_error_t, hs_copy_stream, hs_stream_t **to_id,
114+
const hs_stream_t *from_id);
115+
116+
CREATE_DISPATCH(hs_error_t, hs_reset_stream, hs_stream_t *id,
117+
unsigned int flags, hs_scratch_t *scratch,
118+
match_event_handler onEvent, void *context);
119+
120+
CREATE_DISPATCH(hs_error_t, hs_reset_and_copy_stream, hs_stream_t *to_id,
121+
const hs_stream_t *from_id, hs_scratch_t *scratch,
122+
match_event_handler onEvent, void *context);
123+
124+
CREATE_DISPATCH(hs_error_t, hs_serialize_database, const hs_database_t *db,
125+
char **bytes, size_t *length);
126+
127+
CREATE_DISPATCH(hs_error_t, hs_deserialize_database, const char *bytes,
128+
const size_t length, hs_database_t **db);
129+
130+
CREATE_DISPATCH(hs_error_t, hs_deserialize_database_at, const char *bytes,
131+
const size_t length, hs_database_t *db);
132+
133+
CREATE_DISPATCH(hs_error_t, hs_serialized_database_info, const char *bytes,
134+
size_t length, char **info);
135+
136+
CREATE_DISPATCH(hs_error_t, hs_serialized_database_size, const char *bytes,
137+
const size_t length, size_t *deserialized_size);
138+
139+
CREATE_DISPATCH(hs_error_t, hs_compress_stream, const hs_stream_t *stream,
140+
char *buf, size_t buf_space, size_t *used_space);
141+
142+
CREATE_DISPATCH(hs_error_t, hs_expand_stream, const hs_database_t *db,
143+
hs_stream_t **stream, const char *buf,size_t buf_size);
144+
145+
CREATE_DISPATCH(hs_error_t, hs_reset_and_expand_stream, hs_stream_t *to_stream,
146+
const char *buf, size_t buf_size, hs_scratch_t *scratch,
147+
match_event_handler onEvent, void *context);
148+
149+
/** INTERNALS **/
150+
151+
CREATE_DISPATCH(u32, Crc32c_ComputeBuf, u32 inCrc32, const void *buf, size_t bufLen);
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
"""Macro for building fat runtime variants of hyperscan."""
2+
3+
load("@rules_cc//cc:defs.bzl", "cc_import", "cc_library")
4+
5+
def hs_exec_variant(name, arch, march_flag):
6+
"""Build an architecture-specific variant of the runtime library.
7+
8+
Args:
9+
name: Name of the variant (e.g., "core2", "avx2")
10+
arch: Architecture identifier for symbol prefixing
11+
march_flag: GCC -march flag (e.g., "-march=core2")
12+
"""
13+
lib_name = "hs_exec_" + name
14+
renamed_name = lib_name + "_renamed"
15+
16+
# Compile the runtime sources with specific architecture flags
17+
cc_library(
18+
name = lib_name,
19+
srcs = native.glob(
20+
[
21+
"src/crc32.c",
22+
"src/database.c",
23+
"src/runtime.c",
24+
"src/stream_compress.c",
25+
"src/hs_version.c",
26+
"src/hs_valid_platform.c",
27+
"src/fdr/*.c",
28+
"src/hwlm/*.c",
29+
"src/nfa/*.c",
30+
"src/rose/*.c",
31+
"src/som/*.c",
32+
"src/util/masked_move.c",
33+
"src/util/simd_utils.c",
34+
"src/util/state_compress.c",
35+
],
36+
exclude = [
37+
"src/**/*_dump*.c",
38+
"src/**/test_*.c",
39+
"src/hwlm/noodle_engine_avx2.c",
40+
"src/hwlm/noodle_engine_avx512.c",
41+
"src/hwlm/noodle_engine_sse.c",
42+
],
43+
),
44+
hdrs = native.glob(["src/**/*.h"]),
45+
textual_hdrs = [
46+
"src/hwlm/noodle_engine_avx2.c",
47+
"src/hwlm/noodle_engine_avx512.c",
48+
"src/hwlm/noodle_engine_sse.c",
49+
],
50+
copts = [
51+
"-std=c99",
52+
"-O3",
53+
"-DNDEBUG",
54+
"-fno-strict-aliasing",
55+
march_flag,
56+
"-Wno-unused-parameter",
57+
"-Wno-sign-compare",
58+
],
59+
includes = [
60+
".",
61+
"src",
62+
],
63+
linkstatic = True,
64+
target_compatible_with = [
65+
"@platforms//cpu:x86_64",
66+
"@platforms//os:linux",
67+
],
68+
visibility = ["//visibility:private"],
69+
deps = [":hs_common"],
70+
)
71+
72+
# Extract the static library and rename symbols
73+
native.genrule(
74+
name = renamed_name,
75+
srcs = [":" + lib_name],
76+
outs = ["lib" + renamed_name + ".a"],
77+
cmd = """
78+
# Find the .a file in the inputs
79+
for f in $(SRCS); do
80+
if [[ "$$f" == *.a ]]; then
81+
INPUT_AR="$$f"
82+
break
83+
fi
84+
done
85+
if [ -z "$$INPUT_AR" ]; then
86+
echo "Error: Could not find .a file in inputs"
87+
exit 1
88+
fi
89+
# Run the symbol renaming script
90+
$(location :rename_symbols.sh) """ + arch + """ "$$INPUT_AR" $@
91+
""",
92+
tools = [":rename_symbols.sh"],
93+
target_compatible_with = [
94+
"@platforms//cpu:x86_64",
95+
"@platforms//os:linux",
96+
],
97+
)
98+
99+
# Import the renamed archive back as a cc_library
100+
cc_import(
101+
name = renamed_name + "_import",
102+
static_library = ":" + renamed_name,
103+
target_compatible_with = [
104+
"@platforms//cpu:x86_64",
105+
"@platforms//os:linux",
106+
],
107+
visibility = ["//visibility:private"],
108+
)
109+

0 commit comments

Comments
 (0)