www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - hack on safe functions

reply "Pavel" <phondogo gmail.com> writes:
struct T {
	ubyte[0x12FDB5/*address of local x in main + 1*/] x;
}

 safe void test() {
	T* t = null;
	t.x[$-1] = 99;
}

void main() {
	ubyte x = 0;
	writeln("\n", &x, ' ', x);
	test();
	writeln("\n", x);
}
Feb 03 2014
next sibling parent "Dicebot" <public dicebot.lv> writes:
On Monday, 3 February 2014 at 12:41:30 UTC, Pavel wrote:
 struct T {
 	ubyte[0x12FDB5/*address of local x in main + 1*/] x;
 }

  safe void test() {
 	T* t = null;
 	t.x[$-1] = 99;
 }

 void main() {
 	ubyte x = 0;
 	writeln("\n", &x, ' ', x);
 	test();
 	writeln("\n", x);
 }

This has been recently mentioned if thread about null dereference "safety" :)
Feb 03 2014
prev sibling next sibling parent "Pavel" <phondogo gmail.com> writes:
Some suggesting to compiler checking for that case.

If talking about Linux OS it reserves first page (4kb on 32bit 
cpu, 8kb on 64) for null fault case (try to dereference pointer 
in that memory addresses will cause segmentation fault or smth 
like this).

So compiler can check (at compile time) each structure field 
access for  dereference with ofset > OS page size add code for 
check pointer of structure for null. Or when trying to get 
pointer of field that is in the range of page size, but field 
type size + ofset of this field > page size.

In this solution there will be very little overhead cause it is 
very rare case that structures have size > page size.

Sorry for my bad english. :)
Feb 03 2014
prev sibling parent "Pavel" <phondogo gmail.com> writes:
On Tuesday, 4 February 2014 at 06:08:59 UTC, Pavel wrote:
 Some suggesting to compiler checking for that case.

 If talking about Linux OS it reserves first page (4kb on 32bit 
 cpu, 8kb on 64) for null fault case (try to dereference pointer 
 in that memory addresses will cause segmentation fault or smth 
 like this).

 So compiler can check (at compile time) each structure field 
 access for  dereference with ofset > OS page size add code for 
 check pointer of structure for null. Or when trying to get 
 pointer of field that is in the range of page size, but field 
 type size + ofset of this field > page size.

 In this solution there will be very little overhead cause it is 
 very rare case that structures have size > page size.

 Sorry for my bad english. :)

In that case there will be no need for dissallowing null pointers in safe code.
Feb 03 2014