Skip to content

Commit 11142df

Browse files
authored
Merge pull request github#15764 from michaelnebel/csharp/csharptracerrunapp
C#: Don't inject compiler flags when dotnet is used to execute an application.
2 parents 9c2322d + ac4ad0c commit 11142df

File tree

4 files changed

+39
-24
lines changed

4 files changed

+39
-24
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Console.WriteLine(args[0]);
1+
Console.WriteLine($"<arguments>{string.Join(",", args)}</arguments>");
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
from create_database_utils import *
22
from diagnostics_test_utils import *
33

4-
run_codeql_database_create(['dotnet build'], db=None, lang="csharp")
5-
check_diagnostics()
4+
def check_build_out(msg, s):
5+
if "[build-stdout] " + msg not in s:
6+
raise Exception("The C# tracer did not interpret the dotnet path-to-application command correctly.")
7+
8+
run_codeql_database_create(['dotnet build'], test_db="test1-db", lang="csharp")
9+
check_diagnostics(test_db="test1-db")
10+
11+
# This test checks that we don't inject any flags when running the application using `dotnet`
12+
my_dir = "my_program"
13+
my_abs_path = os.path.abspath(f"{my_dir}/dotnet_build.dll")
14+
s = run_codeql_database_create_stdout(['dotnet clean', 'rm -rf test1-db', 'dotnet build -o my_program', f'dotnet {my_abs_path} build is not a subcommand'], "test2-db", "csharp")
15+
check_build_out("<arguments>build,is,not,a,subcommand</arguments>", s)
16+
check_diagnostics(test_db="test2-db")
Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,57 @@
11
from create_database_utils import *
22
from diagnostics_test_utils import *
33

4-
def run_codeql_database_create_stdout(args, dbname):
5-
stdout = open(dbname + "file.txt", 'w+')
6-
run_codeql_database_create(args, test_db=dbname, db=None, stdout=stdout, lang="csharp")
7-
stdout.seek(0)
8-
s = stdout.read()
9-
stdout.close()
10-
return s
11-
124
def check_build_out(msg, s):
135
if "[build-stdout] " + msg not in s:
14-
raise Exception("The C# extractor did not interpret the 'dotnet run' command correctly")
6+
raise Exception("The C# tracer did not interpret the 'dotnet run' command correctly")
157

168
# no arguments
17-
s = run_codeql_database_create_stdout(['dotnet run'], "test-db")
9+
s = run_codeql_database_create_stdout(['dotnet run'], "test-db", "csharp")
1810
check_build_out("Default reply", s)
1911
check_diagnostics()
2012

2113
# no arguments, but `--`
22-
s = run_codeql_database_create_stdout(['dotnet clean', 'rm -rf test-db', 'dotnet run --'], "test2-db")
14+
s = run_codeql_database_create_stdout(['dotnet clean', 'rm -rf test-db', 'dotnet run --'], "test2-db", "csharp")
2315
check_build_out("Default reply", s)
2416
check_diagnostics(test_db="test2-db")
2517

2618
# one argument, no `--`
27-
s = run_codeql_database_create_stdout(['dotnet clean', 'rm -rf test2-db', 'dotnet run hello'], "test3-db")
19+
s = run_codeql_database_create_stdout(['dotnet clean', 'rm -rf test2-db', 'dotnet run hello'], "test3-db", "csharp")
2820
check_build_out("Default reply", s)
2921
check_diagnostics(test_db="test3-db")
3022

3123
# one argument, but `--`
32-
s = run_codeql_database_create_stdout(['dotnet clean', 'rm -rf test3-db', 'dotnet run -- hello'], "test4-db")
24+
s = run_codeql_database_create_stdout(['dotnet clean', 'rm -rf test3-db', 'dotnet run -- hello'], "test4-db", "csharp")
3325
check_build_out("Default reply", s)
3426
check_diagnostics(test_db="test4-db")
3527

3628
# two arguments, no `--`
37-
s = run_codeql_database_create_stdout(['dotnet clean', 'rm -rf test4-db', 'dotnet run hello world'], "test5-db")
29+
s = run_codeql_database_create_stdout(['dotnet clean', 'rm -rf test4-db', 'dotnet run hello world'], "test5-db", "csharp")
3830
check_build_out("hello, world", s)
3931
check_diagnostics(test_db="test5-db")
4032

4133
# two arguments, and `--`
42-
s = run_codeql_database_create_stdout(['dotnet clean', 'rm -rf test5-db', 'dotnet run -- hello world'], "test6-db")
34+
s = run_codeql_database_create_stdout(['dotnet clean', 'rm -rf test5-db', 'dotnet run -- hello world'], "test6-db", "csharp")
4335
check_build_out("hello, world", s)
4436
check_diagnostics(test_db="test6-db")
4537

4638
# shared compilation enabled; tracer should override by changing the command
4739
# to `dotnet run -p:UseSharedCompilation=true -p:UseSharedCompilation=false -- hello world`
48-
s = run_codeql_database_create_stdout(['dotnet clean', 'rm -rf test6-db', 'dotnet run -p:UseSharedCompilation=true -- hello world'], "test7-db")
40+
s = run_codeql_database_create_stdout(['dotnet clean', 'rm -rf test6-db', 'dotnet run -p:UseSharedCompilation=true -- hello world'], "test7-db", "csharp")
4941
check_build_out("hello, world", s)
5042
check_diagnostics(test_db="test7-db")
5143

5244
# option passed into `dotnet run`
53-
s = run_codeql_database_create_stdout(['dotnet clean', 'rm -rf test7-db', 'dotnet build', 'dotnet run --no-build hello world'], "test8-db")
45+
s = run_codeql_database_create_stdout(['dotnet clean', 'rm -rf test7-db', 'dotnet build', 'dotnet run --no-build hello world'], "test8-db", "csharp")
5446
check_build_out("hello, world", s)
5547
check_diagnostics(test_db="test8-db")
5648

5749
# two arguments, no '--' (first argument quoted)
58-
s = run_codeql_database_create_stdout(['dotnet clean', 'rm -rf test8-db', 'dotnet run "hello world part1" part2'], "test9-db")
50+
s = run_codeql_database_create_stdout(['dotnet clean', 'rm -rf test8-db', 'dotnet run "hello world part1" part2'], "test9-db", "csharp")
5951
check_build_out("hello world part1, part2", s)
6052
check_diagnostics(test_db="test9-db")
6153

6254
# two arguments, no '--' (second argument quoted) and using dotnet to execute dotnet
63-
s = run_codeql_database_create_stdout(['dotnet clean', 'rm -rf test9-db', 'dotnet dotnet run part1 "hello world part2"'], "test10-db")
55+
s = run_codeql_database_create_stdout(['dotnet clean', 'rm -rf test9-db', 'dotnet dotnet run part1 "hello world part2"'], "test10-db", "csharp")
6456
check_build_out("part1, hello world part2", s)
6557
check_diagnostics(test_db="test10-db")

csharp/tools/tracing-config.lua

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ function RegisterExtractorPack(id)
2323
not isDotnetPath(arg)
2424
end
2525

26+
local function isPathToExecutable(path)
27+
return path:match('%.exe$') or path:match('%.dll')
28+
end
29+
2630
function DotnetMatcherBuild(compilerName, compilerPath, compilerArguments,
2731
_languageId)
2832
if not isDotnet(compilerName) then
@@ -56,8 +60,16 @@ function RegisterExtractorPack(id)
5660
NativeArgumentsToArgv(compilerArguments.nativeArgumentPointer)
5761
end
5862
for i, arg in ipairs(argv) do
63+
-- if dotnet is being used to execute any application except dotnet itself, we should
64+
-- not inject any flags.
65+
if not match and isPathToExecutable(arg) and not isDotnetPath(arg) then
66+
Log(1, 'Execute a .NET application usage detected')
67+
Log(1, 'Dotnet path-to-application detected: %s', arg)
68+
break
69+
end
5970
if isPossibleDotnetSubcommand(arg) then
60-
if (not match) and inSubCommandPosition then
71+
if not match and inSubCommandPosition then
72+
Log(1, 'Execute a .NET SDK command usage detected')
6173
Log(1, 'Dotnet subcommand detected: %s', arg)
6274
end
6375
-- only respond to strings that look like sub-command names if we have not yet
@@ -85,7 +97,7 @@ function RegisterExtractorPack(id)
8597
end
8698
-- for `dotnet test`, we should not append `-p:UseSharedCompilation=false` to the command line
8799
-- if an `exe` or `dll` is passed as an argument as the call is forwarded to vstest.
88-
if testMatch and (arg:match('%.exe$') or arg:match('%.dll')) then
100+
if testMatch and isPathToExecutable(arg) then
89101
match = false
90102
break
91103
end

0 commit comments

Comments
 (0)