Skip to content

Commit

Permalink
os: Properly use VirtualFree, oops
Browse files Browse the repository at this point in the history
  • Loading branch information
dd86k committed Jun 10, 2024
1 parent 5c0dc73 commit b028846
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 24 deletions.
13 changes: 7 additions & 6 deletions src/adbg/error.d
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import adbg.include.capstone : csh, cs_errno, cs_strerror;
// - automatically set error code
// adbg_oopsn(AdbgError)
// - returns null
//TODO: Localize error messages as option (including system ones, when able)

extern (C):

Expand All @@ -47,15 +48,15 @@ enum AdbgError {
offsetBounds = 7, /// File offset is outside of file size
indexBounds = 8, /// Index is outside of bounds of list
unavailable = 9, /// Feature or item is unavailable
unfindable = 10, /// Item cannot be found in list
partialRead = 11, /// Not all data could be read.
partialWrite = 12, /// Not all data could be written.
unfindable = 10, /// Item not found
partialRead = 11, /// Not all data could be read
partialWrite = 12, /// Not all data could be written
//
// 100-199: Debugger
//
debuggerUnattached = 100,
debuggerUnpaused = 101,
debuggerInvalidAction = 102, /// Wrong action from creation method.
debuggerInvalidAction = 102, /// Wrong action from creation method
debuggerPresent = 103, /// Debugger already present in remote process
//
// 200-299: Disasembler
Expand Down Expand Up @@ -129,8 +130,8 @@ private immutable adbg_error_msg_t[] errors_msg = [
{ AdbgError.uninitiated, "Object or structure requires to be initialized first." },
{ AdbgError.invalidOption, "Option unknown." },
{ AdbgError.invalidValue, "Option received invalid value." },
{ AdbgError.offsetBounds, "File offset outside file size." },
{ AdbgError.indexBounds, "Index outside of list." },
{ AdbgError.offsetBounds, "Offset outside range." },
{ AdbgError.indexBounds, "Index outside range." },
{ AdbgError.unavailable, "Feature or item is unavailable." },
{ AdbgError.unfindable, "Item was not found." },
//
Expand Down
24 changes: 6 additions & 18 deletions src/adbg/os/file.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,11 @@
/// License: BSD-3-Clause-Clear
module adbg.os.file;

//TODO: wchar_t support

version (Windows) {
import core.sys.windows.winnt :
LPVOID,
MEM_COMMIT, MEM_RELEASE, MEM_RESERVE,
PAGE_EXECUTE, PAGE_EXECUTE_READ, PAGE_READWRITE,
PAGE_EXECUTE_READWRITE, PAGE_EXECUTE_WRITECOPY,
DWORD, HANDLE, LARGE_INTEGER, FALSE,
GENERIC_ALL, GENERIC_READ, GENERIC_WRITE,
FILE_SHARE_READ;
import core.sys.windows.winbase :
GetLastError,
CreateFileA, CreateFileW,
SetFilePointerEx, GetFileSizeEx,
ReadFile, ReadFileEx, WriteFile, FlushFileBuffers, CloseHandle,
OPEN_ALWAYS, OPEN_EXISTING, INVALID_HANDLE_VALUE,
FILE_BEGIN, FILE_CURRENT, FILE_END,
VirtualAlloc, VirtualFree;
import core.sys.windows.winnt;
import core.sys.windows.winbase;

private alias OSHANDLE = HANDLE;
private alias SEEK_SET = FILE_BEGIN;
Expand Down Expand Up @@ -119,7 +107,7 @@ version (Windows) {
);
if (file.handle == INVALID_HANDLE_VALUE) {
version (Trace) trace("CreateFileA=%#x", GetLastError());
VirtualFree(cast(void*)file, OSFILE.sizeof, MEM_RELEASE);
VirtualFree(cast(void*)file, 0, MEM_RELEASE);
return null;
}
} else version (Posix) {
Expand Down Expand Up @@ -244,7 +232,7 @@ version (Windows) {
void osfclose(OSFILE* file) {
version (Windows) {
CloseHandle(file.handle);
VirtualFree(cast(void*)file, OSFILE.sizeof, MEM_RELEASE);
VirtualFree(cast(void*)file, 0, MEM_RELEASE);
} else version (Posix) {
.close(file.handle);
free(file);
Expand Down

0 comments on commit b028846

Please sign in to comment.