aarch64,arm: Fix branch-protection= parsing
commit321477fc3a0f8de18c4452f431309f896ae3a854
authorSzabolcs Nagy <szabolcs.nagy@arm.com>
Thu, 15 Jun 2023 16:15:09 +0000 (15 17:15 +0100)
committerSzabolcs Nagy <szabolcs.nagy@arm.com>
Tue, 12 Dec 2023 15:48:41 +0000 (12 15:48 +0000)
treed3ced6aa2df254b5512b63d8387b468f89ea23c6
parentd83acace704927ee351968258c8a8cd39e305d03
aarch64,arm: Fix branch-protection= parsing

Refactor the parsing to have a single API and fix a few parsing issues:

- Different handling of "bti+none" and "none+bti": these should be
  rejected because "none" can only appear alone.

- Accepted empty strings such as "bti++pac-ret" or "bti+", this bug
  was caused by using strtok_r.

- Memory got leaked (str_root was never freed). And two buffers got
  allocated when one is enough.

The callbacks now have no failure mode, only parsing can fail and
all failures are handled locally.  The "-mbranch-protection=" vs
"target("branch-protection=")" difference in the error message is
handled by a separate argument to aarch_validate_mbranch_protection.

gcc/ChangeLog:

* config/aarch64/aarch64.cc (aarch64_override_options): Update.
(aarch64_handle_attr_branch_protection): Update.
* config/arm/aarch-common-protos.h (aarch_parse_branch_protection):
Remove.
(aarch_validate_mbranch_protection): Add new argument.
* config/arm/aarch-common.cc (aarch_handle_no_branch_protection):
Update.
(aarch_handle_standard_branch_protection): Update.
(aarch_handle_pac_ret_protection): Update.
(aarch_handle_pac_ret_leaf): Update.
(aarch_handle_pac_ret_b_key): Update.
(aarch_handle_bti_protection): Update.
(aarch_parse_branch_protection): Remove.
(next_tok): New.
(aarch_validate_mbranch_protection): Rewrite.
* config/arm/aarch-common.h (struct aarch_branch_protect_type):
Add field "alone".
* config/arm/arm.cc (arm_configure_build_target): Update.

gcc/testsuite/ChangeLog:

* gcc.target/aarch64/branch-protection-attr.c: Update.
* gcc.target/aarch64/branch-protection-option.c: Update.
gcc/config/aarch64/aarch64.cc
gcc/config/arm/aarch-common-protos.h
gcc/config/arm/aarch-common.cc
gcc/config/arm/aarch-common.h
gcc/config/arm/arm.cc
gcc/testsuite/gcc.target/aarch64/branch-protection-attr.c
gcc/testsuite/gcc.target/aarch64/branch-protection-option.c