Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Possible data corruption in User_buffer #486

Closed
ansiwen opened this issue Apr 29, 2022 · 3 comments
Closed

Possible data corruption in User_buffer #486

ansiwen opened this issue Apr 29, 2022 · 3 comments

Comments

@ansiwen
Copy link

ansiwen commented Apr 29, 2022

The write function in User_buffer possibly copies Cstruct values into a list and returns:

let write t datav =
let l = lenv datav in
let mss = Int32.of_int (Window.tx_mss t.wnd) in
match Lwt_dllist.is_empty t.buffer &&
(l = mss || not (Window.tx_inflight t.wnd)) with
| false ->
t.bufbytes <- Int32.add t.bufbytes l;
List.iter (fun data -> ignore(Lwt_dllist.add_r data t.buffer)) datav;
if t.bufbytes < mss then
Lwt.return_unit
else
clear_buffer t
| true ->
let avail_len = available_cwnd t in
match avail_len < l with
| true ->
t.bufbytes <- Int32.add t.bufbytes l;
List.iter (fun data -> ignore(Lwt_dllist.add_r data t.buffer)) datav;
Lwt.return_unit
| false ->
let max_size = Window.tx_mss t.wnd in
transmit_segments ~mss:max_size ~txq:t.txq datav

which basically means it takes ownership, because copies of Cstruct share the data and it's not transparent when they will be flushed. This leads to problems, when code assumes otherwise and reuses the buffers after write returns, like the Faraday library. This leads to issues like this: anmonteiro/gluten#29

I open this issue to clarify the contract conventions (if there are any) for the write function. Is the buffer ownership transferred, or only "borrowed" until write returns?

@haesbaert
Copy link
Member

haesbaert commented Jun 10, 2022

I'm not sure the authors considered this much, but as you can see only the very last case (transmit_segments) is not a clear offender. We should at least document it.

@haesbaert
Copy link
Member

@ansiwen
Copy link
Author

ansiwen commented Jul 2, 2022

Ok, so ownership is transferred. Thanks. So I will close this.

@ansiwen ansiwen closed this as completed Jul 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants