File: test_memcpy_alignment.cpp

package info (click to toggle)
emscripten 3.1.6~dfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 114,112 kB
  • sloc: ansic: 583,052; cpp: 391,943; javascript: 79,361; python: 54,180; sh: 49,997; pascal: 4,658; makefile: 3,426; asm: 2,191; lisp: 1,869; ruby: 488; cs: 142
file content (88 lines) | stat: -rw-r--r-- 2,560 bytes parent folder | download | duplicates (2)
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
// Copyright 2017 The Emscripten Authors.  All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License.  Both these licenses can be
// found in the LICENSE file.

#include <memory.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <iostream>
#include <algorithm>

#include <emscripten/emscripten.h>

char dst[1024*1024*2+64] = {};
char src[1024*1024*2+64] = {};

#define GUARDSIZE 32
void test_memcpy(int copySize, int srcOffset, int dstOffset)
{
	char *dstContent = dst + GUARDSIZE + dstOffset;
	char *srcContent = src + GUARDSIZE + srcOffset;

	char *dstGuardBefore = dstContent - GUARDSIZE;
	char *dstGuardAfter  = dstContent + copySize;

	char *srcGuardBefore = srcContent - GUARDSIZE;
	char *srcGuardAfter  = srcContent + copySize;

	char guard = (char)rand();

	for(int i = 0; i < GUARDSIZE; ++i)
	{
		// Generate a guardband area around dst memory that should not change.
		dstGuardBefore[i] = (char)(guard ^ i);
		dstGuardAfter[i] = (char)(guard ^ i);
	}

	// Generate copy source data
	char s = (char)rand();
	for(int i = 0; i < copySize + srcOffset + 2*GUARDSIZE; ++i)
		src[i] = (char)(s - i);

	memcpy(dstContent, srcContent, copySize);
	if (!!memcmp(dstContent, srcContent, copySize))
	{
		printf("test_memcpy(copySize=%d, srcOffset=%d, dstOffset=%d failed!\n", copySize, srcOffset, dstOffset);
		exit(1);
	}

	// Verify guardband area
	for(int i = 0; i < GUARDSIZE; ++i)
	{
		// Generate a guardband area around dst memory that should not change.
		if (dstGuardBefore[i] != (char)(guard ^ i))
		{
			printf("test_memcpy(copySize=%d, srcOffset=%d, dstOffset=%d failed! Pre-guardband area at i=%d overwritten!\n", copySize, srcOffset, dstOffset, i);
			exit(1);
		}
		if (dstGuardAfter[i] != (char)(guard ^ i))
		{
			printf("test_memcpy(copySize=%d, srcOffset=%d, dstOffset=%d failed! Post-guardband area at i=%d overwritten!\n", copySize, srcOffset, dstOffset, i);
			exit(1);
		}
	}
}

void test_copysize(int copySize)
{
	int offsets[6] = { 0, 3, 4, 5, 8, 11 };

	for(int srcOffset = 0; srcOffset < 6; ++srcOffset)
		for(int dstOffset = 0; dstOffset < 6; ++dstOffset)
			test_memcpy(copySize, offsets[srcOffset], offsets[dstOffset]);
}

int main()
{
	for(int copySize = 0; copySize < 128; ++copySize)
		test_copysize(copySize);

	for(int copySizeI = 128; copySizeI <= 1048576; copySizeI <<= 1)
		for(int copySizeJ = 1; copySizeJ <= 16; copySizeJ <<= 1)
			test_copysize(copySizeI | copySizeJ);

	printf("OK.\n");
}