2 # Generate testcase files and Makefile fragments for DSO sorting test
3 # Copyright (C) 2021-2022 Free Software Foundation, Inc.
4 # This file is part of the GNU C Library.
6 # The GNU C Library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
11 # The GNU C Library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with the GNU C Library; if not, see
18 # <http://www.gnu.org/licenses/>.
20 """Generate testcase files and Makefile fragments for DSO sorting test
22 This script takes a small description string language, and generates
23 testcases for displaying the ELF dynamic linker's dependency sorting
24 behavior, allowing verification.
26 Testcase descriptions are semicolon-separated description strings, and
27 this tool generates a testcase from the description, including main program,
28 associated modules, and Makefile fragments for including into elf/Makefile.
30 This allows automation of what otherwise would be very laborous manual
31 construction of complex dependency cases, however it must be noted that this
32 is only a tool to speed up testcase construction, and thus the generation
33 features are largely mechanical in nature; inconsistencies or errors may occur
34 if the input description was itself erroneous or have unforeseen interactions.
36 The format of the input test description files are:
38 # Each test description has a name, lines of description,
39 # and an expected output specification. Comments use '#'.
40 testname1: <test-description-line>
41 output: <expected-output-string>
43 # Tests can be marked to be XFAIL by using 'xfail_output' instead
44 testname2: <test-description-line>
45 xfail_output: <expected-output-string>
47 # A default set of GLIBC_TUNABLES tunables can be specified, for which
48 # all following tests will run multiple times, once for each of the
49 # GLIBC_TUNABLES=... strings set by the 'tunable_option' command.
50 tunable_option: <glibc-tunable-string1>
51 tunable_option: <glibc-tunable-string2>
53 # Test descriptions can use multiple lines, which will all be merged
54 # together, so order is not important.
55 testname3: <test-description-line>
56 <test-description-line>
57 <test-description-line>
59 output: <expected-output-string>
61 # 'testname3' will be run and compared two times, for both
62 # GLIBC_TUNABLES=<glibc-tunable-string1> and
63 # GLIBC_TUNABLES=<glibc-tunable-string2>. This can be cleared and reset by the
64 # 'clear_tunables' command:
67 # Multiple expected outputs can also be specified, with an associated
68 # tunable option in (), which multiple tests will be run with each
69 # GLIBC_TUNABLES=... option tried.
71 <test-description-line>
73 output(<glibc-tunable-string1>): <expected-output-string-1>
74 output(<glibc-tunable-string2>): <expected-output-string-2>
75 # Individual tunable output cases can be XFAILed, though note that
76 # this will have the effect of XFAILing the entire 'testname4' test
77 # in the final top-level tests.sum summary.
78 xfail_output(<glibc-tunable-string3>): <expected-output-string-3>
80 # When multiple outputs (with specific tunable strings) are specified,
81 # these take priority over any active 'tunable_option' settings.
83 # When a test is meant to be placed under 'xtests' (not run under
84 # "make check", but only when "make xtests" is used), the testcase name can be
85 # declared using 'xtest(<test-name>)':
87 xtest(test-too-big1): <test-description>
88 output: <expected-output-string>
91 # Do note that under current elf/Makefile organization, for such a xtest case,
92 # while the test execution is only run under 'make xtests', the associated
93 # DSOs are always built even under 'make check'.
95 On the description language used, an example description line string:
97 a->b!->[cdef];c=>g=>h;{+c;%c;-c}->a
99 Each identifier represents a shared object module, currently sequences of
100 letters/digits are allowed, case-sensitive.
102 All such shared objects have a constructor/destructor generated for them
103 that emits its name followed by a '>' for constructors, and '<' followed by
104 its name for destructors, e.g. if the name is 'obj1', then "obj1>" and "<obj1"
105 is printed by its constructor/destructor respectively.
107 The -> operator specifies a link time dependency, these can be chained for
108 convenience (e.g. a->b->c->d).
110 The => operator creates a call-reference, e.g. for a=>b, an fn_a() function
111 is created inside module 'a', which calls fn_b() in module 'b'.
112 These module functions emit 'name()' output in nested form,
113 e.g. a=>b emits 'a(b())'
115 For single character object names, square brackets [] in the description
116 allows specifying multiple objects; e.g. a->[bcd]->e is equivalent to
117 a->b->e;a->c->e;a->d->e
119 The () parenthesis construct with space separated names is also allowed for
120 specifying objects. For names with integer suffixes a range can also be used,
121 e.g. (foo1 bar2-5), specifies DSOs foo1, bar2, bar2, bar3, bar4, bar5.
123 A {} construct specifies the main test program, and its link dependencies
124 are also specified using ->. Inside {}, a few ;-separated constructs are
126 +a Loads module a using dlopen(RTLD_LAZY|RTLD_GLOBAL)
127 ^a Loads module a using dlopen(RTLD_LAZY)
128 %a Use dlsym() to load and call fn_a()
129 @a Calls fn_a() directly.
130 -a Unloads module a using dlclose()
132 The generated main program outputs '{' '}' with all output from above
133 constructs in between. The other output before/after {} are the ordered
134 constructor/destructor output.
136 If no {} construct is present, a default empty main program is linked
137 against all objects which have no dependency linked to it. e.g. for
138 '[ab]->c;d->e', the default main program is equivalent to '{}->[abd]'
140 Sometimes for very complex or large testcases, besides specifying a
141 few explicit dependencies from main{}, the above default dependency
142 behavior is still useful to automatically have, but is turned off
143 upon specifying a single explicit {}->dso_name.
144 In this case, add {}->* to explicitly add this generation behavior:
146 # Main program links to 'foo', and all other objects which have no
147 # dependency linked to it.
150 Note that '*' works not only on main{}, but can be used as the
151 dependency target of any object. Note that it only works as a target,
152 not a dependency source.
154 The '!' operator after object names turns on permutation of its
155 dependencies, e.g. while a->[bcd] only generates one set of objects,
156 with 'a.so' built with a link line of "b.so c.so d.so", for a!->[bcd]
157 permutations of a's dependencies creates multiple testcases with
158 different link line orders: "b.so c.so d.so", "c.so b.so d.so",
159 "b.so d.so c.so", etc. Note that for a <test-name> specified on
160 the script command-line, multiple <test-name_1>, <test-name_2>, etc.
161 tests will be generated (e.g. for a!->[bc]!->[de], eight tests with
162 different link orders for a, b, and c will be generated)
164 It is possible to specify the ELF soname field for an object or the
166 # DSO 'a' will be linked with the appropriate -Wl,-soname=x setting
168 # The the main program can also have a soname specified
171 This can be used to test how ld.so behaves when objects and/or the
172 main program have such a field set.
175 Strings Output by Generated Testcase Programs
177 The text output produced by a generated testcase consists of three main
179 1. The constructors' output
180 2. Output from the main program
181 3. Destructors' output
183 To see by example, a simple test description "a->b->c" generates a testcase
184 that when run, outputs: "c>b>a>{}<a<b<c"
186 Each generated DSO constructor prints its name followed by a '>' character,
187 and the "c>b>a" part above is the full constructor output by all DSOs, the
188 order indicating that DSO 'c', which does not depend on any other DSO, has
189 its constructor run first, followed by 'b' and then 'a'.
191 Destructor output for each DSO is a '<' character followed by its name,
192 reflecting its reverse nature of constructors. In the above example, the
193 destructor output part is "<a<b<c".
195 The middle "{}" part is the main program. In this simple example, nothing
196 was specified for the main program, so by default it is implicitly linked
197 to the DSO 'a' (with no other DSOs depending on it) and only prints the
198 brackets {} with no actions inside.
200 To see an example with actions inside the main program, lets see an example
201 description: c->g=>h;{+c;%c;-c}->a->h
203 This produces a testcase, that when executed outputs:
204 h>a>{+c[g>c>];%c();-c[<c<g];}<a<h
206 The constructor and destructor parts display the a->h dependency as expected.
207 Inside the main program, the "+c" action triggers a dlopen() of DSO 'c',
208 causing another chain of constructors "g>c>" to be triggered. Here it is
209 displayed inside [] brackets for each dlopen call. The same is done for "-c",
212 The "%c" output is due to calling to fn_c() inside DSO 'c', this comprises
213 of two parts: the '%' character is printed by the caller, here it is the main
214 program. The 'c' character is printed from inside fn_c(). The '%' character
215 indicates that this is called by a dlsym() of "fn_c". A '@' character would
216 mean a direct call (with a symbol reference). These can all be controlled
217 by the main test program constructs documented earlier.
219 The output strings described here is the exact same form placed in
220 test description files' "output: <expected output>" line.
228 from collections
import OrderedDict
231 # BUILD_GCC is only used under the --build option,
232 # which builds the generated testcase, including DSOs using BUILD_GCC.
233 # Mainly for testing purposes, especially debugging of this script,
234 # and can be changed here to another toolchain path if needed.
238 parser
= argparse
.ArgumentParser("")
239 parser
.add_argument("description",
240 help="Description string of DSO dependency test to be "
241 "generated (see script source for documentation of "
242 "description language), either specified here as "
243 "command line argument, or by input file using "
244 "-f/--description-file option",
245 nargs
="?", default
="")
246 parser
.add_argument("test_name",
247 help="Identifier for testcase being generated",
248 nargs
="?", default
="")
249 parser
.add_argument("--objpfx",
250 help="Path to place generated files, defaults to "
251 "current directory if none specified",
252 nargs
="?", default
="./")
253 parser
.add_argument("-m", "--output-makefile",
254 help="File to write Makefile fragment to, defaults to "
255 "stdout when option not present",
256 nargs
="?", default
="")
257 parser
.add_argument("-f", "--description-file",
258 help="Input file containing testcase descriptions",
259 nargs
="?", default
="")
260 parser
.add_argument("--build", help="After C testcase generated, build it "
261 "using gcc (for manual testing purposes)",
263 parser
.add_argument("--debug-output",
264 help="Prints some internal data "
265 "structures; used for debugging of this script",
269 # Main script starts here.
270 cmdlineargs
= get_parser().parse_args()
271 test_name
= cmdlineargs
.test_name
272 description
= cmdlineargs
.description
273 objpfx
= cmdlineargs
.objpfx
274 description_file
= cmdlineargs
.description_file
275 output_makefile
= cmdlineargs
.output_makefile
277 default_tunable_options
= []
279 current_input_lineno
= 0
281 global current_input_lineno
282 print("Error: %s%s" % ((("Line %d, " % current_input_lineno
)
283 if current_input_lineno
!= 0 else ""),
287 if(test_name
or description
) and description_file
:
288 error("both command-line testcase and input file specified")
289 if test_name
and not description
:
290 error("command-line testcase name without description string")
292 # Main class type describing a testcase.
295 self
.objs
= [] # list of all DSO objects
296 self
.deps
= OrderedDict() # map of DSO object -> list of dependencies
298 # map of DSO object -> list of call refs
299 self
.callrefs
= OrderedDict()
301 # map of DSO object -> list of permutations of dependencies
302 self
.dep_permutations
= OrderedDict()
304 # map of DSO object -> SONAME of object (if one is specified)
305 self
.soname_map
= OrderedDict()
307 # list of main program operations
308 self
.main_program
= []
309 # set if default dependencies added to main
310 self
.main_program_default_deps
= True
312 self
.test_name
= "" # name of testcase
313 self
.expected_outputs
= OrderedDict() # expected outputs of testcase
314 self
.xfail
= False # set if this is a XFAIL testcase
315 self
.xtest
= False # set if this is put under 'xtests'
317 # Add 'object -> [object, object, ...]' relations to CURR_MAP
318 def __add_deps_internal(self
, src_objs
, dst_objs
, curr_map
):
321 if not src
in curr_map
:
323 if not dst
in curr_map
[src
]:
324 curr_map
[src
].append(dst
)
325 def add_deps(self
, src_objs
, dst_objs
):
326 self
.__add
_deps
_internal
(src_objs
, dst_objs
, self
.deps
)
327 def add_callrefs(self
, src_objs
, dst_objs
):
328 self
.__add
_deps
_internal
(src_objs
, dst_objs
, self
.callrefs
)
330 # Process commands inside the {} construct.
331 # Note that throughout this script, the main program object is represented
333 def process_main_program(test_descr
, mainprog_str
):
335 test_descr
.main_program
= mainprog_str
.split(';')
336 for s
in test_descr
.main_program
:
337 m
= re
.match(r
"^([+\-%^@])([0-9a-zA-Z]+)$", s
)
339 error("'%s' is not recognized main program operation" % (s
))
342 if not obj
in test_descr
.objs
:
343 test_descr
.objs
.append(obj
)
344 if opr
== '%' or opr
== '@':
345 test_descr
.add_callrefs(['#'], [obj
])
346 # We have a main program specified, turn this off
347 test_descr
.main_program_default_deps
= False
349 # For(a1 a2 b1-12) object set descriptions, expand into an object list
350 def expand_object_set_string(descr_str
):
352 descr_list
= descr_str
.split()
353 for descr
in descr_list
:
354 m
= re
.match(r
"^([a-zA-Z][0-9a-zA-Z]*)(-[0-9]+)?$", descr
)
356 error("'%s' is not a valid object set description" % (descr
))
360 if not obj
in obj_list
:
363 idx_end
= int(idx_end
[1:])
364 m
= re
.match(r
"^([0-9a-zA-Z][a-zA-Z]*)([0-9]+)$", obj
)
366 error("object description '%s' is malformed" % (obj
))
367 obj_name
= m
.group(1)
368 idx_start
= int(m
.group (2))
369 if idx_start
> idx_end
:
370 error("index range %s-%s invalid" % (idx_start
, idx_end
))
371 for i
in range(idx_start
, idx_end
+ 1):
372 o
= obj_name
+ str(i
)
373 if not o
in obj_list
:
378 tokenspec
= [ ("SONAME", r
"soname\(([0-9a-zA-Z{}]+)\)=([0-9a-zA-Z]+)"),
379 ("OBJ", r
"([0-9a-zA-Z]+)"),
382 ("OBJSET", r
"\[([0-9a-zA-Z]+)\]"),
383 ("OBJSET2", r
"\(([0-9a-zA-Z \-]+)\)"),
385 ("PROG", r
"{([0-9a-zA-Z;+^\-%@]*)}"),
389 tok_re
= '|'.join('(?P<%s>%s)' % pair
for pair
in tokenspec
)
391 # Main line parser of description language
392 def parse_description_string(t
, descr_str
):
393 # State used when parsing dependencies
397 def clear_dep_state():
398 nonlocal in_dep
, in_callref
399 in_dep
= in_callref
= False
401 for m
in re
.finditer(tok_re
, descr_str
):
405 s
= re
.match(r
"soname\(([0-9a-zA-Z{}]+)\)=([0-9a-zA-Z]+)", value
)
409 if '#' in t
.soname_map
:
410 error("soname of main program already set")
411 # Adjust to internal name
414 if re
.match(r
"[{}]", obj
):
415 error("invalid object name '%s'" % (obj
))
416 if not obj
in t
.objs
:
417 error("'%s' is not name of already defined object" % (obj
))
418 if obj
in t
.soname_map
:
419 error("'%s' already has soname of '%s' set"
420 % (obj
, t
.soname_map
[obj
]))
421 t
.soname_map
[obj
] = val
425 t
.add_deps(curr_objs
, [value
])
427 t
.add_callrefs(curr_objs
, [value
])
430 if not value
in t
.objs
:
433 elif kind
== "OBJSET":
434 objset
= value
[1:len(value
)-1]
436 t
.add_deps(curr_objs
, list (objset
))
438 t
.add_callrefs(curr_objs
, list (objset
))
440 curr_objs
= list(objset
)
441 for o
in list(objset
):
445 elif kind
== "OBJSET2":
446 descr_str
= value
[1:len(value
)-1]
448 objs
= expand_object_set_string(descr_str
)
450 error("empty object set '%s'" % (value
))
452 t
.add_deps(curr_objs
, objs
)
454 t
.add_callrefs(curr_objs
, objs
)
461 elif kind
== "OBJSET3":
463 t
.add_deps(curr_objs
, ['*'])
465 t
.add_callrefs(curr_objs
, ['*'])
467 error("non-dependence target set '*' can only be used "
468 "as target of ->/=> operations")
472 elif kind
== "PERMUTE":
473 if in_dep
or in_callref
:
474 error("syntax error, permute operation invalid here")
476 error("syntax error, no objects to permute here")
478 for obj
in curr_objs
:
479 if not obj
in t
.dep_permutations
:
480 # Signal this object has permuted dependencies
481 t
.dep_permutations
[obj
] = []
485 error("cannot have more than one main program")
487 error("objects cannot have dependency on main program")
489 # TODO: A DSO can resolve to a symbol in the main binary,
490 # which we syntactically allow here, but haven't yet
492 t
.add_callrefs(curr_objs
, ["#"])
493 process_main_program(t
, value
[1:len(value
)-1])
498 if in_dep
or in_callref
:
499 error("syntax error, multiple contiguous ->,=> operations")
501 error("non-dependence target set '*' can only be used "
502 "as target of ->/=> operations")
505 elif kind
== "CALLREF":
506 if in_dep
or in_callref
:
507 error("syntax error, multiple contiguous ->,=> operations")
509 error("non-dependence target set '*' can only be used "
510 "as target of ->/=> operations")
513 elif kind
== "SEMICOL":
518 error("unknown token '%s'" % (value
))
521 # Main routine to process each testcase description
522 def process_testcase(t
):
526 base_test_name
= t
.test_name
527 test_subdir
= base_test_name
+ "-dir"
528 testpfx
= objpfx
+ test_subdir
+ "/"
529 test_srcdir
= "dso-sort-tests-src/"
530 testpfx_src
= objpfx
+ test_srcdir
532 if not os
.path
.exists(testpfx
):
534 if not os
.path
.exists(testpfx_src
):
535 os
.mkdir(testpfx_src
)
537 def find_objs_not_depended_on(t
):
538 objs_not_depended_on
= []
541 for r
in t
.deps
.items():
546 objs_not_depended_on
.append(obj
)
547 return objs_not_depended_on
549 non_dep_tgt_objs
= find_objs_not_depended_on(t
)
555 t
.add_deps([obj
], non_dep_tgt_objs
)
556 if obj
in t
.callrefs
:
557 deps
= t
.callrefs
[obj
]
560 t
.add_callrefs([obj
], non_dep_tgt_objs
)
565 t
.add_deps(["#"], non_dep_tgt_objs
)
567 # If no main program was specified in dependency description, make a
568 # default main program with deps pointing to all DSOs which are not
569 # depended by another DSO.
570 if t
.main_program_default_deps
:
571 main_deps
= non_dep_tgt_objs
573 error("no objects for default main program to point "
574 "dependency to(all objects strongly connected?)")
575 t
.add_deps(["#"], main_deps
)
578 if cmdlineargs
.debug_output
:
579 print("Testcase: %s" % (t
.test_name
))
580 print("All objects: %s" % (t
.objs
))
581 print("--- Static link dependencies ---")
582 for r
in t
.deps
.items():
583 print("%s -> %s" % (r
[0], r
[1]))
584 print("--- Objects whose dependencies are to be permuted ---")
585 for r
in t
.dep_permutations
.items():
587 print("--- Call reference dependencies ---")
588 for r
in t
.callrefs
.items():
589 print("%s => %s" % (r
[0], r
[1]))
590 print("--- main program ---")
591 print(t
.main_program
)
593 # Main testcase generation routine, does Makefile fragment generation,
594 # testcase source generation, and if --build specified builds testcase.
595 def generate_testcase(test_descr
, test_suffix
):
597 test_name
= test_descr
.test_name
+ test_suffix
599 # Print out needed Makefile fragments for use in glibc/elf/Makefile.
601 for o
in test_descr
.objs
:
602 rule
= ("$(objpfx)" + test_subdir
+ "/" + test_name
603 + "-" + o
+ ".os: $(objpfx)" + test_srcdir
604 + test_name
+ "-" + o
+ ".c\n"
605 "\t$(compile.c) $(OUTPUT_OPTION)\n")
606 makefile
.write (rule
)
607 module_names
+= " " + test_subdir
+ "/" + test_name
+ "-" + o
608 makefile
.write("modules-names +=%s\n" % (module_names
))
610 # Depth-first traversal, executing FN(OBJ) in post-order
612 def dfs_rec(obj
, fn
, obj_visited
):
613 if obj
in obj_visited
:
615 obj_visited
[obj
] = True
617 for dep
in t
.deps
[obj
]:
618 dfs_rec(dep
, fn
, obj_visited
)
623 dfs_rec(obj
, fn
, obj_visited
)
625 # Generate link dependencies for all DSOs, done in a DFS fashion.
626 # Usually this doesn't need to be this complex, just listing the direct
627 # dependencies is enough. However to support creating circular
628 # dependency situations, traversing it by DFS and tracking processing
629 # status is the natural way to do it.
632 def gen_link_deps(obj
):
633 if obj
in test_descr
.deps
:
634 dso
= test_subdir
+ "/" + test_name
+ "-" + obj
+ ".so"
636 for dep
in test_descr
.deps
[obj
]:
637 if dep
in obj_processed
:
638 depstr
= (" $(objpfx)" + test_subdir
+ "/"
639 + test_name
+ "-" + dep
+ ".so")
641 # A circular dependency is satisfied by making a
642 # fake DSO tagged with the correct SONAME
643 depstr
= (" $(objpfx)" + test_subdir
+ "/"
644 + test_name
+ "-" + dep
+ ".FAKE.so")
645 # Create empty C file and Makefile fragments for fake
646 # object. This only needs to be done at most once for
648 if not dep
in fake_created
:
649 f
= open(testpfx_src
+ test_name
+ "-" + dep
653 # Generate rule to create fake object
655 ("LDFLAGS-%s = -Wl,--no-as-needed "
657 % (test_name
+ "-" + dep
+ ".FAKE.so",
658 ("$(objpfx)" + test_subdir
+ "/"
659 + test_name
+ "-" + dep
+ ".so")))
660 rule
= ("$(objpfx)" + test_subdir
+ "/"
661 + test_name
+ "-" + dep
+ ".FAKE.os: "
662 "$(objpfx)" + test_srcdir
663 + test_name
+ "-" + dep
+ ".FAKE.c\n"
664 "\t$(compile.c) $(OUTPUT_OPTION)\n")
665 makefile
.write (rule
)
667 ("modules-names += %s\n"
669 + test_name
+ "-" + dep
+ ".FAKE"))
670 fake_created
[dep
] = True
671 dependencies
+= depstr
672 makefile
.write("$(objpfx)%s:%s\n" % (dso
, dependencies
))
673 # Mark obj as processed
674 obj_processed
[obj
] = True
676 dfs(test_descr
, gen_link_deps
)
678 # Print LDFLAGS-* and *-no-z-defs
679 for o
in test_descr
.objs
:
680 dso
= test_name
+ "-" + o
+ ".so"
681 ldflags
= "-Wl,--no-as-needed"
682 if o
in test_descr
.soname_map
:
683 soname
= ("$(objpfx)" + test_subdir
+ "/"
685 + test_descr
.soname_map
[o
] + ".so")
686 ldflags
+= (" -Wl,-soname=" + soname
)
687 makefile
.write("LDFLAGS-%s = %s\n" % (dso
, ldflags
))
688 if o
in test_descr
.callrefs
:
689 makefile
.write("%s-no-z-defs = yes\n" % (dso
))
691 # Print dependencies for main test program.
693 if '#' in test_descr
.deps
:
694 for o
in test_descr
.deps
['#']:
695 depstr
+= (" $(objpfx)" + test_subdir
+ "/"
696 + test_name
+ "-" + o
+ ".so")
697 makefile
.write("$(objpfx)%s/%s:%s\n" % (test_subdir
, test_name
, depstr
))
698 ldflags
= "-Wl,--no-as-needed"
699 if '#' in test_descr
.soname_map
:
700 soname
= ("$(objpfx)" + test_subdir
+ "/"
702 + test_descr
.soname_map
['#'] + ".so")
703 ldflags
+= (" -Wl,-soname=" + soname
)
704 makefile
.write("LDFLAGS-%s = %s\n" % (test_name
, ldflags
))
705 rule
= ("$(objpfx)" + test_subdir
+ "/" + test_name
+ ".o: "
706 "$(objpfx)" + test_srcdir
+ test_name
+ ".c\n"
707 "\t$(compile.c) $(OUTPUT_OPTION)\n")
708 makefile
.write (rule
)
710 not_depended_objs
= find_objs_not_depended_on(test_descr
)
711 if not_depended_objs
:
713 for dep
in not_depended_objs
:
714 depstr
+= (" $(objpfx)" + test_subdir
+ "/"
715 + test_name
+ "-" + dep
+ ".so")
716 makefile
.write("$(objpfx)%s.out:%s\n" % (base_test_name
, depstr
))
718 # Add main executable to test-srcs
719 makefile
.write("test-srcs += %s/%s\n" % (test_subdir
, test_name
))
720 # Add dependency on main executable of test
721 makefile
.write("$(objpfx)%s.out: $(objpfx)%s/%s\n"
722 % (base_test_name
, test_subdir
, test_name
))
724 for r
in test_descr
.expected_outputs
.items():
726 specific_tunable
= r
[0]
728 if specific_tunable
!= "":
729 tunable_options
= [specific_tunable
]
731 tunable_options
= default_tunable_options
732 if not tunable_options
:
733 tunable_options
= [""]
735 for tunable
in tunable_options
:
740 tunable_env
= "GLIBC_TUNABLES=%s " % tunable
741 tunable_sfx
= "-" + tunable
.replace("=","_")
743 tunable_sfx
= "-" + specific_tunable
.replace("=","_")
744 exp_tunable_sfx
= tunable_sfx
745 tunable_descr
= ("(%s)" % tunable_env
.strip()
746 if tunable_env
else "")
747 # Write out fragment of shell script for this single test.
748 test_descr
.sh
.write \
749 ("%s${test_wrapper_env} ${run_program_env} \\\n"
750 "${common_objpfx}support/test-run-command \\\n"
751 "${common_objpfx}elf/ld.so \\\n"
752 "--library-path ${common_objpfx}elf/%s:"
753 "${common_objpfx}elf:${common_objpfx}.:"
754 "${common_objpfx}dlfcn \\\n"
755 "${common_objpfx}elf/%s/%s > \\\n"
756 " ${common_objpfx}elf/%s/%s%s.output\n"
757 % (tunable_env
,test_subdir
,
758 test_subdir
, test_name
, test_subdir
, test_name
,
760 # Generate a run of each test and compare with expected out
761 test_descr
.sh
.write \
762 ("if [ $? -ne 0 ]; then\n"
763 " echo '%sFAIL: %s%s execution test'\n"
764 " something_failed=true\n"
766 " diff -wu ${common_objpfx}elf/%s/%s%s.output \\\n"
767 " ${common_objpfx}elf/%s%s%s.exp\n"
768 " if [ $? -ne 0 ]; then\n"
769 " echo '%sFAIL: %s%s expected output comparison'\n"
770 " something_failed=true\n"
773 % (("X" if xfail
else ""), test_name
, tunable_descr
,
774 test_subdir
, test_name
, tunable_sfx
,
775 test_srcdir
, base_test_name
, exp_tunable_sfx
,
776 ("X" if xfail
else ""), test_name
, tunable_descr
))
778 # Generate C files according to dependency and calling relations from
779 # description string.
780 for obj
in test_descr
.objs
:
781 src_name
= test_name
+ "-" + obj
+ ".c"
782 f
= open(testpfx_src
+ src_name
, "w")
783 if obj
in test_descr
.callrefs
:
784 called_objs
= test_descr
.callrefs
[obj
]
785 for callee
in called_objs
:
786 f
.write("extern void fn_%s (void);\n" % (callee
))
788 f
.write("extern int putchar(int);\n")
789 f
.write("static void __attribute__((constructor)) " +
790 "init(void){putchar('%s');putchar('>');}\n" % (obj
))
791 f
.write("static void __attribute__((destructor)) " +
792 "fini(void){putchar('<');putchar('%s');}\n" % (obj
))
794 f
.write('extern int printf(const char *, ...);\n')
795 f
.write('static void __attribute__((constructor)) ' +
796 'init(void){printf("%s>");}\n' % (obj
))
797 f
.write('static void __attribute__((destructor)) ' +
798 'fini(void){printf("<%s");}\n' % (obj
))
799 if obj
in test_descr
.callrefs
:
800 called_objs
= test_descr
.callrefs
[obj
]
802 f
.write("extern int putchar(int);\n")
803 f
.write("void fn_%s (void) {\n" % (obj
))
805 f
.write(" putchar ('%s');\n" % (obj
));
806 f
.write(" putchar ('(');\n");
808 f
.write(' printf ("%s(");\n' % (obj
));
809 for callee
in called_objs
:
810 f
.write(" fn_%s ();\n" % (callee
))
811 f
.write(" putchar (')');\n");
814 for callref
in test_descr
.callrefs
.items():
815 if obj
in callref
[1]:
817 # We need to declare printf here in this case.
818 f
.write('extern int printf(const char *, ...);\n')
819 f
.write("void fn_%s (void) {\n" % (obj
))
820 f
.write(' printf ("%s()");\n' % (obj
))
825 # Open C file for writing main program
826 f
= open(testpfx_src
+ test_name
+ ".c", "w")
828 # if there are some operations in main(), it means we need -ldl
829 f
.write("#include <stdio.h>\n")
830 f
.write("#include <stdlib.h>\n")
831 f
.write("#include <dlfcn.h>\n")
832 for s
in test_descr
.main_program
:
834 f
.write("extern void fn_%s (void);\n" % (s
[1:]));
835 f
.write("int main (void) {\n")
836 f
.write(" putchar('{');\n")
838 # Helper routine for generating sanity checking code.
839 def put_fail_check(fail_cond
, action_desc
):
840 f
.write(' if (%s) { printf ("\\n%s failed: %%s\\n", '
841 'dlerror()); exit (1);}\n' % (fail_cond
, action_desc
))
843 while i
< len(test_descr
.main_program
):
844 s
= test_descr
.main_program
[i
]
846 dso
= test_name
+ "-" + obj
847 if s
[0] == '+' or s
[0] == '^':
849 dlopen_flags
= "RTLD_LAZY|RTLD_GLOBAL"
850 f
.write(" putchar('+');\n");
852 dlopen_flags
= "RTLD_LAZY"
853 f
.write(" putchar(':');\n");
855 f
.write(" putchar('%s');\n" % (obj
));
857 f
.write(' printf("%s");\n' % (obj
));
858 f
.write(" putchar('[');\n");
859 f
.write(' void *%s = dlopen ("%s.so", %s);\n'
860 % (obj
, dso
, dlopen_flags
))
861 put_fail_check("!%s" % (obj
),
862 "%s.so dlopen" % (dso
))
863 f
.write(" putchar(']');\n");
865 f
.write(" putchar('-');\n");
867 f
.write(" putchar('%s');\n" % (obj
));
869 f
.write(' printf("%s");\n' % (obj
));
870 f
.write(" putchar('[');\n");
871 put_fail_check("dlclose (%s) != 0" % (obj
),
872 "%s.so dlclose" % (dso
))
873 f
.write(" putchar(']');\n");
875 f
.write(" putchar('%');\n");
876 f
.write(' void (*fn_%s)(void) = dlsym (%s, "fn_%s");\n'
878 put_fail_check("!fn_%s" % (obj
),
879 "dlsym(fn_%s) from %s.so" % (obj
, dso
))
880 f
.write(" fn_%s ();\n" % (obj
))
882 f
.write(" putchar('@');\n");
883 f
.write(" fn_%s ();\n" % (obj
))
884 f
.write(" putchar(';');\n");
886 f
.write(" putchar('}');\n")
887 f
.write(" return 0;\n")
891 # --build option processing: build generated sources using 'build_gcc'
892 if cmdlineargs
.build
:
893 # Helper routine to run a shell command, for running GCC below
895 cmd
= str.join(' ', args
)
896 if cmdlineargs
.debug_output
:
898 p
= subprocess
.Popen(args
)
900 if p
.returncode
!= 0:
901 error("error running command: %s" % (cmd
))
903 # Compile individual .os files
904 for obj
in test_descr
.objs
:
905 src_name
= test_name
+ "-" + obj
+ ".c"
906 obj_name
= test_name
+ "-" + obj
+ ".os"
907 run_cmd([build_gcc
, "-c", "-fPIC", testpfx_src
+ src_name
,
908 "-o", testpfx
+ obj_name
])
912 # Function to create <test_name>-<obj>.so
914 obj_name
= test_name
+ "-" + obj
+ ".os"
915 dso_name
= test_name
+ "-" + obj
+ ".so"
917 if obj
in test_descr
.deps
:
918 for dep
in test_descr
.deps
[obj
]:
919 if dep
in obj_processed
:
922 deps
.append(dep
+ ".FAKE")
923 if not dep
in fake_created
:
924 base_name
= testpfx
+ test_name
+ "-" + dep
925 src_base_name
= (testpfx_src
+ test_name
927 cmd
= [build_gcc
, "-Wl,--no-as-needed",
928 ("-Wl,-soname=" + base_name
+ ".so"),
929 "-shared", base_name
+ ".FAKE.c",
930 "-o", src_base_name
+ ".FAKE.so"]
932 fake_created
[dep
] = True
933 dso_deps
= map(lambda d
: testpfx
+ test_name
+ "-" + d
+ ".so",
935 cmd
= [build_gcc
, "-shared", "-o", testpfx
+ dso_name
,
936 testpfx
+ obj_name
, "-Wl,--no-as-needed"]
937 if obj
in test_descr
.soname_map
:
938 soname
= ("-Wl,-soname=" + testpfx
+ test_name
+ "-"
939 + test_descr
.soname_map
[obj
] + ".so")
941 cmd
+= list(dso_deps
)
943 obj_processed
[obj
] = True
945 # Build all DSOs, this needs to be in topological dependency order,
947 dfs(test_descr
, build_dso
)
951 if '#' in test_descr
.deps
:
952 deps
= test_descr
.deps
['#']
953 main_deps
= map(lambda d
: testpfx
+ test_name
+ "-" + d
+ ".so",
955 cmd
= [build_gcc
, "-Wl,--no-as-needed", "-o", testpfx
+ test_name
,
956 testpfx_src
+ test_name
+ ".c", "-L%s" % (os
.getcwd()),
957 "-Wl,-rpath-link=%s" % (os
.getcwd())]
958 if '#' in test_descr
.soname_map
:
959 soname
= ("-Wl,-soname=" + testpfx
+ test_name
+ "-"
960 + test_descr
.soname_map
['#'] + ".so")
962 cmd
+= list(main_deps
)
965 # Check if we need to enumerate permutations of dependencies
966 need_permutation_processing
= False
967 if t
.dep_permutations
:
968 # Adjust dep_permutations into map of object -> dependency permutations
969 for r
in t
.dep_permutations
.items():
971 if obj
in t
.deps
and len(t
.deps
[obj
]) > 1:
973 t
.dep_permutations
[obj
] = list(itertools
.permutations (deps
))
974 need_permutation_processing
= True
976 def enum_permutations(t
, perm_list
):
979 def enum_permutations_rec(t
, perm_list
):
980 nonlocal test_subindex
, curr_perms
981 if len(perm_list
) >= 1:
986 # This may be an empty list if no multiple dependencies to
987 # permute were found, skip to next in this case
988 enum_permutations_rec(t
, perm_list
[1:])
992 permstr
= "" if obj
== "#" else obj
+ "_"
993 permstr
+= str.join('', deps
)
994 curr_perms
.append(permstr
)
995 enum_permutations_rec(t
, perm_list
[1:])
996 curr_perms
= curr_perms
[0:len(curr_perms
)-1]
998 # t.deps is now instantiated with one dependency order
999 # permutation(across all objects that have multiple
1000 # permutations), now process a testcase
1001 generate_testcase(t
, ("_" + str (test_subindex
)
1002 + "-" + str.join('-', curr_perms
)))
1004 enum_permutations_rec(t
, perm_list
)
1006 # Create *.exp files with expected outputs
1007 for r
in t
.expected_outputs
.items():
1010 sfx
= "-" + r
[0].replace("=","_")
1011 f
= open(testpfx_src
+ t
.test_name
+ sfx
+ ".exp", "w")
1012 (output
, xfail
) = r
[1]
1013 f
.write('%s' % output
)
1016 # Create header part of top-level testcase shell script, to wrap execution
1017 # and output comparison together.
1018 t
.sh
= open(testpfx_src
+ t
.test_name
+ ".sh", "w")
1019 t
.sh
.write("#!/bin/sh\n")
1020 t
.sh
.write("# Test driver for %s, generated by "
1021 "dso-ordering-test.py\n" % (t
.test_name
))
1022 t
.sh
.write("common_objpfx=$1\n")
1023 t
.sh
.write("test_wrapper_env=$2\n")
1024 t
.sh
.write("run_program_env=$3\n")
1025 t
.sh
.write("something_failed=false\n")
1027 # Starting part of Makefile fragment
1028 makefile
.write("ifeq (yes,$(build-shared))\n")
1030 if need_permutation_processing
:
1031 enum_permutations(t
, list (t
.dep_permutations
.items()))
1033 # We have no permutations to enumerate, just process testcase normally
1034 generate_testcase(t
, "")
1036 # If testcase is XFAIL, indicate so
1038 makefile
.write("test-xfail-%s = yes\n" % t
.test_name
)
1040 # Output end part of Makefile fragment
1041 expected_output_files
= ""
1042 for r
in t
.expected_outputs
.items():
1045 sfx
= "-" + r
[0].replace("=","_")
1046 expected_output_files
+= " $(objpfx)%s%s%s.exp" % (test_srcdir
,
1049 ("$(objpfx)%s.out: $(objpfx)%s%s.sh%s "
1050 "$(common-objpfx)support/test-run-command\n"
1051 % (t
.test_name
, test_srcdir
, t
.test_name
,
1052 expected_output_files
))
1053 makefile
.write("\t$(SHELL) $< $(common-objpfx) '$(test-wrapper-env)' "
1054 "'$(run-program-env)' > $@; $(evaluate-test)\n")
1055 makefile
.write("ifeq ($(run-built-tests),yes)\n")
1057 makefile
.write("xtests-special += $(objpfx)%s.out\n" % (t
.test_name
))
1059 makefile
.write("tests-special += $(objpfx)%s.out\n" % (t
.test_name
))
1060 makefile
.write("endif\n")
1061 makefile
.write("endif\n")
1063 # Write ending part of shell script generation
1064 t
.sh
.write("if $something_failed; then\n"
1067 " echo '%sPASS: all tests for %s succeeded'\n"
1069 "fi\n" % (("X" if t
.xfail
else ""),
1073 # Decription file parsing
1074 def parse_description_file(filename
):
1075 global default_tunable_options
1076 global current_input_lineno
1079 error("cannot open description file %s" % (filename
))
1080 descrfile_lines
= f
.readlines()
1082 for line
in descrfile_lines
:
1083 p
= re
.compile(r
"#.*$")
1084 line
= p
.sub("", line
) # Filter out comments
1085 line
= line
.strip() # Remove excess whitespace
1086 current_input_lineno
+= 1
1088 m
= re
.match(r
"^tunable_option:\s*(.*)$", line
)
1090 if m
.group(1) == "":
1091 error("tunable option cannot be empty")
1092 default_tunable_options
.append(m
.group (1))
1095 m
= re
.match(r
"^clear_tunables$", line
)
1097 default_tunable_options
= []
1100 m
= re
.match(r
"^([^:]+):\s*(.*)$", line
)
1103 o
= re
.match(r
"^output(.*)$", lhs
)
1106 o
= re
.match(r
"^xfail_output(.*)$", lhs
)
1111 error("output specification without testcase description")
1114 ts
= re
.match(r
"^\(([a-zA-Z0-9_.=]*)\)$", o
.group (1))
1116 error("tunable option malformed '%s'" % o
.group(1))
1118 t
.expected_outputs
[tsstr
] = (m
.group(2), xfail
)
1119 # Any tunable option XFAILed means entire testcase
1124 # Starting a new test description, end and process
1128 x
= re
.match(r
"^xtest\((.*)\)$", lhs
)
1131 t
.test_name
= x
.group(1)
1134 descr_string
= m
.group(2)
1135 parse_description_string(t
, descr_string
)
1140 error("no active testcase description")
1141 parse_description_string(t
, line
)
1142 # Process last completed test description
1146 # Setup Makefile output to file or stdout as selected
1148 output_makefile_dir
= os
.path
.dirname(output_makefile
)
1149 if output_makefile_dir
:
1150 os
.makedirs(output_makefile_dir
, exist_ok
= True)
1151 makefile
= open(output_makefile
, "w")
1153 makefile
= open(sys
.stdout
.fileno (), "w")
1155 # Finally, the main top-level calling of above parsing routines.
1156 if description_file
:
1157 parse_description_file(description_file
)
1160 t
.test_name
= test_name
1161 parse_description_string(t
, description
)
1164 # Close Makefile fragment output