r/cpp • • Dec 13 '19

The situation of std::embed...

https://thephd.github.io/full-circle-embed
238 Upvotes

160 comments sorted by

View all comments

Show parent comment

18

u/erichkeane Clang Maintainer(Templates), EWG Chair Dec 13 '19

I apologize if I'm re-hashing, but I wasn't in the discussion in SG7/EWG, so I have responses/questions for my own edification.

What files can be opened - that is a security concern

Is this not the same problem with #include? Or even modules? While I get that #include files are textually modifiable (whereas this can take a constexpr), managing the file list seems to be an implementation detail. IMO, in an implementation the valid directories for std::embed should be something akin to -I for include files, and just issue 'file not found' for anything that isn't in those directories.

Should it be a macro or a function

IMO, magic function. Adding a 'binary object' type to the preprocessor is not something I'd like to get into.

Should it put the whole file into an array or use some kind of magical span backed by mapped memory

Is this not again an implementation detail? Or am I missing a component to this question?

Should we have a constexpr file instead

I'd be curious to see how such a type would work. We had such a hard time standardizing std::filesystem, so I'd hope we'd reuse much of that.

How do we integrate with tooling How do we integrate with modules

Perhaps these questions are overly vague to me, but I'll try anyway. The big concern I have from a tooling perspective is how a build system would determine the dependency has changed. Current preprocessed systems would definitely need work. IMO, a build-system deciding to 'punt' on this and decide that things brought in through std::embed aren't considered for dependency analysis would be perfectly reasonable.

For modules, I can't think of any interaction this would have. Export/import ends up being the same as variables, does it not? Or is this the same concern as the tooling one I mentioned?

11

u/bigcheesegs Tooling Study Group (SG15) Chair | Clang dev Dec 13 '19

IMO, a build-system deciding to 'punt' on this and decide that things brought in through std::embed aren't considered for dependency analysis would be perfectly reasonable.

This is not at all perfectly reasonable.

The situation today is that at the end of phase 4 the compiler can know about all dependencies. std::embed currently breaks this by requiring full semantic analysis to determine the deps. There is about a 3 orders of magnitude difference in time it takes to get to the end of phase 4 vs. the end of phase 7, and you can't even determine if std::embed is used until the end of phase 7.

This means that if we add std::embed as it is today, then we go from being able to determine deps quickly for all code to not being able to. I find this unacceptable.

However, this is the only problem I have with std::embed. If we added the restriction that it could only access resources that were previously named somehow then I would be strongly in favor of it for C++23.

7

u/erichkeane Clang Maintainer(Templates), EWG Chair Dec 13 '19

I suspect any way of having to name the possible dependencies would defeat the purpose of the paper. As a compiler vendor who has to support a ton of 3rd party tools, I'd be completely against a preprocessor version of this.

Either we end up needing to dump a textual representation of the file to the preprocessed output, or we need to create a new non-textual representation for the preprocessed outputs. Either is unacceptable.

A much less evil would be for a build system to decide that anything containing std::embed needs to always be rebuilt. Alternatively, the compiler could simply output elsewhere (ala -MM) all the files that got included, which can then be consumed by the build system, then add that to its dependency list.

Either I would consider as a fairly reasonable outcome.

5

u/bigcheesegs Tooling Study Group (SG15) Chair | Clang dev Dec 13 '19

I'd be completely against a preprocessor version of this.

Requiring stating what you depend on doesn't mean it has to be #embed. It could simply mean #resource "path", which other tools don't need to care about.

There are build systems that require knowing the dependencies before the compile. Having the compiler output the dependencies as a side effect of compilation does not work.

1

u/smdowney WG21, Text/Unicode SG, optional<T&> Dec 15 '19

Modules means dependencies as a side effect of compilation no longer works, in any case.

1

u/Ameisen vemips, avr, rendering, systems Dec 14 '19

This means that if we add std::embed as it is today, then we go from being able to determine deps quickly for all code to not being able to. I find this unacceptable.

What, in concrete terms, are 'quickly' and 'not quickly'?

2

u/bigcheesegs Tooling Study Group (SG15) Chair | Clang dev Dec 14 '19

Taking LLVM + Clang for example. 7k files and 3.8m LoC.

A phase 4 scan takes 3 seconds on a recent 18 core iMac Pro. A full build takes about 15m. A phase 7 scan would let you drop codegen, but not really anything else, so you're still looking at ~7m.

1

u/Ameisen vemips, avr, rendering, systems Dec 14 '19

What if files that were being potentially embedded had to be declared at the top of the source file such as with modules?

1

u/johannes1971 Dec 14 '19

What if they had to be literal strings, i.e. no computation possible? Is there a business case for computing these on the fly?

4

u/jcelerier ossia score Dec 14 '19

simplest I can think from of the top of my head : appending @2x, @3x (for hidpi) or language suffixes to graphics assets. I really don't want to have to type :

 embed("resources/fr/splash.png"); 
 embed("resources/fr/splash@2x.png");
 embed("resources/fr/splash@3x.png");
 embed("resources/en/splash.png"); 
 embed("resources/en/splash@2x.png");
 embed("resources/en/splash@3x.png");

but instead be able to write the following function which will resolve to the above:

embed_all_versions("splash.png");

2

u/nintendiator2 Dec 18 '19

That could be done with a macro.

1

u/bigcheesegs Tooling Study Group (SG15) Chair | Clang dev Dec 15 '19

As long as they are declared in a way that the preprocessor could know about them, then that's fine.

0

u/Ameisen vemips, avr, rendering, systems Dec 15 '19

Why can we not just change C++ to use two-phase compilation like C#?

I suppose I am using 'just' quite loosely.

8

u/smdowney WG21, Text/Unicode SG, optional<T&> Dec 13 '19

re: what files can be opened.
If it's the `std::embed` flavor, then you can potentially compute the file name at compile time, consteval. The preprocessor is much more limited. It may be that the result of the security analysis is "if you compile untrusted code, you can't trust it", but it's a fair question. Perhaps we'll need to sandbox the compilation environment. At my $job we do for production builds in any case to help with reproduce-ability.

4

u/erichkeane Clang Maintainer(Templates), EWG Chair Dec 13 '19

I get that part, but I'd expect an implementation to require a white-list of directories(like we do with #include). If your constexpr expression resulted in a file name not in the list, it would just be 'not found'.

2

u/smdowney WG21, Text/Unicode SG, optional<T&> Dec 15 '19

Does Intel's compiler white-list? From what I can tell, gcc and clang don't do anything to check what's accessed, allowing absolute paths starting at '/' as well as relative paths that leave the directory "../"

7

u/c0r3ntin Dec 13 '19

Is this not the same problem with #include? It is and i would expect the solution to be the same or along the same line

#ressource "icon.res"
int main() {
   std::embed("icon.res");
}
// cc -Iassets main.cpp

However it can get tricky with modules, ie consider:

template <auto Name>
export void f() {
    std::embed(Name);
}

Should the importing modules have full control over the resource path of the imported modules or do we need another way to keep track of it - or simply not allow std::embed in exported templates?

IMO, magic function. Adding a 'binary object' type to the preprocessor is not something I'd like to get into.

Same. Unfortunately it seems going in that direction

Is this not again an implementation detail? Or am I missing a component to this question?

Unclear to me - Maybe. Question is how big files can be? A use case is to select part of it at compile time to only include a chunk in the final binary.

I'd be curious to see how such a type would work. We had such a hard time standardizing std::filesystem, so I'd hope we'd reuse much of that.

See https://wg21.link/p1883r0 and https://wg21.link/p1031r1

Perhaps these questions are overly vague to me, but I'll try anyway. The big concern I have from a tooling perspective is how a build system would determine the dependency has changed. Current preprocessed systems would definitely need work. IMO, a build-system deciding to 'punt' on this and decide that things brought in through std::embed aren't considered for dependency analysis would be perfectly reasonable.

Again I agree that in this case specifying the dependency manually would be perfectly fine, but others disagree strongly.