package main

// Convert an *A to a *B, using a data race.
// Works for any types A and B.
func cast(a *A) *B {
var x interface{}

go func() {
for i := 0; i < 100000; i++ {
x = a
}
}()
go func() {
var b *B
for i := 0; i < 100000; i++ {
x = b
}
}()

for i := 0; i < 100000; i++ {
b, ok := x.(*B)
if ok && b != nil {
// Empty interfaces are 2 words, a type and a value.
// We managed to read the type of b, but the value of a.
return b
}
}
panic("cast failed")
}

type A *int
type B int

func main() {
var a *A = new(A)
b := cast(a)
*b = -1
_ = **a // seg faults at address -1.
}


On Monday, December 4, 2017 at 4:57:42 AM UTC-8, Erik Quanstrom wrote:
>
> i should have mentioned the race detector found nothing.  jan, can you 
> give an example of a go program
> setting a pointer to -1 without using unsafe?  this requires the gc to 
> have free'd something that is still live,
> doesn't it?
>
> - erik
>
> On Monday, December 4, 2017 at 2:58:22 AM UTC-8, Peter Waller wrote:
>>
>> Some worthwhile reading: 
>> https://software.intel.com/en-us/blogs/2013/01/06/benign-data-races-what-could-possibly-go-wrong
>>
>> Races are important, and if your program has a race, otherwise 
>> impossible-seeming things can happen. First ruling out races is a 
>> worthwhile activity.
>>
>> In terms of debugging it, it's difficult, but it will help a lot if you 
>> can make it crash predictably. Moreso if you can share a minimum reproducer.
>>
>> When I've been in such a situation as yours, I've found that once I can 
>> get it to crash predictably, it is often a short path from there to a 
>> solution.
>>
>>
>> On 4 December 2017 at 06:46, <quan...@gmail.com> wrote:
>>
>>> a program not using unsafe cannot set a pointer to -1, even if there is 
>>> a race, right?
>>>
>>> - erik
>>>
>>>
>>> On Sunday, December 3, 2017 at 10:20:44 PM UTC-8, Jan Mercl wrote:
>>>>
>>>> On Mon, Dec 4, 2017 at 7:03 AM <quan...@gmail.com> wrote:
>>>>
>>>> > does anyone have any idea what's going on here, or some hints on 
>>>> debugging this?
>>>>
>>>> What does the race detector say?
>>>>
>>>> -- 
>>>>
>>>> -j
>>>>
>>> -- 
>>> You received this message because you are subscribed to the Google 
>>> Groups "golang-nuts" group.
>>> To unsubscribe from this group and stop receiving emails from it, send 
>>> an email to golang-nuts...@googlegroups.com.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to