Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
martinpitt committed May 6, 2024
1 parent 9ebb6ec commit 6518f46
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 9 deletions.
87 changes: 84 additions & 3 deletions src/umockdev-ioctl.vala
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,87 @@ public class IoctlData : GLib.Object {
Posix.memcpy(&data[offset], new_data, new_data.length);
}

/**
* umockdev_ioctl_get_int:
* @self: A #UMockdevIoctlData
*
* Return data as integer. This avoids problems with unaligned memory.
*/
public int get_int() {
int value = 0; // unnecessary initialization, avoids "Use of possibly unassigned local variable"
Posix.memcpy(&value, data, sizeof(int));
return value;
}

/**
* umockdev_ioctl_set_int:
* @self: A #UMockdevIoctlData
* @value: Value to set
*
* Set data to given value. This avoids problems with unaligned memory.
*/
public void set_int(int value) {
Posix.memcpy(data, &value, sizeof(int));
}

/**
* umockdev_ioctl_set_uint32:
* @self: A #UMockdevIoctlData
* @value: Value to set
*
* Set data to given value. This avoids problems with unaligned memory.
*/
public void set_uint32(uint32 value) {
Posix.memcpy(data, &value, sizeof(uint32));
}

/**
* umockdev_ioctl_get_ulong:
* @self: A #UMockdevIoctlData
*
* Return data as ulong. This avoids problems with unaligned memory.
*/
public ulong get_ulong() {
ulong value = 0; // unnecessary initialization, avoids "Use of possibly unassigned local variable"
Posix.memcpy(&value, data, sizeof(ulong));
return value;
}

/**
* umockdev_ioctl_set_ulong:
* @self: A #UMockdevIoctlData
* @value: Value to set
*
* Set data to given value. This avoids problems with unaligned memory.
*/
public void set_ulong(ulong value) {
Posix.memcpy(data, &value, sizeof(ulong));
}

/**
* umockdev_ioctl_get_long:
* @self: A #UMockdevIoctlData
*
* Return data as long. This avoids problems with unaligned memory.
*/
public long get_long() {
long value = 0; // unnecessary initialization, avoids "Use of possibly unassigned local variable"
Posix.memcpy(&value, data, sizeof(long));
return value;
}

/**
* umockdev_ioctl_get_pointer:
* @self: A #UMockdevIoctlData
*
* Return data as pointer. This avoids problems with unaligned memory.
*/
public void* get_pointer() {
void* value = null; // unnecessary initialization, avoids "Use of possibly unassigned local variable"
Posix.memcpy(&value, data, sizeof(void*));
return value;
}

/**
* umockdev_ioctl_retrieve:
* @self: A #UMockdevIoctlData
Expand Down Expand Up @@ -934,7 +1015,7 @@ internal class IoctlTreeHandler : IoctlBase {
} else {
Posix.errno = Posix.ENOTTY;
}
last = tree.execute(last, request, *(void**) client.arg.data, ref ret);
last = tree.execute(last, request, client.arg.get_pointer(), ref ret);
my_errno = Posix.errno;
Posix.errno = 0;
if (last != null)
Expand All @@ -955,7 +1036,7 @@ internal class IoctlTreeHandler : IoctlBase {
* This should only happen for REAPURB, but it does not hurt to
* just always check.
*/
if (*(void**) data.data == (void*) last_submit_urb.data) {
if (data.get_pointer() == (void*) last_submit_urb.data) {
data.set_ptr(0, last_submit_urb);

last_submit_urb = null;
Expand Down Expand Up @@ -1062,7 +1143,7 @@ internal class IoctlTreeRecorder : IoctlBase {
}

/* Record */
node = new IoctlTree.Tree.from_bin(request, *(void**) client.arg.data, ret);
node = new IoctlTree.Tree.from_bin(request, client.arg.get_pointer(), ret);
if (node != null) {
tree.insert((owned) node);
}
Expand Down
4 changes: 2 additions & 2 deletions src/umockdev-pcap.vala
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ internal class IoctlUsbPcapHandler : IoctlBase {

switch (request) {
case USBDEVFS_GET_CAPABILITIES:
*(uint32*) data.data = capabilities;
data.set_uint32(capabilities);

client.complete(0, 0);
return true;
Expand All @@ -108,7 +108,7 @@ internal class IoctlUsbPcapHandler : IoctlBase {

case USBDEVFS_DISCARDURB:
for (int i = 0; i < urbs.length; i++) {
if (urbs.index(i).urb_data.client_addr == *((ulong*)client.arg.data)) {
if (urbs.index(i).urb_data.client_addr == client.arg.get_ulong()) {
/* Found the urb, add to discard array, remove it and return success */
discarded.prepend_val(urbs.index(i));
urbs.remove_index(i);
Expand Down
6 changes: 2 additions & 4 deletions tests/test-umockdev-vala.vala
Original file line number Diff line number Diff line change
Expand Up @@ -1000,15 +1000,13 @@ static bool
ioctl_custom_handle_ioctl_cb(UMockdev.IoctlBase handler, UMockdev.IoctlClient client)
{
if (client.request == 1) {
client.complete(*(long*)client.arg.data, 0);
client.complete(client.arg.get_long(), 0);
} else if (client.request == 2) {
client.complete(-1, Posix.ENOMEM);
} else if (client.request == 3 ) {
try {
var data = client.arg.resolve(0, sizeof(int));

*(int*) data.data = (int) 0xc00fffee;

data.set_int((int) 0xc00fffee);
client.complete(0, 0);
} catch (Error e) {
error ("cannot resolve client arg: %s", e.message);
Expand Down

0 comments on commit 6518f46

Please sign in to comment.