classes/insane.bbclass: Make .la failures non-fatal the right way, split up the ...
[vuplus_openembedded] / classes / insane.bbclass
1 # BB Class inspired by ebuild.sh
2 #
3 # This class will test files after installation for certain
4 # security issues and other kind of issues.
5 #
6 # Checks we do:
7 #  -Check the ownership and permissions
8 #  -Check the RUNTIME path for the $TMPDIR
9 #  -Check if .la files wrongly point to workdir
10 #  -Check if .pc files wrongly point to workdir
11 #  -Check if packages contains .debug directories or .so files
12 #   where they should be in -dev or -dbg
13 #  -Check if config.log contains traces to broken autoconf tests
14
15
16 #
17 # We need to have the scanelf utility as soon as
18 # possible and this is contained within the pax-utils-native.
19 # The package.bbclass can help us here.
20 #
21 inherit package
22 PACKAGE_DEPENDS += "pax-utils-native desktop-file-utils-native"
23 PACKAGEFUNCS += " do_package_qa "
24
25
26 #
27 # dictionary for elf headers
28 #
29 # feel free to add and correct.
30 #
31 #           TARGET_OS  TARGET_ARCH   MACHINE, OSABI, ABIVERSION, Little Endian, 32bit?
32 def package_qa_get_machine_dict():
33     return {
34             "linux" : { 
35                         "arm" :       (40,    97,    0,          True,          True),
36                         "armeb":      (40,    97,    0,          False,         True),
37                         "powerpc":    (20,     0,    0,          False,         True),
38                         "i386":       ( 3,     0,    0,          True,          True),
39                         "i486":       ( 3,     0,    0,          True,          True),
40                         "i586":       ( 3,     0,    0,          True,          True),
41                         "i686":       ( 3,     0,    0,          True,          True),
42                         "x86_64":     (62,     0,    0,          True,          False),
43                         "ia64":       (50,     0,    0,          True,          False),
44                         "alpha":      (36902,  0,    0,          True,          False),
45                         "hppa":       (15,     3,    0,          False,         True),
46                         "m68k":       ( 4,     0,    0,          False,         True),
47                         "mips":       ( 8,     0,    0,          False,         True),
48                         "mipsel":     ( 8,     0,    0,          True,          True),
49                         "s390":       (22,     0,    0,          False,         True),
50                         "sh4":        (42,     0,    0,          True,          True),
51                         "sparc":      ( 2,     0,    0,          False,         True),
52                       },
53             "linux-uclibc" : { 
54                         "arm" :       (  40,    97,    0,          True,          True),
55                         "armeb":      (  40,    97,    0,          False,         True),
56                         "powerpc":    (  20,     0,    0,          False,         True),
57                         "i386":       (   3,     0,    0,          True,          True),
58                         "i486":       (   3,     0,    0,          True,          True),
59                         "i586":       (   3,     0,    0,          True,          True),
60                         "i686":       (   3,     0,    0,          True,          True),
61                         "mipsel":     (   8,     0,    0,          True,          True),
62                         "avr32":      (6317,     0,    0,          False,         True),
63                       },
64             "uclinux-uclibc" : {
65                         "bfin":       ( 106,     0,    0,          True,         True),
66                       }, 
67             "linux-gnueabi" : {
68                         "arm" :       (40,     0,    0,          True,          True),
69                         "armeb" :     (40,     0,    0,          False,         True),
70                       },
71             "linux-uclibcgnueabi" : {
72                         "arm" :       (40,     0,    0,          True,          True),
73                         "armeb" :     (40,     0,    0,          False,         True),
74                       },
75
76        }
77
78 # factory for a class, embedded in a method
79 def package_qa_get_elf(path, bits32):
80     class ELFFile:
81         EI_NIDENT = 16
82
83         EI_CLASS      = 4
84         EI_DATA       = 5
85         EI_VERSION    = 6
86         EI_OSABI      = 7
87         EI_ABIVERSION = 8
88
89         # possible values for EI_CLASS
90         ELFCLASSNONE = 0
91         ELFCLASS32   = 1
92         ELFCLASS64   = 2
93
94         # possible value for EI_VERSION
95         EV_CURRENT   = 1
96
97         # possible values for EI_DATA
98         ELFDATANONE  = 0
99         ELFDATA2LSB  = 1
100         ELFDATA2MSB  = 2
101
102         def my_assert(expectation, result):
103             if not expectation == result:
104                 #print "'%x','%x'" % (ord(expectation), ord(result))
105                 raise Exception("This does not work as expected")
106         my_assert = staticmethod(my_assert)
107
108         def __init__(self, name):
109             self.name = name
110
111         def open(self):
112             self.file = file(self.name, "r")
113             self.data = self.file.read(ELFFile.EI_NIDENT+4)
114
115             ELFFile.my_assert(len(self.data), ELFFile.EI_NIDENT+4)
116             ELFFile.my_assert(self.data[0], chr(0x7f) )
117             ELFFile.my_assert(self.data[1], 'E')
118             ELFFile.my_assert(self.data[2], 'L')
119             ELFFile.my_assert(self.data[3], 'F')
120             if bits32 :
121                 ELFFile.my_assert(self.data[ELFFile.EI_CLASS], chr(ELFFile.ELFCLASS32))
122             else:
123                 ELFFile.my_assert(self.data[ELFFile.EI_CLASS], chr(ELFFile.ELFCLASS64))
124             ELFFile.my_assert(self.data[ELFFile.EI_VERSION], chr(ELFFile.EV_CURRENT) )
125
126             self.sex = self.data[ELFFile.EI_DATA]
127             if self.sex == chr(ELFFile.ELFDATANONE):
128                 raise Exception("self.sex == ELFDATANONE")
129             elif self.sex == chr(ELFFile.ELFDATA2LSB):
130                 self.sex = "<"
131             elif self.sex == chr(ELFFile.ELFDATA2MSB):
132                 self.sex = ">"
133             else:
134                 raise Exception("Unknown self.sex")
135
136         def osAbi(self):
137             return ord(self.data[ELFFile.EI_OSABI])
138
139         def abiVersion(self):
140             return ord(self.data[ELFFile.EI_ABIVERSION])
141
142         def isLittleEndian(self):
143             return self.sex == "<"
144
145         def isBigEngian(self):
146             return self.sex == ">"
147
148         def machine(self):
149             """
150             We know the sex stored in self.sex and we
151             know the position
152             """
153             import struct
154             (a,) = struct.unpack(self.sex+"H", self.data[18:20])
155             return a
156
157     return ELFFile(path)
158
159
160 # Known Error classes
161 # 0 - non dev contains .so
162 # 1 - package contains a dangerous RPATH
163 # 2 - package depends on debug package
164 # 3 - non dbg contains .so
165 # 4 - wrong architecture
166 # 5 - .la contains installed=yes or reference to the workdir
167 # 6 - .pc contains reference to /usr/include or workdir
168 # 7 - the desktop file is not valid
169
170 def package_qa_clean_path(path,d):
171     """ Remove the common prefix from the path. In this case it is the TMPDIR"""
172     import bb
173     return path.replace(bb.data.getVar('TMPDIR',d,True),"")
174
175 def package_qa_make_fatal_error(error_class, name, path,d):
176     """
177     decide if an error is fatal
178
179     TODO: Load a whitelist of known errors
180     """
181     if error_class == 0 or error_class == 5:
182         return False
183     else:
184         return True
185
186 def package_qa_write_error(error_class, name, path, d):
187     import bb, os
188     if not bb.data.getVar('QA_LOG', d):
189         return
190
191     ERROR_NAMES =[
192         "non dev contains .so",
193         "package contains RPATH",
194         "package depends on debug package",
195         "non dbg contains .debug",
196         "wrong architecture",
197         "evil hides inside the .la",
198         "evil hides inside the .pc",
199     ]
200
201
202     log_path = os.path.join( bb.data.getVar('T', d, True), "log.qa_package" )
203     f = file( log_path, "a+")
204     print >> f, "%s, %s, %s" % \
205              (ERROR_NAMES[error_class], name, package_qa_clean_path(path,d))
206     f.close()
207
208
209 def package_qa_check_rpath(file,name,d):
210     """
211     Check for dangerous RPATHs
212     """
213     import bb, os
214     scanelf = os.path.join(bb.data.getVar('STAGING_BINDIR_NATIVE',d,True),'scanelf')
215     bad_dir = bb.data.getVar('TMPDIR', d, True) + "/work"
216     bad_dir_test = bb.data.getVar('TMPDIR', d, True)
217     if not os.path.exists(scanelf):
218         bb.fatal("Can not check RPATH, scanelf (part of pax-utils-native) not found")
219
220     if not bad_dir in bb.data.getVar('WORKDIR', d, True):
221         bb.fatal("This class assumed that WORKDIR is ${TMPDIR}/work... Not doing any check")
222
223     output = os.popen("%s -B -F%%r#F '%s'" % (scanelf,file))
224     txt    = output.readline().split()
225     for line in txt:
226         if bad_dir in line:
227             package_qa_write_error( 1, name, file, d)
228             bb.error("QA Issue package %s contains bad RPATH %s in file %s" % \
229                       (name, line, file))
230     return True
231
232 def package_qa_check_devdbg(path, name,d):
233     """
234     Check for debug remains inside the binary or
235     non dev packages containing
236     """
237
238     import bb, os
239     sane = True
240
241     if not "-dev" in name:
242         if path[-3:] == ".so" and os.path.islink(path):
243             package_qa_write_error( 0, name, path, d )
244             bb.error("QA Issue: non -dev package contains symlink .so: %s path '%s'" % \
245                      (name, package_qa_clean_path(path,d)))
246             if package_qa_make_fatal_error( 0, name, path, d ):
247                 sane = False
248
249     if not "-dbg" in name:
250         if '.debug' in path:
251             package_qa_write_error( 3, name, path, d )
252             bb.error("QA Issue: non debug package contains .debug directory: %s path %s" % \
253                      (name, package_qa_clean_path(path,d)))
254             if package_qa_make_fatal_error( 3, name, path, d ):
255                 sane = False
256
257     return sane
258
259 def package_qa_check_perm(path,name,d):
260     """
261     Check the permission of files
262     """
263     sane = True
264     return sane
265
266 def package_qa_check_arch(path,name,d):
267     """
268     Check if archs are compatible
269     """
270     import bb, os
271     target_os   = bb.data.getVar('TARGET_OS',   d, True)
272     target_arch = bb.data.getVar('TARGET_ARCH', d, True)
273
274     # FIXME: Cross package confuse this check, so just skip them
275     if bb.data.inherits_class('cross', d) or bb.data.inherits_class('sdk', d):
276         return True
277
278     # avoid following links to /usr/bin (e.g. on udev builds)
279     # we will check the files pointed to anyway...
280     if os.path.islink(path):
281         return True
282
283     #if this will throw an exception, then fix the dict above
284     (machine, osabi, abiversion, littleendian, bits32) \
285         = package_qa_get_machine_dict()[target_os][target_arch]
286     elf = package_qa_get_elf(path, bits32)
287     elf.open()
288
289     # Check the architecture and endiannes of the binary
290     if not machine == elf.machine():
291         bb.error("Architecture did not match (%d to %d) on %s" % \
292                  (machine, elf.machine(), package_qa_clean_path(path,d)))
293         return not package_qa_make_fatal_error( 4, name, path, d )
294     elif not littleendian == elf.isLittleEndian():
295         bb.error("Endiannes did not match (%d to %d) on %s" % \
296                  (littleendian, elf.isLittleEndian(), package_qa_clean_path(path,d)))
297         return not package_qa_make_fatal_error( 4, name, path, d )
298
299     return True
300
301 def package_qa_check_desktop(path, name, d):
302     """
303     Run all desktop files through desktop-file-validate.
304     """
305     import bb, os
306     if path.endswith(".desktop"):
307         validate = os.path.join(bb.data.getVar('STAGING_BINDIR_NATIVE',d,True), \
308                                 'desktop-file-validate')
309         output = os.popen("%s %s" % (validate, path))
310         for l in output:
311             bb.error(l.strip())
312         return not package_qa_make_fatal_error(7, name, path, d)
313     return True
314
315 def package_qa_check_staged(path,d):
316     """
317     Check staged la and pc files for sanity
318       -e.g. installed being false
319
320         As this is run after every stage we should be able
321         to find the one responsible for the errors easily even
322         if we look at every .pc and .la file
323     """
324     import os, bb
325
326     sane = True
327     workdir = os.path.join(bb.data.getVar('TMPDIR', d, True), "work")
328
329     if bb.data.inherits_class("native", d):
330         installed = "installed=no"
331     else:
332         installed = "installed=yes"
333
334     # find all .la and .pc files
335     # read the content
336     # and check for stuff that looks wrong
337     for root, dirs, files in os.walk(path):
338         for file in files:
339             path = os.path.join(root,file)
340             if file[-2:] == "la":
341                 file_content = open(path).read()
342                 if installed in file_content:
343                     bb.error("QA issue: %s failed sanity test (installed) in path %s" % \
344                              (file,root))
345                     if package_qa_make_fatal_error( 5, "staging", path, d):
346                         sane = False
347                 if workdir in file_content:
348                     bb.error("QA issue: %s failed sanity test (workdir) in path %s" % \
349                              (file,root))
350                     if package_qa_make_fatal_error( 5, "staging", path, d):
351                         sane = False
352             elif file[-2:] == "pc":
353                 file_content = open(path).read()
354                 if workdir in file_content:
355                     bb.error("QA issue: %s failed sanity test (workdir) in path %s" % \
356                             (file,root))
357                     if package_qa_make_fatal_error( 6, "staging", path, d):
358                         sane = False
359
360     return sane
361
362 # Walk over all files in a directory and call func
363 def package_qa_walk(path, funcs, package,d):
364     import os
365     sane = True
366
367     for root, dirs, files in os.walk(path):
368         for file in files:
369             path = os.path.join(root,file)
370             for func in funcs:
371                 if not func(path, package,d):
372                     sane = False
373
374     return sane
375
376
377 def package_qa_check_rdepends(pkg, workdir, d):
378     import bb
379     sane = True
380     if not "-dbg" in pkg and not "task-" in pkg and not "-image" in pkg:
381         # Copied from package_ipk.bbclass
382         # boiler plate to update the data
383         localdata = bb.data.createCopy(d)
384         root = "%s/install/%s" % (workdir, pkg)
385
386         bb.data.setVar('ROOT', '', localdata) 
387         bb.data.setVar('ROOT_%s' % pkg, root, localdata)
388         pkgname = bb.data.getVar('PKG_%s' % pkg, localdata, True)
389         if not pkgname:
390             pkgname = pkg
391         bb.data.setVar('PKG', pkgname, localdata)
392
393         overrides = bb.data.getVar('OVERRIDES', localdata)
394         if not overrides:
395             raise bb.build.FuncFailed('OVERRIDES not defined')
396         overrides = bb.data.expand(overrides, localdata)
397         bb.data.setVar('OVERRIDES', overrides + ':' + pkg, localdata)
398
399         bb.data.update_data(localdata)
400
401         # Now check the RDEPENDS
402         rdepends = explode_deps(bb.data.getVar('RDEPENDS', localdata, True) or "")
403
404
405         # Now do the sanity check!!!
406         for rdepend in rdepends:
407             if "-dbg" in rdepend:
408                 package_qa_write_error( 2, pkgname, rdepend, d )
409                 bb.error("QA issue: %s rdepends on %s" % (pkgname,rdepend))
410                 if package_qa_make_fatal_error( 2, pkgname, rdepend, d ):
411                     sane = False
412
413     return sane
414
415 # The PACKAGE FUNC to scan each package
416 python do_package_qa () {
417     bb.note("DO PACKAGE QA")
418     workdir = bb.data.getVar('WORKDIR', d, True)
419     packages = bb.data.getVar('PACKAGES',d, True)
420
421     # no packages should be scanned
422     if not packages:
423         return
424
425     checks = [package_qa_check_rpath, package_qa_check_devdbg,
426               package_qa_check_perm, package_qa_check_arch,
427               package_qa_check_desktop]
428     walk_sane = True
429     rdepends_sane = True
430     for package in packages.split():
431         if bb.data.getVar('INSANE_SKIP_' + package, d, True):
432             bb.note("Package: %s (skipped)" % package)
433             continue
434
435         bb.note("Checking Package: %s" % package)
436         path = "%s/install/%s" % (workdir, package)
437         if not package_qa_walk(path, checks, package, d):
438             walk_sane  = False
439         if not package_qa_check_rdepends(package, workdir, d):
440             rdepends_sane = False
441
442     if not walk_sane or not rdepends_sane:
443         bb.fatal("QA run found fatal errors. Please consider fixing them.")
444     bb.note("DONE with PACKAGE QA")
445 }
446
447
448 # The Staging Func, to check all staging
449 addtask qa_staging after do_populate_staging before do_build
450 python do_qa_staging() {
451     bb.note("QA checking staging")
452
453     if not package_qa_check_staged(bb.data.getVar('STAGING_LIBDIR',d,True), d):
454         bb.fatal("QA staging was broken by the package built above")
455 }
456
457 # Check broken config.log files
458 addtask qa_configure after do_configure before do_compile
459 python do_qa_configure() {
460     bb.note("Checking sanity of the config.log file")
461     import os
462     for root, dirs, files in os.walk(bb.data.getVar('WORKDIR', d, True)):
463         statement = "grep 'CROSS COMPILE Badness:' %s > /dev/null" % \
464                     os.path.join(root,"config.log")
465         if "config.log" in files:
466             if os.system(statement) == 0:
467                 bb.fatal("This autoconf log indicates errors, it looked at \
468                           host includes. Rerun configure task after fixing this. \
469                           Path was '%s'" % root)
470 }