From 1389af1851298843e39996e6b04b211815a3ff08 Mon Sep 17 00:00:00 2001 From: Iulia Dumitru Date: Fri, 4 Aug 2023 14:44:41 +0200 Subject: [PATCH] Allow pattern inside static block, add low confidence label --- lang/correctness/index_by_threadid.jl | 15 ++++++------ lang/correctness/index_by_threadid.yaml | 31 +++++++++++++++++++++++++ lang/correctness/index_by_threadid.yml | 21 ----------------- 3 files changed, 38 insertions(+), 29 deletions(-) create mode 100644 lang/correctness/index_by_threadid.yaml delete mode 100644 lang/correctness/index_by_threadid.yml diff --git a/lang/correctness/index_by_threadid.jl b/lang/correctness/index_by_threadid.jl index 8105054..5fcdc5c 100644 --- a/lang/correctness/index_by_threadid.jl +++ b/lang/correctness/index_by_threadid.jl @@ -1,7 +1,7 @@ -using Base.Threads +using Base.Threads: threadid, @threads states = zeros(10) -Threads.@spawn for i = 1:10 +Threads.@threads for i = 1:10 # ruleid: index-by-threadid tid = Threads.threadid() old_val = states[tid] @@ -9,20 +9,19 @@ Threads.@spawn for i = 1:10 states[tid] = new_val end -@spawn begin +@threads for i = 1:10 # ruleid: index-by-threadid tid = threadid() val = states[tid] states[tid] = val + 1 end -Threads.@spawn for i = 1:10 +@threads :foo for i = 1:10 # ruleid: index-by-threadid - val = states[threadid()] + val = states[Threads.threadid()] end - -Threads.@spawn for i = 1:10 - # ruleid: index-by-threadid +Threads.@threads :static for i = 1:10 + # ok: index-by-threadid val = states[Threads.threadid()] end diff --git a/lang/correctness/index_by_threadid.yaml b/lang/correctness/index_by_threadid.yaml new file mode 100644 index 0000000..9b4d6c5 --- /dev/null +++ b/lang/correctness/index_by_threadid.yaml @@ -0,0 +1,31 @@ +rules: + - id: index-by-threadid + patterns: + - pattern-inside: | + @threads ... for $X = ... + ... + end + - pattern-not-inside: | + @threads :static for $X = ... + ... + end + - pattern-either: + - pattern: $S[Threads.threadid()] + - pattern: $S[threadid()] + - pattern: | + $TID = Threads.threadid() + ... + $Y = $S[$TID] + - pattern: | + $TID = threadid() + ... + $Y = $S[$TID] + message: Indexing by `threadid()` may cause race conditions and should be avoided. + metadata: + confidence: LOW + references: + - https://www.julialang.org/blog/2023/07/PSA-dont-use-threadid/ + license: LGPL + languages: + - julia + severity: WARNING diff --git a/lang/correctness/index_by_threadid.yml b/lang/correctness/index_by_threadid.yml deleted file mode 100644 index c094617..0000000 --- a/lang/correctness/index_by_threadid.yml +++ /dev/null @@ -1,21 +0,0 @@ -rules: - - id: index-by-threadid - pattern-either: - - pattern: | - $TID = Threads.threadid() - ... - $X = $S[$TID] - - pattern: | - $TID = threadid() - ... - $X = $S[$TID] - - pattern: $S[threadid()] - - pattern: $S[Threads.threadid()] - message: Indexing by `threadid()` may cause race conditions and should be avoided. - metadata: - references: - - https://www.julialang.org/blog/2023/07/PSA-dont-use-threadid/ - license: LGPL - languages: - - julia - severity: WARNING