forked from lly-q/MIO-KITCHEN-SOURCE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockimgdiff.py
1230 lines (1043 loc) · 48.9 KB
/
blockimgdiff.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (C) 2014 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function
from array import array
from functools import total_ordering
from heapq import heappop, heappush, heapify
from itertools import chain
from multiprocessing import cpu_count
import os
from re import sub
from subprocess import call, STDOUT
from tempfile import mkstemp
from threading import Lock, Thread
from collections import deque, OrderedDict
from hashlib import sha1
from rangelib import RangeSet
__all__ = ["EmptyImage", "DataImage", "BlockImageDiff"]
class Settings(object):
# Stash size cannot exceed cache_size * threshold.
cache_size = None
stash_threshold = 0.8
def compute_patch(src, tgt, imgdiff=False):
srcfd, srcfile = mkstemp(prefix="src-")
tgtfd, tgtfile = mkstemp(prefix="tgt-")
patchfd, patchfile = mkstemp(prefix="patch-")
os.close(patchfd)
try:
with os.fdopen(srcfd, "wb") as f_src:
for p in src:
f_src.write(p)
with os.fdopen(tgtfd, "wb") as f_tgt:
for p in tgt:
f_tgt.write(p)
try:
os.unlink(patchfile)
except OSError:
pass
if imgdiff:
p = call(["imgdiff", "-z", srcfile, tgtfile, patchfile],
stdout=open("/dev/null", "a"),
stderr=STDOUT)
else:
p = call(["bsdiff", srcfile, tgtfile, patchfile])
if p:
raise ValueError("diff failed: " + str(p))
with open(patchfile, "rb") as f:
return f.read()
finally:
try:
os.unlink(srcfile)
os.unlink(tgtfile)
os.unlink(patchfile)
except OSError:
pass
class Image(object):
def ReadRangeSet(self, ranges):
raise NotImplementedError
def TotalSha1(self, include_clobbered_blocks=False):
raise NotImplementedError
class EmptyImage(Image):
"""A zero-length image."""
blocksize = 4096
care_map = RangeSet()
clobbered_blocks = RangeSet()
extended = RangeSet()
total_blocks = 0
file_map = {}
def ReadRangeSet(self, ranges):
return ()
def TotalSha1(self, include_clobbered_blocks=False):
# EmptyImage always carries empty clobbered_blocks, so
# include_clobbered_blocks can be ignored.
assert self.clobbered_blocks.size() == 0
return sha1().hexdigest()
class DataImage(Image):
"""An image wrapped around a single string of data."""
def __init__(self, data, trim=False, pad=False):
self.data = data
self.blocksize = 4096
assert not (trim and pad)
partial = len(self.data) % self.blocksize
padded = False
if partial > 0:
if trim:
self.data = self.data[:-partial]
elif pad:
self.data += '\0' * (self.blocksize - partial)
padded = True
else:
raise ValueError(("data for DataImage must be multiple of %d bytes "
"unless trim or pad is specified") %
(self.blocksize,))
assert len(self.data) % self.blocksize == 0
self.total_blocks = len(self.data) / self.blocksize
self.care_map = RangeSet(data=(0, self.total_blocks))
# When the last block is padded, we always write the whole block even for
# incremental OTAs. Because otherwise the last block may get skipped if
# unchanged for an incremental, but would fail the post-install
# verification if it has non-zero contents in the padding bytes.
# Bug: 23828506
if padded:
clobbered_blocks = [self.total_blocks - 1, self.total_blocks]
else:
clobbered_blocks = []
self.clobbered_blocks = clobbered_blocks
self.extended = RangeSet()
zero_blocks = []
nonzero_blocks = []
reference = '\0' * self.blocksize
for i in range(self.total_blocks - 1 if padded else self.total_blocks):
d = self.data[i * self.blocksize: (i + 1) * self.blocksize]
if d == reference:
zero_blocks.append(i)
zero_blocks.append(i + 1)
else:
nonzero_blocks.append(i)
nonzero_blocks.append(i + 1)
assert zero_blocks or nonzero_blocks or clobbered_blocks
self.file_map = dict()
if zero_blocks:
self.file_map["__ZERO"] = RangeSet(data=zero_blocks)
if nonzero_blocks:
self.file_map["__NONZERO"] = RangeSet(data=nonzero_blocks)
if clobbered_blocks:
self.file_map["__COPY"] = RangeSet(data=clobbered_blocks)
def ReadRangeSet(self, ranges):
return [self.data[s * self.blocksize:e * self.blocksize] for (s, e) in ranges]
def TotalSha1(self, include_clobbered_blocks=False):
if not include_clobbered_blocks:
ranges = self.care_map.subtract(self.clobbered_blocks)
return sha1(self.ReadRangeSet(ranges)).hexdigest()
else:
return sha1(self.data).hexdigest()
class Transfer(object):
def __init__(self, tgt_name, src_name, tgt_ranges, src_ranges, style, by_id):
self.tgt_name = tgt_name
self.src_name = src_name
self.tgt_ranges = tgt_ranges
self.src_ranges = src_ranges
self.style = style
self.intact = (getattr(tgt_ranges, "monotonic", False) and
getattr(src_ranges, "monotonic", False))
# We use OrderedDict rather than dict so that the output is repeatable;
# otherwise it would depend on the hash values of the Transfer objects.
self.goes_before = OrderedDict()
self.goes_after = OrderedDict()
self.stash_before = []
self.use_stash = []
self.id = len(by_id)
by_id.append(self)
def NetStashChange(self):
return (sum(sr.size() for (_, sr) in self.stash_before) -
sum(sr.size() for (_, sr) in self.use_stash))
def ConvertToNew(self):
assert self.style != "new"
self.use_stash = []
self.style = "new"
self.src_ranges = RangeSet()
def __str__(self):
return (str(self.id) + ": <" + str(self.src_ranges) + " " + self.style +
" to " + str(self.tgt_ranges) + ">")
@total_ordering
class HeapItem(object):
def __init__(self, item):
self.item = item
# Negate the score since python's heap is a min-heap and we want
# the maximum score.
self.score = -item.score
def clear(self):
self.item = None
def __bool__(self):
return self.item is None
def __eq__(self, other):
return self.score == other.score
def __le__(self, other):
return self.score <= other.score
# BlockImageDiff works on two image objects. An image object is
# anything that provides the following attributes:
#
# blocksize: the size in bytes of a block, currently must be 4096.
#
# total_blocks: the total size of the partition/image, in blocks.
#
# care_map: a RangeSet containing which blocks (in the range [0,
# total_blocks) we actually care about; i.e. which blocks contain
# data.
#
# file_map: a dict that partitions the blocks contained in care_map
# into smaller domains that are useful for doing diffs on.
# (Typically a domain is a file, and the key in file_map is the
# pathname.)
#
# clobbered_blocks: a RangeSet containing which blocks contain data
# but may be altered by the FS. They need to be excluded when
# verifying the partition integrity.
#
# ReadRangeSet(): a function that takes a RangeSet and returns the
# data contained in the image blocks of that RangeSet. The data
# is returned as a list or tuple of strings; concatenating the
# elements together should produce the requested data.
# Implementations are free to break up the data into list/tuple
# elements in any way that is convenient.
#
# TotalSha1(): a function that returns (as a hex string) the SHA-1
# hash of all the data in the image (ie, all the blocks in the
# care_map minus clobbered_blocks, or including the clobbered
# blocks if include_clobbered_blocks is True).
#
# When creating a BlockImageDiff, the src image may be None, in which
# case the list of transfers produced will never read from the
# original image.
class BlockImageDiff(object):
def __init__(self, tgt, src=None, version=4, threads=None,
disable_imgdiff=False):
if threads is None:
threads = cpu_count() // 2
if threads == 0:
threads = 1
self.threads = threads
self.version = version
self.transfers = []
self.src_basenames = {}
self.src_numpatterns = {}
self._max_stashed_size = 0
self.touched_src_ranges = RangeSet()
self.touched_src_sha1 = None
self.disable_imgdiff = disable_imgdiff
assert version in (1, 2, 3, 4)
self.tgt = tgt
if src is None:
src = EmptyImage()
self.src = src
# The updater code that installs the patch always uses 4k blocks.
assert tgt.blocksize == 4096
assert src.blocksize == 4096
# The range sets in each filemap should comprise a partition of
# the care map.
self.AssertPartition(src.care_map, src.file_map.values())
self.AssertPartition(tgt.care_map, tgt.file_map.values())
@property
def max_stashed_size(self):
return self._max_stashed_size
def Compute(self, prefix):
# When looking for a source file to use as the diff input for a
# target file, we try:
# 1) an exact path match if available, otherwise
# 2) a exact basename match if available, otherwise
# 3) a basename match after all runs of digits are replaced by
# "#" if available, otherwise
# 4) we have no source for this target.
self.AbbreviateSourceNames()
self.FindTransfers()
# Find the ordering dependencies among transfers (this is O(n^2)
# in the number of transfers).
self.GenerateDigraph()
# Find a sequence of transfers that satisfies as many ordering
# dependencies as possible (heuristically).
self.FindVertexSequence()
# Fix up the ordering dependencies that the sequence didn't
# satisfy.
if self.version == 1:
self.RemoveBackwardEdges()
else:
self.ReverseBackwardEdges()
self.ImproveVertexSequence()
# Ensure the runtime stash size is under the limit.
if self.version >= 2 and Settings.cache_size is not None:
self.ReviseStashSize()
# Double-check our work.
self.AssertSequenceGood()
self.ComputePatches(prefix)
self.WriteTransfers(prefix)
def HashBlocks(self, source, ranges): # pylint: disable=no-self-use
data = source.ReadRangeSet(ranges)
ctx = sha1()
for p in data:
ctx.update(p)
return ctx.hexdigest()
def WriteTransfers(self, prefix):
def WriteTransfersZero(out, to_zero):
"""Limit the number of blocks in command zero to 1024 blocks.
This prevents the target size of one command from being too large; and
might help to avoid fsync errors on some devices."""
zero_blocks_limit = 1024
total = 0
while to_zero.size() > 0:
zero_blocks = to_zero.first(zero_blocks_limit)
out.append("zero %s\n" % (zero_blocks.to_string_raw(),))
total += zero_blocks.size()
to_zero = to_zero.subtract(zero_blocks)
return total
out = []
total = 0
stashes = {}
stashed_blocks = 0
max_stashed_blocks = 0
free_stash_ids = []
next_stash_id = 0
for xf in self.transfers:
if self.version < 2:
assert not xf.stash_before
assert not xf.use_stash
for s, sr in xf.stash_before:
assert s not in stashes
if free_stash_ids:
sid = heappop(free_stash_ids)
else:
sid = next_stash_id
next_stash_id += 1
stashes[s] = sid
if self.version == 2:
stashed_blocks += sr.size()
out.append("stash %d %s\n" % (sid, sr.to_string_raw()))
else:
sh = self.HashBlocks(self.src, sr)
if sh in stashes:
stashes[sh] += 1
else:
stashes[sh] = 1
stashed_blocks += sr.size()
self.touched_src_ranges = self.touched_src_ranges.union(sr)
out.append("stash %s %s\n" % (sh, sr.to_string_raw()))
if stashed_blocks > max_stashed_blocks:
max_stashed_blocks = stashed_blocks
free_string = []
free_size = 0
if self.version == 1:
src_str = xf.src_ranges.to_string_raw() if xf.src_ranges.size() > 0 else ""
elif self.version >= 2:
# <# blocks> <src ranges>
# OR
# <# blocks> <src ranges> <src locs> <stash refs...>
# OR
# <# blocks> - <stash refs...>
size = xf.src_ranges.size()
src_str = [str(size)]
unstashed_src_ranges = xf.src_ranges
mapped_stashes = []
for s, sr in xf.use_stash:
sid = stashes.pop(s)
unstashed_src_ranges = unstashed_src_ranges.subtract(sr)
sh = self.HashBlocks(self.src, sr)
sr = xf.src_ranges.map_within(sr)
mapped_stashes.append(sr)
if self.version == 2:
src_str.append("%d:%s" % (sid, sr.to_string_raw()))
# A stash will be used only once. We need to free the stash
# immediately after the use, instead of waiting for the automatic
# clean-up at the end. Because otherwise it may take up extra space
# and lead to OTA failures.
# Bug: 23119955
free_string.append("free %d\n" % (sid,))
free_size += sr.size()
else:
assert sh in stashes
src_str.append("%s:%s" % (sh, sr.to_string_raw()))
stashes[sh] -= 1
if stashes[sh] == 0:
free_size += sr.size()
free_string.append("free %s\n" % sh)
stashes.pop(sh)
heappush(free_stash_ids, sid)
if unstashed_src_ranges.size() > 0:
src_str.insert(1, unstashed_src_ranges.to_string_raw())
if xf.use_stash:
mapped_unstashed = xf.src_ranges.map_within(unstashed_src_ranges)
src_str.insert(2, mapped_unstashed.to_string_raw())
mapped_stashes.append(mapped_unstashed)
self.AssertPartition(RangeSet(data=(0, size)), mapped_stashes)
else:
src_str.insert(1, "-")
self.AssertPartition(RangeSet(data=(0, size)), mapped_stashes)
src_str = " ".join(src_str)
# all versions:
# zero <rangeset>
# new <rangeset>
# erase <rangeset>
#
# version 1:
# bsdiff patchstart patchlen <src rangeset> <tgt rangeset>
# imgdiff patchstart patchlen <src rangeset> <tgt rangeset>
# move <src rangeset> <tgt rangeset>
#
# version 2:
# bsdiff patchstart patchlen <tgt rangeset> <src_str>
# imgdiff patchstart patchlen <tgt rangeset> <src_str>
# move <tgt rangeset> <src_str>
#
# version 3:
# bsdiff patchstart patchlen srchash tgthash <tgt rangeset> <src_str>
# imgdiff patchstart patchlen srchash tgthash <tgt rangeset> <src_str>
# move hash <tgt rangeset> <src_str>
tgt_size = xf.tgt_ranges.size()
if xf.style == "new":
assert xf.tgt_ranges
out.append("%s %s\n" % (xf.style, xf.tgt_ranges.to_string_raw()))
total += tgt_size
elif xf.style == "move":
assert xf.tgt_ranges
assert xf.src_ranges.size() == tgt_size
if xf.src_ranges != xf.tgt_ranges:
if self.version == 1:
out.append("%s %s %s\n" % (
xf.style,
xf.src_ranges.to_string_raw(), xf.tgt_ranges.to_string_raw()))
elif self.version == 2:
out.append("%s %s %s\n" % (
xf.style,
xf.tgt_ranges.to_string_raw(), src_str))
elif self.version >= 3:
# take into account automatic stashing of overlapping blocks
if xf.src_ranges.overlaps(xf.tgt_ranges):
temp_stash_usage = stashed_blocks + xf.src_ranges.size()
if temp_stash_usage > max_stashed_blocks:
max_stashed_blocks = temp_stash_usage
self.touched_src_ranges = self.touched_src_ranges.union(
xf.src_ranges)
out.append("%s %s %s %s\n" % (
xf.style,
self.HashBlocks(self.tgt, xf.tgt_ranges),
xf.tgt_ranges.to_string_raw(), src_str))
total += tgt_size
elif xf.style in ("bsdiff", "imgdiff"):
assert xf.tgt_ranges
assert xf.src_ranges
if self.version == 1:
out.append("%s %d %d %s %s\n" % (
xf.style, xf.patch_start, xf.patch_len,
xf.src_ranges.to_string_raw(), xf.tgt_ranges.to_string_raw()))
elif self.version == 2:
out.append("%s %d %d %s %s\n" % (
xf.style, xf.patch_start, xf.patch_len,
xf.tgt_ranges.to_string_raw(), src_str))
elif self.version >= 3:
# take into account automatic stashing of overlapping blocks
if xf.src_ranges.overlaps(xf.tgt_ranges):
temp_stash_usage = stashed_blocks + xf.src_ranges.size()
if temp_stash_usage > max_stashed_blocks:
max_stashed_blocks = temp_stash_usage
self.touched_src_ranges = self.touched_src_ranges.union(
xf.src_ranges)
out.append("%s %d %d %s %s %s %s\n" % (
xf.style,
xf.patch_start, xf.patch_len,
self.HashBlocks(self.src, xf.src_ranges),
self.HashBlocks(self.tgt, xf.tgt_ranges),
xf.tgt_ranges.to_string_raw(), src_str))
total += tgt_size
elif xf.style == "zero":
assert xf.tgt_ranges
to_zero = xf.tgt_ranges.subtract(xf.src_ranges)
assert WriteTransfersZero(out, to_zero) == to_zero.size()
total += to_zero.size()
else:
raise ValueError("unknown transfer style '%s'\n" % xf.style)
if free_string:
out.append("".join(free_string))
stashed_blocks -= free_size
if self.version >= 2 and Settings.cache_size is not None:
# Sanity check: abort if we're going to need more stash space than
# the allowed size (cache_size * threshold). There are two purposes
# of having a threshold here. a) Part of the cache may have been
# occupied by some recovery logs. b) It will buy us some time to deal
# with the oversize issue.
cache_size = Settings.cache_size
stash_threshold = Settings.stash_threshold
max_allowed = cache_size * stash_threshold
assert max_stashed_blocks * self.tgt.blocksize < max_allowed, \
'Stash size %d (%d * %d) exceeds the limit %d (%d * %.2f)' % (
max_stashed_blocks * self.tgt.blocksize, max_stashed_blocks,
self.tgt.blocksize, max_allowed, cache_size,
stash_threshold)
if self.version >= 3:
self.touched_src_sha1 = self.HashBlocks(
self.src, self.touched_src_ranges)
# Zero out extended blocks as a workaround for bug 20881595.
if self.tgt.extended.size() > 0:
assert (WriteTransfersZero(out, self.tgt.extended) ==
self.tgt.extended.size())
total += self.tgt.extended.size()
# We erase all the blocks on the partition that a) don't contain useful
# data in the new image; b) will not be touched by dm-verity. Out of those
# blocks, we erase the ones that won't be used in this update at the
# beginning of an update. The rest would be erased at the end. This is to
# work around the eMMC issue observed on some devices, which may otherwise
# get starving for clean blocks and thus fail the update. (b/28347095)
all_tgt = RangeSet(data=(0, self.tgt.total_blocks))
all_tgt_minus_extended = all_tgt.subtract(self.tgt.extended)
new_dontcare = all_tgt_minus_extended.subtract(self.tgt.care_map)
erase_first = new_dontcare.subtract(self.touched_src_ranges)
if erase_first.size() > 0:
out.insert(0, "erase %s\n" % (erase_first.to_string_raw(),))
erase_last = new_dontcare.subtract(erase_first)
if erase_last.size() > 0:
out.append("erase %s\n" % (erase_last.to_string_raw(),))
out.insert(0, "%d\n" % (self.version,)) # format version number
out.insert(1, "%d\n" % (total,))
if self.version >= 2:
# version 2 only: after the total block count, we give the number
# of stash slots needed, and the maximum size needed (in blocks)
out.insert(2, str(next_stash_id) + "\n")
out.insert(3, str(max_stashed_blocks) + "\n")
with open(prefix + ".transfer.list", "wb") as f:
for i in out:
f.write(i.encode("UTF-8"))
if self.version >= 2:
self._max_stashed_size = max_stashed_blocks * self.tgt.blocksize
OPTIONS = Settings
if OPTIONS.cache_size is not None:
max_allowed = OPTIONS.cache_size * OPTIONS.stash_threshold
print("max stashed blocks: %d (%d bytes), "
"limit: %d bytes (%.2f%%)\n" % (
max_stashed_blocks, self._max_stashed_size, max_allowed,
self._max_stashed_size * 100.0 / max_allowed))
else:
print("max stashed blocks: %d (%d bytes), limit: <unknown>\n" % (
max_stashed_blocks, self._max_stashed_size))
def ReviseStashSize(self):
print("Revising stash size...")
stashes = {}
# Create the map between a stash and its def/use points. For example, for a
# given stash of (idx, sr), stashes[idx] = (sr, def_cmd, use_cmd).
for xf in self.transfers:
# Command xf defines (stores) all the stashes in stash_before.
for idx, sr in xf.stash_before:
stashes[idx] = (sr, xf)
# Record all the stashes command xf uses.
for idx, _ in xf.use_stash:
stashes[idx] += (xf,)
# Compute the maximum blocks available for stash based on /cache size and
# the threshold.
cache_size = Settings.cache_size
stash_threshold = Settings.stash_threshold
max_allowed = cache_size * stash_threshold / self.tgt.blocksize
stashed_blocks = 0
new_blocks = 0
# Now go through all the commands. Compute the required stash size on the
# fly. If a command requires excess stash than available, it deletes the
# stash by replacing the command that uses the stash with a "new" command
# instead.
for xf in self.transfers:
replaced_cmds = []
# xf.stash_before generates explicit stash commands.
for idx, sr in xf.stash_before:
if stashed_blocks + sr.size() > max_allowed:
# We cannot stash this one for a later command. Find out the command
# that will use this stash and replace the command with "new".
use_cmd = stashes[idx][2]
replaced_cmds.append(use_cmd)
print("%10d %9s %s" % (sr.size(), "explicit", use_cmd))
else:
stashed_blocks += sr.size()
# xf.use_stash generates free commands.
for _, sr in xf.use_stash:
stashed_blocks -= sr.size()
# "move" and "diff" may introduce implicit stashes in BBOTA v3. Prior to
# ComputePatches(), they both have the style of "diff".
if xf.style == "diff" and self.version >= 3:
assert xf.tgt_ranges and xf.src_ranges
if xf.src_ranges.overlaps(xf.tgt_ranges):
if stashed_blocks + xf.src_ranges.size() > max_allowed:
replaced_cmds.append(xf)
print("%10d %9s %s" % (xf.src_ranges.size(), "implicit", xf))
# Replace the commands in replaced_cmds with "new"s.
for cmd in replaced_cmds:
# It no longer uses any commands in "use_stash". Remove the def points
# for all those stashes.
for idx, sr in cmd.use_stash:
def_cmd = stashes[idx][1]
assert (idx, sr) in def_cmd.stash_before
def_cmd.stash_before.remove((idx, sr))
# Add up blocks that violates space limit and print total number to
# screen later.
new_blocks += cmd.tgt_ranges.size()
cmd.ConvertToNew()
num_of_bytes = new_blocks * self.tgt.blocksize
print(" Total %d blocks (%d bytes) are packed as new blocks due to "
"insufficient cache size." % (new_blocks, num_of_bytes))
def ComputePatches(self, prefix):
print("Reticulating splines...")
diff_q = []
patch_num = 0
with open(prefix + ".new.dat", "wb") as new_f:
for xf in self.transfers:
if xf.style == "zero":
pass
elif xf.style == "new":
for piece in self.tgt.ReadRangeSet(xf.tgt_ranges):
new_f.write(piece)
elif xf.style == "diff":
src = self.src.ReadRangeSet(xf.src_ranges)
tgt = self.tgt.ReadRangeSet(xf.tgt_ranges)
# We can't compare src and tgt directly because they may have
# the same content but be broken up into blocks differently, eg:
#
# ["he", "llo"] vs ["h", "ello"]
#
# We want those to compare equal, ideally without having to
# actually concatenate the strings (these may be tens of
# megabytes).
src_sha1 = sha1()
for p in src:
src_sha1.update(p)
tgt_sha1 = sha1()
tgt_size = 0
for p in tgt:
tgt_sha1.update(p)
tgt_size += len(p)
if src_sha1.digest() == tgt_sha1.digest():
# These are identical; we don't need to generate a patch,
# just issue copy commands on the device.
xf.style = "move"
else:
# For files in zip format (eg, APKs, JARs, etc.) we would
# like to use imgdiff -z if possible (because it usually
# produces significantly smaller patches than bsdiff).
# This is permissible if:
#
# - imgdiff is not disabled, and
# - the source and target files are monotonic (ie, the
# data is stored with blocks in increasing order), and
# - we haven't removed any blocks from the source set.
#
# If these conditions are satisfied then appending all the
# blocks in the set together in order will produce a valid
# zip file (plus possibly extra zeros in the last block),
# which is what imgdiff needs to operate. (imgdiff is
# fine with extra zeros at the end of the file.)
imgdiff = (not self.disable_imgdiff and xf.intact and
xf.tgt_name.split(".")[-1].lower()
in ("apk", "jar", "zip"))
xf.style = "imgdiff" if imgdiff else "bsdiff"
diff_q.append((tgt_size, src, tgt, xf, patch_num))
patch_num += 1
else:
assert False, "unknown style " + xf.style
if diff_q:
if self.threads > 1:
print("Computing patches (using %d threads)..." % (self.threads,))
else:
print("Computing patches...")
diff_q.sort()
patches = [None] * patch_num
# TODO: Rewrite with multiprocessing.ThreadPool?
lock = Lock()
def diff_worker():
while True:
with lock:
if not diff_q:
return
tgt_size, src, tgt, xf, patchnum = diff_q.pop()
patch = compute_patch(src, tgt, imgdiff=(xf.style == "imgdiff"))
size = len(patch)
with lock:
patches[patchnum] = (patch, xf)
print("%10d %10d (%6.2f%%) %7s %s" % (
size, tgt_size, size * 100.0 / tgt_size, xf.style,
xf.tgt_name if xf.tgt_name == xf.src_name else (
xf.tgt_name + " (from " + xf.src_name + ")")))
threads = [Thread(target=diff_worker)
for _ in range(self.threads)]
for th in threads:
th.start()
while threads:
threads.pop().join()
else:
patches = []
p = 0
with open(prefix + ".patch.dat", "wb") as patch_f:
for patch, xf in patches:
xf.patch_start = p
xf.patch_len = len(patch)
patch_f.write(patch)
p += len(patch)
def AssertSequenceGood(self):
# Simulate the sequences of transfers we will output, and check that:
# - we never read a block after writing it, and
# - we write every block we care about exactly once.
# Start with no blocks having been touched yet.
touched = array("B", (0,) * self.tgt.total_blocks)
# Imagine processing the transfers in order.
for xf in self.transfers:
# Check that the input blocks for this transfer haven't yet been touched.
x = xf.src_ranges
if self.version >= 2:
for _, sr in xf.use_stash:
x = x.subtract(sr)
for s, e in x:
# Source image could be larger. Don't check the blocks that are in the
# source image only. Since they are not in 'touched', and won't ever
# be touched.
for i in range(s, min(e, self.tgt.total_blocks)):
assert touched[i] == 0
# Check that the output blocks for this transfer haven't yet
# been touched, and touch all the blocks written by this
# transfer.
for s, e in xf.tgt_ranges:
for i in range(s, e):
assert touched[i] == 0
touched[i] = 1
# Check that we've written every target block.
for s, e in self.tgt.care_map:
for i in range(s, e):
assert touched[i] == 1
def ImproveVertexSequence(self):
print("Improving vertex order...")
# At this point our digraph is acyclic; we reversed any edges that
# were backwards in the heuristically-generated sequence. The
# previously-generated order is still acceptable, but we hope to
# find a better order that needs less memory for stashed data.
# Now we do a topological sort to generate a new vertex order,
# using a greedy algorithm to choose which vertex goes next
# whenever we have a choice.
# Make a copy of the edge set; this copy will get destroyed by the
# algorithm.
for xf in self.transfers:
xf.incoming = xf.goes_after.copy()
xf.outgoing = xf.goes_before.copy()
L = [] # the new vertex order
# S is the set of sources in the remaining graph; we always choose
# the one that leaves the least amount of stashed data after it's
# executed.
S = [(u.NetStashChange(), u.order, u) for u in self.transfers
if not u.incoming]
heapify(S)
while S:
_, _, xf = heappop(S)
L.append(xf)
for u in xf.outgoing:
del u.incoming[xf]
if not u.incoming:
heappush(S, (u.NetStashChange(), u.order, u))
# if this fails then our graph had a cycle.
assert len(L) == len(self.transfers)
self.transfers = L
for i, xf in enumerate(L):
xf.order = i
def RemoveBackwardEdges(self):
print("Removing backward edges...")
in_order = 0
out_of_order = 0
lost_source = 0
for xf in self.transfers:
size = xf.src_ranges.size()
for u in xf.goes_before:
# xf should go before u
if xf.order < u.order:
# it does, hurray!
in_order += 1
else:
# it doesn't, boo. trim the blocks that u writes from xf's
# source, so that xf can go after u.
out_of_order += 1
assert xf.src_ranges.overlaps(u.tgt_ranges)
xf.src_ranges = xf.src_ranges.subtract(u.tgt_ranges)
xf.intact = False
if xf.style == "diff" and not xf.src_ranges:
# nothing left to diff from; treat as new data
xf.style = "new"
lost = size - xf.src_ranges.size()
lost_source += lost
print((" %d/%d dependencies (%.2f%%) were violated; "
"%d source blocks removed.") %
(out_of_order, in_order + out_of_order,
(out_of_order * 100.0 / (in_order + out_of_order))
if (in_order + out_of_order) else 0.0,
lost_source))
def ReverseBackwardEdges(self):
print("Reversing backward edges...")
in_order = 0
out_of_order = 0
stashes = 0
stash_size = 0
for xf in self.transfers:
for u in xf.goes_before.copy():
# xf should go before u
if xf.order < u.order:
# it does, hurray!
in_order += 1
else:
# it doesn't, boo. modify u to stash the blocks that it
# writes that xf wants to read, and then require u to go
# before xf.
out_of_order += 1
overlap = xf.src_ranges.intersect(u.tgt_ranges)
assert overlap
u.stash_before.append((stashes, overlap))
xf.use_stash.append((stashes, overlap))
stashes += 1
stash_size += overlap.size()
# reverse the edge direction; now xf must go after u
del xf.goes_before[u]
del u.goes_after[xf]
xf.goes_after[u] = None # value doesn't matter
u.goes_before[xf] = None
print((" %d/%d dependencies (%.2f%%) were violated; "
"%d source blocks stashed.") %
(out_of_order, in_order + out_of_order,
(out_of_order * 100.0 / (in_order + out_of_order))
if (in_order + out_of_order) else 0.0,
stash_size))
def FindVertexSequence(self):
print("Finding vertex sequence...")
# This is based on "A Fast & Effective Heuristic for the Feedback
# Arc Set Problem" by P. Eades, X. Lin, and W.F. Smyth. Think of
# it as starting with the digraph G and moving all the vertices to
# be on a horizontal line in some order, trying to minimize the
# number of edges that end up pointing to the left. Left-pointing
# edges will get removed to turn the digraph into a DAG. In this
# case each edge has a weight which is the number of source blocks
# we'll lose if that edge is removed; we try to minimize the total
# weight rather than just the number of edges.
# Make a copy of the edge set; this copy will get destroyed by the
# algorithm.
for xf in self.transfers:
xf.incoming = xf.goes_after.copy()
xf.outgoing = xf.goes_before.copy()
xf.score = sum(xf.outgoing.values()) - sum(xf.incoming.values())
# We use an OrderedDict instead of just a set so that the output
# is repeatable; otherwise it would depend on the hash values of
# the transfer objects.
G = OrderedDict()
for xf in self.transfers:
G[xf] = None
s1 = deque() # the left side of the sequence, built from left to right
s2 = deque() # the right side of the sequence, built from right to left
heap = []
for xf in self.transfers:
xf.heap_item = HeapItem(xf)
heap.append(xf.heap_item)
heapify(heap)
sinks = set(u for u in G if not u.outgoing)
sources = set(u for u in G if not u.incoming)
def adjust_score(iu, delta):
iu.score += delta
iu.heap_item.clear()
iu.heap_item = HeapItem(iu)