From 33984d6e4d3596d1361e4f1630c2df27c897eb95 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 0e23243918b31f..7a204acfee0d80 100644 --- a/src/node_wasi.cc +++ b/src/node_wasi.cc @@ -660,27 +660,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); } @@ -763,31 +760,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); } @@ -813,27 +808,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); } @@ -995,31 +987,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); } @@ -1434,21 +1419,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]); @@ -1457,8 +1429,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) { @@ -1470,8 +1442,6 @@ void WASI::PollOneoff(const FunctionCallbackInfo& args) { } } - free(in); - free(out); args.GetReturnValue().Set(err); } @@ -1562,20 +1532,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; } @@ -1584,7 +1547,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, @@ -1594,7 +1557,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); } @@ -1631,20 +1593,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; } @@ -1652,14 +1607,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); }