<div dir="ltr"><div>> <span style="font-size:12.8px">scoping is (mostly) implicit</span><br></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">I agree here. Not sure why I have been fixated on this ornamental prefixing/casting, but I can do away with it. (top) would still exist and any future scopes that are invented which fall into req/bereq would also have to be casted in this way.</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">> variable uses out of order</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Right now this seems to be a limitation in the vcc. Im tempted to say this would simply be irksome for the time being. I would have to research how this could be addressed, but im feeling this fix is out of scope right now (and can be fixed independently of this VIP).</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">> </span><span style="font-size:12.8px">uninitialised objects</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">This is a very valid concern and good thing you brought this up. When I first read it, I thought about how other languages tackle this, in particular, Java and C. Both languages either error out or warn at compile time about uninitialized variables. So I think we could use the same approach. If you define a variable inside of a control statement (if/else), then that's a compile error due to it possibly being uninitialized and unsafe. So this definitely needs to be addressed and fixed.</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">There is also some weirdness around defining variables inside of custom subs. This worked on 4.0, but I think something changed in 5.0. So this needs to be fixed too :)</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">> </span><span style="font-size:12.8px">proper exception mechanism</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Totally agree, right now you can only assert and take down the entire process. There are many times where it would be nice to just have a 'request panic' and kill the request, but leave everything else intact. Not sure if this feature is in scope for this VIP, but its definitely something that VMODs and Varnish code in general needs access to.</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">> </span><span style="font-size:12.8px">scope limits as vmod metadata</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Ya, I thought about this and it probably makes sense to add this in just to give VMOD authors more assurances that their objects will be used in the way they expect. In theory, it shouldn't matter, but obviously in practice there is a big difference in VCL global vs request vs [insert random scope here]. So I will see if I can address this as well with some new vcc definition syntax.</span></div><div><span style="font-size:12.8px"><br></span></div><div>This is good feedback, thank you.<span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px"><br></span></div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr">--<br>Reza Naghibi<br>Varnish Software</div></div></div>
<br><div class="gmail_quote">On Fri, Nov 11, 2016 at 6:06 AM, Martin Blix Grydeland <span dir="ltr"><<a href="mailto:martin@varnish-software.com" target="_blank">martin@varnish-software.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi Reza,<div><br></div><div>I like this a lot. Though I am not completely sure about the way the scoping is expressed. It seems to me that the scoping is (mostly) implicit from where the new keyword is placed. If it's in vcl_backend_*, the scope is bereq. If it's in vcl_init the scope is global, and if it is in the others the scope is req (except when it's top). This leaves top as the only odd scope, and if we found some other way to deal with that special case, we would not need the scope cast in most places making it prettier. Perhaps only require the scope cast for (top)?</div><div><br></div><div>Also how the scoping tests are performed in the vcc compiler leaves things to be desired. The first attached test case shows something that fails as a compiler error, but that I believe should be perfectly valid VCL (and the ordering is certainly something I've seen in plenty of messy user VCLs). In the test the vcl_deliver method comes before the vcl_recv in the input text, and the scoping then errors out on seeing the object in vcl_deliver that is defined in vcl_recv. The execution of vcl_recv is always before vcl_deliver, so the code isn't wrong, and would work just fine if it wasn't for the use of the object. I assume that the same issue is also present for vcl_init today. I believe we should address this, and could do so by enforcing an order in which the VCL functions are compiled. So after having read all of the source code, the compiler should in order do vcl_init, vcl_recv ... vcl_deliver, vcl_synth, vcl_backend_*.</div><div><br></div><div>The 2nd attached test case demonstrates another concern on uninitialised objects. This test case fails with an assertion in the debug vmod for obvious reasons. My concern is that it is tempting to write VCL code like this, and having the Varnish assert itself by a simple VCL mistake is bad. (I do believe that similar issues exist for our current vcl_init-only director objects, though it's less error prone because noone writes if-clauses in vcl_init).</div><div><br></div><div>One way of looking at this issue is to say that the debug object used here is in the wrong to assert when it's uninitialised, and that the uninitialised state is a valid vmod object state. The vmods then need to define correct behaviour for these cases (e.g. do nothing for the right meaning of nothing, for this case I guess return the empty string). And we should formally define this in the nonexistant vmod developer guide.</div><div><br></div><div>Another approach is to define that object initialisation can't fail, which is more in-spirit with the VCL language. So make the VCC compiler give compiler errors if it can't say for sure that the new statement for an object has been executed before the object method invocation. E.g. an object method can't be used in vcl_recv for an object that is created in vcl_deliver. This would then drag with it new scopes for every curly bracket opening inside of the VCL functions. So an object can be new'ed and used inside the if-statement, but is out-of-scope outside of it.</div><div><br></div><div>If we do define that object initialisation can't fail, I would also like to see a proper exception mechanism build in. So vmods can throw exceptions that halt VCL execution and safely call the priv free callbacks to clean up, before presenting an error. Details unclear.</div><div><br></div><div>Lastly I think we should have the calling scope limits as vmod metadata in place before allowing this. So a vmod object that's only meant to be called in a global scope can declare that in its vcc file.</div><div><br></div><div>So in conclusion I am concerned about the Pandora's box this is opening, and there are building blocks that are missing today that should be addressed first.</div><div><br></div><div>Martin</div><br><div class="gmail_quote"><div><div class="h5"><div dir="ltr">On Wed, 9 Nov 2016 at 23:13 Reza Naghibi <<a href="mailto:reza@varnish-software.com" target="_blank">reza@varnish-software.com</a>> wrote:<br></div></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><div dir="ltr" class="m_-2764318201371632491gmail_msg">This is another discussion for VIP9. The previous mailing list thread is linked below [0].<div class="m_-2764318201371632491gmail_msg"><br class="m_-2764318201371632491gmail_msg"></div><div class="m_-2764318201371632491gmail_msg">This allows objects to live in req, req.top, and bereq scopes. Currently objects are global to the VCL and can only be defined in vcl_init.</div><div class="m_-2764318201371632491gmail_msg"><br class="m_-2764318201371632491gmail_msg"></div><div class="m_-2764318201371632491gmail_msg">The driver for this is allowing for multiple VMOD objects to exist in a single request, each with their own attributes. There is a usecase where we need to make multiple HTTP requests from VCL to 3rd party services and then build multiple security related digests on several aspects of the request, response, and 3rd party services. So having proper objects here would make the VMOD based solution extremely clean and easy to understand.</div><div class="m_-2764318201371632491gmail_msg"><br class="m_-2764318201371632491gmail_msg"></div><div class="m_-2764318201371632491gmail_msg">I have a branch ready for a PR, but it was requested to have another discussion. Branch is located here [1] and based on master, 5.0, as of last week. m00026.vtc shows how the VCL looks [2]. The goal would be to have this available to VMODs in the next major release, Q1 2017.</div><div class="m_-2764318201371632491gmail_msg"><br class="m_-2764318201371632491gmail_msg"></div><div class="m_-2764318201371632491gmail_msg">I will briefly address several of the concerns that have been brought up:</div><div class="m_-2764318201371632491gmail_msg"><br class="m_-2764318201371632491gmail_msg"></div><div class="m_-2764318201371632491gmail_msg">* Syntax is too verbose. I agree that the first iteration kind of went overboard with the syntax. Currently, the syntax requires a (scope) style cast when you define the object and thats it. Please see [2].</div><div class="m_-2764318201371632491gmail_msg"><br class="m_-2764318201371632491gmail_msg"></div><div class="m_-2764318201371632491gmail_msg">* This implementation is completely backwards compatible with how objects are defined in 4.X and does not require VMOD code to change. Object VMODs will be immediately available to these new scopes and will still go thru the _init() _fini() lifecycle.</div><div class="m_-2764318201371632491gmail_msg"><br class="m_-2764318201371632491gmail_msg"></div><div class="m_-2764318201371632491gmail_msg">* Conflicts with VIP1. This VIP is only concerned with exposing PRIV_TASK and PRIV_TOP into existing objects at the VCL level. I believe VIP1 is concerned with PRIV_* at the VMOD level. So I believe these are a bit orthogonal and independent.</div><div class="m_-2764318201371632491gmail_msg"><br class="m_-2764318201371632491gmail_msg"></div><div class="m_-2764318201371632491gmail_msg">* VMOD safety. Im pretty confident that VMOD objects actually allow for higher levels of reference and memory safety because objects have explicit _init() and _fini() methods and are passed in a struct for tracking state. So if you wanted to reference objects from other objects, then you could implement your own ref counting algorithm ontop of __init(), __finish(), and the state (or a gc implementation or whatever algorithm you want). I think this argument is a bit arbitrary because if we look at something as simple as VCL_STRING, which all VMODs have access to via req/resp headers, if you were to reference that in a VMOD, it will be freed from under you and leave you with unsafe code and memory. So any kind of unsafe VMOD example probably extends to the entire VMOD universe and should not be limited to this one VIP. Safe coding practices used today in VMODs should not be abandoned in context of this VIP :)</div><div class="m_-2764318201371632491gmail_msg"><br class="m_-2764318201371632491gmail_msg"></div><div class="m_-2764318201371632491gmail_msg">It might be more helpful to see the compiled VCL code interact with the VRT to understand how this PR will work. So the C code for m00026.vtc is located here [3]. Just grep for g0, r0, r1, t0, and b0 to better see how this is implemented.<br class="m_-2764318201371632491gmail_msg"><div class="m_-2764318201371632491gmail_msg"><br class="m_-2764318201371632491gmail_msg"></div><div class="m_-2764318201371632491gmail_msg">[0] <a href="https://www.varnish-cache.org/lists/pipermail/varnish-dev/2016-April/008906.html" class="m_-2764318201371632491gmail_msg" target="_blank">https://www.varnish-cache.org/<wbr>lists/pipermail/varnish-dev/<wbr>2016-April/008906.html</a></div><div class="m_-2764318201371632491gmail_msg"><br class="m_-2764318201371632491gmail_msg"></div><div class="m_-2764318201371632491gmail_msg">[1] <a href="https://github.com/varnishcache/varnish-cache/compare/master...rezan:feature/object_scopes_master" class="m_-2764318201371632491gmail_msg" target="_blank">https://github.com/<wbr>varnishcache/varnish-cache/<wbr>compare/master...rezan:<wbr>feature/object_scopes_master</a></div><div class="m_-2764318201371632491gmail_msg"><br class="m_-2764318201371632491gmail_msg"></div><div class="m_-2764318201371632491gmail_msg">[2] <a href="https://github.com/rezan/varnish-cache/blob/feature/object_scopes_master/bin/varnishtest/tests/m00026.vtc" class="m_-2764318201371632491gmail_msg" target="_blank">https://github.com/rezan/<wbr>varnish-cache/blob/feature/<wbr>object_scopes_master/bin/<wbr>varnishtest/tests/m00026.vtc</a></div><div class="m_-2764318201371632491gmail_msg"><br class="m_-2764318201371632491gmail_msg"></div><div class="m_-2764318201371632491gmail_msg">[3] <a href="https://gist.github.com/rezan/76f92f76d31ee2d2105501b63612db43" class="m_-2764318201371632491gmail_msg" target="_blank">https://gist.github.com/<wbr>rezan/<wbr>76f92f76d31ee2d2105501b63612db<wbr>43</a></div><div class="m_-2764318201371632491gmail_msg"><br class="m_-2764318201371632491gmail_msg"></div><div class="m_-2764318201371632491gmail_msg"><div class="m_-2764318201371632491gmail_msg"><div class="m_-2764318201371632491m_2593724360106898599gmail_signature m_-2764318201371632491gmail_msg"><div dir="ltr" class="m_-2764318201371632491gmail_msg">--<br class="m_-2764318201371632491gmail_msg">Reza Naghibi<br class="m_-2764318201371632491gmail_msg">Varnish Software</div></div></div>
</div></div></div></div></div>
______________________________<wbr>_________________<br class="m_-2764318201371632491gmail_msg">
varnish-dev mailing list<br class="m_-2764318201371632491gmail_msg">
<a href="mailto:varnish-dev@varnish-cache.org" class="m_-2764318201371632491gmail_msg" target="_blank">varnish-dev@varnish-cache.org</a><br class="m_-2764318201371632491gmail_msg">
<a href="https://www.varnish-cache.org/lists/mailman/listinfo/varnish-dev" rel="noreferrer" class="m_-2764318201371632491gmail_msg" target="_blank">https://www.varnish-cache.org/<wbr>lists/mailman/listinfo/<wbr>varnish-dev</a></blockquote></div></div>
</blockquote></div><br></div>