Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

If you write your own allocator in C, do yourself a favor and use the valgrind API inside it. Its use can be conditional so you can "compile it out". Or should I say it has to be, so you can build the code in an environment where there's no valgrind API.


I've replaced Valgrind with sanitizers for most of my workflow. Using the sanitizer APIs in your custom allocator is also easy: https://github.com/tavianator/bfs/blob/main/src/sanity.h https://github.com/tavianator/bfs/blob/31dffd6441ba80ea998ce...


+1 - this isn't even that hard, it is include a single header + annotate allocations/frees, with some redzones around the actual allocation to know when the user-code is writing into the pool pointer areas.

https://developers.redhat.com/articles/2022/03/23/use-valgri...



Thats with Claude, right?


> with Claude

The comment itself is AI generated or the code reference?

Here's some of my old code

https://github.com/php/pecl-caching-apc/blob/0dd2a69bab58822...


oof love on this page how scrolling hijacks the back button


The Address Sanitizer (ASAN) in both clang and gcc is/are excellent:

https://github.com/google/sanitizers/wiki/addresssanitizer

I believe to integrate a custom allocator with ASAN, you need to look at the memory "poisoning" mechanisms:

https://github.com/google/sanitizers/wiki/AddressSanitizerMa...


It is also available on MVSC nowadays.

https://learn.microsoft.com/en-us/cpp/sanitizers/asan?view=m...

.NET team also makes use of it for the runtime parts,

https://devblogs.microsoft.com/cppblog/msvc-address-sanitize...


I have never heard about this (using valgrind with a "non-standard allocator"), but it looks really interesting, specially for other projects I am working on. I will definitely look into it.


I integrated the TXR Lisp garbage collector with valgrind. That came in handy on a number of occasions.

It's not always the case that you want special behavior for the garbage collector to assist with debugging, but also this: if you want to use Valgrind to debug anything that does not have to do with the garbage collector, you want all the false positives generated by it out of your way.

Some of the scanning that a conservative garbage collector has to do looks like access errors to Valgrind. By using the Valgrind API, you can grant yourself access to that memory, and then lock it again when done.

That aspect doesn't necessarily apply to a pool allocator being described in this article; that should not be triggering any false positives.


(In case you missed my other post, I ended up adding valgrind support in the article and in the 'libpool' project.)

> I integrated the TXR Lisp garbage collector with valgrind.

That's funny, because when I said "for other projects I am working on", I specifically meant a Lisp interpreter which uses a memory pool and garbage collection (I am still working on implementing this last part, anyway). I am very happy about the fact that I can continue using valgrind in this project.


This is awesome!


You can also integrate with AddressSanitizer to some extent, look into[1] ASAN_{UN,}POISON_MEMORY_REGION from <sanitizer/asan_interface.h> or the underlying intrinsics __asan_{un,}poison_memory_region.

[1] https://github.com/google/sanitizers/wiki/AddressSanitizerMa...


Thanks for the tip!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: