Skip to content

Commit 0bce57e

Browse files
committed
avoid string copies and fix an invalid String.sub
Co-authored-by: Reynir Björnsson <reynir@reynir.dk>
1 parent 3521796 commit 0bce57e

File tree

5 files changed

+104
-102
lines changed

5 files changed

+104
-102
lines changed

lib/formats.ml

Lines changed: 82 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ let of_int32_le i =
66
let i = Int32.to_int i in
77
String.init 4 (fun p -> Char.chr ((i lsr (p*8)) land 0xff))
88

9+
(* String.get_* exixt since 4.13, this stub will be removed when the min
10+
Ocaml version will match *)
11+
let get_int32_le s =
12+
Bytes.get_int32_le (Bytes.unsafe_of_string s)
13+
let get_int32_be s =
14+
Bytes.get_int32_be (Bytes.unsafe_of_string s)
15+
let get_uint8 s =
16+
Bytes.get_uint8 (Bytes.unsafe_of_string s)
17+
918
module type FRAMING = sig
1019
val header_size : int
1120
val body_size_from_header : String.t -> int
@@ -16,16 +25,16 @@ module Qrexec = struct
1625
ty : int32;
1726
len : int32;
1827
}
19-
let get_msg_header_ty h = String.get_int32_le h 0
28+
let get_msg_header_ty h = get_int32_le h 0
2029
(* let set_msg_header_ty h v = Bytes.set_int32_le h 0 v *)
21-
let get_msg_header_len h = String.get_int32_le h 4
30+
let get_msg_header_len h = get_int32_le h 4
2231
(* let set_msg_heade.r_len h vString= Bytes.set_int32_le h 4 v *)
2332
let sizeof_msg_header = 8
2433

2534
type peer_info = {
2635
version : int32;
2736
}
28-
let get_peer_info_version h = String.get_int32_le h 0
37+
let get_peer_info_version h = get_int32_le h 0
2938
(* let set_peer_info_version h v = Bytes.set_int32_le h 0 v *)
3039
let sizeof_peer_info = 4
3140

@@ -34,16 +43,16 @@ module Qrexec = struct
3443
connect_port : int32;
3544
(* rest of message is command line *)
3645
}
37-
let get_exec_params_connect_domain h = String.get_int32_le h 0
46+
let get_exec_params_connect_domain h = get_int32_le h 0
3847
(* let set_exec_params_connect_domain h v = Bytes.set_int32_le h 0 v *)
39-
let get_exec_params_connect_port h = String.get_int32_le h 4
48+
let get_exec_params_connect_port h = get_int32_le h 4
4049
(* let set_exec_params_connect_port h v = Bytes.set_int32_le h 4 v *)
4150
let sizeof_exec_params = 8
4251

4352
type exit_status = {
4453
return_code : int32;
4554
}
46-
let get_exit_status_return_code h = String.get_int32_le h 0
55+
let get_exit_status_return_code h = get_int32_le h 0
4756
(* let set_exit_status_return_code h v = Bytes.set_int32_le h 0 v *)
4857
let sizeof_exit_status = 4
4958

@@ -171,11 +180,11 @@ module GUI = struct
171180
window : int32;
172181
untrusted_len : int32;
173182
}
174-
let get_msg_header_ty h = String.get_int32_le h 0
183+
let get_msg_header_ty h = get_int32_le h 0
175184
(* let set._msg_header_ty h v = Bytes.set_int32_le h 0 v *)
176-
let get_msg_header_window h = String.get_int32_le h 4
185+
let get_msg_header_window h = get_int32_le h 4
177186
(* let set_msg_header_window h v = Bytes.set_int32_le h 4 v *)
178-
let get_msg_header_untrusted_len h = String.get_int32_le h 8
187+
let get_msg_header_untrusted_len h = get_int32_le h 8
179188
(* let set_msg_header_untrusted_len h v = Bytes.set_int32_le h 8 v *)
180189
let sizeof_msg_header = 12
181190

@@ -184,9 +193,9 @@ module GUI = struct
184193
override_redirect : int32;
185194
transient_for : int32;
186195
}
187-
let get_msg_map_info_override_redirect h = String.get_int32_le h 0
196+
let get_msg_map_info_override_redirect h = get_int32_le h 0
188197
(* let set_msg_map_info_override_redirect h v = Bytes.set_int32_le h 0 v *)
189-
let get_msg_map_info_transient_for h = String.get_int32_le h 4
198+
let get_msg_map_info_transient_for h = get_int32_le h 4
190199
(* let set_msg_map_info_transient_for h v = Bytes.set_int32_le h 4 v *)
191200
let sizeof_msg_map_info = 8
192201

@@ -200,9 +209,9 @@ module GUI = struct
200209
len : int32;
201210
(* followed by a uint8 array of size len *)
202211
}
203-
let get_msg_clipboard_data_window_id h = String.get_int32_be h 0
212+
let get_msg_clipboard_data_window_id h = get_int32_be h 0
204213
(* let set_msg_clipboard_data_window_id h v = Bytes.set_int32_be h 0 v *)
205-
let get_msg_clipboard_data_len h = String.get_int32_le h 4
214+
let get_msg_clipboard_data_len h = get_int32_le h 4
206215
(* let set_msg_clipboard_data_len h v = Bytes.set_int32_le h 4 v *)
207216
let sizeof_msg_clipboard_data = 8
208217

@@ -215,17 +224,17 @@ module GUI = struct
215224
parent : int32;
216225
override_redirect : int32;
217226
}
218-
let get_msg_create_x h = String.get_int32_le h 0
227+
let get_msg_create_x h = get_int32_le h 0
219228
(* let set_msg_create_x h v = Bytes.set_int32_le h 0 v *)
220-
let get_msg_create_y h = String.get_int32_le h 4
229+
let get_msg_create_y h = get_int32_le h 4
221230
(* let set_msg_create_y h v = Bytes.set_int32_le h 4 v *)
222-
let get_msg_create_width h = String.get_int32_le h 8
231+
let get_msg_create_width h = get_int32_le h 8
223232
(* let set_msg_create_width h v = Bytes.set_int32_le h 8 v *)
224-
let get_msg_create_height h = String.get_int32_le h 12
233+
let get_msg_create_height h = get_int32_le h 12
225234
(* let set_msg_create_height h v = Bytes.set_int32_le h 12 v *)
226-
let get_msg_create_parent h = String.get_int32_le h 16
235+
let get_msg_create_parent h = get_int32_le h 16
227236
(* let set_msg_create_parent h v = Bytes.set_int32_le h 16 v *)
228-
let get_msg_create_override_redirect h = String.get_int32_le h 20
237+
let get_msg_create_override_redirect h = get_int32_le h 20
229238
(* let set_msg_create_override_redirect h v = Bytes.set_int32_le h 20 v *)
230239
let sizeof_msg_create = 24
231240

@@ -249,15 +258,15 @@ module GUI = struct
249258
keycode : int32;
250259
}
251260
*)
252-
let get_msg_keypress_ty h = String.get_int32_le h 0
261+
let get_msg_keypress_ty h = get_int32_le h 0
253262
(* let set_msg_keypress_ty h v = Bytes.set_int32_le h 0 v *)
254-
let get_msg_keypress_x h = String.get_int32_le h 4
263+
let get_msg_keypress_x h = get_int32_le h 4
255264
(* let set_msg_keypress_x h v = Bytes.set_int32_le h 4 v *)
256-
let get_msg_keypress_y h = String.get_int32_le h 8
265+
let get_msg_keypress_y h = get_int32_le h 8
257266
(* let set_msg_keypress_y h v = Bytes.set_int32_le h 8 v *)
258-
let get_msg_keypress_state h = String.get_int32_le h 12
267+
let get_msg_keypress_state h = get_int32_le h 12
259268
(* let set_msg_keypress_state h v = Bytes.set_int32_le h 12 v *)
260-
let get_msg_keypress_keycode h = String.get_int32_le h 16
269+
let get_msg_keypress_keycode h = get_int32_le h 16
261270
(* let set_msg_keypress_keycode h v = Bytes.set_int32_le h 16 v *)
262271
let sizeof_msg_keypress = 20
263272

@@ -280,15 +289,15 @@ module GUI = struct
280289
button : int32; (* TODO *)
281290
}
282291
*)
283-
let get_msg_button_ty h = String.get_int32_le h 0
292+
let get_msg_button_ty h = get_int32_le h 0
284293
(* let set_msg_button_ty h v = Bytes.set_int32_le h 0 v *)
285-
let get_msg_button_x h = String.get_int32_le h 4
294+
let get_msg_button_x h = get_int32_le h 4
286295
(* let set_msg_button_x h v = Bytes.set_int32_le h 4 v *)
287-
let get_msg_button_y h = String.get_int32_le h 8
296+
let get_msg_button_y h = get_int32_le h 8
288297
(* let set_msg_button_y h v = Bytes.set_int32_le h 8 v *)
289-
let get_msg_button_state h = String.get_int32_le h 12
298+
let get_msg_button_state h = get_int32_le h 12
290299
(* let set_msg_button_state h v = Bytes.set_int32_le h 12 v *)
291-
let get_msg_button_button h = String.get_int32_le h 16
300+
let get_msg_button_button h = get_int32_le h 16
292301
(* let set_msg_button_button h v = Bytes.set_int32_le h 16 v *)
293302
let sizeof_msg_button = 20
294303

@@ -308,10 +317,10 @@ module GUI = struct
308317
is_hint : int;
309318
}
310319

311-
let get_msg_motion_x h = String.get_int32_le h 0
312-
let get_msg_motion_y h = String.get_int32_le h 4
313-
let get_msg_motion_state h = String.get_int32_le h 8
314-
let get_msg_motion_is_hint h = String.get_int32_le h 12
320+
let get_msg_motion_x h = get_int32_le h 0
321+
let get_msg_motion_y h = get_int32_le h 4
322+
let get_msg_motion_state h = get_int32_le h 8
323+
let get_msg_motion_is_hint h = get_int32_le h 12
315324
let sizeof_msg_motion = 16
316325

317326
let decode_msg_motion str : msg_motion_t option = (*TODO catch exceptions *)
@@ -346,19 +355,19 @@ module GUI = struct
346355
focus : int32;
347356
}
348357
*)
349-
let get_msg_crossing_ty h = String.get_int32_le h 0
358+
let get_msg_crossing_ty h = get_int32_le h 0
350359
(* let set_msg_crossing_ty h v = Bytes.set_int32_le h 0 v *)
351-
let get_msg_crossing_x h = String.get_int32_le h 4
360+
let get_msg_crossing_x h = get_int32_le h 4
352361
(* let set_msg_crossing_x h v = Bytes.set_int32_le h 4 v *)
353-
let get_msg_crossing_y h = String.get_int32_le h 8
362+
let get_msg_crossing_y h = get_int32_le h 8
354363
(* let set_msg_crossing_y h v = Bytes.set_int32_le h 8 v *)
355-
let get_msg_crossing_state h = String.get_int32_le h 12
364+
let get_msg_crossing_state h = get_int32_le h 12
356365
(* let set_msg_crossing_state h v = Bytes.set_int32_le h 12 v *)
357-
let get_msg_crossing_mode h = String.get_int32_le h 16
366+
let get_msg_crossing_mode h = get_int32_le h 16
358367
(* let set_msg_crossing_mode h v = Bytes.set_int32_le h 16 v *)
359-
let get_msg_crossing_detail h = String.get_int32_le h 20
368+
let get_msg_crossing_detail h = get_int32_le h 20
360369
(* let set_msg_crossing_detail h v = Bytes.set_int32_le h 20 v *)
361-
let get_msg_crossing_focus h = String.get_int32_le h 24
370+
let get_msg_crossing_focus h = get_int32_le h 24
362371
(* let set_msg_crossing_focus h v = Bytes.set_int32_le h 24 v *)
363372
let sizeof_msg_crossing = 28
364373

@@ -392,15 +401,15 @@ module GUI = struct
392401
override_redirect : int32;
393402
}
394403
*)
395-
let get_msg_configure_x h = String.get_int32_le h 0
404+
let get_msg_configure_x h = get_int32_le h 0
396405
(* let set_msg_configure_x h v = Bytes.set_int32_le h 0 v *)
397-
let get_msg_configure_y h = String.get_int32_le h 4
406+
let get_msg_configure_y h = get_int32_le h 4
398407
(* let set_msg_configure_y h v = Bytes.set_int32_le h 4 v *)
399-
let get_msg_configure_width h = String.get_int32_le h 8
408+
let get_msg_configure_width h = get_int32_le h 8
400409
(* let set_msg_configure_width h v = Bytes.set_int32_le h 8 v *)
401-
let get_msg_configure_height h = String.get_int32_le h 12
410+
let get_msg_configure_height h = get_int32_le h 12
402411
(* let set_msg_configure_height h v = Bytes.set_int32_le h 12 v *)
403-
let get_msg_configure_override_redirect h = String.get_int32_le h 16
412+
let get_msg_configure_override_redirect h = get_int32_le h 16
404413
(* let set_msg_configure_override_redirect h v = Bytes.set_int32_le h 16 v *)
405414
let sizeof_msg_configure = 20
406415

@@ -419,13 +428,13 @@ module GUI = struct
419428
width : int32;
420429
height: int32;
421430
}
422-
let get_msg_shmimage_x h = String.get_int32_le h 0
431+
let get_msg_shmimage_x h = get_int32_le h 0
423432
(* let set_msg_shmimage_x h v = Bytes.set_int32_le h 0 v *)
424-
let get_msg_shmimage_y h = String.get_int32_le h 4
433+
let get_msg_shmimage_y h = get_int32_le h 4
425434
(* let set_msg_shmimage_y h v = Bytes.set_int32_le h 4 v *)
426-
let get_msg_shmimage_width h = String.get_int32_le h 8
435+
let get_msg_shmimage_width h = get_int32_le h 8
427436
(* let set_msg_shmimage_width h v = Bytes.set_int32_le h 8 v *)
428-
let get_msg_shmimage_height h = String.get_int32_le h 12
437+
let get_msg_shmimage_height h = get_int32_le h 12
429438
(* let set_msg_shmimage_height h v = Bytes.set_int32_le h 12 v *)
430439
let sizeof_msg_shmimage = 16
431440

@@ -441,11 +450,11 @@ module GUI = struct
441450
mode : int32;
442451
detail : int32;
443452
}
444-
let get_msg_focus_ty h = String.get_int32_le h 0
453+
let get_msg_focus_ty h = get_int32_le h 0
445454
(* let set_msg_focus_ty h v = Bytes.set_int32_le h 0 v *)
446-
let get_msg_focus_mode h = String.get_int32_le h 4
455+
let get_msg_focus_mode h = get_int32_le h 4
447456
(* let set_msg_focus_mode h v = Bytes.set_int32_le h 4 v *)
448-
let get_msg_focus_detail h = String.get_int32_le h 8
457+
let get_msg_focus_detail h = get_int32_le h 8
449458
(* let set_msg_focus_detail h v = Bytes.set_int32_le h 8 v *)
450459
let sizeof_msg_focus = 12
451460

@@ -467,13 +476,13 @@ module GUI = struct
467476
linear frame buffer. This entry is not used by many drivers, and it should
468477
only be specified if the driver-specific documentation recommends it. *)
469478
}
470-
let get_xconf_w h = String.get_int32_le h 0
479+
let get_xconf_w h = get_int32_le h 0
471480
(* let set_xconf_w h v = Bytes.set_int32_le h 0 v *)
472-
let get_xconf_h h = String.get_int32_le h 4
481+
let get_xconf_h h = get_int32_le h 4
473482
(* let set_xconf_h h v = Bytes.set_int32_le h 4 v *)
474-
let get_xconf_depth h = String.get_int32_le h 8
483+
let get_xconf_depth h = get_int32_le h 8
475484
(* let set_xconf_depth h v = Bytes.set_int32_le h 8 v *)
476-
let get_xconf_mem h = String.get_int32_le h 12
485+
let get_xconf_mem h = get_int32_le h 12
477486
(* let set_xconf_mem h v = Bytes.set_int32_le h 12 v *)
478487
let sizeof_xconf = 16
479488

@@ -695,7 +704,7 @@ http://ccrc.web.nthu.edu.tw/ezfiles/16/1016/img/598/v14n_xen.pdf
695704

696705
let make_with_header ~window ~ty ~body_len body =
697706
(** see qubes-gui-agent-linux/include/txrx.h:#define write_message *)
698-
String.concat String.empty [
707+
String.concat "" [
699708
of_int32_le (msg_type_to_int ty) ;
700709
of_int32_le window ;
701710
of_int32_le body_len ;
@@ -710,7 +719,7 @@ http://ccrc.web.nthu.edu.tw/ezfiles/16/1016/img/598/v14n_xen.pdf
710719
+ (XC_PAGE_SIZE-1)) / XC_PAGE_SIZE; *)
711720
let cmds = mfns |> List.mapi (fun i -> fun _ ->
712721
of_int32_le (Int32.of_int (sizeof_shm_cmd + i*4))) in
713-
let body = String.concat String.empty @@ List.append [
722+
let body = String.concat "" @@ List.append [
714723
of_int32_le width ;
715724
of_int32_le height ;
716725
of_int32_le 24l ; (* bits per pixel *)
@@ -726,7 +735,7 @@ http://ccrc.web.nthu.edu.tw/ezfiles/16/1016/img/598/v14n_xen.pdf
726735
make_with_header ~window ~ty:MSG_MFNDUMP ~body_len body
727736

728737
let make_msg_shmimage ~window ~x ~y ~width ~height =
729-
let body = String.concat String.empty [
738+
let body = String.concat "" [
730739
of_int32_le x ;
731740
of_int32_le y ;
732741
of_int32_le width ;
@@ -736,7 +745,7 @@ http://ccrc.web.nthu.edu.tw/ezfiles/16/1016/img/598/v14n_xen.pdf
736745
make_with_header ~window ~ty:MSG_SHMIMAGE ~body_len body
737746

738747
let make_msg_create ~window ~width ~height ~x ~y ~override_redirect ~parent =
739-
let body = String.concat String.empty [
748+
let body = String.concat "" [
740749
of_int32_le width ;
741750
of_int32_le height ;
742751
of_int32_le x ;
@@ -748,23 +757,23 @@ http://ccrc.web.nthu.edu.tw/ezfiles/16/1016/img/598/v14n_xen.pdf
748757
make_with_header ~window ~ty:MSG_CREATE ~body_len body
749758

750759
let make_msg_map_info ~window ~override_redirect ~transient_for =
751-
let body = String.concat String.empty [
752-
of_int32_le override_redirect ;
753-
of_int32_le transient_for ;
754-
] in
760+
let body =
761+
of_int32_le override_redirect ^
762+
of_int32_le transient_for
763+
in
755764
let body_len = Int32.of_int sizeof_msg_map_info in
756765
make_with_header ~window ~ty:MSG_MAP ~body_len body
757766

758767
let make_msg_wmname ~window ~wmname =
759-
let body = String.concat String.empty [
760-
wmname ;
768+
let body =
769+
wmname ^
761770
String.make (sizeof_msg_wmname-String.(length wmname)) '\000' ; (* padding to sizeof_msg_wmname *)
762-
] in
771+
in
763772
let body_len = Int32.of_int sizeof_msg_wmname in
764773
make_with_header ~window ~ty:MSG_WMNAME ~body_len body
765774

766775
let make_msg_window_hints ~window ~width ~height =
767-
let body = String.concat String.empty [
776+
let body = String.concat "" [
768777
of_int32_le @@ Int32.(16 lor 32 |> of_int) ;
769778
(*^-- PMinSize | PMaxSize *)
770779
of_int32_le width ; (* min width *)
@@ -776,7 +785,7 @@ http://ccrc.web.nthu.edu.tw/ezfiles/16/1016/img/598/v14n_xen.pdf
776785
make_with_header ~window ~ty:MSG_WINDOW_HINTS ~body_len body
777786

778787
let make_msg_configure ~window ~x ~y ~width ~height =
779-
let body = String.concat String.empty [
788+
let body = String.concat "" [
780789
of_int32_le x ;
781790
of_int32_le y ; (* x and y are from qs->window_x and window_y*)
782791
of_int32_le width ;
@@ -867,18 +876,18 @@ module QubesDB = struct
867876
data_len : int32;
868877
(* rest of message is data *)
869878
}
870-
let get_msg_header_ty h = String.get_uint8 h 0
879+
let get_msg_header_ty h = get_uint8 h 0
871880
(* let set_msg_header_ty h v = Bytes.set_uint8 h 0 v *)
872881
let get_msg_header_path h = String.sub h 1 64
873882
(* let set_msg_header_path h v = Bytes.blit_string v 0 h 1 (min (String.length v) 64) *)
874-
let get_msg_header_data_len h = String.get_int32_le h 68
883+
let get_msg_header_data_len h = get_int32_le h 68
875884
(* let set_msg_header_data_len h v = Bytes.set_int32_le h 68 v *)
876885
let sizeof_msg_header = 72
877886

878887

879888
let make_msg_header ~ty ~path ~data_len =
880889
assert(String.length path <= 64);
881-
String.concat String.empty [
890+
String.concat "" [
882891
String.make 1 (Char.chr (qdb_msg_to_int ty)) ; (* int8 *)
883892
path ;
884893
String.make (3+64-String.length path) '\000' ; (* padding=3 and max size of path=64 *)
@@ -944,10 +953,7 @@ module Rpc_filecopy = struct
944953
(*
945954
let make_result_header_ext last_filename =
946955
let namelen = Bytes.length last_filename in
947-
String.concat String.empty [
948-
of_int32_le @@ (Int32.of_int namelen) ;
949-
last_filename ;
950-
]
956+
of_int32_le @@ (Int32.of_int namelen) ^ last_filename
951957
*)
952958

953959
end

lib/gUI.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ let rec listen t () =
201201
(Ohex.pp ()) msg_buf) ;
202202
UNIT()
203203
| Some MSG_MAP ->
204-
Log.warn (fun f -> f "Event: MAP: %s" (Ohex.encode msg_buf));
204+
Log.warn (fun f -> f "Event: MAP: %a" (Ohex.pp ()) msg_buf);
205205
UNIT()
206206
| Some MSG_KEYPRESS -> decode_KEYPRESS msg_buf
207207
| Some MSG_FOCUS -> decode_FOCUS msg_buf

0 commit comments

Comments
 (0)