Re: [go-nuts] Re: Question about safe unsafe.Pointer conversion rules

2020-06-19 Thread Iskander Sharipov
Many thanks Ian!

On Friday, June 19, 2020 at 11:32:19 PM UTC+3, Ian Lance Taylor wrote:
>
> On Fri, Jun 19, 2020 at 12:29 PM Iskander Sharipov  > wrote: 
> > 
> > I used this to write something like a runtime in Go. 
> > 
> > https://play.golang.org/p/6jYw5L3fy0B 
> > 
> > I was wondering whether this code can break at some point. 
>
> I don't see any way that that code could break, but I agree that it 
> does not follow the rules, so it is not guaranteed to always work. 
>
> Ian 
>
>
>
> > On Friday, June 19, 2020 at 10:04:01 PM UTC+3, Iskander Sharipov wrote: 
> >> 
> >> Hello! 
> >> 
> >> https://golang.org/src/runtime/symtab.go?s=7594:7745#L244 
> >> 
> >> type Func struct { 
> >> 
> >> opaque struct{} // unexported field to disallow conversions 
> >> 
> >> } 
> >> 
> >> 
> >> func (f *Func) raw() *_func { 
> >> 
> >> return (*_func)(unsafe.Pointer(f)) 
> >> 
> >> } 
> >> 
> >> 
> >> An empty struct Func (T1) is converted to a non-zero struct _func (T2). 
> >> 
> >> From the unsafe.Pointer documentation (
> https://golang.org/pkg/unsafe/#Pointer): 
> >> 
> >> > T1->T2 conversion is safe if T2 is no larger than T1 ... (shortened 
> for clarity) 
> >> 
> >> I'm guessing that documentation probably meant "allocation size", not 
> the "static type size"? 
> >> T1 pointer can be obtained from the T2 allocation, so it's allocation 
> size can be different from the unsafe.Sizeof(T1{}). 
> >> 
> >> Or it could be that runtime can violate that particular rule. 
> >> 
> >> My question: is it same to convert T1<->T2 even if 
> sizeof(T1) >> 
> >> Thank you in advance. 
> >> 
> >> 
> >> 
> > -- 
> > 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 golan...@googlegroups.com . 
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/fbeb324a-be55-4673-86a9-f6dc1046b0bco%40googlegroups.com.
>  
>
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/5b626a68-b2f0-4633-b2c4-ec33169a0863o%40googlegroups.com.


Re: [go-nuts] Re: Question about safe unsafe.Pointer conversion rules

2020-06-19 Thread Ian Lance Taylor
On Fri, Jun 19, 2020 at 12:29 PM Iskander Sharipov  wrote:
>
> I used this to write something like a runtime in Go.
>
> https://play.golang.org/p/6jYw5L3fy0B
>
> I was wondering whether this code can break at some point.

I don't see any way that that code could break, but I agree that it
does not follow the rules, so it is not guaranteed to always work.

Ian



> On Friday, June 19, 2020 at 10:04:01 PM UTC+3, Iskander Sharipov wrote:
>>
>> Hello!
>>
>> https://golang.org/src/runtime/symtab.go?s=7594:7745#L244
>>
>> type Func struct {
>>
>> opaque struct{} // unexported field to disallow conversions
>>
>> }
>>
>>
>> func (f *Func) raw() *_func {
>>
>> return (*_func)(unsafe.Pointer(f))
>>
>> }
>>
>>
>> An empty struct Func (T1) is converted to a non-zero struct _func (T2).
>>
>> From the unsafe.Pointer documentation 
>> (https://golang.org/pkg/unsafe/#Pointer):
>>
>> > T1->T2 conversion is safe if T2 is no larger than T1 ... (shortened for 
>> > clarity)
>>
>> I'm guessing that documentation probably meant "allocation size", not the 
>> "static type size"?
>> T1 pointer can be obtained from the T2 allocation, so it's allocation size 
>> can be different from the unsafe.Sizeof(T1{}).
>>
>> Or it could be that runtime can violate that particular rule.
>>
>> My question: is it same to convert T1<->T2 even if sizeof(T1)> but the memory was allocated for the bigger object?
>>
>> Thank you in advance.
>>
>>
>>
> --
> 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.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/fbeb324a-be55-4673-86a9-f6dc1046b0bco%40googlegroups.com.

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcWraFk_hrn%2BENC%3DWD%2BCLs_343hdONYhY%3D4myDNvuHatyQ%40mail.gmail.com.


[go-nuts] Re: Question about safe unsafe.Pointer conversion rules

2020-06-19 Thread Iskander Sharipov
I used this to write something like a runtime in Go.

https://play.golang.org/p/6jYw5L3fy0B

I was wondering whether this code can break at some point.

On Friday, June 19, 2020 at 10:04:01 PM UTC+3, Iskander Sharipov wrote:
>
> Hello!
>
> https://golang.org/src/runtime/symtab.go?s=7594:7745#L244
>
> type Func struct {
>
> opaque struct{} // unexported field to disallow conversions
>
> }
>
>
> func (f *Func) raw() *_func {
>
> return (*_func)(unsafe.Pointer(f))
>
> }
>
>
> An empty struct Func (T1) is converted to a non-zero struct _func (T2).
>
> From the unsafe.Pointer documentation (
> https://golang.org/pkg/unsafe/#Pointer):
>
> > T1->T2 conversion is safe if T2 is no larger than T1 ... (shortened for 
> clarity)
>
> I'm guessing that documentation probably meant "allocation size", not the 
> "static type size"?
> T1 pointer can be obtained from the T2 allocation, so it's allocation size 
> can be different from the unsafe.Sizeof(T1{}).
>
> Or it could be that runtime can violate that particular rule.
>
> *My question*: is it same to convert T1<->T2 even if 
> sizeof(T1)
> Thank you in advance.
>
>
>
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/fbeb324a-be55-4673-86a9-f6dc1046b0bco%40googlegroups.com.


[go-nuts] Re: Question about safe unsafe.Pointer conversion rules

2020-06-19 Thread Iskander Sharipov
s/same/safe/

The question is about whether it's safe to do the same conversion outside 
of the Go runtime.

On Friday, June 19, 2020 at 10:04:01 PM UTC+3, Iskander Sharipov wrote:
>
> Hello!
>
> https://golang.org/src/runtime/symtab.go?s=7594:7745#L244
>
> type Func struct {
>
> opaque struct{} // unexported field to disallow conversions
>
> }
>
>
> func (f *Func) raw() *_func {
>
> return (*_func)(unsafe.Pointer(f))
>
> }
>
>
> An empty struct Func (T1) is converted to a non-zero struct _func (T2).
>
> From the unsafe.Pointer documentation (
> https://golang.org/pkg/unsafe/#Pointer):
>
> > T1->T2 conversion is safe if T2 is no larger than T1 ... (shortened for 
> clarity)
>
> I'm guessing that documentation probably meant "allocation size", not the 
> "static type size"?
> T1 pointer can be obtained from the T2 allocation, so it's allocation size 
> can be different from the unsafe.Sizeof(T1{}).
>
> Or it could be that runtime can violate that particular rule.
>
> *My question*: is it same to convert T1<->T2 even if 
> sizeof(T1)
> Thank you in advance.
>
>
>
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/aff200fb-cbc9-44a1-a009-f8f9965fefefo%40googlegroups.com.