C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Do templates show source code?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Bogus Exception
Guest





PostPosted: Sat Apr 28, 2007 8:30 pm    Post subject: Do templates show source code? Reply with quote



Greetings, and thanks in advance for your patience.

I'm learning C++, and have come across a question whose answer I hope
you can shed light on. It is regarding using template classes/methods/
functions.

If you have a template in corresponding object code that cpp can use
through inclusion, then the type that will be used for that template
(<class T>, etc...) is not known when the template is distributed,
right?

The compiler must create the executable(s) from the template and the
user's source code after the template code has been distributed as an
object file. The nature of templates is that you don't know in advance
what data type(s) it will manage, so if you have object code with
templates, you can't distribute it with every kind of object
imaginable, as you'd have to guess every user-defined class/data
types.

The compiler creates the actual code from the template only once it
knows each type it will serve/utilize, right? So the compiler makes a
complete class/function that uses "int"s if you have ints being
processed, and another for "employees", one for "holidays", etc. as it
goes through the user's code-none of which was known when the template
object file was written.

So if all this is right (might not be!), then is it possible to
distribute "sensitive" source code via templates in object files for
later inclusion, or are there better mechanisms/tricks in the language
that allow the same kind of functionality?

TIA!

Bogus Exception


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Michael Tiomkin
Guest





PostPosted: Sun Apr 29, 2007 5:29 am    Post subject: Re: Do templates show source code? Reply with quote



Well, you can use two methods to hide your templates.
I. Avoid publishing sensitive templates.
Define base classes, define your "sensitive" interfaces
as functions/objects accepting ptrs to these base classes,
and demand that the clients will define their classes that inherit
from
these base classes. This means that you do not publish your templates
for the users.
II. Do remote compilation.
Compile the template functions/methods on your server. As a part of
your SDK,
prepare scripts that send the .h files defining the user classes
needed
for these templates, and obtain the object (.o or .obj) files of the
methods
as a result - you can use rsh, rpc, COM or CORBA to do that. Recall
that
you can simply send a compressed .i file to your compiler. You have
to use
the same compiler on your server, therefore you should also send
the exact version and type of the user compiler to the server.
II'. Do "local" remote compilation.
Demand that the users install a special server that does II. You
should have
exclusive access to this server, at least to the directories
containing
the template definitions. The script and the compiler would better be
readable
for everyone, because in this case the user will think that his
"sensitive"
code doesn't leak outside. In this case the rsh/rpc will be much
faster.

Michael

On Apr 28, 10:30 pm, Bogus Exception <bogusexcept...@gmail.com> wrote:
Quote:
Greetings, and thanks in advance for your patience.

I'm learning C++, and have come across a question whose answer I hope
you can shed light on. It is regarding using template classes/methods/
functions.

If you have a template in corresponding object code that cpp can use
through inclusion, then the type that will be used for that template
(<class T>, etc...) is not known when the template is distributed,
right?

The compiler must create the executable(s) from the template and the
user's source code after the template code has been distributed as an
object file. The nature of templates is that you don't know in advance
what data type(s) it will manage, so if you have object code with
templates, you can't distribute it with every kind of object
imaginable, as you'd have to guess every user-defined class/data
types.

The compiler creates the actual code from the template only once it
knows each type it will serve/utilize, right? So the compiler makes a
complete class/function that uses "int"s if you have ints being
processed, and another for "employees", one for "holidays", etc. as it
goes through the user's code-none of which was known when the template
object file was written.

So if all this is right (might not be!), then is it possible to
distribute "sensitive" source code via templates in object files for
later inclusion, or are there better mechanisms/tricks in the language
that allow the same kind of functionality?



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
John Moeller
Guest





PostPosted: Sun Apr 29, 2007 7:55 am    Post subject: Re: Do templates show source code? Reply with quote



On Sat, 28 Apr 2007 14:30:16 CST, Bogus Exception wrote:

Quote:
Greetings, and thanks in advance for your patience.

I'm learning C++, and have come across a question whose answer I hope
you can shed light on. It is regarding using template classes/methods/
functions.

If you have a template in corresponding object code that cpp can use
through inclusion, then the type that will be used for that template
(<class T>, etc...) is not known when the template is distributed,
right?

The compiler must create the executable(s) from the template and the
user's source code after the template code has been distributed as an
object file. The nature of templates is that you don't know in advance
what data type(s) it will manage, so if you have object code with
templates, you can't distribute it with every kind of object
imaginable, as you'd have to guess every user-defined class/data
types.

The compiler creates the actual code from the template only once it
knows each type it will serve/utilize, right? So the compiler makes a
complete class/function that uses "int"s if you have ints being
processed, and another for "employees", one for "holidays", etc. as it
goes through the user's code-none of which was known when the template
object file was written.

Sort of. You have the basic idea, but I think you're confusing
certain parts of the process. When you use a template in *source
code*, you are telling the compiler to instantiate a version of the
template that will be treated as a complete class. That instantiated
template is then compiled as *object code* with the rest of the file
where you include the template. (Yes, this is probably an overly
rough definition; refinements are welcome.)

You may want to read over the section on templates in the FAQ; it
might give you a better idea:

http://www.parashift.com/c++-faq-lite/templates.html

Quote:
So if all this is right (might not be!), then is it possible to
distribute "sensitive" source code via templates in object files for
later inclusion, or are there better mechanisms/tricks in the language
that allow the same kind of functionality?

Like the FAQ says, you should be able to use the "export" keyword to
hide your template implementation, but as it also indicates, Comeau
C++ is the only compiler that supports the language feature.

Another way that you might want to solve this problem is with design.
You could create a template that uses another class, either as a base
or as a member. You would make that class a non-template and as
generic as possible. You could then hide the definition in the
implementation, and make the template use the interface of the
non-template class.

I used this method recently to put platform-specific code in a base
class, and had my template inherit from the base. The base
definitions all existed in an implementation file that I could change
from platform to platform. The intent is different, but the principle
is the same.

HTH,

John Moeller
John Moeller

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
John Moeller
Guest





PostPosted: Sun Apr 29, 2007 5:44 pm    Post subject: Re: Do templates show source code? Reply with quote

On Sat, 28 Apr 2007 23:29:05 CST, Michael Tiomkin wrote:

Quote:
Well, you can use two methods to hide your templates.
I. Avoid publishing sensitive templates.
Define base classes, define your "sensitive" interfaces
as functions/objects accepting ptrs to these base classes,
and demand that the clients will define their classes that inherit
from
these base classes. This means that you do not publish your templates
for the users.

I wouldn't say that this is the only way to accomplish what you need.
You could also provide other "lightweight" classes/templates that use
the interface classes/functions, but don't have any "sensitive"
implementation. Though I do agree that incorporating an abstract/pure
virtual base into your design is a good way to facilitate
communication.

Quote:
II. Do remote compilation.
Compile the template functions/methods on your server. As a part of
your SDK,
prepare scripts that send the .h files defining the user classes
needed
for these templates, and obtain the object (.o or .obj) files of the
methods
as a result - you can use rsh, rpc, COM or CORBA to do that. Recall
that
you can simply send a compressed .i file to your compiler. You have
to use
the same compiler on your server, therefore you should also send
the exact version and type of the user compiler to the server.

Does anyone do this? It seems awfully expensive to provide such a
service (yes, I know Comeau does it, but they accept donations to
offset the cost, and they don't provide object code in return; just a
guarantee of successful or erroneous compilation). Then there's the
overhead, and uptime guarantees; I'd pick a different library vendor
if I were a client.

Quote:
II'. Do "local" remote compilation.
Demand that the users install a special server that does II. You
should have
exclusive access to this server, at least to the directories
containing
the template definitions. The script and the compiler would better be
readable
for everyone, because in this case the user will think that his
"sensitive"
code doesn't leak outside. In this case the rsh/rpc will be much
faster.

This seems futile. You'd have to go through a lot of trouble and
expense to provide servers that you can't just rootkit in five
minutes. Additionally, you'd have to maintain the machinery, provide
upgrades, etc.; that means hiring staff to perform the tasks. It
seems like a lot of effort just to protect source code.

If you really can't trust the customer with source code, don't provide
it. If you can, make them sign an NDA if you don't want it public.
These seem to be the common methods for closed-source software, at
least the ones that I have run across.

John Moeller
John Moeller

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Mathias Gaunard
Guest





PostPosted: Mon Apr 30, 2007 5:57 pm    Post subject: Re: Do templates show source code? Reply with quote

On Apr 28, 10:30 pm, Bogus Exception <bogusexcept...@gmail.com> wrote:

Quote:
So if all this is right (might not be!), then is it possible to
distribute "sensitive" source code via templates in object files for
later inclusion, or are there better mechanisms/tricks in the language
that allow the same kind of functionality?

Why is hiding the source so important?
What in the code is so sensitive that you can't show it to people
acquiring your software?



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Mathias Gaunard
Guest





PostPosted: Wed May 09, 2007 2:53 am    Post subject: Re: Do templates show source code? Reply with quote

On May 4, 5:59 pm, Bogus Exception <bogusexcept...@gmail.com> wrote:
Quote:
I can tell you from personal experience that _if_ any part of a
product's code base can be used to let an Evil(TM) party gain
advantage, they will.

Showing it doesn't mean letting people use it to do "evil".
Just limit what can be done with a well-defined license, restricting
copy and redistribution rights.

I believe using technical methods to prevent lawsuits is an
aberration.
I am, however, quite an idealist, and know little about the business
world.

For the templates issue, I don't think there is much that can be done,
outside of export maybe that might obfuscate the code a little bit.
It is however not well supported, and it is actually advised not to
support it by the only ones that ever implemented it.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Bogus Exception
Guest





PostPosted: Wed May 09, 2007 3:09 pm    Post subject: Re: Do templates show source code? Reply with quote

On May 8, 10:53 pm, Mathias Gaunard <loufo...@gmail.com> wrote:
Quote:
Showing it doesn't mean letting people use it to do "evil".
Just limit what can be done with a well-defined license, restricting
copy and redistribution rights.

Mathias,

Thanks for the post. I'll let the non-C++ issue go by saying that no
licensing covers lying on the stand.

I appreciate enlightening me about the template issue. It has taught
me a lot about the language, actually.

Before _I_ was sued for someone stealing _my_ code, I was an optimist,
too! :)

Thanks!

Bogus Exception


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.