From 5eecea375f62f27ca5cd59a35822957b3df30279 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Fri, 22 May 2020 21:01:02 -0400 Subject: [PATCH] wasi: simplify WASI memory management This commit migrates the WASI C++ code from UncheckedCalloc() to std::vectors. PR-URL: https://github.com/nodejs/node/pull/33525 Reviewed-By: Anna Henningsen Reviewed-By: David Carlier Reviewed-By: Jiawen Geng Reviewed-By: Ben Noordhuis --- src/node_wasi.cc | 116 ++++++++++++++--------------------------------- 1 file changed, 35 insertions(+), 81 deletions(-) diff --git a/src/node_wasi.cc b/src/node_wasi.cc index dad8bfc07674de..edc1e206966c07 100644 --- a/src/node_wasi.cc +++ b/src/node_wasi.cc @@ -661,27 +661,24 @@ void WASI::FdPread(const FunctionCallbackInfo& args) { iovs_ptr, iovs_len * UVWASI_SERDES_SIZE_iovec_t); CHECK_BOUNDS_OR_RETURN(args, mem_size, nread_ptr, UVWASI_SERDES_SIZE_size_t); - uvwasi_iovec_t* iovs = UncheckedCalloc(iovs_len); + std::vector iovs(iovs_len); uvwasi_errno_t err; - if (iovs == nullptr) { - args.GetReturnValue().Set(UVWASI_ENOMEM); - return; - } - - err = uvwasi_serdes_readv_iovec_t(memory, mem_size, iovs_ptr, iovs, iovs_len); + err = uvwasi_serdes_readv_iovec_t(memory, + mem_size, + iovs_ptr, + iovs.data(), + iovs_len); if (err != UVWASI_ESUCCESS) { - free(iovs); args.GetReturnValue().Set(err); return; } uvwasi_size_t nread; - err = uvwasi_fd_pread(&wasi->uvw_, fd, iovs, iovs_len, offset, &nread); + err = uvwasi_fd_pread(&wasi->uvw_, fd, iovs.data(), iovs_len, offset, &nread); if (err == UVWASI_ESUCCESS) uvwasi_serdes_write_size_t(memory, nread_ptr, nread); - free(iovs); args.GetReturnValue().Set(err); } @@ -764,31 +761,29 @@ void WASI::FdPwrite(const FunctionCallbackInfo& args) { mem_size, nwritten_ptr, UVWASI_SERDES_SIZE_size_t); - uvwasi_ciovec_t* iovs = UncheckedCalloc(iovs_len); + std::vector iovs(iovs_len); uvwasi_errno_t err; - if (iovs == nullptr) { - args.GetReturnValue().Set(UVWASI_ENOMEM); - return; - } - err = uvwasi_serdes_readv_ciovec_t(memory, mem_size, iovs_ptr, - iovs, + iovs.data(), iovs_len); if (err != UVWASI_ESUCCESS) { - free(iovs); args.GetReturnValue().Set(err); return; } uvwasi_size_t nwritten; - err = uvwasi_fd_pwrite(&wasi->uvw_, fd, iovs, iovs_len, offset, &nwritten); + err = uvwasi_fd_pwrite(&wasi->uvw_, + fd, + iovs.data(), + iovs_len, + offset, + &nwritten); if (err == UVWASI_ESUCCESS) uvwasi_serdes_write_size_t(memory, nwritten_ptr, nwritten); - free(iovs); args.GetReturnValue().Set(err); } @@ -814,27 +809,24 @@ void WASI::FdRead(const FunctionCallbackInfo& args) { iovs_ptr, iovs_len * UVWASI_SERDES_SIZE_iovec_t); CHECK_BOUNDS_OR_RETURN(args, mem_size, nread_ptr, UVWASI_SERDES_SIZE_size_t); - uvwasi_iovec_t* iovs = UncheckedCalloc(iovs_len); + std::vector iovs(iovs_len); uvwasi_errno_t err; - if (iovs == nullptr) { - args.GetReturnValue().Set(UVWASI_ENOMEM); - return; - } - - err = uvwasi_serdes_readv_iovec_t(memory, mem_size, iovs_ptr, iovs, iovs_len); + err = uvwasi_serdes_readv_iovec_t(memory, + mem_size, + iovs_ptr, + iovs.data(), + iovs_len); if (err != UVWASI_ESUCCESS) { - free(iovs); args.GetReturnValue().Set(err); return; } uvwasi_size_t nread; - err = uvwasi_fd_read(&wasi->uvw_, fd, iovs, iovs_len, &nread); + err = uvwasi_fd_read(&wasi->uvw_, fd, iovs.data(), iovs_len, &nread); if (err == UVWASI_ESUCCESS) uvwasi_serdes_write_size_t(memory, nread_ptr, nread); - free(iovs); args.GetReturnValue().Set(err); } @@ -996,31 +988,24 @@ void WASI::FdWrite(const FunctionCallbackInfo& args) { mem_size, nwritten_ptr, UVWASI_SERDES_SIZE_size_t); - uvwasi_ciovec_t* iovs = UncheckedCalloc(iovs_len); + std::vector iovs(iovs_len); uvwasi_errno_t err; - if (iovs == nullptr) { - args.GetReturnValue().Set(UVWASI_ENOMEM); - return; - } - err = uvwasi_serdes_readv_ciovec_t(memory, mem_size, iovs_ptr, - iovs, + iovs.data(), iovs_len); if (err != UVWASI_ESUCCESS) { - free(iovs); args.GetReturnValue().Set(err); return; } uvwasi_size_t nwritten; - err = uvwasi_fd_write(&wasi->uvw_, fd, iovs, iovs_len, &nwritten); + err = uvwasi_fd_write(&wasi->uvw_, fd, iovs.data(), iovs_len, &nwritten); if (err == UVWASI_ESUCCESS) uvwasi_serdes_write_size_t(memory, nwritten_ptr, nwritten); - free(iovs); args.GetReturnValue().Set(err); } @@ -1435,21 +1420,8 @@ void WASI::PollOneoff(const FunctionCallbackInfo& args) { mem_size, nevents_ptr, UVWASI_SERDES_SIZE_size_t); - uvwasi_subscription_t* in = - UncheckedCalloc(nsubscriptions); - - if (in == nullptr) { - args.GetReturnValue().Set(UVWASI_ENOMEM); - return; - } - - uvwasi_event_t* out = UncheckedCalloc(nsubscriptions); - - if (out == nullptr) { - free(in); - args.GetReturnValue().Set(UVWASI_ENOMEM); - return; - } + std::vector in(nsubscriptions); + std::vector out(nsubscriptions); for (uint32_t i = 0; i < nsubscriptions; ++i) { uvwasi_serdes_read_subscription_t(memory, in_ptr, &in[i]); @@ -1458,8 +1430,8 @@ void WASI::PollOneoff(const FunctionCallbackInfo& args) { uvwasi_size_t nevents; uvwasi_errno_t err = uvwasi_poll_oneoff(&wasi->uvw_, - in, - out, + in.data(), + out.data(), nsubscriptions, &nevents); if (err == UVWASI_ESUCCESS) { @@ -1471,8 +1443,6 @@ void WASI::PollOneoff(const FunctionCallbackInfo& args) { } } - free(in); - free(out); args.GetReturnValue().Set(err); } @@ -1563,20 +1533,13 @@ void WASI::SockRecv(const FunctionCallbackInfo& args) { ri_data_len * UVWASI_SERDES_SIZE_iovec_t); CHECK_BOUNDS_OR_RETURN(args, mem_size, ro_datalen_ptr, 4); CHECK_BOUNDS_OR_RETURN(args, mem_size, ro_flags_ptr, 4); - uvwasi_iovec_t* ri_data = UncheckedCalloc(ri_data_len); - - if (ri_data == nullptr) { - args.GetReturnValue().Set(UVWASI_ENOMEM); - return; - } - + std::vector ri_data(ri_data_len); uvwasi_errno_t err = uvwasi_serdes_readv_iovec_t(memory, mem_size, ri_data_ptr, - ri_data, + ri_data.data(), ri_data_len); if (err != UVWASI_ESUCCESS) { - free(ri_data); args.GetReturnValue().Set(err); return; } @@ -1585,7 +1548,7 @@ void WASI::SockRecv(const FunctionCallbackInfo& args) { uvwasi_roflags_t ro_flags; err = uvwasi_sock_recv(&wasi->uvw_, sock, - ri_data, + ri_data.data(), ri_data_len, ri_flags, &ro_datalen, @@ -1595,7 +1558,6 @@ void WASI::SockRecv(const FunctionCallbackInfo& args) { uvwasi_serdes_write_roflags_t(memory, ro_flags_ptr, ro_flags); } - free(ri_data); args.GetReturnValue().Set(err); } @@ -1632,20 +1594,13 @@ void WASI::SockSend(const FunctionCallbackInfo& args) { mem_size, so_datalen_ptr, UVWASI_SERDES_SIZE_size_t); - uvwasi_ciovec_t* si_data = UncheckedCalloc(si_data_len); - - if (si_data == nullptr) { - args.GetReturnValue().Set(UVWASI_ENOMEM); - return; - } - + std::vector si_data(si_data_len); uvwasi_errno_t err = uvwasi_serdes_readv_ciovec_t(memory, mem_size, si_data_ptr, - si_data, + si_data.data(), si_data_len); if (err != UVWASI_ESUCCESS) { - free(si_data); args.GetReturnValue().Set(err); return; } @@ -1653,14 +1608,13 @@ void WASI::SockSend(const FunctionCallbackInfo& args) { uvwasi_size_t so_datalen; err = uvwasi_sock_send(&wasi->uvw_, sock, - si_data, + si_data.data(), si_data_len, si_flags, &so_datalen); if (err == UVWASI_ESUCCESS) uvwasi_serdes_write_size_t(memory, so_datalen_ptr, so_datalen); - free(si_data); args.GetReturnValue().Set(err); }