Skip to content

Commit 8de0262

Browse files
authored
Arm backend: Add linting for banned functions (pytorch#21613)
* Use CPP_BANNED_FUNCTIONS linter in lintrunner * Bump lintrunner-adapters version Signed-off-by: Tom Allsop <tom.allsop@arm.com> Change-Id: Ic93db54b4de0e2248fff61d081bc066d4520db3e cc @digantdesai @freddan80 @per @zingo @oscarandersson8218 @mansnils @Sebastian-Larsson @robell @rascani --------- Signed-off-by: Tom Allsop <tom.allsop@arm.com>
1 parent a8256b7 commit 8de0262

9 files changed

Lines changed: 167 additions & 6 deletions

File tree

‎.github/workflows/claude-code.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
uses: pytorch/test-infra/.github/workflows/_claude-code.yml@main
1212
with:
1313
setup_script: |
14-
pip install lintrunner==0.12.7 lintrunner-adapters==0.14.0
14+
pip install lintrunner==0.12.7 lintrunner-adapters==0.14.1
1515
pip install -r requirements-lintrunner.txt
1616
lintrunner init
1717
permissions:

‎.github/workflows/lint.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ jobs:
4646
- name: Install dependencies
4747
run: |
4848
pip install torch --index-url https://download.pytorch.org/whl/cpu
49-
pip install lintrunner==0.12.7 lintrunner-adapters==0.14.0
49+
pip install lintrunner==0.12.7 lintrunner-adapters==0.14.1
5050
pip install -r requirements-lintrunner.txt
5151
USE_CPP=0 pip install --no-build-isolation third-party/ao
5252
pip install pytest numpy parameterized huggingface_hub transformers timm expecttest types-requests
@@ -93,7 +93,7 @@ jobs:
9393

9494
- name: Install lintrunner and linters
9595
run: |
96-
pip install lintrunner==0.12.7 lintrunner-adapters==0.14.0
96+
pip install lintrunner==0.12.7 lintrunner-adapters==0.14.1
9797
pip install -r requirements-lintrunner.txt
9898
lintrunner init
9999

‎.lintrunner.toml‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,3 +735,28 @@ init_command = [
735735
'--requirement=requirements-lintrunner.txt',
736736
]
737737
is_formatter = true
738+
739+
740+
[[linter]]
741+
code = 'CPP_BANNED_FUNCTIONS'
742+
include_patterns = [
743+
'examples/arm/**/*.c',
744+
'examples/arm/**/*.cc',
745+
'examples/arm/**/*.cpp',
746+
'examples/arm/**/*.h',
747+
'examples/arm/**/*.hpp',
748+
]
749+
exclude_patterns = [
750+
'third-party/**',
751+
'**/third-party/**',
752+
]
753+
command = [
754+
'python',
755+
'-m',
756+
'lintrunner_adapters',
757+
'run',
758+
'cpp_banned_functions_linter',
759+
'--config=backends/arm/scripts/banned_functions.json',
760+
'--',
761+
'@{{PATHSFILE}}',
762+
]

‎backends/arm/scripts/LICENSE-MIT‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
{
2+
"banned_functions": [
3+
{
4+
"names": ["alloca"],
5+
"replacements": ["malloc", "new"]
6+
},
7+
{
8+
"names": ["gets"],
9+
"replacements": ["fgets"]
10+
},
11+
{
12+
"names": ["memcpy"],
13+
"replacements": ["memcpy_s"]
14+
},
15+
{
16+
"names": ["wmemcpy"],
17+
"replacements": ["wmemcpy_s"]
18+
},
19+
{
20+
"names": ["memmove"],
21+
"replacements": ["memmove_s"]
22+
},
23+
{
24+
"names": ["wmemmove"],
25+
"replacements": ["wmemmove_s"]
26+
},
27+
{
28+
"names": ["readdir_r"],
29+
"replacements": ["readdir"]
30+
},
31+
{
32+
"names": ["scanf", "wscanf", "_tscanf", "sscanf", "swscanf", "_stscanf"],
33+
"replacements": ["fgets"]
34+
},
35+
{
36+
"names": [
37+
"strcat",
38+
"wcscat",
39+
"_tcscat",
40+
"_mbscat",
41+
"StrCat",
42+
"lstrcat",
43+
"lStrCatBuff",
44+
"_tccat",
45+
"_mbccat",
46+
"_ftcscat",
47+
"strncat",
48+
"wcsncat",
49+
"_tcsncat",
50+
"_mbsncat",
51+
"_mbsnbcat",
52+
"StrCatN",
53+
"StrNCat",
54+
"lstrncat",
55+
"lstrcatn"
56+
],
57+
"replacements": ["strcat_s", "strncat_s"]
58+
},
59+
{
60+
"names": [
61+
"strcpy",
62+
"wcscpy",
63+
"_tcscpy",
64+
"_mbscpy",
65+
"StrCpy",
66+
"lstrcpy",
67+
"_tccpy",
68+
"_mbccpy",
69+
"_ftcscpy",
70+
"strncpy",
71+
"wcsncpy",
72+
"_tcsncpy",
73+
"_mbsncpy",
74+
"_mbsnbcpy",
75+
"StrCpyN",
76+
"StrNCpy",
77+
"lstrcpyn"
78+
],
79+
"replacements": ["strcpy_s", "strncpy_s"]
80+
},
81+
{
82+
"names": ["strlen"],
83+
"replacements": ["strnlen_s"]
84+
},
85+
{
86+
"names": ["wcslen"],
87+
"replacements": ["wcsnlen_s"]
88+
},
89+
{
90+
"names": ["strtok", "_tcstok", "wcstok", "_mbstok"],
91+
"replacements": ["strtok_s"]
92+
},
93+
{
94+
"names": [
95+
"wsprintf",
96+
"sprintf",
97+
"swprintf",
98+
"_stprintf",
99+
"wvsprintf",
100+
"vsprintf",
101+
"_vstprintf",
102+
"vswprintf"
103+
],
104+
"replacements": ["snprintf", "snprintf_s"]
105+
}
106+
]
107+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com>
2+
SPDX-License-Identifier: MIT
3+
4+
File copied from https://github.com/justinchuby/lintrunner-adapters/ which has an MIT license.
5+
File permalink: https://github.com/justinchuby/lintrunner-adapters/blob/772e5936d9b02814e29e12ca1355d33b029688c2/examples/adapters/cpp_banned_functions_linter/example_banned_functions.json

‎examples/arm/executor_runner/arm_executor_runner.cpp‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,10 @@ Error prepare_input_tensors(
489489
} else if (input_evalues[i].isTensor()) {
490490
// Copy the data from the input buffer to the tensor
491491
Tensor& tensor = input_evalues[i].toTensor();
492-
std::memcpy(tensor.mutable_data_ptr<int8_t>(), buffer, buffer_size);
492+
std::memcpy( // NOLINT(memcpy) - Buffer size is checked
493+
tensor.mutable_data_ptr<int8_t>(),
494+
buffer,
495+
buffer_size);
493496
}
494497
}
495498

‎repro_mypy_ci.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ run_in_env() {
2929

3030
run_in_env python -m pip install --upgrade pip
3131
run_in_env python -m pip install torch --index-url https://download.pytorch.org/whl/cpu
32-
run_in_env python -m pip install lintrunner==0.12.7 lintrunner-adapters==0.14.0
32+
run_in_env python -m pip install lintrunner==0.12.7 lintrunner-adapters==0.14.1
3333
run_in_env python -m pip install -r requirements-lintrunner.txt
3434
run_in_env lintrunner init
3535

‎requirements-dev.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ wheel # For building the pip package archive.
99
zstd # Imported by resolve_buck.py.
1010
certifi # Imported by resolve_buck.py.
1111
lintrunner==0.12.7
12-
lintrunner-adapters==0.14.0
12+
lintrunner-adapters==0.14.1
1313
ruff==0.15.4
1414

1515
pytest<9.0

0 commit comments

Comments
 (0)