Fix Bugzilla 17148 - Copying from const(void)[] to void[] breaks immu… (#16583)

This commit is contained in:
Nick Treleaven 2024-06-17 10:11:52 +01:00 committed by GitHub
parent 4c82050e94
commit c6058f9b76
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 68 additions and 16 deletions

View file

@ -0,0 +1,15 @@
Copying from `const(void)[]` to `void[]` is disallowed with `-preview=fixImmutableConv`
If `const(void)[]` data contains tail `const` pointers, copying to `void[]`
can subsequently violate `const` data:
---
void f(int*[] a, const int*[] b)
{
void[] va = a;
const void[] vb = b;
va[] = vb[]; // fills `a` with pointers to const
*a[0] = 0; // const data mutated
}
---
Copying `vb` data to `va` is no longer allowed with the
`-preview=fixImmutableConv` switch.

View file

@ -957,8 +957,8 @@ dmd -cov -unittest myprog.d
"allow use of => for methods and top-level functions in addition to lambdas",
"https://dlang.org/spec/function.html#ShortenedFunctionBody", false, true),
Feature("fixImmutableConv", "fixImmutableConv",
"disallow functions with a mutable `void[]` parameter to be strongly pure",
"https://dlang.org/changelog/2.101.0.html#dmd.fix-immutable-conv"),
"disallow `void[]` data from holding immutable data",
"https://dlang.org/changelog/2.101.0.html#dmd.fix-immutable-conv, https://issues.dlang.org/show_bug.cgi?id=17148"),
Feature("systemVariables", "systemVariables",
"disable access to variables marked '@system' from @safe code",
"https://dlang.org/spec/attribute.html#system-variables"),

View file

@ -11431,17 +11431,38 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
{
if (sc.setUnsafe(false, exp.loc, "cannot copy `%s` to `%s` in `@safe` code", t2, t1))
return setError();
if (global.params.fixImmutableConv && !t2.implicitConvTo(t1))
{
error(exp.loc, "cannot copy `%s` to `%s`", t2.toChars(), t1.toChars());
errorSupplemental(exp.loc,
"Source data has incompatible type qualifier(s)");
errorSupplemental(exp.loc, "Use `cast(%s)` to force copy", t1.toChars());
return setError();
}
}
}
else
{
if (exp.e1.op == EXP.slice &&
(t1.ty == Tarray || t1.ty == Tsarray) &&
t1.nextOf().toBasetype().ty == Tvoid &&
sc.setUnsafePreview(FeatureState.default_, false, exp.loc,
t1.nextOf().toBasetype().ty == Tvoid)
{
if (sc.setUnsafePreview(FeatureState.default_, false, exp.loc,
"cannot copy `%s` to `%s` in `@safe` code", t2, t1))
return setError();
if (global.params.fixImmutableConv && !t2.implicitConvTo(t1))
{
error(exp.loc, "cannot copy `%s` to `%s`",
t2.toChars(), t1.toChars());
errorSupplemental(exp.loc,
"Source data has incompatible type qualifier(s)");
errorSupplemental(exp.loc, "Use `cast(%s)` to force copy", t1.toChars());
return setError();
}
}
if (exp.op == EXP.blit)
e2x = e2x.castTo(sc, exp.e1.type);
else

View file

@ -16,7 +16,7 @@ Upcoming language changes listed by -preview=name:
=nosharedaccess disable access to shared memory objects (https://dlang.org/spec/const3.html#shared)
=in `in` on parameters means `scope const [ref]` and accepts rvalues (https://dlang.org/spec/function.html#in-params)
=inclusiveincontracts 'in' contracts of overridden methods must be a superset of parent contract (https://dlang.org/changelog/2.095.0.html#inclusive-incontracts)
=fixImmutableConv disallow functions with a mutable `void[]` parameter to be strongly pure (https://dlang.org/changelog/2.101.0.html#dmd.fix-immutable-conv)
=fixImmutableConv disallow `void[]` data from holding immutable data (https://dlang.org/changelog/2.101.0.html#dmd.fix-immutable-conv, https://issues.dlang.org/show_bug.cgi?id=17148)
=systemVariables disable access to variables marked '@system' from @safe code (https://dlang.org/spec/attribute.html#system-variables)
----
*/

View file

@ -1,7 +1,13 @@
/* REQUIRED_ARGS: -preview=fixImmutableConv
TEST_OUTPUT:
---
fail_compilation/test15660.d(20): Error: cannot implicitly convert expression `f(v)` of type `int[]` to `immutable(int[])`
fail_compilation/test15660.d(26): Error: cannot implicitly convert expression `f(v)` of type `int[]` to `immutable(int[])`
fail_compilation/test15660.d(34): Error: cannot copy `const(void)[]` to `void[]`
fail_compilation/test15660.d(34): Source data has incompatible type qualifier(s)
fail_compilation/test15660.d(34): Use `cast(void[])` to force copy
fail_compilation/test15660.d(36): Error: cannot copy `const(int*)[]` to `void[]`
fail_compilation/test15660.d(36): Source data has incompatible type qualifier(s)
fail_compilation/test15660.d(36): Use `cast(void[])` to force copy
---
*/
@ -19,3 +25,13 @@ void main()
void[] v;
immutable x = f(v);
}
// https://issues.dlang.org/show_bug.cgi?id=17148
void f(int*[] a, const int*[] b) @system
{
void[] a1 = a;
const(void)[] b1 = b;
a1[] = b1[];
*a[0] = 0; //modify const data
a1[] = new const(int*)[2];
}

View file

@ -104,7 +104,7 @@ T emplace(T, Args...)(T chunk, auto ref Args args)
// Initialize the object in its pre-ctor state
const initializer = __traits(initSymbol, T);
(() @trusted { (cast(void*) chunk)[0 .. initializer.length] = initializer[]; })();
() @trusted { (cast(void*) chunk)[0 .. initializer.length] = cast(void[]) initializer[]; }();
static if (isInnerClass!T)
{
@ -2683,7 +2683,7 @@ T _d_newThrowable(T)() @trusted
debug(PRINTF) printf(" p = %p\n", p);
// initialize it
p[0 .. init.length] = init[];
p[0 .. init.length] = cast(void[]) init[];
import core.internal.traits : hasIndirections;
if (hasIndirections!T)
@ -2776,7 +2776,7 @@ if (is(T == class))
}
// initialize it
p[0 .. init.length] = init[];
p[0 .. init.length] = cast(void[]) init[];
debug(PRINTF) printf("initialization done\n");
return cast(T) p;

View file

@ -2033,7 +2033,7 @@ extern (C) void thread_init() @nogc nothrow
status = sem_init( &suspendCount, 0, 0 );
assert( status == 0 );
}
_mainThreadStore[] = __traits(initSymbol, Thread)[];
_mainThreadStore[] = cast(void[]) __traits(initSymbol, Thread)[];
Thread.sm_main = attachThread((cast(Thread)_mainThreadStore.ptr).__ctor());
}

View file

@ -774,7 +774,7 @@ package void thread_term_tpl(ThreadT, MainThreadStore)(ref MainThreadStore _main
// destruct manually as object.destroy is not @nogc
(cast(ThreadT) cast(void*) ThreadBase.sm_main).__dtor();
_d_monitordelete_nogc(ThreadBase.sm_main);
_mainThreadStore[] = __traits(initSymbol, ThreadT)[];
_mainThreadStore[] = cast(void[]) __traits(initSymbol, ThreadT)[];
ThreadBase.sm_main = null;
assert(ThreadBase.sm_tbeg && ThreadBase.sm_tlen == 1);

View file

@ -688,7 +688,7 @@ extern (C) inout(void[]) _aaValues(inout AA aa, const size_t keysz, const size_t
{
if (!b.filled)
continue;
pval[0 .. valsz] = b.entry[off .. valsz + off];
pval[0 .. valsz] = cast(void[]) b.entry[off .. valsz + off];
pval += valsz;
}
// postblit is done in object.values
@ -710,7 +710,7 @@ extern (C) inout(void[]) _aaKeys(inout AA aa, const size_t keysz, const TypeInfo
{
if (!b.filled)
continue;
pkey[0 .. keysz] = b.entry[0 .. keysz];
pkey[0 .. keysz] = cast(void[]) b.entry[0 .. keysz];
pkey += keysz;
}
// postblit is done in object.keys

View file

@ -129,7 +129,7 @@ extern (C) Object _d_newclass(const ClassInfo ci) @weak
}
// initialize it
p[0 .. init.length] = init[];
p[0 .. init.length] = cast(void[]) init[];
debug(PRINTF) printf("initialization done\n");
return cast(Object) p;
@ -1294,7 +1294,7 @@ extern (C) void rt_finalize2(void* p, bool det = true, bool resetMemory = true)
if (resetMemory)
{
auto w = (*pc).initializer;
p[0 .. w.length] = w[];
p[0 .. w.length] = cast(void[]) w[];
}
}
catch (Exception e)