On 23/09/2021 12:11, Mikhail Barashkov via GNU coreutils General Discussion 
wrote:
Hi,

I've updated this patch as recommended, now it only applies to E2K cpu
in protected mode.

Thanks,

Mikhail

21.09.2021 18:14, Pádraig Brady пишет:
On 21/09/2021 15:40, Mikhail Barashkov via GNU coreutils General
Discussion wrote:
Randread was allocating memory, but not setting it to 0, resulting, in
particular, to a crash in shuf program in E2K CPU protected mode (later
on s->bug is used by shuf in ISAAC_MIX as the seed parameter).

This patch makes sure the memory buffer is zeroed out correctly.

diff --git a/gl/lib/randread.c b/gl/lib/randread.c
index 7124e3df0..8e6b1c5b8 100644
--- a/gl/lib/randread.c
+++ b/gl/lib/randread.c
@@ -132,6 +132,7 @@ static struct randread_source *
    simple_new (FILE *source, void const *handler_arg)
    {
      struct randread_source *s = xmalloc (sizeof *s);
+  memset(s, 0, sizeof *s);
      s->source = source;
      s->handler = randread_error;
      s->handler_arg = handler_arg;

I'm wary of the unconditional initialization of this large struct here.
I'm reminded of this general discussion https://research.swtch.com/sparse

If __e2k__ needs this, we should probably restrict to that arch?
Also should you be using xcalloc(1, sizeof *s) instead?

I'll let Paul review this as he's most familiar with this code.

Attached in the combined patch for e2k.
How complete is this BTW?
Are there other patches required for gnulib etc?
Does the coreutils test suite complete with this patch?

thanks,
Pádraig
From a466108bcb0314900872153faf5fa0d5ffcc43e2 Mon Sep 17 00:00:00 2001
From: Mikhail Barashkov <gnu@mbarashkov.email>
Date: Tue, 21 Sep 2021 15:47:11 +0100
Subject: [PATCH] build: fix E2K architecture compatibility

Avoid crashes on MCST Elbrus 2000, in protected mode.

* src/yes.c (main): Avoid reusing operands memory
in protected mode on __e2k__ architecture.
* gl/lib/randread.c (simple_new): Ensure mem initialized
to avoid crash in shuf when ISAAC_MIX is the seed parameter.
---
 gl/lib/randread.c | 5 +++++
 src/yes.c         | 5 +++++
 2 files changed, 10 insertions(+)

diff --git a/gl/lib/randread.c b/gl/lib/randread.c
index 7124e3df0..5b0ff34f4 100644
--- a/gl/lib/randread.c
+++ b/gl/lib/randread.c
@@ -132,6 +132,11 @@ static struct randread_source *
 simple_new (FILE *source, void const *handler_arg)
 {
   struct randread_source *s = xmalloc (sizeof *s);
+#ifdef __e2k__
+  /* In E2K protected mode uninitialized memory can't be used  */
+  if (sizeof (void*) == 16)
+    memset(s, 0, sizeof *s);
+#endif
   s->source = source;
   s->handler = randread_error;
   s->handler_arg = handler_arg;
diff --git a/src/yes.c b/src/yes.c
index b6bd35a5e..5dd4c4632 100644
--- a/src/yes.c
+++ b/src/yes.c
@@ -99,6 +99,11 @@ main (int argc, char **argv)
 
   /* Fill the buffer with one copy of the output.  If possible, reuse
      the operands strings; this wins when the buffer would be large.  */
+#ifdef __e2k__
+  /* In E2K protected mode we can't reuse operands memory.  */
+  if (sizeof (void*) == 16)
+    reuse_operand_strings = false;
+#endif
   char *buf = reuse_operand_strings ? *operands : xmalloc (bufalloc);
   size_t bufused = 0;
   operandp = operands;
-- 
2.26.2

Reply via email to