public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "eric-bugs at omnifarious dot org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/108337] New: Misaligned memory access issues when inline assembly is used with optimization on x86_64
Date: Sun, 08 Jan 2023 17:38:01 +0000	[thread overview]
Message-ID: <bug-108337-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108337

            Bug ID: 108337
           Summary: Misaligned memory access issues when inline assembly
                    is used with optimization on x86_64
           Product: gcc
           Version: 12.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: eric-bugs at omnifarious dot org
  Target Milestone: ---

The following code works fine when compiled this way:

g++ -std=c++20 -march=znver2 -static -O1 -nostartfiles -nostdlib -Wl,-emain
foo.cpp

but when compiled this way generates a segfault before outputting anything:

g++ -std=c++20 -march=znver2 -static -O2 -nostartfiles -nostdlib -Wl,-emain
foo.cpp


---------- foo.cpp ---------
using val_t = unsigned long;
using call_id = unsigned short;

  template<typename _Tp>
    struct remove_reference
    { typedef _Tp type; };

  template<typename _Tp>
    struct remove_reference<_Tp&>
    { typedef _Tp type; };

  template<typename _Tp>
    struct remove_reference<_Tp&&>
    { typedef _Tp type; };

  template<typename _Tp>
    [[__nodiscard__]]
    constexpr _Tp&&
    forward(typename remove_reference<_Tp>::type& __t) noexcept
    { return static_cast<_Tp&&>(__t); }


struct syscall_param {
   syscall_param(val_t v) noexcept : value(v) { }

   syscall_param(void *v) noexcept : value(reinterpret_cast<val_t>(v)) {
      static_assert(sizeof(void *) == sizeof(val_t));
   }

   syscall_param(void const *v) noexcept : value(reinterpret_cast<val_t>(v)) {
      static_assert(sizeof(void *) == sizeof(val_t));
   }
   val_t value;
};

inline val_t do_syscall(call_id callnum,
                           syscall_param const &p1) noexcept
{
   val_t retval;
   asm volatile (
      "syscall\n\t"
       :"=a"(retval)
       :"a"(static_cast<unsigned long>(callnum)), "D"(p1.value)
       :"%rcx", "%r11", "memory"
      );
   return retval;
}

inline val_t do_syscall(call_id callnum,
                           syscall_param const &p1,
                           syscall_param const &p2,
                           syscall_param const &p3) noexcept
{
   val_t retval;
   asm volatile (
      "syscall\n\t"
       :"=a"(retval)
       :"a"(static_cast<unsigned long>(callnum)), "D"(p1.value), "S"(p2.value),
 "d"(p3.value)
       :"%rcx", "%r11", "memory"
      );
   return retval;
}

template <typename... T>
val_t
syscall_expected(call_id callnum, T &&... args) noexcept
{
   val_t result = do_syscall(callnum, forward<T>(args)...);

   return result;
}

inline val_t write(int fd, char const *data, unsigned long size) noexcept
{
   return syscall_expected(1, fd, data, size);
}

inline void exit [[noreturn]](int status) noexcept {
   syscall_expected(231, status);
   __builtin_unreachable();
}

int main(int argc, char *argv[])
{
    int i = 0;
    char msg[] = "Hello World 0!\n";

    auto result = write(1, msg, sizeof(msg) - 1);
    i = 1;
    while (result >= 0 && i < 10) {
        msg[12] = i++ + '0';
        result = write(1, msg, sizeof(msg) - 1);
    }
    exit(result >= 0 ? 0 : 1);
}

             reply	other threads:[~2023-01-08 17:38 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-08 17:38 eric-bugs at omnifarious dot org [this message]
2023-01-08 17:47 ` [Bug target/108337] " pinskia at gcc dot gnu.org
2023-01-08 18:56 ` pinskia at gcc dot gnu.org
2023-01-08 21:52 ` eric-bugs at omnifarious dot org
2023-01-08 21:56 ` pinskia at gcc dot gnu.org
2023-01-08 22:20 ` eric-bugs at omnifarious dot org

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=bug-108337-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).