|
| 1 | +// semmle-extractor-options: /r:System.Security.Cryptography.Csp.dll /r:System.Security.Cryptography.Algorithms.dll /r:System.Security.Cryptography.Primitives.dll /r:System.Threading.Tasks.dll /r:System.Threading.Thread.dll /r:System.Linq.dll /r:System.Collections.dll /r:System.Threading.Tasks.Parallel.dll |
| 2 | +using System; |
| 3 | +using System.Linq; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Threading; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using System.Security.Cryptography; |
| 8 | + |
| 9 | +class DirectUsagePositiveCase |
| 10 | +{ |
| 11 | + public static void Run(int max) |
| 12 | + { |
| 13 | + const int threadCount = 4; |
| 14 | + |
| 15 | + // This variable is used in multiple threads |
| 16 | + var sha1 = SHA1.Create(); |
| 17 | + Action start = () => { |
| 18 | + for (int i = 0; i < max; i++) |
| 19 | + { |
| 20 | + var bytes = new byte[4]; |
| 21 | + sha1.ComputeHash(bytes); |
| 22 | + } |
| 23 | + }; |
| 24 | + |
| 25 | + // BUG expected |
| 26 | + var threads = Enumerable.Range(0, threadCount) |
| 27 | + .Select(_ => new ThreadStart(start)) |
| 28 | + .Select(x => new Thread(x)) |
| 29 | + .ToList(); |
| 30 | + foreach (var t in threads) t.Start(); |
| 31 | + foreach (var t in threads) t.Join(); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +class DirectUsageNegativeCase |
| 36 | +{ |
| 37 | + public static void Run(int max) |
| 38 | + { |
| 39 | + const int threadCount = 4; |
| 40 | + Action start = () => { |
| 41 | + for (int i = 0; i < max; i++) |
| 42 | + { |
| 43 | + var sha1 = SHA1.Create(); |
| 44 | + var bytes = new byte[4]; |
| 45 | + sha1.ComputeHash(bytes); |
| 46 | + } |
| 47 | + }; |
| 48 | + var threads = Enumerable.Range(0, threadCount) |
| 49 | + .Select(_ => new ThreadStart(start)) |
| 50 | + .Select(x => new Thread(x)) |
| 51 | + .ToList(); |
| 52 | + foreach (var t in threads) t.Start(); |
| 53 | + foreach (var t in threads) t.Join(); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +public class Nest01 |
| 58 | +{ |
| 59 | + private readonly SHA256 _sha; |
| 60 | + |
| 61 | + public Nest01() |
| 62 | + { |
| 63 | + _sha = SHA256.Create(); |
| 64 | + } |
| 65 | + |
| 66 | + public byte[] ComputeHash(byte[] bytes) |
| 67 | + { |
| 68 | + return _sha.ComputeHash(bytes); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +class IndirectUsagePositiveCase |
| 73 | +{ |
| 74 | + public static void Run(int max) |
| 75 | + { |
| 76 | + const int threadCount = 4; |
| 77 | + // This variable is used in multiple threads |
| 78 | + var sha1 = new Nest01(); |
| 79 | + |
| 80 | + // BUG expected |
| 81 | + Action start = () => { |
| 82 | + for (int i = 0; i < max; i++) |
| 83 | + { |
| 84 | + var bytes = new byte[4]; |
| 85 | + sha1.ComputeHash(bytes); |
| 86 | + } |
| 87 | + }; |
| 88 | + var threads = Enumerable.Range(0, threadCount) |
| 89 | + .Select(_ => new ThreadStart(start)) |
| 90 | + .Select(x => new Thread(x)) |
| 91 | + .ToList(); |
| 92 | + foreach (var t in threads) t.Start(); |
| 93 | + foreach (var t in threads) t.Join(); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +class IndirectUsageNegativeCase |
| 98 | +{ |
| 99 | + public static void Run(int max) |
| 100 | + { |
| 101 | + const int threadCount = 4; |
| 102 | + Action start = () => { |
| 103 | + for (int i = 0; i < max; i++) |
| 104 | + { |
| 105 | + var sha1 = new Nest01(); |
| 106 | + var bytes = new byte[4]; |
| 107 | + sha1.ComputeHash(bytes); |
| 108 | + } |
| 109 | + }; |
| 110 | + var threads = Enumerable.Range(0, threadCount) |
| 111 | + .Select(_ => new ThreadStart(start)) |
| 112 | + .Select(x => new Thread(x)) |
| 113 | + .ToList(); |
| 114 | + foreach (var t in threads) t.Start(); |
| 115 | + foreach (var t in threads) t.Join(); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +class LambdaNotStart |
| 120 | +{ |
| 121 | + public static void Run() |
| 122 | + { |
| 123 | + var sha1 = SHA1.Create(); |
| 124 | + |
| 125 | + Func<string> myFunc = () => |
| 126 | + { |
| 127 | + var bytes = new byte[4]; |
| 128 | + return Convert.ToBase64String(sha1.ComputeHash(bytes)); |
| 129 | + }; |
| 130 | + |
| 131 | + var d = myFunc.DynamicInvoke(); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +class ParallelInvoke |
| 136 | +{ |
| 137 | + public static void Run() |
| 138 | + { |
| 139 | + var sha1 = SHA1.Create(); |
| 140 | + |
| 141 | + try |
| 142 | + { |
| 143 | + Parallel.Invoke(() => |
| 144 | + { |
| 145 | + var bytes = new byte[4]; |
| 146 | + Convert.ToBase64String(sha1.ComputeHash(bytes)); |
| 147 | + }, |
| 148 | + () => |
| 149 | + { |
| 150 | + var bytes = new byte[4]; |
| 151 | + Convert.ToBase64String(sha1.ComputeHash(bytes)); |
| 152 | + } |
| 153 | + ); |
| 154 | + |
| 155 | + } |
| 156 | + catch (AggregateException e) |
| 157 | + { |
| 158 | + Console.WriteLine("An action has thrown an exception. THIS WAS UNEXPECTED.\n{0}", e.InnerException.ToString()); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments