Skip to content

Commit 3c77465

Browse files
committed
cseries_memory
1 parent 0c2a77e commit 3c77465

7 files changed

Lines changed: 302 additions & 115 deletions

File tree

config/tag_debug_untracked_jul_11_2011/config.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
"decomp": {
2222
"flags": [
2323
"/Isrc",
24-
"/DNOT_IMPLEMENTED=__assume(0)"
24+
"/DNOT_IMPLEMENTED=__assume(0)",
25+
"/Dmangled_ppc=(void)"
2526
]
2627
},
2728
"base": {
2829
"base": "decomp",
2930
"flags": [
3031
"/IXDK\\9328\\include\\xbox",
32+
"/D_XBOX",
3133
"/nologo",
3234
"/c",
3335
"/Od",

src/decomp.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef __DECOMP_H__
2+
#define __DECOMP_H__
3+
#pragma once
4+
5+
/* ---------- headers */
6+
7+
/* ---------- constants */
8+
9+
#define VAR_PREFIX(PREFIX, VAR) _##PREFIX##_##VAR
10+
11+
/* ---------- definitions */
12+
13+
/* ---------- prototypes */
14+
15+
/* ---------- globals */
16+
17+
/* ---------- public code */
18+
19+
#endif // __DECOMP_H__

src/source/core/corelib/cseries/cseries_asserts.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@
66

77
/* ---------- constants */
88

9+
#define assert(expression) if(!(expression) && !assert_handle(#expression, __FILE__, __LINE__)) { if(assert_is_debugger_present()) { __trap(); } else { assert_halt(); } }
10+
#define assert_decomp(expression, file, line) if(!(expression) && !assert_handle(#expression, file, line)) { if(assert_is_debugger_present()) { __trap(); } else { assert_halt(); } }
11+
#define assert_tag_debug_untracked_jul_11_2011(LINE, ...) if(!(__VA_ARGS__) && !assert_handle(#__VA_ARGS__, __FILE_TAG_DEBUG_UNTRACKED_JUL_11_2011__, LINE)) { if(assert_is_debugger_present()) { __trap(); } else { assert_halt(); } }
12+
913
/* ---------- definitions */
1014

1115
/* ---------- prototypes */
1216

17+
extern bool assert_handle(char const * expression, char const * file, long line);
18+
extern bool assert_is_debugger_present(void);
19+
extern void assert_halt(void);
20+
1321
/* ---------- globals */
1422

1523
/* ---------- public code */
1624

1725
#endif // __CSERIES_ASSERTS_H__
18-

src/source/core/corelib/cseries/cseries_memory.cpp

Lines changed: 153 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,170 @@
1+
#define __FILE_TAG_DEBUG_UNTRACKED_JUL_11_2011__ "C:\\SD\\Reach\\Publishing\\Main\\shared\\engine\\source\\core\\corelib\\cseries\\cseries_memory.cpp"
12
/* ---------- headers */
23

34
#include "core\corelib\cseries\cseries_memory.h"
5+
#include "core\corelib\cseries\cseries_asserts.h"
6+
7+
#include "decomp.h"
8+
#include <xtl.h>
49

510
/* ---------- constants */
611

12+
const size_t k_maximum_memcmp_size = 0x20000000;
13+
const size_t k_maximum_memcpy_size = 0x20000000;
14+
const size_t k_maximum_memmove_size = 0x20000000;
15+
const size_t k_maximum_memset_size = 0x20000000;
16+
717
/* ---------- definitions */
818

19+
struct s_memcmp_context
20+
{
21+
unsigned char const* position;
22+
size_t remaining;
23+
};
24+
925
/* ---------- prototypes */
1026

1127
/* ---------- globals */
1228

1329
/* ---------- public code */
1430

31+
bool memory_is_filled_with(void const * source, size_t size, unsigned char c)
32+
{
33+
mangled_ppc("?memory_is_filled_with@@YA_NPBXIE@Z");
34+
35+
unsigned char const* VAR_PREFIX(1, position) = reinterpret_cast<unsigned char const*>(source);
36+
size_t VAR_PREFIX(0, remaining_size) = size;
37+
38+
while(VAR_PREFIX(0, remaining_size) && *VAR_PREFIX(1, position) == c)
39+
{
40+
VAR_PREFIX(1, position)++;
41+
VAR_PREFIX(0, remaining_size)--;
42+
}
43+
return VAR_PREFIX(0, remaining_size) == 0;
44+
};
45+
46+
bool memory_is_zeroed(const void* source, size_t size)
47+
{
48+
mangled_ppc("?memory_is_zeroed@@YA_NPBXI@Z");
49+
50+
unsigned char const* VAR_PREFIX(1, position) = reinterpret_cast<unsigned char const*>(source);
51+
size_t VAR_PREFIX(0, remaining_size) = size;
52+
53+
bool result = true;
54+
while(result && VAR_PREFIX(0, remaining_size) >= sizeof(unsigned long long))
55+
{
56+
if(*reinterpret_cast<unsigned long long const*>(VAR_PREFIX(1, position)))
57+
{
58+
result = false;
59+
break;
60+
}
61+
VAR_PREFIX(1, position) += sizeof(unsigned long long);
62+
VAR_PREFIX(0, remaining_size) -= sizeof(unsigned long long);
63+
}
64+
65+
if(result && VAR_PREFIX(0, remaining_size) >= sizeof(unsigned long))
66+
{
67+
if(*reinterpret_cast<unsigned long const*>(VAR_PREFIX(1, position)))
68+
{
69+
result = false;
70+
}
71+
VAR_PREFIX(1, position) += sizeof(unsigned long);
72+
VAR_PREFIX(0, remaining_size) -= sizeof(unsigned long);
73+
}
74+
75+
while(result && VAR_PREFIX(0, remaining_size) > 0)
76+
{
77+
if(*VAR_PREFIX(1, position))
78+
{
79+
result = false;
80+
break;
81+
}
82+
VAR_PREFIX(1, position)++;
83+
VAR_PREFIX(0, remaining_size)--;
84+
}
85+
86+
return result;
87+
};
88+
89+
void* csmemcpy(void* destination, const void* source, size_t size)
90+
{
91+
mangled_ppc("csmemcpy");
92+
93+
assert_tag_debug_untracked_jul_11_2011(110, size==0 || (destination && source));
94+
assert_tag_debug_untracked_jul_11_2011(111, size>=0 && size<k_maximum_memcpy_size);
95+
assert_tag_debug_untracked_jul_11_2011(112, offset_pointer(source, size)<=destination || offset_pointer(destination, size)<=source);
96+
97+
return memcpy(destination, source, size);
98+
};
99+
100+
void* csmemmove(void* destination, const void* source, size_t size)
101+
{
102+
mangled_ppc("csmemmove");
103+
104+
assert_tag_debug_untracked_jul_11_2011(122, size==0 || (destination && source));
105+
assert_tag_debug_untracked_jul_11_2011(123, size>=0 && size<=k_maximum_memmove_size);
106+
107+
return memmove(destination, source, size);
108+
};
109+
110+
void memmove_guarded(void* destination, const void* source, size_t size, const void* bounds_start, size_t bounds_size)
111+
{
112+
mangled_ppc("?memmove_guarded@@YAXPAXPBXI1I@Z");
113+
114+
assert_tag_debug_untracked_jul_11_2011(135, size>=0 && size<=k_maximum_memmove_size);
115+
assert_tag_debug_untracked_jul_11_2011(136, bounds_size>=0); // $note this always evaluates to true because an unsigned value is always >= 0
116+
if (size > 0)
117+
{
118+
// $note Yes this is toxic. It was still a 32bit cast even in the 64bit build.
119+
120+
unsigned long write_start = (unsigned long)(static_cast<char const*>(destination));
121+
unsigned long write_end = (unsigned long)(write_start + size - 1);
122+
unsigned long bounds_lower = (unsigned long)(static_cast<char const*>(bounds_start));
123+
unsigned long bounds_upper = (unsigned long)(bounds_lower + bounds_size - 1);
124+
125+
assert_tag_debug_untracked_jul_11_2011(146, bounds_upper>=bounds_lower);
126+
assert_tag_debug_untracked_jul_11_2011(147, bounds_size>0);
127+
assert_tag_debug_untracked_jul_11_2011(148, write_start>=bounds_lower && write_start<=bounds_upper);
128+
assert_tag_debug_untracked_jul_11_2011(149, write_end>=bounds_lower && write_end<=bounds_upper);
129+
130+
memmove(destination, source, size);
131+
}
132+
};
133+
134+
void* csmemset(void* buffer, int c, size_t size)
135+
{
136+
mangled_ppc("csmemset");
137+
138+
assert_tag_debug_untracked_jul_11_2011(160, size==0 || buffer);
139+
assert_tag_debug_untracked_jul_11_2011(161, size>=0 && size<=k_maximum_memset_size);
140+
141+
return memset(buffer, c, size);
142+
};
143+
144+
int csmemcmp(const void* p1, const void* p2, size_t size)
145+
{
146+
mangled_ppc("csmemcmp");
147+
148+
assert_tag_debug_untracked_jul_11_2011(171, size==0 || (p1 && p2));
149+
assert_tag_debug_untracked_jul_11_2011(172, size>=0 && size<=k_maximum_memcmp_size);
150+
151+
return memcmp(p1, p2, size);
152+
};
153+
154+
void * memcpy_big_crossplatform_internal(void * destination, void const * source, size_t size)
155+
{
156+
mangled_ppc("?memcpy_big_crossplatform_internal@@YAPAXPAXPBXI@Z");
157+
158+
return XMemCpy(destination, source, size);
159+
};
160+
161+
void * memset_big_crossplatform_internal(void * destination, int c, size_t size)
162+
{
163+
mangled_ppc("?memset_big_crossplatform_internal@@YAPAXPAXHI@Z");
164+
165+
return XMemSet(destination, c, size);
166+
};
167+
15168
/* ---------- private code */
16169

17170
/* ---------- reverse engineering */
18-
19-
// bool memory_is_filled_with(void const *, unsigned int, unsigned char);
20-
// bool memory_is_zeroed(void const *, unsigned int);
21-
// csmemcpy;
22-
// void const * offset_pointer(void const *, long);
23-
// csmemmove;
24-
// void memmove_guarded(void *, void const *, unsigned int, void const *, unsigned int);
25-
// csmemset;
26-
// csmemcmp;
27-
// void * memcpy_big_crossplatform_internal(void *, void const *, unsigned int);
28-
// void * memset_big_crossplatform_internal(void *, int, unsigned int);
29-
30-
//bool memory_is_filled_with(void const *, unsigned int, unsigned char)
31-
//{
32-
// mangled_ppc("?memory_is_filled_with@@YA_NPBXIE@Z");
33-
//};
34-
35-
//bool memory_is_zeroed(void const *, unsigned int)
36-
//{
37-
// mangled_ppc("?memory_is_zeroed@@YA_NPBXI@Z");
38-
//};
39-
40-
//csmemcpy
41-
//{
42-
// mangled_ppc("csmemcpy");
43-
//};
44-
45-
//void const * offset_pointer(void const *, long)
46-
//{
47-
// mangled_ppc("?offset_pointer@@YAPBXPBXJ@Z");
48-
//};
49-
50-
//csmemmove
51-
//{
52-
// mangled_ppc("csmemmove");
53-
//};
54-
55-
//void memmove_guarded(void *, void const *, unsigned int, void const *, unsigned int)
56-
//{
57-
// mangled_ppc("?memmove_guarded@@YAXPAXPBXI1I@Z");
58-
//};
59-
60-
//csmemset
61-
//{
62-
// mangled_ppc("csmemset");
63-
//};
64-
65-
//csmemcmp
66-
//{
67-
// mangled_ppc("csmemcmp");
68-
//};
69-
70-
//void * memcpy_big_crossplatform_internal(void *, void const *, unsigned int)
71-
//{
72-
// mangled_ppc("?memcpy_big_crossplatform_internal@@YAPAXPAXPBXI@Z");
73-
//};
74-
75-
//void * memset_big_crossplatform_internal(void *, int, unsigned int)
76-
//{
77-
// mangled_ppc("?memset_big_crossplatform_internal@@YAPAXPAXHI@Z");
78-
//};
79-

src/source/core/corelib/cseries/cseries_memory.h

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,65 @@
1010

1111
/* ---------- prototypes */
1212

13+
bool memory_is_filled_with(void const * source, size_t size, unsigned char c);
14+
bool memory_is_zeroed(const void* source, size_t size);
15+
void const * offset_pointer(const void* pointer, long offset); // $todo is this intptr_t equivelant?
16+
void memmove_guarded(void* destination, const void* source, size_t size, const void* bounds_start, size_t bounds_size);
17+
void * memcpy_big_crossplatform_internal(void * destination, void const * source, size_t size);
18+
void * memset_big_crossplatform_internal(void * destination, int c, size_t size);
19+
20+
extern "C"
21+
{
22+
void* csmemcpy(void* destination, const void* source, size_t size);
23+
void* csmemmove(void* destination, const void* source, size_t size);
24+
void* csmemset(void* buffer, int c, size_t size);
25+
int csmemcmp(const void* p1, const void* p2, size_t size);
26+
}
27+
28+
inline bool pointer_is_aligned(void const *, long)
29+
{
30+
mangled_ppc("?pointer_is_aligned@@YA_NPBXJ@Z");
31+
32+
NOT_IMPLEMENTED;
33+
};
34+
35+
inline unsigned long address_from_pointer(void const *)
36+
{
37+
mangled_ppc("?address_from_pointer@@YAKPBX@Z");
38+
39+
NOT_IMPLEMENTED;
40+
};
41+
42+
inline bool address_is_aligned(unsigned long, long)
43+
{
44+
mangled_ppc("?address_is_aligned@@YA_NKJ@Z");
45+
46+
NOT_IMPLEMENTED;
47+
};
48+
49+
inline void * pointer_from_address(unsigned long)
50+
{
51+
mangled_ppc("?pointer_from_address@@YAPAXK@Z");
52+
53+
NOT_IMPLEMENTED;
54+
};
55+
56+
inline void * offset_pointer(void *, long)
57+
{
58+
mangled_ppc("?offset_pointer@@YAPAXPAXJ@Z");
59+
60+
NOT_IMPLEMENTED;
61+
};
62+
63+
inline void const * offset_pointer(const void* pointer, long offset)
64+
{
65+
mangled_ppc("?offset_pointer@@YAPBXPBXJ@Z");
66+
67+
return pointer_from_address(address_from_pointer(pointer) + offset);
68+
};
69+
1370
/* ---------- globals */
1471

1572
/* ---------- public code */
1673

1774
#endif // __CSERIES_MEMORY_H__
18-

0 commit comments

Comments
 (0)