public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "bww9643 at gmail dot com" <sourceware-bugzilla@sourceware.org>
To: glibc-bugs@sourceware.org
Subject: [Bug string/29422] New: unexpected result occured in strcpy function (i386, with __strcpy_sse2)
Date: Thu, 28 Jul 2022 11:30:32 +0000	[thread overview]
Message-ID: <bug-29422-131@http.sourceware.org/bugzilla/> (raw)

https://sourceware.org/bugzilla/show_bug.cgi?id=29422

            Bug ID: 29422
           Summary: unexpected result occured in strcpy function (i386,
                    with __strcpy_sse2)
           Product: glibc
           Version: unspecified
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: string
          Assignee: unassigned at sourceware dot org
          Reporter: bww9643 at gmail dot com
  Target Milestone: ---

function related with "sse" register or instruction can copy more than 4bytes
with xmm() register. However, unexpected error occured with this testcase:

```
Linux ubuntu 5.15.0-41-generic #44~20.04.1-Ubuntu SMP Fri Jun 24 13:27:29 UTC
2022 x86_64 x86_64 x86_64 GNU/Linux
~ > cat test.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

char a[1024];

int main(){
        strcpy(a,"0123456789abcd");
        printf("%s\n", a);
        strcpy(a,a+2);
        printf("%s\n", a);
        strcpy(a,a+2);
        printf("%s\n", a);
        strcpy(a,a+2);
        printf("%s", a);
        return 0;
}
~ > gcc -m32 -o test test.c
~ > ./test
0123456789abcd
23456989abcd
456989abcd
6989abcd%
```

expected result:
0123456789abcd
23456789abcd
456789abcd
6789abcd

root cause:

!> register environment before execute following instructions:
ecx is ptr of a+2
edx is ptr of a
0xf7d60580 <__strcpy_sse2+1408> movlpd xmm0, QWORD PTR [ecx]
0xf7d60584 <__strcpy_sse2+1412> movlpd QWORD PTR [edx], xmm0
0xf7d60588 <__strcpy_sse2+1416> movlpd xmm0, QWORD PTR [ecx+0x5]
0xf7d6058d <__strcpy_sse2+1421> movlpd QWORD PTR [edx+0x5], xmm0

in mode -m32, `strcpy` use specific function `__strcpy_sse2`.
This function is operated with raw assembly code. (ext: .S)

it applys this following process:
    1. check string's length and jmp to each case.
    2. instruction pointer jumps exact line when string's length is 14.
    3. save xmm0 to "23456789" (start with ptr+2) and overwrite ptr
        result : "2345678989abcd"
    4. save xmm0 to "989abcd\0" (start with ptr+7) and overwrite ptr+5
        result : "23456989abcd" (unexpected!)

Idk why this code should be written with this flow.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

             reply	other threads:[~2022-07-28 11:30 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-28 11:30 bww9643 at gmail dot com [this message]
2022-07-28 11:57 ` [Bug string/29422] " schwab@linux-m68k.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-29422-131@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=glibc-bugs@sourceware.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).