Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

clang: support "quantum tunneling" #4372

Closed Quuxplusone closed 14 years ago

Quuxplusone commented 14 years ago
Bugzilla Link PR6937
Status RESOLVED DUPLICATE of bug 2930
Importance P normal
Reported by Török Edwin (edwin+bugs@etorok.eu)
Reported on 2010-04-25 05:17:34 -0700
Last modified on 2010-04-25 13:01:32 -0700
Version unspecified
Hardware PC Linux
CC efriedma@quicinc.com
Fixed by commit(s)
Attachments
Blocks PR4068
Blocked by
See also
The Linux kernel fails to build now, and there is no CONFIG_ to turn off this
"feature":
net/ipv4/netfilter/ip_tables.c:72:9: error: fields must have a constant size:
'variable length array in structure' extension
      will never be supported
        return xt_alloc_initial_table(ipt, IPT);
               ^
In file included from net/ipv4/netfilter/ip_tables.c:31:
net/ipv4/netfilter/../../netfilter/xt_repldata.h:14:26: note: instantiated from:
                struct type##_standard entries[nhooks]; \

Here is the header:
/*
 * Today's hack: quantum tunneling in structs
 *
 * 'entries' and 'term' are never anywhere referenced by word in code. In fact,
 * they serve as the hanging-off data accessed through repl.data[].
 */

#define xt_alloc_initial_table(type, typ2) ({ \
    unsigned int hook_mask = info->valid_hooks; \
    unsigned int nhooks = hweight32(hook_mask); \
    unsigned int bytes = 0, hooknum = 0, i = 0; \
    struct { \
        struct type##_replace repl; \
        struct type##_standard entries[nhooks]; \
        struct type##_error term; \
    } *tbl = kzalloc(sizeof(*tbl), GFP_KERNEL); \
    if (tbl == NULL) \
        return NULL; \

I don't think this is much different from VLAs, but instead of the array size
depending on function parameter, it depends on a local variable.
It also isn't much different from variable-sized structs, it could convert the
last 2 fields into [0xi8], and calculate the sizeof() at runtime.

Here is a workaround for the C code, clang could do this automatically:
diff --git a/net/netfilter/xt_repldata.h b/net/netfilter/xt_repldata.h
index 6efe4e5..e61924d 100644
--- a/net/netfilter/xt_repldata.h
+++ b/net/netfilter/xt_repldata.h
@@ -11,13 +11,16 @@
        unsigned int bytes = 0, hooknum = 0, i = 0; \
        struct { \
                struct type##_replace repl; \
-               struct type##_standard entries[nhooks]; \
-               struct type##_error term; \
-       } *tbl = kzalloc(sizeof(*tbl), GFP_KERNEL); \
+               char rest_of_data[];\
+       } *tbl = kzalloc(sizeof(struct type##_replace)+\
+                        nhooks*sizeof(struct type##_standard)+\
+                        sizeof(struct type##_error), GFP_KERNEL);\
        if (tbl == NULL) \
                return NULL; \
+       struct type##_standard *entries = (struct type##_standard*)&tbl-
>rest_of_data;\
+       struct type##_error *term = (struct type##_error*)((char*)&tbl-
>rest_of_data + nhooks*sizeof(struct type##_standard));
        strncpy(tbl->repl.name, info->name, sizeof(tbl->repl.name)); \
-       tbl->term = (struct type##_error)typ2##_ERROR_INIT;  \
+       *term = (struct type##_error)typ2##_ERROR_INIT;  \
        tbl->repl.valid_hooks = hook_mask; \
        tbl->repl.num_entries = nhooks + 1; \
        tbl->repl.size = nhooks * sizeof(struct type##_standard) + \
@@ -27,7 +30,7 @@
                        continue; \
                tbl->repl.hook_entry[hooknum] = bytes; \
                tbl->repl.underflow[hooknum]  = bytes; \
-               tbl->entries[i++] = (struct type##_standard) \
+               entries[i++] = (struct type##_standard) \
                        typ2##_STANDARD_INIT(NF_ACCEPT); \
                bytes += sizeof(struct type##_standard); \
        } \
Quuxplusone commented 14 years ago

_This bug has been marked as a duplicate of bug 2930_