9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] constant string
@ 2004-03-08 10:15 Prem Mallappa
  2004-03-08 10:27 ` lucio
                   ` (4 more replies)
  0 siblings, 5 replies; 10+ messages in thread
From: Prem Mallappa @ 2004-03-08 10:15 UTC (permalink / raw)
  To: 9fans

hi,

this is my program
------------------------------------------
#include <stdio.h>

int main(void)
{
	char *s = "Hello";
	
	s[2] = 'z';
	
	printf ("%s\n", s);
	
	return 0;
}

------------------------------------------

my question is when i compile this in Linux (gcc) i get a segmentation 
fault at the second statement of main(),
As far as my knowledge i think this is because in 2nd chapter of K&R book
it is being mentioned that
' any thing enclosed between " and " is a string constant' so here i am
changing a constant, and i get a segmentation fault ( and i also noticed
that gcc stores the string Hello in read-only datasegment)

but when i compile the same thing in plan9 C compiler ( both native "8c"
and "pcc" i get a output of "Hezlo")
why this is happenning..

sorry if i am posting to wrong group,
thanks in advance
prem


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [9fans] constant string
  2004-03-08 10:15 [9fans] constant string Prem Mallappa
@ 2004-03-08 10:27 ` lucio
  2004-03-08 10:53 ` [9fans] " Richard Bos
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 10+ messages in thread
From: lucio @ 2004-03-08 10:27 UTC (permalink / raw)
  To: 9fans

> but when i compile the same thing in plan9 C compiler ( both native "8c"
> and "pcc" i get a output of "Hezlo")
> why this is happenning..
> 
I'm sure you've tickled one of those "architecture/implementation"
dependencies the "C" language is deservedly famed for.

++L



^ permalink raw reply	[flat|nested] 10+ messages in thread

* [9fans] Re: constant string
  2004-03-08 10:15 [9fans] constant string Prem Mallappa
  2004-03-08 10:27 ` lucio
@ 2004-03-08 10:53 ` Richard Bos
  2004-03-08 14:13 ` [9fans] " dbailey27
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 10+ messages in thread
From: Richard Bos @ 2004-03-08 10:53 UTC (permalink / raw)
  To: 9fans

Prem Mallappa <prem_mallappa@hotpop.com> wrote:

> #include <stdio.h>
> 
> int main(void)
> {
> 	char *s = "Hello";
>
> 	s[2] = 'z';
> 	
> 	printf ("%s\n", s);
> 	
> 	return 0;
> }

> my question is when i compile this in Linux (gcc) i get a segmentation 
> fault at the second statement of main(),
> As far as my knowledge i think this is because in 2nd chapter of K&R book
> it is being mentioned that
> ' any thing enclosed between " and " is a string constant' so here i am
> changing a constant, and i get a segmentation fault ( and i also noticed
> that gcc stores the string Hello in read-only datasegment)

<http://www.eskimo.com/~scs/C-faq/q1.32.html>. Basically, yes, you're
correct.

> but when i compile the same thing in plan9 C compiler ( both native "8c"
> and "pcc" i get a output of "Hezlo")
> why this is happenning..

To begin with, IIRC Plan 9 C isn't exactly ISO C. But in any case,
assigning to a constant (in those cases where you don't get a mandatory
warning, and when you ignore the warning and do the assignment anyway,
as well) invokes undefined behaviour. This means that the implementation
is free to behave as it chooses to; this includes crashing, behaving as
if it were correct code, and anything in between.

Richard


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [9fans] constant string
  2004-03-08 10:15 [9fans] constant string Prem Mallappa
  2004-03-08 10:27 ` lucio
  2004-03-08 10:53 ` [9fans] " Richard Bos
@ 2004-03-08 14:13 ` dbailey27
  2004-03-08 15:22   ` C H Forsyth
  2004-03-08 22:40 ` William Josephson
  2004-03-10  9:47 ` [9fans] " Peter Pichler
  4 siblings, 1 reply; 10+ messages in thread
From: dbailey27 @ 2004-03-08 14:13 UTC (permalink / raw)
  To: prem_mallappa, 9fans

[-- Attachment #1: Type: text/plain, Size: 1475 bytes --]

You answered your own question. Linux uses an ELF 
file format. When the ELF is loaded, the variables loaded
into the DATA segment are marked as read-only pages.
This, of course, will cause a write to such pages to 
manifest a SIGSEGV. 

Plan 9, however, makes no effort to do this. Objects
marked into the DATA segment of the Plan 9 file format
are loaded into pages that are actually RWX. So, it's really
a two-pronged issue. 
	1) The kernel loader doesn't handle the management
	    of page protection in the DATA, Stack, BSS, or even
	    the TEXT. 
	2) The current binary format doesn't allow for specification
	    of whether or not the DATA segment should *be* ~W.

There is one main reason why you would want a W DATA. 
Variables that end up initialized to a value other than nil must 
be put in the DATA segment. Otherwise, they end up in the 
BSS, which is initialized to nil.

Thus, global pointers to objects in the DATA (not const?) must
be R|W, while the object itself (raw data) must be ~W. This is
the ideal solution. 

This is being implemented in my work on the SPARC/SPARCv9
and AMD64 ports. I've discussed this with other members of
the AMD64 port, and there've been no nay-sayers. There will
be other improvements, as well. However, they are to be seen
later. 

FYI, I'm writing the toolchain(s) from scratch, so the new binary
format for these archs will look only slightly different from the
current. 

Don (north_)

[-- Attachment #2: Type: message/rfc822, Size: 3139 bytes --]

From: Prem Mallappa <prem_mallappa@hotpop.com>
To: 9fans@cse.psu.edu
Subject: [9fans] constant string
Date: Mon, 8 Mar 2004 10:15:07 GMT
Message-ID: <pan.2004.03.07.05.32.00.621137@hotpop.com>

hi,

this is my program
------------------------------------------
#include <stdio.h>

int main(void)
{
	char *s = "Hello";
	
	s[2] = 'z';
	
	printf ("%s\n", s);
	
	return 0;
}

------------------------------------------

my question is when i compile this in Linux (gcc) i get a segmentation 
fault at the second statement of main(),
As far as my knowledge i think this is because in 2nd chapter of K&R book
it is being mentioned that
' any thing enclosed between " and " is a string constant' so here i am
changing a constant, and i get a segmentation fault ( and i also noticed
that gcc stores the string Hello in read-only datasegment)

but when i compile the same thing in plan9 C compiler ( both native "8c"
and "pcc" i get a output of "Hezlo")
why this is happenning..

sorry if i am posting to wrong group,
thanks in advance
prem

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [9fans] constant string
  2004-03-08 14:13 ` [9fans] " dbailey27
@ 2004-03-08 15:22   ` C H Forsyth
  2004-03-09  3:41     ` Russ Cox
  0 siblings, 1 reply; 10+ messages in thread
From: C H Forsyth @ 2004-03-08 15:22 UTC (permalink / raw)
  To: 9fans

the plan 9 compilers, in the main, don't put constant
strings into read-only segments, though they could
(easiest on most platforms that aren't
separate I&D or x-only text
would be to pop them into the text segment).

on the other hand, you'd probably then need something like gcc's
-fwritable-strings to cope with older C programs that
wrote to them (and some did), although that need would
mainly be limited to ape.  (i haven't noticed plan 9
programs that do it, and i've ported quite a few
to other platforms.)



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [9fans] constant string
  2004-03-08 10:15 [9fans] constant string Prem Mallappa
                   ` (2 preceding siblings ...)
  2004-03-08 14:13 ` [9fans] " dbailey27
@ 2004-03-08 22:40 ` William Josephson
  2004-03-10  9:47 ` [9fans] " Peter Pichler
  4 siblings, 0 replies; 10+ messages in thread
From: William Josephson @ 2004-03-08 22:40 UTC (permalink / raw)
  To: 9fans

On Mon, Mar 08, 2004 at 10:15:07AM +0000, Prem Mallappa wrote:
> my question is when i compile this in Linux (gcc) i get a segmentation 
> 
> but when i compile the same thing in plan9 C compiler ( both native "8c"
> and "pcc" i get a output of "Hezlo")

Try compiling with gcc -fwritable-strings on Linux and see what
happens.  By default gcc puts initialized data in a read-only
segment but pcc does not.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [9fans] constant string
  2004-03-08 15:22   ` C H Forsyth
@ 2004-03-09  3:41     ` Russ Cox
  2004-03-09  9:17       ` Charles Forsyth
  0 siblings, 1 reply; 10+ messages in thread
From: Russ Cox @ 2004-03-09  3:41 UTC (permalink / raw)
  To: 9fans

> mainly be limited to ape.  (i haven't noticed plan 9
> programs that do it, and i've ported quite a few
> to other platforms.)

ed.



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [9fans] constant string
  2004-03-09  3:41     ` Russ Cox
@ 2004-03-09  9:17       ` Charles Forsyth
  0 siblings, 0 replies; 10+ messages in thread
From: Charles Forsyth @ 2004-03-09  9:17 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 42 bytes --]

good thing i didn't port that one, then!

[-- Attachment #2: Type: message/rfc822, Size: 2002 bytes --]

From: "Russ Cox" <rsc@swtch.com>
To: 9fans@cse.psu.edu
Subject: Re: [9fans] constant string
Date: Mon, 08 Mar 2004 22:41:15 -0500
Message-ID: <E1B0Y7K-000C9v-R3@t40.swtch.com>

> mainly be limited to ape.  (i haven't noticed plan 9
> programs that do it, and i've ported quite a few
> to other platforms.)

ed.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [9fans] Re: constant string
  2004-03-08 10:15 [9fans] constant string Prem Mallappa
                   ` (3 preceding siblings ...)
  2004-03-08 22:40 ` William Josephson
@ 2004-03-10  9:47 ` Peter Pichler
  2004-03-12  8:56   ` Bruce Ellis
  4 siblings, 1 reply; 10+ messages in thread
From: Peter Pichler @ 2004-03-10  9:47 UTC (permalink / raw)
  To: 9fans

"Prem Mallappa" <prem_mallappa@hotpop.com> wrote:
> #include <stdio.h>
>
> int main(void)
> {
> char *s = "Hello";
>
> s[2] = 'z';
>
> printf ("%s\n", s);
>
> return 0;
> }
>
> ------------------------------------------
>
> my question is when i compile this in Linux (gcc) i get a segmentation
> fault at the second statement of main(),
> As far as my knowledge i think this is because in 2nd chapter of K&R book
> it is being mentioned that
> ' any thing enclosed between " and " is a string constant' so here i am
> changing a constant, and i get a segmentation fault ( and i also noticed
> that gcc stores the string Hello in read-only datasegment)
>
> but when i compile the same thing in plan9 C compiler ( both native "8c"
> and "pcc" i get a output of "Hezlo")
> why this is happenning..

Welcome o the world of undefined behaviour. Modifying (or attempting to) a
string literal is one of many instances of UB. It may "work" on one machine
or with one compiler, but on another. It may even "work" on Tuesdays only.
In other words, do not do it.

> sorry if i am posting to wrong group,
> thanks in advance
> prem

Appropriate group, an equally appropriate answer.

Peter


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [9fans] Re: constant string
  2004-03-10  9:47 ` [9fans] " Peter Pichler
@ 2004-03-12  8:56   ` Bruce Ellis
  0 siblings, 0 replies; 10+ messages in thread
From: Bruce Ellis @ 2004-03-12  8:56 UTC (permalink / raw)
  To: 9fans

undefined or indeterminate behaviour means what it says.
same with the hypot() case.  undefined, and the program
crashed.  pragmatism determines what either U does in ?c.

brucee
----- Original Message ----- 
From: "Peter Pichler" <pichlo7@pobox.sk>
To: <9fans@cse.psu.edu>
Sent: Wednesday, March 10, 2004 8:47 PM
Subject: [9fans] Re: constant string



^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2004-03-12  8:56 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-03-08 10:15 [9fans] constant string Prem Mallappa
2004-03-08 10:27 ` lucio
2004-03-08 10:53 ` [9fans] " Richard Bos
2004-03-08 14:13 ` [9fans] " dbailey27
2004-03-08 15:22   ` C H Forsyth
2004-03-09  3:41     ` Russ Cox
2004-03-09  9:17       ` Charles Forsyth
2004-03-08 22:40 ` William Josephson
2004-03-10  9:47 ` [9fans] " Peter Pichler
2004-03-12  8:56   ` Bruce Ellis

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 NNTP newsgroup(s).