public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/21173] New: miscompiled pointer subtraction broke Linux kernel
@ 2005-04-23  9:23 mikpe at csd dot uu dot se
  2005-04-23 11:56 ` [Bug c/21173] " belyshev at depni dot sinp dot msu dot ru
                   ` (32 more replies)
  0 siblings, 33 replies; 34+ messages in thread
From: mikpe at csd dot uu dot se @ 2005-04-23  9:23 UTC (permalink / raw)
  To: gcc-bugs

/* gcc4pointersubtractionbug.c
 * Written by Mikael Pettersson, mikpe@csd.uu.se, 2005-04-23.
 *
 * This program illustrates a code optimisation bug in
 * gcc-4.0.0 (final) and gcc-4.0.0-20050417, where a pointer
 * subtraction operation is compiled as a pointer addition.
 * Observed at -O2. gcc was configured for i686-pc-linux-gnu.
 *
 * This bug broke net/ipv4/devinet.c:devinet_sysctl_register()
 * in the linux-2.6.12-rc2 Linux kernel, causing /sbin/sysctl
 * to trigger kernel oopses.
 *
 * gcc-4.0.0-20050416 and earlier prereleases do not have this bug.
 */
#include <stdio.h>
#include <string.h>

#define NRVARS  5

struct ipv4_devconf {
    int var[NRVARS];
};
struct ipv4_devconf ipv4_devconf[2];

struct ctl_table {
    void *data;
};

struct devinet_sysctl_table {
    struct ctl_table devinet_vars[NRVARS];
};

void devinet_sysctl_relocate(struct devinet_sysctl_table *t,
                             struct ipv4_devconf *p)
{
    int i;

    for (i = 0; i < NRVARS; i++)
        /* Initially data points to a field in ipv4_devconf[0].
           This code relocates it to the corresponding field in *p.
           At -O2, gcc-4.0.0-20050417 and gcc-4.0.0 (final)
           miscompile this pointer subtraction as a pointer addition. */
        t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf[0];
}

struct devinet_sysctl_table devinet_sysctl;

int main(void)
{
    struct devinet_sysctl_table t;
    int i;

    for(i = 0; i < NRVARS; i++)
        devinet_sysctl.devinet_vars[i].data = &ipv4_devconf[0].var[i];

    memcpy(&t, &devinet_sysctl, sizeof t);
    devinet_sysctl_relocate(&t, &ipv4_devconf[1]);

    for(i = 0; i < NRVARS; i++)
        if (t.devinet_vars[i].data != &ipv4_devconf[1].var[i]) {
            fprintf(stderr, "t.devinet_vars[%u].data == %p, should be %p\n",
                    i,
                    t.devinet_vars[i].data,
                    &ipv4_devconf[1].var[i]);
            return 1;
        }

    printf("all ok\n");
    return 0;
}

-- 
           Summary: miscompiled pointer subtraction broke Linux kernel
           Product: gcc
           Version: 4.0.0
            Status: UNCONFIRMED
          Severity: critical
          Priority: P2
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: mikpe at csd dot uu dot se
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21173


^ permalink raw reply	[flat|nested] 34+ messages in thread

end of thread, other threads:[~2005-06-12 13:24 UTC | newest]

Thread overview: 34+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-04-23  9:23 [Bug c/21173] New: miscompiled pointer subtraction broke Linux kernel mikpe at csd dot uu dot se
2005-04-23 11:56 ` [Bug c/21173] " belyshev at depni dot sinp dot msu dot ru
2005-04-23 12:17 ` azarah at gentoo dot org
2005-04-23 12:30 ` [Bug tree-optimization/21173] [4.0 regression] " belyshev at depni dot sinp dot msu dot ru
2005-04-23 12:52 ` [Bug tree-optimization/21173] [4.0/4.1 " belyshev at depni dot sinp dot msu dot ru
2005-04-23 13:18 ` pinskia at gcc dot gnu dot org
2005-04-23 13:24 ` steven at gcc dot gnu dot org
2005-04-23 13:36 ` steven at gcc dot gnu dot org
2005-04-23 13:39 ` steven at gcc dot gnu dot org
2005-04-23 14:59 ` jakub at gcc dot gnu dot org
2005-04-23 15:09 ` dberlin at dberlin dot org
2005-04-23 15:13 ` dberlin at dberlin dot org
2005-04-23 17:19 ` azarah at gentoo dot org
2005-04-23 19:37 ` steven at gcc dot gnu dot org
2005-04-23 23:01 ` stevenb at suse dot de
2005-04-24  9:23 ` stevenb at suse dot de
2005-04-24 15:19 ` dberlin at gcc dot gnu dot org
2005-04-24 15:27 ` dberlin at dberlin dot org
2005-04-25 14:00 ` cvs-commit at gcc dot gnu dot org
2005-04-25 14:03 ` cvs-commit at gcc dot gnu dot org
2005-04-25 14:19 ` cvs-commit at gcc dot gnu dot org
2005-04-25 14:47 ` pinskia at gcc dot gnu dot org
2005-04-25 14:50 ` jakub at gcc dot gnu dot org
2005-04-25 15:18 ` dberlin at dberlin dot org
2005-04-26 12:36 ` dberlin at gcc dot gnu dot org
2005-04-28  2:35 ` belyshev at depni dot sinp dot msu dot ru
2005-04-28 23:38 ` cvs-commit at gcc dot gnu dot org
2005-05-10 17:32 ` pinskia at gcc dot gnu dot org
2005-05-10 22:31 ` pinskia at gcc dot gnu dot org
2005-05-11 10:51 ` pinskia at gcc dot gnu dot org
2005-05-17 11:35 ` pinskia at gcc dot gnu dot org
2005-05-18  6:05 ` pinskia at gcc dot gnu dot org
2005-05-22 18:47 ` pinskia at gcc dot gnu dot org
2005-06-12 13:24 ` pinskia at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).