rpc: client stream: dispose private data on stream dispose

If we call virStreamFinish and virStreamAbort from 2 distinct
threads for example we can have access to freed memory.
Because when virStreamFinish finishes for example virStreamAbort
yet to be finished and it access virNetClientStreamPtr object
in stream->privateData.

Also it does not make sense to clear @driver field. After
stream is finished/aborted it is better to have appropriate
error message instead of "unsupported error".

This commit reverts [1] or virNetClientStreamPtr and
virStreamPtr will never be unrefed due to cyclic dependency.
Before this patch we don't have leaks because all execution
paths we call virStreamFinish or virStreamAbort.

[1] 8b6ffe40 : virNetClientStreamNew: Track origin stream

Signed-off-by: Nikolay Shirokovskiy <nshirokovskiy@virtuozzo.com>
This commit is contained in:
Nikolay Shirokovskiy 2019-02-07 15:58:46 +03:00 committed by Michal Privoznik
parent d962f56fb6
commit fbcb73866b
6 changed files with 11 additions and 16 deletions

View File

@ -763,6 +763,8 @@ virStreamDispose(void *obj)
virStreamPtr st = obj;
VIR_DEBUG("release dev %p", st);
if (st->ff)
st->ff(st->privateData);
virObjectUnref(st->conn);
}

View File

@ -665,6 +665,7 @@ struct _virStream {
virStreamDriverPtr driver;
void *privateData;
virFreeCallback ff;
};
/**

View File

@ -5835,9 +5835,6 @@ remoteStreamCloseInt(virStreamPtr st, bool streamAbort)
priv->localUses--;
virNetClientRemoveStream(priv->client, privst);
virObjectUnref(privst);
st->privateData = NULL;
st->driver = NULL;
remoteDriverUnlock(priv);
return ret;
@ -6177,8 +6174,7 @@ remoteDomainMigratePrepareTunnel3(virConnectPtr dconn,
memset(&args, 0, sizeof(args));
memset(&ret, 0, sizeof(ret));
if (!(netst = virNetClientStreamNew(st,
priv->remoteProgram,
if (!(netst = virNetClientStreamNew(priv->remoteProgram,
REMOTE_PROC_DOMAIN_MIGRATE_PREPARE_TUNNEL3,
priv->counter,
false)))
@ -6191,6 +6187,7 @@ remoteDomainMigratePrepareTunnel3(virConnectPtr dconn,
st->driver = &remoteStreamDrv;
st->privateData = netst;
st->ff = virObjectFreeCallback;
args.cookie_in.cookie_in_val = (char *)cookiein;
args.cookie_in.cookie_in_len = cookieinlen;
@ -7142,8 +7139,7 @@ remoteDomainMigratePrepareTunnel3Params(virConnectPtr dconn,
goto cleanup;
}
if (!(netst = virNetClientStreamNew(st,
priv->remoteProgram,
if (!(netst = virNetClientStreamNew(priv->remoteProgram,
REMOTE_PROC_DOMAIN_MIGRATE_PREPARE_TUNNEL3_PARAMS,
priv->counter,
false)))
@ -7156,6 +7152,7 @@ remoteDomainMigratePrepareTunnel3Params(virConnectPtr dconn,
st->driver = &remoteStreamDrv;
st->privateData = netst;
st->ff = virObjectFreeCallback;
if (call(dconn, priv, 0, REMOTE_PROC_DOMAIN_MIGRATE_PREPARE_TUNNEL3_PARAMS,
(xdrproc_t) xdr_remote_domain_migrate_prepare_tunnel3_params_args,

View File

@ -1804,7 +1804,7 @@ elsif ($mode eq "client") {
if ($call->{streamflag} ne "none") {
print "\n";
print " if (!(netst = virNetClientStreamNew(st, priv->remoteProgram, $call->{constname}, priv->counter, sparse)))\n";
print " if (!(netst = virNetClientStreamNew(priv->remoteProgram, $call->{constname}, priv->counter, sparse)))\n";
print " goto done;\n";
print "\n";
print " if (virNetClientAddStream(priv->client, netst) < 0) {\n";
@ -1814,6 +1814,7 @@ elsif ($mode eq "client") {
print "\n";
print " st->driver = &remoteStreamDrv;\n";
print " st->privateData = netst;\n";
print " st->ff = virObjectFreeCallback;\n";
}
if ($call->{ProcName} eq "SupportsFeature") {

View File

@ -34,8 +34,6 @@ VIR_LOG_INIT("rpc.netclientstream");
struct _virNetClientStream {
virObjectLockable parent;
virStreamPtr stream; /* Reverse pointer to parent stream */
virNetClientProgramPtr prog;
int proc;
unsigned serial;
@ -133,8 +131,7 @@ virNetClientStreamEventTimer(int timer ATTRIBUTE_UNUSED, void *opaque)
}
virNetClientStreamPtr virNetClientStreamNew(virStreamPtr stream,
virNetClientProgramPtr prog,
virNetClientStreamPtr virNetClientStreamNew(virNetClientProgramPtr prog,
int proc,
unsigned serial,
bool allowSkip)
@ -147,7 +144,6 @@ virNetClientStreamPtr virNetClientStreamNew(virStreamPtr stream,
if (!(st = virObjectLockableNew(virNetClientStreamClass)))
return NULL;
st->stream = virObjectRef(stream);
st->prog = virObjectRef(prog);
st->proc = proc;
st->serial = serial;
@ -167,7 +163,6 @@ void virNetClientStreamDispose(void *obj)
virNetMessageFree(msg);
}
virObjectUnref(st->prog);
virObjectUnref(st->stream);
}
bool virNetClientStreamMatches(virNetClientStreamPtr st,

View File

@ -30,8 +30,7 @@ typedef virNetClientStream *virNetClientStreamPtr;
typedef void (*virNetClientStreamEventCallback)(virNetClientStreamPtr stream,
int events, void *opaque);
virNetClientStreamPtr virNetClientStreamNew(virStreamPtr stream,
virNetClientProgramPtr prog,
virNetClientStreamPtr virNetClientStreamNew(virNetClientProgramPtr prog,
int proc,
unsigned serial,
bool allowSkip);