-
Notifications
You must be signed in to change notification settings - Fork 68
/
utils.c
683 lines (563 loc) · 16.7 KB
/
utils.c
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
#include "pharovm/pharo.h"
#include <sys/stat.h>
#include "pharovm/pathUtilities.h"
#ifndef _WIN32
#include <libgen.h>
#include <sys/param.h>
#else
#include <windows.h>
/* Maximum length of file name */
#if !defined(PATH_MAX)
# define PATH_MAX MAX_PATH
#endif
#if !defined(FILENAME_MAX)
# define FILENAME_MAX MAX_PATH
#endif
#if !defined(NAME_MAX)
# define NAME_MAX FILENAME_MAX
#endif
#endif
#include <signal.h>
char vmName[PATH_MAX];
char imageName[PATH_MAX];
char vmFullPath[PATH_MAX];
char vmPath[PATH_MAX];
#if __APPLE__
void fillApplicationDirectory(char* vmPath);
#endif
#ifdef _WIN32
BOOL fIsConsole = 1;
#endif
int isVMRunOnWorkerThread(void);
void *os_exports[][3]=
{
{ 0, 0, 0 }
};
static const char* systemSearchPaths[] = {
#ifdef _WIN32
#endif
#if defined(__linux__) || defined(unix) || defined(__APPLE__)
"./",
"/usr/local/lib/",
"/usr/lib/",
"/lib/",
# if defined(__linux__)
# if defined(__i386__)
"/usr/local/lib/i386-linux-gnu/",
"/usr/lib/i386-linux-gnu/",
"/lib/i386-linux-gnu/",
# elif defined(__x86_64__)
"/usr/local/lib/x86_64-linux-gnu/",
"/usr/lib/x86_64-linux-gnu/",
"/lib/x86_64-linux-gnu/",
# endif
# endif
#endif
NULL
};
char** pluginPaths = NULL;
char* emptyPaths[] = {NULL};
EXPORT(const char*) getSourceVersion(){
return VM_BUILD_SOURCE_STRING;
}
EXPORT(const char*) getVMVersion(){
return VM_BUILD_STRING;
}
const char * GetAttributeString(sqInt id)
{
if (id < 0) /* VM argument */
{
if (-id < getVMArgumentCount()){
return getVMArgument(-id);
}
success(false);
return "";
}
switch (id)
{
case 0:
return vmFullPath;
case 1:
return getImageName();
case 1001:
/* OS type: "unix", "win32", "mac", ... */
return OS_TYPE;
case 1002:
/* OS name: e.g. "solaris2.5" on unix, "win95" on win32, ... */
return VM_TARGET_OS;
case 1003:
/* processor architecture: e.g. "68k", "x86", "PowerPC", ... */
return VM_TARGET_CPU;
case 1004:
/* Interpreter version string */
return interpreterVersion;
case 1006:
/* vm build string */
return getVMVersion();
#if STACKVM
case 1007: { /* interpreter build info */
extern char *__interpBuildInfo;
return __interpBuildInfo;
}
# if COGVM
case 1008: { /* cogit build info */
extern char *__cogitBuildInfo;
return __cogitBuildInfo;
}
# endif
#endif
case 1009: /* source tree version info */
return getSourceVersion();
case 1010: /* Implements AIO Interrupt */
return "AIO";
case 1011:
return isVMRunOnWorkerThread() ? "WORKER_THREAD" : "MAIN_THREAD";
default:
if ((id - 2) < getImageArgumentCount())
return getImageArgument(id - 2);
}
success(false);
return "";
}
sqInt attributeSize(sqInt id)
{
return strlen(GetAttributeString(id));
}
sqInt getAttributeIntoLength(sqInt id, sqInt byteArrayIndex, sqInt length)
{
if (length > 0) {
#ifdef _WIN32
/*
* Unsafe version of deprecated strncpy for compatibility
* - does not check error code
* - does use count as the size of the destination buffer
*/
strncpy_s(pointerForOop(byteArrayIndex), length + 1, GetAttributeString(id), length);
#else
strncpy(pointerForOop(byteArrayIndex), GetAttributeString(id), length);
#endif
}
return 0;
}
/**
* Returns the VM Name
*/
EXPORT(char*) getVMName(){
return vmName;
}
/**
* Sets the VMName.
* It copies the parameter to internal storage.
*/
void setVMName(const char* name){
#ifdef _WIN32
/*
* Unsafe version of deprecated strcpy for compatibility
* - does not check error code
* - does use count as the size of the destination buffer
*/
strcpy_s(vmName, strlen(name), name);
#else
strcpy(vmName, name);
#endif
}
char* getImageName(){
return imageName;
}
/**
* Sets the ImageName.
* It copies the parameter to internal storage.
*/
void setImageName(const char* name){
#ifdef _WIN32
/*
* Unsafe version of deprecated strcpy for compatibility
* - does not check error code
* - does use count as the size of the destination buffer
*/
strcpy_s(imageName, PATH_MAX, name);
#else
strcpy(imageName, name);
#endif
}
/**
* Sets the Full VM Path.
* It copies the parameter to internal storage.
*/
EXPORT(void) setVMPath(const char* name){
#ifdef _WIN32
/*
* Unsafe version of deprecated strcpy for compatibility
* - does not check error code
* - does use count as the size of the destination buffer
*/
strcpy_s(vmFullPath, PATH_MAX, name);
#else
strcpy(vmFullPath, name);
#endif
#if __APPLE__
fillApplicationDirectory(vmPath);
#else
int bufferSize = strlen(name) + 1;
char* tmpBasedir = (char*)alloca(bufferSize);
getBasePath(name, tmpBasedir, bufferSize);
# ifdef _WIN32
/*
* Unsafe version of deprecated strcpy for compatibility
* - does not check error code
* - does use count as the size of the destination buffer
*/
strcpy_s(vmPath, bufferSize, tmpBasedir);
# else
strcpy(vmPath, tmpBasedir);
# endif
#endif
}
sqInt vmPathSize(void){
return strlen(vmPath);
}
sqInt vmPathGetLength(sqInt sqVMPathIndex, sqInt length){
char *stVMPath= pointerForOop(sqVMPathIndex);
int count;
count = strlen(vmPath);
count = (length < count) ? length : count;
/* copy the file name into the Squeak string */
memcpy(stVMPath, vmPath, count);
return count;
}
sqInt imageNameGetLength(sqInt sqImageNameIndex, sqInt length){
char *sqImageName = pointerForOop(sqImageNameIndex);
int count;
count= strlen(imageName);
count= (length < count) ? length : count;
/* copy the file name into the Squeak string */
memcpy(sqImageName, imageName, count);
return count;
}
sqInt imageNamePutLength(sqInt sqImageNameIndex, sqInt length){
char *sqImageName= pointerForOop(sqImageNameIndex);
int count;
count = (length >= sizeof(imageName)) ? sizeof(imageName) - 1 : length;
/* copy the file name into a null-terminated C string */
memcpy(imageName, sqImageName, count);
imageName[count] = 0;
return count;
}
sqInt
imageNameSize(void)
{
return strlen(imageName);
}
int vmParamsCount = 0;
int imageParamsCount = 0;
char** vmParams = NULL;
char** imageParams = NULL;
int getVMArgumentCount(){
return vmParamsCount;
}
char* getVMArgument(int index){
if(vmParamsCount <= index)
return NULL;
return vmParams[index];
}
int getImageArgumentCount(){
return imageParamsCount;
}
char* getImageArgument(int index){
if(imageParamsCount <= index)
return NULL;
return imageParams[index];
}
static void
copyParams(int newCount, const char** newParams, int* oldCount, char*** old){
int i;
//Releasing the old params
if(*oldCount > 0){
for(i=0; i < *oldCount; i++){
free((*old)[i]);
}
free(*old);
*oldCount = 0;
}
if(newCount==0) return;
*oldCount = newCount;
*old = (char**)malloc(sizeof(char*) * newCount);
for(i=0; i < newCount; i++){
int oldSize = strlen(newParams[i]) + 1;
(*old)[i] = (char*)malloc(oldSize);
#ifdef _WIN32
/*
* Unsafe version of deprecated strcpy for compatibility
* - does not check error code
*/
strcpy_s((*old)[i], oldSize, newParams[i]);
#else
strcpy((*old)[i], newParams[i]);
#endif
}
}
void
setPharoCommandLineParameters(const char** newVMParams, int newVMParamsCount, const char** newImageParams, int newImageParamsCount)
{
copyParams(newVMParamsCount, newVMParams, &vmParamsCount, &vmParams);
copyParams(newImageParamsCount, newImageParams, &imageParamsCount, &imageParams);
}
char**
getSystemSearchPaths(){
return (char**)systemSearchPaths;
}
char**
getPluginPaths(){
if(pluginPaths == NULL) {
return (char**) emptyPaths;
}
return (char**) pluginPaths;
}
sqInt
ioExitWithErrorCode(int errorCode)
{
exit(errorCode);
}
/* New filename converting function; used by the interpreterProxy function
ioFilenamefromStringofLengthresolveAliases. Most platforms can ignore the
resolveAlias boolean - it seems to only be of use by OSX but is crucial there.
*/
sqInt
sqGetFilenameFromString(char * aCharBuffer, char * aFilenameString, sqInt filenameLength, sqInt resolveAlias)
{
memcpy(aCharBuffer, aFilenameString, filenameLength);
aCharBuffer[filenameLength]= 0;
#ifndef _WIN32
struct stat st;
int numLinks = 0;
if (resolveAlias)
{
for (;;) /* aCharBuffer might refer to link or alias */
{
if (!lstat(aCharBuffer, &st) && S_ISLNK(st.st_mode)) /* symlink */
{
char linkbuf[PATH_MAX+1];
if (++numLinks > MAXSYMLINKS)
return -1; /* too many levels of indirection */
filenameLength= readlink(aCharBuffer, linkbuf, PATH_MAX);
if ((filenameLength < 0) || (filenameLength >= PATH_MAX))
return -1; /* link unavailable or path too long */
linkbuf[filenameLength]= 0;
if (filenameLength > 0 && *linkbuf == '/') /* absolute */
strcpy(aCharBuffer, linkbuf);
else {
char *lastSeparator = strrchr(aCharBuffer,'/');
char *append = lastSeparator ? lastSeparator + 1 : aCharBuffer;
if (append - aCharBuffer + strlen(linkbuf) > PATH_MAX)
return -1; /* path too long */
strcpy(append,linkbuf);
}
continue;
}
#if defined(DARWIN)
if (isMacAlias(aCharBuffer))
{
if ((++numLinks > MAXSYMLINKS) || !resolveMacAlias(aCharBuffer, aCharBuffer, PATH_MAX))
return -1; /* too many levels or bad alias */
continue;
}
#endif
break; /* target is no longer a symlink or alias */
}
}
#endif
return 0;
}
#ifdef DARWIN
#include macAlias.c
#endif
#ifndef bzero
//We have to provide a bzero implementation for windows as the Cog code depends on it.
void bzero(void *s, size_t n){
memset(s, 0, n);
}
#endif
#ifdef COGVM
/*
* Cog has already captured CStackPointer before calling this routine. Record
* the original value, capture the pointers again and determine if CFramePointer
* lies between the two stack pointers and hence is likely in use. This is
* necessary since optimizing C compilers for x86 may use %ebp as a general-
* purpose register, in which case it must not be captured.
*/
int
isCFramePointerInUse()
{
extern unsigned long CStackPointer, CFramePointer;
extern void (*ceCaptureCStackPointers)(void);
unsigned long currentCSP = CStackPointer;
currentCSP = CStackPointer;
ceCaptureCStackPointers();
assert(CStackPointer < currentCSP);
return CFramePointer >= CStackPointer && CFramePointer <= currentCSP;
}
#endif // COGVM
/* Answer an approximation of the size of the redzone (if any). Do so by
* sending a signal to the process and computing the difference between the
* stack pointer in the signal handler and that in the caller. Assumes stacks
* descend.
*/
static char * volatile redZoneTestEndPointer = 0;
#ifdef SIGPROF
static void redZoneTestSigHandler(int sig, siginfo_t *info, void *uap)
{
redZoneTestEndPointer = (char *)&sig;
}
#else
static void redZoneTestSigHandler(int sig)
{
redZoneTestEndPointer = (char *)&sig;
}
#endif
#ifndef _WIN32
static long int min(long int x, long int y) { return (x < y) ? x : y; }
#endif
static int getRedZoneSize()
{
#if defined(SIGPROF) /* cygwin */
struct sigaction handler_action, old;
handler_action.sa_sigaction = redZoneTestSigHandler;
handler_action.sa_flags = SA_NODEFER | SA_SIGINFO;
sigemptyset(&handler_action.sa_mask);
(void)sigaction(SIGPROF, &handler_action, &old);
do kill(getpid(),SIGPROF); while (!redZoneTestEndPointer);
(void)sigaction(SIGPROF, &old, 0);
return (int)min((usqInt)&old,(usqInt)&handler_action) - sizeof(struct sigaction) - (usqInt)redZoneTestEndPointer;
#else /* cygwin */
void (*old)(int) = signal(SIGBREAK, redZoneTestSigHandler);
do raise(SIGBREAK); while (!redZoneTestEndPointer);
return (int) ((char *)&old - redZoneTestEndPointer);
#endif /* cygwin */
}
sqInt reportStackHeadroom;
static int stackPageHeadroom;
/* Answer the redzone size plus space for any signal handlers to run in.
* N.B. Space for signal handers may include space for the dynamic linker to
* run in since signal handlers may reference other functions, and linking may
* be lazy. The reportheadroom switch can be used to check empirically that
* there is sufficient headroom.
*/
int
osCogStackPageHeadroom()
{
if (!stackPageHeadroom)
stackPageHeadroom = getRedZoneSize() + 1024;
return stackPageHeadroom;
}
/****************************************************************************/
/* Helper to pop up a message box with a message formatted from the */
/* printf() format string and arguments */
/****************************************************************************/
#ifdef _WIN32
EXPORT(int) __cdecl sqMessageBox(DWORD dwFlags, const char *titleString, const char* fmt, ...)
{ TCHAR *buf;
va_list args;
DWORD result;
buf = (TCHAR*) calloc(sizeof(TCHAR), 4096);
va_start(args, fmt);
vsnprintf(buf, 4096-1, fmt, args);
va_end(args);
result = MessageBox(NULL,buf,titleString,dwFlags|MB_SETFOREGROUND);
free(buf);
return result;
}
EXPORT(void) printLastError(const TCHAR *prefix) {
LPVOID lpMsgBuf;
DWORD lastError;
lastError = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf, 0, NULL );
wprintf(TEXT("%s (%ld) -- %s\n"), prefix, lastError, (unsigned short*)lpMsgBuf);
LocalFree( lpMsgBuf );
}
EXPORT(int) __cdecl abortMessage(TCHAR *fmt, ...)
{ TCHAR *buf;
va_list args;
va_start(args, fmt);
if (fIsConsole) {
vfwprintf(stderr, fmt, args);
exit(-1);
}
buf = (TCHAR*) calloc(sizeof(TCHAR), 4096);
wvsprintf(buf, fmt, args);
va_end(args);
MessageBox(NULL,buf,TEXT(VM_NAME) TEXT("!"),MB_OK | MB_TASKMODAL | MB_SETFOREGROUND);
free(buf);
exit(-1);
return 0;
}
#endif
EXPORT(char*) getFullPath(char const *relativePath, char* fullPath, int fullPathSize){
#ifdef _WIN32
int requiredSize = MultiByteToWideChar(CP_UTF8, 0, relativePath, -1, NULL, 0);
LPWSTR relativePathWide = (LPWSTR)alloca(sizeof(WCHAR) * (requiredSize + 1));
LPWSTR fullPathWide = (LPWSTR)alloca(sizeof(WCHAR)* fullPathSize);
MultiByteToWideChar(CP_UTF8, 0, relativePath, -1, relativePathWide, requiredSize);
if(GetFullPathNameW(relativePathWide, fullPathSize, fullPathWide, NULL) == 0){
return NULL;
}
WideCharToMultiByte(CP_UTF8, 0, fullPathWide, -1, fullPath, fullPathSize, NULL, 0);
return fullPath;
#else
//If the path is relative everything is fine.
if(relativePath[0] == '/' || relativePath[0] == '.'){
return realpath(relativePath, fullPath);
}
//If the path is not relative we have to add the './' in the beginning.
char* tmpPath = (char*)alloca(fullPathSize);
strcpy(tmpPath, "./");
strcat(tmpPath, relativePath);
return realpath(tmpPath, fullPath);
#endif
}
EXPORT(void) getBasePath(char const *path, char* basePath, int basePathSize){
#ifdef _WIN32
int requiredSize = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0);
LPWSTR pathWide = (LPWSTR)alloca(sizeof(WCHAR) * (requiredSize + 1));
LPWSTR basePathWide = (LPWSTR)alloca(sizeof(WCHAR)* basePathSize);
LPWSTR finalBasePathWide = (LPWSTR)alloca(sizeof(WCHAR)* basePathSize);
LPWSTR driveWide = (LPWSTR)alloca(sizeof(WCHAR)* (2 + 1));
MultiByteToWideChar(CP_UTF8, 0, path, -1, pathWide, requiredSize);
int error = _wsplitpath_s(pathWide, driveWide, 3, basePathWide, basePathSize, NULL, 0, NULL,0);
if(error != 0){
logError("Could not extract basepath: %s", path);
strcpy_s(basePath, basePathSize, "");
return;
}
wcscpy_s(finalBasePathWide, basePathSize, driveWide);
wcscat_s(finalBasePathWide, basePathSize, basePathWide);
WideCharToMultiByte(CP_UTF8, 0, finalBasePathWide, -1, basePath, basePathSize, NULL, 0);
#else
char * pathCopy = strdup(path);
strcpy(basePath, dirname(pathCopy));
free(pathCopy);
#endif
}
int vmProcessArgumentCount = 0;
const char ** vmProcessArgumentVector = NULL;
const char ** vmProcessEnvironmentVector = NULL;
EXPORT(void) setProcessArguments(int count, const char** args){
vmProcessArgumentCount = count;
vmProcessArgumentVector = args;
}
EXPORT(void) setProcessEnvironmentVector(const char** environment){
vmProcessEnvironmentVector = environment;
}
EXPORT(int) getProcessArgumentCount(){
return vmProcessArgumentCount;
}
EXPORT(const char**) getProcessArgumentVector(){
return vmProcessArgumentVector;
}
EXPORT(const char **) getProcessEnvironmentVector(){
return vmProcessEnvironmentVector;
}
EXPORT(int) ioGetCurrentWorkingDirectorymaxLength(char * aCString, size_t maxLength){
return vm_path_get_current_working_dir_into(aCString, maxLength) == VM_SUCCESS ? 0 : -1 ;
}