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

[v9] backport 10915 (memory leak) #10927

Merged
merged 3 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/srv/desktop/rdp/rdpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,17 +382,23 @@ func (c *Client) start() {
}

//export handle_bitmap
func handle_bitmap(handle C.uintptr_t, cb C.CGOBitmap) C.CGOError {
func handle_bitmap(handle C.uintptr_t, cb *C.CGOBitmap) C.CGOError {
return cgo.Handle(handle).Value().(*Client).handleBitmap(cb)
}

func (c *Client) handleBitmap(cb C.CGOBitmap) C.CGOError {
func (c *Client) handleBitmap(cb *C.CGOBitmap) C.CGOError {
// Notify the input forwarding goroutine that we're ready for input.
// Input can only be sent after connection was established, which we infer
// from the fact that a bitmap was sent.
atomic.StoreUint32(&c.readyForInput, 1)

data := C.GoBytes(unsafe.Pointer(cb.data_ptr), C.int(cb.data_len))
// use unsafe.Slice here instead of C.GoBytes, because unsafe.Slice
// creates a Go slice backed by data managed from Rust - it does not
// copy. This way we only need one copy into img.Pix below.
ptr := unsafe.Pointer(cb.data_ptr)
uptr := (*uint8)(ptr)
data := unsafe.Slice(uptr, C.int(cb.data_len))

// Convert BGRA to RGBA. It's likely due to Windows using uint32 values for
// pixels (ARGB) and encoding them as big endian. The image.RGBA type uses
// a byte slice with 4-byte segments representing pixels (RGBA).
Expand Down
2 changes: 1 addition & 1 deletion lib/srv/desktop/rdp/rdpclient/librdprs.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,6 @@ void free_rust_string(char *s);

extern void free_go_string(char *s);

extern CGOError handle_bitmap(uintptr_t client_ref, struct CGOBitmap b);
extern CGOError handle_bitmap(uintptr_t client_ref, struct CGOBitmap *b);

extern CGOError handle_remote_copy(uintptr_t client_ref, uint8_t *data, uint32_t len);
7 changes: 3 additions & 4 deletions lib/srv/desktop/rdp/rdpclient/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ fn read_rdp_output_inner(client: &Client) -> Option<String> {
.unwrap()
.read(|rdp_event| match rdp_event {
RdpEvent::Bitmap(bitmap) => {
let cbitmap = match CGOBitmap::try_from(bitmap) {
let mut cbitmap = match CGOBitmap::try_from(bitmap) {
Ok(cb) => cb,
Err(e) => {
error!(
Expand All @@ -469,7 +469,7 @@ fn read_rdp_output_inner(client: &Client) -> Option<String> {
}
};
unsafe {
err = handle_bitmap(client_ref, cbitmap) as CGOError;
err = handle_bitmap(client_ref, &mut cbitmap) as CGOError;
};
}
// These should never really be sent by the server to us.
Expand Down Expand Up @@ -696,8 +696,7 @@ unsafe fn from_cgo_error(e: CGOError) -> String {
// comments.
extern "C" {
fn free_go_string(s: *mut c_char);
fn handle_bitmap(client_ref: usize, b: CGOBitmap) -> CGOError;

fn handle_bitmap(client_ref: usize, b: *mut CGOBitmap) -> CGOError;
fn handle_remote_copy(client_ref: usize, data: *mut u8, len: u32) -> CGOError;
}

Expand Down