wip: fix fuchsia in case strings are too big

This commit is contained in:
Simon Cruanes 2024-02-13 16:12:08 -05:00
parent d3e710605e
commit a1df7eb88e
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 29 additions and 5 deletions

View file

@ -29,13 +29,22 @@ open struct
flush_ self; flush_ self;
let buf = self.buf in let buf = self.buf in
if Buf.available buf < available then if Buf.available buf < available then (
failwith "fuchsia: buffer is too small"; let msg =
Printf.sprintf
"fuchsia: buffer is too small (available: %d bytes, needed: %d bytes)"
(Buf.available buf) available
in
failwith msg
);
buf buf
end end
let[@inline] flush (self : t) : unit = if Buf.size self.buf > 0 then flush_ self 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 *) (** Obtain a buffer with at least [available] bytes *)
let[@inline] get_buf (self : t) ~(available_word : int) : Buf.t = let[@inline] get_buf (self : t) ~(available_word : int) : Buf.t =
let available = available_word lsl 3 in let available = available_word lsl 3 in

View file

@ -41,6 +41,20 @@ module Str_ref = struct
(1 lsl 15) lor size (1 lsl 15) lor size
end 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 module Thread_ref = struct
type t = type t =
| Ref of int | Ref of int
@ -117,8 +131,9 @@ end
module Argument = struct module Argument = struct
type 'a t = string * ([< user_data | `Kid of int ] as 'a) type 'a t = string * ([< user_data | `Kid of int ] as 'a)
let check_valid _ = () let check_valid_ : _ t -> unit = function
(* TODO: check string length *) | _, `String s -> assert (String.length s < max_str_len)
| _ -> ()
let[@inline] is_i32_ (i : int) : bool = Int32.(to_int (of_int i) = i) 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 let len = len self in
if len > 15 then if len > 15 then
invalid_arg (spf "fuchsia: can have at most 15 args, got %d" len); 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) = let[@inline] size_word (self : _ t) =