| 1 |
// Copyright 2025 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package main |
| 6 |
|
| 7 |
/* |
| 8 |
#include <stdlib.h> |
| 9 |
|
| 10 |
int* test() { |
| 11 |
return malloc(sizeof(int)); |
| 12 |
} |
| 13 |
|
| 14 |
void clearStack(int n) { |
| 15 |
if (n > 0) { |
| 16 |
clearStack(n - 1); |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
*/ |
| 21 |
import "C" |
| 22 |
|
| 23 |
type S struct { |
| 24 |
p *C.int |
| 25 |
} |
| 26 |
|
| 27 |
var p *S |
| 28 |
|
| 29 |
//go:noinline |
| 30 |
func F() { |
| 31 |
p = &S{p: C.test()} |
| 32 |
} |
| 33 |
|
| 34 |
func clearStack(n int) { |
| 35 |
if n > 0 { |
| 36 |
clearStack(n - 1) |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
func main() { |
| 41 |
// Test should pass: memory allocated by C does not leak |
| 42 |
// because a Go global variable points to it. |
| 43 |
F() |
| 44 |
clearStack(100) |
| 45 |
C.clearStack(100) |
| 46 |
} |