[Issue 22838] New: std.bitmanip.BitArray.count() reads beyond data when data size is integer size_t multiple
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Wed Mar 2 23:53:09 UTC 2022
https://issues.dlang.org/show_bug.cgi?id=22838
Issue ID: 22838
Summary: std.bitmanip.BitArray.count() reads beyond data when
data size is integer size_t multiple
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: critical
Priority: P1
Component: phobos
Assignee: nobody at puremagic.com
Reporter: johanengelen at weka.io
Bug was found with AddressSanitizer. Report at the end.
The unittest that triggers the bug:
```
@system @nogc unittest
{
size_t[2] buffer;
BitArray b = BitArray(buffer[], buffer.sizeof * 8);
b[] = true;
b[0 .. 1] = true;
b.flip();
b.flip(1);
cast(void) b.count();
}
```
The bug happens on this line:
```
size_t count() const @nogc pure nothrow
{
if (_ptr)
{
size_t bitCount;
foreach (i; 0 .. fullWords)
bitCount += countBitsSet(_ptr[i]);
bitCount += countBitsSet(_ptr[fullWords] & endMask); // <---- BUG!
return bitCount;
}
else
{
return 0;
}
}
```
```
==60514==ERROR: AddressSanitizer: stack-buffer-overflow on address
0x00016d251750 at pc 0x00010402fce0 bp 0x00016d251680 sp 0x00016d251678
READ of size 8 at 0x00016d251750 thread T0
#0 0x10402fcdc in _D3std8bitmanip8BitArray5countMxFNaNbNiZm bitmanip.d:1450
#1 0x1040509d8 in _D3std8bitmanip8BitArray20__unittest_L2753_C19FNiZv
bitmanip.d:2762
#2 0x1040ca2a0 in _D3std8bitmanip10__unittestZ bitmanip.d
#3 0x102bb2208 in
_D11test_runner7testAllFZ14__foreachbody2MFPS6object10ModuleInfoZi
test_runner.d:65
#4 0x1094240dc in
_D6object10ModuleInfo7opApplyFMDFPSQBhQBdZiZ9__lambda2MFyPSQCfQCbZi
object.d:2476
#5 0x10947466c in
_D2rt5minfo17moduleinfos_applyFMDFyPS6object10ModuleInfoZiZ14__foreachbody2MFKSQCz19sections_elf_shared3DSOZi
minfo.d:777
#6 0x10947c38c in _D2rt19sections_elf_shared3DSO7opApplyFMDFKSQBqQBqQyZiZi
sections_elf_shared.d:112
#7 0x109474254 in
_D2rt5minfo17moduleinfos_applyFMDFyPS6object10ModuleInfoZiZi minfo.d:770
#8 0x10941cbb8 in _D6object10ModuleInfo7opApplyFMDFPSQBhQBdZiZi
object.d:2475
#9 0x102bb1abc in _D11test_runner7testAllFZS4core7runtime14UnitTestResult
test_runner.d:63
#10 0x102bb0fa4 in _D11test_runner6testerFZS4core7runtime14UnitTestResult
test_runner.d:33
#11 0x1093ecaf4 in runModuleUnitTests runtime.d:580
#12 0x10944d9ec in _D2rt6dmain212_d_run_main2UAAamPUQgZiZ6runAllMFZv
dmain2.d:487
#13 0x10944d748 in _D2rt6dmain212_d_run_main2UAAamPUQgZiZ7tryExecMFMDFZvZv
dmain2.d:461
#14 0x10944cefc in _d_run_main2 dmain2.d:520
#15 0x10944c384 in _d_run_main dmain2.d:320
#16 0x102bb280c in main entrypoint.d:42
#17 0x1105cd0f0 in start+0x204 (dyld:arm64+0x50f0)
#18 0x960c7ffffffffffc (<unknown module>)
Address 0x00016d251750 is located in stack of thread T0 at offset 48 in frame
#0 0x104050858 in _D3std8bitmanip8BitArray20__unittest_L2753_C19FNiZv
bitmanip.d:2753
This frame has 2 object(s):
[32, 48) '' <== Memory access at offset 48 overflows this variable
[64, 80) ''
HINT: this may be a false positive if your program uses some custom stack
unwind mechanism, swapcontext or vfork
(longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-buffer-overflow bitmanip.d:1450 in
_D3std8bitmanip8BitArray5countMxFNaNbNiZm
Shadow bytes around the buggy address:
0x00702da6a290: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x00702da6a2a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x00702da6a2b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x00702da6a2c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x00702da6a2d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x00702da6a2e0: 00 00 00 00 f1 f1 f1 f1 00 00[f2]f2 00 00 f3 f3
0x00702da6a2f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x00702da6a300: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x00702da6a310: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x00702da6a320: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x00702da6a330: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
Shadow gap: cc
==60514==ABORTING
```
--
More information about the Digitalmars-d-bugs
mailing list