From nobody Sat Nov 30 01:58:05 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1620756453; cv=none; d=zohomail.com; s=zohoarc; b=Dl1KHFqrTuH3OrC31lrQrDGMt6SoYNuT0JO+MmlCSLVWPkhD9RRgIjYHri8VN49cywUEU/sfxK02ao46HMU5sfNJouyeKiCHuhzM7DiQVTEthS8pUAq87DB/72ykrB10kPY/zk0pZRNU7hZXolqlYwXUclMarVpiHVyPjCZkzKc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620756453; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=M6Om0zik6wsFaJKSuF7n0cGUF5K5yBhQTMlCUuOcgxU=; b=AEEyFOQqhoFHugpth+zaNaJksL7P5igKAE1+j04w89iExIVpR076QmfrAHDABcMBKLDg6GyTa2crnZbK1VJJYXCvwq51nOTRxWicnMjmCozfZbXptxPYehc89/VhI7d4AY4LZtIrbk4rzN5zXbCxS6quen8LFyTp/qB3JWLd/7M= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1620756453048408.96187827572953; Tue, 11 May 2021 11:07:33 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.125902.237025 (Exim 4.92) (envelope-from ) id 1lgWmm-00026S-8H; Tue, 11 May 2021 18:07:12 +0000 Received: by outflank-mailman (output) from mailman id 125902.237025; Tue, 11 May 2021 18:07:12 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lgWmm-00026F-3b; Tue, 11 May 2021 18:07:12 +0000 Received: by outflank-mailman (input) for mailman id 125902; Tue, 11 May 2021 18:07:10 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lgWmk-0001nY-Ej for xen-devel@lists.xenproject.org; Tue, 11 May 2021 18:07:10 +0000 Received: from esa4.hc3370-68.iphmx.com (unknown [216.71.155.144]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id f6ea6f30-41ac-4d6b-8f96-044bd00ac273; Tue, 11 May 2021 18:07:07 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: f6ea6f30-41ac-4d6b-8f96-044bd00ac273 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1620756427; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=7RVILLnjZIWgrnPBH52virpLPiH5emkE0QnosYt8EPU=; b=TKAvB5HF3vTRtiVKe2bIvqSPf0WnQwumqf6Hswy6V553XaUKNenbZjy8 H4FBtYmY1fDJ8HXqD6EKlnUphSagHP6+FJTifZrFI62AJLJUNrAuf2mWK o8+Jli/CleY4dmQAYneCqrhONIaTdWuYaHG2N8Kybwg+6e5CHUkQwCAJ2 c=; Authentication-Results: esa4.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: LOuE82vKbi49458eLw7TL0Ke43U6+ZNj/8Bqz/NrLuYslddHdwtEr9X2Jjrf/Kf3vLPTLc/vcV VSw/S1egbUjyZhq/UIFA0seJ8U+xmVH9wzjBAY+85JFcU1gfaGSwRdgQitwXgfvLsmeSoc6a5O oonX0QBAkMQOf/OsfTDWRRALN+mTeCMn8eppPeSCKQVdfa8VAVsNWJKWzb3g+1VyO3cmZIhfq3 PKouk2YEZW4RkXOyth9FFXylEVhbBjvMdcXzJz0vTPT63x2KPYzxTfAPrSsncAEV442IOcINMg 2tM= X-SBRS: 5.1 X-MesageID: 45101007 X-Ironport-Server: esa4.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED IronPort-HdrOrdr: A9a23:4ccXnqyt8H0/fsNrTgiqKrPwFr1zdoMgy1knxilNoRw8SK2lfq eV7YwmPH7P+U8ssR4b6LO90cW7Lk80sKQFhbX5Xo3SOjUO2lHYTr2KhLGKq1aLdkHDH6xmpM BdmsBFeabN5DNB7foSjjPXLz9Z+qjjzJyV X-IronPort-AV: E=Sophos;i="5.82,291,1613451600"; d="scan'208";a="45101007" From: =?UTF-8?q?Edwin=20T=C3=B6r=C3=B6k?= To: CC: =?UTF-8?q?Edwin=20T=C3=B6r=C3=B6k?= , "Christian Lindig" , David Scott , "Ian Jackson" , Wei Liu Subject: [PATCH v2 08/17] Add structured fuzzing unit test Date: Tue, 11 May 2021 19:05:21 +0100 Message-ID: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) Based on ideas from qcstm, implemented using Crowbar. Quickcheck-style property tests that uses AFL for quickly exploring various values that trigger bugs in the code. This is structured/guided fuzzing: we read an arbitrary random number, and use it to generate some valid looking xenstore trees and commands. There are 2 instances of xenstored: one that runs the live update command, and one that ignores it. Live-update should be a no-op wrt to xenstored state: this is our quicheck property. When any mismatch is identified it prints the input (tree+xenstore commands), and a diff of the output: the internal xenstore tree state + quotas. afl-cmin can be used to further minimize the testcase. Crowbar (AFL persistent mode Quickcheck integration) is used due to speed: this very easily gets us a multi-core parallelizable test. Currently the Transaction tests fail, which is why live updates with active transactions are rejected. These tests are commented out. There is also some incomplete code here that attempts to find functional bugs in xenstored by interpeting xenstore commands in a simpler way and comparing states. This will build the fuzzer and run it single core for sanity test: make container-fuzz-sanity-test This will run it multicore (requires all dependencies installed on the host, including ocaml-bun, the multi-core AFL runner): make dune-oxenstored-fuzz 'make check' will also run the fuzzer but with input supplied by OCaml's random number generator, and for a very small number of iterations (few thousand). This doesn't require any external tools (no AFL, bun). On failure it prints a base64 encoding of the fuzzer state that can be used to reproduce the failure instantly, which is very useful for debugging: one can iterate on the failed fuzzer state until it is fixed, and then run the fuzzer again to find next failure. The unit tests here require OCaml 4.06, but the rest of the codebase doesn't (yet). See https://lore.kernel.org/xen-devel/cbb2742191e9c1303fdfd95feef4d829ecf33= a0d.camel@citrix.com/ for previous discussion of OCaml version. Signed-off-by: Edwin T=C3=B6r=C3=B6k --- tools/ocaml/Makefile | 19 + tools/ocaml/xenstored/process.ml | 12 +- tools/ocaml/xenstored/store.ml | 1 + tools/ocaml/xenstored/test/dune | 12 + tools/ocaml/xenstored/test/generator.ml | 189 +++++ tools/ocaml/xenstored/test/model.ml | 253 ++++++ tools/ocaml/xenstored/test/old/arbitrary.ml | 261 +++++++ tools/ocaml/xenstored/test/old/gen_paths.ml | 66 ++ .../xenstored/test/old/xenstored_test.ml | 527 +++++++++++++ tools/ocaml/xenstored/test/pathtree.ml | 40 + tools/ocaml/xenstored/test/testable.ml | 379 +++++++++ tools/ocaml/xenstored/test/types.ml | 437 +++++++++++ tools/ocaml/xenstored/test/xenstored_test.ml | 149 +++- tools/ocaml/xenstored/test/xs_protocol.ml | 733 ++++++++++++++++++ tools/ocaml/xenstored/transaction.ml | 119 ++- 15 files changed, 3188 insertions(+), 9 deletions(-) create mode 100644 tools/ocaml/xenstored/test/generator.ml create mode 100644 tools/ocaml/xenstored/test/model.ml create mode 100644 tools/ocaml/xenstored/test/old/arbitrary.ml create mode 100644 tools/ocaml/xenstored/test/old/gen_paths.ml create mode 100644 tools/ocaml/xenstored/test/old/xenstored_test.ml create mode 100644 tools/ocaml/xenstored/test/pathtree.ml create mode 100644 tools/ocaml/xenstored/test/testable.ml create mode 100644 tools/ocaml/xenstored/test/types.ml create mode 100644 tools/ocaml/xenstored/test/xs_protocol.ml diff --git a/tools/ocaml/Makefile b/tools/ocaml/Makefile index 53dd0a0f0d..de375820a3 100644 --- a/tools/ocaml/Makefile +++ b/tools/ocaml/Makefile @@ -67,3 +67,22 @@ dune-syntax-check: dune-pre .PHONY: build-oxenstored-dune dune-build-oxenstored: dune-pre LD_LIBRARY_PATH=3D$(LIBRARY_PATH) LIBRARY_PATH=3D$(LIBRARY_PATH) C_INCLUD= E_PATH=3D$(C_INCLUDE_PATH) dune build --profile=3Drelease @all + +.PHONY: oxenstored-fuzz1 oxenstored-fuzz +dune-oxenstored-fuzz: dune-pre + # --force is needed, otherwise it would cache a successful run + sh -c '. /etc/profile && C_INCLUDE_PATH=3D$(C_INCLUDE_PATH) dune build --= profile=3Drelease --no-buffer --force @fuzz' + +dune-oxenstored-fuzz1: dune-pre + sh -c '. /etc/profile && C_INCLUDE_PATH=3D$(C_INCLUDE_PATH) dune build --= profile=3Drelease --no-buffer --force @fuzz1' + +.PHONY: container-fuzz +container-fuzz-sanity-test: + dune clean + podman build -t oxenstored-fuzz . + # if UID is 0 then we get EPERM on setrlimit from inside the container + # use containerize script which ensures that uid is not 0 + # (podman/docker run would get us a uid of 0) + # Only do a sanity test with 1 core, actually doing fuzzing inside a cont= ainer is a bad idea + # due to FUSE overlayfs overhead + CONTAINER=3Doxenstored-fuzz CONTAINER_NO_PULL=3D1 DOCKER_CMD=3Dpodman ../= ../automation/scripts/containerize make -C tools/ocaml dune-oxenstored-fuzz1 diff --git a/tools/ocaml/xenstored/process.ml b/tools/ocaml/xenstored/proce= ss.ml index d573c88685..13b7153536 100644 --- a/tools/ocaml/xenstored/process.ml +++ b/tools/ocaml/xenstored/process.ml @@ -169,7 +169,7 @@ let parse_live_update args =3D ] (fun x -> raise (Arg.Bad x)) "live-update -s" ; - debug "Live update process queued" ; + (* debug "Live update process queued" ; *) {!state with deadline =3D Unix.gettimeofday () +. float !timeout ; force=3D !force; pending=3D true}) | _ -> @@ -449,6 +449,8 @@ let transaction_replay c t doms cons =3D (fun () -> try Logging.start_transaction ~con ~tid; + if t.must_fail then + raise Transaction_again; List.iter (perform_exn ~wlog:true replay_t) (Transaction.get_operation= s t); (* May throw EAGAIN *) =20 Logging.end_transaction ~con ~tid; @@ -550,7 +552,7 @@ let do_introduce con t domains cons data =3D | _ -> raise Invalid_Cmd_Args; in let dom =3D - if Domains.exist domains domid then + if Domains.exist domains domid then begin let edom =3D Domains.find domains domid in if (Domain.get_mfn edom) =3D mfn && (Connections.find_domain cons domid= ) !=3D con then begin (* Use XS_INTRODUCE for recreating the xenbus event-channel. *) @@ -558,12 +560,16 @@ let do_introduce con t domains cons data =3D Domain.bind_interdomain edom; end; edom + end else try let ndom =3D Domains.create domains domid mfn port in Connections.add_domain cons ndom; Connections.fire_spec_watches (Transaction.get_root t) cons Store.Path.= introduce_domain; ndom - with _ -> raise Invalid_Cmd_Args + with e -> + let bt =3D Printexc.get_backtrace () in + Logging.debug "process" "do_introduce: %s (%s)" (Printexc.to_string e)= bt; + raise Invalid_Cmd_Args in if (Domain.get_remote_port dom) <> port || (Domain.get_mfn dom) <> mfn th= en raise Domain_not_match diff --git a/tools/ocaml/xenstored/store.ml b/tools/ocaml/xenstored/store.ml index 20e67b1427..85ca3daae9 100644 --- a/tools/ocaml/xenstored/store.ml +++ b/tools/ocaml/xenstored/store.ml @@ -133,6 +133,7 @@ let of_path_and_name path name =3D | _ -> path @ [name] =20 let create path connection_path =3D + Logging.debug "store" "Path.create %S %S" path connection_path; of_string (Utils.path_validate path connection_path) =20 let to_string t =3D diff --git a/tools/ocaml/xenstored/test/dune b/tools/ocaml/xenstored/test/d= une index 2a3eb2b7df..cd62926be9 100644 --- a/tools/ocaml/xenstored/test/dune +++ b/tools/ocaml/xenstored/test/dune @@ -9,3 +9,15 @@ (flags (:standard -w -52)) ;;(action (run %{test} -v --seed 364172147)) (libraries unix xen.bus xen.mmap xenstubs crowbar xen.store fmt fmt.tty)) + +(rule +(alias fuzz1) +(deps xenstored_test.exe) +(action (run bun -svv ./xenstored_test.exe)) +) + +(rule +(alias fuzz) +(deps xenstored_test.exe) +(action (run bun --no-kill ./xenstored_test.exe)) +) diff --git a/tools/ocaml/xenstored/test/generator.ml b/tools/ocaml/xenstore= d/test/generator.ml new file mode 100644 index 0000000000..6f7dc374f8 --- /dev/null +++ b/tools/ocaml/xenstored/test/generator.ml @@ -0,0 +1,189 @@ +module type S =3D sig + type cmd + + type state + + val init_state : state + + val next_state : cmd -> state -> state + + val precond : cmd -> state -> bool +end + +module IntSet =3D Set.Make (Int) +module IntMap =3D Map.Make (Int) + +module Pickable (K : sig + include Map.OrderedType + + include Hashtbl.HashedType with type t :=3D t +end) =3D +struct + (* allow picking a random value from a changing map keys. + Store a random value (hash of key) as first element of key, + then use find_first to pick an item related to the random element if = any. + This should be more efficient than converting to a list and using Lis= t.nth to pick + *) + module Key =3D struct + type t =3D int * K.t + + let of_key k =3D (K.hash k, k) + + let compare (h, k) (h', k') =3D + match Int.compare h h' with 0 -> K.compare k k' | r -> r + end + + module M =3D Map.Make (Key) + + type 'a t =3D 'a M.t + + let empty =3D M.empty + + let singleton k v =3D M.singleton (Key.of_key k) v + + let add k v m =3D M.add (Key.of_key k) v m + + let find_opt k m =3D M.find_opt (Key.of_key k) m + + let mem k m =3D M.mem (Key.of_key k) m + + let remove k m =3D M.remove (Key.of_key k) m + + let merge f m m' =3D M.merge f m m' + + let is_empty =3D M.is_empty + + let update k f m =3D M.update (Key.of_key k) f m + + let choose rnd m =3D + (* function needs to be monotonic, so the hash has to be part of the k= ey *) + let gte (keyhash, _) =3D Int.compare keyhash rnd >=3D 0 in + match M.find_first_opt gte m with + | Some ((_, k), _) -> + k + | None -> + snd @@ fst @@ M.min_binding m +end + +module PickablePath =3D Pickable (struct + type t =3D string + + let hash =3D Hashtbl.hash + + let compare =3D String.compare + + let equal =3D String.equal +end) + +module PickableInt =3D Pickable (struct + include Int + + let hash =3D Hashtbl.hash +end) + +module PathObserver =3D struct + type state =3D + { seen: unit PickablePath.t + ; dom_txs: unit PickableInt.t PickableInt.t + ; next_tid: int } + + let choose_path t rnd =3D PickablePath.choose rnd t.seen + + let choose_domid t rnd =3D PickableInt.choose rnd t.dom_txs + + let choose_txid_opt t domid rnd =3D + match PickableInt.find_opt domid t.dom_txs with + | None -> + 0 + | Some txs -> + if PickableInt.is_empty txs then 0 else PickableInt.choose rnd txs + + let new_domid domid =3D PickableInt.singleton domid PickableInt.empty + + let both _ _ _ =3D Some () + + let merge_txs _ s s' =3D + let s =3D Option.value ~default:PickableInt.empty s in + let s' =3D Option.value ~default:PickableInt.empty s' in + Some (PickableInt.merge both s s') + + let init_state =3D + {seen=3D PickablePath.singleton "/" (); dom_txs=3D new_domid 0; next_t= id=3D 1} + + let with_path path t =3D {t with seen=3D PickablePath.add path () t.seen} + + let split0 str =3D + match Process.split (Some 2) '\000' str with + | [x; y] -> + (x, y) + | _ -> + invalid_arg str + + let next_state (domid, cmd) t =3D + let open Xenbus.Xb in + match cmd with + | {Xenbus.Packet.ty=3D Transaction_start; _} -> + let update =3D function + | None -> + None + | Some txs -> + Some (PickableInt.add t.next_tid () txs) + in + { t with + dom_txs=3D PickableInt.update domid update t.dom_txs + ; next_tid=3D t.next_tid + 1 } + | { Xenbus.Packet.ty=3D + Op.( + ( Rm + | Read + | Directory + | Getperms + | Setperms + | Unwatch + | Reset_watches + | Getdomainpath + | Isintroduced + | Set_target + | Debug )) + ; _ } -> + t + | {Xenbus.Packet.ty=3D Op.(Watchevent | Error | Resume | Invalid); _} = -> + assert false + | {Xenbus.Packet.ty=3D Op.Transaction_end; tid; _} -> + let update =3D function + | None -> + None + | Some txs -> + Some (PickableInt.remove tid txs) + in + {t with dom_txs=3D PickableInt.update domid update t.dom_txs} + | {Xenbus.Packet.ty=3D Op.(Write | Mkdir | Watch); data} -> + let path, _ =3D split0 data in + with_path path t + | {Xenbus.Packet.ty=3D Introduce; data} -> + let domidstr, _ =3D split0 data in + let domid' =3D int_of_string domidstr in + if domid =3D 0 then + { t with + dom_txs=3D PickableInt.merge merge_txs t.dom_txs (new_domid do= mid') } + else t + | {Xenbus.Packet.ty=3D Release; data} -> + let domidstr, _ =3D split0 data in + let domid =3D int_of_string domidstr in + {t with dom_txs=3D PickableInt.remove domid t.dom_txs} + + let precond (domid, cmd) t =3D + ( match PickableInt.find_opt domid t.dom_txs with + | None -> + false + | Some txs -> + let tid =3D cmd.Xenbus.Packet.tid in + tid =3D 0 || PickableInt.mem tid txs ) + && Testable.Command.precond cmd t + + let pp =3D + let open Fmt in + Dump.record + [ Dump.field "domid" fst Fmt.int + ; Dump.field "cmd" snd Testable.Command.pp_dump ] +end diff --git a/tools/ocaml/xenstored/test/model.ml b/tools/ocaml/xenstored/te= st/model.ml new file mode 100644 index 0000000000..4b5ae462fb --- /dev/null +++ b/tools/ocaml/xenstored/test/model.ml @@ -0,0 +1,253 @@ +open Xs_protocol + +(* Conventions: +Aim for correctness, use simplest data structure that unambigously represe= nts state. + +E.g.: +* a list when duplicates are allowed, order matters and the empty list is = a valid value +* a set when elements appearing multiple time have the same semantic meani= ng as them appearing once, +and the order is unspecified or sorted +* a map when a single key is mapped to a single value, and order is unspec= ified or sorted + +When we must retain the original order for queries, but semantically it do= esn't matter +then store both a canonical representation and the original order. + +*) + +let rec string_for_all_from s f pos =3D + pos =3D String.length s || (f s.[pos] && (string_for_all_from s f @@ (po= s + 1))) + +type error =3D [`Msg of string] + +module Path : sig + (** a valid xenstore path *) + type t + + val root : t + + val of_string : string -> t option + (** [of_string path] parses [path]. + @return [None] if the path is syntactically not valid *) + + val to_string : t -> string + (** [to_string path] converts path to string. *) + + (** [is_child parent child] returns true if [child] is a child of [paren= t]. + A path is considered to be a child of itself *) + val is_child : t -> t -> bool +end =3D struct + type t =3D string list + + let is_valid_char =3D function + | '0' .. '9' | 'a' .. 'z' | 'A' .. 'Z' | '-' | '/' | '_' | '@' -> + true + | _ -> + false + + let root =3D [""] + + let nonempty s =3D String.length s > 0 + + let of_string s =3D + let n =3D String.length s in + if + n > 0 (* empty path is not permitted *) + && n < 1024 + (* paths cannot exceed 1024 chars, FIXME: relative vs absolute *) + && string_for_all_from s is_valid_char 0 + then + match String.split_on_char '/' s with + | [] -> + assert false + | [""; ""] -> + Some root + | _ :: tl as path -> + if List.for_all nonempty tl then Some path else None + else None + + let to_string =3D String.concat "/" + + let rec is_child p c =3D + match (p, c) with + | [], [] -> + true (* a path is a child of itself *) + | [], _ -> + true + | phd :: ptl, chd :: ctl when phd =3D chd -> + is_child ptl ctl + | _ -> + false +end + +module PathMap =3D Map.Make (String) +module DomidSet =3D Set.Make (Int) +module DomidMap =3D Map.Make (Int) + +let preserve_order =3D ref true + +module CanonicalACL =3D struct + module RW =3D struct + type t =3D {read: bool; write: bool} + + let of_perm =3D function + | ACL.NONE -> + {read=3D false; write=3D false} + | ACL.WRITE -> + {read=3D false; write=3D true} + | ACL.READ -> + {read=3D true; write=3D false} + | ACL.RDWR -> + {read=3D true; write=3D true} + + let to_perm =3D function + | {read=3D false; write=3D false} -> + ACL.NONE + | {read=3D false; write=3D true} -> + ACL.WRITE + | {read=3D true; write=3D false} -> + ACL.READ + | {read=3D true; write=3D true} -> + ACL.RDWR + + let full =3D {read=3D true; write=3D true} + end + + module RWMap =3D struct + type t =3D {fallback: RW.t; map: RW.t DomidMap.t} + + let lookup t domid =3D + (* other=3DRDWR can be overriden by explicitly revoking + permissions for a domain, so a read=3Dfalse,write=3Dfalse + in the DomidMap is not necessarily redundant + *) + DomidMap.find_opt domid t.map |> Option.value ~default:t.fallback + + let create fallback owner =3D + (* owner always has full permissions, and cannot be overriden *) + {fallback; map=3D DomidMap.singleton owner RW.full} + + let override t (domid, perm) =3D + let rw =3D RW.of_perm perm in + (* first entry wins, see perms.ml, also entries that are same as the= fallback are + not necessarily redundant: (b1,b2,r2) means that domid 2 has rdwr, + but if we remove the seemingly redundant `b2` entry then the over= ride would make it + just read which would be wrong. *) + if DomidMap.mem domid t.map then t + else {t with map=3D DomidMap.add domid rw t.map} + end + + type t =3D {original: ACL.t; owner: ACL.domid; acl: RWMap.t} + + let can_read t domid =3D (RWMap.lookup t.acl domid).read + + let can_write t domid =3D (RWMap.lookup t.acl domid).write + + let owner t =3D t.owner + + let of_acl original =3D + let fallback =3D RW.of_perm original.ACL.other in + let owner =3D original.ACL.owner in + let acl =3D + let init =3D RWMap.create fallback owner in + List.fold_left RWMap.override init original.ACL.acl + in + {original; owner; acl} + + let to_acl t =3D + if !preserve_order then t.original + else + ACL. + { owner=3D t.owner + ; other=3D RW.to_perm t.acl.fallback + ; acl=3D t.acl.map |> DomidMap.map RW.to_perm |> DomidMap.bindings= } +end + +module Store =3D struct + type node =3D {value: string; children: string list; acl: CanonicalACL.t} + + type t =3D {paths: node PathMap.t} + + let create () =3D {paths=3D PathMap.empty} + + let parent x =3D failwith "TODO" + + let add t key value =3D + let paths =3D PathMap.add key value t.paths in + {paths} + + let remove t key =3D + let paths =3D PathMap.remove key t.paths in + {paths} +end + +type t =3D Store.t + +let create () =3D Store.create () + +let reply_enoent =3D Response.Error "ENOENT" + +let reply_eexist =3D Response.Error "EEXIST" + +let with_node_read t path f =3D + ( t + , match PathMap.find_opt path t.paths with + | None -> + reply_enoent + | Some n -> + f n ) + +(* TODO: perm check *) +let perform_path t domid path =3D function + | Request.Read -> + with_node_read t path @@ fun n -> Response.Read n.value + | Request.Directory -> + with_node_read t path @@ fun n -> Response.Directory n.children + | Request.Directory_part _ -> + (t, Response.Error "ENOTSUP") + | Request.Getperms -> + with_node_read t path @@ fun n -> Response.Getperms n.acl + | Request.Write value -> ( + (* TODO: implicit mkdir *) + match PathMap.find_opt path t.paths with + | Some _ -> + (t, reply_eexist) + | None -> + let acl =3D ACL.{owner=3D domid; other=3D NONE; acl=3D []} in + let n =3D {value; children=3D []; acl} in + ({t with paths=3D PathMap.add path n t.paths}, Response.Write) ) + | Request.Setperms acl -> ( + match PathMap.find_opt path t.paths with + | Some _ -> + (t, reply_enoent) + | None -> + let update_node =3D function + | None -> + None + | Some n -> + Some {n with acl} + in + ( {t with paths=3D PathMap.update path update_node t.paths} + , Response.Setperms ) ) + | Request.Mkdir -> ( + (* TODO: implicit mkdir *) + match PathMap.find_opt path t.paths with + | Some _ -> + (t, reply_eexist) + | None -> + let acl =3D ACL.{owner=3D domid; other=3D NONE; acl=3D []} in + let n =3D {value=3D ""; children=3D []; acl} in + ({t with paths=3D PathMap.add path n t.paths}, Response.Mkdir) ) + | Request.Rm -> ( + match PathMap.find_opt path t.paths with + | None -> + (t, reply_enoent) + | Some _ -> + ({t with paths=3D PathMap.remove path t.paths}, Response.Rm) ) + +let perform t domid =3D function + | Request.PathOp (path, op) -> + perform_path t domid path op + | Request.Getdomainpath domid -> + (t, Response.Getdomainpath (Printf.sprintf "/local/domain/%d" domid)) + | _ -> + failwith "TODO" diff --git a/tools/ocaml/xenstored/test/old/arbitrary.ml b/tools/ocaml/xens= tored/test/old/arbitrary.ml new file mode 100644 index 0000000000..6b0bf9864a --- /dev/null +++ b/tools/ocaml/xenstored/test/old/arbitrary.ml @@ -0,0 +1,261 @@ +open QCheck + +(* See https://github.com/gasche/random-generator/blob/51351c16b587a1c4216= d158e847dcfa6db15f009/random_generator.mli#L275-L325 + for background on fueled generators for recursive data structures. + The difference here is that we build an N-ary tree, not a binary tree a= s in the example. + So we need to spread the fuel among elements of a list of random size. +*) + +(** [spread fuel] creates an array of a random size, and spreads fuel amon= g array elements. + Each array slot uses up at least 1 fuel itself. + For example the full list of possible arrays with [4] fuel is: + {[ [[|3|]; [|0; 2|]; [|1; 1|]; [|2; 0|]; [|0; 0; 0; 0|]] ]} +*) +let spread =3D function + | 0 -> + Gen.return [||] + | n when n < 0 -> + invalid_arg "negative fuel" + | n -> + Gen.( + 1 -- n + >>=3D fun per_element -> + (* We got n fuel to divide up, such that most elements have [per_e= lement] fuel. + Round up the number of elements *) + let m =3D (n + per_element - 1) / per_element in + (* each element uses up at least one fuel, this has to be subtract= ed before propagation *) + let a =3D Array.make m (per_element - 1) in + (* handle remainder *) + a.(0) <- n - (per_element * (m - 1)) - 1 ; + assert (Array.fold_left ( + ) m a =3D n) ; + (* ensure that remainder is in a random position *) + Gen.shuffle_a a >|=3D fun () -> a) + +(** [spread_l fuel sized_element] spreads [fuel] among list elements, + where each list element is created using [sized_element]. + [sized_element] needs to create an element of exactly the requested si= ze + (which may be a recursive element, that calls [spread_l] in turn). + Each list element consumes 1 fuel implicitly and sized_element is call= ed with decreased fuel. + *) +let spread_l fuel (sized_elem : 'a Gen.sized) =3D + Gen.( + spread fuel + >>=3D fun a -> + a |> Array.map sized_elem |> Gen.flatten_a |> Gen.map Array.to_list) + +module Tree =3D struct + (* For better shrinking put the (recursive) list first *) + type 'a t =3D Nodes of ('a t * 'a) list + + (** [empty] the empty tree (of size 1) *) + let empty =3D Nodes [] + + (** [nodes subtree] tree constructor *) + let nodes children =3D Nodes children + + (** [tree elem_gen] generates a random tree, with elements generated by = [elem_gen] *) + let tree elem =3D + Gen.sized @@ Gen.fix + @@ fun self fuel -> + (* self is the generator for a subtree *) + let node fuel =3D Gen.(pair (self fuel) elem) in + (* using spread_l ensures that fuel decreases by at least 1, thus ensu= ring termination *) + Gen.map nodes @@ spread_l fuel node + + (** [zero _] is a default implementation for [small] *) + let zero _ =3D 0 + + (** [small elem_size tree] returns the count of nodes in the tree and th= e sum of element sizes + as determined by [elem_size] *) + let rec small ?(elem_size =3D zero) (Nodes tree) =3D + List.fold_left + (fun acc (subtree, elem) -> + acc + elem_size elem + small ~elem_size subtree) + 1 tree + + (** [shrink ?elem tree] returns a list of potentially smaller trees base= d on [tree]. + *) + let shrink ?(elem =3D Shrink.nil) =3D + (* Shrinking needs to generate smaller trees (as determined by [small]= ), + QCheck will keep iterating until it finds a smaller tree that still= reproduces the bug. + It will then invoke the shrinker again on the smaller tree to attem= pt to shrink it further. + Once the tree shape cannot be shrunk further individual node elemen= ts will be shrunk. + *) + let rec tree (Nodes t) =3D + (* first try to shrink the subtree to a leaf, + and if that doesn't work then recursively shrink the subtree + *) + Iter.append (Iter.return empty) + @@ Iter.map nodes + @@ Shrink.list ~shrink:(Shrink.pair tree elem) t + in + tree + + (** [make arb] creates a tree generator with elements generated by [arb]. + The tree has a shrinker and size defined. + *) + let make arb =3D + let gen =3D tree @@ gen arb in + QCheck.make + ~small:(small ?elem_size:arb.small) + ~shrink:(shrink ?elem:arb.shrink) gen + + (** [paths_of_tree ~join tree] return all paths through the tree, + with path elements joined using [join] *) + let paths_of_tree ~join t =3D + let rec paths_of_subtree (paths, path) (Nodes nodes) =3D + ListLabels.fold_left nodes ~init:paths ~f:(fun paths (tree, elem) -> + let path =3D elem :: path in + paths_of_subtree (join (List.rev path) :: paths, path) tree) + in + paths_of_subtree ([], []) t + + let paths join arb =3D + make arb + (* we need to retain the tree, so that the shrinking is done on the tr= ee, + and not on the paths *) + |> map_keep_input (paths_of_tree ~join) +end + +module Case =3D struct + type ('a, 'b) t =3D + { case_tag: string + ; orig: 'a QCheck.arbitrary + ; map: 'a -> 'b + ; shrink: 'a -> 'b Iter.t + ; print: 'a Print.t + ; small: 'a -> int } + + (** [make arb f] defines a new variant case with constructor arguments + generated by [arb] and constructor [f]. *) + let make case_tag orig map =3D + let shrink a =3D + match orig.QCheck.shrink with + | None -> + Iter.empty + | Some s -> + Iter.map map @@ s a + in + let small a =3D match orig.QCheck.small with None -> 0 | Some s -> s a= in + let print a =3D match orig.QCheck.print with None -> "_" | Some p -> p= a in + {case_tag; orig; map; shrink; small; print} + + type 'a call =3D + { tag: string + ; shrink_lazy: 'a Iter.t Lazy.t + ; small_lazy: int Lazy.t + ; print: string Lazy.t } + + (** [call tag case args] used by the implementation of [rev] to build a = shrinker/small of appropriate type *) + let call t a =3D + { tag=3D t.case_tag + ; shrink_lazy=3D lazy (t.shrink a) + ; small_lazy=3D lazy (t.small a) + ; print=3D lazy (t.print a) } + + (** [to_sum case] converts all variant cases to the same type so they ca= n be put into a list *) + let to_sum t =3D Gen.map t.map @@ QCheck.gen t.orig +end + +(** [sum ~print ~rev cases] defines an arbitrary for a sum type consisting= of [cases] + variant case generators. [print] converts the sum type to a string. + [rev] matches on the sum type and should invoke [Case.call = ]. + + E.g. + {| + type t =3D A of int | B of float + + let case_a =3D Case.make "A" int (fun i -> A i) + + let case_b =3D Case.make "B" float (fun f -> B f) + + let rev t =3D + match t with A i -> Case.call case_a i | B g -> Case.call case_b g + + let x =3D + sum + ~print:(fun _ -> failwith "TODO") + [Case.to_sum case_a; Case.to_sum case_b] + |} + *) +let sum ~rev lst =3D + let shrink b =3D Lazy.force (rev b).Case.shrink_lazy in + let small b =3D Lazy.force (rev b).Case.small_lazy in + let collect b =3D (rev b).Case.tag in + let print b =3D let r =3D rev b in r.Case.tag ^ " " ^ Lazy.force r.print= in + QCheck.make ~shrink ~small ~collect ~print (Gen.oneof lst) + +(* +let mk_packet op to_string arb =3D + Case.make arb (fun x -> Xenbus.Packet.create 0 0 op (to_string x)) + +let read_packet =3D + mk_packet Xenbus.Xb.Op.Read Store.Path.to_string (list path_element) + +let write_packet =3D + mk_packet Xenbus.Xb.Op.Write + (fun (x, y) -> Store.Path.to_string x ^ "\x00" ^ y) + (pair (list path_element) binary) + +let packet =3D + sum ~print:Xenbus.Packet.to_string + [Case.to_sum read_packet; Case.to_sum write_packet] +*) + +(** [binary] is a generator of strings containing \x00 characters. *) +let binary =3D + (* increase frequency of '\x00' to 10%, otherwise it'd be ~1/256 *) + string_gen (Gen.frequency [(10, Gen.return '\x00'); (90, Gen.char)]) + |> set_print String.escaped + +(** [path_chars] valid path characters according to Xenstore protocol. *) +let path_chars =3D + List.init 256 Char.chr + |> List.filter Store.Path.char_is_valid + |> Array.of_list |> Gen.oneofa + +(** [path_element] a valid path element *) +let path_element =3D + string_gen_of_size Gen.small_int path_chars + +type tree =3D string Tree.t + +let paths =3D Tree.paths Store.Path.to_string path_element + +let with_validate p =3D + map_same_type + @@ fun v -> + (* reject it in a way known to QCheck: precondition failed, + instead of testcase failed *) + assume @@ p v ; + v + +(** [non_nul string_arb] rejects strings generated by [string_arb] that co= ntain '\x00'. *) +let non_nul =3D with_validate @@ fun s -> not (String.contains s '\x00') + +(** [plus arb] generates a list of 1 or more elements generated by [arb] *) +let plus arb =3D list_of_size Gen.(map succ small_int) arb + +(** [star arb] generates a list of 0 or more elements generated by [arb] *) +let star arb =3D list_of_size Gen.small_int arb + +let reserved =3D + string_of_size Gen.(frequency [(90, Gen.return 0); (10, Gen.small_int)]) + +(** According to xenstore protocol this could go up to 65535, but an actua= l domid + shouldn't go above this value *) +let domid_first_reserved =3D 0x7FF0 + +(** [new_domid] generates DomU domids *) +let new_domid =3D 1 -- domid_first_reserved + +let permty =3D + let open Perms in + oneofl [READ; WRITE; RDWR; NONE] + +let perms domid =3D + map + (fun (domid, other, acls) -> Perms.Node.create domid other acls) + ~rev:(fun n -> + (Perms.Node.get_owner n, Perms.Node.get_other n, Perms.Node.get_acl = n)) + @@ triple domid permty (small_list (pair domid permty)) diff --git a/tools/ocaml/xenstored/test/old/gen_paths.ml b/tools/ocaml/xens= tored/test/old/gen_paths.ml new file mode 100644 index 0000000000..b50c5b7cad --- /dev/null +++ b/tools/ocaml/xenstored/test/old/gen_paths.ml @@ -0,0 +1,66 @@ +open QCheck +open Store + +type tree =3D Leaf | Nodes of (string * tree) list + +let nodes children =3D Nodes children +let gen_tree =3D QCheck.Gen.(sized @@ fix + (fun self n -> + let children =3D frequency [1, pure 0; 2, int_bound n] >>=3D fun m -> + match m with + | 0 -> pure [] + | _ -> list_repeat m (pair string (self (n/m))) + in + frequency + [ 1, pure Leaf + ; 2, map nodes children + ] + )) + +let rec paths_of_tree (acc, path) =3D function +| Leaf -> acc +| Nodes l -> + List.fold_left (fun acc (k, children) -> + let path =3D k :: path in + paths_of_tree (Store.Path.to_string (List.rev path) :: acc, path) chil= dren + ) acc l + +let gen_paths_choices =3D + Gen.map (fun tree -> + tree |> paths_of_tree ([], []) |> Array.of_list + ) gen_tree + +(*let arb_name =3D Gen.small_string + +let arb_permty =3D let open Perms in oneofl [ READ; WRITE; RDWR; NONE ] + +let arb_domid =3D oneofl [ 0; 1; 0x7FEF] + +let arb_perms =3D + map (fun (domid, other, acls) -> Perms.Node.create domid other acls) + ~rev:(fun n -> Perms.Node.get_owner n, Perms.Node.get_other n, Perms.No= de.get_acl n) + @@ triple arb_domid arb_permty (small_list (pair arb_domid arb_permty))= *) + +let arb_name =3D Gen.small_string +let arb_value =3D Gen.small_string + +let node_of name value children =3D + List.fold_left (fun c acc -> Node.add_child acc c) + (Node.create name Perms.Node.default0 value ) children + +let g =3D QCheck.Gen.(sized @@ fix + (fun self n -> + frequency [1, pure 0; 2, int_bound n] >>=3D fun m -> + let children =3D match m with + | 0 -> pure [] + | _ -> list_repeat m (self (n/m)) + in + map3 node_of arb_name arb_value children + )) + +let paths_of_tree t =3D + let paths =3D ref [] in + Store.traversal t (fun path node -> + paths :=3D (Store.Path.of_path_and_name path (Node.get_name node) |> S= tore.Path.to_string) :: !paths + ); + !paths diff --git a/tools/ocaml/xenstored/test/old/xenstored_test.ml b/tools/ocaml= /xenstored/test/old/xenstored_test.ml new file mode 100644 index 0000000000..84cfc45d4f --- /dev/null +++ b/tools/ocaml/xenstored/test/old/xenstored_test.ml @@ -0,0 +1,527 @@ +open Stdext +open QCheck +open Arbitrary + +let () =3D + (* Logging.access_log_nb_files :=3D 1 ; + Logging.access_log_transaction_ops :=3D true ; + Logging.access_log_special_ops :=3D true ; + Logging.access_log_destination :=3D File "/tmp/log" ; + Logging.init_access_log ignore ; + Logging.set_xenstored_log_destination "/dev/stderr"; + Logging.init_xenstored_log (); *) + Domains.xenstored_port :=3D "xenstored-port" ; + let f =3D open_out !Domains.xenstored_port in + Printf.fprintf f "%d" 1 ; + close_out f ; + Domains.xenstored_kva :=3D "/dev/zero" + +module Command =3D struct + type value =3D string + + let value =3D binary + + type token =3D string + + type txid =3D int + + type domid =3D Xenctrl.domid + + type t =3D + | Read of Store.Path.t + | Write of Store.Path.t * value + | Mkdir of Store.Path.t + | Rm of Store.Path.t + | Directory of Store.Path.t + (* | Directory_part not implemented *) + | Get_perms of Store.Path.t + | Set_perms of Store.Path.t * Perms.Node.t + | Watch of Store.Path.t * token + | Unwatch of Store.Path.t * token + | Reset_watches + | Transaction_start + | Transaction_end of bool + | Introduce of domid * nativeint * int + | Release of int + | Get_domain_path of domid + | Is_domain_introduced of domid + | Set_target of domid * domid + | LiveUpdate + + type state =3D + { store: Store.t + ; doms: Domains.domains + ; cons: Connections.t + ; domids: int array } + + let path =3D list path_element + + let token =3D printable_string + + let domid state =3D oneofa ~print:Print.int state.domids + + let cmd state =3D + let domid =3D domid state in + let cmd_read =3D Case.make "READ" path (fun path -> Read path) in + let cmd_write =3D + Case.make "WRITE" (pair path value) (fun (path, value) -> + Write (path, value)) + in + let cmd_mkdir =3D Case.make "MKDIR" path (fun path -> Mkdir path) in + let cmd_rm =3D Case.make "RM" path (fun path -> Rm path) in + let cmd_directory =3D + Case.make "DIRECTORY" path (fun path -> Directory path) + in + let cmd_get_perms =3D + Case.make "GET_PERMS" path (fun path -> Get_perms path) + in + let cmd_set_perms =3D + Case.make "SET_PERMS" + (pair path (perms domid)) + (fun (path, perms) -> Set_perms (path, perms)) + in + let cmd_watch =3D + Case.make "WATCH" (pair path token) (fun (path, token) -> + Watch (path, token)) + in + let cmd_unwatch =3D + Case.make "UNWATCH" (pair path token) (fun (path, token) -> + Unwatch (path, token)) + in + let cmd_reset_watches =3D + Case.make "RESET_WATCHES" unit (fun () -> Reset_watches) + in + let cmd_tx_start =3D + Case.make "TRANSACTION_START" unit (fun () -> Transaction_start) + in + let cmd_tx_end =3D + Case.make "TRANSACTION_END" bool (fun commit -> Transaction_end comm= it) + in + let cmd_introduce =3D + Case.make "INTRODUCE" (triple domid int int) (fun (domid, gfn, port)= -> + Introduce (domid, Nativeint.of_int gfn, port)) + in + let cmd_release =3D Case.make "RELEASE" domid (fun domid -> Release do= mid) in + let cmd_get_domain_path =3D + Case.make "GET_DOMAIN_PATH" domid (fun domid -> Get_domain_path domi= d) + in + let cmd_is_domain_introduced =3D + Case.make "IS_DOMAIN_INTRODUCED" domid (fun domid -> + Is_domain_introduced domid) + in + let cmd_set_target =3D + Case.make "SET_TARGET" (pair domid domid) (fun (domid, tdomid) -> + Set_target (domid, tdomid)) + in + let cmd_live_update =3D + Case.make "CONTROL live-update" unit (fun () -> LiveUpdate) + in + let rev =3D function + | Read a -> + Case.call cmd_read a + | Write (p, v) -> + Case.call cmd_write (p, v) + | Mkdir a -> + Case.call cmd_mkdir a + | Rm a -> + Case.call cmd_rm a + | Directory a -> + Case.call cmd_directory a + | Get_perms a -> + Case.call cmd_get_perms a + | Set_perms (p, v) -> + Case.call cmd_set_perms (p, v) + | Watch (p, t) -> + Case.call cmd_watch (p, t) + | Unwatch (p, t) -> + Case.call cmd_unwatch (p, t) + | Reset_watches -> + Case.call cmd_reset_watches () + | Transaction_start -> + Case.call cmd_tx_start () + | Transaction_end a -> + Case.call cmd_tx_end a + | Introduce (d, g, p) -> + Case.call cmd_introduce (d, Nativeint.to_int g, p) + | Release a -> + Case.call cmd_release a + | Get_domain_path a -> + Case.call cmd_get_domain_path a + | Is_domain_introduced a -> + Case.call cmd_is_domain_introduced a + | Set_target (d, t) -> + Case.call cmd_set_target (d, t) + | LiveUpdate -> + Case.call cmd_live_update () + in + let open Case in + sum ~rev + [ to_sum cmd_read + ; to_sum cmd_write + ; to_sum cmd_mkdir + ; to_sum cmd_rm + ; to_sum cmd_directory + ; to_sum cmd_get_perms + ; to_sum cmd_set_perms + ; to_sum cmd_watch + ; to_sum cmd_unwatch + ; to_sum cmd_reset_watches + ; to_sum cmd_tx_start + ; to_sum cmd_tx_end + ; to_sum cmd_introduce + ; to_sum cmd_release + ; to_sum cmd_get_domain_path + ; to_sum cmd_is_domain_introduced + ; to_sum cmd_set_target + ; to_sum cmd_live_update ] + + let run tid =3D + let open Xenstore.Queueop in + function + | Read p -> + read tid Store.Path.(to_string p) + | Write (p, v) -> + write tid Store.Path.(to_string p) v + | Mkdir p -> + mkdir tid Store.Path.(to_string p) + | Rm p -> + rm tid Store.Path.(to_string p) + | Directory p -> + directory tid Store.Path.(to_string p) + | Get_perms p -> + getperms tid Store.Path.(to_string p) + | Set_perms (p, v) -> + setperms tid Store.Path.(to_string p) Perms.Node.(to_string v) + | Watch (p, t) -> + watch Store.Path.(to_string p) t + | Unwatch (p, t) -> + unwatch Store.Path.(to_string p) t + | Reset_watches -> + let open Xenbus in + fun con -> Xb.queue con (Xb.Packet.create 0 0 Xb.Op.Reset_watches = "") + | Transaction_start -> + transaction_start + | Transaction_end c -> + transaction_end tid c + | Release d -> + release d + | Get_domain_path d -> + getdomainpath d + | Is_domain_introduced d -> + let open Xenbus in + fun con -> + Xb.queue con + (Xb.Packet.create 0 0 Xb.Op.Isintroduced (string_of_int d)) + | Set_target (d, t) -> + let open Xenbus in + fun con -> + Xb.queue con + (Xb.Packet.create 0 0 Xb.Op.Isintroduced + (String.concat "\x00" [string_of_int d; string_of_int t])) + | LiveUpdate -> + debug ["live-update"; "-s"] + | Introduce (d, g, p) -> + introduce d g p +end + +module Spec =3D struct + type cmd =3D New | Cmd of Command.domid * int option * Command.t + + type state =3D + { xb: Xenbus.Xb.t + ; cnt: int + ; cmdstate: Command.state ref option + ; failure: (exn * string) option } + + type sut =3D state ref + + let doms =3D Domains.init (Event.init ()) ignore + + let dom0 =3D Domains.create0 doms + + let new_state () =3D + let cons =3D Connections.create () in + Connections.add_domain cons dom0 ; + let store =3D Store.create () in + let con =3D Perms.Connection.create 0 in + Store.mkdir store con ["tool"] ; + {Command.store; doms; cons; domids=3D [|0|]} + + let print =3D function + | New -> + "NEW" + | Cmd (d, t, c) -> + let s =3D new_state () in + let cmd =3D Command.cmd s in + (Option.get (triple (Command.domid s) (option int) cmd).print) (d,= t, c) + + let shrink =3D function + | New -> + Iter.empty + | Cmd (d, t, c) -> + let s =3D new_state () in + let cmd =3D Command.cmd s in + Iter.map (fun (d, t, c) -> Cmd (d, t, c)) + @@ (Option.get (triple (Command.domid s) (option int) cmd).shrink) + (d, t, c) + + let arb_cmd state =3D + ( match state.cmdstate with + | None -> + always New + | Some s -> + let cmd =3D Command.cmd !s in + QCheck.map + (fun (d, t, c) -> Cmd (d, t, c)) + ~rev:(fun (Cmd (d, t, c)) -> (d, t, c)) + @@ triple (Command.domid !s) (option int) cmd ) + |> set_print print |> set_shrink shrink + + (* |> set_collect (fun (_, _, c) -> (Option.get cmd.QCheck.collect) c= )*) + + let init_state =3D + {cnt=3D 0; xb=3D Xenbus.Xb.open_fd Unix.stdout; cmdstate=3D None; fail= ure=3D None} + + let precond cmd s =3D + match (cmd, s.cmdstate) with + | New, None -> + true + | New, _ -> + false + | Cmd _, None -> + false + | Cmd (_, _, Command.Release 0), _ -> + false + | _ -> + true + + let next_state cmd state =3D + { ( try + assume (precond cmd state) ; + match cmd with + | New -> + {state with cmdstate=3D Some (ref @@ new_state ())} + | Cmd (domid, tid, cmd) -> + let tid =3D match tid with None -> 0 | Some id -> 1 + id in + Command.run tid cmd state.xb ; + let s =3D !(Option.get state.cmdstate) in + let con =3D Connections.find_domain s.Command.cons domid in + Queue.clear con.xb.pkt_out ; + let run_packet packet =3D + let tid, rid, ty, data =3D Xenbus.Xb.Packet.unpack packet = in + let req =3D {Packet.tid; Packet.rid; Packet.ty; Packet.dat= a} in + Process.process_packet ~store:s.Command.store + ~cons:s.Command.cons ~doms:s.Command.doms ~con ~req ; + Process.write_access_log ~ty ~tid + ~con:(Connection.get_domstr con) + ~data ; + let packet =3D Connection.peek_output con in + let tid, _rid, ty, data =3D Xenbus.Xb.Packet.unpack packet= in + Process.write_answer_log ~ty ~tid + ~con:(Connection.get_domstr con) + ~data + in + Queue.iter run_packet state.xb.pkt_out ; + Queue.clear state.xb.pkt_out ; + state + with e -> + let bt =3D Printexc.get_backtrace () in + {state with failure=3D Some (e, bt)} ) + with + cnt=3D state.cnt + 1 } + + let init_sut () =3D ref init_state + + let cleanup _ =3D () + + module P =3D struct + type t =3D string list + + let compare =3D compare + end + + module PathMap =3D Map.Make (P) + + module DomidMap =3D Map.Make (struct + type t =3D Xenctrl.domid + + let compare =3D compare + end) + + module IntMap =3D Map.Make (struct + type t =3D int + + let compare =3D compare + end) + + module FDMap =3D Map.Make (struct + type t =3D Unix.file_descr + + let compare =3D compare + end) + + let map_of_store s =3D + let m =3D ref PathMap.empty in + Store.dump_fct s (fun path node -> m :=3D PathMap.add path node !m) ; + !m + + let node_equiv n n' =3D + Perms.equiv (Store.Node.get_perms n) (Store.Node.get_perms n') + && Store.Node.get_name n =3D Store.Node.get_name n' + && Store.Node.get_value n =3D Store.Node.get_value n' + + let store_root_equiv s s' =3D + if not (PathMap.equal node_equiv (map_of_store s) (map_of_store s')) t= hen + let b =3D Store.dump_store_buf s.root in + let b' =3D Store.dump_store_buf s'.root in + Test.fail_reportf "Store trees are not equivalent:\n %s\n <>\n %s" + (Buffer.contents b) (Buffer.contents b') + else true + + let map_of_domid_table tbl =3D Hashtbl.fold DomidMap.add tbl DomidMap.em= pty + + let map_of_quota q =3D map_of_domid_table q.Quota.cur + + let store_quota_equiv root root' q q' =3D + let _ =3D + DomidMap.merge + (fun domid q q' -> + let q =3D Option.value ~default:(-1) q in + let q' =3D Option.value ~default:(-1) q' in + if q <> q' then + let b =3D Store.dump_store_buf root in + let b' =3D Store.dump_store_buf root' in + Test.fail_reportf "quota mismatch on %d: %d <> %d\n%s\n%s\n" d= omid q + q' (Buffer.contents b) (Buffer.contents b') + else Some q) + (map_of_quota q) (map_of_quota q') + in + true + + let store_equiv s s' =3D + store_root_equiv s s' + && store_quota_equiv s.root s'.root (Store.get_quota s) (Store.get_quo= ta s') + + let map_of_domains d =3D map_of_domid_table d.Domains.table + + let domain_equiv d d' =3D + Domain.get_id d =3D Domain.get_id d' + && Domain.get_remote_port d =3D Domain.get_remote_port d' + + let domains_equiv d d' =3D + DomidMap.equal domain_equiv (map_of_domains d) (map_of_domains d') + + let map_of_fd_table tbl =3D Hashtbl.fold FDMap.add tbl FDMap.empty + + let map_of_int_table tbl =3D Hashtbl.fold IntMap.add tbl IntMap.empty + + let list_of_queue q =3D Queue.fold (fun acc e -> e :: acc) [] q + + let connection_equiv c c' =3D + let l =3D list_of_queue c.Connection.xb.pkt_out in + let l' =3D list_of_queue c'.Connection.xb.pkt_out in + if List.length l <> List.length l' || List.exists2 ( <> ) l l' then ( + let print_packets l =3D + l + |> List.rev_map (fun p -> + let tid, rid, ty, data =3D Xenbus.Packet.unpack p in + let tystr =3D Xenbus.Xb.Op.to_string ty in + Printf.sprintf "tid=3D%d, rid=3D%d, ty=3D%s, data=3D%s" tid= rid tystr + (String.escaped data)) + |> String.concat "\n" + in + let r =3D print_packets l in + let r' =3D print_packets l' in + Test.fail_reportf "Replies not equal:\n%s\n <>\n %s" r r' ) + else + let n =3D Connection.number_of_transactions c in + let n' =3D Connection.number_of_transactions c' in + if n <> n' then Test.fail_reportf "TX count mismatch: %d <> %d" n n' + else true + + let connections_equiv c c' =3D + FDMap.equal connection_equiv + (map_of_fd_table c.Connections.anonymous) + (map_of_fd_table c'.Connections.anonymous) + && IntMap.equal connection_equiv + (map_of_int_table c.Connections.domains) + (map_of_int_table c'.Connections.domains) + + let dump_load s =3D + let tmp =3D Filename.temp_file "xenstored" "qcheck.dump" in + finally + (fun () -> + let fds =3D {Xenstored.DB.rw_sock=3D None; ro_sock=3D None} in + Xenstored.DB.to_file fds !s.Command.store !s.Command.cons tmp ; + s :=3D new_state () ; + let _fds', errors =3D + Xenstored.DB.from_file ~live:true !s.Command.store !s.Command.do= ms + !s.Command.cons tmp + in + if errors > 0 then + Test.fail_reportf "Errors during live update: %d" errors) + (fun () -> Sys.remove tmp) + + let run_cmd cmd state sut =3D + ( match state.failure with + | None -> + true + | Some (e, bt) -> + Test.fail_reportf "Exception %s, backtrace: %s" (Printexc.to_strin= g e) + bt ) + && + match cmd with + | New -> + sut :=3D next_state cmd !sut ; + true + | Cmd (0, _, Command.LiveUpdate) -> + let s =3D !sut.cmdstate in + let store1 =3D Store.copy !(Option.get s).store in + let doms1 =3D !(Option.get s).doms in + dump_load (Option.get s) ; + (* reply is expected not to be equivalent, since after live update= we got an empty reply queue, + so don't compare connections + *) + store_equiv store1 !(Option.get s).store + && domains_equiv doms1 !(Option.get s).doms + | Cmd(_, _, cmd') -> ( + (* TODO: also got same reply, and check for equivalence on the act= ual Live Update *) + sut :=3D next_state cmd !sut ; + let ids =3D Hashtbl.create 47 in + Connections.iter !(Option.get state.cmdstate).cons (fun con -> + Hashtbl.add ids (Connection.get_id con) con.next_tid) ; + let state =3D next_state cmd state in + match (!sut.failure, state.cmdstate, !sut.cmdstate) with + | None, Some s, Some s' -> + let r =3D cmd' =3D Command.Transaction_start (* txid can chang= e *) ||=20 + connections_equiv !s.cons !s'.cons in + Connections.iter !(Option.get state.cmdstate).cons (fun con -> + let tid =3D Hashtbl.find ids (Connection.get_id con) in + if con.next_tid <> tid then ( + let (_ : bool) =3D Connection.end_transaction con tid No= ne in + () ; + con.next_tid <- tid )) ; + r + | None, None, None -> + true + | None, None, _ -> + Test.fail_report "state uninit" + | None, _, None -> + Test.fail_report "sut uninit" + | Some (e, bt), _, _ -> + Test.fail_reportf "Exception %s, backtrace: %s" + (Printexc.to_string e) bt ) +end + +module States =3D QCSTM.Make (Spec) + +(* && watches_equiv c c' *) + +let test =3D States.agree_test ~count:100 ~name:"live-update" + +let test =3D + Test.make ~name:"live-update" ~count:100 + (States.arb_cmds Spec.init_state) + States.agree_prop + +let () =3D QCheck_base_runner.run_tests_main [test] diff --git a/tools/ocaml/xenstored/test/pathtree.ml b/tools/ocaml/xenstored= /test/pathtree.ml new file mode 100644 index 0000000000..50cbb0302d --- /dev/null +++ b/tools/ocaml/xenstored/test/pathtree.ml @@ -0,0 +1,40 @@ +module M =3D Map.Make(String) +type 'a t =3D { data: 'a; children: 'a t M.t } + +type 'a tree =3D 'a t +let of_data data =3D { data; children =3D M.empty } + +let update key f t =3D { t with children =3D M.update key f t.children } +let set t data =3D { t with data } + +module Cursor =3D struct + type 'a t =3D { tree: 'a tree; up: ('a t * M.key) option } + + let of_tree tree =3D { tree; up =3D None } + + let create parent key tree =3D { tree; up =3D Some (parent, key) } + + let down cur k =3D + M.find_opt k cur.tree.children |> Option.map @@ create cur k + + let down_implicit_create ~implicit cur k =3D + match down cur k with + | Some r -> r + | None -> cur.tree.data |> implicit |> of_data |> create cur k + + let rec to_tree t =3D match t.up with + | None -> t.tree + | Some (parent, key) -> + to_tree { parent with tree =3D update key (fun _ -> Some t.tree) p= arent.tree } + + let set cur data =3D { cur with tree =3D set cur.tree data } + let get cur =3D cur.tree.data + + let rm_child cur key =3D { cur with tree =3D update key (fun _ -> None) = cur.tree} + + (* TODO: down with implicit create *) +end + + + +let rec map f t =3D { data =3D f t.data; children =3D M.map (map f) t.chil= dren } diff --git a/tools/ocaml/xenstored/test/testable.ml b/tools/ocaml/xenstored= /test/testable.ml new file mode 100644 index 0000000000..2fa749fbb3 --- /dev/null +++ b/tools/ocaml/xenstored/test/testable.ml @@ -0,0 +1,379 @@ +let is_output_devnull =3D Unix.stat "/dev/null" =3D Unix.fstat Unix.stdout + +let () =3D + if not is_output_devnull then ( + Printexc.record_backtrace true ; + Fmt_tty.setup_std_outputs () ; + try + let cols =3D + let ch =3D Unix.open_process_in "tput cols" in + Stdext.finally + (fun () -> input_line ch |> int_of_string) + (fun () -> Unix.close_process_in ch) + in + Format.set_margin cols + with _ -> () ) + +let devnull () =3D Unix.openfile "/dev/null" [] 0 + +let xb =3D Xenbus.Xb.open_fd (devnull ()) + +module Command =3D struct + type path =3D Store.Path.t + + type value =3D string + + type token =3D string + + type domid =3D int + + type t =3D Xenbus.Packet.t + + open Xenstore.Queueop + + let cmd f =3D + Queue.clear xb.pkt_out ; + let () =3D f xb in + let p =3D Xenbus.Xb.peek_output xb in + Queue.clear xb.pkt_out ; p + + let pathcmd f pathgen tid state =3D cmd @@ f tid @@ pathgen state + + let cmd_read gen tid state =3D pathcmd read gen tid state + + let cmd_write pathgen v tid state =3D cmd @@ write tid (pathgen state) v + + let cmd_mkdir g t s =3D pathcmd mkdir g t s + + let cmd_rm g t s =3D pathcmd rm g t s + + let cmd_directory g t s =3D pathcmd directory g t s + + let cmd_getperms g t s =3D pathcmd getperms g t s + + let cmd_setperms pathgen vgen tid state =3D + cmd @@ setperms tid (pathgen state) (Perms.Node.to_string @@ vgen stat= e) + + let cmd_watch pathgen token _ state =3D cmd @@ watch (pathgen state) tok= en + + let cmd_unwatch pathgen token _ state =3D cmd @@ unwatch (pathgen state)= token + + let cmd_reset_watches tid _state =3D + let open Xenbus in + cmd + @@ fun con -> + Xenbus.Xb.queue con + (Xenbus.Xb.Packet.create 0 0 Xenbus.Xb.Op.Reset_watches "") + + let cmd_transaction_start _ _ =3D cmd @@ transaction_start + + let cmd_transaction_end commit tid _ =3D cmd @@ transaction_end tid comm= it + + let domcmd f idgen _ state =3D cmd @@ f @@ idgen state + + let cmd_release idgen state =3D domcmd release idgen state + + let cmd_getdomainpath i s =3D domcmd getdomainpath i s + + let cmd_isintroduced i t s =3D + domcmd + (fun d con -> + let open Xenbus in + Xenbus.Xb.queue con + (Xenbus.Xb.Packet.create 0 0 Xenbus.Xb.Op.Isintroduced + (string_of_int d))) + i t s + + let cmd_set_target idgen1 idgen2 _ state =3D + let d =3D idgen1 state in + let t =3D idgen2 state in + cmd + @@ fun con -> + Xenbus.Xb.queue con + (Xenbus.Xb.Packet.create 0 0 Xenbus.Xb.Op.Isintroduced + (String.concat "\x00" [string_of_int d; string_of_int t])) + + let cmd_liveupdate _ _ =3D cmd @@ debug ["live-update"; "-s"] + + let cmd_introduce id port _ state =3D cmd @@ introduce id 0n port + + let pp_dump =3D Types.pp_dump_packet + + let precond cmd _state =3D + match cmd with + | {Xenbus.Packet.ty=3D Xenbus.Xb.Op.Release; data=3D "0\000"} -> + false + (* can't release Dom0 in the tests, or we crash due to shared dom0= backend *) + | {ty=3D Xenbus.Xb.Op.Rm; data=3D ""} -> + (* this is expected to cause inconsistencies on pre-created paths = like /local *) + false + | _ -> + true +end + +let with_logger ~on_exn f =3D + if is_output_devnull then f () + else + let old =3D (!Logging.xenstored_logger, !Logging.access_logger) in + let logs =3D ref [] in + let write ?(level =3D Logging.Debug) s =3D + let msg =3D Printf.sprintf "%s %s" (Logging.string_of_level level) s= in + logs :=3D msg :: !logs + in + let logger =3D + Some {Logging.stop=3D ignore; restart=3D ignore; rotate=3D ignore; w= rite} + in + Logging.xenstored_logger :=3D logger ; + Logging.access_logger :=3D logger ; + Stdext.finally + (fun () -> + try f () + with e -> + let bt =3D Printexc.get_raw_backtrace () in + on_exn e bt (List.rev !logs)) + (fun () -> + Logging.xenstored_logger :=3D fst old ; + Logging.access_logger :=3D snd old) + +type t =3D + { store: Store.t + ; cons: Connections.t + ; doms: Domains.domains + ; mutable anon: Unix.file_descr option + ; live_update: bool + ; txidtbl: (int, int) Hashtbl.t } + +let () =3D + Logging.xenstored_log_level :=3D Logging.Debug ; + Logging.access_log_special_ops :=3D true ; + Logging.access_log_transaction_ops :=3D true ; + let name, f =3D Filename.open_temp_file "xenstored" "port" in + Domains.xenstored_port :=3D name ; + Stdext.finally (fun () -> Printf.fprintf f "%d" 1) (fun () -> close_out = f) ; + Domains.xenstored_kva :=3D "/dev/zero" ; + (* entries from a typical oxenstored.conf *) + Transaction.do_coalesce :=3D true ; + Perms.activate :=3D true ; + Quota.activate :=3D true ; + Quota.maxent :=3D 8192 ; + Quota.maxsize :=3D 2048 ; + Define.maxwatch :=3D 512 ; + Define.maxtransaction :=3D 10 ; + Define.maxrequests :=3D 1024 + +(* we MUST NOT release dom0, or we crash, + this is shared between multiple tests, because + it keeps an FD open, and we want to avoid EMFILE +*) + +let create ?(live_update =3D false) () =3D + let store =3D Store.create () in + let cons =3D Connections.create () in + let doms =3D Domains.init (Event.init ()) ignore in + let dom0 =3D Domains.create0 doms in + let txidtbl =3D Hashtbl.create 47 in + Connections.add_domain cons dom0 ; + {store; cons; doms; anon=3D None; live_update; txidtbl} + +let cleanup t =3D Connections.iter t.cons Connection.close + +let init t =3D + let local =3D Store.Path.of_string "/local" in + let con =3D Perms.Connection.create 0 in + Store.mkdir t.store con local ; + (* Store.mkdir t.store con (Store.Path.of_string "/tool") ;*) + let fd =3D devnull () in + t.anon <- Some fd ; + Connections.add_anonymous t.cons fd + +let dump_load s =3D + let tmp =3D Filename.temp_file "xenstored" "qcheck.dump" in + Stdext.finally + (fun () -> + Xenstored.DB.to_file None s.store s.cons tmp ; + let s' =3D create () in + (* preserve FD *) + s'.anon <- s.anon ; + s.anon <- None ; + let _fds', errors =3D + Xenstored.DB.from_file ~live:true s'.store s'.doms s'.cons tmp + in + if errors > 0 then + failwith (Printf.sprintf "Errors during live update: %d" errors) ; + s') + (fun () -> Sys.remove tmp) + +let is_live_update =3D function + | {Xenbus.Packet.ty=3D Xenbus.Xb.Op.Debug; data=3D "live-update\000-s\00= 0"} -> + true + | _ -> + false + +let is_tx_start p =3D p.Xenbus.Packet.ty =3D Xenbus.Xb.Op.Transaction_start + +let with_tmpfile prefix write f =3D + let name, ch =3D Filename.open_temp_file prefix ".txt" in + Stdext.finally + (fun () -> + Stdext.finally (fun () -> write ch) (fun () -> close_out ch) ; + f name) + (fun () -> Sys.remove name) + +let with_pp_to_file prefix pp x f =3D + let write ch =3D + let ppf =3D Format.formatter_of_out_channel ch in + Format.pp_set_margin ppf @@ Format.get_margin () ; + pp ppf x ; + Fmt.flush ppf () + in + with_tmpfile prefix write f + +let run_cmd_get_output ?(ok_codes =3D [0]) cmd =3D + let cmd =3D Array.of_list cmd in + let ch =3D Unix.open_process_args_in cmd.(0) cmd in + Stdext.finally + (fun () -> + let lines =3D ref [] in + try + while true do + lines :=3D input_line ch :: !lines + done ; + assert false + with End_of_file -> List.rev !lines |> String.concat "\n") + (fun () -> + match Unix.close_process_in ch with + | Unix.WEXITED code when List.mem code ok_codes -> + () + | status -> + Crowbar.failf "%a %a" (Fmt.array Fmt.string) cmd + Types.pp_process_status status) + +let call_diff x y =3D + let ok_codes =3D [0; 1] in + run_cmd_get_output ~ok_codes + [ "/usr/bin/git" + ; "diff" + ; "-U10000" (* we want to see the entire state, where possible *) + ; "--no-index" + ; ( "--word-diff=3D" + ^ if Fmt.style_renderer Fmt.stdout =3D `Ansi_tty then "color" else "= plain" + ) + ; "--color-moved=3Ddimmed-zebra" + ; x + ; y ] + +let check_eq_exn prefix ~pp ~eq x y =3D + if not @@ eq x y then + if is_output_devnull then failwith "different" + else + with_pp_to_file "expected" pp x + @@ fun xfile -> + with_pp_to_file "actual" pp y + @@ fun yfile -> + failwith + @@ Printf.sprintf "%s agrement: %s" prefix (call_diff xfile yfile) + +let run next_tid t (domid, cmd) =3D + let con =3D + match domid with + | 0 -> + Connections.find !t.cons (Option.get !t.anon) + | id -> + Connections.find_domain !t.cons domid + in + (* clear out any watch events, TODO: don't *) + Connections.iter !t.cons (fun con -> Queue.clear con.xb.pkt_out) ; + (* TODO: use the global live update state that processing the command se= ts, but remember to reset it *) + if is_live_update cmd then + if !t.live_update then ( + let t0 =3D !t in + let t' =3D dump_load t0 in + Connections.iter t'.cons (fun con -> + Connection.iter_transactions con + @@ fun _ tx -> + (* if tx.Transaction.operations <> [] then TODO: only if we d= ump snapshot state + correctly *) + Transaction.mark_failed tx) ; + Logging.info "store" "store: %s" (Fmt.to_to_string Types.pp_dump_sto= re t'.store); + Logging.info "store" "store: %s" (Fmt.to_to_string Types.pp_dump_sto= re t0.store); + check_eq_exn "store" ~pp:Types.pp_dump_store ~eq:Types.equal_store + t0.store t'.store ; + (* TODO: now we have a disagreement here... so we can't test this un= til TX state is restored *) + (*check_eq_exn "connections" ~pp:Types.pp_dump_connections + ~eq:Types.equal_connections t0.cons t'.cons ;*) + check_eq_exn "domains" ~pp:Types.pp_dump_domains ~eq:Types.equal_dom= ains + t0.doms t'.doms ; + (* avoid double close on anonymous conn *) + Connections.iter_domains t0.cons Connection.close ; + t :=3D {t' with txidtbl=3D !t.txidtbl} ) + else begin + Logging.debug "testable" "BEFORE TXMARK"; + Connections.iter !t.cons (fun con -> + Connection.iter_transactions con + @@ fun txid tx -> + Logging.debug "testable" "marking to fail %d" txid;=20 + (* if tx.Transaction.operations <> [] then see above TODO *) + Transaction.mark_failed tx)=20 + end; + let run_packet packet =3D + let tid, rid, ty, data =3D Xenbus.Xb.Packet.unpack packet in + Logging.debug "testable" "tid: %d" tid ; + let tid =3D if tid <> 0 then Hashtbl.find !t.txidtbl tid else tid in + let req : Packet.request =3D + {Packet.tid; Packet.rid; Packet.ty; Packet.data} + in + Process.process_packet ~store:!t.store ~cons:!t.cons ~doms:!t.doms ~co= n ~req ; + Process.write_access_log ~ty ~tid ~con:(Connection.get_domstr con) ~da= ta ; + let packet =3D Connection.peek_output con in + if ty =3D Xenbus.Xb.Op.Transaction_start then ( + Logging.debug "testable" "Adding mapping for tid %d" next_tid ; + Hashtbl.add !t.txidtbl next_tid (con.Connection.next_tid - 1) ) ; + let tid, _rid, ty, data =3D Xenbus.Xb.Packet.unpack packet in + Process.write_answer_log ~ty ~tid ~con:(Connection.get_domstr con) ~da= ta + in + (* TODO: also a Nodes command with multiple packets *) + run_packet cmd ; (* TODO: act on and clear watches? *) + con + +let is_tx_marked_fail con p =3D + let tid =3D p.Xenbus.Packet.tid in + if tid =3D 0 then false + else begin + let r =3D try (Connection.get_transaction con tid).must_fail + with Not_found -> false in + Logging.info "testable" "TXI %d: %b" tid r; + r + end + +let run2 next_tid t t' (domid, cmd) =3D + let con =3D run next_tid t (domid, cmd) in + let con' =3D run next_tid t' (domid, cmd) in + (* TODO: ignore txid mismatches on transactions *) + if not @@ (is_tx_start cmd || is_tx_marked_fail con cmd) then + (* TODO: ignore disagreements when transactions are marked as failed *) + check_eq_exn "reply packets" ~pp:Types.pp_dump_xb ~eq:Types.equal_xb_p= kts + con.xb con'.xb ; + Queue.clear con'.xb.pkt_out ; + Queue.clear con.xb.pkt_out + +module type S =3D sig + type cmd + + type state + + type sut + + val init_state : state + + val next_state : cmd -> state -> state + + val init_sut : unit -> sut + + val cleanup : sut -> unit + + val run_cmd : cmd -> state -> sut -> bool + + val precond : cmd -> state -> bool + + val pp : cmd Fmt.t +end diff --git a/tools/ocaml/xenstored/test/types.ml b/tools/ocaml/xenstored/te= st/types.ml new file mode 100644 index 0000000000..f46d20b245 --- /dev/null +++ b/tools/ocaml/xenstored/test/types.ml @@ -0,0 +1,437 @@ +(* + * Copyright (C) Citrix Systems Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation; version 2.1 only. with the special + * exception on linking described in file LICENSE. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + *) + +let domid_first_reserved =3D 0x7FF0 + +type 'a eq =3D 'a -> 'a -> bool + +let hashtable_equal (eq : 'a eq) h h' =3D + Hashtbl.length h =3D Hashtbl.length h' + && Hashtbl.fold + (fun k v acc -> + acc + && match Hashtbl.find_opt h' k with Some x -> eq v x | None -> fa= lse) + h true + +let list_equal (eq : 'a eq) l l' =3D + try List.for_all2 eq l l' with Invalid_argument _ -> false + +let queue_equal eq q q' =3D + Queue.length q =3D Queue.length q' + && + let list_of_queue q =3D Queue.fold (fun acc e -> e :: acc) [] q in + list_equal eq (list_of_queue q) (list_of_queue q') + +let pp_process_status ppf =3D function + | Unix.WEXITED code -> + Fmt.pf ppf "exited with code %d" code + | Unix.WSIGNALED osig -> + Fmt.pf ppf "killed by signal %a" Fmt.Dump.signal osig + | Unix.WSTOPPED osig -> + Fmt.pf ppf "stopped by signal %a" Fmt.Dump.signal osig + +let pp_dump_ref dump =3D + Fmt.using ( ! ) Fmt.(dump |> Fmt.braces |> prefix (const string "ref")) + +let pp_file_descr =3D Fmt.using Disk.FD.to_int Fmt.int + +module Quota =3D struct + open Quota + + let pp_dump =3D + let open Fmt in + Dump.record + [ Dump.field "maxent" (fun q -> q.maxent) int + ; Dump.field "maxsize" (fun q -> q.maxsize) int + ; Dump.field "cur" (fun q -> q.cur) @@ Dump.hashtbl int int ] + + let drop_dom0 h =3D + (* Quota is ignored for Dom0 and will be wrong in some situations: + - when domains die any nodes owned by them get inherited by Dom0 + - the root node is owned by Dom0, if its ownership is changed Dom0'= s quota will be off-by-one + Since Dom0's quota is not actually used, just drop it when comparing + *) + let h' =3D Hashtbl.copy h in + Hashtbl.remove h' 0; + h' + + let equal q q' =3D + q.maxent =3D q'.maxent && q.maxsize =3D q'.maxsize + && hashtable_equal Int.equal (drop_dom0 q.cur) (drop_dom0 q'.cur) +end +let pp_dump_quota =3D Quota.pp_dump +let equal_quota =3D Quota.equal + +module Store =3D struct + open Store + + module Node =3D struct + open Node + + let pp_dump ppf t =3D + let buf =3D dump_store_buf t in + Fmt.lines ppf (Buffer.contents buf) + + let rec equal n n' =3D + Symbol.equal n.name n'.name + && Perms.equiv n.perms n'.perms + && String.equal n.value n'.value + && SymbolMap.equal equal n.children n'.children + end + + module Path =3D struct + open Path + + let pp_dump =3D Fmt.using to_string Fmt.string + + let equal p p' =3D list_equal String.equal p p' + + let hash (p : t) =3D Hashtbl.hash p + + let compare (p : t) (p' : t) =3D compare p p' + end + + let pp_dump =3D + let open Fmt in + (* only print relevant fields, expected to stay same during live-updat= e. *) + Dump.record + [ Dump.field "stat_transaction_coalesce" + (fun t -> t.stat_transaction_coalesce) + int + ; Dump.field "stat_transaction_abort" + (fun t -> t.stat_transaction_coalesce) + int + ; Dump.field "store" (fun t -> t.root) Node.pp_dump + ; Dump.field "quota" (fun t -> t.quota) Quota.pp_dump ] + + let equal s s' =3D + (* ignore stats *) + Node.equal s.root s'.root && Quota.equal s.quota s'.quota +end + +let pp_dump_store =3D Store.pp_dump +let equal_store =3D Store.equal + +module Xb =3D struct + open Xenbus.Xb + + module Op =3D struct + open Xenbus.Op + + let pp_dump =3D Fmt.of_to_string to_string + + let equal (op : t) (op' : t) =3D op =3D op' + end + + module Packet =3D struct + open Xenbus.Packet + + let pp_dump =3D + let open Fmt in + Dump.record + [ Dump.field "tid" get_tid int + ; Dump.field "rid" get_rid int + ; Dump.field "ty" get_ty Op.pp_dump + ; Dump.field "data" get_data Dump.string ] + + let equal (p : t) (p' : t) =3D + (* ignore TXID, it can be different after a live-update *) + p.rid =3D p'.rid && p.ty =3D p'.ty && p.data =3D p'.data + end + + module Partial =3D struct + open Xenbus.Partial + + let pp_dump =3D + let open Fmt in + Dump.record + [ Dump.field "tid" (fun p -> p.tid) int + ; Dump.field "rid" (fun p -> p.rid) int + ; Dump.field "ty" (fun p -> p.ty) Op.pp_dump + ; Dump.field "len" (fun p -> p.len) int + ; Dump.field "buf" (fun p -> p.buf) Fmt.buffer ] + + let equal p p' =3D + p.tid =3D p'.tid && p.rid =3D p'.rid && p.ty =3D p'.ty + && Buffer.contents p.buf =3D Buffer.contents p'.buf + end + + let pp_dump_partial_buf ppf =3D function + | HaveHdr pkt -> + Fmt.pf ppf "HaveHdr %a" Partial.pp_dump pkt + | NoHdr (i, b) -> + Fmt.pf ppf "NoHdr(%d, %S)" i (Bytes.to_string b) + + let equal_partial_buf buf buf' =3D + match (buf, buf') with + | HaveHdr pkt, HaveHdr pkt' -> + Partial.equal pkt pkt' + | NoHdr (i, b), NoHdr (i', b') -> + i =3D i' && b =3D b' + | HaveHdr _, NoHdr _ | NoHdr _, HaveHdr _ -> + false + + let pp_backend ppf =3D function + | Fd {fd} -> + Fmt.prefix (Fmt.const Fmt.string "Fd ") pp_file_descr ppf fd + | Xenmmap _ -> + Fmt.const Fmt.string "Xenmmap _" ppf () + + let equal_backend b b' =3D + match (b, b') with + | Fd fd, Fd fd' -> + fd =3D fd' + | Xenmmap _, Xenmmap _ -> + true (* can't extract the FD to compare *) + | Fd _, Xenmmap _ | Xenmmap _, Fd _ -> + false + + let pp_dump =3D + let open Fmt in + Dump.record + [ Dump.field "backend" (fun x -> x.backend) pp_backend + ; Dump.field "pkt_in" (fun x -> x.pkt_in) @@ Dump.queue Packet.pp_du= mp + ; Dump.field "pkt_out" (fun x -> x.pkt_out) @@ Dump.queue Packet.pp_= dump + ; Dump.field "partial_in" (fun x -> x.partial_in) pp_dump_partial_buf + ; Dump.field "partial_out" (fun x -> x.partial_out) Dump.string ] + + let equal_pkts xb xb' =3D + let queue_eq =3D queue_equal Packet.equal in + queue_eq xb.pkt_in xb'.pkt_in + && queue_eq xb.pkt_out xb'.pkt_out + && xb.partial_in =3D xb'.partial_in + && xb.partial_out =3D xb'.partial_out + + let equal xb xb' =3D equal_backend xb.backend xb'.backend && equal_pkts = xb xb' +end + +let pp_dump_packet =3D Xb.Packet.pp_dump +let pp_dump_xb =3D Xb.pp_dump +let equal_xb =3D Xb.equal +let equal_xb_pkts =3D Xb.equal_pkts + +module Packet =3D struct + open Packet + + let pp_dump_request =3D + let open Fmt in + Dump.record + [ Dump.field "tid" (fun t -> t.tid) int + ; Dump.field "rid" (fun t -> t.rid) int + ; Dump.field "ty" (fun t -> t.ty) Xb.Op.pp_dump + ; Dump.field "data" (fun t -> t.data) Dump.string ] + + let equal_req r r' =3D + r.tid =3D r'.tid && r.rid =3D r'.rid && r.ty =3D r'.ty && r.data =3D r= '.data + + let pp_dump_response ppf =3D function + | Reply str -> + Fmt.pf ppf "Reply %S" str + | Error str -> + Fmt.pf ppf "Error %S" str + | Ack _ -> + Fmt.string ppf "Ack" + + let equal_response =3D response_equal +end + +module Transaction =3D struct + open Transaction + + let pp_dump_ty ppf =3D function + | Transaction.No -> + Fmt.string ppf "No" + | Full (id, orig, canonical) -> + Fmt.pf ppf "Full @[(%d, %a, %a)@]" id Store.pp_dump orig Store.pp_= dump + canonical + + let equal_ty t t' =3D + match (t, t') with + | Transaction.No, Transaction.No -> + true + | Transaction.Full _, Transaction.Full _ -> + (* We expect the trees not to be identical, so we ignore any diffe= rences here. + The reply comparison tests will find any mismatches in observab= le transaction state + *) + true + | Transaction.No, Transaction.Full _ | Transaction.Full _, Transaction= .No -> + false + + let equal_pathop (op, path) (op', path') =3D + op =3D op' && Store.Path.equal path path' + + let pp_dump_op =3D Fmt.pair Packet.pp_dump_request Packet.pp_dump_respon= se + + let equal_op (req, reply) (req', reply') =3D + Packet.equal_req req req' && Packet.equal_response reply reply' + + let pp_dump =3D + let open Fmt in + let open Transaction in + Dump.record + [ Dump.field "ty" (fun t -> t.ty) pp_dump_ty + ; Dump.field "start_count" (fun t -> t.start_count) int64 + ; Dump.field "store" (fun t -> t.store) Store.pp_dump + ; Dump.field "quota" (fun t -> t.quota) Quota.pp_dump + ; Dump.field "must_fail" (fun t -> t.must_fail) Fmt.bool + ; Dump.field "paths" (fun t -> t.paths) + @@ Dump.list (pair Xb.Op.pp_dump Store.Path.pp_dump) + ; Dump.field "operations" (fun t -> t.operations) + @@ list (pair Packet.pp_dump_request Packet.pp_dump_response) + ; Dump.field "read_lowpath" (fun t -> t.read_lowpath) + @@ option Store.Path.pp_dump + ; Dump.field "write_lowpath" (fun t -> t.write_lowpath) + @@ option Store.Path.pp_dump ] + + let equal t t' =3D + equal_ty t.ty t'.ty + (* ignored: quota at start of transaction, not relevant + && Quota.equal t.quota t'.quota *) + (*&& list_equal equal_pathop t.paths t'.paths *) + (*&& list_equal equal_op t.operations t'.operations*) + && t.must_fail =3D t'.must_fail + (* ignore lowpath, impossible to recreate from limited migration info = *) + (*&& Option.equal Store.Path.equal t.read_lowpath t'.read_lowpath + && Option.equal Store.Path.equal t.write_lowpath t'.write_lowpath *) +end + +module Connection =3D struct + open Connection + + let pp_dump_watch =3D + let open Fmt in + Dump.record + [ Dump.field "token" (fun w -> w.token) Dump.string + ; Dump.field "path" (fun w -> w.path) Dump.string + ; Dump.field "base" (fun w -> w.base) Dump.string + ; Dump.field "is_relative" (fun w -> w.is_relative) Fmt.bool ] + + let pp_dump =3D + let open Fmt in + Dump.record + [ Dump.field "xb" (fun c -> c.xb) Xb.pp_dump + ; Dump.field "transactions" (fun c -> c.transactions) + @@ Dump.hashtbl int Transaction.pp_dump + ; Dump.field "next_tid" (fun t -> t.next_tid) int + ; Dump.field "nb_watches" (fun c -> c.nb_watches) int + ; Dump.field "anonid" (fun c -> c.anonid) int + ; Dump.field "watches" (fun c -> c.watches) + @@ Dump.hashtbl Dump.string (Dump.list pp_dump_watch) + ; Dump.field "perm" (fun c -> c.perm) + @@ Fmt.using Perms.Connection.to_string Fmt.string ] + + let equal c c' =3D + let watch_equal w w' =3D + (* avoid recursion, these must be physically equal *) + w.con =3D=3D c && w'.con =3D=3D c' && w.token =3D w'.token && w.path= =3D w'.path + && w.base =3D w'.base + && w.is_relative =3D w'.is_relative + in + Xb.equal c.xb c'.xb + && hashtable_equal Transaction.equal c.transactions c'.transactions + (* next_tid ignored, not preserved *) + && hashtable_equal (list_equal watch_equal) c.watches c'.watches + && c.nb_watches =3D c'.nb_watches + (* anonid ignored, not preserved *) + (* && c.anonid =3D c'.anonid *) && c.perm =3D c'.perm + + let equal_watch w w' =3D + equal w.con w'.con && w.token =3D w'.token && w.path =3D w'.path + && w.base =3D w'.base + && w.is_relative =3D w'.is_relative +end + +module Trie =3D struct + open Trie + + let pp_dump dump_elt =3D + Fmt.Dump.iter_bindings Trie.iter (Fmt.any "trie") Fmt.string + Fmt.(option dump_elt) + + let plus1 _ _ acc =3D acc + 1 + + let length t =3D fold plus1 t 0 + + (* Trie.iter doesn't give full path so we can't compare the paths/values= exactly. + They will be compared as part of the individual connections + *) + let equal _eq t t' =3D length t =3D length t' +end + +module Connections =3D struct + open Connections + + let pp_dump =3D + let open Fmt in + Dump.record + [ Dump.field "anonymous" (fun t -> t.anonymous) + @@ Dump.hashtbl Fmt.(any "") Connection.pp_dump + ; Dump.field "domains" (fun t -> t.domains) + @@ Dump.hashtbl Fmt.int Connection.pp_dump + ; Dump.field "ports" (fun t -> t.ports) + @@ Dump.hashtbl + (Fmt.using Xeneventchn.to_int Fmt.int) + Connection.pp_dump + ; Dump.field "watches" (fun t -> t.watches) + @@ Trie.pp_dump (Dump.list Connection.pp_dump_watch) ] + + let equal c c' =3D + hashtable_equal Connection.equal c.anonymous c'.anonymous + && hashtable_equal Connection.equal c.domains c'.domains + (* TODO: local port changes for now *) + (*&& hashtable_equal Connection.equal c.ports c'.ports *) + && Trie.equal (list_equal Connection.equal_watch) c.watches c'.watches +end + +let pp_dump_connections =3D Connections.pp_dump +let equal_connections =3D Connections.equal + +module Domain =3D struct + open Domain + + let pp_dump =3D + let open Fmt in + Dump.record + [ Dump.field "id" Domain.get_id int + ; Dump.field "remote_port" Domain.get_remote_port int + ; Dump.field "bad_client" Domain.is_bad_domain bool + ; Dump.field "io_credit" Domain.get_io_credit int + ; Dump.field "conflict_credit" (fun t -> t.conflict_credit) float + ; Dump.field "caused_conflicts" (fun t -> t.caused_conflicts) int64 ] + + (* ignore stats fields *) + let equal t t' =3D t.id =3D t'.id && t.remote_port =3D t'.remote_port +end + +module Domains =3D struct + open Domains + + let pp_dump =3D + let open Fmt in + Dump.record + [ Dump.field "table" (fun t -> t.table) + @@ Dump.hashtbl Fmt.int Domain.pp_dump + ; Dump.field "doms_conflict_paused" (fun t -> t.doms_conflict_paused) + @@ Dump.queue (pp_dump_ref @@ Dump.option Domain.pp_dump) + ; Dump.field "doms_with_conflict_penalty" (fun t -> + t.doms_with_conflict_penalty) + @@ Dump.queue (pp_dump_ref @@ Dump.option Domain.pp_dump) + ; Dump.field "n_paused" (fun t -> t.n_paused) int + ; Dump.field "n_penalised" (fun t -> t.n_penalised) int ] + + (* ignore statistic fields *) + let equal t t' =3D hashtable_equal Domain.equal t.table t'.table +end +let pp_dump_domains =3D Domains.pp_dump +let equal_domains =3D Domains.equal diff --git a/tools/ocaml/xenstored/test/xenstored_test.ml b/tools/ocaml/xen= stored/test/xenstored_test.ml index e86b68e867..acf3209087 100644 --- a/tools/ocaml/xenstored/test/xenstored_test.ml +++ b/tools/ocaml/xenstored/test/xenstored_test.ml @@ -1,2 +1,147 @@ -open Xenstored -let () =3D () +open Testable +open Generator +module Cb =3D Crowbar + +let random_path =3D Cb.list Cb.bytes + +let value =3D Cb.bytes + +let token =3D Cb.bytes + +let permty =3D + [Perms.READ; Perms.WRITE; Perms.RDWR; Perms.NONE] + |> List.map Cb.const |> Cb.choose + +let new_domid =3D Cb.range ~min:1 Types.domid_first_reserved + +let port =3D Cb.range 0xFFFF_FFFF (*uint32_t*) + +let arb_cmd =3D + let open Command in + let path =3D + Cb.choose + [ Cb.map [Cb.int] (fun rnd state -> PathObserver.choose_path state r= nd) + ; Cb.map [random_path] (fun x _ -> Store.Path.to_string x) ] + in + let domid =3D + Cb.map [Cb.int] (fun rnd state -> PathObserver.choose_domid state rnd) + in + let perms =3D + Cb.map [domid; permty; Cb.pair domid permty |> Cb.list] + @@ fun idgen owner other state -> + let other =3D List.map (fun (idgen, ty) -> (idgen state, ty)) other in + Perms.Node.create (idgen state) owner other + in + let guard' ~f gen state =3D + let v =3D gen state in + Cb.guard (f v) ; + v + in + let cmd =3D + let open Testable.Command in + Cb.choose + [ Cb.map [path] cmd_read + ; Cb.map [path; value] cmd_write + ; Cb.map [path] cmd_mkdir + ; Cb.map [path] (fun p -> cmd_rm @@ guard' ~f:(fun p -> p <> "/") p) + ; Cb.map [path] cmd_directory + ; Cb.map [path] cmd_getperms + ; Cb.map [path; perms] cmd_setperms + ; Cb.map [path; token] cmd_watch + ; Cb.map [path; token] cmd_unwatch + ; Cb.const cmd_reset_watches + ; Cb.const cmd_transaction_start + ; Cb.map [Cb.bool] cmd_transaction_end + ; Cb.map [new_domid; port] cmd_introduce + ; Cb.map [domid] (fun idgen -> + cmd_release @@ guard' ~f:(fun id -> id <> 0) idgen) + ; Cb.map [domid] cmd_getdomainpath + ; Cb.map [domid] cmd_isintroduced + ; Cb.map [domid; domid] cmd_set_target + ; Cb.const cmd_liveupdate ] + in + Cb.map [domid; Cb.int; cmd] (fun this rnd cmd state -> + let this =3D this state in + let txid =3D PathObserver.choose_txid_opt state this rnd in + let cmd =3D cmd txid state in + (this, cmd)) + +(* based on QCSTM *) +module Make (Spec : sig + include Testable.S + + val arb_cmd : (state -> cmd) Crowbar.gen +end) =3D +struct + let arb_cmds =3D + Crowbar.with_printer (Fmt.Dump.list Spec.pp) + @@ Crowbar.map [Crowbar.list1 Spec.arb_cmd] (fun cmdgens -> + let cmds, _ =3D + List.fold_left + (fun (cmds, s) f -> + let cmd =3D f s in + Crowbar.check (Spec.precond cmd s) ; + (cmd :: cmds, Spec.next_state cmd s)) + ([], Spec.init_state) cmdgens + in + List.rev cmds) + + let interp_agree sut cs =3D + List.fold_left + (fun s cmd -> + Crowbar.check + ( try Spec.run_cmd cmd s sut + with Failure msg -> Crowbar.failf "%a" Fmt.lines msg ) ; + Spec.next_state cmd s) + Spec.init_state cs + + let agree_prop cs =3D + let on_exn e bt logs =3D + List.iter prerr_endline logs ; + Printexc.raise_with_backtrace e bt + in + Testable.with_logger ~on_exn (fun () -> + let sut =3D Spec.init_sut () in + Stdext.finally (fun () ->=20 + let (_ : Spec.state) =3D interp_agree sut cs in ()) + (fun () ->=20 + Spec.cleanup sut)) + + let agree_test ~name =3D Crowbar.add_test ~name [arb_cmds] agree_prop +end + +module LU =3D Make (struct + include PathObserver + + type cmd =3D int * Testable.Command.t + + type sut =3D Testable.t ref * Testable.t ref + + let arb_cmd =3D arb_cmd + + let init_sut () =3D + let sut1 =3D Testable.create () in + Testable.init sut1 ; + let sut2 =3D Testable.create ~live_update:true () in + Testable.init sut2 ; + let sut1 =3D ref sut1 in + let sut2 =3D ref sut2 in + (sut1, sut2) + + let cleanup (sut1, sut2) =3D + Testable.cleanup !sut1 ; Testable.cleanup !sut2 + + let run_cmd cmd state (sut1, sut2) =3D + Testable.run2 state.next_tid sut1 sut2 cmd ; + true +end) + +let () =3D + (* Crowbar runs at_exit, and after bisect's coverage dumper, + registering an at_exit here would run *before* Crowbar starts, + hence the nested at_exit which puts the bisect dumper in the proper p= lace + to dump coverage *after* crowbar is finished. + *) + (* at_exit (fun () -> at_exit Bisect.Runtime.write_coverage_data);*) + print_endline ""; + LU.agree_test ~name:"live-update-agree"; diff --git a/tools/ocaml/xenstored/test/xs_protocol.ml b/tools/ocaml/xensto= red/test/xs_protocol.ml new file mode 100644 index 0000000000..b5da2aff34 --- /dev/null +++ b/tools/ocaml/xenstored/test/xs_protocol.ml @@ -0,0 +1,733 @@ +(* + * Copyright (C) Citrix Systems Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation; version 2.1 only. with the special + * exception on linking described in file LICENSE. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + *) + +let ( |> ) f g =3D g f +let ( ++ ) f g x =3D f (g x) + +module Op =3D struct + type t =3D + | Debug | Directory | Read | Getperms + | Watch | Unwatch | Transaction_start + | Transaction_end | Introduce | Release + | Getdomainpath | Write | Mkdir | Rm + | Setperms | Watchevent | Error | Isintroduced + | Resume | Set_target + | Reset_watches | Directory_part + + let to_int32 =3D function + | Debug -> 0l + | Directory -> 1l + | Read -> 2l + | Getperms -> 3l + | Watch -> 4l + | Unwatch -> 5l + | Transaction_start -> 6l + | Transaction_end -> 7l + | Introduce -> 8l + | Release -> 9l + | Getdomainpath -> 10l + | Write -> 11l + | Mkdir -> 12l + | Rm -> 13l + | Setperms -> 14l + | Watchevent -> 15l + | Error -> 16l + | Isintroduced -> 17l + | Resume -> 18l + | Set_target -> 19l + | Reset_watches -> 21l (* 20 is reserved *) + | Directory_part -> 22l + + (* The index of the value in the array is the integer representation used + by the wire protocol. Every element of t exists exactly once in the a= rray. *) + let on_the_wire =3D + let a =3D Array.make 23 None in + ListLabels.iter + ~f:(fun v -> a.(v |> to_int32 |> Int32.to_int) <- Some v) + [ Debug; Directory; Read; Getperms; Watch; Unwatch; Transaction_start + ; Transaction_end; Introduce; Release; Getdomainpath; Write; Mkdir; = Rm + ; Setperms; Watchevent; Error; Isintroduced; Resume; Set_target + ; Reset_watches; Directory_part ] ; + a + + let of_int32 i =3D + let i =3D Int32.to_int i in + if i >=3D 0 && i < Array.length on_the_wire then on_the_wire.(i) else = None + + let to_string =3D function + | Debug -> "debug" + | Directory -> "directory" + | Read -> "read" + | Getperms -> "getperms" + | Watch -> "watch" + | Unwatch -> "unwatch" + | Transaction_start -> "transaction_start" + | Transaction_end -> "transaction_end" + | Introduce -> "introduce" + | Release -> "release" + | Getdomainpath -> "getdomainpath" + | Write -> "write" + | Mkdir -> "mkdir" + | Rm -> "rm" + | Setperms -> "setperms" + | Watchevent -> "watchevent" + | Error -> "error" + | Isintroduced -> "isintroduced" + | Resume -> "resume" + | Set_target -> "set_target" + | Reset_watches -> "reset_watches" + | Directory_part -> "directory_part" +end + +let split_string ?limit:(limit=3Dmax_int) c s =3D + let len =3D String.length s in + let next_c from =3D + try + Some (String.index_from s from c) + with + | Not_found -> None + in + let decr n =3D max 0 (n-1) in + let rec loop n from acc =3D + match decr n, next_c from with + | 0, _ + | _, None -> + (* No further instances of c, or we've reached limit *) + String.sub s from (len - from) :: acc + | n', Some idx -> + let a =3D String.sub s from (idx - from) in + (loop[@tailcall]) n' (idx + 1) (a :: acc) + in loop limit 0 [] |> List.rev + + +module ACL =3D struct + type perm =3D + | NONE + | READ + | WRITE + | RDWR + + let char_of_perm =3D function + | READ -> 'r' + | WRITE -> 'w' + | RDWR -> 'b' + | NONE -> 'n' + + let perm_of_char =3D function + | 'r' -> Some READ + | 'w' -> Some WRITE + | 'b' -> Some RDWR + | 'n' -> Some NONE + | _ -> None + + type domid =3D int + + type t =3D { + owner: domid; (** domain which "owns", has full access *) + other: perm; (** default permissions for all others... *) + acl: (domid * perm) list; (** ... unless overridden in the ACL *) + } + + let to_string perms =3D + let string_of_perm (id, perm) =3D Printf.sprintf "%c%u" (char_of_perm = perm) id in + String.concat "\000" (List.map string_of_perm ((perms.owner,perms.othe= r) :: perms.acl)) + + let of_string s =3D + (* A perm is stored as 'domid' *) + let perm_of_char_exn x =3D match (perm_of_char x) with Some y -> y | N= one -> raise Not_found in + try + let perm_of_string s =3D + if String.length s < 2 + then invalid_arg (Printf.sprintf "Permission string too short: '%s= '" s); + int_of_string (String.sub s 1 (String.length s - 1)), perm_of_char= _exn s.[0] in + let l =3D List.map perm_of_string (split_string '\000' s) in + match l with + | (owner, other) :: l -> Some { owner =3D owner; other =3D other; ac= l =3D l } + | [] -> Some { owner =3D 0; other =3D NONE; acl =3D [] } + with _ -> + None +end + +type t =3D { + tid: int32; + rid: int32; + ty: Op.t; + len: int; + data: Buffer.t; +} + +let sizeof_header =3D 16 +let get_header_ty v =3D Cstruct.LE.get_uint32 v 0 +let set_header_ty v x =3D Cstruct.LE.set_uint32 v 0 x +let get_header_rid v =3D Cstruct.LE.get_uint32 v 4 +let set_header_rid v x =3D Cstruct.LE.set_uint32 v 4 x +let get_header_tid v =3D Cstruct.LE.get_uint32 v 8 +let set_header_tid v x =3D Cstruct.LE.set_uint32 v 8 x +let get_header_len v =3D Cstruct.LE.get_uint32 v 12 +let set_header_len v x =3D Cstruct.LE.set_uint32 v 12 x + +let to_bytes pkt =3D + let header =3D Cstruct.create sizeof_header in + let len =3D Int32.of_int (Buffer.length pkt.data) in + let ty =3D Op.to_int32 pkt.ty in + set_header_ty header ty; + set_header_rid header pkt.rid; + set_header_tid header pkt.tid; + set_header_len header len; + let result =3D Buffer.create 64 in + Buffer.add_bytes result (Cstruct.to_bytes header); + Buffer.add_buffer result pkt.data; + Buffer.to_bytes result + +let get_tid pkt =3D pkt.tid +let get_ty pkt =3D pkt.ty +let get_data pkt =3D + if pkt.len > 0 && Buffer.nth pkt.data (pkt.len - 1) =3D '\000' then + Buffer.sub pkt.data 0 (pkt.len - 1) + else + Buffer.contents pkt.data +let get_rid pkt =3D pkt.rid + +module Parser =3D struct + (** Incrementally parse packets *) + + let header_size =3D 16 + + let xenstore_payload_max =3D 4096 (* xen/include/public/io/xs_wire.h *) + + let allow_oversize_packets =3D ref true + + type state =3D + | Unknown_operation of int32 + | Parser_failed of string + | Need_more_data of int + | Packet of t + + type parse =3D + | ReadingHeader of int * bytes + | ReadingBody of t + | Finished of state + + let start () =3D ReadingHeader (0, Bytes.make header_size '\000') + + let state =3D function + | ReadingHeader(got_already, _) -> Need_more_data (header_size - got_a= lready) + | ReadingBody pkt -> Need_more_data (pkt.len - (Buffer.length pkt.data= )) + | Finished r -> r + + let parse_header str =3D + let header =3D Cstruct.create sizeof_header in + Cstruct.blit_from_string str 0 header 0 sizeof_header; + let ty =3D get_header_ty header in + let rid =3D get_header_rid header in + let tid =3D get_header_tid header in + let len =3D get_header_len header in + + let len =3D Int32.to_int len in + (* A packet which is bigger than xenstore_payload_max is illegal. + This will leave the guest connection is a bad state and will + be hard to recover from without restarting the connection + (ie rebooting the guest) *) + let len =3D if !allow_oversize_packets then len else max 0 (min xensto= re_payload_max len) in + + begin match Op.of_int32 ty with + | Some ty -> + let t =3D { + tid =3D tid; + rid =3D rid; + ty =3D ty; + len =3D len; + data =3D Buffer.create len; + } in + if len =3D 0 + then Finished (Packet t) + else ReadingBody t + | None -> Finished (Unknown_operation ty) + end + + let input state (bytes : string) =3D + match state with + | ReadingHeader(got_already, (str : bytes)) -> + Bytes.blit_string bytes 0 str got_already (String.length bytes); + let got_already =3D got_already + (String.length bytes) in + if got_already < header_size + then ReadingHeader(got_already, str) + else parse_header (Bytes.to_string str) + | ReadingBody x -> + Buffer.add_string x.data bytes; + let needed =3D x.len - (Buffer.length x.data) in + if needed > 0 + then ReadingBody x + else Finished (Packet x) + | Finished f -> Finished f +end + +(* Should we switch to an explicit stream abstraction here? *) +module type IO =3D sig + type 'a t + val return: 'a -> 'a t + val ( >>=3D ): 'a t -> ('a -> 'b t) -> 'b t + + type channel + val read: channel -> bytes -> int -> int -> int t + val write: channel -> bytes -> int -> int -> unit t +end + +exception Unknown_xenstore_operation of int32 +exception Response_parser_failed of string +exception EOF + +type ('a, 'b) result =3D + | Ok of 'a + | Exception of 'b + +module PacketStream =3D functor(IO: IO) -> struct + let ( >>=3D ) =3D IO.( >>=3D ) + let return =3D IO.return + + type stream =3D { + channel: IO.channel; + mutable incoming_pkt: Parser.parse; (* incrementally parses the next p= acket *) + } + + let make t =3D { + channel =3D t; + incoming_pkt =3D Parser.start (); + } + + (* [recv client] returns a single Packet, or fails *) + let rec recv t =3D + let open Parser in match Parser.state t.incoming_pkt with + | Packet pkt -> + t.incoming_pkt <- start (); + return (Ok pkt) + | Need_more_data x -> + let buf =3D Bytes.make x '\000' in + IO.read t.channel buf 0 x + >>=3D (function + | 0 -> return (Exception EOF) + | n -> + let fragment =3D Bytes.sub_string buf 0 n in + t.incoming_pkt <- input t.incoming_pkt fragment; + recv t) + | Unknown_operation x -> return (Exception (Unknown_xenstore_operation= x)) + | Parser_failed x -> return (Exception (Response_parser_failed x)) + + (* [send client pkt] sends [pkt] and returns (), or fails *) + let send t request =3D + let req =3D to_bytes request in + IO.write t.channel req 0 (Bytes.length req) +end + +module Token =3D struct + type t =3D string + + (** [to_user_string x] returns the user-supplied part of the watch token= *) + let to_user_string x =3D Scanf.sscanf x "%d:%s" (fun _ x -> x) + + let to_debug_string x =3D x + + let of_string x =3D x + let to_string x =3D x +end + +let data_concat ls =3D (String.concat "\000" ls) ^ "\000" + +let create tid rid ty data =3D + let len =3D String.length data in + let b =3D Buffer.create len in + Buffer.add_string b data; + { + tid =3D tid; + rid =3D rid; + ty =3D ty; + len =3D len; + data =3D b; + } + +module Response =3D struct + + type payload =3D + | Read of string + | Directory of string list + | Getperms of ACL.t + | Getdomainpath of string + | Transaction_start of int32 + | Write + | Mkdir + | Rm + | Setperms + | Watch + | Unwatch + | Transaction_end + | Debug of string list + | Introduce + | Resume + | Release + | Set_target + | Reset_watches + | Directory_part of int * string list + | Isintroduced of bool + | Error of string + | Watchevent of string * string + + let prettyprint_payload =3D + let open Printf in function + | Read x -> sprintf "Read %s" x + | Directory xs -> sprintf "Directory [ %s ]" (String.concat "; " xs) + | Getperms acl -> sprintf "Getperms %s" (ACL.to_string acl) + | Getdomainpath p -> sprintf "Getdomainpath %s" p + | Transaction_start x -> sprintf "Transaction_start %ld" x + | Write -> "Write" + | Mkdir -> "Mkdir" + | Rm -> "Rm" + | Setperms -> "Setperms" + | Watch -> "Watch" + | Unwatch -> "Unwatch" + | Transaction_end -> "Transaction_end" + | Debug xs -> sprintf "Debug [ %s ]" (String.concat "; " xs) + | Introduce -> "Introduce" + | Resume -> "Resume" + | Release -> "Release" + | Set_target -> "Set_target" + | Reset_watches -> "Reset_watches" + | Directory_part (gencnt, xs) -> + sprintf "Directory_part #%d [ %s ]" gencnt (String.concat "; " x= s) + | Isintroduced x -> sprintf "Isintroduced %b" x + | Error x -> sprintf "Error %s" x + | Watchevent (x, y) -> sprintf "Watchevent %s %s" x y + + let ty_of_payload =3D function + | Read _ -> Op.Read + | Directory _ -> Op.Directory + | Getperms _ -> Op.Getperms + | Getdomainpath _ -> Op.Getdomainpath + | Transaction_start _ -> Op.Transaction_start + | Debug _ -> Op.Debug + | Isintroduced _ -> Op.Isintroduced + | Watchevent (_, _) -> Op.Watchevent + | Error _ -> Op.Error + | Write -> Op.Write + | Mkdir -> Op.Mkdir + | Rm -> Op.Rm + | Setperms -> Op.Setperms + | Watch -> Op.Watch + | Unwatch -> Op.Unwatch + | Transaction_end -> Op.Transaction_end + | Introduce -> Op.Introduce + | Resume -> Op.Resume + | Release -> Op.Release + | Set_target -> Op.Set_target + | Reset_watches -> Op.Reset_watches + | Directory_part _ -> Op.Directory_part + + let ok =3D "OK\000" + + let data_of_payload =3D function + | Read x -> x + | Directory ls -> if ls =3D [] then "" else data_concat ls + | Getperms perms -> data_concat [ ACL.to_string perms ] + | Getdomainpath x -> data_concat [ x ] + | Transaction_start tid -> data_concat [ Int32.to_string tid ] + | Debug items -> data_concat items + | Isintroduced b -> data_concat [ if b then "T" else "F" ] + | Watchevent (path, token) -> data_concat [ path; token ] + | Error x -> data_concat [ x ] + | _ -> ok + + let print x tid rid =3D + create tid rid (ty_of_payload x) (data_of_payload x) +end + +module Request =3D struct + + type path_op =3D + | Read + | Directory + | Directory_part of int + | Getperms + | Write of string + | Mkdir + | Rm + | Setperms of ACL.t + + type payload =3D + | PathOp of string * path_op + | Getdomainpath of int + | Transaction_start + | Watch of string * string + | Unwatch of string * string + | Transaction_end of bool + | Debug of string list + | Introduce of int * Nativeint.t * int + | Resume of int + | Release of int + | Set_target of int * int + | Reset_watches + | Isintroduced of int + | Error of string + | Watchevent of string + + open Printf + + let prettyprint_pathop x =3D function + | Read -> sprintf "Read %s" x + | Directory -> sprintf "Directory %s" x + | Directory_part off -> sprintf "Directory %s @%d" x off + | Getperms -> sprintf "Getperms %s" x + | Write v -> sprintf "Write %s %s" x v + | Mkdir -> sprintf "Mkdir %s" x + | Rm -> sprintf "Rm %s" x + | Setperms acl -> sprintf "Setperms %s %s" x (ACL.to_string acl) + + let prettyprint_payload =3D function + | PathOp (path, op) -> prettyprint_pathop path op + | Getdomainpath x -> sprintf "Getdomainpath %d" x + | Transaction_start -> "Transaction_start" + | Watch (x, y) -> sprintf "Watch %s %s" x y + | Unwatch (x, y) -> sprintf "Unwatch %s %s" x y + | Transaction_end x -> sprintf "Transaction_end %b" x + | Debug xs -> sprintf "Debug [ %s ]" (String.concat "; " xs) + | Introduce (x, n, y) -> sprintf "Introduce %d %nu %d" x n y + | Resume x -> sprintf "Resume %d" x + | Release x -> sprintf "Release %d" x + | Set_target (x, y) -> sprintf "Set_target %d %d" x y + | Reset_watches -> "Reset_watches" + | Isintroduced x -> sprintf "Isintroduced %d" x + | Error x -> sprintf "Error %s" x + | Watchevent x -> sprintf "Watchevent %s" x + + exception Parse_failure + + let strings data =3D split_string '\000' data + + let one_string data =3D + let args =3D split_string ~limit:2 '\000' data in + match args with + | x :: [] -> x + | _ -> + raise Parse_failure + + let two_strings data =3D + let args =3D split_string ~limit:2 '\000' data in + match args with + | a :: b :: [] -> a, b + | a :: [] -> a, "" (* terminating NULL removed by get_data *) + | _ -> + raise Parse_failure + + let acl x =3D match ACL.of_string x with + | Some x -> x + | None -> + raise Parse_failure + + let domid s =3D + let v =3D ref 0 in + let is_digit c =3D c >=3D '0' && c <=3D '9' in + let len =3D String.length s in + let i =3D ref 0 in + while !i < len && not (is_digit s.[!i]) do incr i done; + while !i < len && is_digit s.[!i] + do + let x =3D (Char.code s.[!i]) - (Char.code '0') in + v :=3D !v * 10 + x; + incr i + done; + !v + + let bool =3D function + | "F" -> false + | "T" -> true + | _ -> + raise Parse_failure + + let parse_exn request =3D + let data =3D get_data request in + match get_ty request with + | Op.Read -> PathOp (data |> one_string, Read) + | Op.Directory -> PathOp (data |> one_string, Directory) + | Op.Getperms -> PathOp (data |> one_string, Getperms) + | Op.Getdomainpath -> Getdomainpath (data |> one_string |> domid) + | Op.Transaction_start -> Transaction_start + | Op.Write -> + let path, value =3D two_strings data in + PathOp (path, Write value) + | Op.Mkdir -> PathOp (data |> one_string, Mkdir) + | Op.Rm -> PathOp (data |> one_string, Rm) + | Op.Setperms -> + let path, perms =3D two_strings data in + let perms =3D acl perms in + PathOp(path, Setperms perms) + | Op.Watch -> + let path, token =3D two_strings data in + Watch(path, token) + | Op.Unwatch -> + let path, token =3D two_strings data in + Unwatch(path, token) + | Op.Transaction_end -> Transaction_end(data |> one_string |> bool) + | Op.Debug -> Debug (strings data) + | Op.Introduce -> + begin match strings data with + | d :: mfn :: port :: _ -> + let d =3D domid d in + let mfn =3D Nativeint.of_string mfn in + let port =3D int_of_string port in + Introduce (d, mfn, port) + | _ -> + raise Parse_failure + end + | Op.Resume -> Resume (data |> one_string |> domid) + | Op.Release -> Release (data |> one_string |> domid) + | Op.Set_target -> + let mine, yours =3D two_strings data in + let mine =3D domid mine and yours =3D domid yours in + Set_target(mine, yours) + | Op.Reset_watches -> Reset_watches + | Op.Directory_part -> + let path, offstr =3D two_strings data in + PathOp (path, Directory_part (int_of_string offstr)) + | Op.Isintroduced -> Isintroduced (data |> one_string |> domid) + | Op.Error -> Error(data |> one_string) + | Op.Watchevent -> Watchevent(data |> one_string) + + let parse request =3D + try + Some (parse_exn request) + with _ -> None + + let prettyprint request =3D + Printf.sprintf "tid =3D %ld; rid =3D %ld; payload =3D %s" + (get_tid request) (get_rid request) + (match parse request with + | None -> "None" + | Some x -> "Some " ^ (prettyprint_payload x)) + + let ty_of_payload =3D function + | PathOp(_, Directory) -> Op.Directory + | PathOp(_, Read) -> Op.Read + | PathOp(_, Getperms) -> Op.Getperms + | Debug _ -> Op.Debug + | Watch (_, _) -> Op.Watch + | Unwatch (_, _) -> Op.Unwatch + | Transaction_start -> Op.Transaction_start + | Transaction_end _ -> Op.Transaction_end + | Introduce(_, _, _) -> Op.Introduce + | Release _ -> Op.Release + | Resume _ -> Op.Resume + | Getdomainpath _ -> Op.Getdomainpath + | PathOp(_, Write _) -> Op.Write + | PathOp(_, Mkdir) -> Op.Mkdir + | PathOp(_, Rm) -> Op.Rm + | PathOp(_, Setperms _) -> Op.Setperms + | Set_target (_, _) -> Op.Set_target + | Reset_watches -> Op.Reset_watches + | PathOp(_, Directory_part _) -> Op.Directory_part + | Isintroduced _ -> Op.Isintroduced + | Error _ -> Op.Error + | Watchevent _ -> Op.Watchevent + + let transactional_of_payload =3D function + | PathOp(_, _) + | Transaction_end _ -> true + | _ -> false + + let data_of_payload =3D function + | PathOp(path, Write value) -> + path ^ "\000" ^ value (* no NULL at the end *) + | PathOp(path, Setperms perms) -> + data_concat [ path; ACL.to_string perms ] + | PathOp(path, _) -> data_concat [ path ] + | Debug commands -> data_concat commands + | Watch (path, token) + | Unwatch (path, token) -> data_concat [ path; token ] + | Transaction_start -> data_concat [] + | Transaction_end commit -> data_concat [ if commit then "T" else "F" ] + | Introduce(domid, mfn, port) -> + data_concat [ + Printf.sprintf "%u" domid; + Printf.sprintf "%nu" mfn; + string_of_int port; + ] + | Release domid + | Resume domid + | Getdomainpath domid + | Isintroduced domid -> + data_concat [ Printf.sprintf "%u" domid; ] + | Reset_watches -> data_concat [] + | Set_target (mine, yours) -> + data_concat [ Printf.sprintf "%u" mine; Printf.sprintf "%u" yours; ] + | Error _ -> + failwith "Unimplemented: data_of_payload (Error)" + | Watchevent _ -> + failwith "Unimplemented: data_of_payload (Watchevent)" + + let print x tid rid =3D + create + (if transactional_of_payload x then tid else 0l) + rid + (ty_of_payload x) + (data_of_payload x) +end + +module Unmarshal =3D struct + let some x =3D Some x + let int_of_string_opt x =3D try Some(int_of_string x) with _ -> None + let int32_of_string_opt x =3D try Some(Int32.of_string x) with _ -> None + let unit_of_string_opt x =3D if x =3D "" then Some () else None + let ok x =3D if x =3D "OK" then Some () else None + + let string =3D some ++ get_data + let list =3D some ++ split_string '\000' ++ get_data + let acl =3D ACL.of_string ++ get_data + let int =3D int_of_string_opt ++ get_data + let int32 =3D int32_of_string_opt ++ get_data + let unit =3D unit_of_string_opt ++ get_data + let ok =3D ok ++ get_data +end + +exception Enoent of string +exception Eagain +exception Eexist +exception Invalid +exception Error of string + +let response hint sent received f =3D match get_ty sent, get_ty received w= ith + | _, Op.Error -> + begin match get_data received with + | "ENOENT" -> raise (Enoent hint) + | "EAGAIN" -> raise Eagain + | "EINVAL" -> raise Invalid + | "EEXIST" -> raise Eexist + | s -> raise (Error s) + end + | x, y when x =3D y -> + begin match f received with + | None -> raise (Error (Printf.sprintf "failed to parse response (hi= nt:%s) (payload:%s)" hint (get_data received))) + | Some z -> z + end + | x, y -> + raise (Error (Printf.sprintf "unexpected packet: expected %s; got %s" = (Op.to_string x) (Op.to_string y))) + +type address =3D + | Unix of string + | Domain of int + +let string_of_address =3D function + | Unix x -> x + | Domain x -> string_of_int x + +let domain_of_address =3D function + | Unix _ -> 0 + | Domain x -> x + diff --git a/tools/ocaml/xenstored/transaction.ml b/tools/ocaml/xenstored/t= ransaction.ml index 17b1bdf2ea..0466b04ae3 100644 --- a/tools/ocaml/xenstored/transaction.ml +++ b/tools/ocaml/xenstored/transaction.ml @@ -82,6 +82,7 @@ type t =3D { start_count: int64; store: Store.t; (* This is the store that we change in write operations. = *) quota: Quota.t; + mutable must_fail: bool; oldroot: Store.Node.t; mutable paths: (Xenbus.Xb.Op.operation * Store.Path.t) list; mutable operations: (Packet.request * Packet.response) list; @@ -89,7 +90,7 @@ type t =3D { mutable write_lowpath: Store.Path.t option; } let get_id t =3D match t.ty with No -> none | Full (id, _, _) -> id - +let mark_failed t =3D t.must_fail <- true let counter =3D ref 0L let failed_commits =3D ref 0L let failed_commits_no_culprit =3D ref 0L @@ -117,6 +118,8 @@ let trim_short_running_transactions txn =3D keep !short_running_txns =20 +let invalid_op =3D Xenbus.Xb.Op.Invalid, [] + let make ?(internal=3Dfalse) id store =3D let ty =3D if id =3D none then No else Full(id, Store.copy store, store) = in let txn =3D { @@ -129,6 +132,7 @@ let make ?(internal=3Dfalse) id store =3D operations =3D []; read_lowpath =3D None; write_lowpath =3D None; + must_fail =3D false; } in if id <> none && not internal then ( let now =3D Unix.gettimeofday () in @@ -139,10 +143,11 @@ let make ?(internal=3Dfalse) id store =3D let get_store t =3D t.store let get_paths t =3D t.paths =20 +let is_read_only t =3D t.paths =3D [] && not t.must_fail let get_root t =3D Store.get_root t.store =20 -let is_read_only t =3D t.paths =3D [] let add_wop t ty path =3D t.paths <- (ty, path) :: t.paths +let clear_wops t =3D t.paths <- [] let add_operation ~perm t request response =3D if !Define.maxrequests >=3D 0 && not (Perms.Connection.is_dom0 perm) @@ -151,7 +156,9 @@ let add_operation ~perm t request response =3D t.operations <- (request, response) :: t.operations let get_operations t =3D List.rev t.operations let set_read_lowpath t path =3D t.read_lowpath <- get_lowest path t.read_l= owpath -let set_write_lowpath t path =3D t.write_lowpath <- get_lowest path t.writ= e_lowpath +let set_write_lowpath t path =3D + Logging.debug "transaction" "set_writelowpath (%d) %s" (get_id t) (Stor= e.Path.to_string path); + t.write_lowpath <- get_lowest path t.write_lowpath =20 let path_exists t path =3D Store.path_exists t.store path =20 @@ -200,7 +207,7 @@ let commit ~con t =3D let has_commited =3D match t.ty with | No -> true - | Full (_id, oldstore, cstore) -> (* "cstore" meaning current canon= ical store *) + | Full (id, oldstore, cstore) -> (* "cstore" meaning current canoni= cal store *) let commit_partial oldroot cstore store =3D (* get the lowest path of the query and verify that it hasn't been modified by others transactions. *) @@ -240,11 +247,16 @@ let commit ~con t =3D (* we try a partial commit if possible *) commit_partial oldroot cstore store in + if t.must_fail then begin + Logging.info "transaction" "Transaction %d was marked to fail (by live-= update)" id; + false + end else if !test_eagain && Random.int 3 =3D 0 then false else try_commit (Store.get_root oldstore) cstore t.store in + Logging.info "transaction" "has_commited: %b" has_commited; if has_commited && has_write_ops then Disk.write t.store; if not has_commited @@ -252,3 +264,102 @@ let commit ~con t =3D else if not !has_coalesced then Logging.commit ~tid:(get_id t) ~con; has_commited + +module LR =3D Disk.LiveRecord + +(* here instead of Store.ml to avoid dependency cycle *) +let write_node ch txidaccess path node =3D + let value =3D Store.Node.get_value node in + let perms =3D Store.Node.get_perms node in + let path =3D Store.Path.of_path_and_name path (Symbol.to_string node.Stor= e.Node.name) |> Store.Path.to_string in + LR.write_node_data ch ~txidaccess ~path ~value ~perms + +let split limit c s =3D + let limit =3D match limit with None -> 8 | Some x -> x in + String.split ~limit c s +=09 +exception Invalid_Cmd_Args +let split_one_path data conpath =3D + let args =3D split (Some 2) '\000' data in + match args with + | path :: "" :: [] -> Store.Path.create path conpath + | _ -> raise Invalid_Cmd_Args +=09 +let dump base conpath ~conid txn ch =3D + (* TODO: implicit paths need to be converted to explicit *) + let txid =3D get_id txn in + LR.write_transaction_data ch ~conid ~txid; + let store =3D get_store txn in + let write_node_mkdir path =3D + let perms, value =3D match Store.get_node store path with + | None -> Perms.Node.default0, "" (* need to dump mkdir anyway even if l= ater deleted due to implicit path creation *) + | Some node -> Store.Node.get_perms node, Store.Node.get_value node (* n= ot always "", e.g. on EEXIST *) in + LR.write_node_data ch ~txidaccess:(Some (conid, txid, LR.W)) ~path:(Stor= e.Path.to_string path) ~value ~perms +in + maybe (fun path -> + (* if there were any reads make sure the tree matches, remove all conten= ts and write out subtree *) + match Store.get_node store path with + | None -> (* we've only read nodes that we ended up deleting, nothing to= do *) () + | Some node -> + write_node ch (Some (conid, txid, LR.Del)) (Store.Path.get_parent path)= node; + let path =3D Store.Path.get_parent path in + Store.traversal node @@ fun path' node -> + write_node ch (Some (conid,txid, LR.R)) (List.append path path') node + ) txn.read_lowpath; + (* we could do something similar for write_lowpath, but that would become=20 + complicated to handle correctly wrt to permissions and quotas if there= are nodes + owned by other domains in the subtree. + *) + let ops =3D get_operations txn in + if ops <> [] then + (* mark that we had some operation, these could be failures, etc. + we want to fail the transaction after a live-update, + unless it is completely a no-op + *) + let perms =3D Store.getperms store Perms.Connection.full_rights [] in + let value =3D Store.get_root store |> Store.Node.get_value in + LR.write_node_data ch ~txidaccess:(Some (conid, txid, LR.R)) ~path:"/" = ~value ~perms; + ListLabels.iter (fun (req, reply) -> + Logging.debug "transaction" "dumpop %s" (Xenbus.Xb.Op.to_string req.Pack= et.ty);=20 + let data =3D req.Packet.data in + let open Xenbus.Xb.Op in + match reply with + | Packet.Error _ -> () + | _ -> + try match req.Packet.ty with +| Debug +| Watch +| Unwatch +| Transaction_start +| Transaction_end +| Introduce +| Release +| Watchevent +| Getdomainpath +| Error +| Isintroduced +| Resume +| Set_target +| Reset_watches +| Invalid +| Directory +| (Read|Getperms) -> () +| (Write|Setperms) -> + (match (split (Some 2) '\000' data) with + | path :: _ :: _ -> + let path =3D Store.Path.create path conpath in + if req.Packet.ty =3D Write then + write_node_mkdir (Store.Path.get_parent path);(* implicit mkdir *) + (match Store.get_node store path with + | None -> () + | Some node -> + write_node ch (Some (conid, txid, LR.W)) (Store.Path.get_parent path) nod= e) + | _ -> raise Invalid_Cmd_Args) +| Mkdir -> + let path =3D split_one_path data conpath in + write_node_mkdir path; +| Rm -> + let path =3D split_one_path data conpath |> Store.Path.to_string in + LR.write_node_data ch ~txidaccess:(Some (conid, txid, LR.Del)) ~path ~val= ue:"" ~perms:Perms.Node.default0 + with Invalid_Cmd_Args|Define.Invalid_path|Not_found-> () + ) ops --=20 2.25.1