Какова цель __mptr в последнем макросе container_of?

Я читаю ядро ​​Linux 5.17.5 и теперь смотрю на макрос container_of().

      /**
 * container_of - cast a member of a structure out to the containing structure
 * @ptr:    the pointer to the member.
 * @type:   the type of the container struct this is embedded in.
 * @member: the name of the member within the struct.
 *
 */
#define container_of(ptr, type, member) ({              \
    void *__mptr = (void *)(ptr);                   \
    static_assert(__same_type(*(ptr), ((type *)0)->member) ||   \
              __same_type(*(ptr), void),            \
              "pointer type mismatch in container_of()");   \
    ((type *)(__mptr - offsetof(type, member))); })

Мой вопрос очень прост: какова цель, могу ли я просто заменить __mptrпо (void *)ptrкак это?

      #define container_of(ptr, type, member) ({          \
    static_assert(__same_type(*(ptr), ((type *)0)->member) ||   \
              __same_type(*(ptr), void),            \
              "pointer type mismatch in container_of()");   \
    ((type *)((void*)ptr - offsetof(type, member))); })

1 ответ

Так как «kernel.h: лучше обрабатывать указатели на массивы в container_of()» , больше не служит для проверки типов. Он используется для устранения информативного сообщения, такого как inform(gimple_location(stmt), "found mismatched ssa struct pointer types: %qT and %qT\n", ptr_lhs_type, ptr_rhs_type);. Одна из задач этого файла:

      /*
 * iterate over all statements to find "bad" casts:
 * those where the address of the start of a structure is cast
 * to a pointer of a structure of a different type, or a
 * structure pointer type is cast to a different structure pointer type
 */

Если отсутствует, макрос будет включать код, как вы сказали:

(type *)((void*)ptr - offsetof(type, member)))

(ПС: char *здесь лучше, потому что стандарт iso c гарантирует 1 байт для , гарантируется только gnu c)

Если offsetofполучает ноль, а ptr будет содержать начальный адрес типа структуры member, то это будет совершенно другой тип: struct type *. Эта форма неисправна и будет обнаружена ею.

С введением void *__mptr = (void *)(ptr);, компилятор больше не знает тип, поэтому scripts/gcc-plugins/randomize_layout_plugin.cне будет жаловаться при литье void *к (type *)Вы можете увидеть реальный случай и связанный с ним исправленный патч по адресу https://lkml.org/lkml/2017/6/20/873 .


Ниже приведен исходный ответ для ядра до версии 4.13, когда он использовался для проверки типов:

Давайте упростим и посмотрим на этот случай:

      #include <stdio.h>
#include <stddef.h>
#define container_of(ptr, type, member) ({          \
    const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
    (type *)( (char *)__mptr - offsetof(type,member) );})
#define container_of_without_typechecking(ptr, type, member) ({         \
    (type *)( (char *)ptr - offsetof(type,member) );})
struct Foo
{
    int a;
};
int main(int argc, char *argv[]) {
    struct Foo foo;
    int *a;

    printf("foo addr: %p\n", &foo);
    a = &foo.a;
    printf("container of a: %p\n", container_of_without_typechecking((unsigned long long*)a, struct Foo, a));
    printf("typecheck: container of a: %p\n", container_of((unsigned long long*)a, struct Foo, a));
    return 0;
}

не имеет __mptrно делает.

При компиляции:

      a.c: In function ‘main’:
a.c:13:55: warning: initialization of ‘const int *’ from incompatible pointer type ‘long long unsigned int *’ [-Wincompatible-pointer-types]
   13 |         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
      |                                                       ^
a.c:28:47: note: in expansion of macro ‘container_of’
   28 |     printf("typecheck: container of a: %p\n", container_of((unsigned long long*)a, struct Foo, a));
      |                                               ^~~~~~~~~~~~

Как видите, бросает incompatible pointer typeпредупреждение во время container_of_without_typecheckingнет, так что это просто для проверки типа.

Также обратите внимание, что ядро ​​Linux расценивает это предупреждение как ошибку:

      KBUILD_CFLAGS   += $(call cc-option,-Werror=incompatible-pointer-types)

Таким образом, вы получите ошибку вместо предупреждения, если вы передадите неправильный тип в container_of.

Другие вопросы по тегам