Bug 82411 - const is not always read-only
Summary: const is not always read-only
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-10-02 20:26 UTC by Kees Cook
Modified: 2018-03-27 23:34 UTC (History)
2 users (show)

See Also:
Host:
Target: powerpc*-*-*
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-10-02 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kees Cook 2017-10-02 20:26:53 UTC
On powerpc, a const variable may end up in the .sdata section, which is writable. This means authors cannot depend on the "const" marking to mean "read-only", as is required for sane Linux kernel memory protection security.

Thread here:
https://lkml.org/lkml/2017/10/2/488

At the very least, there should be a way to request never putting a const variable into a writable section.
Comment 1 Andrew Pinski 2017-10-02 20:54:03 UTC
Actually it is just undefined what happens when a write to a const variable. 

So a trap or the write happened for a const both are valid thing.

Now const really should be put in the read only section if possible.

Putting it in the sdata section is valid thing to do but I doubt some people want it there. 

Powerpc should have an option which disabled this optimization for const variables.

Right now there is a way to disable all of sdata by -G0 option which is a good workaround.
Comment 2 Segher Boessenkool 2017-10-02 21:25:29 UTC
To access this as sdata is faster and smaller than as rodata (one
instruction instead of two).

You can use -G0 as Andrew says, or -mno-sdata (or -msdata=none).
Comment 3 Kees Cook 2017-10-04 23:41:50 UTC
To clarify, using -mno-sdata means all things are removed from sdata, not just const, yes? I'd like to be able to leave writable stuff there, to avoid any additional performance penalty.
Comment 4 Segher Boessenkool 2017-10-05 00:46:39 UTC
That what it means yes.  You can use it as a workaround.

There is no option yet to put only writable data in sdata.
Comment 5 Segher Boessenkool 2018-02-28 15:09:29 UTC
RFC patch is at https://gcc.gnu.org/ml/gcc-patches/2018-02/msg01533.html ;
so far it has been tested with Linux and with EABI.
Comment 6 Segher Boessenkool 2018-03-07 20:27:43 UTC
Author: segher
Date: Wed Mar  7 20:27:11 2018
New Revision: 258340

URL: https://gcc.gnu.org/viewcvs?rev=258340&root=gcc&view=rev
Log:
rs6000: -mreadonly-in-sdata (PR82411)

This adds a new option -mreadonly-in-sdata (on by default) that
controls whether readonly data can be put in sdata.  (For EABI this
does nothing, readonly data is put in sdata2 as usual).


	PR target/82411
	* config/rs6000/rs6000.c (rs6000_elf_in_small_data_p): Don't put
	readonly data in sdata, if that is disabled.
	* config/rs6000/sysv4.opt (mreadonly-in-sdata): New option.
	* doc/invoke.texi (RS/6000 and PowerPC Options): Document
	-mreadonly-in-sdata option.

gcc/testsuite/
	PR target/82411
	* gcc.target/powerpc/ppc-sdata-2.c: Skip if -mno-readonly-in-sdata.

Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/config/rs6000/rs6000.c
    trunk/gcc/config/rs6000/sysv4.opt
    trunk/gcc/doc/invoke.texi
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gcc.target/powerpc/ppc-sdata-2.c
Comment 7 Segher Boessenkool 2018-03-07 20:29:20 UTC
Done on trunk; backports pending.
Comment 8 Segher Boessenkool 2018-03-27 23:13:33 UTC
Author: segher
Date: Tue Mar 27 23:13:02 2018
New Revision: 258907

URL: https://gcc.gnu.org/viewcvs?rev=258907&root=gcc&view=rev
Log:
rs6000: -mreadonly-in-sdata (PR82411)

This adds a new option -mreadonly-in-sdata (on by default) that
controls whether readonly data can be put in sdata.  (For EABI this
does nothing, readonly data is put in sdata2 as usual).


	Backport from mainline
	2018-03-08  Segher Boessenkool  <segher@kernel.crashing.org>

	PR target/82411
	* config/rs6000/rs6000.c (rs6000_elf_in_small_data_p): Don't put
	readonly data in sdata, if that is disabled.
	* config/rs6000/sysv4.opt (mreadonly-in-sdata): New option.
	* doc/invoke.texi (RS/6000 and PowerPC Options): Document
	-mreadonly-in-sdata option.


	Backport from mainline
	2018-03-08  Segher Boessenkool  <segher@kernel.crashing.org>

	PR target/82411
	* gcc.target/powerpc/ppc-sdata-2.c: Skip if -mno-readonly-in-sdata.

Modified:
    branches/gcc-7-branch/gcc/ChangeLog
    branches/gcc-7-branch/gcc/config/rs6000/rs6000.c
    branches/gcc-7-branch/gcc/config/rs6000/sysv4.opt
    branches/gcc-7-branch/gcc/doc/invoke.texi
    branches/gcc-7-branch/gcc/testsuite/ChangeLog
    branches/gcc-7-branch/gcc/testsuite/gcc.target/powerpc/ppc-sdata-2.c
Comment 9 Segher Boessenkool 2018-03-27 23:28:57 UTC
Author: segher
Date: Tue Mar 27 23:28:25 2018
New Revision: 258909

URL: https://gcc.gnu.org/viewcvs?rev=258909&root=gcc&view=rev
Log:
rs6000: -mreadonly-in-sdata (PR82411)

This adds a new option -mreadonly-in-sdata (on by default) that
controls whether readonly data can be put in sdata.  (For EABI this
does nothing, readonly data is put in sdata2 as usual).


	Backport from mainline
	2018-03-08  Segher Boessenkool  <segher@kernel.crashing.org>

	PR target/82411
	* config/rs6000/rs6000.c (rs6000_elf_in_small_data_p): Don't put
	readonly data in sdata, if that is disabled.
	* config/rs6000/sysv4.opt (mreadonly-in-sdata): New option.
	* doc/invoke.texi (RS/6000 and PowerPC Options): Document
	-mreadonly-in-sdata option.


gcc/testsuite/
	Backport from mainline
	2018-03-08  Segher Boessenkool  <segher@kernel.crashing.org>

	PR target/82411
	* gcc.target/powerpc/ppc-sdata-2.c: Skip if -mno-readonly-in-sdata.

Modified:
    branches/gcc-6-branch/gcc/ChangeLog
    branches/gcc-6-branch/gcc/config/rs6000/rs6000.c
    branches/gcc-6-branch/gcc/config/rs6000/sysv4.opt
    branches/gcc-6-branch/gcc/doc/invoke.texi
    branches/gcc-6-branch/gcc/testsuite/ChangeLog
    branches/gcc-6-branch/gcc/testsuite/gcc.target/powerpc/ppc-sdata-2.c
Comment 10 Segher Boessenkool 2018-03-27 23:34:02 UTC
All done.