All of lore.kernel.org
 help / color / mirror / Atom feed
From: Philippe Gerum <rpm@xenomai.org>
To: dietmar.schindler@manrolandgoss.com
Cc: xenomai@lists.linux.dev
Subject: Re: data modified by second NRT write with same memory location
Date: Wed, 02 Aug 2023 11:01:17 +0200	[thread overview]
Message-ID: <877cqderv0.fsf@xenomai.org> (raw)
In-Reply-To: <bcc0cdae5a594bcfa625e3e75fa35e17@manrolandgoss.com>


<dietmar.schindler@manrolandgoss.com> writes:

>> From: Philippe Gerum <rpm@xenomai.org>
>> Sent: Wednesday, August 2, 2023 9:31 AM
>> ...
>> yo sang <stlfatboy@hotmail.com> writes:
>>
>> > class RTMem
>> > {
>> > public:
>> > RTMem() {}
>> >
>> > RTMem(const char* data, int size, RTMemType type) {
>> > Data = Buf;
>> > memcpy(Data, data, size);
>> > Size = size;
>> > Type = type;
>> > }
>> >
>> > intSize{};
>> > RTMemTypeType{};
>> > char*Data{};
>> > charBuf[256]{};
>> >
>> > };
>>
>> <snip>
>>
>> > static void *realtime_thread(void *arg)
>> > {
>> >    struct timespec ts;
>> >    int ret;
>> >    RTMem buf;
>> >    for (;;) {
>> >        ret = recvfrom(s, &buf, sizeof(buf), 0, NULL, 0);
>> >        if (ret <= 0)
>> >fail("recvfrom");
>> >        printf("   => %s \n", buf.Data);
>>
>> <snip>
>>
>> >
>> >     RTMem data("data1", 6, RTMEM_1);
>> >     ret = write(fd, &data, sizeof(data));
>> >     if (ret != sizeof(data))
>> >         fail("write");
>> >     memcpy(data.Data, "datadata", 9);
>> >     data.Size = 9;
>> >     data.Type = RTMEM_2;
>> >     ret = write(fd, &data, sizeof(data));
>> >     if (ret != sizeof(data))
>> >         fail("write");
>>
>> This example code is broken, badly. Please consider what the reader
>> thread actually reads, which is definitely not a value, but a descriptor
>> containing a pointer to some value, which the writer thread will
>> overwrite before realtime_thread() runs. Which explains the outcome.
>>
>> At the very least, you should have a distinct RTMem instance to feed
>> each write() with. This said, since XDDP is by design an inter-stage,
>> inter-process IPC, passing process-private pointers in messages looks
>> wrong in the first place.
>
> I think it would also work if only the reader used buf.Buf rather than buf.Data, i. e.
>
>         printf("   => %s \n", buf.Buf);
>
> since buf.Buf is copied and remains meaningful, unlike the pointer.

Agreed. I'm giving the original implementation the benefit of the doubt
assuming that such pointer would in fact mimic some kind of read cursor
on the receiver end in the actual application, unfortunately initialized
by the send site in the example.

-- 
Philippe.

  reply	other threads:[~2023-08-02  9:15 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <DM6PR14MB3871876E6E078D3BE974ADCBA30AA@DM6PR14MB3871.namprd14.prod.outlook.com>
2023-08-01  9:18 ` data modified by second NRT write with same memory location yo sang
2023-08-01 17:29   ` Philippe Gerum
2023-08-02  1:19     ` yo sang
2023-08-02  6:04       ` yo sang
2023-08-02  7:30         ` Philippe Gerum
2023-08-02  8:14           ` dietmar.schindler
2023-08-02  9:01             ` Philippe Gerum [this message]
2023-08-02  8:32           ` yo sang
2023-08-02  8:56             ` Philippe Gerum
2023-08-03  1:03               ` yo sang

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=877cqderv0.fsf@xenomai.org \
    --to=rpm@xenomai.org \
    --cc=dietmar.schindler@manrolandgoss.com \
    --cc=xenomai@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.