Skip to content

Commit 01f7b12

Browse files
willieyzmkannwischer
authored andcommitted
Port: check-namespace
Signed-off-by: willieyz <[email protected]>
1 parent c1d9522 commit 01f7b12

File tree

7 files changed

+135
-3
lines changed

7 files changed

+135
-3
lines changed

.github/actions/bench/action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ runs:
8989
--ldflags="${{ inputs.ldflags }}" \
9090
--opt=$([[ ${{ inputs.opt }} == "false" ]] && echo "no_opt" || echo "opt") \
9191
-v ${{ inputs.bench_extra_args }}
92+
- name: Check namespace
93+
shell: ${{ env.SHELL }}
94+
run: |
95+
check-namespace
9296
- name: Store benchmark result
9397
uses: benchmark-action/github-action-benchmark@d48d326b4ca9ba73ca0cd0d59f108f9e02a381c7 # v1.20.4
9498
with:

.github/actions/functest/action.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ inputs:
4848
examples:
4949
description: Determine whether to run examples or not
5050
default: "true"
51+
check_namespace:
52+
description: Determine whether to check namespacing or not
53+
default: "true"
5154
stack:
5255
description: Determine whether to run stack analysis or not
5356
default: "false"
@@ -101,7 +104,7 @@ runs:
101104
shell: ${{ env.SHELL }}
102105
run: |
103106
make clean
104-
./scripts/tests all --exec-wrapper="${{ inputs.exec_wrapper }}" --cross-prefix="${{ inputs.cross_prefix }}" --cflags="${{ inputs.cflags }}" --ldflags="${{ inputs.ldflags }}" --opt=${{ inputs.opt }} --${{ env.FUNC }} --${{ env.KAT }} --${{ env.ACVP }} --${{ env.EXAMPLES }} --${{ env.STACK }} -v ${{ inputs.extra_args }}
107+
./scripts/tests all ${{ inputs.check_namespace == 'true' && '--check-namespace' || ''}} --exec-wrapper="${{ inputs.exec_wrapper }}" --cross-prefix="${{ inputs.cross_prefix }}" --cflags="${{ inputs.cflags }}" --ldflags="${{ inputs.ldflags }}" --opt=${{ inputs.opt }} --${{ env.FUNC }} --${{ env.KAT }} --${{ env.ACVP }} --${{ env.EXAMPLES }} --${{ env.STACK }} -v ${{ inputs.extra_args }}
105108
- name: Post ${{ env.MODE }} Tests
106109
shell: ${{ env.SHELL }}
107110
if: success() || failure()

.github/actions/multi-functest/action.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ inputs:
4545
examples:
4646
description: Determine whether to run examples or not
4747
default: "true"
48+
check_namespace:
49+
description: Determine whether to check namespacing or not
50+
default: "true"
4851
stack:
4952
description: Determine whether to run stack analysis or not
5053
default: "false"
@@ -70,6 +73,7 @@ runs:
7073
kat: ${{ inputs.kat }}
7174
acvp: ${{ inputs.acvp }}
7275
examples: ${{ inputs.examples }}
76+
check_namespace: ${{ inputs.check_namespace }}
7377
stack: ${{ inputs.stack }}
7478
extra_args: ${{ inputs.extra_args }}
7579
- name: Cross x86_64 Tests
@@ -90,6 +94,7 @@ runs:
9094
kat: ${{ inputs.kat }}
9195
acvp: ${{ inputs.acvp }}
9296
examples: ${{ inputs.examples }}
97+
check_namespace: ${{ inputs.check_namespace }}
9398
stack: ${{ inputs.stack }}
9499
extra_args: ${{ inputs.extra_args }}
95100
- name: Cross aarch64 Tests
@@ -110,6 +115,7 @@ runs:
110115
kat: ${{ inputs.kat }}
111116
acvp: ${{ inputs.acvp }}
112117
examples: ${{ inputs.examples }}
118+
check_namespace: ${{ inputs.check_namespace }}
113119
stack: ${{ inputs.stack }}
114120
extra_args: ${{ inputs.extra_args }}
115121
- name: Cross ppc64le Tests
@@ -130,6 +136,7 @@ runs:
130136
kat: ${{ inputs.kat }}
131137
acvp: ${{ inputs.acvp }}
132138
examples: ${{ inputs.examples }}
139+
check_namespace: ${{ inputs.check_namespace }}
133140
stack: ${{ inputs.stack }}
134141
extra_args: ${{ inputs.extra_args }}
135142
- name: Cross aarch64_be Tests
@@ -150,6 +157,7 @@ runs:
150157
kat: ${{ inputs.kat }}
151158
acvp: ${{ inputs.acvp }}
152159
examples: ${{ inputs.examples }}
160+
check_namespace: ${{ inputs.check_namespace }}
153161
stack: ${{ inputs.stack }}
154162
extra_args: ${{ inputs.extra_args }}
155163
- name: Cross riscv64 Tests (RVV, VLEN=128)
@@ -170,6 +178,7 @@ runs:
170178
kat: ${{ inputs.kat }}
171179
acvp: ${{ inputs.acvp }}
172180
examples: ${{ inputs.examples }}
181+
check_namespace: ${{ inputs.check_namespace }}
173182
stack: ${{ inputs.stack }}
174183
extra_args: ${{ inputs.extra_args }}
175184
- name: Cross riscv64 Tests (RVV, VLEN=256)
@@ -189,6 +198,7 @@ runs:
189198
kat: ${{ inputs.kat }}
190199
acvp: ${{ inputs.acvp }}
191200
examples: ${{ inputs.examples }}
201+
check_namespace: ${{ inputs.check_namespace }}
192202
stack: ${{ inputs.stack }}
193203
extra_args: ${{ inputs.extra_args }}
194204
- name: Cross riscv64 Tests (RVV, VLEN=512)
@@ -208,6 +218,7 @@ runs:
208218
kat: ${{ inputs.kat }}
209219
acvp: ${{ inputs.acvp }}
210220
examples: ${{ inputs.examples }}
221+
check_namespace: ${{ inputs.check_namespace }}
211222
stack: ${{ inputs.stack }}
212223
extra_args: ${{ inputs.extra_args }}
213224
- name: Cross riscv64 Tests (RVV, VLEN=1024)
@@ -227,6 +238,7 @@ runs:
227238
kat: ${{ inputs.kat }}
228239
acvp: ${{ inputs.acvp }}
229240
examples: ${{ inputs.examples }}
241+
check_namespace: ${{ inputs.check_namespace }}
230242
stack: ${{ inputs.stack }}
231243
extra_args: ${{ inputs.extra_args }}
232244
- name: Cross riscv32 Tests
@@ -247,6 +259,7 @@ runs:
247259
kat: ${{ inputs.kat }}
248260
acvp: ${{ inputs.acvp }}
249261
examples: ${{ inputs.examples }}
262+
check_namespace: ${{ inputs.check_namespace }}
250263
stack: ${{ inputs.stack }}
251264
extra_args: ${{ inputs.extra_args }}
252265

.github/workflows/base.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ jobs:
5757
- uses: ./.github/actions/setup-os
5858
- name: tests func
5959
run: |
60-
./scripts/tests func
60+
./scripts/tests func --check-namespace
6161
quickcheck-acvp:
6262
strategy:
6363
fail-fast: false
@@ -161,7 +161,7 @@ jobs:
161161
- uses: ./.github/actions/setup-apt
162162
- name: tests func
163163
run: |
164-
./scripts/tests func --cflags="-std=c90"
164+
./scripts/tests func --cflags="-std=c90" --check-namespace
165165
- name: tests bench
166166
run: |
167167
./scripts/tests bench -c NO --cflags="-std=c90"

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ jobs:
153153
compile_mode: native
154154
cflags: "-DMLDSA_DEBUG -fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all"
155155
ldflags: "-fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all"
156+
check_namespace: 'false'
156157
backend_tests:
157158
name: AArch64 FIPS202 backends (${{ matrix.backend }})
158159
strategy:
@@ -173,6 +174,7 @@ jobs:
173174
examples: 'false'
174175
cflags: "-DMLDSA_DEBUG -fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all"
175176
ldflags: "-fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all"
177+
check_namespace: 'false'
176178
extra_args: "--fips202-aarch64-backend ${{ matrix.backend }}"
177179
compiler_tests:
178180
name: Compiler tests (${{ matrix.compiler.name }}, ${{ matrix.target.name }}, ${{ matrix.cflags }})
@@ -415,6 +417,7 @@ jobs:
415417
acvp: false
416418
examples: false
417419
stack: true
420+
check_namespace: false
418421
config_variations:
419422
name: Non-standard configurations
420423
strategy:

scripts/check-namespace

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) The mlkem-native project authors
3+
# Copyright (c) The mldsa-native project authors
4+
# SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
5+
6+
# This scripts runs nm on the object files (excluding test objects) and checks that all exported
7+
# symbols are properly namespaced.
8+
# It assumes that object files are present under test/build/mldsa{44,65,87} and
9+
# test/build/fips202.
10+
11+
# The checked namespaces are
12+
# PQCP_MLDSA_NATIVE_FIPS202_ for FIPS202 code
13+
# PQCP_MLDSA_NATIVE_MLDSA44_ for MLDSA44 code
14+
# PQCP_MLDSA_NATIVE_MLDSA65_ for MLDSA65 code
15+
# PQCP_MLDSA_NATIVE_MLDSA87_ for MLDSA87 code
16+
17+
import subprocess
18+
import os
19+
20+
21+
def check_file(file_path, namespaces):
22+
print("checking namespacing: {}".format(file_path))
23+
command = ["nm", "-g", file_path]
24+
25+
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
26+
27+
result = result.stdout.decode("utf-8")
28+
lines = result.strip().split("\n")
29+
symbols = []
30+
for line in lines:
31+
if line.startswith("00"):
32+
symbols.append(line)
33+
34+
def is_namespaced(symbol):
35+
for namespace in namespaces:
36+
if symbol.startswith(namespace) or symbol.startswith("_" + namespace):
37+
return True
38+
return False
39+
40+
non_namespaced = []
41+
for symbolstr in symbols:
42+
*_, symtype, symbol = symbolstr.split()
43+
if symtype in "TDRS":
44+
if is_namespaced(symbol) is False:
45+
non_namespaced.append(symbol)
46+
47+
if len(non_namespaced) > 0:
48+
print("Missing namespace literal from {}".format(namespaces))
49+
for symbol in non_namespaced:
50+
print("\tsymbol: {}".format(symbol))
51+
assert not non_namespaced, "Literals with missing namespaces"
52+
53+
54+
def check_folder(folder, namespace):
55+
checked = 0
56+
# recursively go through folder and check all object files
57+
for root, dirnames, filenames in os.walk(folder):
58+
for filename in filenames:
59+
if filename.endswith(".o"):
60+
check_file(os.path.join(root, filename), namespace)
61+
checked += 1
62+
print("Checked {} files".format(checked))
63+
assert checked > 0
64+
65+
66+
def make_mldsa_namespace(lvl):
67+
return [f"PQCP_MLDSA_NATIVE_MLDSA{lvl}"]
68+
69+
70+
def run():
71+
check_folder("test/build/mldsa44/mldsa", make_mldsa_namespace(44))
72+
check_folder("test/build/mldsa65/mldsa", make_mldsa_namespace(65))
73+
check_folder("test/build/mldsa87/mldsa", make_mldsa_namespace(87))
74+
75+
76+
if __name__ == "__main__":
77+
os.chdir(os.path.join(os.path.dirname(__file__), ".."))
78+
run()

scripts/tests

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,14 @@ class Tests:
577577
def func(self):
578578
def _func(opt):
579579
self._compile_schemes(TEST_TYPES.FUNC, opt)
580+
if self.args.check_namespace is True:
581+
p = subprocess.run(
582+
["python3", "check-namespace"],
583+
stdout=subprocess.DEVNULL if not self.args.verbose else None,
584+
cwd="scripts",
585+
)
586+
if p.returncode != 0:
587+
self.fail(f"Namespacing failed for opt={opt}")
580588
if self.args.run:
581589
self._run_schemes(TEST_TYPES.FUNC, opt)
582590

@@ -744,6 +752,15 @@ class Tests:
744752
if stack is True:
745753
self._compile_schemes(TEST_TYPES.STACK, opt)
746754

755+
if self.args.check_namespace is True:
756+
p = subprocess.run(
757+
["python3", "check-namespace"],
758+
stdout=subprocess.DEVNULL if not self.args.verbose else None,
759+
cwd="scripts",
760+
)
761+
if p.returncode != 0:
762+
self.fail(f"Namespacing failed for opt={opt}")
763+
747764
if self.args.run is False:
748765
return
749766

@@ -1022,6 +1039,13 @@ def cli():
10221039
"all", help="Run all tests (except benchmark for now)", parents=[common_parser]
10231040
)
10241041

1042+
all_parser.add_argument(
1043+
"--check-namespace",
1044+
help="Check namespacing of binaries",
1045+
action="store_true",
1046+
default=False,
1047+
)
1048+
10251049
func_group = all_parser.add_mutually_exclusive_group()
10261050
func_group.add_argument(
10271051
"--func", action="store_true", dest="func", help="Run func test", default=True
@@ -1253,6 +1277,13 @@ def cli():
12531277
parents=[common_parser],
12541278
)
12551279

1280+
func_parser.add_argument(
1281+
"--check-namespace",
1282+
help="Check namespacing of binaries",
1283+
action="store_true",
1284+
default=False,
1285+
)
1286+
12561287
# kat arguments
12571288
kat_parser = cmd_subparsers.add_parser(
12581289
"kat", help="Run the kat tests for all parameter sets", parents=[common_parser]

0 commit comments

Comments
 (0)