mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: "Thomas Weißschuh" <linux@weissschuh.net>
Cc: Willy Tarreau <w@1wt.eu>, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 2/3] selftests/nolibc: cast execve() argv string to character pointer
Date: Sat, 23 May 2026 10:53:42 +0100	[thread overview]
Message-ID: <20260523105342.7c1def4b@pumpkin> (raw)
In-Reply-To: <0cbdc16f-5882-4440-aadf-8ca5c7cd2c67@t-8ch.de>

On Fri, 22 May 2026 23:40:17 +0200
Thomas Weißschuh <linux@weissschuh.net> wrote:

> On 2026-05-22 19:48:07+0100, David Laight wrote:
> > On Fri, 22 May 2026 16:39:58 +0200
> > Thomas Weißschuh <linux@weissschuh.net> wrote:  
> > > On 2026-05-21 19:15:58+0100, David Laight wrote:  
> > > > On Thu, 21 May 2026 18:29:30 +0200
> > > > Thomas Weißschuh <linux@weissschuh.net> wrote:
> > > >     
> > > > > The existing code would trigger a warning under -Wwrite-strings which is
> > > > > about to be enabled. execve() is specified as not modifying the argv
> > > > > array, but the exact semantics are not representable in the type system.    
> > > > 
> > > > I suspect you'll have to fix it again to avoid 'casting away const'.    
> > > 
> > > Where would this warning be coming from? Which compiler flags are needed?
> > > Afaik it is legal to cast away const.  
> > 
> > IIRC -Wcast-qual  
> 
> Yes that's it, thanks.
> 
> > Lots of things are legal :-)
> > The problem with enabling -Wcast-qual (NetBSD's kernel does/did) it is makes
> > life annoying when you really do have to do it.
> > (From what I remember there weren't really that many.)
> > You sort of want an (unconst foo *) cast that won't generate a warning when
> > a simple (foo *) cast would.  
> 
> There seem to be a fair amount of standard C APIs which require such
> casts, for example strstr(). Also the UAPI headers currently emit such
> warnings. So I am not sure if it makes sense to try to get nolibc
> compile with this warning.

It ought to be an aim :-)

Very recent headers use _Generic() so that the return type of strchr()
and strstr() is the same as the argument.
It should be possibly to get the linker to use the same symbol for both
so the code only exists once.

There is also the problem that 'const foo *' can either mean 'the data
area cannot be written to through this pointer' or 'the data area cannot
be written to at all'.
I've seen gcc assume the latter and then 'miscompile':
int f(const struct foo *foo)
{
	int n = foo->n;
	g();
	return foo->n == n;
}
because it assumes that g() cannot change the contents of foo.

> 
> > > > Can you use something like (char[]){"/"} ?    
> > > 
> > > That looks good.

Given that the exec() test does the same for argv[] (mostly to get
it all one one line) and object size doesn't really matter there
that one could be done that was - almost for consistency.

> > > However if this issue is real we will also have it in
> > > nolibc's errno.h. There I don't want to use this pattern, as it requires
> > > more memory.  
> > 
> > You can move a string from .rodata to .data easily enough.
> > Doesn't change the memory footprint.  
> 
> When using the proposed pattern in errno.h I get plus 4 bytes of .bss
> usage for each variable. While it doesn't make a difference in the
> binary, at runtime these bytes are quite wasted.
> I didn't look closely at it yet, but the compiler could be reusing a
> single empty string in .rodata for all different users, while the .data
> one needs to be duplicated for each one.

That will happen, and the linker might merge the string with another '\0'
in .rodata.str.1
You'd need to have a named 'char null[] = ""'.

> So I think the current version, casting to (char *), is still the
> best aproach for now.

Avoiding casting away const is hard.
ISTR that there are also oddities with volatile.

-- David



  reply	other threads:[~2026-05-23  9:53 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-21 16:29 [PATCH 0/3] tools/nolibc: -Wwrite-strings compatibility Thomas Weißschuh
2026-05-21 16:29 ` [PATCH 1/3] tools/nolibc: cast default values of program_invocation_name Thomas Weißschuh
2026-05-21 16:29 ` [PATCH 2/3] selftests/nolibc: cast execve() argv string to character pointer Thomas Weißschuh
2026-05-21 18:15   ` David Laight
2026-05-22 14:39     ` Thomas Weißschuh
2026-05-22 18:48       ` David Laight
2026-05-22 21:40         ` Thomas Weißschuh
2026-05-23  9:53           ` David Laight [this message]
2026-05-24  8:18             ` Thomas Weißschuh
2026-05-21 16:29 ` [PATCH 3/3] selftests/nolibc: test against -Wwrite-strings Thomas Weißschuh
2026-05-22  3:28 ` [PATCH 0/3] tools/nolibc: -Wwrite-strings compatibility Willy Tarreau

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=20260523105342.7c1def4b@pumpkin \
    --to=david.laight.linux@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@weissschuh.net \
    --cc=w@1wt.eu \
    /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®