system.pp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2020,2021 by the Free Pascal development team.
  4. System unit for The WebAssembly System Interface (WASI).
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit system;
  12. interface
  13. {$IFNDEF FPC_DISABLE_MONITOR}
  14. {$DEFINE SYSTEM_HAS_FEATURE_MONITOR}
  15. {$ENDIF}
  16. {$define FPC_IS_SYSTEM}
  17. {$if defined(WASIp1threads) and not defined(FPC_WASM_THREADS)}
  18. {$fatal The wasip1threads target requires that WebAssembly threads are turned on! Maybe you want to use the wasip1 target, instead?}
  19. {$endif}
  20. {$if defined(WASIp1) and not defined(WASIp1threads) and defined(FPC_WASM_THREADS)}
  21. {$fatal The wasip1 target requires that WebAssembly threads are turned off! Maybe you want to use the wasip1threads target, instead?}
  22. {$endif}
  23. {$ifdef FPC_WASM_THREADS}
  24. {$define DISABLE_NO_THREAD_MANAGER}
  25. {$else FPC_WASM_THREADS}
  26. {$define USE_NOTHREADMANAGER}
  27. {$endif FPC_WASM_THREADS}
  28. {$I systemh.inc}
  29. const
  30. LineEnding = #10;
  31. LFNSupport = true;
  32. DirectorySeparator = '/';
  33. DriveSeparator = '';
  34. ExtensionSeparator = '.';
  35. PathSeparator = ':';
  36. AllowDirectorySeparators : set of AnsiChar = ['\','/'];
  37. AllowDriveSeparators : set of AnsiChar = [];
  38. { FileNameCaseSensitive and FileNameCasePreserving are defined below! }
  39. maxExitCode = 65535;
  40. MaxPathLen = 4096;
  41. AllFilesMask = '*';
  42. const
  43. UnusedHandle = -1;
  44. StdInputHandle = 0;
  45. StdOutputHandle = 1;
  46. StdErrorHandle = 2;
  47. FileNameCaseSensitive : boolean = true;
  48. FileNameCasePreserving: boolean = true;
  49. CtrlZMarksEOF: boolean = false; (* #26 not considered as end of file *)
  50. sLineBreak = LineEnding;
  51. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsLF;
  52. var
  53. argc: longint;
  54. argv: PPAnsiChar;
  55. envp: PPAnsiChar;
  56. ___fpc_wasm_suspender: WasmExternRef; section 'WebAssembly.Global';
  57. function __fpc_get_wasm_suspender: WasmExternRef;
  58. procedure __fpc_set_wasm_suspender(v: WasmExternRef);
  59. property __fpc_wasm_suspender: WasmExternRef read __fpc_get_wasm_suspender write __fpc_set_wasm_suspender;
  60. Procedure DebugWriteln(aString : ShortString);
  61. implementation
  62. var
  63. StkLen: SizeUInt; external name '__stklen';
  64. {$I wasitypes.inc}
  65. {$I wasiprocs.inc}
  66. function IntToStr(I : Longint) : ShortString;
  67. Var
  68. S : ShortString;
  69. begin
  70. Str(I,S);
  71. IntToStr:=S;
  72. end;
  73. Procedure DebugWriteln(aString : ShortString);
  74. var
  75. our_iov: __wasi_ciovec_t;
  76. our_nwritten: longint;
  77. res: __wasi_errno_t;
  78. begin
  79. our_iov.buf := @aString[1];
  80. our_iov.buf_len := Length(aString);
  81. repeat
  82. res:=__wasi_fd_write(1, @our_iov, 1, @our_nwritten);
  83. if res=__WASI_ERRNO_SUCCESS then
  84. begin
  85. our_iov.buf_len:=our_iov.buf_len-our_nwritten;
  86. our_iov.buf:=our_iov.buf+our_nwritten;
  87. end;
  88. Until (our_iov.buf_len=0) or (res=__WASI_ERRNO_SUCCESS) or ((res<>__WASI_ERRNO_INTR) and (res<>__WASI_ERRNO_AGAIN));
  89. end;
  90. function WasiAlloc (aLength : Longint) : Pointer; [public, alias: 'wasiAlloc'];
  91. begin
  92. Result:=GetMem(aLength);
  93. end;
  94. procedure WasiFree (aMem : pointer); [public, alias: 'wasiFree'];
  95. begin
  96. FreeMem(aMem);
  97. end;
  98. exports
  99. WasiAlloc,WasiFree;
  100. function __fpc_get_wasm_suspender: WasmExternRef;
  101. begin
  102. result:=___fpc_wasm_suspender;
  103. end;
  104. procedure __fpc_set_wasm_suspender(v: WasmExternRef);
  105. begin
  106. ___fpc_wasm_suspender:=v;
  107. end;
  108. function ConvertToFdRelativePath(path: RawByteString; out fd: LongInt; out relfd_path: RawByteString): Word; forward;
  109. function fpc_wasi_path_readlink_ansistring(
  110. fd: __wasi_fd_t;
  111. const path: PAnsiChar;
  112. path_len: size_t;
  113. out link: rawbytestring): __wasi_errno_t;[Public, Alias : 'FPC_WASI_PATH_READLINK_ANSISTRING'];
  114. const
  115. InitialBufLen=64;
  116. MaximumBufLen=16384;
  117. var
  118. CurrentBufLen: __wasi_size_t;
  119. BufUsed: __wasi_size_t;
  120. begin
  121. CurrentBufLen:=InitialBufLen div 2;
  122. repeat
  123. CurrentBufLen:=CurrentBufLen*2;
  124. SetLength(link,CurrentBufLen);
  125. result:=__wasi_path_readlink(fd,path,path_len,@(link[1]),CurrentBufLen,@BufUsed);
  126. until (result<>__WASI_ERRNO_SUCCESS) or ((result=__WASI_ERRNO_SUCCESS) and (BufUsed<CurrentBufLen)) or (CurrentBufLen>MaximumBufLen);
  127. if result=__WASI_ERRNO_SUCCESS then
  128. begin
  129. SetLength(link,BufUsed);
  130. setcodepage(link,DefaultRTLFileSystemCodePage,true);
  131. end
  132. else
  133. link:='';
  134. end;
  135. function HasDriveLetter(const path: rawbytestring): Boolean;
  136. begin
  137. HasDriveLetter:=(Length(path)>=2) and (UpCase(path[1]) in ['A'..'Z']) and (path[2] = ':');
  138. end;
  139. type
  140. PPreopenedDir = ^TPreopenedDir;
  141. TPreopenedDir = record
  142. dir_name: RawByteString;
  143. drive_str: RawByteString;
  144. fd: longint;
  145. end;
  146. PCurrentDir = ^TCurrentDir;
  147. TCurrentDir = record
  148. dir_name: RawByteString;
  149. drive_str: RawByteString;
  150. end;
  151. var
  152. preopened_dirs_count: longint;
  153. preopened_dirs: PPreopenedDir;
  154. drives_count: longint;
  155. current_dirs: PCurrentDir;
  156. current_drive: longint;
  157. {$I system.inc}
  158. var
  159. argv_size,
  160. argv_buf_size: __wasi_size_t;
  161. argv_buf: Pointer;
  162. environc,environ_buf_size,envp_size: __wasi_size_t;
  163. environ_buf: Pointer;
  164. function GetProcessID: SizeUInt;
  165. begin
  166. end;
  167. Procedure Randomize;
  168. Begin
  169. __wasi_random_get(@RandSeed,SizeOf(RandSeed));
  170. End;
  171. procedure System_exit;
  172. begin
  173. if ExitCode>=126 then
  174. begin
  175. Debugwriteln('##WASI-EXITCODE: '+IntToStr(ExitCode)+' -> 125##');
  176. ExitCode:=125;
  177. end;
  178. __wasi_proc_exit(ExitCode);
  179. End;
  180. function StripLeadingDirSep(const path: RawByteString): RawByteString;
  181. var
  182. chridx: longint;
  183. begin
  184. if path='' then
  185. begin
  186. Result:='';
  187. exit;
  188. end;
  189. chridx:=1;
  190. while (chridx<=Length(path)) and (path[chridx] in AllowDirectorySeparators) do
  191. Inc(chridx);
  192. Result:=Copy(path,chridx);
  193. end;
  194. function EscapesToParent(const relpath: RawByteString): Boolean;
  195. var
  196. balance: longint;
  197. item: RawByteString;
  198. procedure FinishItem;
  199. begin
  200. case item of
  201. '', '.':
  202. {nothing};
  203. '..':
  204. Dec(balance);
  205. else
  206. Inc(balance);
  207. end;
  208. end;
  209. var
  210. i: longint;
  211. begin
  212. balance:=0;
  213. item:='';
  214. i:=1;
  215. while i<=Length(relpath) do
  216. begin
  217. if relpath[i] in AllowDirectorySeparators then
  218. begin
  219. FinishItem;
  220. if balance<0 then
  221. begin
  222. Result:=True;
  223. exit;
  224. end;
  225. item:='';
  226. end
  227. else
  228. item:=item+relpath[i];
  229. Inc(i);
  230. end;
  231. FinishItem;
  232. Result:=balance<0;
  233. end;
  234. function Do_ConvertToFdRelativePath(path: RawByteString; out fd: LongInt; out relfd_path: RawByteString): Word;
  235. var
  236. drive_nr,I,pdir_drive,longest_match,chridx: longint;
  237. IsAbsolutePath: Boolean;
  238. pdir: RawByteString;
  239. begin
  240. fd:=0;
  241. relfd_path:='';
  242. if HasDriveLetter(path) then
  243. begin
  244. drive_nr:=Ord(UpCase(path[1]))-(Ord('A')-1);
  245. delete(path,1,2);
  246. end
  247. else
  248. drive_nr:=current_drive;
  249. { path is relative to a current directory? }
  250. if (path='') or not (path[1] in AllowDirectorySeparators) then
  251. begin
  252. { if so, convert to absolute }
  253. if (drive_nr>=drives_count) or (current_dirs[drive_nr].dir_name='') then
  254. begin
  255. Do_ConvertToFdRelativePath:=15;
  256. exit;
  257. end;
  258. if current_dirs[drive_nr].dir_name[Length(current_dirs[drive_nr].dir_name)] in AllowDirectorySeparators then
  259. path:=current_dirs[drive_nr].dir_name+path
  260. else
  261. path:=current_dirs[drive_nr].dir_name+DirectorySeparator+path;
  262. end;
  263. { path is now absolute. Try to find it in the preopened dirs array }
  264. longest_match:=0;
  265. for I:=0 to preopened_dirs_count-1 do
  266. begin
  267. pdir:=preopened_dirs[I].dir_name;
  268. if preopened_dirs[I].drive_str<>'' then
  269. pdir_drive:=Ord(UpCase(preopened_dirs[I].drive_str[1]))-(Ord('A')-1)
  270. else
  271. pdir_drive:=0;
  272. if pdir_drive<>drive_nr then
  273. continue;
  274. if Length(pdir)>Length(path) then
  275. continue;
  276. if Copy(path,1,Length(pdir))<>pdir then
  277. continue;
  278. chridx:=Length(pdir)+1;
  279. if ((Length(pdir)<>1) or ((Length(pdir)=1) and not (pdir[1] in AllowDirectorySeparators))) and
  280. ((chridx>Length(path)) or not (path[chridx] in AllowDirectorySeparators)) then
  281. continue;
  282. if EscapesToParent(StripLeadingDirSep(Copy(path,chridx))) then
  283. continue;
  284. if Length(pdir)>longest_match then
  285. begin
  286. longest_match:=Length(pdir);
  287. while (chridx<=Length(path)) and (path[chridx] in AllowDirectorySeparators) do
  288. Inc(chridx);
  289. fd:=preopened_dirs[I].fd;
  290. relfd_path:=Copy(path,chridx,Length(path)-chridx+1);
  291. end;
  292. end;
  293. if longest_match>0 then
  294. Do_ConvertToFdRelativePath:=0
  295. else
  296. Do_ConvertToFdRelativePath:=3;
  297. end;
  298. function ConvertToFdRelativePath(path: RawByteString; out fd: LongInt; out relfd_path: RawByteString): Word;[Public, Alias : 'FPC_WASI_CONVERTTOFDRELATIVEPATH'];
  299. begin
  300. ConvertToFdRelativePath:=Do_ConvertToFdRelativePath(ToSingleByteFileSystemEncodedFileName(path),fd,relfd_path);
  301. setcodepage(relfd_path,DefaultRTLFileSystemCodePage,true);
  302. end;
  303. procedure Setup_PreopenedDirs;
  304. var
  305. fd: __wasi_fd_t;
  306. prestat: __wasi_prestat_t;
  307. res: __wasi_errno_t;
  308. prestat_dir_name: RawByteString;
  309. drive_nr: longint;
  310. begin
  311. preopened_dirs_count:=0;
  312. preopened_dirs:=nil;
  313. drives_count:=0;
  314. current_dirs:=nil;
  315. current_drive:=0;
  316. fd:=3;
  317. repeat
  318. res:=__wasi_fd_prestat_get(fd, @prestat);
  319. if res=__WASI_ERRNO_SUCCESS then
  320. begin
  321. if (prestat.tag=__WASI_PREOPENTYPE_DIR) and (prestat.u.dir.pr_name_len>0) then
  322. begin
  323. SetLength(prestat_dir_name,prestat.u.dir.pr_name_len);
  324. if __wasi_fd_prestat_dir_name(fd,PByte(prestat_dir_name),prestat.u.dir.pr_name_len)=__WASI_ERRNO_SUCCESS then
  325. begin
  326. setcodepage(prestat_dir_name,DefaultRTLFileSystemCodePage,true);
  327. Inc(preopened_dirs_count);
  328. if preopened_dirs=nil then
  329. preopened_dirs:=AllocMem(preopened_dirs_count*SizeOf(TPreopenedDir))
  330. else
  331. ReAllocMem(preopened_dirs, preopened_dirs_count*SizeOf(TPreopenedDir));
  332. preopened_dirs[preopened_dirs_count-1].fd:=fd;
  333. if HasDriveLetter(prestat_dir_name) then
  334. begin
  335. drive_nr:=Ord(UpCase(prestat_dir_name[1]))-(Ord('A')-1);
  336. preopened_dirs[preopened_dirs_count-1].drive_str:=Copy(prestat_dir_name,1,2);
  337. preopened_dirs[preopened_dirs_count-1].dir_name:=Copy(prestat_dir_name,2,Length(prestat_dir_name)-2);
  338. end
  339. else
  340. begin
  341. drive_nr:=0;
  342. preopened_dirs[preopened_dirs_count-1].drive_str:='';
  343. preopened_dirs[preopened_dirs_count-1].dir_name:=prestat_dir_name;
  344. end;
  345. if (drive_nr+1)>drives_count then
  346. begin
  347. drives_count:=drive_nr+1;
  348. if current_dirs=nil then
  349. current_dirs:=AllocMem(drives_count*SizeOf(TCurrentDir))
  350. else
  351. ReAllocMem(current_dirs,drives_count*SizeOf(TCurrentDir));
  352. end;
  353. if current_dirs[drive_nr].dir_name='' then
  354. current_dirs[drive_nr].dir_name:=prestat_dir_name;
  355. end;
  356. end;
  357. end;
  358. Inc(fd);
  359. until res<>__WASI_ERRNO_SUCCESS;
  360. while (current_drive<(drives_count-1)) and (current_dirs[current_drive].dir_name='') do
  361. Inc(current_drive);
  362. for drive_nr:=0 to drives_count-1 do
  363. begin
  364. if drive_nr>0 then
  365. current_dirs[drive_nr].drive_str:=Chr(Ord('A')+drive_nr-1)+':';
  366. if current_dirs[drive_nr].dir_name='' then
  367. current_dirs[drive_nr].dir_name:=DirectorySeparator;
  368. end;
  369. end;
  370. procedure Setup_Environment;
  371. begin
  372. if envp<>nil then
  373. exit;
  374. if __wasi_environ_sizes_get(@environc, @environ_buf_size)<>__WASI_ERRNO_SUCCESS then
  375. begin
  376. envp:=nil;
  377. exit;
  378. end;
  379. envp_size:=(environc+1)*SizeOf(PAnsiChar);
  380. GetMem(envp, envp_size);
  381. GetMem(environ_buf, environ_buf_size);
  382. envp[environc]:=nil;
  383. if __wasi_environ_get(Pointer(envp), environ_buf)<>__WASI_ERRNO_SUCCESS then
  384. begin
  385. FreeMem(envp, envp_size);
  386. FreeMem(environ_buf, environ_buf_size);
  387. envp:=nil;
  388. end;
  389. end;
  390. procedure setup_arguments;
  391. begin
  392. if argv<>nil then
  393. exit;
  394. if __wasi_args_sizes_get(@argc, @argv_buf_size)<>__WASI_ERRNO_SUCCESS then
  395. begin
  396. argc:=0;
  397. argv:=nil;
  398. exit;
  399. end;
  400. argv_size:=(argc+1)*SizeOf(PAnsiChar);
  401. GetMem(argv, argv_size);
  402. GetMem(argv_buf, argv_buf_size);
  403. if __wasi_args_get(Pointer(argv), argv_buf)<>__WASI_ERRNO_SUCCESS then
  404. begin
  405. FreeMem(argv, argv_size);
  406. FreeMem(argv_buf, argv_buf_size);
  407. argc:=0;
  408. argv:=nil;
  409. end;
  410. end;
  411. Function ParamCount: Longint;
  412. Begin
  413. if argv=nil then
  414. setup_arguments;
  415. paramcount := argc - 1;
  416. End;
  417. function paramstr(l: longint) : shortstring;
  418. begin
  419. if argv=nil then
  420. setup_arguments;
  421. if (l>=0) and (l+1<=argc) then
  422. paramstr:=strpas(argv[l])
  423. else
  424. paramstr:='';
  425. end;
  426. procedure SysInitStdIO;
  427. begin
  428. OpenStdIO(Input,fmInput,StdInputHandle);
  429. OpenStdIO(Output,fmOutput,StdOutputHandle);
  430. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  431. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  432. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  433. end;
  434. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  435. begin
  436. result := stklen;
  437. end;
  438. begin
  439. StackLength:=CheckInitialStkLen(stklen);
  440. StackBottom:=Pointer(PtrUInt(InitialHeapBlockStart)-PtrUInt(StackLength));
  441. { To be set if this is a GUI or console application }
  442. IsConsole := TRUE;
  443. {$ifdef FPC_HAS_FEATURE_DYNLIBS}
  444. { If dynlibs feature is disabled,
  445. IsLibrary is a constant, which can thus not be set to a value }
  446. { To be set if this is a library and not a program }
  447. IsLibrary := FALSE;
  448. {$endif def FPC_HAS_FEATURE_DYNLIBS}
  449. { Setup heap }
  450. InitInitialHeapBlock;
  451. InitHeap;
  452. SysInitExceptions;
  453. initunicodestringmanager;
  454. { Reset IO Error }
  455. InOutRes:=0;
  456. {$ifdef FPC_HAS_FEATURE_THREADING}
  457. InitSystemThreads;
  458. {$ifdef FPC_WASM_THREADS}
  459. InitThreadVars(@WasiRelocateThreadVar);
  460. {$endif}
  461. {$endif}
  462. { Setup stdin, stdout and stderr }
  463. SysInitStdIO;
  464. Setup_Environment;
  465. Setup_PreopenedDirs;
  466. {$ifdef FPC_WASM_THREADS}
  467. TLSInfoBlock:=Nil;
  468. {$endif}
  469. end.