From c32fcf918b1bc2554bd02f733c564860d4d7aead Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Thu, 4 Feb 2010 21:50:33 -0600 Subject: [PATCH 01/11] Fix user-name in unix.users, add unit test --- basis/unix/groups/groups.factor | 2 +- basis/unix/users/users-tests.factor | 2 ++ basis/unix/users/users.factor | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/basis/unix/groups/groups.factor b/basis/unix/groups/groups.factor index c34affb9c3..b009fe529f 100644 --- a/basis/unix/groups/groups.factor +++ b/basis/unix/groups/groups.factor @@ -59,7 +59,7 @@ PRIVATE> [ nip ] [ number>string ] if* ; : group-id ( string -- id/f ) - group-struct [ gr_gid>> ] [ f ] if* ; + group-struct dup [ gr_gid>> ] when ; > ] [ number>string ] if* ; -: user-id ( string -- id ) - user-passwd uid>> ; +: user-id ( string -- id/f ) + user-passwd dup [ uid>> ] when ; : real-user-id ( -- id ) unix.ffi:getuid ; inline From 5a55f3db28124237dcf4ffdb0b6fcd253908c13e Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 4 Feb 2010 22:43:14 -0600 Subject: [PATCH 02/11] windows.types: fix LONG_PTR type on win64 --- basis/windows/types/types.factor | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/basis/windows/types/types.factor b/basis/windows/types/types.factor index ea5daba688..cf21f74fd8 100644 --- a/basis/windows/types/types.factor +++ b/basis/windows/types/types.factor @@ -36,8 +36,8 @@ TYPEDEF: int HFILE TYPEDEF: long LONG TYPEDEF: long* LPLONG -TYPEDEF: long LONG_PTR -TYPEDEF: long* PLONG_PTR +TYPEDEF: intptr_t LONG_PTR +TYPEDEF: intptr_t* PLONG_PTR TYPEDEF: uint ULONG TYPEDEF: void* ULONG_PTR @@ -55,9 +55,6 @@ TYPEDEF: intptr_t UHALF_PTR TYPEDEF: intptr_t INT_PTR TYPEDEF: intptr_t UINT_PTR -TYPEDEF: int LONG_PTR -TYPEDEF: ulong ULONG_PTR - TYPEDEF: int INT32 TYPEDEF: uint UINT32 TYPEDEF: uint DWORD32 From 06f02d13149fa286d9e5bc6fe43f6357c0c296b9 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Thu, 4 Feb 2010 23:52:55 -0600 Subject: [PATCH 03/11] Refactor error handling in io.cpp. Update image.cpp and header files for changes. --- vm/image.cpp | 2 +- vm/io.cpp | 174 +++++++++++++++++++++++++++++---------------------- vm/io.hpp | 4 -- vm/vm.hpp | 9 +++ 4 files changed, 110 insertions(+), 79 deletions(-) diff --git a/vm/image.cpp b/vm/image.cpp index ad21270378..c74351c191 100755 --- a/vm/image.cpp +++ b/vm/image.cpp @@ -301,7 +301,7 @@ bool factor_vm::save_image(const vm_char *saving_filename, const vm_char *filena if(safe_fwrite(&h,sizeof(image_header),1,file) != 1) ok = false; if(safe_fwrite((void*)data->tenured->start,h.data_size,1,file) != 1) ok = false; if(safe_fwrite(code->allocator->first_block(),h.code_size,1,file) != 1) ok = false; - if(safe_fclose(file)) ok = false; + safe_fclose(file); if(!ok) std::cout << "save-image failed: " << strerror(errno) << std::endl; diff --git a/vm/io.cpp b/vm/io.cpp index 880675b212..ae27b5a3d8 100755 --- a/vm/io.cpp +++ b/vm/io.cpp @@ -31,7 +31,38 @@ void factor_vm::io_error() general_error(ERROR_IO,tag_fixnum(errno),false_object,NULL); } -size_t safe_fread(void *ptr, size_t size, size_t nitems, FILE *stream) +FILE *factor_vm::safe_fopen(char *filename, char *mode) +{ + FILE *file; + do { + file = fopen(filename,mode); + if(file == NULL) + io_error(); + else + break; + } while(errno == EINTR); + return file; +} + +int factor_vm::safe_fgetc(FILE *stream) +{ + int c; + do { + c = fgetc(stream); + if(c == EOF) + { + if(feof(stream)) + return EOF; + else + io_error(); + } + else + break; + } while(errno == EINTR); + return c; +} + +size_t factor_vm::safe_fread(void *ptr, size_t size, size_t nitems, FILE *stream) { size_t items_read = 0; @@ -42,7 +73,17 @@ size_t safe_fread(void *ptr, size_t size, size_t nitems, FILE *stream) return items_read; } -size_t safe_fwrite(void *ptr, size_t size, size_t nitems, FILE *stream) +void factor_vm::safe_fputc(int c, FILE *stream) +{ + do { + if(fputc(c,stream) == EOF) + io_error(); + else + break; + } while(errno == EINTR); +} + +size_t factor_vm::safe_fwrite(void *ptr, size_t size, size_t nitems, FILE *stream) { size_t items_written = 0; @@ -53,15 +94,55 @@ size_t safe_fwrite(void *ptr, size_t size, size_t nitems, FILE *stream) return items_written; } -int safe_fclose(FILE *stream) +int factor_vm::safe_ftell(FILE *stream) { - int ret = 0; + off_t offset; + do { + if((offset = FTELL(stream)) == -1) + io_error(); + else + break; + } while(errno == EINTR); + return offset; +} + +void factor_vm::safe_fseek(FILE *stream, off_t offset, int whence) +{ + switch(whence) + { + case 0: whence = SEEK_SET; break; + case 1: whence = SEEK_CUR; break; + case 2: whence = SEEK_END; break; + default: + critical_error("Bad value for whence",whence); + } do { - ret = fclose(stream); - } while(ret != 0 && errno == EINTR); + if(FSEEK(stream,offset,whence) == -1) + io_error(); + else + break; + } while(errno == EINTR); +} - return ret; +void factor_vm::safe_fflush(FILE *stream) +{ + do { + if(fflush(stream) == EOF) + io_error(); + else + break; + } while(errno == EINTR); +} + +void factor_vm::safe_fclose(FILE *stream) +{ + do { + if(fclose(stream) == EOF) + io_error(); + else + break; + } while(errno == EINTR); } void factor_vm::primitive_fopen() @@ -72,13 +153,8 @@ void factor_vm::primitive_fopen() path.untag_check(this); FILE *file; - do { - file = fopen((char *)(path.untagged() + 1), - (char *)(mode.untagged() + 1)); - if(file == NULL) - io_error(); - } while(errno == EINTR); - + file = safe_fopen((char *)(path.untagged() + 1), + (char *)(mode.untagged() + 1)); ctx->push(allot_alien(file)); } @@ -91,24 +167,11 @@ void factor_vm::primitive_fgetc() { FILE *file = pop_file_handle(); - do { - int c = fgetc(file); - if(c == EOF) - { - if(feof(file)) - { - ctx->push(false_object); - break; - } - else - io_error(); - } - else - { - ctx->push(tag_fixnum(c)); - break; - } - } while(errno == EINTR); + int c = safe_fgetc(file); + if(c == EOF && feof(file)) + ctx->push(false_object); + else + ctx->push(tag_fixnum(c)); } void factor_vm::primitive_fread() @@ -156,13 +219,7 @@ void factor_vm::primitive_fputc() { FILE *file = pop_file_handle(); fixnum ch = to_fixnum(ctx->pop()); - - do { - if(fputc(ch,file) == EOF) - io_error(); - else - break; - } while(errno == EINTR); + safe_fputc(ch, file); } void factor_vm::primitive_fwrite() @@ -183,16 +240,7 @@ void factor_vm::primitive_fwrite() void factor_vm::primitive_ftell() { FILE *file = pop_file_handle(); - off_t offset; - - do { - if((offset = FTELL(file)) == -1) - io_error(); - else - break; - } while(errno == EINTR); - - ctx->push(from_signed_8(offset)); + ctx->push(from_signed_8(safe_ftell(file))); } void factor_vm::primitive_fseek() @@ -200,41 +248,19 @@ void factor_vm::primitive_fseek() int whence = to_fixnum(ctx->pop()); FILE *file = pop_file_handle(); off_t offset = to_signed_8(ctx->pop()); - - switch(whence) - { - case 0: whence = SEEK_SET; break; - case 1: whence = SEEK_CUR; break; - case 2: whence = SEEK_END; break; - default: - critical_error("Bad value for whence",whence); - break; - } - - do { - if(FSEEK(file,offset,whence) == -1) - io_error(); - else - break; - } while(errno == EINTR); + safe_fseek(file,offset,whence); } void factor_vm::primitive_fflush() { FILE *file = pop_file_handle(); - do { - if(fflush(file) == EOF) - io_error(); - else - break; - } while(errno == EINTR); + safe_fflush(file); } void factor_vm::primitive_fclose() { FILE *file = pop_file_handle(); - if(safe_fclose(file) == EOF) - io_error(); + safe_fclose(file); } /* This function is used by FFI I/O. Accessing the errno global directly is diff --git a/vm/io.hpp b/vm/io.hpp index c96c95863d..80010fc993 100755 --- a/vm/io.hpp +++ b/vm/io.hpp @@ -1,10 +1,6 @@ namespace factor { -size_t safe_fread(void *ptr, size_t size, size_t nitems, FILE *stream); -size_t safe_fwrite(void *ptr, size_t size, size_t nitems, FILE *stream); -int safe_fclose(FILE *stream); - /* Platform specific primitives */ VM_C_API int err_no(); diff --git a/vm/vm.hpp b/vm/vm.hpp index 0ccf502078..85799056a6 100755 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -492,6 +492,15 @@ struct factor_vm //io void init_c_io(); void io_error(); + size_t safe_fread(void *ptr, size_t size, size_t nitems, FILE *stream); + size_t safe_fwrite(void *ptr, size_t size, size_t nitems, FILE *stream); + void safe_fclose(FILE *stream); + FILE* safe_fopen(char *filename, char *mode); + int safe_fgetc(FILE *stream); + void safe_fputc(int c, FILE* stream); + void safe_fflush(FILE *stream); + int safe_ftell(FILE *stream); + void safe_fseek(FILE *stream, off_t offset, int whence); void primitive_fopen(); FILE *pop_file_handle(); void primitive_fgetc(); From f72bcd33463719a2b06ca0aeed9572719f97c575 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Thu, 4 Feb 2010 23:55:17 -0600 Subject: [PATCH 04/11] reorder functions in header file --- vm/vm.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/vm/vm.hpp b/vm/vm.hpp index 85799056a6..5d9b3204d3 100755 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -492,15 +492,15 @@ struct factor_vm //io void init_c_io(); void io_error(); - size_t safe_fread(void *ptr, size_t size, size_t nitems, FILE *stream); - size_t safe_fwrite(void *ptr, size_t size, size_t nitems, FILE *stream); - void safe_fclose(FILE *stream); FILE* safe_fopen(char *filename, char *mode); int safe_fgetc(FILE *stream); + size_t safe_fread(void *ptr, size_t size, size_t nitems, FILE *stream); void safe_fputc(int c, FILE* stream); - void safe_fflush(FILE *stream); + size_t safe_fwrite(void *ptr, size_t size, size_t nitems, FILE *stream); int safe_ftell(FILE *stream); void safe_fseek(FILE *stream, off_t offset, int whence); + void safe_fflush(FILE *stream); + void safe_fclose(FILE *stream); void primitive_fopen(); FILE *pop_file_handle(); void primitive_fgetc(); From 7d73d8585669a8a8d7e562bbc5a577162c118782 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Fri, 5 Feb 2010 01:20:40 -0600 Subject: [PATCH 05/11] Fix help lint for user-id --- basis/unix/users/users-docs.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basis/unix/users/users-docs.factor b/basis/unix/users/users-docs.factor index c7867db2bf..e676f6fef6 100644 --- a/basis/unix/users/users-docs.factor +++ b/basis/unix/users/users-docs.factor @@ -62,7 +62,7 @@ HELP: user-name HELP: user-id { $values { "string" string } - { "id" integer } } + { "id/f" "an integer or f" } } { $description "Returns the user id associated with the user-name." } ; HELP: with-effective-user From 5fb4c30eb3f7e7fdcb71cedc738613f9acf8c5f3 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 5 Feb 2010 01:47:03 -0600 Subject: [PATCH 06/11] windows.types: another Win64 fix --- basis/windows/types/types.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basis/windows/types/types.factor b/basis/windows/types/types.factor index cf21f74fd8..9aeec7f228 100644 --- a/basis/windows/types/types.factor +++ b/basis/windows/types/types.factor @@ -40,7 +40,7 @@ TYPEDEF: intptr_t LONG_PTR TYPEDEF: intptr_t* PLONG_PTR TYPEDEF: uint ULONG -TYPEDEF: void* ULONG_PTR +TYPEDEF: uintptr_t ULONG_PTR TYPEDEF: void* PULONG_PTR TYPEDEF: void VOID From aa3d65272a7a9c1f0bd8e38e3671eab33f70f33a Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 5 Feb 2010 14:29:45 +1300 Subject: [PATCH 07/11] cpu.x86: fix crashes when using certain features (threads, errors, etc) with profiling enabled on x86-64. The profiling stub was clobbering an argument for the unwind_native_frames() entry point --- basis/cpu/x86/bootstrap.factor | 10 +++++----- basis/tools/profiler/profiler-tests.factor | 9 +++++++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/basis/cpu/x86/bootstrap.factor b/basis/cpu/x86/bootstrap.factor index 19ac0189a3..8f1a4d7f49 100644 --- a/basis/cpu/x86/bootstrap.factor +++ b/basis/cpu/x86/bootstrap.factor @@ -56,15 +56,15 @@ big-endian off [ ! Load word - temp0 0 MOV rc-absolute-cell rt-literal jit-rel + safe-reg 0 MOV rc-absolute-cell rt-literal jit-rel ! Bump profiling counter - temp0 profile-count-offset [+] 1 tag-fixnum ADD + safe-reg profile-count-offset [+] 1 tag-fixnum ADD ! Load word->code - temp0 temp0 word-code-offset [+] MOV + safe-reg safe-reg word-code-offset [+] MOV ! Compute word entry point - temp0 compiled-header-size ADD + safe-reg compiled-header-size ADD ! Jump to entry point - temp0 JMP + safe-reg JMP ] jit-profiling jit-define [ diff --git a/basis/tools/profiler/profiler-tests.factor b/basis/tools/profiler/profiler-tests.factor index a85d934506..5c31cdaeb4 100644 --- a/basis/tools/profiler/profiler-tests.factor +++ b/basis/tools/profiler/profiler-tests.factor @@ -1,6 +1,6 @@ USING: accessors tools.profiler tools.test kernel memory math threads alien alien.c-types tools.profiler.private sequences -compiler.test compiler.units words ; +compiler.test compiler.units words arrays ; IN: tools.profiler.tests [ t ] [ @@ -9,7 +9,7 @@ IN: tools.profiler.tests \ length counter>> = ] unit-test -[ ] [ [ 10 [ gc ] times ] profile ] unit-test +[ ] [ [ 3 [ gc ] times ] profile ] unit-test [ ] [ [ 1000000 sleep ] profile ] unit-test @@ -72,3 +72,8 @@ IN: tools.profiler.tests ] profile counter>> ] unit-test + +! unwind_native_frames() would fail if profiling was enabled +! because the jit-profiling stub would clobber a parameter register +! on x86-64 +[ [ -10 f ] profile ] must-fail From 59193af701741e4c1ad2d365b0fae4ec6baf41db Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 5 Feb 2010 20:26:20 +1300 Subject: [PATCH 08/11] alien.syntax: save source location for TYPEDEF:, raise error on redefinition --- basis/alien/c-types/c-types-tests.factor | 12 +++++++++++- basis/alien/syntax/syntax.factor | 4 ++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/basis/alien/c-types/c-types-tests.factor b/basis/alien/c-types/c-types-tests.factor index d134d57189..faee8955e9 100644 --- a/basis/alien/c-types/c-types-tests.factor +++ b/basis/alien/c-types/c-types-tests.factor @@ -1,6 +1,7 @@ USING: alien alien.syntax alien.c-types alien.parser eval kernel tools.test sequences system libc alien.strings -io.encodings.utf8 math.constants classes.struct classes ; +io.encodings.utf8 math.constants classes.struct classes +accessors compiler.units ; IN: alien.c-types.tests CONSTANT: xyz 123 @@ -100,3 +101,12 @@ DEFER: struct-redefined \ struct-redefined class? ] unit-test +[ + "IN: alien.c-types.tests + USE: alien.syntax + USE: alien.c-types + TYPEDEF: int type-redefinition-test + TYPEDEF: int type-redefinition-test" eval( -- ) +] +[ error>> error>> redefine-error? ] +must-fail-with diff --git a/basis/alien/syntax/syntax.factor b/basis/alien/syntax/syntax.factor index 609ed2826d..295bcff089 100644 --- a/basis/alien/syntax/syntax.factor +++ b/basis/alien/syntax/syntax.factor @@ -1,4 +1,4 @@ -! Copyright (C) 2005, 2009 Slava Pestov, Alex Chapman. +! Copyright (C) 2005, 2010 Slava Pestov, Alex Chapman. ! See http://factorcode.org/license.txt for BSD license. USING: accessors arrays alien alien.c-types alien.arrays alien.strings kernel math namespaces parser @@ -22,7 +22,7 @@ SYNTAX: CALLBACK: (CALLBACK:) define-inline ; SYNTAX: TYPEDEF: - scan-c-type CREATE-C-TYPE typedef ; + scan-c-type CREATE-C-TYPE dup save-location typedef ; SYNTAX: C-ENUM: ";" parse-tokens From b6b780fb72f30df3dc9cb20c2c827dfb1e5cc0e3 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 5 Feb 2010 06:04:12 -0600 Subject: [PATCH 09/11] windows.types: remove duplicate typedefs flagged by recent alien.syntax change --- basis/windows/types/types.factor | 7 ------- 1 file changed, 7 deletions(-) diff --git a/basis/windows/types/types.factor b/basis/windows/types/types.factor index 9aeec7f228..9e322d9cde 100644 --- a/basis/windows/types/types.factor +++ b/basis/windows/types/types.factor @@ -37,11 +37,9 @@ TYPEDEF: int HFILE TYPEDEF: long LONG TYPEDEF: long* LPLONG TYPEDEF: intptr_t LONG_PTR -TYPEDEF: intptr_t* PLONG_PTR TYPEDEF: uint ULONG TYPEDEF: uintptr_t ULONG_PTR -TYPEDEF: void* PULONG_PTR TYPEDEF: void VOID TYPEDEF: void* PVOID @@ -85,7 +83,6 @@ TYPEDEF: TCHAR TBYTE TYPEDEF: WORD ATOM TYPEDEF: BYTE BOOLEAN -TYPEDEF: DWORD COLORREF TYPEDEF: ULONGLONG DWORDLONG TYPEDEF: ULONG_PTR DWORD_PTR TYPEDEF: PVOID HANDLE @@ -132,7 +129,6 @@ TYPEDEF: DWORD LGRPID TYPEDEF: LONG_PTR LPARAM TYPEDEF: BOOL* LPBOOL TYPEDEF: BYTE* LPBYTE -TYPEDEF: DWORD* LPCOLORREF TYPEDEF: WCHAR* LPCWSTR ! TYPEDEF: WCHAR* LPWSTR @@ -196,8 +192,6 @@ TYPEDEF: WCHAR* PWSTR TYPEDEF: HANDLE SC_HANDLE TYPEDEF: LPVOID SC_LOCK TYPEDEF: HANDLE SERVICE_STATUS_HANDLE -TYPEDEF: ULONG_PTR SIZE_T -TYPEDEF: LONG_PTR SSIZE_T TYPEDEF: LONGLONG USN TYPEDEF: UINT_PTR WPARAM @@ -332,7 +326,6 @@ TYPEDEF: RECT* LPRECT TYPEDEF: PIXELFORMATDESCRIPTOR PFD TYPEDEF: PFD* LPPFD TYPEDEF: HANDLE HGLRC -TYPEDEF: HANDLE HRGN TYPEDEF: void* PWNDCLASS TYPEDEF: void* PWNDCLASSEX From 4946beef3c13414763111bd42658fb7c51ec2dca Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Fri, 5 Feb 2010 14:09:17 -0600 Subject: [PATCH 10/11] Fix typo in dinput binding --- basis/windows/directx/dinput/dinput.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basis/windows/directx/dinput/dinput.factor b/basis/windows/directx/dinput/dinput.factor index 2a7ae19ecc..56cd4937ce 100644 --- a/basis/windows/directx/dinput/dinput.factor +++ b/basis/windows/directx/dinput/dinput.factor @@ -72,7 +72,7 @@ STRUCT: DICONFIGUREDEVICESPARAMSW { dics DICOLORSET } { lpUnkDDSTarget IUnknown* } ; TYPEDEF: DICONFIGUREDEVICESPARAMSW* LPDICONFIGUREDEVICESPARAMSW -TYPEDEF: DICONFIGUREDEVICESPARAMSW* LPDICONFIGUREDEVICESPARAMSW +TYPEDEF: DICONFIGUREDEVICESPARAMSW* LPCDICONFIGUREDEVICESPARAMSW STRUCT: DIDEVCAPS { dwSize DWORD } From 342af1a8dd59815bfd1f0b6ccdb08428d2ad439c Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Fri, 5 Feb 2010 14:10:53 -0600 Subject: [PATCH 11/11] gitignore .RES --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index ef3acdb280..3bc5a6ffda 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ Factor/factor *.lib *.exp *.res +*.RES *.image *.dylib factor