|
| 1 | +Subject: [PATCH] cmd/cgo: fix unaligned arguments typedmemmove crash on iOS (go1.25.3) |
| 2 | + |
| 3 | +--- |
| 4 | + src/cmd/cgo/out.go | 9 ++++++++- |
| 5 | + 1 file changed, 8 insertions(+), 1 deletion(-) |
| 6 | + |
| 7 | +diff --git a/src/cmd/cgo/out.go b/src/cmd/cgo/out.go |
| 8 | +index 10870b7c85..3027217b3f 100644 |
| 9 | +--- a/src/cmd/cgo/out.go |
| 10 | ++++ b/src/cmd/cgo/out.go |
| 11 | +@@ -949,6 +949,8 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) { |
| 12 | + fmt.Fprintf(gotype, "struct {\n") |
| 13 | + off := int64(0) |
| 14 | + npad := 0 |
| 15 | ++ // the align is at least 1 (for char) |
| 16 | ++ maxAlign := int64(1) |
| 17 | + argField := func(typ ast.Expr, namePat string, args ...interface{}) { |
| 18 | + name := fmt.Sprintf(namePat, args...) |
| 19 | + t := p.cgoType(typ) |
| 20 | +@@ -963,6 +965,11 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) { |
| 21 | + noSourceConf.Fprint(gotype, fset, typ) |
| 22 | + fmt.Fprintf(gotype, "\n") |
| 23 | + off += t.Size |
| 24 | ++ // keep track of the maximum alignment among all fields |
| 25 | ++ // so that we can align the struct correctly |
| 26 | ++ if t.Align > maxAlign { |
| 27 | ++ maxAlign = t.Align |
| 28 | ++ } |
| 29 | + } |
| 30 | + if fn.Recv != nil { |
| 31 | + argField(fn.Recv.List[0].Type, "recv") |
| 32 | +@@ -1051,7 +1058,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) { |
| 33 | + // string.h for memset, and is also robust to C++ |
| 34 | + // types with constructors. Both GCC and LLVM optimize |
| 35 | + // this into just zeroing _cgo_a. |
| 36 | +- fmt.Fprintf(fgcc, "\ttypedef %s %v _cgo_argtype;\n", ctype.String(), p.packedAttribute()) |
| 37 | ++ fmt.Fprintf(fgcc, "\ttypedef %s %v __attribute__((aligned(%d))) _cgo_argtype;\n", ctype.String(), p.packedAttribute(), maxAlign) |
| 38 | + fmt.Fprintf(fgcc, "\tstatic _cgo_argtype _cgo_zero;\n") |
| 39 | + fmt.Fprintf(fgcc, "\t_cgo_argtype _cgo_a = _cgo_zero;\n") |
| 40 | + if gccResult != "void" && (len(fntype.Results.List) > 1 || len(fntype.Results.List[0].Names) > 1) { |
| 41 | +-- |
| 42 | +2.39.0 |
0 commit comments