From 3932785450fb74a886b8eb3d6f889de9766b7e8e Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Wed, 23 Dec 2020 13:54:35 -0800 Subject: [PATCH] compiler: nil-checked pointers and index expressions can trap The compiler was treating indirections through pointers that had been explicitly checked against nil and slice and string index expressions as non-trapping memory references. That is true for ordinary Go programs, but it isn't true if the programs construct their own memory addresses. In particular it isn't true for the kinds of programs that want to use runtime.SetPanicOnFault. The effect of this will be slightly larger binaries, due to additional exception information, and perhaps slightly less optimization. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/279960 --- gcc/go/gofrontend/MERGE | 2 +- gcc/go/gofrontend/expressions.cc | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/gcc/go/gofrontend/MERGE b/gcc/go/gofrontend/MERGE index fbac942ad42..600d9769624 100644 --- a/gcc/go/gofrontend/MERGE +++ b/gcc/go/gofrontend/MERGE @@ -1,4 +1,4 @@ -d238487d5c6e0c7f12c38305060fba8b7ec3605f +8d49adead59b8103f3bfeebd53ee508eda5ee94a The first line of this file holds the git revision number of the last merge done from the gofrontend repository. diff --git a/gcc/go/gofrontend/expressions.cc b/gcc/go/gofrontend/expressions.cc index adc1ebb4643..17b4cfd2c19 100644 --- a/gcc/go/gofrontend/expressions.cc +++ b/gcc/go/gofrontend/expressions.cc @@ -5334,7 +5334,6 @@ Unary_expression::do_get_backend(Translate_context* context) { go_assert(this->expr_->type()->points_to() != NULL); - bool known_valid = false; Type* ptype = this->expr_->type()->points_to(); Btype* pbtype = ptype->get_backend(gogo); switch (this->requires_nil_check(gogo)) @@ -5375,14 +5374,12 @@ Unary_expression::do_get_backend(Translate_context* context) compare, bcrash, ubexpr, loc); - known_valid = true; break; } case NIL_CHECK_DEFAULT: go_unreachable(); } - ret = gogo->backend()->indirect_expression(pbtype, bexpr, - known_valid, loc); + ret = gogo->backend()->indirect_expression(pbtype, bexpr, false, loc); } break; @@ -13339,7 +13336,8 @@ Array_index_expression::do_get_backend(Translate_context* context) Type* ele_type = this->array_->type()->array_type()->element_type(); Btype* ele_btype = ele_type->get_backend(gogo); - ret = gogo->backend()->indirect_expression(ele_btype, ptr, true, loc); + ret = gogo->backend()->indirect_expression(ele_btype, ptr, false, + loc); } return ret; } @@ -13679,7 +13677,7 @@ String_index_expression::do_get_backend(Translate_context* context) { ptr = gogo->backend()->pointer_offset_expression(ptr, bstart, loc); Btype* ubtype = Type::lookup_integer_type("uint8")->get_backend(gogo); - return gogo->backend()->indirect_expression(ubtype, ptr, true, loc); + return gogo->backend()->indirect_expression(ubtype, ptr, false, loc); } Expression* end = NULL; -- 2.30.2