Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switches to Fowler-Noll-Vo hash algorithm for hashing names #1221

Merged
merged 1 commit into from Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 15 additions & 9 deletions lib/knowledge/bap_knowledge.ml
Expand Up @@ -170,7 +170,7 @@ type slot_status =
type fullname = {
package : string;
name : string;
} [@@deriving bin_io, compare, sexp]
} [@@deriving bin_io, equal, compare, sexp]


module Name : sig
Expand Down Expand Up @@ -218,22 +218,26 @@ end = struct

let registry = Hashtbl.create (module Int63)

let hash_name str =
(* using FNV-1a algorithm *)
let hash_name =
let open Int63 in
String.fold str ~init:(of_int 5381) ~f:(fun h c ->
(h lsl 5) + h + of_int (Char.to_int c))
let init = of_int64_exn 0xCBF29CE484222325L in
let m = of_int64_exn 0x100000001B3L in
let hash init = String.fold ~init ~f:(fun h c ->
(h lxor of_int (Char.to_int c)) * m) in
fun {package; name} ->
hash (hash init package) name

let intern name =
let str = full name in
let id = hash_name str in
let id = hash_name name in
match Hashtbl.find registry id with
| None -> Hashtbl.add_exn registry id name; id
| Some name ->
if full name = str
| Some name' ->
if equal_fullname name name'
then id
else invalid_argf "Names %S and %S have the same hash value, \
Change one of them."
(full name) str ()
(full name) (full name') ()

let fullname = Hashtbl.find_exn registry
include Int63
Expand Down Expand Up @@ -796,6 +800,8 @@ module Persistent = struct
let key = of_string pk key
and data = of_string pd data in
Map.add_exn xs ~key ~data))

let name = of_binable (module Name)
end


Expand Down
6 changes: 5 additions & 1 deletion lib/knowledge/bap_knowledge.mli
Expand Up @@ -1278,6 +1278,11 @@ module Knowledge : sig
(** string is a persistent data type. *)
val string : string persistent

(** names are persistent.

@since 2.2.0 *)
val name : name persistent

(** [list t] derives persistence for a list. *)
val list : 'a persistent -> 'a list persistent

Expand Down Expand Up @@ -1351,7 +1356,6 @@ module Knowledge : sig
*)
val create : ?package:string -> string -> t


(** [read ?package input] reads a full name from input.

This function will parse the [input] and return a
Expand Down