diff --git a/src/fuchsia/write/output.ml b/src/fuchsia/write/output.ml index 0ee6be7..a2f79d4 100644 --- a/src/fuchsia/write/output.ml +++ b/src/fuchsia/write/output.ml @@ -29,13 +29,22 @@ open struct flush_ self; let buf = self.buf in - if Buf.available buf < available then - failwith "fuchsia: buffer is too small"; + if Buf.available buf < available then ( + let msg = + Printf.sprintf + "fuchsia: buffer is too small (available: %d bytes, needed: %d bytes)" + (Buf.available buf) available + in + failwith msg + ); buf end let[@inline] flush (self : t) : unit = if Buf.size self.buf > 0 then flush_ self +(** Maximum size available, in words, for a single message *) +let[@inline] max_size_word (self : t) : int = self.buf_pool.buf_size lsr 3 + (** Obtain a buffer with at least [available] bytes *) let[@inline] get_buf (self : t) ~(available_word : int) : Buf.t = let available = available_word lsl 3 in diff --git a/src/fuchsia/write/trace_fuchsia_write.ml b/src/fuchsia/write/trace_fuchsia_write.ml index ebcc8cf..04ea591 100644 --- a/src/fuchsia/write/trace_fuchsia_write.ml +++ b/src/fuchsia/write/trace_fuchsia_write.ml @@ -41,6 +41,20 @@ module Str_ref = struct (1 lsl 15) lor size end +open struct + (** maximum length as specified in the + {{: https://fuchsia.dev/fuchsia-src/reference/tracing/trace-format} spec} *) + let max_str_len = 32000 +end + +(** [truncate_string s] truncates [s] to the maximum length allowed for + strings. If [s] is already short enough, no allocation is done. *) +let truncate_string s : string = + if String.length s <= max_str_len then + s + else + String.sub s 0 max_str_len + module Thread_ref = struct type t = | Ref of int @@ -117,8 +131,9 @@ end module Argument = struct type 'a t = string * ([< user_data | `Kid of int ] as 'a) - let check_valid _ = () - (* TODO: check string length *) + let check_valid_ : _ t -> unit = function + | _, `String s -> assert (String.length s < max_str_len) + | _ -> () let[@inline] is_i32_ (i : int) : bool = Int32.(to_int (of_int i) = i) @@ -204,7 +219,7 @@ module Arguments = struct let len = len self in if len > 15 then invalid_arg (spf "fuchsia: can have at most 15 args, got %d" len); - List.iter Argument.check_valid self; + List.iter Argument.check_valid_ self; () let[@inline] size_word (self : _ t) =