<div dir="ltr">Thanks again for the feedback guys.<div><br></div><div>I've committed this, with some modifications, as r211956.</div><div><br></div><div>Based on feedback from Dave Blaikie, Nick Kledzik and Jim Grosbach I've extended the original language with two new features: Bit-slicing and basic disassembler support.</div>
<div><br></div><div>Bit-slicing provides a compact syntax for accessing subsections of a word: Following any simple expression, you can append the syntax [ <high-bit-index> : <low-bit-index> ] to extract that bit-range. This is handy for testing situations where some value is split across multiple instructions.</div>
<div><br></div><div>The disassembler support consists of two built-in functions: decode_operand and next_pc. These provide access to instruction operands (handy when extracting operands with weird encodings), and the PC of the next instruction (for PC-relative expressions).</div>
<div><br></div><div>Ongoing comments, suggestions and patches are of course welcome.</div><div><br></div><div>- Lang.</div><div>  </div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Jun 24, 2014 at 11:36 AM, Lang Hames <span dir="ltr"><<a href="mailto:lhames@gmail.com" target="_blank">lhames@gmail.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">It may help the discussion if I actually attach the correct patch:<div><br></div><div>- Lang.</div><div>
<br></div><div><br></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Jun 24, 2014 at 10:57 AM, Lang Hames <span dir="ltr"><<a href="mailto:lhames@gmail.com" target="_blank">lhames@gmail.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 All,<div><br></div><div>Dropping in a relevant discussion that Dave and I had off list:</div><div><br>

</div><div><span style="color:rgb(38,38,38);font-family:arial,sans-serif;font-size:13px;line-height:16px">From Dave: "In my mind it's mostly that we have a fairly general purpose tool for checking things and we do that by separating output from verification that has proved pretty valuable in other areas (by having general purpose output we can use it for better debugging/investigation, rather than just for writing test cases, for example - I can run llvm-rtdyld with the disassembly mode to see what the state in-memory looks like, with a -verify mode I can't do that)"</span><br>


</div><div><span style="color:rgb(38,38,38);font-family:arial,sans-serif;font-size:13px;line-height:16px"><br></span></div><div><font color="#262626" face="arial, sans-serif"><span style="line-height:16px">My argument against testing RuntimeDyld via examining textual output is that sensibly rendering RuntimeDyld's memory would require considerable work (see discussion below), and I don't think anybody actually wants to look at it. People have requested the ability to examine the output of the JIT, and I'm happy to add that feature if it hasn't been added already (I'll have check), but the appropriate way to do that is to dump the object files produced by the JIT before they're passed to RuntimeDyld, not to try to render RuntimeDyld's memory.</span></font></div>


<div><span style="color:rgb(38,38,38);font-family:arial,sans-serif;line-height:16px"><br></span></div><div><font color="#262626" face="arial, sans-serif"><span style="line-height:16px">As for why dumping RuntimeDyld's memory is a lot of work: RuntimeDyld really only knows about blobs of bytes (one blob per section in the object file) and addresses of symbols. Consider, for example, how the following C struct looks in assembly and to RuntimeDyld:</span></font></div>


<div><font color="#262626" face="arial, sans-serif"><span style="line-height:16px"><br></span></font></div><div><font color="#262626" face="arial, sans-serif"><span style="line-height:16px">In C:</span></font></div><div>

<font color="#262626" face="arial, sans-serif"><span style="line-height:16px"><br>
</span></font></div><div><font color="#262626" face="courier new, monospace"><span style="line-height:16px">struct {</span></font></div><div><font color="#262626" face="courier new, monospace"><span style="line-height:16px">  unsigned a;</span></font></div>


<div><font color="#262626" face="courier new, monospace"><span style="line-height:16px">  void* b;</span></font></div><div><font color="#262626" face="courier new, monospace"><span style="line-height:16px">  unsigned c;</span></font></div>


<div><font color="#262626" face="courier new, monospace"><span style="line-height:16px">} x = {1, &foo, 2};</span></font></div><div><font color="#262626" face="arial, sans-serif"><span style="line-height:16px"><br></span></font></div>


<div><font color="#262626" face="arial, sans-serif"><span style="line-height:16px">In pseudo-asm:</span></font></div><div><font color="#262626" face="arial, sans-serif"><span style="line-height:16px"><br></span></font></div>


<div><font color="#262626" face="courier new, monospace"><span style="line-height:16px">.section __data:</span></font></div><div><font color="#262626" face="courier new, monospace"><span style="line-height:16px">x:</span></font></div>


<div><font color="#262626" face="courier new, monospace"><span style="line-height:16px">  .long 1</span></font></div><div><font color="#262626" face="courier new, monospace"><span style="line-height:16px">  .quad foo</span></font></div>


<div><font color="#262626" face="courier new, monospace"><span style="line-height:16px">  .long 2</span></font></div><div><font color="#262626" face="arial, sans-serif"><span style="line-height:16px"><br></span></font></div>


<div><font color="#262626" face="arial, sans-serif"><span style="line-height:16px">To RuntimeDyld:</span></font></div><div><font color="#262626" face="arial, sans-serif"><span style="line-height:16px"><br></span></font></div>


<div><font color="#262626" face="courier new, monospace"><span style="line-height:16px">x: 0x01 0x00 0x00 0x00 0xEF 0xBE 0xAD 0xDE 0xCE 0xFA 0xED 0xFE 0x02 0x00 0x00 0x00</span></font></div><div><font color="#262626" face="arial, sans-serif"><span style="line-height:16px"><br>


</span></font></div><div><font color="#262626" face="arial, sans-serif"><span style="line-height:16px"><br></span></font></div><div><font color="#262626" face="arial, sans-serif"><span style="line-height:16px">RuntimeDyld doesn't know where the fields of x start or end, or indeed where x itself ends. We could add extra labels to the assembly code to delineate the fields and teach RuntimeDyld to render the bytes between each symbol, and how to recognize common integer sizes and render them appropriately (so that you don't have to write expressions to reassemble ints from the individual bytes). But all of that complicates both the test cases themselves  and RuntimeDyld. None of those features would be available to a client who just wants to inspect the JIT's memory, since the code-generator doesn't (and IMHO shouldn't) pepper its output with extraneous labels on the off-chance that someone wants to pretty-print RuntimeDyld's memory.</span></font></div>


<div><font color="#262626" face="arial, sans-serif"><span style="line-height:16px"><br></span></font></div><div><span style="color:rgb(38,38,38);font-family:arial,sans-serif;font-size:13px;line-height:16px">From Dave: "in many other cases we've invented canonical textual representations to check against (objdump, dwarfdump, readelf, etc)."</span><br>


</div><div><span style="color:rgb(38,38,38);font-family:arial,sans-serif;font-size:13px;line-height:16px"><br></span></div><div><font color="#262626" face="arial, sans-serif"><span style="line-height:16px">In each of those cases there was a desire for people to be able to visually inspect the information for understanding/correctness. Once you have that functionality, inspecting dumps with FileCheck makes much more sense. In RuntimeDyld's case all the interesting information is contained in the object file, which can already be rendered with existing tools. The only thing RuntimeDyld changes is some byte values in memory. Being able to inspect them when something goes wrong tells you nothing interesting about why they were wrong, so textually rendering isn't helpful.</span></font></div>


<div><font color="#262626" face="arial, sans-serif"><span style="line-height:16px"><br></span></font></div><div><font color="#262626" face="arial, sans-serif"><span style="line-height:16px">I think directly checking the invariants makes more sense here, rather than going via text.</span></font></div>


<div><font color="#262626" face="arial, sans-serif"><span style="line-height:16px"><br></span></font></div><div><font color="#262626" face="arial, sans-serif"><span style="line-height:16px">Cheers,</span></font></div><div>


<font color="#262626" face="arial, sans-serif"><span style="line-height:16px">Lang.</span></font></div><div><br></div><div><span style="color:rgb(38,38,38);font-family:arial,sans-serif;font-size:13px;line-height:16px"><br>


</span></div></div><div><div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Jun 23, 2014 at 8:54 PM, Lang Hames <span dir="ltr"><<a href="mailto:lhames@gmail.com" target="_blank">lhames@gmail.com</a>></span> wrote:<br>


<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Dave,<br>
<br>
Jim Grosbach asked the same question, so you're in good company. With hindsight I think it was a mistake to say "FileCheck workflow". What I really meant was that this system plays well with lit. Not that your question about using FileCheck would have been any less valid.<br>



<br>
I did consider using FileCheck for this, but decided it was the wrong approach. The fundamental reason is that there's no demand for textually rendering RuntimeDyld's memory, and developing a textual renderer so that we could output text just to pattern match and re-assemble ints in FileCheck would be a lot of pain for (as far as I can see) no gain.<br>



<br>
If there's a desire for FileCheck to support expression evaluation we could flesh out this evaluator and make it available as a support library that both FileCheck and RuntimeDyld could use.<br>
<br>
You (and independently Nick Kledzik) do raise the really useful idea of leveraging the disassembler though. I like the idea of adding some special syntax to disassemble an instruction at a label and use one of its immediates. That would eliminate a lot of the bit bashing that would have been required on instruction sets with tricky immediate encodings (E.g. ARM). Something like:<br>



<br>
# rtdyld-check: @test_inst[0] = foo - (test_inst + 5)<br>
test_inst:<br>
  callq foo<br>
<br>
Cheers,<br>
Lang.<br>
<div><div><br>
> On Jun 23, 2014, at 5:01 PM, David Blaikie <<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>> wrote:<br>
><br>
>> On Mon, Jun 23, 2014 at 3:01 PM, Lang Hames <<a href="mailto:lhames@gmail.com" target="_blank">lhames@gmail.com</a>> wrote:<br>
>> Hi Everyone,<br>
>><br>
>> For your consideration: A proposal to improve regression test support for<br>
>> RuntimeDyld.<br>
><br>
> Thanks for working on this, Lang. It's great to see.<br>
><br>
>> Short version: We can make RuntimeDyld far more testable by adding a trivial<br>
>> pointer-expression language that allows us to describe how memory should<br>
>> look post-relocation. Jump down to "The Proposal" for details.<br>
><br>
> I've been trying to puzzle over what this would look like with a<br>
> possibly more general feature*.<br>
><br>
> What would testing look like if we had a rtdyld dumping mode that<br>
> printed the disassembly of the relocated machine code, and a symbol<br>
> table (or just inserted the labels for the symbols into the<br>
> disassembly?).<br>
><br>
> I understand we'd need to beef up FileCheck with slightly more<br>
> arithmetic operations - but is it really so much (& would they be so<br>
> useless for other tests) that it's not worth putting it there?<br>
><br>
> To take your example, here's my vague idea of what it might look like<br>
> to use a dump+FileCheck. The dump would look something like:<br>
><br>
> (obviously I don't know, nor for this purpose care, how big the<br>
> instructions are, just that they have distinct addresses, etc)<br>
><br>
>  0x42: bar:<br>
>  0x42:   retq<br>
>  0x43: foo:<br>
>  0x43:   callq 0x42<br>
>  0x44: inst1:<br>
>  0x44:   retq<br>
><br>
> And the FileCheck equivalent of<br>
><br>
>  # rtdyld-check: *{4}(inst1 - 4) = (bar - inst1) & 0xffffffff<br>
><br>
> would be something like:<br>
><br>
>  CHECK: [[CALL_ADDR:.*]]: bar:<br>
>  CHECK: callq [[CALL_ADDR]]<br>
><br>
> Which, I suppose, depends on disassembler working correctly, not sure<br>
> if that's high risk/complicated.<br>
><br>
> Alternatively - could llvm-rtdyld just print a simple description of<br>
> relocations its applied and the location of symbols? (similar to a<br>
> static display of relocations like llvm-objdump -r) then FileCheck<br>
> that.<br>
><br>
> * all that said, a feature like you've proposed/implemented isn't<br>
> without precedent - clang's -verify is very similar to what you've got<br>
> here<br>
><br>
><br>
>> Long version:<br>
>><br>
>> Background:<br>
>><br>
>> For those unfamiliar with it, RuntimeDyld a component of MCJIT, LLVM's JIT<br>
>> compiler infrastructure. MCJIT produces an object file in memory for each<br>
>> module that is JIT'd. RuntimeDyld's job is to apply all the relocations<br>
>> necessary to make the code in the object file runnable. In other words,<br>
>> RuntimeDyld is acting as both the static and dynamic linker for the JIT.<br>
>><br>
>> The Problem:<br>
>><br>
>> We can't directly test RuntimeDyld at the moment. We currently infer the<br>
>> correctness of RuntimeDyld indirectly from the success of the MCJIT<br>
>> regression tests - if they pass, we assume RuntimeDyld must have done its<br>
>> job right. That's far from an ideal. The biggest issues with it are:<br>
>><br>
>> (1) Each platform is testing only its own relocations and no others. I.e.<br>
>> X86 testers are testing X86 relocations only. ARM testers are testing ARM<br>
>> relocations only. If someone running on X86 breaks a relocation for ARM they<br>
>> won't see the error in their regression test run - they'll have to wait<br>
>> until an ARM buildbot breaks before they realize anything is wrong. Fixes<br>
>> for platforms that you don't have access to are difficult to test - all you<br>
>> can do is eyeball disassembled memory and see if everything looks sane. This<br>
>> is not much fun.<br>
>><br>
>> (2) Relocations are produced by CodeGen from IR, rather than described<br>
>> directly. That's a lot of machinery to have between the test-case and the<br>
>> final result. It is difficult to know what relocations each IR regression<br>
>> test is testing (and they're often incidental - we don't have a dedicated<br>
>> relocation test set). This also means that if/when the code generator<br>
>> produces different relocation types the existing tests will keep on passing<br>
>> but will silently stop testing the thing they used to test.<br>
>><br>
>> The Proposal:<br>
>><br>
>> (1) We provide a mechanism for describing how pieces of relocated memory<br>
>> should look immediately prior to execution, and then inspect the memory<br>
>> rather than executing it. This addresses point (1) above: Tests for any<br>
>> platform can be loaded, linked and verified on any platform. If you're<br>
>> coding on X86 and you break an ARM relocation you'll know about it<br>
>> immediately.<br>
>><br>
>> (2) RuntimeDyld test cases should be written in assembly, rather than IR.<br>
>> This addresses point (2) above - we can cut the code generators out and<br>
>> guarantee that we're testing what we're interested in.<br>
>><br>
>> The way to do this is to introduce a simple pointer expression language.<br>
>> This should be able to express things like: "The immediate for this call<br>
>> points at symbol foo".<br>
>><br>
>> Symbolically, what I have in mind would look something like:<br>
>><br>
>>        // some asm ...<br>
>> # assert *(inst1 + 1) = foo<br>
>> inst1:<br>
>>        callq   foo<br>
>>        // some asm...<br>
>><br>
>> Here we add the "inst1" label to give us a address from which we can get at<br>
>> the immediate for the call. The " + 1" expression skips the call opcode (we<br>
>> know the size of the opcode ahead of time, since this is assembly and so<br>
>> target-specific).<br>
>><br>
>> To verify that constraints expressed in this language hold, we can add an<br>
>> expression evaluator to the llvm-rtdyld utility, which is a command-line<br>
>> interface to RuntimeDyld.<br>
>><br>
>> I find these things are easier to discuss in the concrete, so I've attached<br>
>> a basic implementation of this idea. The following discussion is in terms of<br>
>> my patch, but I'm very open to tweaking all this.<br>
>><br>
>> The language I've implemented is:<br>
>><br>
>> test = expr '=' expr<br>
>><br>
>> expr = '*{' number '}' load_addr_expr<br>
>>     | binary_expr<br>
>>     | '(' expr ')'<br>
>>     | symbol<br>
>>     | number<br>
>><br>
>> load_addr_expr = symbol<br>
>>               | '(' symbol '+' number ')'<br>
>>               | '(' symbol '-' number ')'<br>
>><br>
>> binary_expr = expr '+' expr<br>
>>            | expr '-' expr<br>
>>            | expr '&' expr<br>
>>            | expr '|' expr<br>
>>            | expr '<<' expr<br>
>>            | expr '>>' expr<br>
>><br>
>> This expression language supports simple pointer arithmetic, shifting,<br>
>> masking and loading. All values are internally held as 64-bit unsigneds,<br>
>> since RuntimeDlyd is designed to support cross-platform linking, including<br>
>> linking for 64-bit targets from a 32-bit host. I think the only stand-out<br>
>> wart is the *{#size}<addr> syntax for loads. This comes from the fact that<br>
>> immediates aren't always 64-bits, so it's not safe to do a 64-bit load: you<br>
>> could read past the end of allocated memory. The #size field indicates how<br>
>> many bytes to read.<br>
>><br>
>> This patch adds a "-verify" option to llvm-rtdyld to attach the expression<br>
>> evaluator to a RuntimeDyld instance after linking. When -verify is passed,<br>
>> llvm-rtdyld does not execute any code. Files containing rules are passed via<br>
>> "-check=<filename>" arguments, and rules are read from any line prefixed<br>
>> with the string "# rtdyld-check: ". The intended workflow is modeled on the<br>
>> FileCheck regression tests.<br>
>><br>
>> Here's an example of what a test case for a test for an x86-64 PC-relative<br>
>> MACHO_VANILLA relocation would look like:<br>
>><br>
>> ; RUN: clang -triple x86_64-apple-macosx10.9.0 -c -o foo.o %s<br>
>> ; RUN: llvm-rtdyld -verify -check=foo.s foo.o<br>
>> ; RUN: rm foo.o<br>
>> ;<br>
>> ; Test an x86-64 PC-relative MACHO_VANILLA relocation.<br>
>><br>
>>        .text<br>
>>        .globl  bar<br>
>>        .align  16, 0x90<br>
>> bar:<br>
>>        retq<br>
>><br>
>>        .globl  foo<br>
>>        .align  16, 0x90<br>
>> foo:<br>
>> # rtdyld-check: *{4}(inst1 - 4) = (bar - inst1) & 0xffffffff<br>
>>        callq   bar<br>
>> inst1:<br>
>>        retq<br>
>><br>
>><br>
>> With this system, we could write targeted regression tests for every<br>
>> relocation type on every platform, and test them on any system. Failures<br>
>> would immediately identify which target and relocation type broke.<br>
>><br>
>> I think this system would massively improve the testability of the<br>
>> RuntimeDyld layer, which is good news in light of the increased usage MCJIT<br>
>> is getting these days.<br>
>><br>
>> Please let me know what you think. Comments and critiques are very welcome,<br>
>> both of the language and the proposed workflow.<br>
>><br>
>> Cheers,<br>
>> Lang.<br>
>><br>
>> TL;DR: lhames responds to dblaikie's incessant demand for test cases. ;)<br>
>><br>
>> _______________________________________________<br>
>> LLVM Developers mailing list<br>
>> <a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
>> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
>><br>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>