ReactOS  0.4.12-dev-685-gf36cbf7
atom.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Kernel
3  * COPYRIGHT: GPL - See COPYING in the top level directory
4  * FILE: ntoskrnl/ex/atom.c
5  * PURPOSE: Executive Atom Functions
6  * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
7  * Gunnar Dalsnes
8  */
9 
10 /* INCLUDES *****************************************************************/
11 
12 #include <ntoskrnl.h>
13 #define NDEBUG
14 #include <debug.h>
15 
16 #define TAG_ATOM 'motA'
17 
18 /* GLOBALS ****************************************************************/
19 
20 /*
21  * FIXME: this is WRONG! The global atom table should live in the WinSta struct
22  * and accessed through a win32k callout (received in PsEstablishWin32Callouts)
23  * NOTE: There is a session/win32k global atom table also, but its private to
24  * win32k. Its used for RegisterWindowMessage() and for window classes.
25  * -Gunnar
26  */
28 
29 /* PRIVATE FUNCTIONS *********************************************************/
30 
31 /*++
32  * @name ExpGetGlobalAtomTable
33  *
34  * Gets pointer to a global atom table, creates it if not already created
35  *
36  * @return Pointer to the RTL_ATOM_TABLE, or NULL if it's impossible
37  * to create atom table
38  *
39  * @remarks Internal function
40  *
41  *--*/
43 NTAPI
45 {
47 
48  /* Return it if we have one */
49  if (GlobalAtomTable) return GlobalAtomTable;
50 
51  /* Create it */
53 
54  /* If we couldn't create it, return NULL */
55  if (!NT_SUCCESS(Status)) return NULL;
56 
57  /* Return the newly created one */
58  return GlobalAtomTable;
59 }
60 
61 /* FUNCTIONS ****************************************************************/
62 
63 /*++
64  * @name NtAddAtom
65  * @implemented
66  *
67  * Function NtAddAtom creates new Atom in Global Atom Table. If Atom
68  * with the same name already exist, internal Atom counter is incremented.
69  * See: http://undocumented.ntinternals.net/UserMode/Undocumented%20Functions/Atoms/NtAddAtom.html
70  *
71  * @param AtomName
72  * Atom name in Unicode
73  *
74  * @param AtomNameLength
75  * Length of the atom name
76  *
77  * @param Atom
78  * Pointer to RTL_ATOM
79  *
80  * @return STATUS_SUCCESS in case of success, proper error code
81  * otherwise.
82  *
83  * @remarks None
84  *
85  *--*/
87 NTAPI
88 NtAddAtom(IN PWSTR AtomName,
89  IN ULONG AtomNameLength,
91 {
95  LPWSTR CapturedName;
96  ULONG CapturedSize;
97  RTL_ATOM SafeAtom;
98  PAGED_CODE();
99 
100  /* Check for the table */
101  if (AtomTable == NULL) return STATUS_ACCESS_DENIED;
102 
103  /* Check for valid name */
104  if (AtomNameLength > (RTL_MAXIMUM_ATOM_LENGTH * sizeof(WCHAR)))
105  {
106  /* Fail */
107  DPRINT1("Atom name too long\n");
109  }
110 
111  /* Check if we're called from user-mode or kernel-mode */
112  if (PreviousMode == KernelMode)
113  {
114  /* Re-use the given name if kernel mode */
115  CapturedName = AtomName;
116  }
117  else
118  {
119  /* Check if we have a name */
120  if (AtomName)
121  {
122  /* Allocate an aligned buffer + the null char */
123  CapturedSize = ((AtomNameLength + sizeof(WCHAR)) &
124  ~(sizeof(WCHAR) -1));
125  CapturedName = ExAllocatePoolWithTag(PagedPool,
126  CapturedSize,
127  TAG_ATOM);
128 
129  if (!CapturedName)
130  {
131  /* Fail the call */
133  }
134 
135  /* Enter SEH */
136  _SEH2_TRY
137  {
138  /* Probe the atom */
139  ProbeForRead(AtomName, AtomNameLength, sizeof(WCHAR));
140 
141  /* Copy the name and null-terminate it */
142  RtlCopyMemory(CapturedName, AtomName, AtomNameLength);
143  CapturedName[AtomNameLength / sizeof(WCHAR)] = UNICODE_NULL;
144 
145  /* Probe the atom too */
147  }
149  {
150  /* Return the exception code */
152  }
153  _SEH2_END;
154  }
155  else
156  {
157  /* No name */
158  CapturedName = NULL;
159  }
160  }
161 
162  /* Call the runtime function */
163  Status = RtlAddAtomToAtomTable(AtomTable, CapturedName, &SafeAtom);
164  if (NT_SUCCESS(Status) && (Atom))
165  {
166  /* Success and caller wants the atom back.. .enter SEH */
167  _SEH2_TRY
168  {
169  /* Return the atom */
170  *Atom = SafeAtom;
171  }
173  {
174  /* Get the exception code */
176  }
177  _SEH2_END;
178  }
179 
180  /* If we captured anything, free it */
181  if ((CapturedName != NULL) && (CapturedName != AtomName))
182  ExFreePoolWithTag(CapturedName, TAG_ATOM);
183 
184  /* Return to caller */
185  return Status;
186 }
187 
188 /*++
189  * @name NtDeleteAtom
190  * @implemented
191  *
192  * Removes Atom from Global Atom Table. If Atom's reference counter
193  * is greater then 1, function decrements this counter, but Atom
194  * stayed in Global Atom Table.
195  * See: http://undocumented.ntinternals.net/UserMode/Undocumented%20Functions/Atoms/NtDeleteAtom.html
196  *
197  * @param Atom
198  * Atom identifier
199  *
200  * @return STATUS_SUCCESS in case of success, proper error code
201  * otherwise.
202  *
203  * @remarks None
204  *
205  *--*/
206 NTSTATUS
207 NTAPI
209 {
211  PAGED_CODE();
212 
213  /* Check for valid table */
214  if (AtomTable == NULL) return STATUS_ACCESS_DENIED;
215 
216  /* Call worker function */
217  return RtlDeleteAtomFromAtomTable(AtomTable, Atom);
218 }
219 
220 /*++
221  * @name NtFindAtom
222  * @implemented
223  *
224  * Retrieves existing Atom's identifier without incrementing Atom's
225  * internal counter
226  * See: http://undocumented.ntinternals.net/UserMode/Undocumented%20Functions/Atoms/NtFindAtom.html
227  *
228  * @param AtomName
229  * Atom name in Unicode
230  *
231  * @param AtomNameLength
232  * Length of the atom name
233  *
234  * @param Atom
235  * Pointer to RTL_ATOM
236  *
237  * @return STATUS_SUCCESS in case of success, proper error code
238  * otherwise.
239  *
240  * @remarks None
241  *
242  *--*/
243 NTSTATUS
244 NTAPI
245 NtFindAtom(IN PWSTR AtomName,
246  IN ULONG AtomNameLength,
248 {
252  LPWSTR CapturedName = NULL;
253  ULONG CapturedSize;
254  RTL_ATOM SafeAtom;
255  PAGED_CODE();
256 
257  /* Check for the table */
258  if (AtomTable == NULL) return STATUS_ACCESS_DENIED;
259 
260  /* Check for valid name */
261  if (AtomNameLength > (RTL_MAXIMUM_ATOM_LENGTH * sizeof(WCHAR)))
262  {
263  /* Fail */
264  DPRINT1("Atom name too long\n");
266  }
267 
268  /* Re-use the given name if kernel mode or no atom name */
269  CapturedName = AtomName;
270 
271  /* Check if we're called from user-mode*/
272  if (PreviousMode != KernelMode)
273  {
274  /* Enter SEH */
275  _SEH2_TRY
276  {
277  /* Check if we have a name */
278  if (AtomName)
279  {
280  /* Probe the atom */
281  ProbeForRead(AtomName, AtomNameLength, sizeof(WCHAR));
282 
283  /* Allocate an aligned buffer + the null char */
284  CapturedSize = ((AtomNameLength + sizeof(WCHAR)) &~
285  (sizeof(WCHAR) -1));
286  CapturedName = ExAllocatePoolWithTag(PagedPool,
287  CapturedSize,
288  TAG_ATOM);
289  if (!CapturedName)
290  {
291  /* Fail the call */
293  }
294  else
295  {
296  /* Copy the name and null-terminate it */
297  RtlCopyMemory(CapturedName, AtomName, AtomNameLength);
298  CapturedName[AtomNameLength / sizeof(WCHAR)] = UNICODE_NULL;
299  }
300 
301  /* Probe the atom too */
303  }
304  }
306  {
307  /* Return the exception code */
309  }
310  _SEH2_END;
311  }
312 
313  /* Call the runtime function */
314  Status = RtlLookupAtomInAtomTable(AtomTable, CapturedName, &SafeAtom);
315  if (NT_SUCCESS(Status) && (Atom))
316  {
317  /* Success and caller wants the atom back.. .enter SEH */
318  _SEH2_TRY
319  {
320  /* Return the atom */
321  *Atom = SafeAtom;
322  }
324  {
326  }
327  _SEH2_END;
328  }
329 
330  /* If we captured anything, free it */
331  if ((CapturedName) && (CapturedName != AtomName))
332  ExFreePoolWithTag(CapturedName, TAG_ATOM);
333 
334  /* Return to caller */
335  return Status;
336 }
337 
338 /*++
339  * @name NtQueryInformationAtom
340  * @implemented
341  *
342  * Gets single Atom properties or reads Global Atom Table
343  * See: http://undocumented.ntinternals.net/UserMode/Undocumented%20Functions/Atoms/NtQueryInformationAtom.html
344  *
345  * @param Atom
346  * Atom to query. If AtomInformationClass parameter is
347  * AtomTableInformation, Atom parameter is not used.
348  *
349  * @param AtomInformationClass
350  * See ATOM_INFORMATION_CLASS enumeration type for details
351  *
352  * @param AtomInformation
353  * Result of call - pointer to user's allocated buffer for data
354  *
355  * @param AtomInformationLength
356  * Size of AtomInformation buffer, in bytes
357  *
358  * @param ReturnLength
359  * Pointer to ULONG value containing required AtomInformation
360  * buffer size
361  *
362  * @return STATUS_SUCCESS in case of success, proper error code
363  * otherwise.
364  *
365  * @remarks None
366  *
367  *--*/
368 NTSTATUS
369 NTAPI
371  ATOM_INFORMATION_CLASS AtomInformationClass,
372  PVOID AtomInformation,
373  ULONG AtomInformationLength,
375 {
377  PATOM_BASIC_INFORMATION BasicInformation = AtomInformation;
378  PATOM_TABLE_INFORMATION TableInformation = AtomInformation;
380  ULONG Flags, UsageCount, NameLength, RequiredLength = 0;
382 
383  PAGED_CODE();
384 
385  /* Check for valid table */
386  if (AtomTable == NULL) return STATUS_ACCESS_DENIED;
387 
389 
390  _SEH2_TRY
391  {
392  /* Probe the parameters */
393  if (PreviousMode != KernelMode)
394  {
395  ProbeForWrite(AtomInformation,
396  AtomInformationLength,
397  sizeof(ULONG));
398 
399  if (ReturnLength != NULL)
400  {
402  }
403  }
404 
405  /* Choose class */
406  switch (AtomInformationClass)
407  {
408  /* Caller requested info about an atom */
410 
411  /* Size check */
413  if (RequiredLength > AtomInformationLength)
414  {
415  /* Fail */
416  DPRINT1("Buffer too small\n");
418  _SEH2_LEAVE;
419  }
420 
421  /* Prepare query */
422  UsageCount = 0;
423  NameLength = AtomInformationLength - RequiredLength;
424  BasicInformation->Name[0] = UNICODE_NULL;
425 
426  /* Query the data */
427  Status = RtlQueryAtomInAtomTable(AtomTable,
428  Atom,
429  &UsageCount,
430  &Flags,
431  BasicInformation->Name,
432  &NameLength);
433  if (NT_SUCCESS(Status))
434  {
435  /* Return data */
436  BasicInformation->UsageCount = (USHORT)UsageCount;
437  BasicInformation->Flags = (USHORT)Flags;
438  BasicInformation->NameLength = (USHORT)NameLength;
439  RequiredLength += NameLength + sizeof(WCHAR);
440  }
441  break;
442 
443  /* Caller requested info about an Atom Table */
445 
446  /* Size check */
448  if (RequiredLength > AtomInformationLength)
449  {
450  /* Fail */
451  DPRINT1("Buffer too small\n");
453  _SEH2_LEAVE;
454  }
455 
456  /* Query the data */
458  (AtomInformationLength - RequiredLength) /
459  sizeof(RTL_ATOM),
460  &TableInformation->NumberOfAtoms,
461  TableInformation->Atoms);
462  if (NT_SUCCESS(Status))
463  {
464  /* Update the return length */
465  RequiredLength += TableInformation->NumberOfAtoms * sizeof(RTL_ATOM);
466  }
467  break;
468 
469  /* Caller was on crack */
470  default:
471 
472  /* Unrecognized class */
474  break;
475  }
476 
477  /* Return the required size */
478  if (ReturnLength != NULL)
479  {
481  }
482  }
484  {
486  }
487  _SEH2_END;
488 
489  /* Return to caller */
490  return Status;
491 }
492 
493 /* EOF */
#define ProbeForWriteUlong(Ptr)
Definition: probe.h:36
#define TAG_ATOM
Definition: atom.c:16
IN CINT OUT PVOID IN ULONG OUT PULONG ReturnLength
Definition: dumpinfo.c:39
#define IN
Definition: typedefs.h:38
NTSTATUS NTAPI NtQueryInformationAtom(RTL_ATOM Atom, ATOM_INFORMATION_CLASS AtomInformationClass, PVOID AtomInformation, ULONG AtomInformationLength, PULONG ReturnLength)
Definition: atom.c:370
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
unsigned short RTL_ATOM
Definition: atom.c:42
#define STATUS_INFO_LENGTH_MISMATCH
Definition: udferr_usr.h:133
NTSTATUS NTAPI NtDeleteAtom(IN RTL_ATOM Atom)
Definition: atom.c:208
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define ProbeForWriteUshort(Ptr)
Definition: probe.h:34
uint16_t * PWSTR
Definition: typedefs.h:54
LONG NTSTATUS
Definition: precomp.h:26
KPROCESSOR_MODE NTAPI ExGetPreviousMode(VOID)
Definition: sysinfo.c:2966
_Out_ RTL_ATOM * Atom
Definition: class.h:54
#define WCHAR
Definition: msvc.h:43
VOID NTAPI ProbeForWrite(IN PVOID Address, IN SIZE_T Length, IN ULONG Alignment)
Definition: exintrin.c:143
#define PAGED_CODE()
Definition: video.h:57
_SEH2_TRY
Definition: create.c:4250
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
#define UNICODE_NULL
NTSTATUS NTAPI RtlQueryAtomInAtomTable(PRTL_ATOM_TABLE AtomTable, RTL_ATOM Atom, PULONG RefCount, PULONG PinCount, PWSTR AtomName, PULONG NameLength)
Definition: atom.c:600
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
smooth NULL
Definition: ftsmooth.c:416
enum _ATOM_INFORMATION_CLASS ATOM_INFORMATION_CLASS
_In_ KPROCESSOR_MODE PreviousMode
Definition: sefuncs.h:103
#define _SEH2_YIELD(STMT_)
Definition: pseh2_64.h:8
NTSTATUS NTAPI RtlDeleteAtomFromAtomTable(IN PRTL_ATOM_TABLE AtomTable, IN RTL_ATOM Atom)
Definition: atom.c:426
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:24
#define RTL_MAXIMUM_ATOM_LENGTH
Definition: rtltypes.h:36
#define STATUS_ACCESS_DENIED
Definition: udferr_usr.h:145
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
NTSTATUS NTAPI NtAddAtom(IN PWSTR AtomName, IN ULONG AtomNameLength, OUT PRTL_ATOM Atom)
Definition: atom.c:88
VOID NTAPI ProbeForRead(IN CONST VOID *Address, IN SIZE_T Length, IN ULONG Alignment)
Definition: exintrin.c:102
#define STATUS_INVALID_INFO_CLASS
Definition: ntstatus.h:226
NTSTATUS NTAPI RtlCreateAtomTable(IN ULONG TableSize, IN OUT PRTL_ATOM_TABLE *AtomTable)
Definition: atom.c:150
struct atom_table ** PRTL_ATOM_TABLE
Definition: atom.c:43
unsigned short * PRTL_ATOM
Definition: atom.c:42
Status
Definition: gdiplustypes.h:24
PRTL_ATOM_TABLE GlobalAtomTable
Definition: atom.c:27
_SEH2_END
Definition: create.c:4424
LONG NTAPI ExSystemExceptionFilter(VOID)
Definition: harderr.c:351
unsigned short USHORT
Definition: pedump.c:61
_In_ ULONG _Out_opt_ PULONG RequiredLength
Definition: wmifuncs.h:29
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:254
NTSTATUS NTAPI NtFindAtom(IN PWSTR AtomName, IN ULONG AtomNameLength, OUT PRTL_ATOM Atom)
Definition: atom.c:245
unsigned int * PULONG
Definition: retypes.h:1
PRTL_ATOM_TABLE NTAPI ExpGetGlobalAtomTable(VOID)
Definition: atom.c:44
#define DPRINT1
Definition: precomp.h:8
#define OUT
Definition: typedefs.h:39
unsigned int ULONG
Definition: retypes.h:1
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
NTSTATUS NTAPI RtlQueryAtomListInAtomTable(IN PRTL_ATOM_TABLE AtomTable, IN ULONG MaxAtomCount, OUT ULONG *AtomCount, OUT RTL_ATOM *AtomList)
Definition: atom.c:704
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:12
#define _SEH2_LEAVE
Definition: filesup.c:20
WCHAR * LPWSTR
Definition: xmlstorage.h:184
NTSTATUS NTAPI RtlAddAtomToAtomTable(IN PRTL_ATOM_TABLE AtomTable, IN PWSTR AtomName, OUT PRTL_ATOM Atom)
Definition: atom.c:308
return STATUS_SUCCESS
Definition: btrfs.c:2725
NTSTATUS NTAPI RtlLookupAtomInAtomTable(IN PRTL_ATOM_TABLE AtomTable, IN PWSTR AtomName, OUT PRTL_ATOM Atom)
Definition: atom.c:495