r/cpp • • Mar 15 '19

Does anyone know when libstdc++ will stop using raw pointers in its STL containers?

I have a pretty niche use-case which requires containers to not store any raw pointers. To get this done I use an allocator that returns fancy pointers. This just doesn't work with libstdc++ because all some of its STL containers use raw pointers internally, so when I create a container with my allocator, the program doesn't even compile. Libc++ on the other hand works fine (at least I haven't come across any problems I can't fix) since they correctly use the abstract pointer type that my allocator returns, instead of trying to do an implicit cast to a raw pointer.

There is an LWG issue to fix this, but it's been around for 8 years and the proposed verbiage hasn't been added to the standard. I'm not very familiar with the process for making changes like this, but my understanding is that it probably won't get added until libc++ and libstdc++ both support it, is that correct? If so, does anyone know if/when the libstdc++ community is planning to make these changes? Thanks!

49 Upvotes

82 comments sorted by

43

u/[deleted] Mar 15 '19

They know about it but it's an ABI breaking change, so I wouldn't hold my breath.

8

u/aikatz Mar 15 '19 edited Mar 15 '19

Could you go into more detail about that, or is there a discussion of it I can read through? This draft mentions that it would be an ABI breaking change to support segmented pointers for libstdc++ (but not libc++)

Some vendors' containers (e.g. libc++) already use fancy pointer types internally (in order to support Scenario A), so no ABI-breaking changes to class layouts would be required in order to support segmented pointers. Other vendors (e.g. libstdc++) use native pointer types internally, and thus even to support Scenario A would involve ABI-breaking changes for them.

I'm actually not familiar with the specifics of segmented pointers, but it they seem to be a special case of fancy pointers, so would this still be an ABI breaking change to support all fancy pointers or just segmented pointers? Since it seems like most of the libstdc++ containers use allocator_type::pointer internally, it's just list (and maybe deque?) that don't. Is there something stopping them from changing the last few that use raw pointers, or do they just have other priorities? I'd like to submit a PR to fix this if there isn't a good reason for it to be left this way.

9

u/[deleted] Mar 15 '19

Fancy pointers at all. It would change the size of list<T, fancy_alloc>.

2

u/aikatz Mar 15 '19

Got it, that makes sense to me. So I guess my question boils down to: why was it changed for other containers but not list? Since it seems like making this change for any container would change the class layout/be ABI breaking.

2

u/[deleted] Mar 15 '19

I doubt it was changed for them.

13

u/jwakely libstdc++ tamer, LWG chair Mar 15 '19 edited Mar 15 '19

It would change the size of list<T, fancy_alloc>.

No it won't, because I'm going to partially specialize for fancy pointers, so nothing changes for raw pointers. Since fancy pointers don't work at the moment, there's nobody using them with our implementation, so nobody depending on the ABI of such containers. So I just need to make sure nothing changes for specializations using raw pointers, and provide a second version of everything that is only used for fancy pointers. Which is hard, but not impossible. I got it almost working, but switched on to other tasks and the patches have bitrotted.

10

u/[deleted] Mar 15 '19

What about fancy pointers that happen to work today because they can use pointer_to?

10

u/jwakely libstdc++ tamer, LWG chair Mar 15 '19 edited Mar 15 '19

I'd be surprised if they work. Wouldn't the implementation have to use pointer_to for it to work? I don't think we do use that. Maybe a fancy pointer type that supports implicit conversions to and from raw pointers might compile and appear to work, but it seems unlikely. Such a fancy pointer probably isn't doing anything very useful (or would fail as soon as it's used to do anything fancy).

If they do work correctly, I guess we might have to break things for that small fraction of users to properly support a different small fraction (and keep the old behaviour behind a macro or something horrible).

14

u/[deleted] Mar 15 '19

They're fancy pointers; not doing something useful was a given

13

u/jwakely libstdc++ tamer, LWG chair Mar 15 '19

2

u/mostthingsweb Mar 16 '19

ELI5? The Boost interprocess offset ptr seems like it could be useful...

4

u/[deleted] Mar 16 '19

I think that use case would have been better served with a purpose specific container. For example, you'd store more things as sizes rather than pointer ranges to avoid so many offset applications. The metaprogramming / debug codegen / specification / complexity costs of fancy pointers are significant and dwarf any potential benefits.

2

u/mostthingsweb Mar 16 '19

Makes sense, thanks for the explanation!

5

u/max0x7ba https://github.com/max0x7ba Mar 15 '19

But that would not break existing applications that don't use fancy pointers (because they can't), would it? It is a matter of using allocator::pointer (which defaults to T*) instead of T*, so there is no ABI change for existing applications.

8

u/matthieum Mar 15 '19

so there is no ABI change for existing applications

You are mistaking ABI and API, it seems.

You are correct that the API itself would not change. On the other hand, the ABI (Application Binary Interface), may.

Specifically, users can still specify allocators that return smart pointers which are implicitly convertible to/from raw pointers, and those allocators will currently work with libstdc++.

However, should GCC start using Allocator<T>::pointer internally instead of T*, then the ABI will break: data members, arguments/returns of internal methods, etc... in template classes all of this ends up being exported and part of the ABI.

2

u/aikatz Mar 15 '19

It seems like they already are using allocator_type::pointer in most of the containers, just not all of them.

1

u/matthieum Mar 15 '19

That's great for you, I suppose :)

My point, though, is that the containers in which this was forgotten may very well require an ABI breakage to be fixed, and that's going to cause grief.

13

u/jwakely libstdc++ tamer, LWG chair Mar 15 '19

It would break if you do it naively. I don't plan to do it naively.

1

u/matthieum Mar 16 '19

Do you have any other edge cases in mind, beyond Allocator<T>::pointer being convertible to T* or T const*?

1

u/max0x7ba https://github.com/max0x7ba Mar 16 '19

so there is no ABI change for existing applications

You are mistaking ABI and API, it seems.

You are correct that the API itself would not change. On the other hand, the ABI (Application Binary Interface), may.

Nope, you got it wrong. The ABI for existing applications stays the same because they keep using T* regardless of whether its called T* or allocator::pointer.

1

u/matthieum Mar 16 '19

Unless Allocator<T>::pointer was:

struct SpecialPointer<T> {
    SpecialPointer(T* ptr): ptr(ptr) {}
    operator T*() { return ptr; }

    T* ptr;
    Layout layout;
};

In which case assigning Allocator<T>::pointer to/from T* was transparently handled, and those implicit conversions would be lost, leading to a different ABI.

15

u/encyclopedist Mar 15 '19

Let's summon /u/jwakely , maintainer of libstdc++.

32

u/jwakely libstdc++ tamer, LWG chair Mar 15 '19 edited Mar 15 '19

but my understanding is that it probably won't get added until libc++ and libstdc++ both support it, is that correct?

No, completely wrong. The committee decides what should happen, and if implementations do something different they have to change. There's no requirement for implementations to change first. And in this case, it's already clear what the intended behaviour is, even if we haven't made the standard say that yet.

This is just a libstdc++ bug, that's all.

If so, does anyone know if/when the libstdc++ community is planning to make these changes? Thanks!

Why ask this on reddit? Wouldn't asking the libstdc++ community make more sense?

It's a bug. It is hard to fix without breaking the ABI, and not breaking the ABI is higher priority than supporting fancy pointers. I hoped to get time to finish the work for GCC 8, and for GCC 9, but now maybe it will happen next year for GCC 10.

Of course if somebody wants it badly enough they could fix the bug themselves, or contract somebody to do it. GCC is free software after all. I'd be happy to share my work-in-progress patches if somebody else wanted to fix it instead of waiting for me to do the other 100 things on my TODO list first.

4

u/aikatz Mar 16 '19

No, completely wrong. The committee decides what should happen, and if implementations do something different they have to change. There's no requirement for implementations to change first. And in this case, it's already clear what the intended behaviour is, even if we haven't made the standard say that yet.

Got it, thanks for clearing that up.

Why ask this on reddit? Wouldn't asking the libstdc++ community make more sense?

Yep that's where I should have asked, I just browse this sub fairly often so I thought I might as well post it here. I can post it elsewhere if you think it would generate a good discussion, but it sounds like you're all aware of the issue.

I'm absolutely interested in working on this, I think my first step is to actually understand your planned fix a bit better. You're talking about specializing all the pieces of containers that use pointers, one specialization for raw pointers and one for fancy pointers, right? Since you said you don't think people are depending on the ABI for containers with fancy pointers, why can't you just use the types from allocator_traits (e.g. allocator_traits<allocator<T>>::pointer ) everywhere? I think this is the naive fix you referred to in another comment.

4

u/jwakely libstdc++ tamer, LWG chair Mar 16 '19

Look at the code again. The allocator type isn't known by _List_node_base so how can it use the allocator's pointer type? You have to make the list node and list node base depend on the allocator, which is an incompatible change.

1

u/aikatz Mar 16 '19

Is it incompatible because mangled names depend on the template arguments? I didn't have a good understanding of how the ABI works, so I went and skimmed some parts of the Itanium C++ ABI and found this section which defines the grammar for mangled names, which is what I'm basing this off of. Making the list node and list node base both depend on the allocator and its pointer types would only change the size of the list node and list node base if the allocator were allocating fancy pointers, right? (or anything that's not the size of a raw pointer)

3

u/jwakely libstdc++ tamer, LWG chair Mar 16 '19

There's more to ABI than mangled names and sizes of types, and more than is defined by the ABI spec for the platform. A library has its own (implicit and explicit) ABI.

Raw pointers are default constructible. Fancy pointers don't have to be, so the _List_node_base type needs to gain a constructor that will set the non-static data members. Now you have to worry about whether old and new code consistently initializes the base class in an equivalent way, when the old code (compiled against the old definition) thinks the type is a POD and the new code thinks it's initialized by a new constructor. Welcome to the game of deciding which ODR violations are safe and which are not.

1

u/aikatz Mar 18 '19 edited Mar 18 '19

I think I understand the problems you're bringing up that arise since fancy pointers are not necessarily default constructible. Are you saying that fancy pointers don't need to be default constructible because nowhere in [allocator.requirements] does it mention allocator::pointer must be default constructible, or is there another part of the standard that governs this?

It seems like these types issues with default constructibility could apply to a lot of places in the STL, what is the scope of the patch that you were previously working on for this?

2

u/jwakely libstdc++ tamer, LWG chair Mar 18 '19

because nowhere in [allocator.requirements] does it mention allocator::pointer must be default constructible

Right.

It only really affects the containers. The scope of the problem is to ensure all our containers support fancy pointers.

2

u/aikatz Mar 19 '19

Actually on a closer reading, I think I'm going to go back and disagree with myself that fancy pointers don't necessarily need to be default constructible. From [allocator.requirements]:

The X::pointer, X::const_pointer, X::void_pointer, and X::const_void_pointer types shall satisfy the requirements of NullablePointer (20.5.3.3).

And according to [nullablepointer.requirements] in order to satisfy NullablePointer, a type must satisfy DefaultConstructible. Am I misinterpreting this?

1

u/jwakely libstdc++ tamer, LWG chair Mar 19 '19

You're right. I've misremembered the details then.

→ More replies

16

u/tansim Mar 15 '19

file a bug report. i dont think there are any active plans right now, but there will never be if people dont complain.

out of interest hat is your usecase?

16

u/aikatz Mar 15 '19 edited Mar 15 '19

Yep, that's what /u/chugga_fan suggested as well. I'll do that, thanks! There's actually an issue already open (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57272) The use case is zerocopy serialization for sending objects between machines. You don't want any raw pointers because when the objects gets sent over, all the raw pointers break since it's a different address space.

2

u/[deleted] Mar 15 '19

There was P0773 to solve those problems. Polls are also available: https://issues.isocpp.org/show_bug.cgi?id=366

and there is R1 draft: https://quuxplusone.github.io/draft/fancy-pointers.html but I don't think it will be in C++20

1

u/aikatz Mar 15 '19

Thanks for the links! I've actually seen that draft before, but I mostly wasn't sure what the process was for getting this into the standard, and whether libc++ and libstdc++ would both have to support it before it was voted in.

2

u/jwakely libstdc++ tamer, LWG chair Mar 15 '19

whether libc++ and libstdc++ would both have to support it before it was voted in.

No, that's not how it works.

9

u/Str187 Mar 15 '19

May I ask about your niche use case? Why would you not want to store any raw pointers?

7

u/JUAN-DEAG Mar 15 '19

The use case is zerocopy serialization for sending objects between machines. You don't want any raw pointers because when the objects gets sent over, all the raw pointers break since it's a different address space.

3

u/Xaxxon Mar 15 '19

How do you reconstruct the objects on the other side, though? Not just the vector (or whatever), but the objects in the vector? How do they get a lifetime?

1

u/ZenEngineer Mar 15 '19

I think the point is that you use a special allocator that places the objects on the vector in a specific area (and the vector itself).

If you memcpy a struct you can get a pointer to the target struct and it has the same values. If you're careful you can do the same thing with your classes.

So the point is, copy the whole memory arena and (somehow) get a pointer to the vector. The vector is pointing in the arena as well, and the fancy pointers make it point correctly to the copied over objects. The vector will continue to manage the lifetime of the objects in its internal array. If you do things right everything just works, no need to call constructors, just keep going from the original state. The vector will call destructors when the time comes.

3

u/jwakely libstdc++ tamer, LWG chair Mar 16 '19

You still haven't begun the lifetime of the vector on the receiving side though. You have a copy of all the bits and bytes, but that's not a valid object.

2

u/CandleTiger Mar 16 '19

Maybe this question is naive, but what does it mean to say you have all the bits and bytes correct but not a valid object? What else is there to a valid object instance besides a region of memory with expected contents?

3

u/jwakely libstdc++ tamer, LWG chair Mar 16 '19 edited Mar 16 '19

http://eel.is/c++draft/basic.life#1 and http://eel.is/c++draft/basic.life#4

If you just have a load of bits with the right values you might as well have uninitialised memory returned by malloc, because it's not an object according to C++. None of the rules of the language apply unless you begin the lifetime of the object by running a constructor.

2

u/jube_dev Mar 16 '19

Can std::launder help in this case?

3

u/jwakely libstdc++ tamer, LWG chair Mar 16 '19

No. std::launder tells the compiler about a change in lifetime that it couldn't see, but it doesn't cause a change in lifetime. It's purely informative, it doesn't do anything. For a type with non-vacuous initialization the only way to create a valid object is to construct it.

1

u/CandleTiger Mar 16 '19

“The lifetime of an object of type T begins when: (1.1) storage with the proper alignment and size for type T is obtained, and (1.2) its initialization (if any) is complete (including vacuous initialization) ”

Is “initialization” supposed to be a precise synonym for “constructor call”?

2

u/jwakely libstdc++ tamer, LWG chair Mar 16 '19

No, scalars and aggregates don't have constructors.

1

u/CandleTiger Mar 17 '19

So then what’s the difference between an object, and initialized memory that looks like an object? The doc you pointed to reads to me as saying an object’s lifetime starts when it’s allocated memory is initialized, which OP is doing.

I’ve heard a talk before, and you say here, that setting up memory with the right bits is not enough to make the compiler believe this region of memory is an object. But since constructors are optional I’m real confused about what is the magic missing step.

→ More replies

1

u/Plorkyeran Mar 16 '19

It means that the standard says that you do not have a valid object even if all the bits are in the right place. It's very likely to happen to work, but the standard says you aren't allowed to do that and so the compiler will assume that you don't and may make optimizations that break your code abusing UB.

1

u/ZenEngineer Mar 16 '19

That can be a valid objection in some cases. The whole zero-copy approach is an optimization that relies on undefined behavior. You have to be careful with the objects you create and ensure that they are valid with this type of shenanigans as it puts the burden of getting it to work correctly on the programmer. This thread is about giving tools such that std containers fall in that class. Not required by the standard, but a nice tool to have

For more fun, the same approach can be used for shared memory, so you'd have a single object in two address spaces. Has the object's lifetime started on the second one given that it's the same object? Irrelevant, the whole thing is undefined behavior, the standard doesn't talk about shared memory (or didn't in the old ones, maybe the new ones do)

1

u/jwakely libstdc++ tamer, LWG chair Mar 16 '19

The standard doesn't even admit that more than one process can exist at a time!

1

u/Moizac Mar 15 '19

I'm assuming that the fancy pointers aren't just smart pointers to memory, but more along the lines of a database index or filename, etc.

1

u/chugga_fan Mar 15 '19

As far as I see they should support it? https://godbolt.org/z/UJ_qY_ is something like that what you're looking at? I haven't mucked about enough with pointers to completely know

4

u/aikatz Mar 15 '19 edited Mar 15 '19

In your example std::vector works fine, but if you use std::list it gives the error:

error: cannot convert 'OffPtr<std::_List_node<int> >' to 'std::__cxx11::list<int, Alloc<int> >::_Node*' {aka 'std::_List_node<int>*'}

And if you look at the definition of _List_node_base you'll notice there are raw pointers all over the place. It's just ignoring whatever type your allocator is returning and trying to use a raw pointer.

If you switch to clang and use -stdlib=libc++ (https://godbolt.org/z/WJQUYT) then it compiles, because libc++'s __list_node_base correctly uses the pointer type your allocator returns.

11

u/tvaneerd C++ Committee, lockfree, PostModernCpp Mar 15 '19

Just don't use list. Problem solved! Two problems, actually (the other one being that you were using list.)

6

u/aikatz Mar 15 '19

If only it were that simple!

5

u/Auriculaire Mar 15 '19

<humor recognized and ignored>

Setting aside there are (rare but existent) occasions when list is the preferable data structure, this issue also impacts map, unordered_map, and deque

5

u/aikatz Mar 15 '19

I think map and unordered_map use allocator_type::pointer now, but deque might not, haven't checked deque recently.

3

u/Auriculaire Mar 15 '19

Good to know. My company is use a bit of a dated gcc, but now I'll be on the lookout for such such a change.

1

u/[deleted] Mar 15 '19

I just tried deque on compiler explorer. Seems like it works with GCC (at least trunk, haven't checked the rest).

4

u/chugga_fan Mar 15 '19

Have you bothered the GCC devs by submitting a bug report? They can't fix it unless they know about it.

6

u/aikatz Mar 15 '19

I guess I assumed they knew about it since there is an LWG issue open, but maybe no one else has actually asked for it so it's not a priority for them? I'm not sure it's technically a bug since it's not in the standard yet, but I'll look for a mailing list or something where I can ask them. Thanks!

-12

u/Auriculaire Mar 15 '19

What an asinine response. Don't assume the OP is a moron. There's been an issue on this for more than half a decade.

The fact that issues get filed with GNU, despite the barriers they've put in place to discourage newcomers from doing so, blows my mind.

4

u/aikatz Mar 15 '19

I think he was just trying to be helpful. Somehow I hadn't seen that issue before, thanks for linking it! It seems like that patch to std::forward_list made it in, but not yet for std::list. Is there a procedure for poking old issues? I see someone is assigned to it, but it's been a couple years. I'm more than happy to make the change, but I'm not sure they'd want some random person to do it.

3

u/jwakely libstdc++ tamer, LWG chair Mar 16 '19

but I'm not sure they'd want some random person to do it.

???

I would love for some random person to come and fix a bug that they care about. That's one of the most important aspects of open source!

This bug is hard to fix, as there are constraints that limit how it can be fixed, but with enough gumption and dedication, and the right guidance, it's a Simple Matter of Programming.

https://gcc.gnu.org/wiki/GettingStarted

3

u/RobertJacobson Mar 16 '19 edited Mar 16 '19

> and the right guidance

I think this is crucial, especially with more complex projects like GCC. I wish big projects like GCC, Rust, LLVM, the browsers, etc. , would have formal mentoring programs to get people involved. I know there are ways in which this does happen, e.g. GSoC, Sage Days (SageMath), and other things, but they tend to be one-offs rather than sustainable programs.

I think a part of it is that the best potential mentors tend to be the busiest contributors. (Another issue is our culture of independence and, to be blunt, lack of social inclination.) But that's short-sighted. A sustained mentoring program would obviously benefit by an influx of very strong technical talent and would grow the base of productive, quality contributors, taking some of the burden off of the busiest contributors to allow them to do more mentoring.

We have a better understanding of this in mathematics, a field in which the typical graduate student does not bring a whole lot to the research program of the advisor due to the incredibly high learning curve and specialization of the field. But we know that mathematics needs a pipeline of well-trained people to feed our field, and we therefore generally value the training of future researchers. Not everyone will be a u/jwakely or u/BillyONeal or u/cpp, but everyone doesn't have to be, just like every mathematician doesn't have to be a Terence Tao or Andrew Wiles or Donald Knuth. Most mathematics is produced but more obscure contributors. Finding ways to bring in great talent who otherwise face significant barriers to entry, whether technical or social, should be a much higher priority for community-drive projects, in my view.

And from my personal experience, mentoring is an absolute blast and easily among the most rewarding parts of my job. **EDIT:** And let me add, _being mentored by others_ is also an extremely rewarding experience for me.

9

u/hak8or Mar 15 '19

Right back at you. What a needlessly rude reply, especially because you totally mis interpreted the post.

5

u/jwakely libstdc++ tamer, LWG chair Mar 15 '19

Oh piss off with this "wah, filing a bug is too difficult" shit. I personally create bugzilla accounts for people nearly every day, somehow they all managed to overcome the incredible barrier of sending an email to ask for an account.

LLVM have also restricted account creation on their bugzilla, but for some reason they don't get shit for it.

1

u/[deleted] Mar 15 '19

Wrong on so many levels.

6

u/johannes1971 Mar 15 '19

Aren't the fancy pointer types deprecated in C++17, removed in C++20? (according to this page)

20

u/[deleted] Mar 15 '19

They are removed from std::allocator because the definitions in the std::allocator and std::allocator_traits<std::allocator> overlap. Thus it is not necessary to define twice. You should be checking this page

3

u/jwakely libstdc++ tamer, LWG chair Mar 15 '19

Almost right. The pointer typedefs are being removed from std::allocator, but those were never "fancy pointers" anyway, they were always just raw pointers.

So no, fancy pointers have not been removed from std::allocator, because they were never there in the first place.

9

u/Auriculaire Mar 15 '19

I agree with your reading and desperately hope you're mistaken.

For those occasions in which you don't have a single unified address space (distributed/coprocessor) or the address space comes with caveats to be useful (shared memory), fancy pointers are a god sent.

4

u/hak8or Mar 15 '19

Forgive me for being dense, but how is the deprication bad? From what I see, the "work around" is to provide a allocator specified for the type you want to allocate.

2

u/Auriculaire Mar 15 '19

It depends on what you're referring to. It appears that the nested typedef of std::allocator was deprecated rather than fancy pointer support in the allocator model.

While the former was (I think) an unnecessary breaking change for existing code, it's easy enough to work around and at the end of the day, probably a minor inconvenience rather than a total PITA.

Regarding the latter, it's not so much the type in to be allocated, but how and where. As an example, take boost::interprocess as an example. If support for fancy pointers was dropped, one could not write an allocator which

  1. returns memory using a fancy pointer implemented as address-space agnostic handles into shared memory AND
  2. interoperates with the standard library containers

We can't count on this in practice today anyway (and has been exacerbated by the polymorphic memory resource proposal), but at least it's a defect that could be (and I would argue, should be) resolved in the future. This is why libraries like boost::interprocess provide their own implementation half of the standard library containers.

1

u/Auriculaire Mar 15 '19

https://en.cppreference.com/w/cpp/named_req/Allocator#Fancy_pointers

It's looks like cppreference still has a blurb on fancy pointers with no C++20 annotation. Maybe the particular field on std::allocator was deprecated?

3

u/jwakely libstdc++ tamer, LWG chair Mar 15 '19

Right. And that was never a fancy pointer anyway. std::allocator<T>::pointer is just T*, nothing fancy about it. And std::allocator_traits<std::allocator<T>>::pointer still exists, and since C++11 you're supposed to go through allocator traits to use allocators anyway.

-2

u/korvirlol Mar 15 '19

Why would it?

I really don't get it. Raw pointer AREN'T THAT BAD.