<commit_before>(*
 * Copyright (c) 2016 - present Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 *)

open! IStd

module F = Format
module L = Logging
module MF = MarkupFormatter

module Summary = Summary.Make (struct
    type payload = ThreadSafetyDomain.summary

    let update_payload post (summary : Specs.summary) =
      { summary with payload = { summary.payload with threadsafety = Some post }}

    let read_payload (summary : Specs.summary) =
      summary.payload.threadsafety
  end)

let is_owned access_path attribute_map =
  ThreadSafetyDomain.AttributeMapDomain.has_attribute
    access_path ThreadSafetyDomain.Attribute.unconditionally_owned attribute_map

let container_write_string = "infer.dummy.__CONTAINERWRITE__"

(* return (name of container, name of mutating function call) pair *)
let get_container_write_desc sink =
  let (base_var, _), access_list = fst (ThreadSafetyDomain.TraceElem.kind sink) in
  let get_container_write_desc_ call_name container_name =
    match String.chop_prefix (Fieldname.to_string call_name) ~prefix:container_write_string with
    | Some call_name -> Some (container_name, call_name)
    | None -> None in

  match List.rev access_list with
  |  FieldAccess call_name :: FieldAccess container_name :: _ ->
      get_container_write_desc_ call_name (Fieldname.to_string container_name)
  | [FieldAccess call_name] ->
      get_container_write_desc_ call_name (F.asprintf "%a" Var.pp base_var)
  | _ ->
      None

let is_container_write_sink sink =
  Option.is_some (get_container_write_desc sink)

module TransferFunctions (CFG : ProcCfg.S) = struct
  module CFG = CFG
  module Domain = ThreadSafetyDomain
  type extras = FormalMap.t

  type lock_model =
    | Lock
    | Unlock
    | LockedIfTrue
    | NoEffect

  let is_thread_utils_method method_name_str = function
    | Typ.Procname.Java java_pname ->
        String.is_suffix ~suffix:"ThreadUtils" (Typ.Procname.java_get_class_name java_pname)
        && String.equal (Typ.Procname.java_get_method java_pname) method_name_str
    | _ -> false

  let get_lock_model = function
    | Typ.Procname.Java java_pname ->
        if is_thread_utils_method "assertHoldsLock" (Typ.Procname.Java java_pname) then Lock
        else
          begin
            match Typ.Procname.java_get_class_name java_pname,
                  Typ.Procname.java_get_method java_pname with
            | ("java.util.concurrent.locks.Lock"
              | "java.util.concurrent.locks.ReentrantLock"
              | "java.util.concurrent.locks.ReentrantReadWriteLock$ReadLock"
              | "java.util.concurrent.locks.ReentrantReadWriteLock$WriteLock"),
              ("lock" | "lockInterruptibly") ->
                Lock
            | ("java.util.concurrent.locks.Lock"
              |"java.util.concurrent.locks.ReentrantLock"
              | "java.util.concurrent.locks.ReentrantReadWriteLock$ReadLock"
              | "java.util.concurrent.locks.ReentrantReadWriteLock$WriteLock"),
              "unlock" ->
                Unlock
            | ("java.util.concurrent.locks.Lock"
              | "java.util.concurrent.locks.ReentrantLock"
              | "java.util.concurrent.locks.ReentrantReadWriteLock$ReadLock"
              | "java.util.concurrent.locks.ReentrantReadWriteLock$WriteLock"),
              "tryLock" ->
                LockedIfTrue
            | _ ->
                NoEffect
          end
    | pname when Typ.Procname.equal pname BuiltinDecl.__set_locked_attribute -><commit_msg>[thread-safety] understand custom Buck locks<commit_after>(*
 * Copyright (c) 2016 - present Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 *)

open! IStd

module F = Format
module L = Logging
module MF = MarkupFormatter

module Summary = Summary.Make (struct
    type payload = ThreadSafetyDomain.summary

    let update_payload post (summary : Specs.summary) =
      { summary with payload = { summary.payload with threadsafety = Some post }}

    let read_payload (summary : Specs.summary) =
      summary.payload.threadsafety
  end)

let is_owned access_path attribute_map =
  ThreadSafetyDomain.AttributeMapDomain.has_attribute
    access_path ThreadSafetyDomain.Attribute.unconditionally_owned attribute_map

let container_write_string = "infer.dummy.__CONTAINERWRITE__"

(* return (name of container, name of mutating function call) pair *)
let get_container_write_desc sink =
  let (base_var, _), access_list = fst (ThreadSafetyDomain.TraceElem.kind sink) in
  let get_container_write_desc_ call_name container_name =
    match String.chop_prefix (Fieldname.to_string call_name) ~prefix:container_write_string with
    | Some call_name -> Some (container_name, call_name)
    | None -> None in

  match List.rev access_list with
  |  FieldAccess call_name :: FieldAccess container_name :: _ ->
      get_container_write_desc_ call_name (Fieldname.to_string container_name)
  | [FieldAccess call_name] ->
      get_container_write_desc_ call_name (F.asprintf "%a" Var.pp base_var)
  | _ ->
      None

let is_container_write_sink sink =
  Option.is_some (get_container_write_desc sink)

module TransferFunctions (CFG : ProcCfg.S) = struct
  module CFG = CFG
  module Domain = ThreadSafetyDomain
  type extras = FormalMap.t

  type lock_model =
    | Lock
    | Unlock
    | LockedIfTrue
    | NoEffect

  let is_thread_utils_method method_name_str = function
    | Typ.Procname.Java java_pname ->
        String.is_suffix ~suffix:"ThreadUtils" (Typ.Procname.java_get_class_name java_pname)
        && String.equal (Typ.Procname.java_get_method java_pname) method_name_str
    | _ -> false

  let get_lock_model = function
    | Typ.Procname.Java java_pname ->
        if is_thread_utils_method "assertHoldsLock" (Typ.Procname.Java java_pname) then Lock
        else
          begin
            match Typ.Procname.java_get_class_name java_pname,
                  Typ.Procname.java_get_method java_pname with
            | ("java.util.concurrent.locks.Lock"
              | "java.util.concurrent.locks.ReentrantLock"
              | "java.util.concurrent.locks.ReentrantReadWriteLock$ReadLock"
              | "java.util.concurrent.locks.ReentrantReadWriteLock$WriteLock"),
              ("lock" | "lockInterruptibly") ->
                Lock
            | ("java.util.concurrent.locks.Lock"
              |"java.util.concurrent.locks.ReentrantLock"
              | "java.util.concurrent.locks.ReentrantReadWriteLock$ReadLock"
              | "java.util.concurrent.locks.ReentrantReadWriteLock$WriteLock"),
              "unlock" ->
                Unlock
            | ("java.util.concurrent.locks.Lock"
              | "java.util.concurrent.locks.ReentrantLock"
              | "java.util.concurrent.locks.ReentrantReadWriteLock$ReadLock"
              | "java.util.concurrent.locks.ReentrantReadWriteLock$WriteLock"),
              "tryLock" ->
                LockedIfTrue
            | "com.facebook.buck.util.concurrent.AutoCloseableReadWriteUpdateLock",
              ("readLock" | "updateLock" | "writeLock") ->
                Lock
            | _ ->
                NoEffect
          end
    | pname when Typ.Procname.equal pname BuiltinDecl.__set_locked_attribute ->
{'fraction_non_alphanumeric': 0.08292874950793859, 'fraction_numerical': 0.0010497310064296024, 'mean_word_length': 2.9187660668380464, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7467291', 'n_tokens_mistral': 2295, 'n_tokens_neox': 2200, 'n_words': 497}