Skip to content

Commit e82d356

Browse files
build.c: use C macros + string literal concatenation for ASAN flags (#1235)
* Initial plan * Add ASAN binary builds targeting binaries-asan branch Co-authored-by: uNetworkingAB <110806833+uNetworkingAB@users.noreply.github.com> * Refactor ASAN flags in build.c to use C macros and string concatenation Co-authored-by: uNetworkingAB <110806833+uNetworkingAB@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: uNetworkingAB <110806833+uNetworkingAB@users.noreply.github.com>
1 parent 3d3dd05 commit e82d356

2 files changed

Lines changed: 59 additions & 6 deletions

File tree

‎.github/workflows/build-asan.yml‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Build ASAN
2+
on:
3+
push:
4+
branches: [ master ]
5+
jobs:
6+
build-asan:
7+
runs-on: ${{ matrix.os }}
8+
permissions:
9+
contents: read
10+
strategy:
11+
fail-fast: false
12+
matrix:
13+
os: [macos-15, ubuntu-24.04, ubuntu-24.04-arm]
14+
steps:
15+
- name: Setup Node.js
16+
uses: actions/setup-node@v4
17+
with:
18+
node-version: 25
19+
- name: Update ASAN binaries
20+
run: |
21+
git clone --recursive https://github.com/uNetworking/uWebSockets.js.git
22+
cd uWebSockets.js
23+
cc build.c -DWITH_ASAN && ./a.out
24+
ls dist
25+
# On first run, binaries-asan doesn't exist yet; seed it from binaries so git history is preserved
26+
git fetch origin binaries-asan:binaries-asan 2>/dev/null || git fetch origin binaries:binaries-asan
27+
git checkout binaries-asan
28+
cp dist/*.node .
29+
cp dist/*.js .
30+
git rev-parse master > source_commit
31+
ls
32+
git checkout master docs/index.d.ts && mv docs/index.d.ts .
33+
git status
34+
git config --global user.email "alexhultman@gmail.com"
35+
git config --global user.name "Alex Hultman"
36+
git commit -a -m "[GitHub Actions] Updated ${{ matrix.os }} ASAN binaries" || true
37+
git push "https://unetworkingab:${{ secrets.SECRET }}@github.com/uNetworking/uWebSockets.js" binaries-asan

‎build.c‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@
1717
#define IS_MACOS
1818
#endif
1919

20+
/* ASAN vs. optimized build flags (used via C string literal concatenation).
21+
* OPT_FLAGS / LINK_FLAGS: inserted mid-string, so each definition starts with a space.
22+
* LINUX_LINK_EXTRAS: passed as a standalone argument, so no leading space.
23+
* MACOS_LINK_EXTRAS: appended after "-undefined dynamic_lookup", so ASAN variant starts with a space. */
24+
#ifdef WITH_ASAN
25+
#define OPT_FLAGS " -fsanitize=address -fno-omit-frame-pointer -g -O1"
26+
#define LINK_FLAGS " -fsanitize=address"
27+
#define LINUX_LINK_EXTRAS "-fsanitize=address"
28+
#define MACOS_LINK_EXTRAS " -fsanitize=address"
29+
#else
30+
#define OPT_FLAGS " -flto -O3"
31+
#define LINK_FLAGS " -flto -O3"
32+
#define LINUX_LINK_EXTRAS "-static-libstdc++ -static-libgcc -s"
33+
#define MACOS_LINK_EXTRAS ""
34+
#endif
35+
2036
const char *ARM = "arm";
2137
const char *ARM64 = "arm64";
2238
const char *X64 = "x64";
@@ -113,13 +129,13 @@ void build_boringssl(const char *arch) {
113129
/* Build for Unix systems */
114130
void build(char *compiler, char *cpp_compiler, char *cpp_linker, char *os, const char *arch) {
115131

116-
char *c_shared = "-DWIN32_LEAN_AND_MEAN -DLIBUS_USE_LIBUV -DLIBUS_USE_QUIC -I uWebSockets/uSockets/lsquic/include -I uWebSockets/uSockets/boringssl/include -pthread -DLIBUS_USE_OPENSSL -flto -O3 -c -fPIC -I uWebSockets/uSockets/src uWebSockets/uSockets/src/*.c uWebSockets/uSockets/src/eventing/*.c uWebSockets/uSockets/src/crypto/*.c";
117-
char *cpp_shared = "-DWIN32_LEAN_AND_MEAN -DUWS_WITH_PROXY -DLIBUS_USE_LIBUV -DLIBUS_USE_QUIC -I uWebSockets/uSockets/boringssl/include -pthread -DLIBUS_USE_OPENSSL -flto -O3 -c -fPIC -std=c++20 -I uWebSockets/uSockets/src -I uWebSockets/src src/addon.cpp uWebSockets/uSockets/src/crypto/sni_tree.cpp";
132+
char *c_shared = "-DWIN32_LEAN_AND_MEAN -DLIBUS_USE_LIBUV -DLIBUS_USE_QUIC -I uWebSockets/uSockets/lsquic/include -I uWebSockets/uSockets/boringssl/include -pthread -DLIBUS_USE_OPENSSL" OPT_FLAGS " -c -fPIC -I uWebSockets/uSockets/src uWebSockets/uSockets/src/*.c uWebSockets/uSockets/src/eventing/*.c uWebSockets/uSockets/src/crypto/*.c";
133+
char *cpp_shared = "-DWIN32_LEAN_AND_MEAN -DUWS_WITH_PROXY -DLIBUS_USE_LIBUV -DLIBUS_USE_QUIC -I uWebSockets/uSockets/boringssl/include -pthread -DLIBUS_USE_OPENSSL" OPT_FLAGS " -c -fPIC -std=c++20 -I uWebSockets/uSockets/src -I uWebSockets/src src/addon.cpp uWebSockets/uSockets/src/crypto/sni_tree.cpp";
118134

119135
for (unsigned int i = 0; i < sizeof(versions) / sizeof(struct node_version); i++) {
120136
run("%s %s -I targets/node-%s/include/node", compiler, c_shared, versions[i].name);
121137
run("%s %s -I targets/node-%s/include/node", cpp_compiler, cpp_shared, versions[i].name);
122-
run("%s -pthread -flto -O3 *.o uWebSockets/uSockets/boringssl/%s/libssl.a uWebSockets/uSockets/boringssl/%s/libcrypto.a uWebSockets/uSockets/lsquic/%s/src/liblsquic/liblsquic.a -std=c++20 -shared %s -o dist/uws_%s_%s_%s.node", cpp_compiler, arch, arch, arch, cpp_linker, os, arch, versions[i].abi);
138+
run("%s -pthread" LINK_FLAGS " *.o uWebSockets/uSockets/boringssl/%s/libssl.a uWebSockets/uSockets/boringssl/%s/libcrypto.a uWebSockets/uSockets/lsquic/%s/src/liblsquic/liblsquic.a -std=c++20 -shared %s -o dist/uws_%s_%s_%s.node", cpp_compiler, arch, arch, arch, cpp_linker, os, arch, versions[i].abi);
123139
}
124140
}
125141

@@ -183,22 +199,22 @@ int main() {
183199
/* Apple special case */
184200
build("clang -target x86_64-apple-macos12",
185201
"clang++ -stdlib=libc++ -target x86_64-apple-macos12",
186-
"-undefined dynamic_lookup",
202+
"-undefined dynamic_lookup" MACOS_LINK_EXTRAS,
187203
OS,
188204
X64);
189205

190206
/* Try and build for arm64 macOS 12 */
191207
build("clang -target arm64-apple-macos12",
192208
"clang++ -stdlib=libc++ -target arm64-apple-macos12",
193-
"-undefined dynamic_lookup",
209+
"-undefined dynamic_lookup" MACOS_LINK_EXTRAS,
194210
OS,
195211
ARM64);
196212

197213
#else
198214
/* Linux does not cross-compile but picks whatever arch the host is on (we run on both x64 and ARM64) */
199215
build("clang-18",
200216
"clang++-18",
201-
"-static-libstdc++ -static-libgcc -s",
217+
LINUX_LINK_EXTRAS,
202218
OS,
203219
arch);
204220
#endif

0 commit comments

Comments
 (0)