[llvm-bugs] [Bug 38860] New: Mapping a struct onto an aligned byte array produces incorrect output when compiled with -O2

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Sep 6 21:01:46 PDT 2018


https://bugs.llvm.org/show_bug.cgi?id=38860

            Bug ID: 38860
           Summary: Mapping a struct onto an aligned byte array produces
                    incorrect output when compiled with -O2
           Product: clang
           Version: unspecified
          Hardware: Macintosh
                OS: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: adrianwjw at gmail.com
                CC: llvm-bugs at lists.llvm.org

Version: Apple LLVM version 9.1.0 (clang-902.0.39.2)

Reduced test case. Expected value of sum is 553757185, but under -O2 the first
four bytes of the target buffer are empty, resulting in an incorrect sum value
of 64000:

#include "stdint.h"
#include "stdio.h"
#include "stdlib.h"

typedef struct some_struct {
    unsigned short fst;
    unsigned short snd;
} some_struct;

void test(void *source_buf, uint32_t source_buf_size) {
    void *target_buf;
    target_buf = calloc(source_buf_size, 1);

    unsigned short fst = 0;
    unsigned short snd = 0;

    uint32_t num_structs = source_buf_size / sizeof(some_struct);
    printf("num_structs = %d\n", num_structs);

    for(int i = 0; i < num_structs; i++) {
        fst = ((some_struct *) source_buf)[i].fst;
        snd = ((some_struct *) source_buf)[i].snd;

        ((some_struct *) target_buf)[i].fst = fst;
        ((some_struct *) target_buf)[i].snd = snd;
    }

    uint32_t sum = 0;
    uint32_t num_uint32s = source_buf_size / sizeof(uint32_t);
    printf("num_uint32s = %d\n", num_uint32s);

    for(int i = 0; i < num_uint32s; i++) {
        sum += ((uint32_t *) target_buf)[i];
        printf("%d: *buffer = 0x%08x sum = %d\n", i, ((uint32_t *)
target_buf)[i], sum);
    }
    printf("sum = %d\n", sum);

    free(target_buf);
}

// Apple LLVM version 9.1.0 (clang-902.0.39.2)

// clang -Wall -Wcast-align -O2 subset.c -o subset
//     num_structs = 2
//     num_uint32s = 2
//     0: *buffer = 0x00000000 sum = 0
//     1: *buffer = 0x0000fa00 sum = 64000
//     sum = 64000

// clang -Wall -Wcast-align -O1 subset.c -o subset
//     num_structs = 2
//     num_uint32s = 2
//     0: *buffer = 0x2100b001 sum = 553693185
//     1: *buffer = 0x0000fa00 sum = 553757185
//     sum = 553757185
int main() {
    unsigned char source_buf[8] = {0x01,0xb0,0x00,0x21,0x00,0xfa,0x00,0x00};

    test(source_buf, 8);

    return 0;
}

Or am I writing some highly illegal C code here?


Thanks,
Adrian

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20180907/b495e099/attachment-0001.html>


More information about the llvm-bugs mailing list