Giter Club home page Giter Club logo

Comments (9)

gnzlbg avatar gnzlbg commented on September 23, 2024 1

The behavior of calling {GlobalAlloc, Alloc}::dealloc with a null pointer or with a size of 0 is undefined, so the bug that's causing that must be somewhere else. I'm going to leave this issue open until the documentation bug for jemalloc-sys is fixed.

We should probably also add an assume!(!ptr.is_null()) to {GlobalAlloc, Alloc}::dealloc.

from jemallocator.

gnzlbg avatar gnzlbg commented on September 23, 2024

According to jemalloc's docs:

  • The dallocx() function causes the memory referenced by ptr to be made available for future allocations.

  • The sdallocx() function is an extension of dallocx() with a size parameter to allow the caller to pass in the allocation size as an optimization. The minimum valid input size is the original requested size of the allocation, and the maximum valid input size is the corresponding value returned by nallocx() or sallocx().

so IIUC the following should work:

void* ptr = malloc(0); // Does this return NULL ? jemalloc docs don't say
sdallocx(ptr, 0);

yet that would always make the assert fail. So either this snippet is correct and the assert is incorrect, or the docs are incorrect, and this snippet would need to be written like this:

void* ptr = malloc(size);
if (size == 0) { free(ptr); } else { sdallocx(ptr, size); }

which IMO would make the API of jemalloc unnecessarily hard to use correctly.

@davidtgoldblatt could you clarify what the intent is here?

from jemallocator.

gnzlbg avatar gnzlbg commented on September 23, 2024

@davidMcneil

The malloc() and calloc() functions return a pointer to the allocated memory if successful; otherwise a NULL pointer is returned and errno is set to ENOMEM.

That is, jemalloc memory allocation functions only returns NULL if the allocation failed, and sdallocx does not support being called with NULL, so the error is in the comment in this crate. Calling sdallocx with NULL is not allowed, you need to call it with an allocation that succeeded.

from jemallocator.

davidMcneil avatar davidMcneil commented on September 23, 2024

Thanks for the response @gnzlbg! I am not using jemalloc-sys directly. The bad call to sdallocx is occurring in jemallocator's implementation of dealloc.

I will see if I can get a more minimal example replicating the issue.

from jemallocator.

davidtgoldblatt avatar davidtgoldblatt commented on September 23, 2024

@davidtgoldblatt could you clarify what the intent is here?

We've historically tried to avoid taking a strong stance on this sort of implementation-defined thing (e.g. return value of malloc(0)). Wanting C++ interop has made this harder and harder, to the point that I think we might just as well commit to malloc(0) being non-NULL (barring OOM).

I'm not sure anyone ever thought too hard about the sdallocx-with-zero case. We used to assert against it, but took it away (again, for C++ interop reasons). The more I think about it, this less I'm convinced that it was ever the right thing to do. If we give out non-NULL pointers in response to a request for a particular size, we ought to accept that size back again in sdallocx.

from jemallocator.

gnzlbg avatar gnzlbg commented on September 23, 2024

@davidtgoldblatt is there an issue in jemalloc upstream that we could cross-reference from here ?

In Rust, the behavior of requesting an allocation with zero-size is undefined, i.e., malloc(N) will always be called with N > 0, so if it returns a nullptr, then that must be because the allocation failed. Trying to deallocate such a nullptr (e.g. free(NULL), or sdallocx(NULL, N > 0)) is also UB in Rust, so these cases should never be triggered. That is, we will add asserts anyways to our wrappers to try to diagnose these issues in the Rust side.

from jemallocator.

davidtgoldblatt avatar davidtgoldblatt commented on September 23, 2024

@davidtgoldblatt is there an issue in jemalloc upstream that we could cross-reference from here ?

I don't think so; what specifically are you referring to though? (I think a catch-all "there should be better documentation around edge-cases" issue with a list would be useful; I don't think we have one at the moment).

from jemallocator.

davidtgoldblatt avatar davidtgoldblatt commented on September 23, 2024

I started a list at jemalloc/jemalloc#1716

from jemallocator.

gnzlbg avatar gnzlbg commented on September 23, 2024

Thanks, that was precisely what I was referring to.

from jemallocator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.