Fix issue 15645 - Tuple.slice() causes memory corruption.#3973
Fix issue 15645 - Tuple.slice() causes memory corruption.#3973tsbockman wants to merge 2 commits intodlang:masterfrom
Conversation
|
71088f2 to
2cd8ba5
Compare
|
Some other issues I noticed with
|
It can't because of the |
221943d to
0e66130
Compare
|
I think I finally got the pointer math right - it seems to be working on all platforms now. |
|
I posted a poll on dlang's "General" forum to help us decide which fix to use: http://forum.dlang.org/post/inswmiscuqirkhfqlhtd@forum.dlang.org |
|
Hmm... instead of inserting a padding at the beginning, wouldn't it be possible to adjust the alignment of all following members to preserve the original layout? The struct equivalents to the tuples are: struct Original {
int member1;
bool member2;
ubyte[3] __padding1;
string member3;
}
struct Sliced {
bool member2;
ubyte[3] __padding1;
string member3;
}The latter is equivalent to: align(1)
struct SlicedWithAlignment {
bool member2;
align(4) string member3;
}But there's a catch: copying this struct must be disallowed, because that would result in a struct with misaligned members (mostly harmless on x86, but fatal on some other architectures). It would work as long as it's only a reference to the original Tuple, though. |
|
@schuetzm I don't really see what problem you're trying to solve with the What advantage would this have over my current approach, to make up for the obvious limitations? |
|
@tsbockman The resulting Tuple would have the expected elements, i.e. the padding wouldn't show up in |
|
@schuetzm Using The publicly documented way to get a list of Writing code that depends upon the |
b135a0b to
37992a2
Compare
|
I have updated the docs and the |
37992a2 to
1728d20
Compare
|
@andralex @JakobOvrum @schveiguy @jmdavis Personally, my feeling is that for |
|
I understand why people are not enthusiastic about the approach I have taken in this PR, but in the absence of a better one, we really ought to move forward with this. |
|
ping @andralex |
If needed, insert a hidden padding member at the start of Tuple slices to ensure they have the same memory layout as their source.
… padding member.
1728d20 to
2ffe762
Compare
|
Ping @andralex . |
|
This has been ignored long enough. |
|
I was wondering whether a simpler solution using |
Setting all members to Using a large fixed alignment is not a solution in general, because there could always be a The implementation of this PR could probably be simplified somewhat if the argument for |
I'm thinking align(4), not align(1). How would that perform on the supported platforms? |
|
Using align(4), Tuple!(ubyte, ubyte, ubyte, ubyte) will have a size of 16 instead of 8. That is not pleasant at all. I think we should do either:
|
As I already pointed out in the very next sentence after the one you quoted, forcing any fixed alignment will violate the alignment requirements of any element that needs a larger alignment. In theory the alignment is unbounded. Even in practice, alignments up to In the general case, violating the alignment requirements of a type has the potential to crash the program or stymie the garbage collector. This is not a solution. |
This is an option, but my fix will still be needed during the deprecation period to prevent memory corruption for people who haven't updated their code to the new by-value slice yet.
This isn't really a solution by itself, IMO. It's allowing implementation details to leak into the public API in a major and unpleasant way. It could, however, be combined with your solution (1) as a simpler alternative to using my fix during the deprecation period for the by- |
|
Here I think we should just return by value. The whole business with suboptimal layout just for the sake of this function seems too much. |
I'll close this PR then. As this will be a major breaking change, replacement should do all of the following:
I'm a little sceptical that just removing the by- |
https://issues.dlang.org/show_bug.cgi?id=15645
If needed, insert a hidden padding member at the start of Tuple slices to ensure they have
the same memory layout as their source.
EDIT: @saurabhdas Has proposed an alternative fix.
I don't recommend it, because it is a breaking change. But, his way does have some other benefits: #3975