Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: address coverity warning of memory leak #52586

Closed
wants to merge 1 commit into from

Conversation

mhdawson
Copy link
Member

Covery reported the cach passed in the user as being overwritten by newly added automatic caching. Guard the automatica cachine to avoid this.

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/loaders
  • @nodejs/vm

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Apr 18, 2024
@mhdawson
Copy link
Member Author

Coverity report which looks valid to me. Since code later on seems to use the used_cache_from_user to decided how to report the error (user reported error or automatic cache one), it seems like the right answer is to only use the automatic caching if a cache has not been provided by the user. @joyeecheung what do you think?

    uint8_t* data =
244            static_cast<uint8_t*>(cached_data_buf->Buffer()->Data());
     	15. alloc_fn: Storage is returned from allocation function operator new.
     	16. var_assign: Assigning: cached_data = storage returned from new v8::ScriptCompiler::CachedData(data + cached_data_buf->ByteOffset(), cached_data_buf->ByteLength(), v8::ScriptCompiler::CachedData::BufferNotOwned).
245        cached_data =
246            new ScriptCompiler::CachedData(data + cached_data_buf->ByteOffset(),
247                                           cached_data_buf->ByteLength());
248      }
249
250      Local<String> source_text = args[2].As<String>();
251      ScriptOrigin origin(isolate,
252                          url,
253                          line_offset,
254                          column_offset,
255                          true,                             // is cross origin
256                          -1,                               // script id
257                          Local<Value>(),                   // source map URL
258                          false,                            // is opaque (?)
259                          false,                            // is WASM
260                          true,                             // is ES Module
261                          host_defined_options);
262
263      CompileCacheEntry* cache_entry = nullptr;
     	17. Condition can_use_builtin_cache, taking true branch.
     	18. Condition realm->env()->use_compile_cache(), taking true branch.
264      if (can_use_builtin_cache && realm->env()->use_compile_cache()) {
265        cache_entry = realm->env()->compile_cache_handler()->GetOrInsert(
266            source_text, url, CachedCodeType::kESM);
267      }
     	19. Condition cache_entry != NULL, taking true branch.
     	20. Condition cache_entry->cache != std::nullptr_t(), taking true branch.
268      if (cache_entry != nullptr && cache_entry->cache != nullptr) {
269        // source will take ownership of cached_data.
     	
CID 361813: (#1 of 1): Resource leak (RESOURCE_LEAK)
21. overwrite_var: Overwriting cached_data in cached_data = cache_entry->CopyCache() leaks the storage that cached_data points to.
270        cached_data = cache_entry->CopyCache();
271      }

Covery reported the cach passed in the user as being
overwritten by newly added automatic caching. Guard
the automatica cachine to avoid this.

Signed-off-by: Michael Dawson <midawson@redhat.com>
@joyeecheung
Copy link
Member

joyeecheung commented Apr 19, 2024

Does DCHECK_IMPLIES(can_use_builtin_cache , !args[5]->IsArrayBufferView()); make the warning go away? The two are mutually exclusive, so it should not be possible for the overwrite to happen, but since the values come from JS land maybe coverity cannot understand it.

Also used_cache_from_user will go away in #52413 though again, the mutual exclusiveness come from JS land and I am not sure if coverity can understand…it might be because we will use the exiting state of an std::optional that will be overwritten to decide instead of a boolean that’s deduced from JS land arguments?

@mhdawson
Copy link
Member Author

@joyeecheung I don't have a way to run locally, so I can't answer if adding DCHECK_IMPLIES(can_use_builtin_cache , !args[5]->IsArrayBufferView()); would resolve other than PRing/landing.

If its guarded at the JS level so that it will not use the autocache if the user has provided a cache I can just mark the report as a false positive if we don't think guarding in the C++ code makes sense.

@joyeecheung
Copy link
Member

joyeecheung commented Apr 19, 2024

If its guarded at the JS level so that it will not use the autocache if the user has provided a cache

Yes, the auto cache is not used from the user-accessible path. The binding is shared by both vm.Module constructors as well as our internal ESM loader. The latter passes a Symbol as the 6th argument to signify that it's okay to use the on-disk cache. The former passes an ArrayBufferView as the 6th argument to signify that users provide a cache (which is in that ArrayBufferView). So the mutual exclusiveness comes from how the JS invokes the binding. To overwrite it it means the binding's 6th argument is both a Symbol and an ArrayBufferView which is impossible (but that's JS semantics, probably not something coverity can understand).

@mhdawson
Copy link
Member Author

@joyeecheung just to be 100% sure I should close this just mark the report as a false positive in coverity, right?

@joyeecheung
Copy link
Member

Yes

@mhdawson mhdawson closed this Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants