Replies: 1 comment 6 replies
|
Allocations don't have a type layout. It should be possible to dynamically allocate raw memory and then write and read various types from it. |
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
The document assumes that one can do bad things in language only using unsafe memory access using pointers. (Please point other possibilities if is there).
With unsafe memory access using pointers, we need to tackle following problems:
(Please point out, if there are other possible problems I am missing here).
There are 2 sections:
Solution with Composed region
Accessing Invalid Place
Motivating usecase:
storeinstruction doesn't need destination place to be in composed region. However, still the place should be valid i.e., it should not be allowed to store anIntin an allocation at offsetowhere aFloatshould have been stored.Solution:
Reading from memory that is not initialized yet
Motivating usecase:
loadfromsourcewheresourceis a valid place. However,sourceis not initialized yet.Solution (Part 1):
Solution (Part 2):
storeandmemory_copyinstruction actually writes to memory and thus update composed regions after these instructions.Invalid access to place
Motivating usecase: First
letaccess to a place and then attemptinginoutaccess to same place withoutend_access.Solution:
set > inout > let, then every composed region can have stack of accesses with invariant that "greater" access can't appear after "smaller" access.a.b, on inout access of b, replaces the composition records ofawith children ofawith all accesses ofa, plus inout access forbtoo.Alternate Solution
To me, it seems that initialization records and stack of access is being merged using composed region strategy and making it really complex. This merging of responsibility comes from one of @dabrahams line: "inout access replaces composed region of parent with children".
Also, it feels to me, we need to traverse composed region and type layout multiple times to implement the above strategy, that feels like duplicate work.
Instead, we can use 3 different data structures to solve these problems independently:
(I believe, Rust's Miri does some similar thing, but not sure on that).
Initialization byte-mask
var initialized: [Bool]in the allocation of same size as storage.i, we update theinitializedto be true.Type layout of allocation
Stack of access per region
(id, access_type)describing an access in allocation for any offset region.[(i, j), Access], describing access for bytes in[i, j).set > inout > let.let, then any access can be used for reading memory, otherwise only top access can be used.For subfield access:
Valid example for struct
A[B(0-4) C(4-8)]:[(0, 8), [base]] // base is initial access[(0, 4), [base]], [(4, 8), [base , let]][(0, 4), [base, let]], [(4, 8), [base , let, let]]Other than base access, every access is a let access for records being updated and hence a valid operation.
Valid example for struct
A[B(0-4) C(4-8)]:[(0, 8), [base]] // base is initial access[(0, 4), [base]], [(4, 8), [base , inout]][(0, 4), [base, let]], [(4, 8), [base , inout, inout]]Can create multiple inout access to same element in array, if top element in inout.
Invalid example for struct
A[B(0-4) C(4-8)]:[(0, 8), [base]] // base is initial access[(0, 4), [base]], [(4, 8), [base , inout]]inoutaccess for records being updated (for multiple record case) and thus, we are trying to create bigger inout than possible. Thus reject it.All reactions