Skip to content

ctest: add tests for field size, offset, and field ptr #4561

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mbyx
Copy link
Contributor

@mbyx mbyx commented Jul 23, 2025

Description

Adds tests for checking if the size and offset of a field is the same in Rust and C, as well as the pointer to that field.

Sources

Checklist

  • Relevant tests in libc-test/semver have been updated
  • No placeholder or unstable values like *LAST or *MAX are
    included (see #3131)
  • Tested locally (cd libc-test && cargo test --target mytarget);
    especially relevant for platforms that may not be checked in CI

@rustbot rustbot added ctest Issues relating to the ctest crate S-waiting-on-review labels Jul 23, 2025
@mbyx mbyx force-pushed the ctest-field-test branch from ce1a907 to 9848a68 Compare July 23, 2025 13:34
Comment on lines 377 to 394
#[derive(Clone, Debug)]
pub(crate) struct TestFieldPtr {
pub test_name: BoxStr,
pub id: BoxStr,
pub field: Field,
pub c_field: BoxStr,
pub volatile_keyword: BoxStr,
pub c_signature: BoxStr,
}

#[derive(Clone, Debug)]
pub(crate) struct TestFieldSizeOffset {
pub test_name: BoxStr,
pub id: BoxStr,
pub field: Field,
pub c_field: BoxStr,
pub c_ty: BoxStr,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, I think these should go after TestConst since they're used later in the templates. Same for the new field_* functions.

{{ item.volatile_keyword }}{{ item.c_signature }} {
return &b->{{ item.c_field }};
}
{%- endfor +%}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing \n

Comment on lines 76 to 77
// The name of the function remains `ctest_field_ptr__{{ item.id }}__{{ item.field.ident() }}`
{{ item.volatile_keyword }}{{ item.c_signature }} {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the args and return type be split to separate fields so this is directly visible rather than a comment? So

{{ item.volatile_keyword }} {{ item.c_ret_ty }} ctest_field_ptr__{{ item.id }}__{{ item.field.ident() }}({{ item.c_args }})

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see the problem, function pointer syntax is horrid.

I'd suggest eliminating this with a typedef when you're dealing with function pointers:

typedef ret_ty (*some_name)(argty1, argty2);
some_name ctest_field_ptr__{{ item.id }}__{{ item.field.ident() }}(...) { /* ... */ }

some_name can be something like ctest_field_ty__{{ item.id }}__{{ item.field.ident() }}

check_same(field_ptr as *mut _,
ctest_field_ptr__{{ item.id }}__{{ item.field.ident() }}(ty_ptr),
"field type {{ item.field.ident() }} of {{ item.id }}");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block can be split a bit so we can see what unsafe is handling. Something like:

  1. let field_ptr = &raw const ((*ty_ptr).{{ item.field.ident() }}) should get its own unsafe block that says it doesn't actually read ty_ptr (AIUI requiring unsafe for this op is a limitation of the unsafe check, can't cause UB)
  2. Getting the size and offset from C should each get their own unsafe block assigned to a variable
  3. The read_unaligned function should get its own block
  4. The rest should be able to exist outside of unsafe

fn ctest_field_ptr__{{ item.id }}__{{ item.field.ident() }}(a: *const {{ item.id }}) -> *mut u8;
}

unsafe {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the above, everything except let field_ptr = ... and let c_field_ptr = ctest_field_ptr__...(...) doesn't need to be in unsafe

Comment on lines 16 to 17
#[expect(unused_imports)]
use std::mem::{MaybeUninit, offset_of};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be allow rather than expect? Because when there are field tests, it's not actually unused.

@mbyx mbyx force-pushed the ctest-field-test branch from 9848a68 to 46f0c4e Compare July 24, 2025 05:39
@mbyx mbyx requested a review from tgross35 July 24, 2025 05:43
Copy link
Contributor

@tgross35 tgross35 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With these two small changes, the size+offset changes look good to me. I do have some more feedback for the field pointer tests though; maybe want to split that off to another PR so the rest of this can merge now?

Comment on lines 198 to 239
for struct_ in helper.ffi_items.structs() {
for field in &struct_.fields {
let should_skip_field_tests = helper
.generator
.skips
.iter()
.any(|f| f(&MapInput::StructField(struct_, field)));

if should_skip_field_tests {
continue;
}

let item = TestFieldSizeOffset {
test_name: field_size_offset_test_ident(struct_.ident(), field.ident()),
id: struct_.ident().into(),
c_ty: helper.c_type(struct_)?.into(),
field: field.clone(),
c_field: helper
.c_ident(MapInput::StructField(struct_, field))
.into_boxed_str(),
};
self.field_size_offset_tests.push(item.clone());
self.test_idents.push(item.test_name);
}
}

for union_ in helper.ffi_items.unions() {
for field in &union_.fields {
let should_skip_field_tests = helper
.generator
.skips
.iter()
.any(|f| f(&MapInput::UnionField(union_, field)));

if should_skip_field_tests {
continue;
}

let item = TestFieldSizeOffset {
test_name: field_size_offset_test_ident(union_.ident(), field.ident()),
id: union_.ident().into(),
c_ty: helper.c_type(union_)?.into(),
field: field.clone(),
c_field: helper
.c_ident(MapInput::UnionField(union_, field))
.into_boxed_str(),
};
self.field_size_offset_tests.push(item.clone());
self.test_idents.push(item.test_name);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since structs and unions use the same field types, I think this could be collapsed to a single iterator. Something like:

let should_skip = |map_input| helper
    .generator
    .skips
    .iter()
    .any(|f| f(map_input));

let struct_fields = helper.ffi_items.structs().iter()
    .flat_map(|st| st.fields().map(|field| (st, field))
    .filter(|(st, field)| should_skip(&MapInput::StructField(st, field))
    .map(|(st, field)| field);
let union_fields = helper.ffi_items.unions().iter()
    .flat_map(|un| un.fields().map(|field| (un, field))
    .filter(|(un, field)| should_skip(&MapInput::UnionField(un, field))
    .map(|(un, field)| field);

for field in struct_fields.chain(union_fields) {
    // ...
}

Something similar for populate_field_ptr_tests

Comment on lines 156 to 167
let ty_ptr = unsafe { &raw const (*uninit_ty).{{ item.field.ident() }} };
let val = unsafe { ty_ptr.read_unaligned() };

let ctest_field_offset = unsafe { ctest_offset_of__{{ item.id }}__{{ item.field.ident() }}() };
check_same(offset_of!({{ item.id }}, {{ item.field.ident() }}) as u64, ctest_field_offset,
"field offset {{ item.field.ident() }} of {{ item.id }}");

let ctest_field_size = unsafe { ctest_size_of__{{ item.id }}__{{ item.field.ident() }}() };
Copy link
Contributor

@tgross35 tgross35 Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Safety comments

Suggested change
let ty_ptr = unsafe { &raw const (*uninit_ty).{{ item.field.ident() }} };
let val = unsafe { ty_ptr.read_unaligned() };
let ctest_field_offset = unsafe { ctest_offset_of__{{ item.id }}__{{ item.field.ident() }}() };
check_same(offset_of!({{ item.id }}, {{ item.field.ident() }}) as u64, ctest_field_offset,
"field offset {{ item.field.ident() }} of {{ item.id }}");
let ctest_field_size = unsafe { ctest_size_of__{{ item.id }}__{{ item.field.ident() }}() };
// SAFETY: we assume the field access doesn't wrap
let ty_ptr = unsafe { &raw const (*uninit_ty).{{ item.field.ident() }} };
// SAFETY: we assume that all zeros is a valid bitpattern for `ty_ptr`, otherwise the
// test should be skipped.
let val = unsafe { ty_ptr.read_unaligned() };
// SAFETY: FFI call with no preconditions
let ctest_field_offset = unsafe { ctest_offset_of__{{ item.id }}__{{ item.field.ident() }}() };
check_same(offset_of!({{ item.id }}, {{ item.field.ident() }}) as u64, ctest_field_offset,
"field offset {{ item.field.ident() }} of {{ item.id }}");
// SAFETY: FFI call with no preconditions
let ctest_field_size = unsafe { ctest_size_of__{{ item.id }}__{{ item.field.ident() }}() };

@mbyx mbyx force-pushed the ctest-field-test branch from 46f0c4e to b3008cb Compare July 24, 2025 07:10
@mbyx mbyx force-pushed the ctest-field-test branch from b3008cb to 8996f9b Compare July 24, 2025 07:12
@mbyx
Copy link
Contributor Author

mbyx commented Jul 24, 2025

It's fine to review the field ptr checks now, splitting it would be harder.

@mbyx mbyx requested a review from tgross35 July 24, 2025 07:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ctest Issues relating to the ctest crate S-waiting-on-review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants