From: "Michael J. Baars" <mjbaars1977.gcc@cyberfiber.eu>
To: gcc@gcc.gnu.org, linux-kernel@vger.kernel.org
Subject: problems with memory allocation and the alignment check
Date: Mon, 22 Feb 2021 10:08:50 +0100 [thread overview]
Message-ID: <80753cbc54ef69b4fc136f791666197fc8b1f8bb.camel@cyberfiber.eu> (raw)
[-- Attachment #1: Type: text/plain, Size: 390 bytes --]
Hi,
I just wrote this little program to demonstrate a possible flaw in both malloc and calloc.
If I allocate a the simplest memory region from main(), one out of three optimization flags fail.
If I allocate the same region from a function, three out of three optimization flags fail.
Does someone know if this really is a flaw, and if so, is it a gcc or a kernel flaw?
Regards,
Mischa.
[-- Attachment #2: main.c --]
[-- Type: text/x-csrc, Size: 862 bytes --]
#include <stdlib.h>
#include <stdint.h>
#define RFLAGS_REGISTER_GET(y) \
{ \
asm volatile \
( \
" pushfq \n" \
" pop %0 \n" \
\
: "=m" (* y) \
); \
};
#define RFLAGS_REGISTER_SET(x) \
{ \
asm volatile \
( \
" push %0 \n" \
" popfq \n" \
\
: \
: "r" (* x) \
); \
};
struct storage
{
uint8_t* c;
};
int function(struct storage* s)
{
s->c = calloc (sizeof(uint8_t), 8);
free (s->c);
};
int main()
{
struct storage s;
uint64_t rflags;
RFLAGS_REGISTER_GET(&rflags); rflags ^= 0x0000000000040000;
RFLAGS_REGISTER_SET(&rflags);
// function(&s);
s.c = calloc (sizeof(uint8_t), 8);
free (s.c);
RFLAGS_REGISTER_GET(&rflags); rflags ^= 0x0000000000040000;
RFLAGS_REGISTER_SET(&rflags);
}
[-- Attachment #3: makefile --]
[-- Type: text/x-makefile, Size: 102 bytes --]
all:
gcc -o main main.c
gcc -O2 -o mainO2 main.c
gcc -Ofast -o mainOfast main.c
next reply other threads:[~2021-02-22 9:33 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-02-22 9:08 Michael J. Baars [this message]
2021-02-22 9:29 ` Andrew Pinski
2021-02-22 9:37 ` Michael J. Baars
2021-02-22 9:41 ` Andrew Pinski
2021-02-22 10:04 ` Michael J. Baars
2021-02-24 7:05 ` Michael J. Baars
2021-02-23 11:46 ` David Laight
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=80753cbc54ef69b4fc136f791666197fc8b1f8bb.camel@cyberfiber.eu \
--to=mjbaars1977.gcc@cyberfiber.eu \
--cc=gcc@gcc.gnu.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®