|
| 1 | +// Copyright 2017 The Emscripten Authors. All rights reserved. |
| 2 | +// Emscripten is available under two separate licenses, the MIT license and the |
| 3 | +// University of Illinois/NCSA Open Source License. Both these licenses can be |
| 4 | +// found in the LICENSE file. |
| 5 | + |
| 6 | +#include <memory.h> |
| 7 | +#include <string.h> |
| 8 | +#include <stdio.h> |
| 9 | +#include <stdlib.h> |
| 10 | + |
| 11 | +char dst[1024*1024*2+64] = {}; |
| 12 | +char src[1024*1024*2+64] = {}; |
| 13 | + |
| 14 | +#define GUARDSIZE 32 |
| 15 | +void test_memcpy(int copySize, int srcOffset, int dstOffset) { |
| 16 | + char *dstContent = dst + GUARDSIZE + dstOffset; |
| 17 | + char *srcContent = src + GUARDSIZE + srcOffset; |
| 18 | + |
| 19 | + char *dstGuardBefore = dstContent - GUARDSIZE; |
| 20 | + char *dstGuardAfter = dstContent + copySize; |
| 21 | + |
| 22 | + char *srcGuardBefore = srcContent - GUARDSIZE; |
| 23 | + char *srcGuardAfter = srcContent + copySize; |
| 24 | + |
| 25 | + char guard = (char)rand(); |
| 26 | + |
| 27 | + for (int i = 0; i < GUARDSIZE; ++i) { |
| 28 | + // Generate a guardband area around dst memory that should not change. |
| 29 | + dstGuardBefore[i] = (char)(guard ^ i); |
| 30 | + dstGuardAfter[i] = (char)(guard ^ i); |
| 31 | + } |
| 32 | + |
| 33 | + // Generate copy source data |
| 34 | + char s = (char)rand(); |
| 35 | + for (int i = 0; i < copySize + srcOffset + 2*GUARDSIZE; ++i) { |
| 36 | + src[i] = (char)(s - i); |
| 37 | + } |
| 38 | + |
| 39 | + memcpy(dstContent, srcContent, copySize); |
| 40 | + if (!!memcmp(dstContent, srcContent, copySize)) { |
| 41 | + printf("test_memcpy(copySize=%d, srcOffset=%d, dstOffset=%d failed!\n", copySize, srcOffset, dstOffset); |
| 42 | + exit(1); |
| 43 | + } |
| 44 | + |
| 45 | + // Verify guardband area |
| 46 | + for (int i = 0; i < GUARDSIZE; ++i) { |
| 47 | + // Generate a guardband area around dst memory that should not change. |
| 48 | + if (dstGuardBefore[i] != (char)(guard ^ i)) { |
| 49 | + printf("test_memcpy(copySize=%d, srcOffset=%d, dstOffset=%d failed! Pre-guardband area at i=%d overwritten!\n", copySize, srcOffset, dstOffset, i); |
| 50 | + exit(1); |
| 51 | + } |
| 52 | + if (dstGuardAfter[i] != (char)(guard ^ i)) { |
| 53 | + printf("test_memcpy(copySize=%d, srcOffset=%d, dstOffset=%d failed! Post-guardband area at i=%d overwritten!\n", copySize, srcOffset, dstOffset, i); |
| 54 | + exit(1); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +void test_copysize(int copySize) { |
| 60 | + int offsets[6] = { 0, 3, 4, 5, 8, 11 }; |
| 61 | + |
| 62 | + for (int srcOffset = 0; srcOffset < 6; ++srcOffset) { |
| 63 | + for (int dstOffset = 0; dstOffset < 6; ++dstOffset) { |
| 64 | + test_memcpy(copySize, offsets[srcOffset], offsets[dstOffset]); |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +int main() { |
| 70 | + for (int copySize = 0; copySize < 128; ++copySize) { |
| 71 | + test_copysize(copySize); |
| 72 | + } |
| 73 | + |
| 74 | + for (int copySizeI = 128; copySizeI <= 1048576; copySizeI <<= 1) { |
| 75 | + for (int copySizeJ = 1; copySizeJ <= 16; copySizeJ <<= 1) { |
| 76 | + test_copysize(copySizeI | copySizeJ); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + printf("OK.\n"); |
| 81 | +} |
0 commit comments