aboutsummaryrefslogtreecommitdiff
path: root/gcc/ada
diff options
context:
space:
mode:
authorAlexandre Oliva <oliva@adacore.com>2022-09-08 00:15:19 -0300
committerMarc Poulhiès <poulhies@adacore.com>2022-10-06 11:22:49 +0200
commit21130676a8032b53dad05d224a78244a419e4929 (patch)
treec67bda32d5660d5290bf1ed89212185f14b5ce13 /gcc/ada
parent09203b787a9a64bd57ee83d1be1495975a384ec6 (diff)
downloadgcc-21130676a8032b53dad05d224a78244a419e4929.zip
gcc-21130676a8032b53dad05d224a78244a419e4929.tar.gz
gcc-21130676a8032b53dad05d224a78244a419e4929.tar.bz2
ada: hardened booleans: exemplify codegen changes
Show the sort of code that is to be expected from using hardened booleans in Ada code. Mention that C traps instead of raising exceptions. gcc/ada/ * doc/gnat_rm/security_hardening_features.rst: Add examples of codegen changes in hardened booleans. Mention that C traps where Ada raises exceptions. * gnat_rm.texi: Regenerate.
Diffstat (limited to 'gcc/ada')
-rw-r--r--gcc/ada/doc/gnat_rm/security_hardening_features.rst29
-rw-r--r--gcc/ada/gnat_rm.texi26
2 files changed, 48 insertions, 7 deletions
diff --git a/gcc/ada/doc/gnat_rm/security_hardening_features.rst b/gcc/ada/doc/gnat_rm/security_hardening_features.rst
index e36d475..d8ea849 100644
--- a/gcc/ada/doc/gnat_rm/security_hardening_features.rst
+++ b/gcc/ada/doc/gnat_rm/security_hardening_features.rst
@@ -265,19 +265,40 @@ further remove checks found to be redundant.
For additional hardening, the ``hardbool`` :samp:`Machine_Attribute`
pragma can be used to annotate boolean types with representation
clauses, so that expressions of such types used as conditions are
-checked even when compiling with :switch:`-gnatVT`.
+checked even when compiling with :switch:`-gnatVT`:
.. code-block:: ada
pragma Machine_Attribute (HBool, "hardbool");
+ function To_Boolean (X : HBool) returns Boolean is (Boolean (X));
+
+
+is compiled roughly like:
+
+.. code-block:: ada
+
+ function To_Boolean (X : HBool) returns Boolean is
+ begin
+ if X not in True | False then
+ raise Constraint_Error;
+ elsif X in True then
+ return True;
+ else
+ return False;
+ end if;
+ end To_Boolean;
+
Note that :switch:`-gnatVn` will disable even ``hardbool`` testing.
Analogous behavior is available as a GCC extension to the C and
-Objective C programming languages, through the ``hardbool`` attribute.
-For usage and more details on that attribute, see :title:`Using the
-GNU Compiler Collection (GCC)`.
+Objective C programming languages, through the ``hardbool`` attribute,
+with the difference that, instead of raising a Constraint_Error
+exception, when a hardened boolean variable is found to hold a value
+that stands for neither True nor False, the program traps. For usage
+and more details on that attribute, see :title:`Using the GNU Compiler
+Collection (GCC)`.
.. Control Flow Redundancy:
diff --git a/gcc/ada/gnat_rm.texi b/gcc/ada/gnat_rm.texi
index ff18456..dad0092 100644
--- a/gcc/ada/gnat_rm.texi
+++ b/gcc/ada/gnat_rm.texi
@@ -28920,17 +28920,37 @@ further remove checks found to be redundant.
For additional hardening, the @code{hardbool} @code{Machine_Attribute}
pragma can be used to annotate boolean types with representation
clauses, so that expressions of such types used as conditions are
-checked even when compiling with @code{-gnatVT}.
+checked even when compiling with @code{-gnatVT}:
@example
pragma Machine_Attribute (HBool, "hardbool");
+
+function To_Boolean (X : HBool) returns Boolean is (Boolean (X));
+@end example
+
+is compiled roughly like:
+
+@example
+function To_Boolean (X : HBool) returns Boolean is
+begin
+ if X not in True | False then
+ raise Constraint_Error;
+ elsif X in True then
+ return True;
+ else
+ return False;
+ end if;
+end To_Boolean;
@end example
Note that @code{-gnatVn} will disable even @code{hardbool} testing.
Analogous behavior is available as a GCC extension to the C and
-Objective C programming languages, through the @code{hardbool} attribute.
-For usage and more details on that attribute, see @cite{Using the GNU Compiler Collection (GCC)}.
+Objective C programming languages, through the @code{hardbool} attribute,
+with the difference that, instead of raising a Constraint_Error
+exception, when a hardened boolean variable is found to hold a value
+that stands for neither True nor False, the program traps. For usage
+and more details on that attribute, see @cite{Using the GNU Compiler Collection (GCC)}.
@c Control Flow Redundancy: