From d17aed8e8266398113b50e7a7cc6abbd46d205fb Mon Sep 17 00:00:00 2001 From: Nick Brassel Date: Tue, 19 Apr 2022 12:33:56 +1000 Subject: [PATCH] Verify struct sizing at build time. --- data/xap/xap_0.0.1.hjson | 2 ++ data/xap/xap_0.1.0.hjson | 2 ++ data/xap/xap_0.2.0.hjson | 2 ++ lib/python/qmk/xap/gen_firmware/header_generator.py | 9 +++++++++ 4 files changed, 15 insertions(+) diff --git a/data/xap/xap_0.0.1.hjson b/data/xap/xap_0.0.1.hjson index b8a479f01b9..382075b9415 100755 --- a/data/xap/xap_0.0.1.hjson +++ b/data/xap/xap_0.0.1.hjson @@ -142,6 +142,7 @@ name: Request Header description: Packet format for inbound data. type: struct + struct_length: 3 struct_members: [ { type: token @@ -158,6 +159,7 @@ name: Response Header description: Packet format for inbound data. type: struct + struct_length: 4 struct_members: [ { type: token diff --git a/data/xap/xap_0.1.0.hjson b/data/xap/xap_0.1.0.hjson index 7ad69aafba4..9499314d4bf 100755 --- a/data/xap/xap_0.1.0.hjson +++ b/data/xap/xap_0.1.0.hjson @@ -83,6 +83,7 @@ name: Broadcast Header description: Packet format for broadcast messages. type: struct + struct_length: 4 struct_members: [ { type: token @@ -240,6 +241,7 @@ Retrieves the set of identifying information for the board. ''' return_type: struct + return_struct_length: 10 return_struct_members: [ { type: u16 diff --git a/data/xap/xap_0.2.0.hjson b/data/xap/xap_0.2.0.hjson index f2fce07b844..2cb71035789 100755 --- a/data/xap/xap_0.2.0.hjson +++ b/data/xap/xap_0.2.0.hjson @@ -38,6 +38,7 @@ define: GET_KEYMAP_KEYCODE description: TODO request_type: struct + request_struct_length: 3 request_struct_members: [ { type: u8 @@ -61,6 +62,7 @@ define: SET_KEYMAP_KEYCODE description: TODO request_type: struct + request_struct_length: 5 request_struct_members: [ { type: u8 diff --git a/lib/python/qmk/xap/gen_firmware/header_generator.py b/lib/python/qmk/xap/gen_firmware/header_generator.py index 52b1bc29cad..7ce1d9d84ed 100755 --- a/lib/python/qmk/xap/gen_firmware/header_generator.py +++ b/lib/python/qmk/xap/gen_firmware/header_generator.py @@ -133,6 +133,9 @@ def _append_route_types(lines, container, container_id=None, route_stack=None): lines.append(f' {member_type} {member_name};') lines.append(f'}} __attribute__((__packed__)) {route_name}_arg_t;') + req_len = container['request_struct_length'] + lines.append(f'_Static_assert(sizeof({route_name}_arg_t) == {req_len}, "{route_name}_arg_t needs to be {req_len} bytes in size");') + elif 'request_type' in container: request_type = container['request_type'] found = re.search(r'(u\d+)\[(\d+)\]', request_type) @@ -153,6 +156,9 @@ def _append_route_types(lines, container, container_id=None, route_stack=None): lines.append(f' {member_type} {member_name};') lines.append(f'}} __attribute__((__packed__)) {route_name}_t;') + req_len = container['return_struct_length'] + lines.append(f'_Static_assert(sizeof({route_name}_t) == {req_len}, "{route_name}_t needs to be {req_len} bytes in size");') + elif 'return_type' in container: return_type = container['return_type'] found = re.search(r'(u\d+)\[(\d+)\]', return_type) @@ -213,6 +219,9 @@ def _append_internal_types(lines, container): member_type = additional_types[member["type"]] lines.append(f' {member_type} {member_name};') lines.append(f'}} __attribute__((__packed__)) xap_{key}_t;') + + req_len = value['struct_length'] + lines.append(f'_Static_assert(sizeof(xap_{key}_t) == {req_len}, "xap_{key}_t needs to be {req_len} bytes in size");') else: lines.append(f'typedef {data_type} xap_{key}_t;')